:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #14202e;
  --muted: #5d6b7a;
  --line: #dde3ea;
  --brand: #0b3a6e;
  --brand-ink: #ffffff;
  --accent: #ffd400;
  --good: #1f8a4c;
  --bad: #c62828;
  --sign-bg: #ffffff;
  --shadow: 0 1px 2px rgba(16, 32, 48, .06), 0 4px 16px rgba(16, 32, 48, .08);
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e141b;
    --surface: #18212b;
    --text: #e8edf2;
    --muted: #9aa8b6;
    --line: #2a3644;
    --brand: #133f6f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .3);
    /* signs stay on a light panel so their colours read correctly */
    --sign-bg: #f4f6f8;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- top bar ---------- */
.bar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px;
  background: var(--brand); color: var(--brand-ink);
}
.bar h1 { flex: 1; margin: 0; font-size: 18px; line-height: 1.2; font-weight: 700; min-width: 0; }
.bar h1 small { display: block; font-size: 12.5px; font-weight: 500; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.iconbtn {
  border: 0; background: rgba(255,255,255,.12); color: inherit;
  min-width: 40px; height: 40px; border-radius: 12px; padding: 0 10px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-size: 14px; font-weight: 600; cursor: pointer;
}
.iconbtn:active { background: rgba(255,255,255,.25); }
.iconbtn svg { width: 22px; height: 22px; }
.iconbtn.on { background: var(--accent); color: #1b1b1b; }

/* ---------- home ---------- */
.wrap { max-width: 720px; margin: 0 auto; padding: 14px 16px calc(env(safe-area-inset-bottom) + 28px); }
.hero { padding: 6px 2px 12px; }
.hero p { margin: 0; color: var(--muted); font-size: 14px; }
.allbtn {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 10px;
  border: 0; border-radius: 14px; padding: 14px; margin-bottom: 14px;
  background: var(--accent); color: #1b1b1b; font-weight: 700; font-size: 16px; cursor: pointer;
}
.cats { display: grid; gap: 10px; }
.cat {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: left;
  background: var(--surface); border: 0; border-radius: var(--radius); padding: 12px 14px;
  box-shadow: var(--shadow); cursor: pointer;
}
.cat:active { transform: scale(.99); }
.cat .thumb {
  width: 60px; height: 60px; flex: none; border-radius: 12px; background: var(--sign-bg);
  display: flex; align-items: center; justify-content: center; padding: 6px;
}
.cat .thumb img { max-width: 100%; max-height: 100%; }
.cat .meta { flex: 1; min-width: 0; }
.cat .t { font-weight: 700; font-size: 16.5px; }
.cat .s { color: var(--muted); font-size: 13.5px; }
.cat .num { color: var(--muted); font-size: 12px; font-weight: 600; letter-spacing: .02em; }
.prog { height: 5px; background: var(--line); border-radius: 3px; margin-top: 7px; overflow: hidden; }
.prog i { display: block; height: 100%; background: var(--good); border-radius: 3px; }
.foot { color: var(--muted); font-size: 12.5px; margin-top: 22px; line-height: 1.5; }
.foot a { color: inherit; }
.foot button { background: none; border: 0; padding: 0; text-decoration: underline; cursor: pointer; color: inherit; font-size: inherit; }

/* ---------- grid / search results ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 10px; }
.grid > * { min-width: 0; }
.tile {
  background: var(--surface); border: 0; border-radius: 14px; padding: 8px 8px 10px;
  box-shadow: var(--shadow); cursor: pointer; text-align: center; position: relative;
}
.tile .img { background: var(--sign-bg); border-radius: 10px; height: 84px; display: flex; align-items: center; justify-content: center; padding: 6px; }
.tile img { max-width: 100%; max-height: 100%; }
.tile .n { font-size: 11px; color: var(--muted); margin-top: 6px; font-weight: 600; min-height: 1em; }
.tile .l { font-size: 12.5px; line-height: 1.25; margin-top: 7px; }
.tile.known::after {
  content: "✓"; position: absolute; top: 6px; right: 6px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--good); color: #fff; font-size: 12px; line-height: 20px; font-weight: 700;
}
.empty { color: var(--muted); text-align: center; padding: 30px 0; }

/* ---------- deck ---------- */
.deck {
  height: 100dvh; display: flex; flex-direction: column;
  max-width: 720px; margin: 0 auto;
}
.tools { display: flex; gap: 6px; padding: 10px 16px 0; flex-wrap: wrap; align-items: center; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 10px; font-size: 13px; font-weight: 600; cursor: pointer; display: inline-flex; gap: 6px; align-items: center;
}
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.counter { margin-left: auto; align-self: center; color: var(--muted); font-size: 14px; font-weight: 600; }
.stage { flex: 1; min-height: 0; padding: 12px 16px; perspective: 1400px; position: relative; }
.card {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d; transition: transform .45s cubic-bezier(.2, .7, .2, 1);
  cursor: pointer; touch-action: pan-y;
}
.card.flipped { transform: rotateY(180deg); }
.card.dragging { transition: none; }
.face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--surface); border-radius: 24px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.front { padding: 18px; }
.front .signs {
  flex: 1; min-height: 0; background: var(--sign-bg); border-radius: 16px;
  display: grid; gap: 12px; padding: 18px; place-items: center;
}
.front .signs img { max-width: 100%; max-height: 100%; object-fit: contain; min-height: 0; }
.front .signs.n1 img { width: 100%; height: 100%; }
.front .signs.n2 { grid-template-columns: 1fr 1fr; }
.front .signs.n3, .front .signs.n4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.front .signs.many { grid-template-columns: repeat(3, 1fr); grid-auto-rows: 1fr; }
.front .cell { width: 100%; height: 100%; min-height: 0; display: flex; align-items: center; justify-content: center; }
.front .cell img { width: 100%; height: 100%; }
.front .hint { text-align: center; color: var(--muted); font-size: 13.5px; padding-top: 12px; }
.front .peek { text-align: center; padding-top: 12px; }
.front .peek b { display: block; font-size: 19px; }
.front .peek span { color: var(--muted); font-size: 14.5px; }
.back { transform: rotateY(180deg); padding: 18px 18px 14px; }
.back .head { display: flex; gap: 14px; align-items: flex-start; justify-content: space-between; }
.back .mini { width: 74px; height: 74px; flex: none; background: var(--sign-bg); border-radius: 12px; padding: 6px; display: flex; align-items: center; justify-content: center; }
.back .mini img { max-width: 100%; max-height: 100%; }
.back .badge { display: inline-block; background: var(--accent); color: #1b1b1b; font-weight: 800; font-size: 13px; padding: 2px 8px; border-radius: 6px; }
.back h2 { margin: 12px 0 0; font-size: 21px; line-height: 1.2; }
.back .head > div:last-child { flex: 1; min-width: 0; }
.back .en { color: var(--muted); font-size: 15.5px; margin-top: 2px; }
.back .scroll { flex: 1; min-height: 0; overflow-y: auto; margin-top: 14px; -webkit-overflow-scrolling: touch; }
.back .desc { white-space: pre-line; font-size: 15.5px; margin: 0 0 14px; }
.variants { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.variants figure { margin: 0; background: var(--sign-bg); border-radius: 10px; padding: 6px; text-align: center; }
.variants img { width: 100%; height: 64px; object-fit: contain; }
.variants figcaption { font-size: 10.5px; color: #5d6b7a; line-height: 1.2; margin-top: 3px; word-break: break-word; }
.variants-label { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 4px 0 8px; }

.actions {
  display: grid; grid-template-columns: 50px 1fr 1fr 50px; gap: 8px;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 14px);
}
.actions button {
  height: 56px; border-radius: 16px; border: 0; font-weight: 700; font-size: 15.5px; cursor: pointer;
  background: var(--surface); box-shadow: var(--shadow); display: flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap; padding: 0 6px;
}
.actions button:active { transform: scale(.97); }
.actions .no { background: #fdecec; color: var(--bad); }
.actions .yes { background: #e6f4ec; color: var(--good); }
@media (prefers-color-scheme: dark) {
  .actions .no { background: #3a1d1f; color: #ff8a80; }
  .actions .yes { background: #16321f; color: #7bd89c; }
}
.actions svg { width: 22px; height: 22px; flex: none; }
@media (max-width: 360px) { .actions .no svg, .actions .yes svg { display: none; } }
.done { text-align: center; padding: 30px 10px; display: flex; flex-direction: column; justify-content: center; height: 100%; }
.done h2 { margin: 0 0 6px; }
.done p { color: var(--muted); margin: 0 0 18px; }
.done .row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.btn { border: 0; border-radius: 14px; padding: 13px 18px; font-weight: 700; cursor: pointer; background: var(--brand); color: #fff; }
.btn.alt { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

@media (min-width: 700px) {
  .stage { padding: 18px 16px; }
}
@media (max-height: 600px) {
  .actions button { height: 48px; }
}

[data-fit] { min-width: 0; max-width: 100%; }

.langbtn { position: relative; min-width: 46px; }
.langbtn select { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; font-size: 16px; }
