:root {
  --bg: #0b0d17;
  --bg-2: #141a2e;
  --card: #1a2140;
  --card-2: #222a4f;
  --line: rgba(212, 175, 55, 0.22);
  --ink: #efe6cf;
  --ink-dim: #b7ae97;
  --muted: #8b86a0;
  --gold: #d4af37;
  --gold-2: #f1d27a;
  --accent: #d4af37;
  --accent-ink: #14110a;
  --ok: #3fa96b;
  --ok-bg: rgba(63, 169, 107, 0.16);
  --bad: #d0483f;
  --bad-bg: rgba(208, 72, 63, 0.16);
  --warn: #e0a72f;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --radius: 14px;
  --font-head: 'Cinzel', 'Georgia', serif;
  --font-body: 'Crimson Pro', 'Georgia', serif;
  --nav-h: 62px;
}

:root[data-house="gryffindor"] { --accent: #c9302c; --accent-ink: #fff3e0; --gold: #eeba30; --gold-2: #ffd86b; }
:root[data-house="slytherin"] { --accent: #2a7a4b; --accent-ink: #eefbf2; --gold: #b9c6c2; --gold-2: #dfe8e5; }
:root[data-house="ravenclaw"] { --accent: #2f5bb7; --accent-ink: #eef3ff; --gold: #b9853a; --gold-2: #e0b061; }
:root[data-house="hufflepuff"] { --accent: #e3b52f; --accent-ink: #201a10; --gold: #f0cf5a; --gold-2: #ffe08a; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg) radial-gradient(1200px 600px at 50% -10%, #1e2650 0%, var(--bg) 60%);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 18px;
  line-height: 1.45;
  min-height: 100vh;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
}
body::before {
  content: "";
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background-image:
    radial-gradient(1px 1px at 12% 18%, rgba(255,255,255,0.35) 50%, transparent 51%),
    radial-gradient(1px 1px at 68% 8%, rgba(255,255,255,0.3) 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 84% 32%, rgba(255,255,255,0.28) 50%, transparent 51%),
    radial-gradient(1px 1px at 30% 62%, rgba(255,255,255,0.22) 50%, transparent 51%),
    radial-gradient(1px 1px at 52% 44%, rgba(255,255,255,0.25) 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 92% 76%, rgba(255,255,255,0.2) 50%, transparent 51%),
    radial-gradient(1px 1px at 8% 86%, rgba(255,255,255,0.25) 50%, transparent 51%);
}
#app { position: relative; z-index: 1; max-width: 760px; margin: 0 auto; padding: 16px 16px 24px; }

h1, h2, h3, h4 { font-family: var(--font-head); font-weight: 700; letter-spacing: 0.02em; margin: 0 0 10px; color: var(--gold-2); }
h1 { font-size: 1.7rem; line-height: 1.2; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; color: var(--ink); }
p { margin: 0 0 10px; }
a { color: var(--gold-2); }
small, .small { font-size: 0.85rem; color: var(--ink-dim); }
.muted { color: var(--muted); }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1; }
.mt { margin-top: 14px; }
.mb { margin-bottom: 14px; }
.hidden { display: none !important; }

/* Top bar */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; }
.brand .sigil { width: 38px; height: 38px; border-radius: 50%; border: 2px solid var(--gold); display: grid; place-items: center; font-family: var(--font-head); color: var(--gold-2); font-size: 1.1rem; background: var(--bg-2); }
.brand .title { font-family: var(--font-head); color: var(--gold-2); font-size: 1rem; line-height: 1.1; }
.brand .title small { display: block; font-family: var(--font-body); color: var(--muted); font-size: 0.75rem; letter-spacing: 0.08em; text-transform: uppercase; }

/* Cards */
.card { background: linear-gradient(180deg, var(--card), var(--bg-2)); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.card.tight { padding: 12px 14px; }
.card h2:first-child { margin-top: 0; }
.hero { text-align: center; padding: 28px 16px 22px; }
.hero h1 { font-size: 1.9rem; margin-bottom: 6px; }
.hero .sub { color: var(--ink-dim); font-style: italic; font-size: 1.05rem; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font-family: var(--font-head); font-weight: 700; font-size: 0.95rem; letter-spacing: 0.03em;
  padding: 12px 18px; border-radius: 12px; border: 1px solid var(--line); cursor: pointer;
  background: var(--card-2); color: var(--ink); text-decoration: none; transition: transform 0.06s ease, background 0.15s ease;
  min-height: 46px; -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: #2a3360; }
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.small { padding: 8px 12px; min-height: 36px; font-size: 0.8rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.icon { padding: 8px 10px; min-height: 36px; min-width: 36px; font-family: var(--font-body); font-size: 1rem; }

/* Home menu */
.menu { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.tile {
  display: flex; flex-direction: column; gap: 6px; padding: 16px 14px; border-radius: var(--radius);
  background: linear-gradient(160deg, var(--card-2), var(--card)); border: 1px solid var(--line); text-decoration: none; color: var(--ink);
  box-shadow: var(--shadow); min-height: 118px; -webkit-tap-highlight-color: transparent;
}
.tile:hover { border-color: var(--gold); }
.tile .ico { font-size: 1.7rem; line-height: 1; }
.tile .name { font-family: var(--font-head); font-weight: 700; color: var(--gold-2); font-size: 0.98rem; }
.tile .desc { font-size: 0.86rem; color: var(--ink-dim); line-height: 1.3; }
.tile.wide { grid-column: 1 / -1; flex-direction: row; align-items: center; min-height: 0; }
.tile.wide .ico { font-size: 1.5rem; }

.stats-strip { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { text-align: center; background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 6px; }
.stat .v { font-family: var(--font-head); font-size: 1.3rem; color: var(--gold-2); }
.stat .l { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }

/* House picker */
.houses { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.house-btn { border-radius: 10px; border: 1px solid var(--line); padding: 8px 4px; text-align: center; background: var(--bg-2); color: var(--ink); cursor: pointer; font-family: var(--font-head); font-size: 0.7rem; }
.house-btn.g { background: linear-gradient(180deg, #7a1b18, #4a100e); }
.house-btn.s { background: linear-gradient(180deg, #1f5a37, #123a22); }
.house-btn.r { background: linear-gradient(180deg, #223c7a, #14264f); }
.house-btn.h { background: linear-gradient(180deg, #b58a1a, #7a5c0f); }
.house-btn.on { outline: 2px solid var(--gold-2); }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid var(--line); background: var(--bg-2); color: var(--ink-dim); border-radius: 999px; padding: 7px 12px; font-size: 0.88rem; cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.chip .n { opacity: 0.7; font-size: 0.78rem; margin-left: 4px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; flex-wrap: wrap; }
.seg button { border: 0; background: var(--bg-2); color: var(--ink-dim); padding: 9px 14px; font-family: var(--font-body); font-size: 0.95rem; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
label.field { display: block; font-family: var(--font-head); font-size: 0.8rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin: 14px 0 8px; }
label.field:first-child { margin-top: 0; }
input[type="search"], input[type="text"], select {
  width: 100%; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-2); color: var(--ink);
  font-family: var(--font-body); font-size: 1rem; outline: none;
}
input:focus, select:focus { border-color: var(--gold); }

/* Quiz */
.qhead { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 0.9rem; color: var(--ink-dim); }
.qhead .score { font-family: var(--font-head); color: var(--gold-2); }
.progress { height: 6px; background: var(--bg-2); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); }
.progress > div { height: 100%; background: var(--accent); transition: width 0.3s ease; }
.timer { height: 5px; background: var(--bg-2); border-radius: 999px; overflow: hidden; margin-top: 8px; }
.timer > div { height: 100%; background: var(--warn); transition: width 0.25s linear; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.badge { font-size: 0.72rem; letter-spacing: 0.06em; text-transform: uppercase; padding: 4px 8px; border-radius: 6px; background: var(--bg-2); border: 1px solid var(--line); color: var(--ink-dim); }
.badge.d1 { color: #7fd39a; } .badge.d2 { color: #f0cf5a; } .badge.d3 { color: #f08a7e; }
.question { font-size: 1.28rem; line-height: 1.35; margin: 6px 0 16px; color: var(--ink); }
.question.big { font-size: 1.6rem; }
.options { display: grid; gap: 10px; }
.opt {
  display: flex; align-items: center; gap: 12px; text-align: left; width: 100%;
  padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-2); color: var(--ink);
  font-family: var(--font-body); font-size: 1.05rem; cursor: pointer; -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.opt:hover { border-color: var(--gold); }
.opt .key { flex: 0 0 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; font-family: var(--font-head); font-size: 0.8rem; background: var(--card-2); color: var(--gold-2); }
.opt.correct { background: var(--ok-bg); border-color: var(--ok); }
.opt.wrong { background: var(--bad-bg); border-color: var(--bad); }
.opt.dim { opacity: 0.55; }
.opt:disabled { cursor: default; }
.explain { margin-top: 14px; padding: 12px 14px; border-radius: 12px; background: rgba(212, 175, 55, 0.08); border: 1px dashed var(--line); font-size: 0.98rem; }
.explain b { color: var(--gold-2); font-family: var(--font-head); font-size: 0.85rem; letter-spacing: 0.04em; }
.verdict { font-family: var(--font-head); font-size: 1.05rem; margin-bottom: 4px; }
.verdict.ok { color: var(--ok); } .verdict.bad { color: var(--bad); }
.actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.actions .btn { flex: 1; }

/* Results */
.grade { font-family: var(--font-head); font-size: 4rem; line-height: 1; color: var(--gold-2); text-align: center; }
.grade-name { text-align: center; font-family: var(--font-head); color: var(--ink); margin-bottom: 4px; }
.bars { display: grid; gap: 8px; }
.bar { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; font-size: 0.92rem; }
.bar .track { grid-column: 1 / -1; height: 8px; background: var(--bg-2); border-radius: 999px; overflow: hidden; }
.bar .track > div { height: 100%; background: var(--accent); }
.missed { border-top: 1px solid var(--line); padding: 10px 0; }
.missed .q { font-weight: 600; }
.missed .a { color: var(--ok); }

/* Lists / spellbook */
.list { display: grid; gap: 10px; }
.spell { border: 1px solid var(--line); border-radius: 12px; background: var(--bg-2); overflow: hidden; }
.spell summary { list-style: none; cursor: pointer; padding: 12px 14px; display: flex; align-items: center; gap: 10px; }
.spell summary::-webkit-details-marker { display: none; }
.spell .inc { font-family: var(--font-head); font-weight: 700; font-size: 1.05rem; color: var(--gold-2); }
.spell .nm { color: var(--ink-dim); font-size: 0.9rem; }
.spell .tag { margin-left: auto; font-size: 0.7rem; letter-spacing: 0.06em; text-transform: uppercase; padding: 3px 7px; border-radius: 6px; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.spell .tag.duel { color: var(--gold-2); border-color: var(--gold); }
.spell .tag.dark { color: #f08a7e; border-color: #7a2c26; }
.spell .body { padding: 0 14px 14px; display: grid; gap: 6px; font-size: 0.97rem; border-top: 1px dashed var(--line); padding-top: 10px; }
.spell .body .k { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; margin-right: 6px; }
.spell .body .fr { color: var(--gold-2); }

/* Flashcards */
.flash { min-height: 220px; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; cursor: pointer; padding: 24px 16px; }
.flash .front { font-size: 1.3rem; }
.flash .back { font-family: var(--font-head); font-size: 1.7rem; color: var(--gold-2); }
.flash .hint { color: var(--muted); font-size: 0.85rem; margin-top: 12px; }
.duel-scene { text-align: center; padding: 20px 14px; }
.duel-scene .who { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.1em; }
.duel-scene .cast { font-family: var(--font-head); font-size: 1.9rem; color: var(--gold-2); margin: 6px 0; text-shadow: 0 0 22px rgba(241, 210, 122, 0.45); }
.duel-scene .need { font-size: 1.2rem; }
.pulse { animation: pulse 0.9s ease-in-out infinite alternate; }
@keyframes pulse { from { text-shadow: 0 0 8px rgba(241,210,122,0.3); } to { text-shadow: 0 0 28px rgba(241,210,122,0.8); } }
.shake { animation: shake 0.35s ease; }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* Tables (cheat sheets) */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 0.93rem; }
th, td { text-align: left; padding: 8px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-family: var(--font-head); font-size: 0.75rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--gold-2); }
td:first-child { color: var(--gold-2); font-weight: 600; }
details.sheet { border: 1px solid var(--line); border-radius: var(--radius); background: linear-gradient(180deg, var(--card), var(--bg-2)); margin-bottom: 10px; }
details.sheet summary { cursor: pointer; padding: 14px 16px; font-family: var(--font-head); font-weight: 700; color: var(--gold-2); list-style: none; display: flex; align-items: center; gap: 10px; }
details.sheet summary::-webkit-details-marker { display: none; }
details.sheet summary::after { content: "+"; margin-left: auto; color: var(--muted); }
details.sheet[open] summary::after { content: "–"; }
details.sheet .content { padding: 0 16px 16px; }
details.sheet .content p { color: var(--ink-dim); font-size: 0.95rem; }
ul.clean { margin: 0; padding-left: 18px; }
ul.clean li { margin-bottom: 6px; }

/* Bottom nav */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: rgba(11, 13, 23, 0.92); backdrop-filter: blur(10px); border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(5, 1fr);
}
.nav a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 0.68rem; letter-spacing: 0.04em; text-transform: uppercase; font-family: var(--font-head); }
.nav a .i { font-size: 1.25rem; line-height: 1; }
.nav a.on { color: var(--gold-2); }

.toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + 18px); transform: translateX(-50%); background: var(--card-2); color: var(--ink); border: 1px solid var(--gold); padding: 10px 16px; border-radius: 12px; z-index: 20; box-shadow: var(--shadow); font-size: 0.95rem; }

.kbd { font-family: monospace; font-size: 0.8rem; background: var(--bg-2); border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; color: var(--ink-dim); }
.loading { text-align: center; padding: 60px 0; color: var(--muted); font-style: italic; }

@media (min-width: 560px) {
  .menu { grid-template-columns: repeat(3, 1fr); }
  .tile.wide { grid-column: 1 / -1; }
  body { font-size: 18px; }
}
@media (max-width: 380px) {
  .houses { grid-template-columns: repeat(5, 1fr); }
  .house-btn { font-size: 0.6rem; padding: 7px 2px; }
  .question { font-size: 1.15rem; }
}
@media (prefers-reduced-motion: reduce) {
  .pulse, .shake { animation: none; }
}
