/* Hogwarts Library: mobile-first, big and readable for young kids.
   Colours are CSS variables, so dark mode and house colours just swap them. */

:root {
  --bg: #fbf7ef;            /* warm parchment */
  --surface: #ffffff;
  --surface-2: #f1ead9;
  --text: #1d1a16;
  --muted: #5f574b;
  --border: #d9cfba;
  --accent: #5b3fa0;        /* default purple until a house is chosen */
  --accent-text: #ffffff;
  --accent-soft: #ece6f8;
  --danger: #a3261b;
  --ok: #1f6b37;
  --radius: 16px;
  --tap: 48px;              /* minimum tap target */
  --shadow: 0 2px 10px rgba(40, 30, 10, .08);
  font-size: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15131b;
    --surface: #211e29;
    --surface-2: #2c2836;
    --text: #f3efe6;
    --muted: #b8b0a2;
    --border: #3d3848;
    --accent: #b9a2f2;
    --accent-text: #15131b;
    --accent-soft: #2f2742;
    --danger: #ff8a7d;
    --ok: #7fd49a;
    --shadow: none;
  }
}

/* House colours (accent only; no crests or artwork). */
body[data-house="Gryffindor"] { --accent: #8c1c13; --accent-soft: #f6e3df; --accent-text: #fff; }
body[data-house="Hufflepuff"] { --accent: #8a6a00; --accent-soft: #fbf1cf; --accent-text: #fff; }
body[data-house="Ravenclaw"]  { --accent: #1f3a8a; --accent-soft: #e2e8f8; --accent-text: #fff; }
body[data-house="Slytherin"]  { --accent: #1d6b3a; --accent-soft: #dff0e5; --accent-text: #fff; }
@media (prefers-color-scheme: dark) {
  body[data-house="Gryffindor"] { --accent: #ff8f7a; --accent-soft: #3a1f1c; --accent-text: #1a0d0b; }
  body[data-house="Hufflepuff"] { --accent: #f2c94c; --accent-soft: #352c12; --accent-text: #1a1505; }
  body[data-house="Ravenclaw"]  { --accent: #8fb0ff; --accent-soft: #1d2540; --accent-text: #0b1020; }
  body[data-house="Slytherin"]  { --accent: #7fd49a; --accent-soft: #17301f; --accent-text: #08150c; }
}
.house-Gryffindor { --swatch: #8c1c13; }
.house-Hufflepuff { --swatch: #c99a06; }
.house-Ravenclaw  { --swatch: #1f3a8a; }
.house-Slytherin  { --swatch: #1d6b3a; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.3rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- layout ---------- */
.screen {
  min-height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.screen.center { display: flex; flex-direction: column; justify-content: center; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.title-mark { font-size: 2.2rem; text-align: center; margin-bottom: 4px; }

/* ---------- controls ---------- */
button, .btn {
  font: inherit;
  min-height: var(--tap);
  padding: 10px 18px;
  border-radius: 999px;
  border: 2px solid transparent;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
}
button.secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
button.ghost { background: transparent; color: var(--accent); border-color: transparent; padding: 10px 12px; }
button.big { width: 100%; font-size: 1.1rem; padding: 14px 18px; }
button:disabled { opacity: .5; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}
input, select, textarea {
  font: inherit;
  width: 100%;
  min-height: var(--tap);
  padding: 10px 14px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
textarea { resize: none; }
label { display: block; font-weight: 600; margin-bottom: 6px; }
.error { color: var(--danger); font-weight: 600; min-height: 1.5em; }

/* Big choice buttons (houses, books, ages, personas). */
.choices { display: grid; gap: 10px; }
.choices.two { grid-template-columns: 1fr 1fr; }
.choice {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-weight: 600;
}
.choice[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.choice .emoji { font-size: 1.6rem; }
.choice .sub { display: block; font-weight: 400; font-size: .85rem; color: var(--muted); }
.swatch { width: 18px; height: 18px; border-radius: 50%; background: var(--swatch, var(--accent)); flex: none; }

/* PIN pad */
.pin-dots { display: flex; gap: 14px; justify-content: center; margin: 16px 0; }
.pin-dots span { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--accent); }
.pin-dots span.on { background: var(--accent); }
.pinpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; max-width: 320px; margin: 0 auto; }
.pinpad button { font-size: 1.4rem; min-height: 60px; border-radius: 18px; background: var(--surface-2); color: var(--text); border-color: var(--border); }
.big-pin { font-size: 3rem; letter-spacing: .4em; padding-left: .4em; text-align: center; font-weight: 800; color: var(--accent); }

/* The Sorting Hat's speech during onboarding */
.speaker { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 16px; }
.speaker .avatar { font-size: 2.2rem; line-height: 1; }
.speaker .bubble { margin: 0; }

/* ---------- main app ---------- */
.app { display: flex; flex-direction: column; height: 100dvh; max-width: 760px; margin: 0 auto; }
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.topbar .who { display: flex; align-items: center; gap: 8px; font-weight: 700; min-width: 0; }
.topbar .who .swatch { width: 14px; height: 14px; }
.topbar .who span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.tabs {
  display: grid; grid-template-columns: repeat(4, 1fr);
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  border-radius: 0; background: transparent; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: .8rem; min-height: 60px;
}
.tabs button .ico { font-size: 1.4rem; }
.tabs button[aria-selected="true"] { color: var(--accent); }

/* Ask tab */
.ask { display: flex; flex-direction: column; height: 100%; }
.ask-tools { display: flex; gap: 6px; align-items: center; padding: 8px 10px; }
.ask-tools .new-chat { margin-left: auto; font-size: .85rem; padding: 6px 8px; min-height: 40px; white-space: nowrap; }
.persona-btn { display: flex; align-items: center; gap: 6px; background: var(--surface-2); color: var(--text); border-color: var(--border); padding: 4px 12px 4px 6px; min-height: 40px; font-size: .9rem; max-width: 40%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.persona-btn .emoji { font-size: 1.4rem; }
.segmented { display: inline-flex; border: 2px solid var(--border); border-radius: 999px; overflow: hidden; }
.segmented button { border-radius: 0; background: transparent; color: var(--text); min-height: 40px; padding: 4px 9px; font-size: .78rem; font-weight: 600; }
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }
.messages { flex: 1; overflow-y: auto; padding: 8px 12px 16px; }
.empty-hint { text-align: center; color: var(--muted); padding: 24px 12px; }
.empty-hint .emoji { font-size: 3rem; display: block; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 12px; }
.suggestions button { background: var(--surface); color: var(--text); border-color: var(--border); font-weight: 500; font-size: .9rem; min-height: 40px; }

.msg { margin: 12px 0; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg.user .bubble { background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 6px; }
.msg.bot { align-items: flex-start; }
.msg .name { font-size: .8rem; font-weight: 700; color: var(--muted); margin: 0 6px 4px; display: flex; gap: 6px; align-items: center; }
.bubble {
  max-width: 88%;
  padding: 12px 16px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.bot .bubble { border-bottom-left-radius: 6px; }
.status { color: var(--muted); font-style: italic; }
.dots::after { content: "…"; animation: dots 1.2s steps(4, end) infinite; display: inline-block; width: 1.2em; text-align: left; overflow: hidden; vertical-align: bottom; }
@keyframes dots { 0% { width: 0; } 100% { width: 1.2em; } }

/* Source chips: plain, outside the speech bubble */
.source-box { margin: 4px 6px 0; max-width: 92%; font-size: .8rem; color: var(--muted); }
.source-box summary { cursor: pointer; padding: 6px 2px; min-height: 32px; }
.sources { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.source-chip { font-size: .75rem; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; background: transparent; }

/* Focus chips above the input */
.focus-bar { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 12px; align-items: center; }
.focus-chip { display: inline-flex; align-items: center; gap: 4px; background: var(--accent-soft); color: var(--text); border: 1px solid var(--accent); border-radius: 999px; padding: 2px 4px 2px 12px; font-size: .85rem; font-weight: 600; }
.focus-chip button { min-height: 32px; min-width: 32px; padding: 0; background: transparent; color: var(--text); font-size: 1.1rem; border-radius: 50%; }
.focus-bar .ghost { font-size: .85rem; min-height: 36px; }

.composer { display: flex; gap: 8px; padding: 8px 12px 12px; align-items: flex-end; }
.composer textarea { min-height: 52px; max-height: 140px; }
.composer button { min-width: 64px; }

/* History */
.list { list-style: none; margin: 0; padding: 12px; }
.list li + li { margin-top: 10px; }
.list .choice .when { margin-left: auto; font-weight: 400; font-size: .8rem; color: var(--muted); white-space: nowrap; }

/* Dialogs (persona picker, focus picker, settings) */
dialog {
  border: none; border-radius: 24px; padding: 20px;
  width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 48px);
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(10, 8, 20, .55); }
dialog h2 { margin-bottom: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.picker-group h3 { font-size: .95rem; margin: 12px 0 8px; color: var(--muted); }

.placeholder { text-align: center; padding: 48px 16px; color: var(--muted); }
.placeholder .emoji { font-size: 3rem; display: block; margin-bottom: 8px; }

/* Narrow phones: the guide button shows just its emoji, so the toolbar fits one line. */
@media (max-width: 480px) {
  .persona-btn .pname { display: none; }
  .persona-btn { padding: 4px 8px; }
}

/* ---------- Quiz ---------- */
.screen-pad { padding: 12px 12px 24px; }
.quiz-speaker { margin-bottom: 4px; }
.quiz-speaker .bubble { max-width: 100%; }
.level-card .emoji { font-size: 1.8rem; }
.topic-card label { margin: 0; }
.topic-card .row button { white-space: nowrap; flex: none; }
.quiz-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: .85rem; color: var(--muted); font-weight: 600; }
.badge { background: var(--accent-soft); color: var(--text); border: 1px solid var(--accent); border-radius: 999px; padding: 2px 10px; white-space: nowrap; }
.score { color: var(--text); white-space: nowrap; }
.question { font-size: 1.3rem; margin: 4px 0; }
.question-context { margin: 0; font-size: .85rem; font-weight: 600; color: var(--muted); }
.answer { min-height: 60px; }
.answer .letter { flex: none; width: 32px; height: 32px; border-radius: 50%; background: var(--surface-2); display: inline-flex; align-items: center; justify-content: center; font-size: .9rem; }
.answer .mark { margin-left: auto; font-size: 1.3rem; }
.answer:disabled { opacity: 1; cursor: default; }
.answer.right { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, var(--surface)); }
.answer.wrong { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, var(--surface)); }
.hint { background: var(--accent-soft); border-radius: 12px; padding: 10px 14px; margin: 0; }
.feedback { border-radius: var(--radius); padding: 14px 16px; border: 2px solid var(--border); background: var(--surface); }
.feedback.good { border-color: var(--ok); }
.feedback.bad { border-color: var(--danger); }
.feedback .verdict { font-weight: 800; font-size: 1.15rem; margin-bottom: 4px; }
.feedback.good .verdict { color: var(--ok); }
.feedback.bad .verdict { color: var(--danger); }
.streak, .level-banner { font-weight: 700; margin-bottom: 6px; }
.summary { text-align: center; }
.big-score { font-size: 2.6rem; font-weight: 800; color: var(--accent); margin: 0; }
.bars { display: grid; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 100px 1fr 56px; align-items: center; gap: 8px; font-weight: 600; font-size: .9rem; }
.bar-track { height: 18px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar-fill { display: block; height: 100%; min-width: 4px; background: var(--swatch); border-radius: 999px; }
.bar-value { text-align: right; }
.players-board { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.players-board li { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); }
.players-board li.me { border: 2px solid var(--accent); }
.making { text-align: center; padding-top: 32px; }
.making .speaker { text-align: left; }
.topic-title { font-size: 1.2rem; font-weight: 700; }

/* ---------- Grown-ups ---------- */
button.danger { background: var(--danger); color: #fff; }
.admin-nav { display: flex; flex-wrap: wrap; gap: 6px; }
.admin-nav button { min-height: 40px; padding: 6px 12px; font-size: .85rem; }
.admin-nav button[aria-selected="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.admin-body h3 { font-size: 1rem; margin: 0; }
.toggle { display: flex; gap: 12px; align-items: flex-start; font-weight: 600; cursor: pointer; }
.toggle input { width: 26px; height: 26px; min-height: 0; flex: none; margin-top: 2px; accent-color: var(--accent); }
.toggle .sub { display: block; font-weight: 400; font-size: .85rem; color: var(--muted); }
.list.flush { padding: 0; }
.admin-chat summary { cursor: pointer; font-weight: 600; }
.admin-msg { margin-top: 8px; font-size: .9rem; white-space: pre-wrap; }
.admin-msg.user strong { color: var(--accent); }
.pin-inline { font-size: 1.4rem; letter-spacing: .2em; color: var(--accent); }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { border: 1px solid var(--border); border-radius: 12px; padding: 10px; display: flex; flex-direction: column; }
.stat-value { font-size: 1.3rem; font-weight: 800; }
.stat.ok .stat-value, td.ok { color: var(--ok); }
.stat.slow .stat-value, td.slow { color: var(--danger); }
.warn { background: color-mix(in srgb, var(--danger) 12%, var(--surface)); border-radius: 12px; padding: 10px 12px; margin: 0; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; }
table { border-collapse: collapse; width: 100%; font-size: .85rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: var(--surface-2); }
.review-choices { margin: 0; padding-left: 0; list-style: none; display: grid; gap: 6px; }
.review-choices li { padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; }
.review-choices li.right { border-color: var(--ok); font-weight: 700; }
.tabs button { font-size: .75rem; white-space: nowrap; }

/* While a quiz request is on its way: dim the buttons and ignore taps. */
#content.busy { cursor: progress; }
#content.busy button { pointer-events: none; opacity: .6; }
.answer.picked { border-color: var(--accent); background: var(--accent-soft); }

/* ---------- Voice ---------- */
.speak-btn { min-height: 32px; min-width: 32px; padding: 0 6px; background: transparent; color: var(--text); border-radius: 50%; font-size: 1rem; }
.question-row { align-items: flex-start; }
.question-row .speak-btn { min-height: 44px; min-width: 44px; font-size: 1.3rem; }
.composer .mic { min-width: 52px; padding: 0; font-size: 1.3rem; }
.composer .mic.listening { background: var(--danger); color: #fff; border-color: var(--danger); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

@media (min-width: 700px) {
  .choices.wide-2 { grid-template-columns: 1fr 1fr; }
}
