:root {
  --bg: #0e0c0a;
  --panel: #1b1713;
  --panel2: #241f19;
  --line: #3d3328;
  --text: #e9dfc9;
  --dim: #a8997e;
  --gold: #d4a94a;
  --gold2: #f0cf7a;
  --red: #c0392b;
  --green: #4caf50;
  --blue: #4a90d9;
  --head: 'Cinzel', Georgia, serif;
  --body: 'Crimson Text', Georgia, serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--body); font-size: 17px; overflow: hidden; }
button { font-family: var(--head); cursor: pointer; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; font-size: 14px; }
button:hover:not(:disabled) { border-color: var(--gold); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: linear-gradient(#8a5a1a, #5e3c10); border-color: var(--gold); color: #fff4d8; }
button.primary:hover:not(:disabled) { background: linear-gradient(#a06a22, #6e4612); }
button.ghost { background: transparent; }
button.big { font-size: 18px; padding: 10px 26px; }
button.small { font-size: 12px; padding: 4px 8px; }
button.icon { width: 34px; height: 34px; padding: 0; font-size: 17px; }
input, select { font-family: var(--body); font-size: 16px; background: #120f0c; color: var(--text); border: 1px solid var(--line); border-radius: 5px; padding: 5px 8px; }
h1, h2, h3 { font-family: var(--head); color: var(--gold2); margin: 0 0 8px; font-weight: 700; }
h3 { font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--gold); }
.hidden { display: none !important; }

#app { height: 100%; }
.screen { display: none; height: 100%; }
.screen.active { display: flex; flex-direction: column; }

/* Title & end */
#screen-title, #screen-end { align-items: center; justify-content: center; background: radial-gradient(ellipse at 50% 35%, #3a2a18 0%, #0e0c0a 65%); }
.title-card { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 14px; max-width: 960px; padding: 20px; }
.title-card h1 { font-size: 64px; text-shadow: 0 0 30px rgba(240, 160, 60, 0.4); }
.subtitle { color: var(--dim); font-style: italic; font-size: 20px; margin: -6px 0 18px; }
#end-title.win { color: var(--gold2); }
#end-title.lose { color: #e06050; }
#end-art { border: 1px solid var(--line); border-radius: 8px; max-width: 100%; }
#end-text { font-size: 19px; line-height: 1.45; }
#end-stats table { border-collapse: collapse; margin: 0 auto 6px; }
#end-stats td { padding: 3px 12px; border-bottom: 1px solid var(--line); }
#end-stats p { color: var(--dim); }

/* Setup */
#screen-setup { background: var(--bg); overflow: hidden; }
.setup-header { padding: 14px 24px 0; }
.setup-grid { flex: 1; display: grid; grid-template-columns: 300px 1fr; gap: 16px; padding: 12px 24px; min-height: 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
#adventure-panel { overflow-y: auto; }
#creator { display: flex; flex-direction: column; min-height: 0; }
.duration { display: block; width: 100%; text-align: left; margin-bottom: 8px; padding: 9px 11px; position: relative; }
.duration.selected { border-color: var(--gold); background: #33281a; }
.duration.disabled { opacity: 0.5; }
.d-name { font-size: 15px; color: var(--gold2); }
.d-time { color: var(--dim); font-size: 12px; margin-left: 6px; }
.d-desc { font-family: var(--body); font-size: 14px; color: var(--dim); margin-top: 2px; }
.d-soon { position: absolute; top: 8px; right: 10px; font-size: 10px; color: var(--dim); border: 1px solid var(--line); padding: 1px 6px; border-radius: 8px; }
.seg { display: flex; gap: 6px; margin-bottom: 10px; }
.seg button { flex: 1; font-size: 18px; }
.seg button.selected, .tab.selected, .card.selected { border-color: var(--gold); background: #3a2c18; }
.note { color: var(--dim); font-size: 14px; font-style: italic; }
.tabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.tab { display: flex; align-items: center; gap: 6px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
#char-form { flex: 1; display: grid; grid-template-columns: 1fr 260px; gap: 18px; min-height: 0; }
.form-left { overflow-y: auto; padding-right: 6px; }
.form-right { display: flex; flex-direction: column; align-items: center; gap: 8px; overflow-y: auto; }
.field { margin-bottom: 12px; }
.field > label { display: block; font-family: var(--head); font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--gold); margin-bottom: 5px; }
.field.inline { display: grid; grid-template-columns: 110px 180px 1fr; align-items: center; gap: 10px; }
.field.inline > label { margin: 0; }
.sel-desc { color: var(--dim); font-size: 14px; }
.name-wrap { display: flex; gap: 6px; }
.name-wrap input { width: 200px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; }
.card { text-align: left; padding: 6px 9px; }
.c-name { font-size: 14px; color: var(--gold2); }
.c-desc { font-family: var(--body); font-size: 13px; color: var(--dim); line-height: 1.25; }
.sw-row { display: flex; align-items: center; gap: 5px; margin-bottom: 6px; flex-wrap: wrap; }
.sw-label { width: 90px; font-size: 14px; color: var(--dim); }
.swatch { width: 24px; height: 24px; border-radius: 50%; padding: 0; border: 2px solid #000; }
.swatch.selected { border-color: #fff; box-shadow: 0 0 0 2px var(--gold); }
#preview-art { border: 1px solid var(--line); border-radius: 8px; }
.token-row { display: flex; align-items: center; gap: 10px; }
.token-label { color: var(--dim); font-size: 14px; }
#preview-stats { width: 100%; font-size: 15px; }
.ps-title { font-family: var(--head); font-size: 20px; color: var(--gold2); text-align: center; }
.ps-sub { text-align: center; color: var(--dim); margin-bottom: 6px; }
.ps-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; text-align: center; margin-bottom: 6px; }
.ps-grid div { background: var(--panel2); border-radius: 5px; padding: 3px; }
.ps-grid b { display: block; font-size: 18px; }
.ps-grid span { font-size: 11px; color: var(--dim); }
.ps-stats { display: grid; grid-template-columns: repeat(6, 1fr); gap: 3px; text-align: center; margin-bottom: 6px; }
.ps-stats div { background: #120f0c; border-radius: 4px; padding: 2px 0; }
.ps-stats span { display: block; font-size: 10px; color: var(--gold); font-family: var(--head); }
.ps-stats i { display: block; font-size: 12px; color: var(--dim); font-style: normal; }
.ps-line, .ps-traits { font-size: 14px; color: var(--dim); margin-bottom: 4px; }
.pill { display: inline-block; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 1px 8px; margin: 2px; font-size: 13px; }
.setup-footer { display: flex; justify-content: space-between; padding: 10px 24px 16px; }

/* Game */
#screen-game { background: #050506; }
#topbar { height: 64px; display: flex; align-items: center; gap: 10px; padding: 0 10px; background: var(--panel); border-bottom: 1px solid var(--line); }
#round-label { font-family: var(--head); color: var(--gold2); white-space: nowrap; min-width: 82px; }
#initiative { flex: 1; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: thin; padding: 4px 0; }
.chip { flex: none; width: 62px; display: flex; flex-direction: column; align-items: center; padding: 3px; border-radius: 6px; border: 1px solid transparent; cursor: pointer; opacity: 0.8; }
.chip.current { border-color: var(--gold); background: #33281a; opacity: 1; }
.chip.downed { filter: grayscale(0.8); }
.chip-name { font-size: 11px; max-width: 58px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-hp { width: 40px; height: 3px; background: #000; }
.chip-hp div { height: 100%; background: var(--green); }
.env-icon { font-size: 24px; height: 34px; line-height: 34px; }
.top-buttons { display: flex; gap: 6px; }
#game-main { flex: 1; display: flex; min-height: 0; }
#map-wrap { flex: 1; position: relative; min-width: 0; }
#map { display: block; cursor: crosshair; }
#sidebar { width: 340px; display: flex; flex-direction: column; gap: 8px; padding: 8px; background: #0f0d0b; border-left: 1px solid var(--line); min-height: 0; }
#sidebar .panel { padding: 10px; }
.q-title { font-family: var(--head); font-size: 16px; color: var(--text); }
.q-obj { color: var(--dim); font-size: 14px; line-height: 1.3; }
.q-prog { color: var(--gold2); font-size: 14px; margin-top: 4px; }
.cp-head { display: flex; gap: 10px; align-items: center; }
.cp-info { flex: 1; }
.cp-name { font-family: var(--head); font-size: 18px; color: var(--gold2); }
.cp-sub { font-size: 14px; color: var(--dim); }
.hpbar { position: relative; height: 18px; background: #2a0d0d; border-radius: 4px; overflow: hidden; margin-top: 4px; }
.hpbar div { height: 100%; background: linear-gradient(#5cbf60, #2f8a33); }
.hpbar span { position: absolute; inset: 0; text-align: center; font-size: 13px; line-height: 18px; text-shadow: 0 1px 2px #000; }
.ap-row { display: flex; align-items: center; gap: 5px; margin: 10px 0 6px; }
.ap-label { font-family: var(--head); font-size: 12px; color: var(--gold); width: 24px; }
.pip { width: 18px; height: 18px; border-radius: 50%; border: 2px solid #4a6a8a; background: #0e1620; }
.pip.on { background: radial-gradient(circle at 35% 35%, #bfe4ff, #3a8ad9); border-color: #9ad0ff; box-shadow: 0 0 6px #3a8ad9; }
.ap-extra { font-size: 13px; color: var(--dim); margin-left: 4px; }
.statuses { margin-bottom: 4px; }
.abilities { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; margin: 6px 0; }
.ab { position: relative; display: flex; align-items: center; gap: 5px; text-align: left; padding: 5px 6px; font-size: 12px; }
.ab.off { opacity: 0.45; }
.ab.active { border-color: #ffcc40; background: #4a3a12; }
.ab-key { font-size: 10px; color: var(--dim); width: 9px; }
.ab-icon { font-size: 16px; }
.ab-name { flex: 1; line-height: 1.1; }
.ab-cost { font-size: 10px; color: #9ad0ff; white-space: nowrap; }
.ab-cd { position: absolute; top: -5px; right: -5px; background: #8a2020; color: #fff; border-radius: 50%; width: 17px; height: 17px; font-size: 10px; line-height: 17px; text-align: center; }
.cp-actions { display: flex; gap: 6px; }
.cp-actions button { flex: 1; }
.waiting { text-align: center; color: var(--dim); font-style: italic; padding: 18px 0; }
.hourglass { font-size: 28px; animation: spin 2s linear infinite; display: inline-block; margin-bottom: 4px; }
@keyframes spin { to { transform: rotate(360deg); } }
.pm { display: flex; align-items: center; gap: 8px; padding: 3px 4px; border-radius: 5px; cursor: pointer; }
.pm.current { background: #33281a; }
.pm.dead { opacity: 0.5; }
.pm-name { font-size: 15px; }
.pm-state { font-size: 12px; color: var(--dim); }
#log-panel { flex: 1; display: flex; flex-direction: column; min-height: 80px; }
#log { flex: 1; overflow-y: auto; font-size: 14px; line-height: 1.3; }
.log-line { padding: 2px 0; border-bottom: 1px solid #221c16; color: var(--dim); }
.log-line.major { color: var(--text); }
.log-line.round { color: var(--gold); text-align: center; font-family: var(--head); font-size: 12px; border: none; padding-top: 6px; }

#tooltip { position: absolute; pointer-events: none; background: rgba(15, 12, 9, 0.94); border: 1px solid var(--gold); border-radius: 6px; padding: 6px 9px; font-size: 14px; line-height: 1.3; max-width: 240px; z-index: 5; }
#tooltip .warn { color: #ff8060; }
#tooltip .ok { color: #8fe08f; }
#map-hint { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: rgba(15, 12, 9, 0.9); border: 1px solid var(--line); border-radius: 6px; padding: 5px 14px; font-size: 15px; opacity: 0; transition: opacity 0.3s; pointer-events: none; white-space: nowrap; }
#map-hint.show { opacity: 1; }

#dialog { position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); width: min(860px, calc(100% - 30px)); display: flex; gap: 14px; background: linear-gradient(#2a2118, #18130e); border: 2px solid var(--gold); border-radius: 10px; padding: 10px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.8); cursor: pointer; z-index: 10; animation: rise 0.18s ease-out; }
@keyframes rise { from { transform: translate(-50%, 12px); opacity: 0; } }
#dialog-art { width: 300px; height: 183px; border-radius: 6px; border: 1px solid var(--line); flex: none; }
.dialog-body { flex: 1; display: flex; flex-direction: column; }
#dialog-text { flex: 1; font-size: 20px; line-height: 1.4; padding: 4px 4px 0; }
#dialog-hint { font-size: 12px; color: var(--dim); text-align: right; font-style: italic; }

.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card { background: var(--panel); border: 1px solid var(--gold); border-radius: 10px; padding: 18px 22px; min-width: 380px; max-height: 90vh; overflow-y: auto; }
.modal-card.wide { width: min(920px, 94vw); }
.modal-card .row { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.modal-foot { text-align: right; margin-top: 12px; }
.help-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.help-cols p, .help-cols li { font-size: 15px; line-height: 1.4; color: var(--text); }
.help-cols ul { padding-left: 18px; }

/* ---------- Touch & small screens ---------- */
#app { height: 100vh; height: 100dvh; }
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }
#map { touch-action: none; }
button { -webkit-tap-highlight-color: transparent; }
#sheet-toggle { display: none; }
#map-controls { position: absolute; right: 10px; bottom: 10px; display: none; flex-direction: column; gap: 6px; z-index: 6; }
#map-controls button { width: 44px; height: 44px; font-size: 20px; background: rgba(27, 23, 19, 0.88); }
body.touch #map-controls { display: flex; }
body.touch .ab-key { display: none; }
body.touch .ab { min-height: 42px; }
.title-card h1 { font-size: clamp(34px, 9vw, 64px); }
#map-hint { max-width: 92%; white-space: normal; text-align: center; }

/* Tablets */
@media (max-width: 1100px) {
  #sidebar { width: 290px; }
  .setup-grid { grid-template-columns: 240px 1fr; padding: 10px 14px; }
  #char-form { grid-template-columns: 1fr 210px; }
  #preview-art { width: 200px; height: auto; }
}

/* Phones (portrait) and narrow tablets */
@media (max-width: 760px) {
  body { font-size: 16px; }
  #screen-setup { overflow-y: auto; -webkit-overflow-scrolling: touch; }
  #screen-setup.active { display: block; }
  .setup-header { padding: 12px 14px 0; }
  .setup-header h1 { font-size: 26px; }
  .setup-grid { display: block; padding: 8px 10px; }
  #adventure-panel { margin-bottom: 10px; overflow: visible; }
  #duration-list { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .duration { margin: 0; }
  .d-desc { display: none; }
  .d-soon { position: static; display: inline-block; margin-top: 4px; }
  .d-time { display: block; margin: 0; }
  .duration.selected .d-desc { display: block; }
  #creator { display: block; }
  #char-form { display: flex; flex-direction: column; gap: 10px; }
  .form-left { overflow: visible; padding: 0; }
  .form-right { order: -1; display: grid; grid-template-columns: 130px 1fr; align-items: start; gap: 10px; overflow: visible; }
  #preview-art { width: 130px; height: auto; grid-row: span 2; }
  .token-row { display: none; }
  #preview-stats { font-size: 14px; }
  .ps-title { text-align: left; font-size: 18px; }
  .ps-sub { text-align: left; }
  .ps-stats { grid-template-columns: repeat(3, 1fr); }
  .field.inline { grid-template-columns: 1fr; gap: 4px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
  .name-wrap { flex-wrap: wrap; }
  .name-wrap input { flex: 1; min-width: 140px; width: auto; }
  .swatch { width: 30px; height: 30px; }
  .sw-label { width: 100%; }
  .setup-footer { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding: 16px 12px 12px; }

  #topbar { height: 52px; gap: 6px; padding: 0 6px; }
  #round-label { min-width: 0; font-size: 12px; }
  .chip { width: 46px; padding: 2px; }
  .chip canvas { width: 28px; height: 28px; }
  .chip-name { font-size: 9px; max-width: 44px; }
  .env-icon { font-size: 20px; height: 28px; line-height: 28px; }
  .top-buttons button.icon { width: 32px; height: 32px; }

  #game-main { flex-direction: column; }
  #map-wrap { flex: 1; min-height: 0; }
  #sidebar { width: 100%; flex: none; max-height: 44vh; max-height: 44dvh; border-left: none; border-top: 1px solid var(--line); padding: 6px; gap: 6px; overflow-y: auto; }
  #sidebar.expanded { max-height: 60vh; max-height: 60dvh; }
  #sheet-toggle { display: block; width: 100%; padding: 6px; order: 2; }
  #char-panel { order: 1; }
  #quest-panel { order: 3; }
  #party-panel { order: 4; }
  #log-panel { order: 5; min-height: 160px; }
  #sidebar:not(.expanded) #quest-panel, #sidebar:not(.expanded) #party-panel, #sidebar:not(.expanded) #log-panel { display: none; }
  #sidebar .panel { padding: 8px; }
  .cp-head canvas { width: 40px; height: 40px; }
  .cp-name { font-size: 16px; }
  .ap-row { margin: 6px 0 4px; }
  .pip { width: 16px; height: 16px; }
  .abilities { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 4px; }
  .ab { padding: 4px; font-size: 11px; }
  .ab-icon { font-size: 15px; }

  #dialog { position: fixed; top: 58px; bottom: auto; padding: 6px; gap: 8px; width: calc(100% - 12px); max-height: 45vh; max-height: 45dvh; }
  #dialog-art { width: 120px; height: 73px; }
  #dialog-text { font-size: 15px; overflow-y: auto; }
  #tooltip { font-size: 13px; max-width: 200px; }
  #map-controls { bottom: auto; top: 10px; }

  .modal-card { min-width: 0; width: 94vw; padding: 14px; }
  .help-cols { grid-template-columns: 1fr; gap: 0; }
  #screen-end { overflow-y: auto; justify-content: flex-start; }
  #end-text { font-size: 16px; }
}

/* Phones held sideways */
@media (max-height: 520px) and (min-width: 561px) {
  #topbar { height: 44px; }
  .chip { width: 46px; }
  .chip canvas { width: 26px; height: 26px; }
  .chip-name { font-size: 9px; }
  #sidebar { width: 250px; padding: 5px; gap: 5px; overflow-y: auto; }
  #sidebar .panel { padding: 7px; }
  #quest-panel .q-obj { display: none; }
  #char-panel { order: 1; }
  #quest-panel { order: 2; }
  #party-panel { order: 3; }
  #log-panel { order: 4; }
  .cp-head canvas { width: 36px; height: 36px; }
  .cp-name { font-size: 15px; }
  .hpbar { height: 15px; }
  .hpbar span { line-height: 15px; font-size: 11px; }
  .abilities { grid-template-columns: 1fr 1fr; gap: 3px; }
  .ab { padding: 3px 4px; font-size: 11px; min-height: 34px; }
  #log-panel { min-height: 120px; }
  #dialog { bottom: 6px; padding: 6px; gap: 8px; }
  #dialog-art { width: 150px; height: 92px; }
  #dialog-text { font-size: 15px; }
  #screen-setup { overflow-y: auto; }
  .setup-grid { min-height: auto; }
  .title-card { gap: 8px; }
  .subtitle { margin-bottom: 6px; }
}

/* In-game menu */
.menu-card { width: min(420px, 92vw); min-width: 0; text-align: center; }
.menu-card button { display: block; width: 100%; margin-top: 12px; }
.menu-note { color: var(--dim); font-size: 14px; margin: 4px 0 0; }
#menu-confirm-text { font-size: 18px; line-height: 1.4; }

#set-voicename { max-width: 230px; }
.voice-note { margin: -4px 0 6px; font-size: 13px; }

/* Side quest line in the quest panel */
.q-side { margin-top: 6px; padding-top: 6px; border-top: 1px dashed var(--line); font-size: 14px; color: var(--text); }
.q-side span { display: block; font-family: var(--head); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--gold); }
.q-side.done { color: #8fe08f; }
