:root {
  --ink: #0b0c12;
  --panel: rgba(14, 15, 22, 0.86);
  --panel-solid: #12131b;
  --edge: rgba(150, 140, 120, 0.28);
  --edge-strong: rgba(190, 175, 140, 0.5);
  --text: #e8dfc8;
  --muted: #a39b86;
  --dead: #6dffa8;
  --dead-dim: #2f7a55;
  --gold: #ffd27a;
  --purple: #b78cff;
  --blood: #e2524a;
  --mana: #5fb6ff;
  --font-title: "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --font-body: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --radius: 10px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--ink);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
[hidden] { display: none !important; }
canvas#world, canvas#overlay { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; }
canvas#overlay { pointer-events: none; z-index: 2; }

button, select {
  font: inherit;
  color: var(--text);
  background: linear-gradient(#262734, #1a1b24);
  border: 1px solid var(--edge-strong);
  border-radius: 8px;
  padding: 0.55em 1.1em;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
}
button:hover { border-color: var(--gold); background: linear-gradient(#30313f, #20212c); }
button:active { transform: translateY(1px); }
button:focus-visible, select:focus-visible, a:focus-visible, input:focus-visible { outline: 2px solid var(--dead); outline-offset: 2px; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: linear-gradient(#2a5a43, #1b3d2e); border-color: var(--dead); color: #eafff2; }
button.primary:hover { background: linear-gradient(#347053, #214b38); }
button.big { font-size: 1.15rem; padding: 0.7em 1.6em; }
kbd {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.8em;
  border: 1px solid var(--edge-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.35em;
  background: #1c1d27;
}
.ico svg { width: 1.2em; height: 1.2em; display: block; }

/* ---------------------------------------------------------------- loading and screens */
#loading {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: radial-gradient(circle at 50% 40%, #182030, #07080c 70%);
}
.loading-card { text-align: center; }
.spinner {
  width: 46px; height: 46px; margin: 1.2rem auto;
  border-radius: 50%;
  border: 3px solid rgba(109, 255, 168, 0.15);
  border-top-color: var(--dead);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.logo {
  font-family: var(--font-title);
  font-size: clamp(3rem, 9vw, 6.5rem);
  letter-spacing: 0.06em;
  color: #e9e1cb;
  text-shadow: 0 0 30px rgba(109, 255, 168, 0.35), 0 4px 0 #000;
}
.logo span { color: var(--dead); }
.logo.small { font-size: 2.4rem; }

.screen {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  padding: 16px;
  overflow-y: auto;
  background: radial-gradient(ellipse at 50% 30%, rgba(10, 14, 22, 0.25), rgba(4, 5, 8, 0.82) 75%);
}
.title-card { text-align: center; max-width: 640px; }
.tagline { font-style: italic; color: var(--muted); font-size: 1.15rem; margin: 0.2rem 0 2rem; }
.title-buttons { display: flex; flex-direction: column; gap: 0.7rem; align-items: center; }
.title-buttons button { min-width: 260px; }
.foot { color: var(--muted); font-size: 0.9rem; margin-top: 2rem; }
.home-link { color: var(--muted); text-decoration: none; font-size: 0.95rem; }
.home-link:hover { color: var(--gold); }

.chapters-card, .story-card {
  width: min(760px, 100%);
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 14px;
  padding: clamp(18px, 4vw, 38px);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}
.chapters-card h1, .story-card h1 { font-family: var(--font-title); font-weight: 600; margin: 0 0 1rem; letter-spacing: 0.03em; }
#chapter-list { display: grid; gap: 0.6rem; margin-bottom: 1.4rem; }
.chapter {
  display: grid; grid-template-columns: 3.2rem 1fr auto; align-items: center; gap: 0.9rem;
  text-align: left; padding: 0.8rem 1rem;
}
.chapter .num { font-family: var(--font-title); font-size: 1.6rem; color: var(--dead); text-align: center; }
.chapter .name { font-size: 1.1rem; }
.chapter .place { display: block; color: var(--muted); font-size: 0.85rem; }
.chapter .done { color: var(--gold); font-size: 0.85rem; }
.chapter.locked { opacity: 0.4; }
.difficulty { margin: 0.4rem 0 1.2rem; }
.difficulty > span { color: var(--muted); margin-right: 0.6rem; }
#difficulty-buttons { display: inline-flex; gap: 0.4rem; flex-wrap: wrap; }
#difficulty-buttons button[aria-checked="true"] { border-color: var(--dead); color: var(--dead); }
#difficulty-blurb { color: var(--muted); font-size: 0.9rem; margin: 0.5rem 0 0; }
.row { display: flex; gap: 0.6rem; flex-wrap: wrap; justify-content: flex-end; margin-top: 1rem; }
.col { display: flex; flex-direction: column; gap: 0.6rem; }

.story-chapter { color: var(--dead); letter-spacing: 0.25em; text-transform: uppercase; font-size: 0.8rem; }
.story-card h1 { font-size: clamp(1.8rem, 5vw, 2.8rem); margin: 0.3rem 0 0.2rem; }
.story-place { color: var(--muted); font-style: italic; margin-bottom: 1.2rem; }
.story-text p { font-size: 1.12rem; line-height: 1.65; margin: 0 0 1rem; animation: rise 0.8s both; }
.story-text p:nth-child(2) { animation-delay: 0.25s; }
.story-text p:nth-child(3) { animation-delay: 0.5s; }
.story-text p:nth-child(4) { animation-delay: 0.75s; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.story-goals { margin: 0.4rem 0 0; padding-left: 1.2rem; color: var(--gold); }
.story-goals li { margin: 0.2rem 0; }
.story-choices { display: grid; gap: 0.7rem; margin-top: 1rem; }
.story-choices button { text-align: left; padding: 0.9rem 1.1rem; }
.story-choices button b { display: block; font-size: 1.1rem; color: var(--dead); }
.story-choices button span { color: var(--muted); font-size: 0.95rem; }
.story-choices button.crown b { color: var(--purple); }

/* ---------------------------------------------------------------- HUD */
#hud { position: fixed; inset: 0; z-index: 5; pointer-events: none; }
#hud > * { pointer-events: auto; }
#topbar {
  position: absolute; top: 8px; left: 8px; right: 8px;
  display: flex; align-items: center; gap: 0.5rem;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
#topbar .spacer { flex: 1; pointer-events: none; }
.res {
  display: flex; align-items: center; gap: 0.45rem;
  background: var(--panel); border: 1px solid var(--edge); border-radius: 999px;
  padding: 0.3rem 0.8rem; font-variant-numeric: tabular-nums;
}
.res b { font-size: 1.02rem; }
.res small { color: var(--muted); font-size: 0.75rem; }
#res-souls .ico { color: #8fffd0; }
#res-mana .ico { color: var(--mana); }
#res-dominion .ico { color: var(--gold); }
.res .bar { width: 90px; height: 7px; background: #0a0b10; border-radius: 4px; overflow: hidden; border: 1px solid #000; }
#mana-fill { height: 100%; width: 0; background: linear-gradient(90deg, #3a7fd8, #7ccaff); transition: width 0.2s; }
#res-dominion.full b { color: var(--blood); }
#clock { background: var(--panel); border: 1px solid var(--edge); border-radius: 999px; padding: 0.3rem 0.8rem; font-variant-numeric: tabular-nums; }
.icon-btn { display: inline-flex; align-items: center; gap: 0.3rem; padding: 0.35rem 0.6rem; border-radius: 999px; background: var(--panel); }

#objectives {
  position: absolute; top: 52px; right: 8px; width: min(330px, 46vw);
  background: var(--panel); border: 1px solid var(--edge); border-radius: var(--radius);
  padding: 0.5rem 0.8rem 0.6rem;
}
.obj-head { display: flex; justify-content: space-between; align-items: center; font-family: var(--font-title); color: var(--gold); letter-spacing: 0.05em; }
.obj-head button { padding: 0 0.5rem; line-height: 1.3; }
#obj-list { list-style: none; padding: 0; margin: 0.35rem 0 0; font-size: 0.92rem; }
#obj-list li { display: flex; gap: 0.45rem; padding: 0.18rem 0; line-height: 1.3; }
#obj-list li::before { content: "◇"; color: var(--gold); flex: none; }
#obj-list li.done { color: var(--muted); text-decoration: line-through; }
#obj-list li.done::before { content: "◆"; color: var(--dead); }
#obj-list li.optional { color: #c9bfa6; font-style: italic; }
#obj-list li .count { color: var(--dead); margin-left: auto; font-variant-numeric: tabular-nums; }
#objectives.collapsed #obj-list { display: none; }
#obj-list li.focusable { cursor: pointer; }
#obj-list li.focusable:hover span:first-child { color: var(--gold); text-decoration: underline dotted; }
#obj-list li.fresh { animation: flash 1.2s; }
@keyframes flash { 0%, 40% { background: rgba(109, 255, 168, 0.18); } }

#hint {
  position: absolute; top: 52px; left: 50%; transform: translateX(-50%);
  width: min(560px, calc(100vw - 380px));
  background: rgba(16, 30, 24, 0.92); border: 1px solid var(--dead-dim); border-radius: var(--radius);
  padding: 0.65rem 2.2rem 0.65rem 0.9rem; font-size: 0.95rem; line-height: 1.45;
}
#hint p { margin: 0; }
#hint button { position: absolute; top: 4px; right: 4px; padding: 0 0.5rem; background: none; border: none; font-size: 1.2rem; }
#notice {
  position: absolute; top: 34%; left: 50%; transform: translateX(-50%);
  font-family: var(--font-title); font-size: 1.25rem; color: var(--gold);
  text-shadow: 0 2px 8px #000; pointer-events: none; text-align: center; white-space: nowrap;
  opacity: 0; transition: opacity 0.3s;
}
#notice.show { opacity: 1; }
#notice.bad { color: #ff9a8a; }
#alert {
  position: absolute; left: 50%; top: 150px; transform: translateX(-50%);
  background: rgba(70, 14, 18, 0.92); border: 1px solid #c33; color: #ffd9d4;
  padding: 0.35rem 0.9rem; border-radius: 999px; font-size: 0.95rem; cursor: pointer;
}

#dialogue {
  position: absolute; left: 8px; bottom: calc(206px + var(--safe-b));
  width: min(470px, calc(100vw - 16px));
  display: flex; gap: 0.8rem; align-items: center;
  background: rgba(10, 11, 16, 0.92); border: 1px solid var(--edge-strong); border-radius: 12px;
  padding: 0.6rem 0.9rem; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  animation: rise 0.25s both; cursor: pointer;
}
#dialogue .portrait { width: 58px; height: 58px; flex: none; border-radius: 10px; background: #050608; border: 1px solid var(--edge); padding: 4px; }
#dialogue .portrait svg { width: 100%; height: 100%; }
#dialogue b { font-family: var(--font-title); letter-spacing: 0.04em; }
#dialogue p { margin: 0.2rem 0 0; line-height: 1.4; font-size: 1.02rem; }

#bottom {
  position: absolute; left: 8px; right: 8px; bottom: calc(8px + var(--safe-b));
  display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: end;
  pointer-events: none;
}
#bottom > * { pointer-events: auto; }
#minimap-wrap { background: var(--panel); border: 1px solid var(--edge-strong); border-radius: var(--radius); padding: 4px; line-height: 0; }
#minimap { width: 180px; height: 180px; border-radius: 6px; cursor: pointer; image-rendering: auto; }

/* The Gravecaller's bar. */
#herobar { justify-self: center; display: flex; flex-direction: column; align-items: center; gap: 6px; }
#roster {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: center;
  background: var(--panel); border: 1px solid var(--edge); border-radius: 999px; padding: 0.2rem 0.8rem; font-size: 0.85rem;
}
#roster .stance { color: var(--muted); font-style: italic; margin-right: 0.2rem; }
#roster .stance.guard { color: var(--gold); }
#roster .r { display: inline-flex; align-items: center; gap: 2px; color: var(--dead); }
#roster .r svg { width: 20px; height: 20px; }
#roster .r b { color: var(--text); font-variant-numeric: tabular-nums; }
.hero-row {
  display: flex; gap: 8px; align-items: center;
  background: var(--panel); border: 1px solid var(--edge-strong); border-radius: 14px; padding: 6px 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}
#hero-portrait { position: relative; width: 62px; height: 62px; padding: 3px; border-radius: 12px; background: #08070c; }
#hero-portrait .portrait { display: block; width: 100%; height: 100%; }
#hero-portrait .portrait svg { width: 100%; height: 100%; }
#hero-portrait .level {
  position: absolute; left: -6px; bottom: -6px; width: 24px; height: 24px; border-radius: 50%;
  background: #2b1d44; border: 2px solid #b78cff; color: #f1e6ff; font-weight: 700; font-size: 0.8rem;
  display: grid; place-items: center;
}
#hero-portrait .gambit {
  position: absolute; right: -6px; bottom: -6px; width: 22px; height: 22px; border-radius: 50%;
  background: #0d1712; border: 1px solid var(--edge); display: grid; place-items: center; font-size: 0.7rem; color: var(--dead);
}
#hero-portrait .gambit.cooling { color: var(--muted); }
#hero-portrait.hurt { animation: hurt 0.6s infinite alternate; }
@keyframes hurt { to { border-color: var(--blood); box-shadow: 0 0 18px rgba(226, 82, 74, 0.6); } }
.hero-bars { display: flex; flex-direction: column; gap: 4px; width: 170px; }
.hbar { position: relative; height: 14px; background: #0a0b10; border-radius: 4px; overflow: hidden; border: 1px solid #000; }
.hbar.xp { height: 12px; }
.hbar i { display: block; height: 100%; transition: width 0.15s; }
.hbar.hp i { background: linear-gradient(90deg, #8a5ad8, #c9a6ff); }
.hbar.mp i { background: linear-gradient(90deg, #3a7fd8, #7ccaff); }
.hbar.xp i { background: linear-gradient(90deg, #5d3f13, #ffd27a); }
.hbar span { position: absolute; inset: 0; display: grid; place-items: center; font-size: 0.68rem; color: #fff; text-shadow: 0 1px 2px #000; font-variant-numeric: tabular-nums; }
.hbar.xp span { font-size: 0.62rem; }
#spells, #horde { display: flex; gap: 5px; }
#horde { padding-left: 8px; border-left: 1px solid var(--edge); }

#gifts {
  position: absolute; right: 8px; bottom: calc(150px + var(--safe-b));
  width: min(360px, calc(100vw - 16px)); text-align: center; animation: rise 0.3s both;
}
.gifts-head { font-family: var(--font-title); color: #d8b8ff; text-shadow: 0 2px 8px #000; margin-bottom: 6px; letter-spacing: 0.04em; }
.gift-cards { display: flex; flex-direction: column; gap: 8px; }
.gift {
  text-align: left; position: relative; padding: 0.7rem 0.9rem 0.7rem 2.4rem;
  background: linear-gradient(#2a1d3d, #171021); border-color: #8b6bc9; line-height: 1.35;
}
.gift:hover { border-color: #d8b8ff; background: linear-gradient(#352551, #1d1429); }
.gift .k { position: absolute; left: 0.7rem; top: 0.7rem; font-family: ui-monospace, Consolas, monospace; color: #b78cff; }
.gift b { display: block; color: #f1e6ff; font-family: var(--font-title); }
.gift span:last-child { font-size: 0.88rem; color: #d6cbe6; }

.cmd {
  position: relative; width: 52px; height: 52px; padding: 0;
  display: grid; place-items: center; color: var(--text); overflow: hidden;
}
.cmd svg { width: 28px; height: 28px; }
.cmd .glyph { display: grid; place-items: center; }
.cmd .key { position: absolute; top: 2px; left: 4px; font-size: 0.7rem; color: var(--muted); font-family: ui-monospace, Consolas, monospace; }
.cmd .cost { position: absolute; bottom: 1px; right: 3px; font-size: 0.68rem; color: #8fffd0; }
.cmd .cost.mana { color: #8ccfff; }
.cmd .cd { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.62); transform-origin: bottom; pointer-events: none; }
.cmd .cdtext { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 700; text-shadow: 0 1px 3px #000; pointer-events: none; }
.cmd.spell { color: var(--dead); }
.cmd.on { border-color: var(--dead); box-shadow: inset 0 0 12px rgba(109, 255, 168, 0.35); }
.cmd.active { border-color: var(--gold); box-shadow: inset 0 0 12px rgba(255, 210, 122, 0.4); }
.cmd.cant { color: #6b6658; }
.cmd.owned { border-color: var(--dead-dim); color: var(--dead-dim); }
.cmd.new { animation: newcmd 1.4s 3; }
@keyframes newcmd { 50% { box-shadow: 0 0 18px var(--dead); border-color: var(--dead); } }

#tooltip {
  position: fixed; z-index: 30; max-width: 300px; pointer-events: none;
  background: rgba(8, 9, 13, 0.96); border: 1px solid var(--edge-strong); border-radius: 8px;
  padding: 0.55rem 0.7rem; font-size: 0.88rem; line-height: 1.4;
}
#tooltip b { display: block; font-family: var(--font-title); color: var(--gold); margin-bottom: 0.15rem; }
#tooltip .cost { color: #8fffd0; }
#tooltip .k { color: var(--muted); }

#touch-bar { display: none; position: absolute; right: 8px; top: 42%; flex-direction: column; gap: 6px; }
#touch-bar button { display: flex; gap: 0.3rem; align-items: center; padding: 0.4rem 0.7rem; background: var(--panel); }
#touch-bar button.on { border-color: var(--dead); color: var(--dead); }
body.touch #touch-bar { display: flex; }

/* ---------------------------------------------------------------- modals */
.modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; background: rgba(3, 4, 7, 0.66); padding: 16px; overflow-y: auto; }
.modal-card {
  width: min(420px, 100%);
  background: var(--panel-solid); border: 1px solid var(--edge-strong); border-radius: 14px;
  padding: 1.4rem 1.6rem; box-shadow: 0 30px 80px rgba(0, 0, 0, 0.7);
}
.modal-card.wide { width: min(940px, 100%); }
.modal-card h2 { margin: 0 0 1rem; font-family: var(--font-title); font-weight: 600; letter-spacing: 0.04em; }
.modal-card h3 { margin: 0.9rem 0 0.3rem; font-family: var(--font-title); color: var(--gold); font-weight: 600; }
.modal-card p { line-height: 1.55; margin: 0.2rem 0; }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.6rem; }
.keys { padding-left: 1.1rem; margin: 0.2rem 0; line-height: 1.7; }
.setting { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin: 0.7rem 0; }
.setting.check { justify-content: flex-start; }
.setting input[type="range"] { width: 55%; accent-color: var(--dead); }
.setting input[type="checkbox"] { accent-color: var(--dead); width: 18px; height: 18px; }
#result-stats { display: grid; grid-template-columns: auto auto; gap: 0.25rem 1.2rem; margin: 1rem 0; color: var(--muted); }
#result-stats dd { margin: 0; color: var(--text); text-align: right; font-variant-numeric: tabular-nums; }
#result-title.victory { color: var(--dead); }
#result-title.defeat { color: var(--blood); }
.noscript { padding: 2rem; text-align: center; }

/* ---------------------------------------------------------------- small screens */
@media (max-width: 980px) {
  #minimap { width: 128px; height: 128px; }
  .cmd { width: 44px; height: 44px; }
  .cmd svg { width: 24px; height: 24px; }
  .hero-bars { width: 120px; }
  #hero-portrait { width: 50px; height: 50px; }
  #gifts { bottom: calc(126px + var(--safe-b)); width: min(320px, calc(100vw - 16px)); }
  .gift { padding: 0.45rem 0.6rem 0.45rem 2rem; font-size: 0.85rem; }
  #hint { top: 112px; left: 8px; transform: none; width: min(440px, calc(100vw - 340px)); font-size: 0.88rem; }
  #dialogue { bottom: calc(150px + var(--safe-b)); width: min(420px, calc(100vw - 16px)); }
  .help-grid { grid-template-columns: 1fr; }
  #objectives { top: 104px; width: min(300px, 60vw); font-size: 0.85rem; }
  .res .bar { width: 52px; }
}
@media (max-width: 640px) {
  #bottom { grid-template-columns: 1fr; }
  #minimap-wrap { display: none; }
  .hero-row { flex-wrap: wrap; justify-content: center; }
  .hero-bars { width: 110px; }
  #horde { border-left: none; padding-left: 0; }
  .cmd { width: 40px; height: 40px; }
  #gifts { left: 8px; right: 8px; width: auto; bottom: calc(222px + var(--safe-b)); }
  body.gifting #dialogue { display: none; }
  .gift { padding: 0.5rem 0.6rem 0.5rem 2rem; font-size: 0.85rem; }
  #topbar { flex-wrap: wrap; }
  .res { padding: 0.2rem 0.55rem; font-size: 0.9rem; }
  #res-mana .bar { display: none; }
  #clock { display: none; }
  #objectives { top: 150px; left: 8px; right: auto; width: min(240px, 62vw); font-size: 0.8rem; padding: 0.35rem 0.6rem; }
  #touch-bar { top: 40%; gap: 4px; }
  #touch-bar button { padding: 0.3rem 0.55rem; font-size: 0.85rem; }
  #minimap-wrap { justify-self: start; }
  #hint { top: 206px; bottom: auto; left: 8px; right: 8px; width: auto; transform: none; font-size: 0.82rem; padding: 0.45rem 2rem 0.45rem 0.6rem; }
  #dialogue { bottom: calc(232px + var(--safe-b)); font-size: 0.9rem; padding: 0.4rem 0.6rem; }
  #dialogue p { font-size: 0.88rem; }
  #dialogue .portrait { width: 38px; height: 38px; }
  #minimap { width: 104px; height: 104px; }
  .title-buttons button { min-width: 0; width: 100%; }
}
