:root {
  --bg: #f4f1eb;
  --panel: #ffffff;
  --panel-2: #faf7f2;
  --ink: #1f1b17;
  --muted: #6c645c;
  --line: #ddd5ca;
  --accent: #b4471f;
  --accent-ink: #ffffff;
  --accent-soft: #f6e4da;
  --you: #e9efe9;
  --bad: #b3261e;
  --good: #1f6f43;
  --gold: #a8761a;
  --hp: #3d9a5c;
  --hp-mid: #c99a2e;
  --hp-low: #c0392b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131110;
    --panel: #1c1917;
    --panel-2: #221e1b;
    --ink: #ece6df;
    --muted: #a39a90;
    --line: #342e29;
    --accent: #e3804f;
    --accent-ink: #1a0f09;
    --accent-soft: #3a2519;
    --you: #1f2a22;
    --bad: #ff8a80;
    --good: #5cc48a;
    --gold: #e0b44c;
    --hp: #4fb874;
    --hp-mid: #d8aa3c;
    --hp-low: #e0564a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; display: flex; flex-direction: column; min-height: 100vh; }
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.bad { color: var(--bad); }
.hint { color: var(--muted); font-size: 13px; }

.top { padding: 16px 16px 0; max-width: 1400px; width: 100%; margin: 0 auto; }
.title-row { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; }
.top h1 { margin: 0; font: 700 26px/1.15 Georgia, "Iowan Old Style", "Palatino Linotype", serif; letter-spacing: -0.3px; }
.sub { margin: 4px 0 10px; color: var(--muted); }
.top-actions { display: flex; gap: 8px; align-items: center; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tab { background: none; border: 0; padding: 9px 13px; cursor: pointer; color: var(--muted); white-space: nowrap; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

button.primary, button.ghost, label.ghost { border-radius: 8px; padding: 8px 14px; cursor: pointer; border: 1px solid var(--line); display: inline-block; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.ghost, label.ghost { background: var(--panel); }
button.small, label.small { padding: 5px 10px; font-size: 13px; }
button:disabled { opacity: .45; cursor: default; }
.pill { border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; border: 1px solid var(--line); background: var(--panel); color: var(--muted); }
.pill.ok { color: var(--good); }
.pill.warn { color: var(--bad); }
.btnrow { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 8px; }

.layout { flex: 1; width: 100%; max-width: 1400px; margin: 0 auto; padding: 12px 16px 16px; display: grid; grid-template-columns: minmax(0, 1fr) 420px; gap: 16px; align-items: start; }
.story { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; display: flex; flex-direction: column; height: calc(100vh - 150px); min-height: 480px; overflow: hidden; }
.side { position: sticky; top: 12px; max-height: calc(100vh - 150px); overflow-y: auto; }

.hud { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; padding: 8px 14px; border-bottom: 1px solid var(--line); background: var(--panel-2); font-size: 13px; }
.hud-name { font-weight: 700; font-family: Georgia, serif; font-size: 15px; }
.hpbar { position: relative; display: inline-block; width: 120px; height: 18px; background: var(--line); border-radius: 9px; overflow: hidden; vertical-align: middle; }
.hpbar i { position: absolute; inset: 0 auto 0 0; background: var(--hp); transition: width .3s; }
.hpbar i.mid { background: var(--hp-mid); }
.hpbar i.low { background: var(--hp-low); }
.hpbar b { position: relative; display: block; text-align: center; font-size: 11.5px; line-height: 18px; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,.6); }
.hpbar.small { width: 100%; height: 14px; }
.hpbar.small b { line-height: 14px; font-size: 11px; }

.log { flex: 1; overflow-y: auto; padding: 18px 22px 8px; scroll-behavior: smooth; }
.ptitle { font: 700 21px Georgia, serif; margin: 22px 0 8px; color: var(--accent); }
.log > .ptitle:first-child { margin-top: 0; }
.passage { font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif; font-size: 16.5px; line-height: 1.62; max-width: 760px; }
.passage p { margin: 0 0 11px; }
.you { margin: 12px 0; padding: 6px 12px; background: var(--you); border-radius: 10px; display: inline-block; font-weight: 600; }
.line { font-size: 13.5px; margin: 3px 0; color: var(--muted); }
.line.sys { color: var(--gold); }
.line.level { color: var(--good); background: color-mix(in srgb, var(--good) 10%, transparent); padding: 8px 12px; border-radius: 8px; font-size: 14px; margin: 10px 0; }
.line.roll { color: var(--ink); }
.line.combat { color: var(--ink); }
.line.quip { color: var(--accent); font-style: italic; }
.line .ok { color: var(--good); font-weight: 700; }
.line .no { color: var(--bad); font-weight: 700; }
.line .rnd { display: inline-block; margin-top: 6px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; font-size: 12px; }

.choices { border-top: 1px solid var(--line); padding: 10px 14px 12px; background: var(--panel-2); max-height: 55%; overflow-y: auto; }
.choice { display: block; width: 100%; text-align: left; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin: 0 0 7px; cursor: pointer; }
.choice:hover { border-color: var(--accent); }
.choice .ctext { font-weight: 600; }
.tag { display: inline-block; margin-left: 8px; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.tag.soft { background: var(--panel-2); color: var(--muted); }
.risk { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.panel p { margin: 0 0 8px; }
.panel.dead { border-color: var(--bad); }
.check .big { font: 700 40px Georgia, serif; margin: 4px 0; }
.check .big small { font-size: 16px; font-family: system-ui, sans-serif; margin-left: 6px; }
.check .big.ok { color: var(--good); }
.check .big.no { color: var(--bad); }
.manual input { width: 64px; padding: 6px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); }

.combat .cb-head { display: flex; gap: 10px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.combat .cb-head > span:first-child { font-weight: 700; color: var(--ink); }
.order span.now { color: var(--accent); font-weight: 700; }
.order span.gone { text-decoration: line-through; opacity: .5; }
.objective { font-size: 13.5px; background: var(--accent-soft); border-radius: 8px; padding: 6px 10px; }
.units { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px; margin: 6px 0; }
.unit { display: flex; flex-direction: column; gap: 3px; text-align: left; border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 6px 8px; font-size: 13px; }
button.unit { cursor: pointer; }
.unit.target { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 35%, transparent); }
.unit.down { opacity: .45; }
.unit .uname { font-weight: 700; }
.unit.foe .hpbar i { background: var(--hp-low); }
.actgroup h4 { margin: 8px 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.act { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 10px; margin: 0 6px 6px 0; cursor: pointer; font-size: 14px; }
.act:hover:not(:disabled) { border-color: var(--accent); }
.act small { color: var(--muted); margin-left: 4px; }
.act.obj { background: var(--accent-soft); }

/* Character creator */
.creator { flex: 1; overflow-y: auto; padding: 18px 22px; }
.cr h3 { margin: 20px 0 8px; font: 700 18px Georgia, serif; }
.cr h4 { margin: 14px 0 6px; font-size: 14px; }
.opts { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 8px; }
.opts.three { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.opt { text-align: left; border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px; padding: 10px 12px; cursor: pointer; display: flex; flex-direction: column; gap: 3px; }
.opt span { color: var(--muted); font-size: 13.5px; }
.opt:hover { border-color: var(--accent); }
.opt.sel { border-color: var(--accent); background: var(--accent-soft); }
.opt.small { padding: 8px 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chipbtn { border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 4px 12px; cursor: pointer; }
.chipbtn.sel { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chipbtn small { color: var(--muted); }
#crName { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); min-width: 240px; }
table.scores { border-collapse: collapse; width: 100%; font-size: 14px; }
table.scores th, table.scores td { border-bottom: 1px solid var(--line); padding: 6px; text-align: left; vertical-align: middle; }
table.scores select { padding: 4px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel-2); }
.sheet.review { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: var(--panel-2); }

/* Side panes */
.pane { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; }
.pane-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.pane h2 { margin: 0; font: 700 19px Georgia, serif; }
.pane h3, #pack h3 { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
#pack h3:first-child { margin-top: 0; }
#pack ul { margin: 0; padding-left: 18px; }
#pack ul.items { list-style: none; padding: 0; }
#pack ul.items li { padding: 5px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.journal li { font-size: 14px; margin-bottom: 4px; }
.pane input[type=text], .pane input[type=password], .pane input[type=number] { border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: var(--panel-2); }
#passInput { flex: 1; min-width: 0; }
.check-row { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; }
.slots { display: flex; flex-direction: column; gap: 6px; }
.slot { display: flex; justify-content: space-between; gap: 8px; align-items: center; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--panel-2); font-size: 14px; flex-wrap: wrap; }
.slot .btnrow { margin: 0; }
.filebtn { font-size: 13px; }

.sheet { font-size: 14px; }
.sheet h1 { font: 700 22px Georgia, serif; margin: 0; }
.sheet .sub { margin: 2px 0 8px; }
.sheet h2 { font: 700 15px Georgia, serif; margin: 14px 0 4px; border-bottom: 1px solid var(--line); padding-bottom: 2px; }
.sheet table { border-collapse: collapse; width: 100%; margin: 6px 0; }
.sheet th, .sheet td { border: 1px solid var(--line); padding: 4px 6px; text-align: left; vertical-align: top; }
.sheet table.stats th, .sheet table.stats td { text-align: center; }
.sheet table.skills td { border-width: 0 0 1px; padding: 2px 4px; }
.sheet table.skills tr.prof { font-weight: 600; }
.sheet table.kv th { background: var(--panel-2); width: 25%; }
.sheet ul { margin: 4px 0; padding-left: 18px; }
.notes-space { min-height: 60px; border: 1px dashed var(--line); border-radius: 6px; }

.dice-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.die { border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px; padding: 10px 0; cursor: pointer; font-weight: 700; }
.die:hover { border-color: var(--accent); color: var(--accent); }
.dice-opts { display: flex; gap: 8px; margin-top: 10px; }
.dice-opts label { font-size: 12px; color: var(--muted); width: 78px; }
.dice-opts label.grow { flex: 1; width: auto; }
.dice-opts input { width: 100%; }
.seg { display: inline-flex; margin-top: 8px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 5px 10px; font-size: 13px; cursor: pointer; }
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.dice-result { margin-top: 10px; padding: 14px; border-radius: 10px; background: var(--panel-2); border: 1px solid var(--line); min-height: 56px; }
.dice-result .total { font: 700 30px Georgia, serif; color: var(--accent); margin-right: 8px; }
.roll-history { margin: 0; padding-left: 20px; font-size: 13px; color: var(--muted); max-height: 220px; overflow: auto; }

.flash { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--bg); padding: 8px 16px; border-radius: 999px; font-size: 14px; opacity: 0; pointer-events: none; transition: all .2s; }
.flash.show { opacity: 1; transform: translateX(-50%); }

.mobile-only { display: none; }
@media (max-width: 900px) {
  .mobile-only { display: inline-block; }
  .layout { grid-template-columns: 1fr; padding: 10px 16px 12px; }
  body.tab-story .side { display: none; }
  body:not(.tab-story) .story { display: none; }
  .story { height: calc(100vh - 120px); height: calc(100dvh - 120px); }
  .side { position: static; max-height: none; }
  .log { padding: 14px; }
  .passage { font-size: 16px; }
  .top h1 { font-size: 22px; }
  .sub { display: none; }
  .choices { max-height: 60%; }
}

.print-area { display: none; }
@media print {
  body > *:not(.print-area) { display: none !important; }
  .print-area { display: block !important; color: #000; background: #fff; font: 11pt/1.4 Georgia, serif; }
  .print-area table { border-collapse: collapse; margin: 4px 0 8px; width: 100%; }
  .print-area th, .print-area td { border: 1px solid #999; padding: 2px 6px; text-align: left; }
  .print-area table.skills td { border-width: 0 0 1px; }
  .print-area h1 { font-size: 20pt; margin: 0; }
  .print-area h2 { font-size: 12.5pt; margin: 10px 0 3px; border-bottom: 1px solid #999; break-after: avoid; }
  .print-area .muted { color: #555; }
  .print-area .notes-space { min-height: 120px; border: 1px dashed #999; }
  .print-area table, .print-area li { break-inside: avoid; }
}
