:root {
  --bg: #14161b; --panel: #1c2027; --panel2: #232831; --line: #323945; --text: #e6e8ec; --muted: #8d96a5;
  --accent: #d4a24c; --good: #34c759; --bad: #ef4444; --blue: #3b82f6;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, "Segoe UI", sans-serif; display: flex; flex-direction: column; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 5px 10px; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); color: #1a1405; border-color: var(--accent); font-weight: 600; }
button.danger { color: #fca5a5; }
button.small { padding: 2px 7px; font-size: 12px; }
input, select, textarea { background: #11141a; border: 1px solid var(--line); border-radius: 6px; padding: 4px 7px; }
input[type=number] { width: 64px; }
input:focus, select:focus, textarea:focus { outline: 1px solid var(--accent); }
small, .muted { color: var(--muted); }
h3 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

/* toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.toolbar .group { display: flex; gap: 6px; align-items: center; padding-right: 14px; border-right: 1px solid var(--line); }
.toolbar .group:last-child { border-right: 0; }
.toolbar label { display: flex; gap: 4px; align-items: center; color: var(--muted); }
.brand { font-weight: 700; color: var(--accent); letter-spacing: .04em; }
.round { font-weight: 700; min-width: 70px; }
details.dd { position: relative; }
details.dd > summary { list-style: none; cursor: pointer; background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 5px 10px; }
details.dd[open] > .dd-body { position: absolute; z-index: 30; top: 34px; left: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: grid; gap: 8px; min-width: 260px; box-shadow: 0 8px 30px #000a; }

/* layout */
#app { flex: 1; display: flex; min-height: 0; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); width: 270px; flex: none; display: flex; flex-direction: column; transition: width .15s; overflow: hidden; }
.sidebar.collapsed { width: 38px; }
.sidebar.collapsed .sb-body, .sidebar.collapsed .sb-title { display: none; }
.sb-head { display: flex; align-items: center; justify-content: space-between; padding: 8px; border-bottom: 1px solid var(--line); gap: 6px; }
.sb-head .sb-title { font-weight: 700; }
.sb-body { overflow: auto; padding: 8px; flex: 1; }
#stage { flex: 1; position: relative; min-width: 0; background: #0d0f13; }
.grid-svg { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; user-select: none; }
.grid-svg.panning { cursor: grabbing; }
.board { fill: #2a2f27; }
.scale-bar { position: absolute; left: 10px; bottom: 8px; background: #000a; padding: 3px 8px; border-radius: 6px; color: var(--muted); font-size: 12px; pointer-events: none; }
.zoom-ctl { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 4px; }
.zoom-ctl button { width: 34px; height: 34px; font-size: 18px; padding: 0; }
.right { width: 380px; flex: none; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 12px; }

/* tokens */
.token { cursor: pointer; }
.token.draggable { cursor: move; }
.token.down circle:not(.active-ring) { opacity: .45; }
.token.hidden-unit { opacity: .55; }
.token-text { fill: #fff; font: 700 17px system-ui; pointer-events: none; paint-order: stroke; stroke: rgba(0,0,0,.6); stroke-width: 3px; }
.token-name { fill: #e6e8ec; font: 11px system-ui; pointer-events: none; paint-order: stroke; stroke: #000; stroke-width: 3px; }
.token-down { fill: #fca5a5; font: 700 10px system-ui; pointer-events: none; paint-order: stroke; stroke: #000; stroke-width: 3px; }
.active-ring { fill: none; stroke: #fbbf24; stroke-width: 5; filter: drop-shadow(0 0 6px #fbbf24); }
.drag-label { fill: #fde68a; paint-order: stroke; stroke: #000; stroke-width: 4px; }
.drag-label.over { fill: #fca5a5; }

/* initiative */
.init-row { display: grid; grid-template-columns: 38px 1fr auto; gap: 6px; align-items: center; padding: 5px 6px; border-radius: 6px; border: 1px solid transparent; cursor: pointer; }
.init-row:hover { background: var(--panel2); }
.init-row.active { border-color: var(--accent); background: #2b2415; }
.init-row.selected { outline: 1px solid #fff5; }
.init-row .total { font-weight: 700; font-size: 16px; text-align: center; }
.init-row .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.init-row.pc .nm { color: #93c5fd; }
.badge { font-size: 11px; padding: 1px 6px; border-radius: 10px; background: #374151; margin-left: 4px; }
.badge.down { background: #7f1d1d; color: #fecaca; }
.badge.dead { background: #111; color: #888; }
.mini-btns { display: flex; gap: 2px; }
.mini-btns button { padding: 0 5px; font-size: 11px; }
.section-title { margin: 12px 0 4px; }

/* panel */
.card { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; }
.unit-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.unit-head h2 { margin: 0; font-size: 18px; }
.hp-line { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.hp-big { font-size: 22px; font-weight: 700; }
.hp-bar { height: 8px; background: #000; border-radius: 4px; overflow: hidden; margin: 4px 0; }
.hp-bar > div { height: 100%; background: var(--good); }
.statblock .quick, .statblock .abilities { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; text-align: center; margin: 6px 0; }
.statblock .quick > div, .statblock .abilities > div { background: #11141a; border-radius: 6px; padding: 4px 2px; display: flex; flex-direction: column; }
.statblock b { font-size: 16px; }
.statblock i { font-style: normal; color: var(--muted); font-size: 12px; }
.kv { display: flex; gap: 8px; font-size: 12px; margin: 2px 0; }
.kv span { color: var(--muted); min-width: 90px; }
.weapon { display: flex; justify-content: space-between; align-items: center; width: 100%; text-align: left; margin: 3px 0; gap: 8px; }
.weapon small { display: block; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg label { padding: 3px 9px; cursor: pointer; color: var(--muted); }
.seg input { display: none; }
.seg label:has(input:checked) { background: var(--accent); color: #1a1405; font-weight: 600; }
.trait { font-size: 12px; margin: 4px 0; }
.trait b { color: var(--accent); }
details > summary { cursor: pointer; color: var(--muted); }

/* roll log */
.roll { border-left: 3px solid var(--line); padding: 5px 8px; margin: 6px 0; background: #11141a; border-radius: 0 6px 6px 0; }
.roll.attack { border-color: #f59e0b; } .roll.damage { border-color: #ef4444; } .roll.initiative { border-color: #3b82f6; }
.roll .who { font-weight: 600; }
.die { min-width: 28px; padding: 1px 6px; font-weight: 700; background: #0b0d11; border-color: #4b5563; }
.die.dropped { opacity: .4; text-decoration: line-through; }
.die-input { width: 52px !important; }
.res-hit, .res-crit { color: var(--good); font-weight: 700; } .res-miss, .res-fumble { color: var(--bad); font-weight: 700; }
.roll .actions { margin-top: 4px; display: flex; gap: 6px; flex-wrap: wrap; }
.applied-note { color: var(--muted); font-size: 12px; margin-top: 3px; }

/* modal */
.modal-back { position: fixed; inset: 0; background: #000b; z-index: 50; display: flex; align-items: center; justify-content: center; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; width: min(760px, 94vw); max-height: 90vh; overflow: auto; padding: 18px; }
.modal h2 { margin: 0 0 12px; }
.modal .row { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; margin-bottom: 10px; }
.modal .row > label { display: flex; flex-direction: column; gap: 2px; color: var(--muted); font-size: 12px; }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.wrow { display: grid; grid-template-columns: 1.5fr 70px 1.4fr 1fr auto; gap: 6px; margin-bottom: 5px; }
.wrow input { width: 100% !important; }
.result-list { max-height: 340px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.result { display: flex; justify-content: space-between; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--line); gap: 10px; }
.result:last-child { border: 0; }
.toggle { display: inline-flex; align-items: center; gap: 10px; }
.switch { position: relative; width: 52px; height: 26px; }
.switch input { display: none; }
.switch span { position: absolute; inset: 0; background: #3b3f4a; border-radius: 26px; cursor: pointer; transition: .2s; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: .2s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(26px); }
.toggle .on { color: var(--text); font-weight: 600; } .toggle .off { color: var(--muted); }
.link-row { display: grid; grid-template-columns: 120px 1fr auto; gap: 8px; align-items: center; margin: 6px 0; }
.link-row input { width: 100%; }

#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 99; align-items: center; }
.toast { background: #111827; border: 1px solid var(--line); padding: 8px 14px; border-radius: 8px; box-shadow: 0 4px 20px #000a; }
.toast.error { border-color: var(--bad); color: #fecaca; }
.empty { color: var(--muted); text-align: center; padding: 20px 8px; }

/* v2: token images, action groups, encounter chat */
.token-thumb { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; border: 2px solid var(--line); }
.wgroup { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--accent); margin: 8px 0 2px; }
.wrow { grid-template-columns: 1.5fr 70px 1.4fr 120px auto; }
.import-notes { background: #2b2415; border: 1px solid var(--accent); border-radius: 8px; padding: 8px 10px; margin-bottom: 10px; font-size: 13px; }
.chat-log { height: 48vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin: 8px 0; display: flex; flex-direction: column; gap: 10px; background: #11141a; }
.msg { display: flex; flex-direction: column; gap: 6px; max-width: 92%; }
.msg.user { align-self: flex-end; }
.msg .bubble { white-space: pre-wrap; padding: 8px 11px; border-radius: 10px; background: var(--panel2); }
.msg.user .bubble { background: #2b2415; border: 1px solid #5c4a24; }
.proposal { border: 1px solid var(--accent); border-radius: 10px; padding: 10px; background: var(--panel); }
.proposal ul { margin: 6px 0; padding-left: 18px; }
.modal:has(.chat-log) { width: min(900px, 96vw); }

/* v3: scene tools, conditions, death saves */
.token-badge { fill: #fff; font: 700 10px system-ui; pointer-events: none; }
.tools { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.tools button { border: 0; border-radius: 0; border-right: 1px solid var(--line); padding: 5px 9px; }
.tools button:last-child { border-right: 0; }
.tools button.on { background: var(--accent); color: #1a1405; font-weight: 600; }
.grid-svg.tool-draw, .grid-svg.tool-wall, .grid-svg.tool-reveal, .grid-svg.tool-hide { cursor: crosshair; }
.grid-svg.tool-erase { cursor: not-allowed; }
.cond-grid { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0; }
.cond { font-size: 11px; padding: 2px 7px; border-radius: 12px; background: #11141a; color: var(--muted); }
.cond.on { background: #7c3aed; color: #fff; border-color: #a78bfa; }
.cond-tags { font-size: 11px; color: #c4b5fd; }
.pips { display: inline-flex; gap: 3px; vertical-align: middle; }
.pip { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--muted); cursor: pointer; }
.pip.s.on { background: var(--good); border-color: var(--good); }
.pip.f.on { background: var(--bad); border-color: var(--bad); }
.dispo-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
.assess { margin: 0 8px 8px; padding: 6px 8px; border-radius: 8px; background: var(--panel2); font-size: 12px; border-left: 4px solid var(--muted); }
.assess.easy { border-color: #22c55e; } .assess.medium { border-color: #eab308; } .assess.hard { border-color: #f97316; } .assess.deadly { border-color: #ef4444; }
.assess b { text-transform: capitalize; }
.roll.save { border-color: #0ea5e9; } .roll.deathsave { border-color: #a78bfa; }
.res-pass { color: var(--good); font-weight: 700; } .res-fail { color: var(--bad); font-weight: 700; }
.quick-prompts { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }

/* v4: turn economy, spellcasting, player actions, scenes */
.eco-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: 4px 0 8px; }
.eco { font-size: 12px; padding: 2px 9px; border-radius: 12px; background: #14361f; border-color: #22c55e; color: #bbf7d0; }
.eco.used { background: #2a2d34; border-color: var(--line); color: var(--muted); text-decoration: line-through; }
.eco:disabled { opacity: 1; cursor: default; }
.slot-row { display: flex; gap: 8px; align-items: center; margin: 3px 0; }
.slot-row small:first-child { width: 28px; color: var(--muted); }
.pip.slot { border-color: #60a5fa; } .pip.slot.on { background: #60a5fa; }
.spell-row { display: flex; justify-content: space-between; align-items: center; gap: 6px; padding: 2px 0; font-size: 13px; }
.upcast { padding: 1px 3px; font-size: 12px; }
.by-player { font-size: 10px; background: #1e3a8a; color: #bfdbfe; border-radius: 8px; padding: 0 6px; margin-left: 4px; }
.roll.cast { border-color: #60a5fa; } .roll.note { border-color: #e5e7eb; font-style: italic; }
.your-turn { background: var(--accent); color: #1a1405; font-weight: 700; padding: 4px 10px; border-radius: 6px; }
.scene-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--line); }
.gen-card { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; margin: 6px 0; cursor: pointer; }
.gen-card input { margin-top: 4px; }
