/* ---------- Dhivehi (Thaana) font ----------
   Faruma is bundled in public/fonts (from fonts.mv), so every student device gets it,
   even offline on the classroom Wi-Fi. Noto Sans Thaana is a backup if it fails to load.
   unicode-range keeps these fonts on Thaana text only, so English stays in Nunito and
   Arabic is untouched. */
@font-face {
  font-family: "Dhivehi";
  src: local("Faruma"), url("/fonts/faruma-400.woff2") format("woff2");
  unicode-range: U+0780-07BF;
  font-display: swap;
}
@font-face {
  font-family: "Thaana Fallback";
  src: url("/fonts/noto-sans-thaana-400.woff2") format("woff2");
  font-weight: 400 600;
  unicode-range: U+0780-07BF;
  font-display: swap;
}
@font-face {
  font-family: "Thaana Fallback";
  src: url("/fonts/noto-sans-thaana-700.woff2") format("woff2");
  font-weight: 700 900;
  unicode-range: U+0780-07BF;
  font-display: swap;
}

:root {
  --bg: #0a2e2c;
  --bg2: #0f413d;
  --surface: #ffffff;
  --surface2: #f4f1ea;
  --ink: #12302e;
  --muted: #5f716f;
  --line: #e0dbd0;
  --gold: #e8b04b;
  --gold-ink: #3d2a05;
  --good: #16a36f;
  --bad: #d9475a;
  --a0: #12a37f; --a1: #e25563; --a2: #2b7bd6; --a3: #e8a92f; --a4: #8a5cd1; --a5: #f0805a;
  --radius: 14px;
  --shadow: 0 6px 24px rgba(0,0,0,.18);
  font-family: "Dhivehi", "Thaana Fallback", "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: radial-gradient(1200px 600px at 10% -10%, #155a53 0%, transparent 60%),
              radial-gradient(900px 500px at 110% 110%, #1b4f3a 0%, transparent 60%), var(--bg);
  color: #fff; min-height: 100vh;
}
body.light { background: var(--surface2); color: var(--ink); }
a { color: inherit; }
button, input, select, textarea { font: inherit; }
.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 20px; background: var(--bg); color: #fff; position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 900; font-size: 1.35rem; text-decoration: none; letter-spacing: .3px; }
.brand .logo { width: 34px; height: 34px; border-radius: 10px; background: var(--gold); color: var(--gold-ink); display: grid; place-items: center; font-size: 1.1rem; }
.topbar nav { display: flex; gap: 6px; margin-left: 16px; }
.topbar nav a { text-decoration: none; padding: 8px 12px; border-radius: 10px; opacity: .8; font-weight: 700; }
.topbar nav a.active, .topbar nav a:hover { background: rgba(255,255,255,.1); opacity: 1; }
.spacer { flex: 1; }

/* ---------- buttons ---------- */
.btn { border: 0; border-radius: 12px; padding: 10px 18px; font-weight: 800; cursor: pointer; background: #e9e4d8; color: var(--ink); display: inline-flex; align-items: center; gap: 8px; text-decoration: none; transition: transform .08s, filter .15s; white-space: nowrap; }
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--gold); color: var(--gold-ink); box-shadow: 0 4px 0 #b9862c; }
.btn.green { background: var(--good); color: #fff; box-shadow: 0 4px 0 #0e7a52; }
.btn.danger { background: #fbe3e6; color: #a32035; }
.btn.ghost { background: transparent; color: inherit; border: 2px solid currentColor; opacity: .85; }
.btn.small { padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
.btn.big { padding: 16px 28px; font-size: 1.25rem; border-radius: 16px; }
.icon-btn { border: 0; background: transparent; cursor: pointer; padding: 6px 8px; border-radius: 8px; color: inherit; font-size: 1rem; }
.icon-btn:hover { background: rgba(0,0,0,.07); }

/* ---------- form ---------- */
.input, select.input, textarea.input { width: 100%; padding: 12px 14px; border: 2px solid var(--line); border-radius: 12px; background: #fff; color: var(--ink); outline: none; }
.input:focus { border-color: var(--good); }
label.lbl { display: block; font-weight: 800; font-size: .85rem; color: var(--muted); margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .4px; }

/* ---------- cards / layout ---------- */
.container { max-width: 1100px; margin: 0 auto; padding: 24px 20px 60px; }
.card { background: var(--surface); color: var(--ink); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.muted { color: var(--muted); }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h1, h2, h3 { margin: 0 0 8px; }
.pill { display: inline-block; padding: 3px 10px; border-radius: 999px; background: var(--surface2); font-size: .8rem; font-weight: 800; color: var(--muted); }

#toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #10302d; color: #fff; padding: 12px 20px; border-radius: 12px; font-weight: 700; z-index: 100; box-shadow: var(--shadow); max-width: 90vw; }
#toast.error { background: var(--bad); }

/* ---------- answer tiles ---------- */
.tiles { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
.tile { border: 0; border-radius: 14px; padding: 18px; min-height: 84px; color: #fff; font-weight: 800; font-size: 1.2rem; display: flex; align-items: center; gap: 14px; text-align: left; box-shadow: 0 5px 0 rgba(0,0,0,.22); cursor: pointer; position: relative; }
.tile .letter { width: 40px; height: 40px; border-radius: 10px; background: rgba(255,255,255,.25); display: grid; place-items: center; flex: none; font-size: 1.1rem; }
.tile.c0 { background: var(--a0); } .tile.c1 { background: var(--a1); } .tile.c2 { background: var(--a2); }
.tile.c3 { background: var(--a3); color: #3a2800; } .tile.c4 { background: var(--a4); } .tile.c5 { background: var(--a5); }
.tile.selected { outline: 5px solid #fff; outline-offset: -5px; }
.tile.selected::after { content: "✓"; position: absolute; right: 14px; font-size: 1.5rem; }
.tile.dim { opacity: .3; }
.tile:disabled { cursor: default; }
.arabic, [dir=auto] { unicode-bidi: plaintext; }

/* ---------- misc ---------- */
.center-screen { min-height: calc(100vh - 60px); display: grid; place-items: center; padding: 20px; text-align: center; }
.spinner { width: 46px; height: 46px; border: 5px solid rgba(255,255,255,.25); border-top-color: var(--gold); border-radius: 50%; animation: spin 1s linear infinite; margin: 18px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pop { 0% { transform: scale(.6); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
.pop { animation: pop .35s cubic-bezier(.2,1.4,.5,1); }
.empty { text-align: center; padding: 50px 20px; opacity: .8; }

@media (max-width: 640px) {
  .tiles { grid-template-columns: 1fr; }
  .topbar nav { margin-left: 0; }
  .topbar .brand span.name { display: none; }
}

/* ---------- Categorize board ---------- */
.cat-board { display: flex; flex-direction: column; gap: 12px; flex: 1; }
.cat-zones { display: grid; gap: 10px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cat-zones.n3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cat-zones.n4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.cat-zone { border-radius: 14px; padding: 8px; display: flex; flex-direction: column; gap: 8px; min-height: 150px; border: 3px solid transparent; transition: transform .1s, border-color .1s; cursor: pointer; }
.cat-zone.z0 { background: rgba(18,163,127,.3); } .cat-zone.z0 .cat-name { background: var(--a0); }
.cat-zone.z1 { background: rgba(226,85,99,.3); } .cat-zone.z1 .cat-name { background: var(--a1); }
.cat-zone.z2 { background: rgba(43,123,214,.3); } .cat-zone.z2 .cat-name { background: var(--a2); }
.cat-zone.z3 { background: rgba(232,169,47,.3); } .cat-zone.z3 .cat-name { background: var(--a3); color: #3a2800; }
.cat-zone.over { border-color: #fff; transform: scale(1.02); }
.cat-name { color: #fff; font-weight: 900; text-align: center; border-radius: 10px; padding: 8px; font-size: 1.05rem; word-break: break-word; }
.cat-drop, .cat-tray { display: flex; flex-wrap: wrap; gap: 8px; align-content: flex-start; justify-content: center; flex: 1; }
.cat-tray { background: rgba(255,255,255,.08); border: 2px dashed rgba(255,255,255,.35); border-radius: 14px; padding: 10px; min-height: 100px; flex: none; }
.cat-tray.over { border-color: #fff; }
.cat-empty { opacity: .65; font-weight: 700; align-self: center; padding: 10px; font-size: .9rem; }
.cat-item { width: 92px; background: #fff; color: var(--ink); border-radius: 12px; padding: 4px; display: flex; flex-direction: column; align-items: center; gap: 2px; box-shadow: 0 3px 0 rgba(0,0,0,.25); cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; position: relative; }
.cat-item img { width: 84px; height: 84px; object-fit: contain; border-radius: 9px; display: block; pointer-events: none; }
.cat-item span { font-weight: 800; font-size: .8rem; text-align: center; line-height: 1.3; padding: 2px; overflow-wrap: break-word; max-width: 100%; }
.cat-item.text { width: auto; min-width: 92px; max-width: 100%; min-height: 56px; justify-content: center; padding: 10px 12px; }
.cat-item.text span { font-size: 1rem; line-height: 1.45; overflow-wrap: anywhere; }
.cat-item.sel { outline: 4px solid var(--gold); transform: translateY(-3px); }
.cat-item.dragging { opacity: .3; }
.cat-ghost { position: fixed; z-index: 200; pointer-events: none; transform: translate(-50%, -50%) rotate(-4deg) scale(1.08); box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.cat-item.mark-ok { outline: 4px solid var(--good); }
.cat-item.mark-no { outline: 4px solid var(--bad); }
.cat-item.mark-ok::after, .cat-item.mark-no::after { position: absolute; top: -8px; right: -8px; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 900; font-size: .8rem; }
.cat-item.mark-ok::after { content: "✓"; background: var(--good); }
.cat-item.mark-no::after { content: "✗"; background: var(--bad); }
.cat-board.locked .cat-item, .cat-board.locked .cat-zone { cursor: default; }
.cat-hint { margin: 0; opacity: .85; text-align: center; }
@media (max-width: 640px) {
  .cat-zones.n3, .cat-zones.n4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cat-item { width: 76px; } .cat-item img { width: 68px; height: 68px; }
}

.auth-warn { background: #fff3cd; color: #5c4400; border-bottom: 3px solid var(--gold); padding: 12px 20px; font-weight: 700; line-height: 1.5; }
.auth-warn code { background: rgba(0,0,0,.08); padding: 1px 6px; border-radius: 6px; word-break: break-all; }
.cat-item.text.short span { font-size: 2rem; line-height: 1.2; }
.cat-item.text.ar span { font-size: 1.35rem; }

/* ---------- Paint board ---------- */
.paint-board { display: flex; flex-direction: column; gap: 14px; flex: 1; }
.paint-text { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: center; background: rgba(255,255,255,.08); border-radius: 14px; padding: 14px; }
.seg { background: #fff; color: var(--ink); border: 3px solid #fff; border-radius: 12px; padding: 6px 12px; font-weight: 800; font-size: 1.15rem; line-height: 1.5; cursor: pointer; user-select: none; -webkit-user-select: none; touch-action: none; position: relative; display: inline-flex; flex-direction: column; align-items: center; gap: 4px; box-shadow: 0 3px 0 rgba(0,0,0,.25); max-width: 100%; transition: background .12s; }
.seg span { overflow-wrap: anywhere; text-align: center; }
.paint-text[dir=rtl] .seg { font-size: 1.45rem; }
.seg.short { font-size: 2rem !important; min-width: 56px; }
.seg.painted { background: var(--pc); color: var(--pi); border-color: var(--pc); }
.seg.has-img { padding: 4px; }
.seg img { width: 84px; height: 84px; object-fit: contain; border-radius: 8px; background: #fff; pointer-events: none; display: block; }
.paint-board.locked .seg { cursor: default; }
.seg.mark-ok { outline: 4px solid var(--good); outline-offset: 2px; }
.seg.mark-no { outline: 4px dashed var(--bad); outline-offset: 2px; }
.seg.mark-ok::after, .seg.mark-no::after { position: absolute; top: -10px; right: -10px; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 900; font-size: .75rem; font-family: system-ui, sans-serif; }
.seg.mark-ok::after { content: "✓"; background: var(--good); }
.seg.mark-no::after { content: "✗"; background: var(--bad); }
.seg .pct { font-weight: 900; font-size: .8rem; border-radius: 99px; padding: 1px 8px; color: #fff; font-family: system-ui, sans-serif; }
.seg .pct.ok { background: var(--good); } .seg .pct.no { background: var(--bad); }
.palette, .paint-legend { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.swatch { border: 3px solid transparent; background: rgba(255,255,255,.12); color: inherit; border-radius: 999px; padding: 5px 14px 5px 5px; display: inline-flex; align-items: center; gap: 8px; font-weight: 800; font-size: 1rem; cursor: pointer; }
.swatch i { width: 30px; height: 30px; border-radius: 50%; background: var(--pc); border: 2px solid #fff; flex: none; }
.swatch.sel { border-color: var(--gold); background: rgba(255,255,255,.28); transform: translateY(-2px); }
.swatch.eraser i { background: repeating-linear-gradient(45deg, #fff 0 5px, #c9c9c9 5px 10px); }
.paint-legend .swatch { cursor: default; }
