:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #16181d;
  --muted: #5d6472;
  --line: #e3e6ee;
  --brand: #4f46e5;
  --brand-ink: #ffffff;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --gold: #b45309;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 16px;
  --A1: #16a34a; --A2: #0d9488; --B1: #2563eb; --B2: #7c3aed; --C1: #db2777; --C2: #dc2626;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117; --card: #181b24; --text: #eceef3; --muted: #9aa3b2; --line: #2a2f3c;
    --brand: #818cf8; --brand-ink: #0f1117; --ok: #4ade80; --ok-bg: #12351f; --bad: #f87171; --bad-bg: #3b1515;
    --gold: #fbbf24; --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Bengali", "Hind Siliguri", sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
button, input { font: inherit; color: inherit; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.err { color: var(--bad); min-height: 1.2em; margin: .25rem 0; font-size: .9rem; }
[lang="bn"], .bn { font-family: "Noto Sans Bengali", "Hind Siliguri", "Nirmala UI", "Vrinda", sans-serif; }

/* Header */
.top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--text); font-size: 1.05rem; }
.stats { display: flex; gap: 6px; }
.pill { background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; font-size: .9rem; font-variant-numeric: tabular-nums; }

/* Level chips */
.levels { display: flex; gap: 8px; padding: 12px 16px 4px; overflow-x: auto; scrollbar-width: none; }
.levels::-webkit-scrollbar { display: none; }
.levels[hidden] { display: none; }
.chip {
  flex: 0 0 auto; border: 2px solid var(--line); background: var(--card); border-radius: 999px;
  padding: 6px 14px; font-weight: 700; cursor: pointer; min-width: 52px;
}
.chip[aria-pressed="true"] { border-color: var(--lv); color: var(--lv); background: color-mix(in srgb, var(--lv) 10%, var(--card)); }
.chip small { display: block; font-weight: 500; font-size: .7rem; color: var(--muted); }

main { max-width: 640px; margin: 0 auto; padding: 12px 16px 24px; outline: none; }
h1 { font-size: 1.35rem; margin: .5rem 0 .25rem; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 10px; align-items: center; }
.row > .grow { flex: 1; }
.between { justify-content: space-between; }

.btn {
  appearance: none; border: 0; border-radius: 12px; background: var(--brand); color: var(--brand-ink);
  padding: 12px 18px; font-weight: 700; cursor: pointer; min-height: 48px; transition: transform .06s;
  /* Links styled as buttons must behave like blocks, or they sit side by side and overlap on phones. */
  display: inline-flex; align-items: center; justify-content: center; text-align: center;
  text-decoration: none; line-height: 1.25;
}
.btn:active { transform: scale(.98); }
.btn.ghost { background: transparent; color: var(--text); border: 2px solid var(--line); }
.btn.ok { background: var(--ok); color: #fff; }
.btn.bad { background: var(--bad); color: #fff; }
.btn.wide { display: flex; width: 100%; }
.btn[disabled] { opacity: .5; cursor: default; }
.icon-btn { border: 2px solid var(--line); background: var(--card); border-radius: 12px; min-width: 48px; min-height: 48px; cursor: pointer; font-size: 1.25rem; }

/* Progress */
.bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--lv, var(--brand)); border-radius: 99px; transition: width .3s; }

/* Flashcard */
.flash { perspective: 1200px; min-height: 300px; cursor: pointer; }
.flash-inner { position: relative; min-height: 300px; transition: transform .45s; transform-style: preserve-3d; }
.flash.flipped .flash-inner { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; gap: 8px;
}
.face.back { transform: rotateY(180deg); }
.word { font-size: clamp(1.8rem, 8vw, 2.6rem); font-weight: 800; letter-spacing: -.01em; word-break: break-word; }
.pos { font-style: italic; color: var(--muted); }
.bn-mean { font-size: 1.35rem; font-weight: 700; }
.example { color: var(--muted); }
.example b, .hl { color: var(--lv, var(--brand)); }
.hint { font-size: .85rem; color: var(--muted); }

/* Options */
.opts { display: grid; gap: 10px; }
.opt {
  text-align: left; border: 2px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 14px 16px; cursor: pointer; min-height: 52px; line-height: 1.35;
}
.opt:hover:not([disabled]) { border-color: var(--brand); }
.opt.right { border-color: var(--ok); background: var(--ok-bg); }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); }
.opt[disabled] { cursor: default; }
.qtag { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }
.prompt { font-size: 1.3rem; font-weight: 700; }

/* Spelling */
.spell-input {
  width: 100%; font-size: 1.5rem; text-align: center; letter-spacing: .08em; padding: 12px; border-radius: 12px;
  border: 2px solid var(--line); background: var(--bg);
}
.spell-input:focus { outline: none; border-color: var(--brand); }
.slots { font-family: ui-monospace, monospace; letter-spacing: .35em; color: var(--muted); text-align: center; }
.feedback { border-radius: 12px; padding: 12px 14px; font-weight: 600; }
.feedback.ok { background: var(--ok-bg); color: var(--ok); }
.feedback.bad { background: var(--bad-bg); color: var(--bad); }

/* Results */
.score-big { font-size: 3rem; font-weight: 800; text-align: center; line-height: 1; }
.grid-emoji { font-size: 2rem; text-align: center; letter-spacing: .15em; }
.res-list { list-style: none; padding: 0; margin: 0; }
.res-list li { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.res-list li:last-child { border: 0; }

/* Leaderboard */
.seg { display: flex; background: var(--line); border-radius: 12px; padding: 3px; gap: 3px; overflow-x: auto; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 9px; padding: 8px 10px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.seg button[aria-pressed="true"] { background: var(--card); box-shadow: var(--shadow); }
.board { list-style: none; margin: 0; padding: 0; }
.board li { display: grid; grid-template-columns: 40px 1fr auto; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--line); }
.board li.me { background: color-mix(in srgb, var(--brand) 12%, transparent); border-radius: 10px; }
.rank { font-weight: 800; text-align: center; font-variant-numeric: tabular-nums; }
.nick { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pts { font-weight: 800; font-variant-numeric: tabular-nums; }
.pts small { display: block; font-weight: 500; color: var(--muted); font-size: .75rem; text-align: right; }

.level-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.level-tile { border: 2px solid var(--line); border-radius: 14px; padding: 12px; background: var(--card); cursor: pointer; text-align: left; }
.level-tile strong { color: var(--lv); font-size: 1.2rem; }

/* Bottom tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs a { display: flex; flex-direction: column; align-items: center; padding: 8px 0 6px; font-size: .72rem; color: var(--muted); text-decoration: none; font-weight: 600; min-height: 56px; }
.tabs a span { font-size: 1.3rem; line-height: 1.2; }
.tabs a[aria-current="page"] { color: var(--brand); }

dialog { border: 0; border-radius: var(--radius); padding: 20px; width: min(92vw, 420px); background: var(--card); color: var(--text); }
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog input { width: 100%; padding: 12px; border: 2px solid var(--line); border-radius: 12px; background: var(--bg); font-size: 1.1rem; }
dialog .row { justify-content: flex-end; margin-top: 12px; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; z-index: 9; max-width: 90vw;
}
@media (min-width: 720px) {
  .tabs { max-width: 640px; margin: 0 auto; border: 1px solid var(--line); border-bottom: 0; border-radius: 16px 16px 0 0; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
