:root, [data-theme="shelter"] {
  --bg: #0f1522; --panel: #172033; --panel2: #1e2940; --line: #2c3a57; --text: #dfe6f2; --muted: #8f9bb3;
  --accent: #d4a62a; --accent-text: #1a1406; --blue: #4d8dd8; --hp: #d9534f; --en: #3fa7d6; --xp: #b48be0;
  --good: #57b36b; --warn: #e0a030; --bad: #e0555a; --story: #d4a62a; --link: #8fc1ff;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
}
[data-theme="daylight"] {
  --bg: #eef0f3; --panel: #ffffff; --panel2: #f3f5f8; --line: #d3d8e0; --text: #1d2433; --muted: #5d6679;
  --accent: #a87b00; --accent-text: #fff; --blue: #2d6cb5; --good: #2e8a45; --warn: #b87400; --bad: #c0343a; --story: #a87b00; --link: #1f5fae;
}
[data-theme="terminal"] {
  --bg: #0b0a06; --panel: #12100a; --panel2: #1a170e; --line: #3a3016; --text: #ffbf47; --muted: #a5823c;
  --accent: #ffbf47; --accent-text: #0b0a06; --blue: #ffbf47; --hp: #ff7a47; --en: #ffd98a; --xp: #ffbf47; --story: #fff0c2; --link: #ffe08a;
  --font: var(--mono);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: var(--fs, 15px)/1.45 var(--font); }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; cursor: pointer;
  transition: border-color .12s, background .12s;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
input, select { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 7px 10px; }
h1, h2, h3 { margin: 0 0 .4em; font-weight: 650; }
h3 { font-size: .8em; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.error { color: var(--bad); min-height: 1.4em; margin: .5em 0 0; }
.muted { color: var(--muted); }

/* ---------- auth & creation */
.screen:not(#game) { min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(ellipse at 50% 0%, #25385e 0%, var(--bg) 60%); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; width: 100%; box-shadow: 0 10px 40px #0006; }
.auth { max-width: 400px; }
.auth h1, .create h1 { color: var(--accent); letter-spacing: .02em; }
.tagline { color: var(--muted); margin-top: 0; }
.card label { display: grid; gap: 4px; margin: 12px 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
.create { max-width: 900px; }
.create h2 { font-size: 1em; margin-top: 18px; color: var(--muted); }
.choices { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
.choice { text-align: left; padding: 10px; border-radius: 8px; display: flex; flex-direction: column; justify-content: flex-start; }
.choice b { display: block; }
.choice small { color: var(--muted); }
.choice[aria-pressed="true"] { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--panel2)); }
.preview { margin: 16px 0; padding: 12px; background: var(--panel2); border-radius: 8px; min-height: 90px; }
.preview .stats { display: flex; gap: 14px; flex-wrap: wrap; font-family: var(--mono); margin: 6px 0; }

/* ---------- game layout */
#game { height: 100%; display: flex; flex-direction: column; }
#topbar { display: flex; align-items: center; gap: 14px; padding: 8px 14px; border-bottom: 1px solid var(--line); background: var(--panel); }
.brand { font-weight: 700; color: var(--accent); white-space: nowrap; }
#objective { flex: 1; min-width: 0; font-size: .92em; }
#objective .obj { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#objective .star { color: var(--story); }
#objective .prog { color: var(--muted); }
#topbar nav { display: flex; gap: 6px; }
#layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 290px minmax(0, 1fr) 340px; gap: 10px; padding: 10px; }
.swap #layout { grid-template-columns: 340px minmax(0, 1fr) 290px; }
.swap #left { order: 3; } .swap #right { order: 1; }
#left, #right, #center { min-height: 0; overflow: auto; }
#left { padding: 12px; display: flex; flex-direction: column; gap: 10px; }
#center { display: flex; flex-direction: column; gap: 10px; }
#right { display: flex; flex-direction: column; gap: 10px; }

/* vitals */
.who { display: flex; justify-content: space-between; align-items: baseline; }
.who b { font-size: 1.1em; }
.bar { position: relative; height: 18px; background: var(--bg); border: 1px solid var(--line); border-radius: 5px; overflow: hidden; margin: 5px 0; }
.bar > i { position: absolute; inset: 0 auto 0 0; background: var(--c, var(--blue)); transition: width .25s; }
.bar > span { position: relative; display: block; font-size: .78em; line-height: 16px; padding: 0 6px; text-shadow: 0 1px 2px #000a; color: #fff; }
[data-theme="daylight"] .bar > span { color: var(--text); text-shadow: none; }
.bar.hp { --c: var(--hp); } .bar.en { --c: var(--en); } .bar.xp { --c: var(--xp); height: 10px; }
.bar.xp > span { display: none; }
.bar.small { height: 12px; } .bar.small > span { line-height: 10px; font-size: .7em; }
.effects { display: flex; gap: 4px; flex-wrap: wrap; }
.tag { font-size: .75em; padding: 1px 6px; border-radius: 10px; background: var(--panel2); border: 1px solid var(--line); }
.tag.good { border-color: var(--good); } .tag.bad { border-color: var(--bad); }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; border: 0; border-radius: 6px 6px 0 0; padding: 6px 2px; background: none; font-size: .88em; }
.tabs button[aria-selected="true"] { background: var(--panel2); color: var(--accent); }
#tab-body { font-size: .92em; }
.list { display: grid; gap: 6px; }
.li { background: var(--panel2); border-radius: 7px; padding: 7px 9px; }
.li .t { display: flex; justify-content: space-between; gap: 6px; }
.li .s { color: var(--muted); font-size: .88em; }
.li .btns { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.li .btns button, .mini { padding: 1px 8px; font-size: .82em; }
.li.locked { opacity: .55; }
.li.main { border-left: 3px solid var(--story); }
.li.done { opacity: .6; }
.section { margin: 12px 0 4px; }

/* room */
#room { display: grid; grid-template-columns: minmax(180px, 38%) 1fr; overflow: hidden; }
.noimage #room, #room.no-art { grid-template-columns: 1fr; } .noimage #scene, #room.no-art #scene { display: none; }
#scene { background: #000; min-height: 180px; }
#scene svg, #scene img { width: 100%; height: 100%; display: block; object-fit: cover; }
.room-text { padding: 12px 16px; }
#room-name { font-size: 1.25em; margin: 0; }
#room-area { color: var(--muted); font-size: .85em; margin-bottom: 6px; }
#room-desc { margin: 0 0 8px; }
#room-who { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: .85em; padding: 2px 9px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel2); cursor: pointer; }
.chip.npc { border-color: var(--blue); } .chip.player { border-color: var(--good); } .chip.item { border-style: dashed; }
.chip.mob.trivial { border-color: #777; } .chip.mob.easy { border-color: var(--good); } .chip.mob.even { border-color: #d8d04a; }
.chip.mob.hard { border-color: var(--warn); } .chip.mob.deadly { border-color: var(--bad); color: var(--bad); }
.chip .lvl { color: var(--muted); font-size: .85em; }

/* response & scrollback */
#response, #scrollback { padding: 12px 16px; min-height: 90px; overflow: auto; }
#response { flex: 1 1 auto; max-height: 40vh; }
#response:empty { display: none; }
#scrollback { flex: 1 1 auto; }
.scroll-mode #response, .scroll-mode #room { display: none; }
.line { white-space: pre-wrap; margin: 2px 0; }
.line.note { color: var(--accent); }
.line.combat { color: var(--muted); font-size: .92em; }
.line.cmd { color: var(--muted); font-family: var(--mono); font-size: .85em; }
.line.chat { color: var(--good); }
.line.log { color: var(--text); }
.line b { color: var(--text); }
.link { background: none; border: 0; border-bottom: 1px dashed var(--link); color: var(--link); padding: 0 2px; border-radius: 0; }
.opt { display: block; text-align: left; margin: 4px 0; width: 100%; background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; color: var(--text); }
.opt:hover { border-color: var(--story); }

/* actions */
#actions { display: flex; flex-wrap: wrap; gap: 6px; }
#actions button { position: relative; overflow: hidden; }
#actions .story { background: color-mix(in srgb, var(--story) 22%, var(--panel2)); border-color: var(--story); font-weight: 600; }
#actions .story::before { content: "★ "; color: var(--story); }
#actions .fight { border-color: var(--bad); }
#actions .ability, #actions .item, #actions .flee, #actions .stance { text-align: left; }
#actions .meta { display: block; font-size: .72em; color: var(--muted); }
#actions .meta.warn { color: var(--warn); } #actions .meta.ok { color: var(--good); }
#actions .cd { position: absolute; inset: auto 0 0 0; height: 3px; background: var(--accent); }
.compact #actions button { padding: 3px 8px; font-size: .85em; }
.compact #actions .meta { display: inline; margin-left: 6px; }
#cmd-form { display: flex; gap: 6px; }
#cmd { flex: 1; font-family: var(--mono); }

/* combat */
#combat { padding: 10px 14px; border-color: var(--bad); }
#combat .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
#combat .foe { background: var(--panel2); border: 1px solid var(--line); border-radius: 7px; padding: 6px 8px; cursor: pointer; margin-bottom: 6px; }
#combat .foe.mine { border-color: var(--bad); }
#combat .foe .t { display: flex; justify-content: space-between; font-size: .9em; }
#combat .label { font-size: .8em; color: var(--muted); display: flex; justify-content: space-between; }
.bar.round { --c: var(--accent); height: 14px; }
.bar.act { --c: var(--blue); height: 14px; }
.bar.act.spell { --c: #a66ee0; }
.bar.act.foe { --c: var(--bad); }
.round-note { font-size: .8em; }
.round-note.warn { color: var(--warn); }

/* map */
#map-panel { padding: 10px; }
#map-title { font-size: .85em; color: var(--muted); margin-bottom: 6px; }
#map svg { width: 100%; height: auto; display: block; }
#map .cell { cursor: default; }
#map .cell.adj { cursor: pointer; }
#map .cell.adj:hover rect { stroke: var(--accent); stroke-width: 2; }
#compass { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; margin-top: 8px; }
#compass button { padding: 4px 0; font-size: .85em; }
#compass button.obj { border-color: var(--story); color: var(--story); font-weight: 700; }
#compass .blank { visibility: hidden; }
#activity { padding: 10px; flex: 1; min-height: 140px; display: flex; flex-direction: column; }
#feed { overflow: auto; flex: 1; font-size: .88em; }
#feed .line { border-bottom: 1px solid color-mix(in srgb, var(--line) 50%, transparent); padding: 2px 0; }
#feed time { color: var(--muted); font-size: .8em; margin-right: 6px; }
.noactivity #activity { display: none; }

#toasts { position: fixed; bottom: 16px; right: 16px; display: grid; gap: 6px; z-index: 10; max-width: min(380px, calc(100vw - 32px)); }
.toast { background: var(--panel); border: 1px solid var(--accent); border-radius: 8px; padding: 8px 12px; box-shadow: 0 6px 20px #0008; animation: pop .2s ease-out; }
@keyframes pop { from { transform: translateY(-6px); opacity: 0; } }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 20px; width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: #0008; }
dialog label { display: flex; gap: 8px; align-items: center; justify-content: space-between; margin: 10px 0; }

/* ---------- narrow screens: stack panels */
@media (max-width: 1100px) {
  #layout { grid-template-columns: 260px minmax(0, 1fr); }
  #right { grid-column: 1 / -1; flex-direction: row; }
  #right > * { flex: 1; }
}
@media (max-width: 720px) {
  body { font-size: 14px; }
  #game { height: auto; min-height: 100%; }
  #layout { display: flex; flex-direction: column; padding: 8px; }
  #left, #right, #center { overflow: visible; }
  #center { order: 1; } #right { order: 2; flex-direction: column; } #left { order: 3; }
  #room { grid-template-columns: 1fr; }
  #scene { max-height: 160px; }
  #topbar { flex-wrap: wrap; }
  #objective { order: 3; flex-basis: 100%; }
  #response { max-height: none; }
  #combat .grid { grid-template-columns: 1fr; }
}

/* ---------- immortal builder */
#builder { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: flex; flex-direction: column; }
#builder > header { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
#builder > header b { color: var(--accent); }
#b-status { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .88em; }
.b-body { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 440px; gap: 10px; padding: 10px; }
#b-map { overflow: auto; padding: 10px; }
#b-map svg { width: 100%; height: auto; min-width: 480px; display: block; }
.b-note { font-size: .82em; margin-bottom: 8px; }
.b-cell, .b-ghost, .b-hit { cursor: pointer; }
.b-cell:hover rect, .b-ghost:hover rect { stroke: var(--accent); stroke-width: 2; }
.b-hit:hover { stroke: color-mix(in srgb, var(--accent) 35%, transparent); }
.b-body > aside { overflow: auto; padding: 10px 12px; }
.b-form { display: grid; gap: 8px; padding-top: 8px; }
.b-field { display: grid; gap: 3px; font-size: .88em; }
.b-field > span { color: var(--muted); }
.b-form textarea { font: inherit; color: inherit; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 7px 10px; resize: vertical; }
.b-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.b-row .b-field { flex: 1; min-width: 90px; }
.b-grow { flex: 1; min-width: 0; }
.b-dir { width: 6.5em; color: var(--muted); font-size: .88em; }
.b-checks { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; font-size: .9em; }
.b-check { display: flex; gap: 4px; align-items: center; }
.b-scene { display: grid; grid-template-columns: 1fr 120px; gap: 8px; align-items: center; }
.b-preview { height: 78px; border-radius: 6px; overflow: hidden; background: #000; }
.b-preview .thumb { width: 100%; height: 100%; border-radius: 0; border: 0; }
.b-scene { grid-template-columns: 1fr 150px; }
.b-scene .b-preview { height: 96px; }
.b-head { border-top: 1px solid var(--line); padding-top: 8px; }
.b-list { max-height: 220px; overflow: auto; display: grid; gap: 2px; }
.b-item { text-align: left; padding: 4px 8px; font-size: .88em; }
.b-item.on { border-color: var(--accent); }
.b-new { background: var(--panel2); border-radius: 8px; padding: 6px 10px; }
.b-new summary { cursor: pointer; color: var(--accent); }
.b-new[open] { display: grid; gap: 6px; }
.b-mini { display: grid; font-size: .75em; color: var(--muted); }
button.danger { border-color: var(--bad); color: var(--bad); }
/* Story tab: the side panel takes the whole window (NPC dialogue, quests and recipes edited as JSON) */
.b-wide .b-body { grid-template-columns: 1fr; }
.b-wide #b-map { display: none; }
.b-story { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 14px; padding-top: 8px; align-items: start; }
.b-story .b-list { max-height: 55vh; }
.b-story > div + div { min-width: 0; }
.b-story > .b-form:first-child { position: sticky; top: 0; }
.b-sticky { position: sticky; top: -10px; z-index: 2; background: var(--panel); padding: 8px 0; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
.b-errors { color: var(--bad); font-size: .88em; margin: 0; padding-left: 1.2em; }
.b-warn { color: var(--warn); font-size: .88em; }
.b-sec { display: grid; gap: 8px; }
.b-card { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; display: grid; gap: 6px; background: var(--panel2); scroll-margin-top: 90px; }
.b-say { display: grid; gap: 4px; }
.b-say textarea, .b-card input:not([type]), .b-card select { min-width: 0; }
.b-reply { border-left: 3px solid var(--accent); padding: 4px 0 4px 10px; display: grid; gap: 4px; }
.b-sub { font-size: .82em; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-top: 4px; }
.b-lbl { color: var(--muted); font-size: .88em; }
.b-keyed { display: grid; gap: 4px; font-size: .92em; }
.b-keyed.on { border: 1px dashed var(--line); border-radius: 6px; padding: 4px 8px; }
.b-kv { display: grid; grid-template-columns: minmax(8em, 13em) minmax(0, 1fr) auto; gap: 6px; align-items: start; }
.b-kv > div { display: grid; gap: 4px; }
.b-adder { justify-self: start; color: var(--muted); font-size: .9em; }
.b-alt { display: grid; gap: 4px; }
.b-x { padding: 1px 7px; font-size: .85em; }
.b-add { justify-self: start; font-size: .85em; color: var(--accent); }
@media (max-width: 900px) { .b-story { grid-template-columns: 1fr; } }
@media (max-width: 900px) { .b-body { grid-template-columns: 1fr; } #b-map svg { min-width: 0; } }

/* ---------- pictures: portraits, avatars, icons */
.me-head { display: flex; gap: 10px; align-items: flex-start; }
.me-pic { display: grid; justify-items: center; gap: 2px; }
.me-info { flex: 1; min-width: 0; }
button.portrait { padding: 0; border-radius: 10px; overflow: hidden; width: 68px; height: 68px; border: 2px solid var(--line); }
button.portrait:hover { border-color: var(--accent); }
button.portrait .pic { width: 100%; height: 100%; object-fit: cover; display: grid; font-size: 1.8em; border: 0; border-radius: 0; }
.with-icon { display: flex; gap: 8px; align-items: center; }
.icon { width: 28px; height: 28px; border-radius: 6px; flex: none; object-fit: cover; font-size: .85em; }
.chip { display: inline-flex; align-items: center; gap: 5px; }
.av { width: 20px; height: 20px; border-radius: 50%; object-fit: cover; margin-left: -5px; font-size: .7em; }
.faced { display: flex; gap: 12px; align-items: flex-start; margin: 4px 0 8px; }
.face { width: 96px; height: 96px; border-radius: 10px; object-fit: cover; flex: none; border: 1px solid var(--line); font-size: 2.4em; }
#combat .foe { display: flex; gap: 8px; align-items: center; }
.foe-pic { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; flex: none; }
.foe-body { flex: 1; min-width: 0; }
@media (max-width: 720px) { .face { width: 64px; height: 64px; font-size: 1.6em; } }
.b-pic { display: flex; gap: 10px; align-items: center; }
.thumb { width: 64px; height: 64px; border-radius: 8px; object-fit: cover; border: 1px solid var(--line); flex: none; font-size: 1.6em; }
.b-npc { background: var(--panel2); border-radius: 8px; padding: 6px 8px; margin-bottom: 6px; display: grid; gap: 4px; }

/* Placeholder while default art is still being made: the name's initial in a dashed box. */
.ph { display: inline-grid; place-items: center; background: var(--panel2); color: var(--muted); border: 1px dashed var(--line); font-weight: 700; line-height: 1; }
