:root {
  --bg: #fafaf8;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --cell-border: #c9c9c4;
  --cell-bg: #ffffff;
  --cell-filled: #f0f0ea;
  --pair-bg: #4a5cc5;
  --pair-fg: #ffffff;
  --locked-bg: #4caf6d;
  --locked-fg: #ffffff;
  --hint-bg: #d9a520;
  --hint-fg: #ffffff;
  --tile-bg: #e4e4dd;
  --tile-fg: #1a1a1a;
  --accent: #4a5cc5;
  --dialog-bg: #ffffff;
  --shadow: rgba(0, 0, 0, 0.15);
  --cursor: #4a5cc5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16161a;
    --fg: #eaeaea;
    --muted: #9a9aa0;
    --cell-border: #3c3c44;
    --cell-bg: #1f1f26;
    --cell-filled: #2a2a33;
    --pair-bg: #5b6ee0;
    --locked-bg: #3f9e62;
    --hint-bg: #c39418;
    --tile-bg: #34343e;
    --tile-fg: #eaeaea;
    --accent: #7c8cf0;
    --dialog-bg: #202028;
    --shadow: rgba(0, 0, 0, 0.5);
    --cursor: #8b9af5;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { height: 100%; }

body {
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 32px;
}

header {
  width: 100%;
  max-width: 520px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--cell-border);
}

h1 {
  font-size: 1.4rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

nav { display: flex; gap: 6px; }

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  border: 1px solid var(--cell-border);
  background: transparent;
  color: var(--fg);
  font-size: 1rem;
  cursor: pointer;
}
.icon-btn:hover { background: var(--cell-filled); }

#banner[hidden] { display: none; }

#banner {
  width: 100%;
  max-width: 520px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 16px;
  background: var(--accent);
  color: #fff;
  font-size: 0.9rem;
}
#banner button {
  border: 1px solid rgba(255, 255, 255, 0.6);
  background: transparent;
  color: #fff;
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
}

main {
  width: 100%;
  max-width: 520px;
  padding: 12px 12px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

#tagline {
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
}
#tagline strong { color: var(--accent); letter-spacing: 0.05em; }

#status { text-align: center; color: var(--muted); font-size: 0.9rem; }

/* ---- board ---- */

#board {
  display: flex;
  flex-direction: column;
  gap: 6px;
  --cell: min(44px, calc((100vw - 24px - 8 * 5px) / 9));
}

.row {
  display: flex;
  gap: 5px;
  justify-content: center;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell) * 0.5);
  font-weight: 700;
  text-transform: uppercase;
  border: 2px solid var(--cell-border);
  border-radius: 6px;
  background: var(--cell-bg);
  color: var(--fg);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  padding: 0;
  font-family: inherit;
}
.cell.filled { background: var(--cell-filled); }
.cell.pair {
  background: var(--pair-bg);
  border-color: var(--pair-bg);
  color: var(--pair-fg);
  cursor: default;
}
.cell.hinted {
  background: var(--hint-bg);
  border-color: var(--hint-bg);
  color: var(--hint-fg);
  cursor: default;
}
.cell.cursor { border-color: var(--cursor); box-shadow: 0 0 0 2px var(--cursor) inset; }
.row.locked .cell {
  background: var(--locked-bg);
  border-color: var(--locked-bg);
  color: var(--locked-fg);
  cursor: pointer;
  animation: pop 0.25s ease;
}
.row.locked .cell:hover { filter: brightness(0.92); }
.row.shake { animation: shake 0.4s ease; }

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.08); }
  100% { transform: scale(1); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

/* ---- pool ---- */

#pool {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  max-width: 420px;
  min-height: 44px;
}

.tile {
  width: 40px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
  font-weight: 700;
  text-transform: uppercase;
  border: none;
  border-radius: 6px;
  background: var(--tile-bg);
  color: var(--tile-fg);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 2px 0 var(--shadow);
  font-family: inherit;
}
.tile:active { transform: translateY(1px); box-shadow: none; }
.tile.used {
  background: transparent;
  border: 1px dashed var(--cell-border);
  color: transparent;
  box-shadow: none;
  cursor: default;
}

/* ---- actions & buttons ---- */

.actions { display: flex; gap: 10px; }

.actions button,
dialog button {
  padding: 10px 22px;
  font-size: 1rem;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid var(--cell-border);
  background: var(--cell-bg);
  color: var(--fg);
  cursor: pointer;
  font-family: inherit;
}
.actions button:disabled { opacity: 0.4; cursor: default; }
button.primary, #btn-check:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ---- dialogs ---- */

dialog {
  border: none;
  border-radius: 14px;
  background: var(--dialog-bg);
  color: var(--fg);
  padding: 22px;
  max-width: min(420px, 90vw);
  width: 100%;
  box-shadow: 0 12px 40px var(--shadow);
  margin: auto;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin-bottom: 12px; font-size: 1.2rem; }
dialog p, dialog ul { margin-bottom: 10px; line-height: 1.45; font-size: 0.95rem; }
dialog ul { padding-left: 20px; }
dialog form { margin-top: 12px; text-align: right; }
dialog .actions { margin-top: 12px; justify-content: flex-end; }
dialog .actions form { margin-top: 0; }

.example { margin: 10px 0; }
.ex-row { display: flex; gap: 4px; margin-bottom: 8px; }
.ex-cell {
  width: 30px; height: 30px;
  border: 2px solid var(--cell-border);
  border-radius: 5px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.9rem;
}
.ex-cell.pair { background: var(--pair-bg); border-color: var(--pair-bg); color: var(--pair-fg); }
.ex-pool { color: var(--muted); font-size: 0.9rem; }

/* ---- stats ---- */

#stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
  margin: 8px 0;
}
.stat-num { font-size: 1.6rem; font-weight: 700; }
.stat-label { font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

/* ---- archive ---- */

#archive-list {
  max-height: 50vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.archive-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--cell-border);
  border-radius: 8px;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  font-size: 0.95rem;
  font-family: inherit;
  text-align: left;
}
.archive-item:hover { background: var(--cell-filled); }
.archive-item .date { color: var(--muted); font-size: 0.85rem; }
.archive-item .done { color: var(--locked-bg); font-weight: 700; }

/* ---- win dialog / toast ---- */

#win-detail {
  white-space: pre-line;
  text-align: center;
  line-height: 1.6;
  font-size: 1.1rem;
  letter-spacing: 0.15em;
}

#win-stats {
  display: flex;
  gap: 18px;
  justify-content: center;
  margin: 10px 0;
}
#win-stats div { text-align: center; }

#toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 0.9rem;
  box-shadow: 0 4px 16px var(--shadow);
  z-index: 10;
}
