/* ---------- Design tokens ---------- */
:root {
  --bg: #f6f4ff;
  --bg-2: #eef2ff;
  --card: #ffffff;
  --ink: #1e1b4b;
  --ink-2: #4b4a6b;
  --muted: #7c7a99;
  --line: #e4e2f3;
  --primary: #4f46e5;
  --primary-2: #4338ca;
  --primary-soft: #e0e7ff;
  --good: #16a34a;
  --good-soft: #dcfce7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --shadow: 0 6px 24px rgba(30, 27, 75, .08);
  --shadow-lg: 0 16px 48px rgba(30, 27, 75, .16);
  --radius: 18px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
button, a, input, select { touch-action: manipulation; } /* no 300ms tap delay / double-tap zoom on iOS */
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: radial-gradient(1200px 600px at 10% -10%, #e9e5ff 0%, transparent 60%),
              radial-gradient(900px 500px at 110% 10%, #ffe4f1 0%, transparent 55%),
              var(--bg);
  min-height: 100vh;
  line-height: 1.45;
}
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.1rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .75rem 1.25rem;
  background: rgba(255,255,255,.75);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; color: var(--ink); text-decoration: none; font-size: 1.05rem; }
.brand-emoji { font-size: 1.4rem; }
.nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.nav a { text-decoration: none; color: var(--ink-2); padding: .45rem .8rem; border-radius: 999px; font-weight: 600; font-size: .92rem; }
.nav a:hover { background: var(--primary-soft); color: var(--primary-2); }
.nav a.active { background: var(--primary); color: #fff; }
.nav-logout { color: var(--muted) !important; }

/* ---------- Layout ---------- */
.app { max-width: 1080px; margin: 0 auto; padding: 1.5rem 1rem 4rem; }
.loading { text-align: center; color: var(--muted); padding: 4rem 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

.card {
  background: var(--card); border-radius: var(--radius); padding: 1.25rem 1.4rem;
  box-shadow: var(--shadow); border: 1px solid rgba(255,255,255,.6);
}
.card-link { text-decoration: none; color: inherit; display: block; transition: transform .15s, box-shadow .15s; }
.card-link:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); }
.card-emoji { font-size: 2.2rem; line-height: 1; margin-bottom: .5rem; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: .6rem 1.1rem; border-radius: 12px; font-weight: 600; font-size: .95rem;
  cursor: pointer; text-decoration: none; transition: background .12s, transform .08s, box-shadow .12s;
  font-family: inherit;
}
.btn:hover { background: var(--bg-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-2); }
.btn-good { background: var(--good); border-color: var(--good); color: #fff; }
.btn-bad { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--primary); }
.btn-lg { padding: .85rem 1.5rem; font-size: 1.05rem; border-radius: 14px; }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }

/* ---------- Forms ---------- */
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .75rem; color: var(--ink-2); }
input[type=text], input[type=password], input[type=number], select {
  display: block; width: 100%; margin-top: .3rem; padding: .65rem .8rem; font: inherit;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
}
input:focus, select:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.alert { background: var(--bad-soft); color: #991b1b; border-radius: 10px; padding: .65rem .9rem; margin-bottom: 1rem; font-weight: 600; }
.alert-ok { background: var(--good-soft); color: #14532d; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { text-align: left; padding: .5rem .4rem; border-bottom: 1px solid var(--line); }
.table th { color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; }

/* ---------- Choice chips (setup screens) ---------- */
.chips { display: flex; gap: .5rem; flex-wrap: wrap; }
.chip {
  border: 2px solid var(--line); background: #fff; border-radius: 999px; padding: .45rem .95rem;
  font-weight: 600; cursor: pointer; font-family: inherit; font-size: .92rem; color: var(--ink-2);
  transition: all .12s;
}
.chip:hover { border-color: var(--primary); }
.chip.on { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip.cat { border-color: var(--c); color: var(--c); }
.chip.cat.on { background: var(--c); color: #fff; }
.chip-dot { display: inline-block; width: .7em; height: .7em; border-radius: 50%; background: var(--c); margin-right: .4em; vertical-align: -1px; }

/* ---------- Category key / legend ---------- */
.legend { display: flex; flex-wrap: wrap; gap: .4rem .9rem; font-size: .85rem; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: .35rem; }
.legend i { width: .8em; height: .8em; border-radius: 3px; display: inline-block; background: var(--c); }
.cat-badge {
  display: inline-flex; align-items: center; gap: .35rem; font-size: .78rem; font-weight: 700;
  color: #fff; background: var(--c); padding: .2rem .6rem; border-radius: 999px; letter-spacing: .02em;
}

/* ---------- Login ---------- */
.login-body { display: grid; place-items: center; padding: 1rem; }
.login-card { background: var(--card); border-radius: 24px; padding: 2rem 2rem 1.5rem; width: 100%; max-width: 400px; box-shadow: var(--shadow-lg); text-align: center; }
.login-card form { text-align: left; margin-top: 1rem; }
.login-logo { font-size: 3rem; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: .7rem 1.1rem; border-radius: 12px; font-weight: 600;
  box-shadow: var(--shadow-lg); z-index: 100; max-width: 90vw;
}

/* ---------- Home ---------- */
.hero { display: flex; gap: 1.5rem; align-items: center; flex-wrap: wrap; margin-bottom: 1.5rem; }
.hero h1 { font-size: 2rem; }
.stat-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .75rem; }
.tile { background: var(--card); border-radius: 14px; padding: .9rem 1rem; box-shadow: var(--shadow); }
.tile b { display: block; font-size: 1.6rem; line-height: 1.1; }
.tile span { color: var(--muted); font-size: .82rem; font-weight: 600; }
.cat-bars { display: grid; gap: .55rem; }
.cat-bar { display: grid; grid-template-columns: 1fr auto; gap: .35rem .75rem; align-items: center; font-size: .9rem; }
.cat-bar .name { font-weight: 600; }
.cat-bar .track { grid-column: 1 / -1; height: 9px; border-radius: 999px; background: var(--bg-2); overflow: hidden; }
.cat-bar .fill { height: 100%; border-radius: 999px; background: var(--c); transition: width .5s; }
.cat-bar .pct { color: var(--ink-2); font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Flashcards ---------- */
.fc-wrap { max-width: 720px; margin: 0 auto; }
.fc-progress { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; font-weight: 600; color: var(--ink-2); font-size: .9rem; }
.progress { height: 8px; background: var(--bg-2); border-radius: 999px; overflow: hidden; margin-bottom: 1rem; }
.progress > div { height: 100%; background: var(--primary); border-radius: 999px; transition: width .3s; }
.flashcard {
  perspective: 1400px; min-height: 320px; cursor: pointer; user-select: none;
}
.flashcard-inner { position: relative; width: 100%; min-height: 320px; transition: transform .55s cubic-bezier(.4,.2,.2,1); transform-style: preserve-3d; }
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.fc-face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border-radius: 22px; padding: 1.5rem 1.6rem; display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg); color: #fff;
  background: var(--c); /* fallback for older iOS without color-mix() */
  background: linear-gradient(160deg, var(--c) 0%, color-mix(in srgb, var(--c) 70%, #1e1b4b) 100%);
}
.fc-back { transform: rotateY(180deg); background: #fff; color: var(--ink); border: 3px solid var(--c); }
.fc-top { display: flex; justify-content: space-between; align-items: center; gap: .5rem; font-size: .8rem; font-weight: 700; opacity: .95; }
.fc-back .fc-top { color: var(--c); }
.fc-body { flex: 1; display: flex; align-items: center; justify-content: center; text-align: center; font-size: 1.25rem; font-weight: 600; padding: 1rem .25rem; }
.fc-back .fc-body { flex-direction: column; gap: .6rem; }
.fc-answer { font-size: 1.6rem; font-weight: 800; color: var(--c); }
.fc-expl { font-size: .95rem; font-weight: 400; color: var(--ink-2); }
.fc-hint { text-align: center; font-size: .82rem; opacity: .8; }
.fc-stats { display: flex; flex-wrap: wrap; justify-content: center; gap: .3rem .9rem; font-size: .8rem; font-weight: 600; opacity: .95; margin-bottom: .5rem; }
.fc-stats .fc-pile { flex-basis: 100%; text-align: center; margin-top: .15rem; letter-spacing: .02em; }
.fc-stats .fc-stat-sub { font-weight: 400; opacity: .85; }
.mc-status { color: var(--ink-2); margin-top: .5rem; }
.mc-status .fc-stats { justify-content: flex-start; opacity: 1; }
.fc-actions { display: flex; gap: .75rem; justify-content: center; margin-top: 1.1rem; }
.fc-actions .btn { flex: 1; max-width: 240px; }
.mc-list { display: grid; gap: .6rem; margin-top: 1rem; }
.mc-opt {
  text-align: left; padding: .85rem 1rem; border-radius: 12px; border: 2px solid var(--line); background: #fff;
  font: inherit; font-weight: 600; cursor: pointer; color: var(--ink); transition: all .12s; display: flex; gap: .7rem; align-items: center;
}
.mc-opt:hover:not(:disabled) { border-color: var(--primary); background: var(--primary-soft); }
.mc-opt .letter { width: 1.8rem; height: 1.8rem; border-radius: 50%; background: var(--bg-2); display: grid; place-items: center; font-size: .85rem; flex-shrink: 0; }
.mc-opt.correct { border-color: var(--good); background: var(--good-soft); }
.mc-opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.mc-opt:disabled { cursor: default; }
.explain { background: var(--bg-2); border-radius: 12px; padding: .8rem 1rem; margin-top: 1rem; font-size: .95rem; }
.result-card { text-align: center; padding: 2rem 1.5rem; }
.result-card .big { font-size: 3rem; line-height: 1; margin-bottom: .5rem; }
.result-card .score { font-size: 2.4rem; font-weight: 800; color: var(--primary); }
.mini-bars { display: grid; gap: .4rem; margin: 1rem auto; max-width: 420px; text-align: left; }

/* ---------- Bingo ---------- */
.bingo-layout { display: grid; grid-template-columns: minmax(280px, 1fr) minmax(280px, 420px); gap: 1.25rem; align-items: start; }
@media (max-width: 800px) { .bingo-layout { grid-template-columns: minmax(0, 1fr); } .bingo-question { position: static; order: -1; } }
.bingo-question { position: sticky; top: 4.5rem; }
.bingo-q-text { font-size: 1.15rem; font-weight: 600; margin: .5rem 0 .75rem; }
.bingo-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .4rem; }
.bingo-cell {
  aspect-ratio: 1; border-radius: 12px; border: 2px solid var(--line); background: #fff;
  font: inherit; font-weight: 700; font-size: clamp(.62rem, 1.6vw, .85rem); line-height: 1.15; padding: .3rem;
  cursor: pointer; display: grid; place-items: center; text-align: center; color: var(--ink); position: relative;
  transition: transform .1s, background .15s; word-break: break-word; hyphens: auto;
  border-color: var(--c, var(--line)); min-width: 0;
}
.bingo-cell.long { font-size: clamp(.55rem, 1.3vw, .72rem); letter-spacing: -.01em; }
.bingo-cell:hover:not(.marked):not(.free) { transform: scale(1.04); background: var(--bg-2); }
.bingo-cell.marked { background: var(--c, var(--primary)); color: #fff; border-color: var(--c, var(--primary)); }
.bingo-cell.marked::after { content: "✓"; position: absolute; top: .15rem; right: .3rem; font-size: .8rem; opacity: .9; }
.bingo-cell.free { background: linear-gradient(135deg, #fde68a, #f59e0b); color: #78350f; border-color: #f59e0b; font-size: .9rem; }
.bingo-cell.wrong { animation: shake .35s; border-color: var(--bad); background: var(--bad-soft); }
.bingo-cell.win { animation: pop .6s infinite alternate; box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--c, var(--primary)); }
.bingo-cell:disabled { cursor: default; }
.bingo-header { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .4rem; margin-bottom: .4rem; }
.bingo-header span { text-align: center; font-weight: 900; font-size: 1.4rem; color: var(--primary); letter-spacing: .05em; }
@keyframes shake { 0%,100% { transform: translateX(0);} 25% { transform: translateX(-5px);} 75% { transform: translateX(5px);} }
@keyframes pop { from { transform: scale(1);} to { transform: scale(1.06);} }

/* ---------- Tic Tac Toe ---------- */
.ttt-layout { display: grid; grid-template-columns: minmax(260px, 420px) minmax(280px, 1fr); gap: 1.25rem; justify-content: center; align-items: start; }
@media (max-width: 760px) { .ttt-layout { grid-template-columns: minmax(0, 1fr); } }
.ttt-board { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; }
.ttt-cell {
  aspect-ratio: 1; border-radius: 18px; border: 2px solid var(--line); background: #fff; font: inherit;
  font-size: clamp(2.5rem, 8vw, 4.5rem); font-weight: 900; cursor: pointer; display: grid; place-items: center;
  transition: transform .1s, background .15s; box-shadow: var(--shadow);
}
.ttt-cell:hover:not(:disabled) { transform: scale(1.03); background: var(--primary-soft); }
.ttt-cell.x { color: var(--primary); border-color: var(--primary); }
.ttt-cell.o { color: #ec4899; border-color: #ec4899; }
.ttt-cell.win { background: var(--good-soft); border-color: var(--good); }
.ttt-cell:disabled { cursor: default; }
.ttt-status { font-weight: 700; font-size: 1.05rem; margin: .75rem 0; text-align: center; }
.ttt-score { display: flex; justify-content: center; gap: 1.5rem; font-weight: 700; color: var(--ink-2); }
.ttt-score b { font-size: 1.4rem; display: block; text-align: center; }

/* ---------- Jeopardy ---------- */
.jp-wrap { --jp-blue: #0b1e8c; --jp-gold: #f5c518; }
.jp-top { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: .75rem; margin-bottom: 1rem; }
.jp-score { font-size: 1.8rem; font-weight: 900; font-variant-numeric: tabular-nums; padding: .3rem 1rem; border-radius: 12px; background: var(--jp-blue); color: var(--jp-gold); }
.jp-score.neg { color: #fca5a5; }
.jp-board { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .4rem; background: #06114f; padding: .5rem; border-radius: 16px; }
.jp-cat { background: var(--jp-blue); color: #fff; border-radius: 8px; padding: .5rem .25rem; text-align: center; font-weight: 800; font-size: clamp(.6rem, 1.5vw, .85rem); line-height: 1.2; min-height: 3.6rem; display: grid; place-items: center; border-bottom: 5px solid var(--c); text-transform: uppercase; letter-spacing: .02em; }
.jp-clue {
  background: var(--jp-blue); color: var(--jp-gold); border: 0; border-radius: 8px; font: inherit; font-weight: 900;
  font-size: clamp(1rem, 3vw, 1.7rem); padding: .8rem .2rem; cursor: pointer; text-shadow: 2px 2px 0 #000; transition: transform .1s, background .15s;
  min-height: 3.5rem;
}
.jp-clue:hover:not(:disabled) { background: #1a30b8; transform: scale(1.03); }
.jp-clue:disabled { background: #0a1866; color: transparent; text-shadow: none; cursor: default; }
.jp-modal-bg { position: fixed; inset: 0; background: rgba(6,17,79,.85); display: grid; place-items: center; z-index: 50; padding: 1rem; }
.jp-modal { background: var(--jp-blue); color: #fff; border-radius: 20px; padding: 1.75rem; width: 100%; max-width: 720px; box-shadow: var(--shadow-lg); text-align: center; }
.jp-modal .jp-value { color: var(--jp-gold); font-weight: 900; font-size: 1.4rem; }
.jp-modal .jp-clue-text { font-size: clamp(1.15rem, 2.8vw, 1.6rem); font-weight: 700; margin: 1.25rem 0; line-height: 1.35; text-transform: uppercase; letter-spacing: .01em; }
.jp-modal input { text-align: center; font-size: 1.15rem; padding: .8rem; border-radius: 12px; border: 0; }
.jp-modal .btn { font-size: 1rem; }
.jp-dd { font-size: 2rem; font-weight: 900; color: var(--jp-gold); text-shadow: 3px 3px 0 #000; animation: pop .4s infinite alternate; }
.jp-verdict { border-radius: 14px; padding: 1rem; margin-top: 1rem; background: rgba(255,255,255,.1); text-align: left; }
.jp-verdict.good { background: rgba(22,163,74,.35); }
.jp-verdict.bad { background: rgba(220,38,38,.35); }
.jp-verdict .lead { font-weight: 800; font-size: 1.15rem; }

/* ---------- Modal (generic) ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(30,27,75,.6); display: grid; place-items: center; z-index: 50; padding: 1rem; }
.modal { background: var(--card); border-radius: 20px; padding: 1.5rem; width: 100%; max-width: 640px; box-shadow: var(--shadow-lg); max-height: 92vh; overflow: auto; }

/* ---------- Stats ---------- */
.spark { display: flex; align-items: flex-end; gap: 4px; height: 90px; padding-top: .5rem; }
.spark > div { flex: 1; height: 100%; background: var(--primary-soft); border-radius: 4px 4px 0 0; position: relative; min-height: 3px; }
.spark > div > i { position: absolute; left: 0; right: 0; bottom: 0; background: var(--primary); border-radius: 4px 4px 0 0; }
.spark-labels { display: flex; gap: 4px; font-size: .65rem; color: var(--muted); }
.spark-labels span { flex: 1; text-align: center; }
.rank-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .45rem; }
.rank-list li { display: flex; align-items: center; gap: .6rem; font-weight: 600; }
.rank-list .cat-badge { min-width: 0; }
.mode-row { display: grid; grid-template-columns: 2.2rem 1fr auto; gap: .75rem; align-items: center; padding: .55rem 0; border-bottom: 1px solid var(--line); }
.mode-row:last-child { border-bottom: 0; }
.mode-row .emo { font-size: 1.5rem; }
.mode-row b { display: block; }
.mode-row .small { color: var(--muted); }
.missed { display: grid; gap: .6rem; }
.missed .q { font-weight: 600; }
.missed .a { color: var(--good); font-weight: 700; }

/* ---------- Phones ---------- */
@media (max-width: 600px) {
  .topbar { padding: .6rem .8rem; flex-direction: column; align-items: stretch; gap: .5rem;
            padding-left: max(.8rem, env(safe-area-inset-left)); padding-right: max(.8rem, env(safe-area-inset-right)); }
  .nav { justify-content: space-between; }
  .nav a { padding: .5rem .55rem; font-size: .85rem; }
  .app { padding: 1rem .75rem calc(3rem + env(safe-area-inset-bottom)); }
  .card { padding: 1rem; }
  .hero h1 { font-size: 1.5rem; }
  .btn { padding: .7rem 1rem; min-height: 44px; }           /* Apple's 44pt tap target */
  .chip { padding: .55rem .9rem; min-height: 40px; }
  .mc-opt { padding: .9rem .9rem; }
  input[type=text], input[type=password], input[type=number], select { font-size: 16px; } /* stops iOS zooming into fields */
  .fc-body { font-size: 1.1rem; }
  .fc-answer { font-size: 1.4rem; }
  .fc-actions { gap: .5rem; }
  .fc-actions .btn { max-width: none; }
  /* modals: anchor to the top so the on-screen keyboard doesn't hide the input, and let them scroll */
  .modal-bg, .jp-modal-bg { align-items: start; padding: 1rem .75rem; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .modal, .jp-modal { padding: 1.1rem 1rem; max-height: none; }
  .jp-modal .jp-clue-text { font-size: 1.1rem; margin: .9rem 0; }
  .jp-modal input { font-size: 16px; }
  .jp-score { font-size: 1.4rem; }
  .ttt-cell { border-radius: 14px; }
  .bingo-cell { border-radius: 9px; }
  .toast { bottom: calc(1rem + env(safe-area-inset-bottom)); font-size: .9rem; }
  .stat-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .table { font-size: .85rem; }
  .table th, .table td { padding: .45rem .3rem; }
}
