:root {
  --bg: #15121a;
  --bg2: #1e1a24;
  --bg3: #2a2431;
  --line: #3a3242;
  --text: #ece4d4;
  --muted: #a99f90;
  --gold: #d4af37;
  --accent: #c0563a;
  --good: #3cb44b;
  --warn: #e0a030;
  --bad: #d63b3b;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font-size: 14px; }
h1, h2, h3 { font-family: Georgia, "Times New Roman", serif; font-weight: normal; margin: 0 0 .5em; }
h1 { color: var(--gold); letter-spacing: .02em; }
a { color: var(--gold); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }

button, .btn {
  font: inherit; color: var(--text); background: var(--bg3); border: 1px solid var(--line);
  border-radius: 8px; padding: 7px 12px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
}
button:hover:not(:disabled), .btn:hover { border-color: var(--gold); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: #e07050; color: #fff; }
button.gold { background: var(--gold); border-color: #f0d070; color: #1a1408; }
button.danger { background: #5a1f1f; border-color: #8a2f2f; }
button.small { padding: 4px 8px; font-size: 12px; }
button.active { border-color: var(--gold); background: #3a3020; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px;
}
input:focus, select:focus { outline: 2px solid var(--gold); outline-offset: -1px; }
input[type=number] { width: 80px; }
label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
label.inline { flex-direction: row; align-items: center; gap: 6px; }
.card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }

/* ---------- toasts & menus ---------- */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100000; pointer-events: none; }
.toast { background: var(--bg3); border: 1px solid var(--line); border-radius: 8px; padding: 10px 16px; box-shadow: 0 6px 24px #0008; transition: opacity .4s; }
.toast.error { border-color: var(--bad); background: #3a1a1a; }
.toast.out { opacity: 0; }
.popup-menu {
  position: fixed; z-index: 50000; min-width: 200px; max-width: 320px; max-height: 70vh; overflow: auto;
  background: var(--bg2); border: 1px solid var(--gold); border-radius: var(--radius); padding: 6px; box-shadow: 0 10px 40px #000a;
}
.popup-title { font-family: Georgia, serif; color: var(--gold); padding: 6px 8px 8px; font-size: 15px; }
.popup-heading { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; padding: 8px 8px 4px; }
.popup-item { display: flex; flex-direction: column; align-items: flex-start; width: 100%; border: 0; background: none; padding: 7px 8px; border-radius: 6px; text-align: left; }
.popup-item:hover:not(:disabled) { background: var(--bg3); }
.popup-sub { color: var(--muted); font-size: 11px; }
.popup-menu form { display: flex; gap: 6px; padding: 6px 8px; flex-wrap: wrap; }
.popup-menu form input { flex: 1; min-width: 0; }

/* ---------- layout shells ---------- */
.topbar { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--bg2); border-bottom: 1px solid var(--line); min-height: 52px; flex-wrap: wrap; }
.topbar .title { font-family: Georgia, serif; font-size: 18px; color: var(--gold); }
.app { display: grid; height: 100vh; grid-template-rows: auto 1fr; }
.main { display: grid; min-height: 0; }
.main.play { grid-template-columns: 1fr 320px; }
.main.dm { grid-template-columns: 290px 1fr 330px; }
.main.editor { grid-template-columns: 300px 1fr; }
.sidebar { background: var(--bg2); border-right: 1px solid var(--line); overflow: auto; min-height: 0; padding: 12px; }
.right { display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--line); background: var(--bg2); }
@media (max-width: 900px) {
  .main.play, .main.dm, .main.editor { grid-template-columns: 1fr; grid-template-rows: 1fr 300px; }
  .main.dm .sidebar { display: none; }
}

/* ---------- combat bar ---------- */
.combat-bar { padding: 6px 14px; border-radius: 999px; font-weight: 600; letter-spacing: .04em; background: var(--bg3); border: 1px solid var(--line); }
.combat-bar.idle { color: var(--muted); font-weight: normal; }
.combat-bar.player { background: #1f3a24; border-color: var(--good); }
.combat-bar.dm { background: #3a1f1f; border-color: var(--bad); }
.combat-bar.urgent { animation: pulse 0.8s infinite alternate; }
@keyframes pulse { to { box-shadow: 0 0 16px var(--warn); } }

/* ---------- board ---------- */
.board { position: relative; min-height: 0; min-width: 0; overflow: hidden; background: #0d0b10; }
.board-viewport { position: absolute; inset: 0; overflow: auto; }
.board-sizer { position: relative; margin: 8px; }
.board-stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; user-select: none; -webkit-user-select: none; }
.board-bg { position: absolute; left: 0; top: 0; pointer-events: none; }
.board-grid { position: absolute; pointer-events: none; overflow: visible; }
.spawn-mark { fill: #42d4f422; stroke: #42d4f4; stroke-width: 2; stroke-dasharray: 6 4; }
.spawn-text { fill: #42d4f4; font-weight: bold; }
.layer { position: absolute; inset: 0; pointer-events: none; }
.layer.tokens > * { pointer-events: auto; }
.board-zoom { position: absolute; right: 10px; bottom: 10px; display: flex; gap: 4px; z-index: 5; }
.board-zoom button { padding: 4px 10px; background: #000a; }
.board-empty { position: absolute; inset: 0; display: grid; place-items: center; font-family: Georgia, serif; font-size: 26px; color: var(--muted); text-align: center; padding: 20px; }
.object { position: absolute; }
.object img { width: 100%; height: 100%; object-fit: contain; }
.object-label, .token-name {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: -16px; white-space: nowrap; font-size: 11px;
  background: #000b; padding: 0 5px; border-radius: 4px; pointer-events: none; color: #fff;
}
.hl { position: absolute; border-radius: 4px; }
.hl.move { background: #42d4f433; box-shadow: inset 0 0 0 1px #42d4f488; }
.hl.range { background: #e6194b22; box-shadow: inset 0 0 0 1px #e6194b55; }
.hl.place { background: #d4af3733; box-shadow: inset 0 0 0 2px var(--gold); }
.token { position: absolute; cursor: pointer; --ring: transparent; }
.token-body { position: absolute; inset: 3px; border-radius: 50%; box-shadow: 0 0 0 3px var(--ring), 0 3px 8px #000c; background: #222; overflow: hidden; }
.token-body.big { border-radius: 14px; background: transparent; box-shadow: 0 0 0 3px var(--ring); }
.token-body img { width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.token-body.big img { object-fit: contain; }
.kind-object .token-body { border-radius: 6px; background: transparent; box-shadow: none; }
.kind-object .token-body img { object-fit: contain; }
.kind-object.obstacle .token-body { box-shadow: inset 0 0 0 2px #e6194b99; background: repeating-linear-gradient(45deg, #e6194b22 0 6px, transparent 6px 12px); }
.kind-spawn .token-body { background: transparent; box-shadow: none; }
.token-fallback { display: grid; place-items: center; height: 100%; font-weight: bold; font-size: 20px; }
.token.selected .token-body { box-shadow: 0 0 0 3px var(--gold), 0 0 18px var(--gold); }
.token.mine .token-body { box-shadow: 0 0 0 3px var(--ring), 0 0 14px var(--ring); }
.token.targetable { cursor: crosshair; }
.token.targetable .token-body { animation: target 0.9s infinite alternate; }
@keyframes target { from { box-shadow: 0 0 0 3px #e6194b; } to { box-shadow: 0 0 0 5px #ff6b8a, 0 0 18px #e6194b; } }
.token.dimmed { opacity: .45; }
.token.dragging { opacity: .8; z-index: 2000000 !important; cursor: grabbing; }
.token.status-down .token-body img { filter: grayscale(1) brightness(.7); }
.token.status-dead .token-body img { filter: grayscale(1) brightness(.35); }
.token-badge { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 800; letter-spacing: .05em; color: #fff; text-shadow: 0 0 4px #000; }
.token-badge.down { background: #d63b3b55; font-size: 12px; }
.token-badge.dead { font-size: 28px; }
.stack-count { position: absolute; right: -6px; top: -6px; background: var(--gold); color: #000; border-radius: 999px; font-size: 10px; font-weight: bold; padding: 1px 5px; z-index: 3; }
.hpbar { position: absolute; left: 50%; transform: translateX(-50%); width: max(60px, 100%); height: 12px; background: #000c; border: 1px solid #000; border-radius: 4px; overflow: hidden; z-index: 4; pointer-events: none; }
.hpfill { height: 100%; transition: width .3s; }
.hpbar.ok .hpfill { background: var(--good); }
.hpbar.warn .hpfill { background: var(--warn); }
.hpbar.bad .hpfill { background: var(--bad); }
.hptext { position: absolute; inset: 0; font-size: 9px; line-height: 11px; text-align: center; font-weight: bold; color: #fff; text-shadow: 0 0 2px #000; }
.donut { position: absolute; overflow: visible; pointer-events: none; z-index: 2; }
.donut .seg { pointer-events: auto; cursor: pointer; }
.donut .seg path { fill: #1e1a24e6; stroke: #d4af37; stroke-width: 1.5; transition: fill .15s; }
.donut .seg:hover path, .donut .seg.active path { fill: #6a4e1ae6; }
.donut .seg.off path { fill: #1e1a2499; stroke: #555; }
.donut .seg.off text { opacity: .4; }
.donut .seg text { fill: #fff; pointer-events: none; }
.donut .seg-roll circle { fill: var(--bad); stroke: #fff; stroke-width: 2; }
.donut .seg-roll:hover circle { fill: #ff5555; }
.token.has-donut .token-name { bottom: auto; top: calc(100% + 26px); }
.fx { position: absolute; transform: translate(-50%, -50%); font-family: Georgia, serif; font-weight: 900; font-size: 28px; -webkit-text-stroke: 1.5px #000; text-shadow: 0 2px 6px #000; animation: rise 1.6s ease-out forwards; pointer-events: none; z-index: 10; }
.fx-crit { font-size: 34px; }
.fx-miss { font-size: 22px; }
@keyframes rise { 0% { opacity: 0; margin-top: 10px; } 10% { opacity: 1; } 100% { opacity: 0; margin-top: -50px; } }

/* ---------- side panel ---------- */
.side-panel { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.panel-section { display: flex; flex-direction: column; min-height: 0; padding: 10px 12px; }
.panel-section h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); font-family: inherit; }
.panel-section.chat { flex: 1; border-bottom: 1px solid var(--line); }
.panel-section.activity { flex: 1.2; }
.chat-list, .activity-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; min-height: 0; }
.chat-msg { line-height: 1.35; word-wrap: break-word; }
.chat-msg .who { font-weight: 700; margin-right: 6px; }
.ts { color: #6e6658; font-size: 10px; margin-right: 6px; }
.activity { border-left: 3px solid var(--accent, #888); padding: 2px 6px; background: #ffffff06; border-radius: 0 4px 4px 0; line-height: 1.35; font-size: 13px; }
.activity { border-left-color: var(--accent); }
.activity.type-combat, .activity.type-phase { font-weight: 700; background: #d4af3714; }
.activity.type-move { font-size: 11px; color: var(--muted); }
.chat-form { display: flex; gap: 6px; margin-top: 8px; }
.chat-form input { flex: 1; min-width: 0; }

/* ---------- home / lists ---------- */
.page { max-width: 1000px; margin: 0 auto; padding: 24px 16px; }
.tabs { display: flex; gap: 8px; margin: 16px 0; }
.tabs button { font-size: 16px; padding: 10px 24px; }
.list { display: grid; gap: 10px; }
.list-item { display: flex; gap: 14px; align-items: center; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; flex-wrap: wrap; }
.list-item .thumb { width: 80px; height: 54px; border-radius: 6px; object-fit: cover; background: var(--bg3); }
.list-item .name { font-size: 16px; font-family: Georgia, serif; }
.empty-note { color: var(--muted); padding: 24px; text-align: center; border: 1px dashed var(--line); border-radius: var(--radius); }
.center-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 12px; }

/* ---------- player join flow ---------- */
.flow { max-width: 900px; margin: 0 auto; padding: 40px 16px; text-align: center; }
.flow h1 { font-size: 36px; }
.flow .big-input { font-size: 22px; padding: 12px 16px; width: min(400px, 100%); text-align: center; }
.role-grid, .hero-grid { display: grid; gap: 16px; margin-top: 24px; }
.role-grid { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.hero-grid { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); text-align: left; }
.role-card, .hero-card { background: var(--bg2); border: 2px solid var(--line); border-radius: 14px; padding: 20px; cursor: pointer; transition: transform .1s, border-color .1s; }
.role-card:hover, .hero-card:hover:not(.taken) { transform: translateY(-3px); border-color: var(--gold); }
.role-card .icon { font-size: 48px; }
.role-card h2 { font-size: 26px; margin-top: 8px; }
.hero-card { display: flex; flex-direction: column; gap: 8px; }
.hero-card.taken { opacity: .45; cursor: not-allowed; }
.hero-card .portrait { width: 84px; height: 84px; border-radius: 50%; align-self: center; }
.hero-card h3 { text-align: center; font-size: 20px; color: var(--gold); }
.stats { display: flex; gap: 10px; justify-content: center; font-size: 12px; color: var(--muted); }
.stats b { color: var(--text); }
.hero-card ul { margin: 0; padding-left: 18px; font-size: 12px; color: var(--muted); }
.waiting { font-family: Georgia, serif; font-size: 30px; color: var(--muted); margin-top: 18vh; }
.waiting .spinner { font-size: 48px; display: block; animation: spin 3s linear infinite; margin-bottom: 20px; }
@keyframes spin { to { transform: rotate(360deg); } }
.swatches { display: flex; gap: 6px; flex-wrap: wrap; padding: 6px 8px; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid #0008; cursor: pointer; padding: 0; }
.swatch.current { outline: 3px solid #fff; }
.swatch:disabled { opacity: .2; }
.me-chip { display: inline-flex; align-items: center; gap: 8px; padding: 4px 10px; border-radius: 999px; background: var(--bg3); border: 1px solid var(--line); cursor: pointer; }
.me-chip .dot { width: 12px; height: 12px; border-radius: 50%; }
.mode-banner { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 6; background: #000c; border: 1px solid var(--gold); border-radius: 999px; padding: 6px 14px; display: flex; gap: 10px; align-items: center; }
.turn-info { color: var(--muted); font-size: 12px; }

/* ---------- DM sidebar/inspector ---------- */
.side-tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.side-tabs button { flex: 1; padding: 6px; font-size: 12px; }
.section-title { font-size: 11px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin: 14px 0 6px; }
.scene-item, .player-item, .bank-item {
  display: flex; align-items: center; gap: 8px; padding: 8px; border-radius: 8px; background: var(--bg3); border: 1px solid var(--line); margin-bottom: 6px;
}
.scene-item.active { border-color: var(--gold); background: #3a3020; }
.scene-item .name, .player-item .name, .bank-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bank-item img, .player-item img { width: 30px; height: 30px; border-radius: 50%; object-fit: cover; }
.presence { width: 8px; height: 8px; border-radius: 50%; background: #666; flex: none; }
.presence.online { background: var(--good); }
.presence.reconnecting { background: var(--warn); }
.inspector { border-bottom: 1px solid var(--line); padding: 12px; max-height: 55%; overflow: auto; }
.inspector h3 { color: var(--gold); font-size: 18px; display: flex; gap: 8px; align-items: center; }
.inspector .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.inspector .tag { font-size: 10px; text-transform: uppercase; border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; color: var(--muted); }
.inv-item { display: flex; justify-content: space-between; align-items: center; padding: 3px 0; border-bottom: 1px dashed var(--line); }
.scene-edit-banner { background: repeating-linear-gradient(45deg, #5a4a10, #5a4a10 12px, #4a3c0c 12px, #4a3c0c 24px); color: #ffe9a0; font-weight: 800; letter-spacing: .15em; padding: 6px 14px; border-radius: 6px; }
.tool-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); gap: 6px; }
.tool-grid button { flex-direction: column; padding: 4px; font-size: 10px; }
.tool-grid img { width: 40px; height: 40px; object-fit: contain; }
.tool-grid button.active { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold); }

/* ---------- assets & data pages ---------- */
.page.wide { max-width: 1300px; }
code { background: var(--bg3); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.tabs .count { color: var(--muted); font-size: 12px; }
.dropzone { display: block; border: 2px dashed var(--line); border-radius: var(--radius); padding: 26px; text-align: center; cursor: pointer; color: var(--muted); font-size: 14px; margin-bottom: 10px; }
.dropzone:hover, .dropzone.over { border-color: var(--gold); color: var(--text); background: #d4af370d; }
.upload-status { display: grid; gap: 4px; margin-bottom: 12px; font-size: 13px; }
.upload-status .ok { color: var(--good); }
.upload-status .err { color: var(--bad); }
.asset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.asset-card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.asset-card .preview { height: 130px; display: grid; place-items: center; background: repeating-conic-gradient(#25202b 0 25%, #1c1822 0 50%) 0 0 / 16px 16px; }
.asset-card .preview img { max-width: 100%; max-height: 130px; object-fit: contain; }
.asset-card .meta { padding: 8px 10px; display: grid; gap: 6px; }
.asset-card .fname { font-size: 13px; word-break: break-all; }
.data-main { display: flex; flex-direction: column; min-height: 0; padding: 12px; gap: 10px; }
.json-editor { flex: 1; min-height: 300px; width: 100%; resize: none; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; tab-size: 2; white-space: pre; }
.file-item { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 6px; cursor: pointer; border: 1px solid transparent; }
.file-item:hover { background: var(--bg3); }
.file-item.active { border-color: var(--gold); background: #3a3020; }
.file-item .name { flex: 1; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.messages { display: grid; gap: 3px; max-height: 160px; overflow: auto; font-size: 13px; }
.messages .err { color: #ff8080; }
.messages .warn { color: var(--warn); }
.messages .ok { color: var(--good); }
details.example pre { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px; overflow: auto; font-size: 12px; max-height: 260px; }
