/* ── Tokens ────────────────────────────────────────── */
:root {
  --bg: #f6ede0;
  --bg-tint: #efe2d0;
  --surface: #fffaf3;
  --surface-2: #f3e7d7;
  --ink: #3a2d20;
  --ink-soft: #7d6b57;
  --line: #e0cfb8;
  --grid-line: #00000022;
  --region-line: #46352180;
  --mark: #2b1f15;
  --accent: #d97b34;
  --accent-ink: #fffaf3;
  --danger: #c74a3c;
  --good: #3f8f5f;
  --shadow: 0 1px 2px #6b543a1f, 0 8px 24px #6b543a1a;
  --radius: 16px;

  --rg0: #f3948c;
  --rg1: #f6bd6e;
  --rg2: #dfd45f;
  --rg3: #a6dd86;
  --rg4: #62cbb4;
  --rg5: #8fd0ee;
  --rg6: #92a8e6;
  --rg7: #c3adee;
  --rg8: #ee9ccd;
  --rg9: #d4b391;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1713;
    --bg-tint: #231d18;
    --surface: #2a231d;
    --surface-2: #332a22;
    --ink: #f2e6d6;
    --ink-soft: #b09c84;
    --line: #443930;
    --grid-line: #00000055;
    --region-line: #0f0b08cc;
    --mark: #f4e9da;
    --accent: #e3893f;
    --accent-ink: #231d18;
    --shadow: 0 1px 2px #0006, 0 8px 24px #0005;
    --rg0: #9e4c46;
    --rg1: #9c6f33;
    --rg2: #8a8232;
    --rg3: #5d8c45;
    --rg4: #2f7d6c;
    --rg5: #3a7691;
    --rg6: #4a5c99;
    --rg7: #6b568f;
    --rg8: #944f78;
    --rg9: #806048;
  }
}

:root[data-theme="dark"] {
  --bg: #1b1713;
  --bg-tint: #231d18;
  --surface: #2a231d;
  --surface-2: #332a22;
  --ink: #f2e6d6;
  --ink-soft: #b09c84;
  --line: #443930;
  --grid-line: #00000055;
  --region-line: #0f0b08cc;
  --mark: #f4e9da;
  --accent: #e3893f;
  --accent-ink: #231d18;
  --shadow: 0 1px 2px #0006, 0 8px 24px #0005;

  --rg0: #9e4c46;
  --rg1: #9c6f33;
  --rg2: #8a8232;
  --rg3: #5d8c45;
  --rg4: #2f7d6c;
  --rg5: #3a7691;
  --rg6: #4a5c99;
  --rg7: #6b568f;
  --rg8: #944f78;
  --rg9: #806048;
}

/* ── Base ──────────────────────────────────────────── */
* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { margin: 0; font-weight: 800; letter-spacing: .02em; }

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 0 16px env(safe-area-inset-bottom);
  max-width: 620px;
  margin-inline: auto;
}

/* ── Top bar ───────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) 0 10px;
  min-height: 56px;
}

.topbar-title {
  font-size: 1.05rem;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
}

.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.main { flex: 1; padding-bottom: 24px; }

.view { animation: fade .22s ease both; }

@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

/* ── Home ──────────────────────────────────────────── */
.hero { text-align: center; padding: 18px 0 26px; }

.hero-art { width: 118px; margin: 0 auto 14px; animation: bob 3.4s ease-in-out infinite; }
.hero-art .mascot { width: 100%; height: auto; }

@keyframes bob { 50% { transform: translateY(-7px) rotate(-2deg); } }

.hero-title { font-size: 2rem; }
.hero-sub { margin: 6px 0 0; color: var(--ink-soft); font-size: .95rem; }

.progress-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.progress-line { display: flex; justify-content: space-between; font-size: .88rem; color: var(--ink-soft); margin-bottom: 9px; }
.progress-line strong { color: var(--ink); }

.progress-track { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress-fill { height: 100%; width: 0; border-radius: 99px; background: var(--accent); transition: width .4s ease; }

.menu { display: grid; gap: 10px; margin-top: 20px; }

/* ── Buttons ───────────────────────────────────────── */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  padding: 14px 18px;
  border-radius: 14px;
  cursor: pointer;
  transition: transform .12s ease, background .15s ease;
}

.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.98); }

.btn-primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.06); }
.btn-quiet { background: transparent; border-color: transparent; color: var(--ink-soft); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }

/* ── Level select ──────────────────────────────────── */
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(62px, 1fr));
  gap: 9px;
  padding-top: 4px;
}

.level-cell {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  font-size: 1.02rem;
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow);
  transition: transform .12s ease;
}

.level-cell:active { transform: scale(.95); }
.level-cell.done { background: color-mix(in srgb, var(--accent) 16%, var(--surface)); border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
.level-cell .size { position: absolute; bottom: 5px; font-size: .6rem; font-weight: 600; color: var(--ink-soft); letter-spacing: .02em; }
.level-cell .star { position: absolute; top: 3px; right: 5px; font-size: .68rem; }

/* ── HUD ───────────────────────────────────────────── */
.hud {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

.hud-item {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 10px;
  text-align: center;
}

.hud-label { display: block; font-size: .68rem; color: var(--ink-soft); letter-spacing: .06em; }
.hud-item strong { font-size: 1.02rem; font-variant-numeric: tabular-nums; }

/* ── Board ─────────────────────────────────────────── */
.board-wrap { position: relative; }

.board {
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  aspect-ratio: 1;
  width: 100%;
  border-radius: 14px;
  overflow: hidden;
  border: 2.5px solid var(--region-line);
  background: var(--surface);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: relative;
  appearance: none;
  border: 0;
  padding: 0;
  margin: 0;
  background: var(--rc);
  cursor: pointer;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
}

.cell::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 7px;
  pointer-events: none;
  transition: box-shadow .14s ease;
}

.cell:focus-visible { outline: none; }
.cell:focus-visible::after { box-shadow: 0 0 0 3px var(--ink); }
.cell.bad::after { box-shadow: 0 0 0 3px var(--danger); }
.cell.nudge .pet { animation: nudge .5s ease; }

@keyframes nudge {
  0%, 100% { transform: none; }
  25% { transform: scale(1.14); }
  60% { transform: scale(.96); }
}

.mark {
  width: 42%;
  height: 42%;
  position: relative;
  opacity: 0;
  transform: scale(.4);
  transition: opacity .12s ease, transform .12s ease;
}

.mark::before, .mark::after {
  content: "";
  position: absolute;
  inset: 45% -2%;
  border-radius: 99px;
  background: var(--mark);
}

.mark::before { transform: rotate(45deg); }
.mark::after { transform: rotate(-45deg); }

.pet {
  position: absolute;
  inset: 8%;
  opacity: 0;
  transform: scale(.3) rotate(-18deg);
  transition: opacity .16s ease, transform .22s cubic-bezier(.34, 1.7, .5, 1);
  pointer-events: none;
}

.pet .mascot { filter: drop-shadow(0 1px 1px #0000002e); }

.cell[data-state="1"] .mark { opacity: .62; transform: none; }
.cell[data-state="3"] .mark { opacity: .26; transform: none; }
.cell[data-state="2"] .pet { opacity: 1; transform: none; }

.board-flash {
  position: absolute;
  inset: 0;
  border-radius: 14px;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(circle at 50% 50%, #fff8 0%, #fff0 65%);
}

.board-flash.on { animation: flash .7s ease; }

@keyframes flash { 0% { opacity: 0; } 30% { opacity: 1; } 100% { opacity: 0; } }

.hint-line {
  min-height: 1.35em;
  margin: 12px 2px 0;
  font-size: .88rem;
  color: var(--ink-soft);
  text-align: center;
}

.hint-line.warn { color: var(--danger); }
.hint-line.good { color: var(--good); }

/* ── Toolbar ───────────────────────────────────────── */
.toolbar { display: flex; gap: 9px; margin-top: 14px; }

.tool {
  flex: 1;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: .78rem;
  font-weight: 700;
  border-radius: 14px;
  padding: 10px 4px;
  cursor: pointer;
  display: grid;
  gap: 3px;
  justify-items: center;
}

.tool:active { transform: scale(.96); }
.tool:disabled { opacity: .4; cursor: default; }
.tool svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

/* ── Dialogs ───────────────────────────────────────── */
.sheet {
  border: 0;
  border-radius: 20px;
  padding: 22px;
  max-width: 400px;
  width: calc(100vw - 32px);
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}

.sheet::backdrop { background: #2a1d0f8c; backdrop-filter: blur(2px); }
.sheet h2 { font-size: 1.25rem; margin-bottom: 12px; }
.sheet h3 { font-size: .9rem; color: var(--ink-soft); margin: 18px 0 8px; }

.rules { margin: 0; padding-left: 1.1em; display: grid; gap: 8px; font-size: .9rem; line-height: 1.55; }
.rules b { font-weight: 800; }

.sheet form { margin-top: 20px; }
.sheet form .btn { width: 100%; }

.switch {
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  font-size: .92rem;
}

.switch small { display: block; color: var(--ink-soft); font-size: .76rem; margin-top: 2px; }
.switch input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }

#btn-reset-progress { width: 100%; margin-top: 18px; }

.sheet-win { text-align: center; }
.win-art { width: 96px; margin: 0 auto 10px; animation: pop .5s cubic-bezier(.34, 1.7, .5, 1) both; }
.win-art .mascot { width: 100%; height: auto; }

@keyframes pop { from { transform: scale(.3) rotate(-20deg); opacity: 0; } }

.win-stats { color: var(--ink-soft); font-size: .9rem; margin: 6px 0 0; line-height: 1.7; }
.win-actions { display: grid; gap: 9px; margin-top: 20px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

.size-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; margin-top: 14px; }
.size-grid .btn { padding: 16px 4px; font-size: 1rem; }
.size-grid .btn small { display: block; font-size: .66rem; font-weight: 600; color: var(--ink-soft); margin-top: 3px; }

/* ── Daily challenge card ──────────────────────────── */
.daily-card {
  width: 100%;
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  text-align: left;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: 13px 16px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.daily-card:active { transform: scale(.99); }
.daily-left { display: grid; gap: 2px; }
.daily-tag { font-size: .68rem; letter-spacing: .08em; color: var(--accent); font-weight: 800; }
.daily-left strong { font-size: 1.02rem; }
.daily-right { display: grid; gap: 2px; text-align: right; font-size: .78rem; color: var(--ink-soft); }
.daily-streak { font-weight: 800; color: var(--ink); }

/* ── Lives ─────────────────────────────────────────── */
.lives { display: flex; justify-content: center; gap: 7px; margin-bottom: 11px; }

.lives span {
  width: 17px;
  height: 17px;
  background: var(--danger);
  clip-path: path("M8.5 16S0 10.4 0 5.2A4.8 4.8 0 0 1 8.5 2 4.8 4.8 0 0 1 17 5.2C17 10.4 8.5 16 8.5 16z");
  transition: opacity .25s ease, transform .25s ease;
}

.lives span.gone { opacity: .3; transform: scale(.8); background: var(--ink-soft); }

/* ── Mascot picker ─────────────────────────────────── */
.mascot-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

.mascot-option {
  border: 1.5px solid var(--line);
  background: var(--surface-2);
  border-radius: 13px;
  padding: 8px 2px 6px;
  cursor: pointer;
  display: grid;
  justify-items: center;
  gap: 3px;
  font: inherit;
  font-size: .68rem;
  font-weight: 700;
  color: var(--ink-soft);
}

.mascot-option .mascot { width: 34px; height: 34px; display: block; }
.mascot-option[aria-pressed="true"] { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 15%, var(--surface)); color: var(--ink); }

.mascot { width: 100%; height: 100%; display: block; }
.hero-art .mascot, .win-art .mascot { filter: drop-shadow(0 6px 10px #6b543a2e); }

/* A wrong placement flashes red and clears itself, so nothing to clean up. */
.cell.wrong::after { animation: wrong-ring .9s ease; }
.cell.wrong .mark { animation: shake .4s ease; }

@keyframes wrong-ring {
  0%, 55% { box-shadow: 0 0 0 3px var(--danger); }
  100% { box-shadow: 0 0 0 3px #0000; }
}

@keyframes shake {
  0%, 100% { transform: none; }
  20% { transform: translateX(-3px); }
  45% { transform: translateX(3px); }
  70% { transform: translateX(-2px); }
}

/* A mascot can bring its own cross glyph (see mascots.js). */
.mark.mark-icon { width: 58%; height: 58%; }
.mark.mark-icon::before, .mark.mark-icon::after { display: none; }
.mark svg { width: 100%; height: 100%; display: block; fill: var(--mark); }

.cell[data-state="1"] .mark.mark-icon { opacity: .7; }
.cell[data-state="3"] .mark.mark-icon { opacity: .34; }

/* ── Difficulty tiers ──────────────────────────────── */
:root {
  --t1: #5aa15c;
  --t2: #93a83e;
  --t3: #d8a13a;
  --t4: #dd7a3a;
  --t5: #c9463c;
}

.legend-title { margin: 2px 2px 10px; font-size: .78rem; color: var(--ink-soft); }

.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }

.legend .chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: .7rem;
  font-weight: 700;
  color: var(--ink-soft);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 99px;
  padding: 4px 9px;
}

.legend .chip i, .techniques i {
  width: 8px;
  height: 8px;
  border-radius: 99px;
  flex: none;
}

.level-cell { box-shadow: var(--shadow), inset 0 -3.5px var(--tier, transparent); }

.techniques {
  margin: 0;
  padding-left: 1.2em;
  display: grid;
  gap: 10px;
  font-size: .86rem;
  line-height: 1.55;
}

.techniques b { display: inline-flex; align-items: center; gap: 6px; font-weight: 800; }
.techniques small { display: block; color: var(--ink-soft); font-size: .78rem; margin-top: 1px; }

.sheet-note { margin: 0 0 12px; font-size: .78rem; color: var(--ink-soft); line-height: 1.5; }

/* Cells the hint is pointing at. */
.cell.focus::after { animation: focus-ring 2.4s ease-in-out 3; }

@keyframes focus-ring {
  0%, 100% { box-shadow: 0 0 0 0 #0000; }
  50% { box-shadow: 0 0 0 3px var(--accent); }
}

/* ── Endless-mode pickers ──────────────────────────── */
.size-grid .btn, .tier-grid .btn { position: relative; }
.size-grid .btn[aria-pressed="true"], .tier-grid .btn[aria-pressed="true"] {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, var(--surface));
}

.tier-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 10px; }
.tier-grid .btn { padding: 11px 4px; font-size: .78rem; display: grid; gap: 4px; justify-items: center; }
.tier-grid .btn i { width: 8px; height: 8px; border-radius: 99px; }

/* ── Busy veil ─────────────────────────────────────── */
.veil {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: #2a1d0f8c;
  backdrop-filter: blur(2px);
}

.veil-card {
  background: var(--surface);
  color: var(--ink);
  border-radius: 18px;
  padding: 24px 30px;
  display: grid;
  gap: 12px;
  justify-items: center;
  box-shadow: var(--shadow);
  font-size: .88rem;
  font-weight: 700;
}

.veil-card p { margin: 0; }

.veil-spinner {
  width: 30px;
  height: 30px;
  border-radius: 99px;
  border: 3px solid var(--surface-2);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Lessons ───────────────────────────────────────── */
.lesson-banner {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--tier, var(--accent));
  border-radius: var(--radius);
  padding: 11px 14px;
  margin-bottom: 12px;
}

.lesson-tag { font-size: .66rem; font-weight: 800; letter-spacing: .08em; color: var(--ink-soft); }
.lesson-banner strong { display: block; font-size: 1rem; margin-top: 1px; }
.lesson-banner p { margin: 5px 0 0; font-size: .8rem; line-height: 1.55; color: var(--ink-soft); }

.lesson-list { display: grid; gap: 8px; margin-top: 4px; }

.lesson-row {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  text-align: left;
  font: inherit;
  color: var(--ink);
  background: var(--surface-2);
  border: 1.5px solid var(--line);
  border-radius: 13px;
  padding: 11px 13px;
  cursor: pointer;
}

.lesson-row i { width: 10px; height: 10px; border-radius: 99px; flex: none; }
.lesson-row b { font-size: .92rem; }
.lesson-row small { display: block; color: var(--ink-soft); font-size: .74rem; margin-top: 1px; }
.lesson-row .tick { margin-left: auto; color: var(--good); font-weight: 800; }
.lesson-row.done { border-color: color-mix(in srgb, var(--good) 45%, var(--line)); }

/* ── Leaderboard ───────────────────────────────────── */
.name-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0 14px;
  font-size: .88rem;
  font-weight: 700;
}

.name-row input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-weight: 600;
  color: var(--ink);
  background: var(--surface-2);
  border: 1.5px solid var(--line);
  border-radius: 11px;
  padding: 9px 12px;
}

.name-row input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.board-list { display: grid; gap: 5px; }

.board-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface-2);
  border-radius: 11px;
  padding: 9px 12px;
  font-size: .85rem;
}

.board-row .rank { width: 1.8em; font-weight: 800; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.board-row .who { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.board-row .when { color: var(--ink-soft); font-size: .78rem; }
.board-row .time { font-weight: 800; font-variant-numeric: tabular-nums; }
.board-row .tags { font-size: .72rem; letter-spacing: .05em; }
.board-row.mine { background: color-mix(in srgb, var(--accent) 17%, var(--surface)); }
.board-empty { color: var(--ink-soft); font-size: .82rem; padding: 6px 2px; }

/* ── Campaign switch ───────────────────────────────── */
:root { --t6: #a83a6a; }

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 5px;
  margin-bottom: 14px;
}

.mode-switch button {
  font: inherit;
  font-weight: 800;
  font-size: .9rem;
  color: var(--ink-soft);
  background: transparent;
  border: 0;
  border-radius: 10px;
  padding: 9px 4px;
  cursor: pointer;
  display: grid;
  gap: 1px;
  justify-items: center;
}

.mode-switch button small { font-size: .64rem; font-weight: 600; letter-spacing: .06em; }
.mode-switch button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.star-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 10px; }
.star-grid .btn { padding: 13px 4px; }
.star-grid .btn small { display: block; font-size: .66rem; font-weight: 600; color: var(--ink-soft); margin-top: 3px; }
.star-grid .btn[aria-pressed="true"] { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, var(--surface)); }
