:root {
  --ink: #1a1c2c;
  --plum: #5d275d;
  --red: #b13e53;
  --orange: #ef7d57;
  --gold: #ffcd75;
  --lime: #a7f070;
  --green: #38b764;
  --teal: #257179;
  --navy: #29366f;
  --blue: #3b5dc9;
  --sky: #41a6f6;
  --cyan: #73eff7;
  --white: #f4f4f4;
  --silver: #94b0c2;
  --slate: #566c86;
  --dusk: #333c57;
  --bg: #1a1c2c;
  --panel: #232640;
  --panel-2: #2b2f4f;
  --text: #e8ecf2;
  --dim: #8a9bb3;
  --pixel: 'Press Start 2P', monospace;
  --mono: 'VT323', ui-monospace, 'Cascadia Mono', Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font-family: var(--mono); font-size: 20px; line-height: 1.2; overflow: hidden; }
button { font: inherit; }
.hidden { display: none !important; }
.dim { color: var(--dim); }
.small { font-size: 17px; }

kbd {
  display: inline-block;
  min-width: 1.3em;
  padding: 0 5px;
  margin: 0 1px;
  font-family: var(--mono);
  font-size: 0.95em;
  line-height: 1.15;
  text-align: center;
  color: var(--ink);
  background: var(--white);
  border: 2px solid var(--ink);
  border-bottom-width: 4px;
  border-radius: 3px;
  vertical-align: 1px;
}
code { font-family: var(--mono); color: var(--gold); background: rgba(0,0,0,.3); padding: 0 4px; white-space: pre; }

#app { display: grid; grid-template-rows: 48px 1fr; height: 100vh; }

/* top bar */
#topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 14px; background: var(--ink); border-bottom: 3px solid var(--dusk);
}
.brand { display: flex; align-items: baseline; gap: 16px; min-width: 0; }
.logo { font-family: var(--pixel); font-size: 14px; color: var(--gold); text-shadow: 2px 2px 0 var(--red); letter-spacing: 1px; }
.level-name { color: var(--silver); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-right { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.rank { color: var(--cyan); }
.star { color: var(--gold); }

.px-btn {
  cursor: pointer; color: var(--text); background: var(--panel-2); padding: 2px 10px;
  border: 2px solid var(--ink); box-shadow: inset -2px -2px 0 var(--dusk), inset 2px 2px 0 var(--slate);
}
.px-btn:hover { background: var(--dusk); }
.px-btn:active { box-shadow: inset 2px 2px 0 var(--dusk); }
.px-btn:focus-visible { outline: 2px solid var(--gold); }

/* stage */
#stage { display: grid; grid-template-columns: minmax(0, 1fr) 330px; min-height: 0; }
#world-col { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#world-wrap { position: relative; flex: 1 1 auto; min-height: 220px; overflow: hidden; background: var(--ink); }
#world { position: absolute; top: 0; left: 0; display: block; image-rendering: pixelated; image-rendering: crisp-edges; }
#modebar {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%);
  font-family: var(--pixel); font-size: 11px; padding: 6px 10px; color: var(--ink);
  background: var(--gold); border: 3px solid var(--ink); display: none;
}
#modebar.insert { display: block; background: var(--lime); }
#modebar.visual { display: block; background: var(--sky); }

.toast {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  max-width: min(90%, 620px); padding: 8px 14px; text-align: center;
  background: var(--ink); border: 3px solid var(--gold); color: var(--text);
  box-shadow: 4px 4px 0 rgba(0,0,0,.4); z-index: 20;
}
.toast.bad { border-color: var(--red); }
.toast.good { border-color: var(--lime); }

.panel {
  background: var(--panel); border: 3px solid var(--ink);
  box-shadow: inset 0 0 0 2px var(--dusk); padding: 8px 12px;
}
.panel-title { font-family: var(--pixel); font-size: 10px; color: var(--gold); margin: 2px 0 8px; letter-spacing: 1px; }
.panel-title .dim { font-family: var(--mono); font-size: 16px; letter-spacing: 0; }

/* scroll (buffer view) */
#scroll { flex: 0 0 auto; max-height: 42%; display: flex; flex-direction: column; min-height: 0; }
#buffer { overflow: auto; font-family: var(--mono); font-size: 22px; line-height: 24px; white-space: pre; min-height: 0; flex: 1 1 auto; }
.bl { display: flex; }
.ln { display: inline-block; width: 3.2ch; text-align: right; padding-right: 1ch; color: var(--slate); flex: 0 0 auto; user-select: none; }
.ln.cur { color: var(--gold); }
.bl.wrong .tx { background: rgba(177, 62, 83, .18); }
.bl.right .tx { background: rgba(56, 183, 100, .12); }
.tx { flex: 0 0 auto; }
.cur-n { background: var(--gold); color: var(--ink); }
.cur-i { box-shadow: -2px 0 0 var(--lime); }
.sel { background: var(--blue); color: var(--white); }
.tgt { color: var(--ink); background: var(--lime); animation: pulse 1s steps(2) infinite; }
.tgt.gem { background: var(--cyan); }
@keyframes pulse { 50% { filter: brightness(1.35); } }
#statusline {
  display: flex; gap: 16px; margin-top: 6px; padding: 2px 8px; background: var(--dusk); color: var(--white);
}
#st-mode { font-weight: bold; min-width: 9ch; }
#st-pending { color: var(--gold); flex: 1; }
#st-pos { color: var(--silver); }
#cmdline { min-height: 24px; font-family: var(--mono); font-size: 22px; padding: 0 8px; white-space: pre; }
#cmdline .err { color: var(--orange); }

/* sidebar */
#sidebar { display: flex; flex-direction: column; gap: 6px; padding: 6px; overflow-y: auto; background: var(--bg); min-height: 0; }
#objective { color: var(--white); margin-bottom: 6px; }
.hint { color: var(--cyan); margin: 6px 0; min-height: 1.2em; }
.hint:empty { display: none; }
#goal { font-family: var(--mono); font-size: 18px; }
#goal .gl { white-space: pre; overflow-x: auto; padding: 1px 4px; border-left: 3px solid var(--red); margin: 2px 0; background: rgba(0,0,0,.25); }
#goal .gl.ok { border-color: var(--green); color: var(--lime); }
#goal .req { color: var(--orange); margin-top: 4px; }
#score { margin-top: 8px; color: var(--silver); }
#score b { color: var(--gold); }
.side-actions { display: flex; gap: 8px; margin-top: 10px; }
#spells, #log { list-style: none; margin: 0; padding: 0; }
#spells li { display: flex; gap: 8px; align-items: baseline; padding: 1px 0; }
#spells li .k { flex: 0 0 92px; color: var(--gold); white-space: nowrap; }
#spells li .d { color: var(--dim); font-size: 18px; }
#spells li.new .k { color: var(--lime); }
#spells li.new .d { color: var(--text); }
#spells li.new .k::after { content: ' NEW'; font-size: 13px; color: var(--ink); background: var(--lime); padding: 0 3px; margin-left: 4px; }
#log li { display: flex; gap: 10px; font-size: 18px; color: var(--dim); }
#log li .k { color: var(--white); flex: 0 0 76px; white-space: pre; overflow: hidden; text-overflow: ellipsis; }
#log li.bad .k { color: var(--orange); }
#log li:first-child .k { color: var(--gold); }

/* overlays */
.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(26, 28, 44, .72); z-index: 10; padding: 16px;
}
#title-screen { background: linear-gradient(rgba(26,28,44,.35), rgba(26,28,44,.8)); }
.title-card, .map-card, .complete-card, .dialog-box, .paywall-card {
  background: var(--panel); border: 4px solid var(--ink); box-shadow: inset 0 0 0 3px var(--dusk), 8px 8px 0 rgba(0,0,0,.45);
}
.title-card { text-align: center; padding: 28px 36px; max-width: 640px; }
.title-card h1 { font-family: var(--pixel); font-size: clamp(26px, 6vw, 48px); color: var(--gold); text-shadow: 4px 4px 0 var(--red), 8px 8px 0 var(--ink); margin: 10px 0 16px; }
.subtitle { font-family: var(--pixel); font-size: 11px; color: var(--cyan); }
.blurb { color: var(--silver); font-size: 22px; }
.press { color: var(--white); animation: blink 1.2s steps(2) infinite; }
.dialog-foot .press, .complete-card .press { animation: none; }
@keyframes blink { 50% { opacity: .35; } }

.map-card { width: min(760px, 100%); max-height: 92vh; overflow-y: auto; padding: 18px 24px; }
.map-card h2, .complete-card h2 { font-family: var(--pixel); font-size: 16px; color: var(--gold); margin: 6px 0 10px; }
.chapter { margin: 12px 0 4px; }
.chapter-name { font-family: var(--pixel); font-size: 10px; color: var(--cyan); }
.chapter-name .dim { font-family: var(--mono); font-size: 17px; }
.quest-row {
  display: flex; align-items: center; gap: 12px; padding: 4px 10px; margin: 3px 0; cursor: pointer;
  border: 2px solid transparent; background: rgba(0,0,0,.2);
}
.quest-row .qn { color: var(--slate); width: 2.5ch; }
.quest-row .qt { flex: 1; }
.quest-row .qs { color: var(--gold); letter-spacing: 2px; }
.quest-row .qs .off { color: var(--dusk); }
.quest-row.sel { border-color: var(--gold); background: var(--panel-2); }
.quest-row.sel .qn::before { content: '▸'; color: var(--gold); margin-left: -1.2ch; position: absolute; }
.quest-row.locked { color: var(--slate); cursor: not-allowed; }
.quest-row.sealed { color: var(--silver); cursor: pointer; }
.quest-row.sealed .qs { color: var(--gold); letter-spacing: 0; }
.seal-tag { font-family: var(--pixel); font-size: 8px; color: var(--ink); background: var(--gold); padding: 2px 4px; margin-left: 6px; vertical-align: 2px; }
.map-foot { margin-top: 16px; text-align: right; }

/* paywall */
.paywall-card { width: min(620px, 100%); max-height: 92vh; overflow-y: auto; padding: 20px 28px; }
.paywall-card h2 { font-family: var(--pixel); font-size: 16px; color: var(--gold); margin: 6px 0 12px; text-align: center; }
.paywall-card .blurb { margin: 0 0 10px; }
.perks { margin: 0 0 16px; padding-left: 0; list-style: none; }
.perks li { padding: 2px 0 2px 1.6ch; text-indent: -1.6ch; }
.perks li::before { content: '✦ '; color: var(--lime); }
.buy-row { text-align: center; margin: 8px 0 4px; }
.px-btn.buy { font-family: var(--pixel); font-size: 12px; padding: 10px 16px; background: var(--green); color: var(--ink); box-shadow: inset -3px -3px 0 var(--teal), inset 3px 3px 0 var(--lime); }
.px-btn.buy:hover { background: var(--lime); }
.px-btn:disabled { opacity: .6; cursor: wait; }
.buy-note { text-align: center; margin: 4px 0 16px; }
#signin-form { border-top: 2px dashed var(--dusk); padding-top: 12px; }
.signin-row { display: flex; gap: 8px; margin-top: 6px; }
#signin-email {
  flex: 1; min-width: 0; font: inherit; color: var(--text); background: var(--ink);
  border: 2px solid var(--dusk); padding: 2px 8px;
}
#signin-email:focus { outline: none; border-color: var(--lime); }
#paywall-msg { min-height: 1.2em; margin: 10px 0 0; color: var(--cyan); }
#paywall-msg.bad { color: var(--orange); }
.paywall-card .press { text-align: center; animation: none; margin: 12px 0 0; }

.dialog-box { display: flex; gap: 18px; width: min(760px, 100%); padding: 18px 20px; align-self: flex-end; margin-bottom: 4vh; }
#portrait { width: 96px; height: 144px; image-rendering: pixelated; flex: 0 0 auto; background: var(--ink); border: 3px solid var(--dusk); padding: 4px; }
.dialog-body { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.speaker { font-family: var(--pixel); font-size: 11px; color: var(--cyan); margin-bottom: 10px; }
#dialog-text { font-size: 23px; line-height: 1.35; flex: 1; }
.dialog-foot { display: flex; justify-content: space-between; margin-top: 12px; color: var(--dim); }

.complete-card { text-align: center; padding: 22px 32px; min-width: min(460px, 100%); }
.big-stars { font-size: 56px; letter-spacing: 8px; color: var(--gold); text-shadow: 3px 3px 0 var(--ink); }
.big-stars .off { color: var(--dusk); }
#complete-stats { color: var(--silver); margin: 8px 0; }
#complete-new { color: var(--lime); min-height: 1.2em; }

@media (max-width: 900px) {
  body { overflow: auto; }
  #app { height: auto; min-height: 100vh; }
  #stage { grid-template-columns: 1fr; }
  #world-wrap { height: 52vh; flex: none; }
  #scroll { max-height: none; }
  #buffer { max-height: 40vh; }
  #sidebar { overflow: visible; }
  .rank { display: none; }
}
