/* A House Divided: storybook battle look. Cream paper, ink outlines, chunky
   shadows, bright pixel scenes. Pixel font for labels only; Nunito for
   anything students have to read. */

:root {
  --page: #fff7e6; --paper: #fffaf0; --ink: #2a1f3d; --muted: #6b5f80; --line: #e8dfcc;
  --gold: #ffcf3f; --red: #e2574c; --blue: #3d7be0; --orange: #e39b2d; --green: #3fae5a; --purple: #7a5a9e;
  --good: #3fae5a; --bad: #e2574c;
  --shadow: 4px 4px 0 rgba(42, 31, 61, .3);
  --px: 'Pixelify Sans', 'Courier New', monospace;
  --sans: Nunito, system-ui, -apple-system, 'Segoe UI', sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--page); color: var(--ink); font: 700 16px/1.45 var(--sans); -webkit-tap-highlight-color: transparent; overflow-x: hidden; }
canvas { image-rendering: pixelated; image-rendering: crisp-edges; display: block; }
button { font: inherit; color: inherit; cursor: pointer; }
.px { font-family: var(--px); }
.muted { color: var(--muted); }
.loading { display: grid; place-items: center; height: 100dvh; font-size: 22px; }
#ui { min-height: 100dvh; }

/* ---------------------------------------------------------------- basics */
.paper { background: var(--paper); border: 3px solid var(--ink); border-radius: 14px; box-shadow: var(--shadow); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 3px solid var(--ink); border-radius: 12px; background: #fff; padding: .55em 1em; font: 800 17px/1.15 var(--sans); box-shadow: 3px 3px 0 rgba(42,31,61,.3); transition: transform .06s; text-decoration: none; color: var(--ink); }
.btn:hover { transform: translate(-1px, -1px); box-shadow: 4px 4px 0 rgba(42,31,61,.35); }
.btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 rgba(42,31,61,.3); }
.btn:focus-visible, .opt:focus-visible, .chip:focus-visible, .chapter:focus-visible { outline: 3px solid var(--blue); outline-offset: 3px; }
.btn.big { font-size: 20px; padding: .65em 1.3em; }
.btn.gold { background: var(--gold); }
.btn.green { background: #7be38f; }
.btn.blue { background: #bfe0ff; }
.btn.ghost { background: transparent; box-shadow: none; border-color: transparent; }
.btn.icon { padding: 6px; width: 44px; height: 44px; }
.btn.icon svg { width: 24px; height: 24px; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.center { text-align: center; justify-content: center; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); padding: 10px 18px; z-index: 50; animation: pop .2s; }
.screen.dim { position: fixed; inset: 0; background: rgba(42,31,61,.55); display: grid; place-items: center; padding: 16px; z-index: 40; overflow: auto; }
@keyframes pop { from { transform: translateX(-50%) scale(.9); opacity: 0; } }
.chip { display: inline-flex; align-items: center; gap: 6px; border: 2px solid var(--ink); border-radius: 999px; padding: 3px 12px; background: #fff; font: 800 15px var(--sans); }
.chip.on { background: var(--ink); color: var(--paper); }
input.field { font: 800 20px var(--sans); border: 3px solid var(--ink); border-radius: 10px; padding: 8px 12px; width: 100%; background: #fff; color: var(--ink); }
input.field:focus { outline: 3px solid var(--blue); outline-offset: 1px; }
label.lbl { display: block; font: 700 15px var(--px); margin: 14px 0 6px; }
.err { color: var(--red); min-height: 1.4em; margin-top: 8px; }

/* ---------------------------------------------------------------- title */
.title-screen { position: relative; min-height: 100dvh; display: grid; place-items: center; padding: 16px; overflow: hidden; }
.title-screen > canvas.bg { position: absolute; inset: 0; width: 100%; height: 100%; }
.title-card { position: relative; max-width: 560px; width: 100%; padding: 26px 22px 22px; text-align: center; }
.title-card h1 { margin: 0; font: 700 clamp(38px, 9vw, 64px)/1 var(--px); letter-spacing: 1px; }
.title-card h1 span { color: var(--red); }
.title-card .sub { margin: 8px 0 0; font: 800 18px var(--sans); color: var(--muted); }
.title-card .cast { display: flex; justify-content: center; gap: 4px; margin: 16px 0 10px; }
.title-card .cast canvas { width: 56px; height: 72px; }
.title-card .actions { display: grid; gap: 10px; margin-top: 14px; }
.title-card .who { margin-top: 2px; font-size: 15px; color: var(--muted); }
.title-card .fine { margin-top: 14px; font-size: 13px; color: var(--muted); font-weight: 700; }

/* ---------------------------------------------------------------- forms */
.form-screen { min-height: 100dvh; display: grid; place-items: center; padding: 16px; background: linear-gradient(#bfe6ff, var(--page)); }
.form-card { width: 100%; max-width: 620px; padding: 20px 20px 18px; }
.form-card h2 { margin: 0 0 4px; font: 700 28px var(--px); }
.form-card p { margin: 0; color: var(--muted); }
.hero-groups { display: grid; gap: 8px; }
.hero-groups .grp { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.hero-groups .grp b { font: 700 13px var(--px); color: var(--muted); width: 100%; }
.pin-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.codename-big { font: 700 clamp(28px, 7vw, 44px) var(--px); text-align: center; background: #fff4c2; border: 3px dashed var(--ink); border-radius: 12px; padding: 14px; margin: 14px 0; }

/* ---------------------------------------------------------------- map */
.map-screen { min-height: 100dvh; padding: 0 0 40px; background: linear-gradient(#bfe6ff 0, #e9f7ff 180px, var(--page) 420px); }
.topbar { position: sticky; top: 0; z-index: 10; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 10px 16px; background: rgba(255,250,240,.94); border-bottom: 3px solid var(--ink); backdrop-filter: blur(4px); }
.topbar .name { font: 700 20px var(--px); margin-right: auto; display: flex; gap: 10px; align-items: center; }
.topbar .name small { font: 800 13px var(--sans); color: var(--muted); }
.stat { display: inline-flex; flex-direction: column; align-items: center; line-height: 1.05; padding: 4px 12px; border: 2px solid var(--ink); border-radius: 10px; background: #fff; }
.stat b { font: 700 20px var(--px); }
.stat span { font: 800 11px var(--sans); color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.map-head { max-width: 1100px; margin: 18px auto 0; padding: 0 16px; display: flex; gap: 12px; align-items: end; flex-wrap: wrap; }
.map-head h2 { margin: 0; font: 700 30px var(--px); margin-right: auto; }
.mode-switch { display: inline-flex; border: 3px solid var(--ink); border-radius: 12px; overflow: hidden; background: #fff; }
.mode-switch button { border: 0; background: transparent; padding: 6px 14px; font: 800 15px var(--sans); }
.mode-switch button.on { background: var(--ink); color: var(--paper); }
.chapters { max-width: 1100px; margin: 16px auto 0; padding: 0 16px; display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }
.chapter { text-align: left; padding: 0; overflow: hidden; display: flex; flex-direction: column; background: var(--paper); border: 3px solid var(--ink); border-radius: 14px; box-shadow: var(--shadow); transition: transform .08s; }
.chapter:hover { transform: translate(-2px, -2px); box-shadow: 6px 6px 0 rgba(42,31,61,.3); }
.chapter .thumb { position: relative; aspect-ratio: 16 / 8; border-bottom: 3px solid var(--ink); }
.chapter .thumb canvas { width: 100%; height: 100%; }
.chapter .num { position: absolute; left: 8px; top: 8px; font: 700 15px var(--px); background: var(--ink); color: var(--paper); border-radius: 8px; padding: 2px 8px; }
.chapter .beaten { position: absolute; right: 8px; top: 8px; font: 700 14px var(--px); background: var(--gold); border: 2px solid var(--ink); border-radius: 8px; padding: 1px 8px; }
.chapter .body { padding: 10px 14px 12px; display: grid; gap: 4px; }
.chapter h3 { margin: 0; font: 900 20px/1.15 var(--sans); }
.chapter .vs { font-size: 14px; color: var(--muted); }
.chapter .meter { height: 10px; border: 2px solid var(--ink); border-radius: 6px; background: var(--line); overflow: hidden; margin-top: 4px; }
.chapter .meter i { display: block; height: 100%; background: linear-gradient(#7be38f, #3fae5a); }
.chapter .foot { display: flex; justify-content: space-between; align-items: center; font-size: 14px; }
.chapter .stars { color: #d9a21b; font-size: 20px; letter-spacing: 1px; -webkit-text-stroke: .5px var(--ink); }
.chapter.soon { opacity: .6; pointer-events: none; }
.chapter.soon .thumb { filter: grayscale(.6); }
.map-tools { max-width: 1100px; margin: 22px auto 0; padding: 0 16px; display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------------------------------------------------------------- battle */
.battle { height: 100dvh; display: grid; grid-template-rows: minmax(140px, 44%) minmax(0, 1fr); background: var(--paper); }
.battle .scene { position: relative; overflow: hidden; border-bottom: 4px solid var(--ink); background: #bfe6ff; }
.battle .scene canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.hpbox { position: absolute; background: var(--paper); border: 3px solid var(--ink); border-radius: 10px; padding: 5px 10px 6px; box-shadow: 4px 4px 0 rgba(42,31,61,.35); width: min(46%, 300px); }
.hpbox b { font: 700 clamp(13px, 1.7vw, 19px)/1.1 var(--px); display: flex; justify-content: space-between; gap: 10px; }
.hpbox b small { font-size: .78em; color: var(--muted); white-space: nowrap; }
.hpbar { height: 11px; border: 2px solid var(--ink); border-radius: 6px; background: var(--line); overflow: hidden; margin-top: 4px; }
.hpbar i { display: block; height: 100%; background: linear-gradient(#ff9a8a, #e2574c); transition: width .5s steps(8); }
.hearts { display: flex; gap: 2px; margin-top: 3px; }
.hearts svg { width: clamp(15px, 2vw, 20px); height: clamp(15px, 2vw, 20px); }
.hearts svg.lost { opacity: .22; filter: grayscale(1); }
.hp-boss { left: 10px; top: 10px; }
.hp-hero { right: 10px; bottom: 10px; }
.scene .tools { position: absolute; right: 10px; top: 10px; display: flex; gap: 6px; }
.scene .tools .btn.icon { width: 38px; height: 38px; background: rgba(255,250,240,.92); }
.pop-text { position: absolute; font: 700 clamp(20px, 3.4vw, 36px) var(--px); color: #fff; text-shadow: 3px 3px 0 var(--ink), -1px -1px 0 var(--ink); pointer-events: none; animation: rise 1.1s ease-out forwards; white-space: nowrap; }
.pop-text.crit { color: var(--gold); font-size: clamp(24px, 4.2vw, 44px); }
.pop-text.bad { color: #ffb0a6; }
@keyframes rise { from { transform: translate(-50%, 0) scale(.7); opacity: 0; } 15% { transform: translate(-50%, -6px) scale(1.1); opacity: 1; } to { transform: translate(-50%, -46px) scale(1); opacity: 0; } }

.panel { overflow: auto; padding: 12px clamp(12px, 2.4vw, 26px) 16px; display: grid; gap: 10px; align-content: start; }
.panel .qa { display: grid; gap: 10px; align-content: start; }
.qline { display: flex; gap: 10px; align-items: baseline; font: 900 clamp(17px, 2.3vw, 25px)/1.25 var(--sans); }
.qline .tag { flex: none; font: 700 clamp(11px, 1.2vw, 14px) var(--px); background: var(--ink); color: var(--paper); padding: 2px 8px; border-radius: 6px; transform: translateY(-2px); }
.opts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.opts.three { grid-template-columns: 1fr 1fr 1fr; }
.opt { display: flex; align-items: center; gap: 10px; font: 800 clamp(15px, 1.75vw, 20px)/1.2 var(--sans); background: #fff; border: 3px solid var(--ink); border-radius: 12px; padding: .5em .65em; box-shadow: 3px 3px 0 rgba(42,31,61,.3); color: var(--ink); text-align: left; min-height: 56px; transition: transform .06s, background .15s; }
.opt:hover:not([disabled]) { background: #fff4c2; transform: translate(-1px, -1px); }
.opt i { flex: none; font: 700 clamp(13px, 1.4vw, 17px) var(--px); font-style: normal; width: 1.8em; height: 1.8em; display: grid; place-items: center; border-radius: 7px; color: #fff; border: 2px solid var(--ink); }
.opt:nth-child(1) i { background: var(--red); } .opt:nth-child(2) i { background: var(--blue); } .opt:nth-child(3) i { background: var(--orange); } .opt:nth-child(4) i { background: var(--green); }
.opt[disabled] { cursor: default; }
.opt.right { background: #c9f5d0; border-color: #1f6b33; }
.opt.wrong { background: #ffd4cf; border-color: #9a2e25; animation: shake .3s; }
.opt.faded { opacity: .45; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

.source { padding: 10px 12px; display: grid; gap: 6px; align-content: start; background: #fffdf6; }
.source .kind { font: 700 12px var(--px); color: var(--muted); text-transform: uppercase; letter-spacing: 1px; display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.source blockquote { margin: 0; font: 700 clamp(15px, 1.65vw, 19px)/1.45 Georgia, 'Times New Roman', serif; color: #2a1f3d; }
.source blockquote.plain { font-family: var(--sans); font-weight: 800; }
.source cite { font: 700 13px/1.3 var(--sans); color: var(--muted); font-style: normal; }
.source .swap { border: 2px solid var(--ink); background: #fff; border-radius: 8px; font: 800 12px var(--sans); padding: 2px 8px; text-transform: none; letter-spacing: 0; }
.source img, .source .map { width: 100%; max-height: 34vh; object-fit: contain; border-radius: 8px; border: 2px solid var(--ink); background: #f3ead6; cursor: zoom-in; }
.source .map { aspect-ratio: 16 / 10; }
.zoom { max-width: 98vw; max-height: 96dvh; display: grid; gap: 8px; padding: 8px; }
.zoom img.map, .zoom img { width: min(94vw, 1400px); height: auto; max-height: 84dvh; border: 0; background: none; cursor: zoom-out; }
.zoom img, .zoom canvas, .zoom svg { max-width: 100%; max-height: 80dvh; object-fit: contain; margin: auto; }

.explain { padding: 12px 14px; display: grid; gap: 8px; animation: slide .25s ease-out; }
.explain.good { background: #e8fbe9; } .explain.bad { background: #fff0ec; }
.explain h4 { margin: 0; font: 700 20px var(--px); }
.explain.good h4 { color: #1f6b33; } .explain.bad h4 { color: #9a2e25; }
.explain p { margin: 0; font-size: clamp(15px, 1.6vw, 18px); }
.explain .row { justify-content: flex-end; }
@keyframes slide { from { transform: translateY(8px); opacity: 0; } }

/* Wide screens: the source sits beside the question */
@media (min-width: 900px) and (min-aspect-ratio: 5/4) {
  .battle.has-src .panel { grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); align-items: start; }
  .battle.has-src .source img, .battle.has-src .source .map { max-height: 40dvh; }
  .battle.has-src .opts.three { grid-template-columns: 1fr; }
}
/* Phones: shorter scene, answers stack */
@media (max-width: 640px) {
  .battle { grid-template-rows: minmax(130px, 32%) minmax(0, 1fr); }
  .battle.has-src { grid-template-rows: minmax(110px, 24%) minmax(0, 1fr); }
  .opts, .opts.three { grid-template-columns: 1fr; gap: 8px; }
  .opt { min-height: 50px; }
  .hpbox { width: 52%; padding: 4px 8px; }
  .scene .tools { top: auto; bottom: 10px; left: 10px; right: auto; }
  .source img, .source .map { max-height: 26dvh; }
}

/* ---------------------------------------------------------------- results */
.results { min-height: 100dvh; display: grid; place-items: center; padding: 16px; background: linear-gradient(#ffe9a8, var(--page)); }
.results .card { width: 100%; max-width: 640px; padding: 20px; text-align: center; }
.results h2 { margin: 0; font: 700 clamp(30px, 6vw, 44px) var(--px); }
.results .bigstars { font-size: 54px; color: #f2c14e; -webkit-text-stroke: 2px var(--ink); letter-spacing: 6px; margin: 4px 0; }
.results .bigstars .off { color: #e8dfcc; }
.results .stats { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin: 10px 0 14px; }
.results .duo { display: flex; justify-content: center; align-items: end; gap: 18px; margin: 6px 0 4px; }
.results .duo canvas { width: 112px; height: 144px; }
.misslist { text-align: left; display: grid; gap: 8px; margin: 12px 0; }
.misslist .m { border: 2px solid var(--ink); border-radius: 10px; padding: 8px 12px; background: #fff; }
.misslist .m b { display: block; }
.misslist .m .a { color: #1f6b33; }

/* ---------------------------------------------------------------- review */
.review { min-height: 100dvh; padding: 16px; display: grid; place-items: start center; background: linear-gradient(#e6dcff, var(--page)); }
.review .card { width: 100%; max-width: 820px; padding: 16px 18px; display: grid; gap: 12px; }
.review .head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.review .head b { font: 700 22px var(--px); }

/* ---------------------------------------------------------------- board */
.board { width: min(640px, 100%); padding: 16px; max-height: 90dvh; overflow: auto; }
.board h3 { margin: 0 0 8px; font: 700 26px var(--px); }
.board table { width: 100%; border-collapse: collapse; font-size: 15px; }
.board th { text-align: left; font: 700 13px var(--px); color: var(--muted); padding: 4px 6px; }
.board td { padding: 6px; border-top: 2px solid var(--line); }
.board tr.me td { background: #fff4c2; }

/* ---------------------------------------------------------------- android joke */
.android-tax { width: 100%; max-width: 460px; padding: 20px 18px 18px; text-align: center; }
.android-tax .stamp { display: inline-block; font: 700 clamp(26px, 7vw, 38px)/1.1 var(--px); text-transform: uppercase; color: var(--red); border: 4px solid var(--red); border-radius: 10px; padding: 6px 14px; transform: rotate(-3deg); animation: slam .35s ease-out; }
.android-tax .sub { margin: 12px 0; font-weight: 800; }
.android-tax .speech { display: flex; gap: 12px; align-items: flex-start; text-align: left; background: #e8fbe9; border: 2px solid var(--ink); border-radius: 12px; padding: 10px 12px; margin-bottom: 14px; }
.android-tax .speech canvas { flex: none; }
.android-tax .speech p { margin: 4px 0 0; }
.stat.taxed b { color: var(--red); }
@keyframes slam { from { transform: rotate(-3deg) scale(2.2); opacity: 0; } }

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01s !important; transition-duration: .01s !important; } }
