@font-face {
  font-family: "Noto Nastaliq Urdu";
  src: url("/static/fonts/NotoNastaliqUrdu.ttf") format("truetype");
  font-weight: 400 700;
  font-display: swap;
}

:root {
  color-scheme: light;
  --surface-0: #f4f4f1;
  --surface-1: #fcfcfb;
  --surface-2: #ecebe7;
  --border: #dddcd6;
  --grid: #e6e5e0;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #77766f;
  --accent: #0f6b3a;
  --accent-hover: #0b5a30;
  --accent-soft: #e2f1e8;
  --on-accent: #ffffff;
  --series-1: #2a78d6;
  --good: #0f7b3e;
  --good-soft: #e3f4e8;
  --bad: #c0362c;
  --bad-soft: #fbe9e7;
  --warn: #9a6400;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 4%);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #252523;
    --border: #34342f;
    --grid: #2c2c29;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #9a998f;
    --accent: #3fa96c;
    --accent-hover: #52bb7e;
    --accent-soft: #1d3326;
    --on-accent: #08130c;
    --series-1: #3987e5;
    --good: #4cc27b;
    --good-soft: #17301f;
    --bad: #ec6f64;
    --bad-soft: #3a1c19;
    --warn: #e0a83a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--surface-0);
  color: var(--text-primary);
  line-height: 1.5;
  font-size: 16px;
}

.ur {
  font-family: "Noto Nastaliq Urdu", serif;
  direction: rtl;
  unicode-bidi: isolate;
  line-height: 2.1;
}

a { color: var(--accent); }

h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }

/* --- Layout ----------------------------------------------------------------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .4rem 1rem;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}
.topbar[hidden] { display: none; }
.brand { font-weight: 700; color: var(--text-primary); text-decoration: none; font-size: 1.1rem; }
.brand .ur { font-size: 1.3rem; line-height: 1.6; }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.topbar nav a {
  padding: .4rem .7rem; border-radius: 8px; text-decoration: none;
  color: var(--text-secondary); font-weight: 500;
}
.topbar nav a:hover { background: var(--surface-2); }
.topbar nav a.active { color: var(--accent); background: var(--accent-soft); }

main { max-width: 760px; margin: 0 auto; padding: 1.25rem 1rem 4rem; outline: none; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}

.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.row { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.stack > * + * { margin-top: .75rem; }

/* --- Forms & buttons ------------------------------------------------------ */

button, .btn {
  font: inherit; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .65rem 1.2rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
  font-weight: 600; text-decoration: none;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:focus-visible, .btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
button:disabled { opacity: .5; cursor: not-allowed; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.primary:hover { background: var(--accent-hover); }
.danger { color: var(--bad); }
.small { padding: .35rem .7rem; font-size: .9rem; }
.big { padding: .9rem 1.6rem; font-size: 1.1rem; width: 100%; }

label { display: block; font-weight: 600; margin-bottom: .25rem; }
input, select, textarea {
  font: inherit; width: 100%;
  padding: .6rem .75rem; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
}
textarea { min-height: 4rem; }
input.ur, textarea.ur { font-size: 1.3rem; line-height: 2; }
.form { max-width: 380px; margin: 2rem auto; }
.form .field { margin-bottom: 1rem; }
.error { color: var(--bad); min-height: 1.5em; }

/* --- Course map ---------------------------------------------------------------- */

.goal-card { display: flex; gap: 1.25rem; align-items: center; }
.ring { flex: none; }
.ring circle { fill: none; stroke-width: 8; }
.ring .track { stroke: var(--surface-2); }
.ring .fill { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset .6s; }
.ring text { fill: var(--text-primary); font-weight: 700; font-size: 15px; }

.streak { font-size: 1.1rem; font-weight: 700; }

.stage-title { margin: 1.5rem 0 .5rem; font-size: 1.05rem; color: var(--text-secondary); }
.lesson-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
.lesson-link {
  display: flex; align-items: center; gap: .9rem;
  padding: .6rem .9rem; border-radius: 10px;
  background: var(--surface-1); border: 1px solid var(--border);
  color: var(--text-primary); text-decoration: none;
}
.lesson-link:hover { border-color: var(--accent); }
.lesson-link .num {
  flex: none; width: 2.2rem; height: 2.2rem; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: .9rem;
  background: var(--surface-2); color: var(--text-secondary);
}
.lesson-link.done .num { background: var(--good-soft); color: var(--good); }
.lesson-link.open .num { background: var(--accent); color: var(--on-accent); }
.lesson-link.locked { opacity: .5; pointer-events: none; }
.lesson-link .title { flex: 1; font-weight: 600; }
.lesson-link .ur { font-size: 1.15rem; color: var(--text-secondary); line-height: 1.8; }

/* --- Lesson: intro & cards --------------------------------------------------- */

.intro { font-size: 1.02rem; }
.intro .ur { font-size: 1.25em; }
.intro table.grammar { border-collapse: collapse; margin: .75rem 0; width: 100%; }
.intro table.grammar td, .intro table.grammar th {
  border-bottom: 1px solid var(--border); padding: .3rem .5rem; text-align: start; vertical-align: middle;
}
.intro table.grammar td.ur { font-size: 1.3rem; text-align: right; }

.progress-bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin-bottom: 1rem; }
.progress-bar > div { height: 100%; background: var(--accent); transition: width .3s; }

.flash { text-align: center; padding: 1.5rem 1rem; }
.flash .big-ur { font-size: 3.2rem; line-height: 2.2; display: block; }
.flash .big-ur.letter { font-size: 5rem; line-height: 1.8; }
.flash .de { font-size: 1.3rem; font-weight: 600; }
.flash .note { color: var(--text-secondary); margin-top: .5rem; }
.forms { display: flex; justify-content: center; gap: 1.25rem; flex-direction: row-reverse; margin: .5rem 0; }
.forms div { text-align: center; }
.forms .ur { font-size: 2.2rem; display: block; line-height: 2; }
.forms small { color: var(--text-muted); }
.example { margin-top: .75rem; padding: .5rem; border-radius: 8px; background: var(--surface-2); }
.example .ur { font-size: 1.6rem; }

.icon { display: inline-flex; line-height: 0; }
h1 .ur.subtitle { display: block; font-size: .85em; color: var(--text-secondary); line-height: 1.9; }

.audio-btn {
  border-radius: 50%; width: 3rem; height: 3rem; padding: 0; font-size: 1.3rem;
  background: var(--accent-soft); border-color: transparent; color: var(--accent);
}
.audio-btn.large { width: 5rem; height: 5rem; font-size: 2rem; }
.audio-btn.playing { background: var(--accent); color: var(--on-accent); }

/* --- Exercises --------------------------------------------------------------- */

.ex-prompt { text-align: center; margin: .5rem 0 1.25rem; }
.ex-prompt .ur { font-size: 2.4rem; display: block; line-height: 2.2; }
.ex-prompt .de { font-size: 1.35rem; font-weight: 600; }
.ex-instr { color: var(--text-muted); text-align: center; font-size: .95rem; margin-bottom: .25rem; }

.options { display: grid; gap: .6rem; grid-template-columns: 1fr 1fr; }
.options button { min-height: 3.6rem; font-weight: 500; white-space: normal; }
.options button.ur { font-size: 1.5rem; line-height: 1.9; font-weight: 400; }
.options button.correct { background: var(--good-soft); border-color: var(--good); color: var(--good); }
.options button.wrong { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }

.type-display {
  min-height: 4rem; padding: .25rem .75rem; margin-bottom: .75rem; border-radius: 10px;
  border: 2px solid var(--border); background: var(--surface-0);
  font-size: 2rem; line-height: 2; text-align: right;
}
.type-display.correct { border-color: var(--good); background: var(--good-soft); }
.type-display.wrong { border-color: var(--bad); background: var(--bad-soft); }
.keyboard { display: grid; gap: .35rem; margin-bottom: .75rem; }
.keys { display: grid; grid-template-columns: repeat(8, 1fr); gap: .3rem; }
.keys.marks { grid-template-columns: repeat(4, 1fr) 2fr 2fr; }
.key {
  padding: 0; min-height: 2.8rem; border-radius: 8px; font-weight: 400;
  font-family: "Noto Nastaliq Urdu", serif; font-size: 1.35rem; line-height: 1;
}
.key.wide { font-family: inherit; font-size: 1.2rem; }
.type-actions { justify-content: space-between; }

.build-answer, .build-bank {
  display: flex; flex-wrap: wrap; gap: .5rem; direction: rtl;
  min-height: 4.2rem; padding: .5rem; border-radius: 10px;
}
.build-answer { border: 2px dashed var(--border); margin-bottom: .75rem; }
.build-bank { justify-content: center; }
.token { font-family: "Noto Nastaliq Urdu", serif; font-size: 1.4rem; line-height: 1.9; padding: .2rem .8rem; font-weight: 400; }

.gap-sentence { font-size: 2rem; }
.gap-sentence .blank { display: inline-block; min-width: 3em; height: 1em; border-bottom: 3px solid var(--accent); vertical-align: baseline; }

.trace-wrap { display: flex; justify-content: center; }
.trace-canvas {
  --trace-guide: #d9d8d2; --trace-ink: #0f6b3a; --trace-outline: #8a8981;
  touch-action: none; border-radius: 12px; background: var(--surface-0);
  border: 1px solid var(--border); cursor: crosshair;
}
.trace-canvas.done { pointer-events: none; }
@media (prefers-color-scheme: dark) {
  .trace-canvas { --trace-guide: #3a3a36; --trace-ink: #3fa96c; --trace-outline: #9a998f; }
}
.trace-msg { text-align: center; margin: .5rem 0; min-height: 1.5em; }
.trace-actions { justify-content: center; }

.feedback {
  margin-top: 1rem; padding: .9rem 1rem; border-radius: 10px;
  display: flex; gap: 1rem; align-items: center; justify-content: space-between; flex-wrap: wrap;
}
.feedback.good { background: var(--good-soft); color: var(--good); }
.feedback.bad { background: var(--bad-soft); color: var(--bad); }
.feedback .solution { color: var(--text-primary); }
.feedback .solution .ur { font-size: 1.4rem; }

.result { text-align: center; }
.result .score { font-size: 3rem; font-weight: 800; }

/* --- Stats ------------------------------------------------------------------- */

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; }
.tile .label { color: var(--text-secondary); font-size: .9rem; }
.tile .value { font-size: 1.8rem; font-weight: 700; color: var(--text-primary); }
.tile .sub { color: var(--text-muted); font-size: .85rem; }

.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .bar { fill: var(--series-1); }
.chart .bar-hit { fill: transparent; cursor: default; }
.chart .bar-hit:hover + .bar, .chart .bar.hover { opacity: .8; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .goal-line { stroke: var(--text-muted); stroke-width: 1; }
.chart .axis-label { fill: var(--text-muted); font-size: 11px; }
.chart .goal-label { fill: var(--text-secondary); font-size: 11px; }
.tooltip {
  position: absolute; pointer-events: none; transform: translate(-50%, -100%);
  background: var(--surface-1); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: 8px; padding: .35rem .6rem; font-size: .85rem; white-space: nowrap; color: var(--text-primary);
}
.tooltip[hidden] { display: none; }
details.table-view summary { cursor: pointer; color: var(--text-secondary); margin-top: .5rem; }
table.data { border-collapse: collapse; width: 100%; font-size: .9rem; margin-top: .5rem; }
table.data td, table.data th { border-bottom: 1px solid var(--border); padding: .25rem .5rem; text-align: start; }
table.data td.num, table.data th.num { text-align: end; font-variant-numeric: tabular-nums; }

/* --- Admin ------------------------------------------------------------------- */

.user-row { display: flex; gap: .75rem; align-items: center; justify-content: space-between; padding: .6rem 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .8rem; font-weight: 600; background: var(--surface-2); color: var(--text-secondary); }
.badge.pending { background: #fff1d6; color: #7a4f00; }
.badge.admin { background: var(--accent-soft); color: var(--accent); }
@media (prefers-color-scheme: dark) { .badge.pending { background: #3a2c10; color: #e8b85a; } }
.item-row { padding: .6rem 0; border-bottom: 1px solid var(--border); }
.item-row .ur { font-size: 1.3rem; }
.tabs { display: flex; gap: .5rem; margin-bottom: 1rem; }
.tabs button.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

#toast {
  position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%);
  background: var(--text-primary); color: var(--surface-1); padding: .6rem 1.1rem; border-radius: 10px;
  opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 90vw;
}
#toast.show { opacity: 1; }

@media (max-width: 520px) {
  .topbar { flex-direction: column; align-items: stretch; gap: 0; }
  .topbar nav { justify-content: space-between; }
  .topbar nav a { padding: .35rem .5rem; font-size: .95rem; }
  .options { grid-template-columns: 1fr; }
  .flash .big-ur { font-size: 2.6rem; }
}
