:root {
  color-scheme: dark;
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  --bg: #162129;
  --panel: #21313a;
  --panel-2: #2b3e49;
  --line: #60727b;
  --ink: #f6f0e2;
  --muted: #d2c7ae;
  --amber: #e1af58;
  --amber-2: #ffe3a7;
  --cyan: #9ed8d2;
  --danger: #d26c59;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body, #app { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { background: var(--bg); color: var(--ink); }
button, input { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--amber-2); outline-offset: 2px; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

#app { display: grid; grid-template-rows: minmax(0, 59fr) minmax(300px, 41fr); background: #18242c; }
#visualStage { position: relative; min-height: 0; overflow: hidden; background: #263740; border-bottom: 3px solid #50616a; }
#viewport { position: absolute; inset: 0; display: grid; place-items: center; background: #2b3d46; }
#viewport canvas { width: 100% !important; height: 100% !important; display: block; image-rendering: pixelated; image-rendering: crisp-edges; cursor: crosshair; }
.pixel-grid, .scanlines, .vignette, #damageFlash { position: absolute; inset: 0; pointer-events: none; }
.pixel-grid { z-index: 2; opacity: .04; background-image: linear-gradient(rgba(255,255,255,.08) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.06) 1px, transparent 1px); background-size: 4px 4px; mix-blend-mode: overlay; }
.scanlines { z-index: 3; opacity: .08; background: repeating-linear-gradient(0deg, rgba(0,0,0,.22) 0, rgba(0,0,0,.22) 1px, transparent 1px, transparent 4px); }
.vignette { z-index: 4; background: radial-gradient(circle at 50% 45%, transparent 36%, rgba(10,18,22,.12) 72%, rgba(8,12,14,.42) 112%); }
#damageFlash { z-index: 8; opacity: 0; background: rgba(190,35,25,.45); }
#damageFlash.active { animation: damage .5s steps(3); }
@keyframes damage { 0% { opacity: 0; } 25% { opacity: .9; } 100% { opacity: 0; } }

.game-header { position: absolute; z-index: 10; inset: 0 0 auto 0; min-height: 74px; padding: 14px 18px; display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; background: linear-gradient(to bottom, rgba(24,36,42,.86), rgba(24,36,42,.36), transparent); }
.kicker { margin: 0 0 5px; color: var(--amber); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 10px; font-weight: 800; letter-spacing: .14em; }
h1 { margin: 0; font-family: Georgia, "Noto Serif TC", serif; font-size: clamp(18px, 2.2vw, 29px); letter-spacing: .04em; text-shadow: 2px 2px rgba(20,28,33,.55); }
.header-actions { display: flex; align-items: center; gap: 8px; }
.scene-chip, .square-button { border: 2px solid #6b7d80; background: rgba(35,49,58,.9); box-shadow: 3px 3px 0 rgba(18,24,28,.45); }
.scene-chip { padding: 8px 11px; color: var(--amber-2); font-size: 12px; white-space: nowrap; }
.square-button { width: 36px; height: 34px; cursor: pointer; }
.square-button:hover { border-color: var(--amber); color: var(--amber-2); }

.mission-card { position: absolute; z-index: 10; left: 18px; top: 83px; width: min(340px, calc(100% - 36px)); padding: 10px 12px; border-left: 4px solid var(--amber); background: rgba(31,47,54,.82); box-shadow: 4px 4px 0 rgba(14,20,23,.35); }
.mission-card span { display: block; color: var(--muted); font-size: 10px; letter-spacing: .14em; }
.mission-card strong { display: block; margin-top: 3px; font-size: 13px; line-height: 1.4; }
.hud-row { position: absolute; z-index: 10; left: 18px; right: 18px; bottom: 12px; display: flex; gap: 6px; pointer-events: none; }
.hud-row > div { min-width: 82px; padding: 7px 9px; border: 2px solid #607179; background: rgba(33,48,56,.84); box-shadow: 3px 3px 0 rgba(15,21,24,.38); }
.hud-row .wide { flex: 1; min-width: 130px; }
.hud-row span { display: block; color: var(--muted); font-size: 9px; letter-spacing: .12em; }
.hud-row strong { display: block; margin-top: 1px; color: #fff4d1; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#hotspotLabel { position: absolute; z-index: 12; transform: translate(-50%, calc(-100% - 14px)); display: none; max-width: 220px; padding: 7px 9px; border: 2px solid var(--amber); background: #26404b; color: var(--amber-2); font-size: 12px; box-shadow: 4px 4px 0 rgba(18,24,28,.45); pointer-events: none; }
.transition-label { position: absolute; z-index: 15; inset: 0; display: grid; place-items: center; background: rgba(21,32,37,.45); color: var(--amber-2); font-family: ui-monospace, monospace; font-size: 13px; letter-spacing: .14em; text-shadow: 2px 2px #000; }

#commandDeck { min-height: 0; display: grid; grid-template-rows: auto minmax(74px, 1fr) auto auto auto; gap: 8px; padding: 12px 16px calc(10px + var(--safe-bottom)); background: linear-gradient(180deg, #23343d, #1a262c); border-top: 1px solid #70838b; }
.deck-topline { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.deck-topline h2 { margin: 0; font-family: Georgia, "Noto Serif TC", serif; font-size: clamp(18px, 2vw, 24px); }
.system-badges { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 5px; }
.system-badges span { padding: 4px 7px; border: 1px solid #73848a; color: var(--muted); font: 10px ui-monospace, monospace; background: rgba(255,255,255,.03); }
#saveStatus { color: var(--cyan); }
.message-log { min-height: 0; overflow-y: auto; padding: 10px 12px; border: 2px solid #64747a; background: #172228; box-shadow: inset 0 0 0 2px #213039; scrollbar-color: #90a1a6 #152026; }
.message { display: grid; grid-template-columns: 68px minmax(0,1fr); gap: 9px; padding: 5px 0; font-size: 13px; line-height: 1.55; }
.message + .message { border-top: 1px dashed rgba(255,255,255,.08); }
.message .speaker { color: var(--amber); font: 700 10px ui-monospace, monospace; letter-spacing: .08em; padding-top: 3px; }
.message.player .speaker { color: var(--cyan); }
.message.system .speaker { color: #8c8f92; }
.message p { margin: 0; color: #f0ebdd; }
.choice-row { display: flex; gap: 7px; overflow-x: auto; padding-bottom: 2px; }
.choice-button { flex: 0 0 auto; min-height: 38px; padding: 8px 12px; border: 2px solid #6f8187; background: #324550; box-shadow: 3px 3px 0 rgba(17,23,27,.42); cursor: pointer; font-size: 12px; }
.choice-button:hover { border-color: var(--amber); color: var(--amber-2); transform: translateY(-1px); }
.choice-button.primary { border-color: #bf8d39; background: #7b5930; color: #fff3d1; }
.choice-button:disabled { opacity: .42; cursor: not-allowed; transform: none; }
.command-form { display: grid; grid-template-columns: minmax(0,1fr) 74px; gap: 7px; }
.command-form input { min-width: 0; height: 42px; border: 2px solid #73858b; background: #edf1ee; color: #25333a; padding: 0 12px; border-radius: 0; }
.command-form input::placeholder { color: #697b82; }
.command-form button, .primary-button { border: 2px solid #d29b44; background: #8f6530; color: #fff4d3; box-shadow: 3px 3px 0 rgba(18,24,28,.45); cursor: pointer; font-weight: 800; }
.command-form button:hover, .primary-button:hover { background: #8b682f; }
.deck-footer { display: flex; justify-content: space-between; align-items: center; gap: 12px; color: #c8cfcd; font-size: 10px; }
.deck-footer > div { display: flex; gap: 10px; }
.text-button { padding: 0; border: 0; background: transparent; color: #d8dfdc; cursor: pointer; text-decoration: underline; }
.text-button:hover { color: var(--amber-2); }

.overlay-screen, .modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 20px; background: rgba(23,34,39,.78); backdrop-filter: blur(5px); }
.start-card, .modal-card { width: min(610px, 100%); padding: 24px; border: 3px solid #6f8086; background: linear-gradient(145deg, #28404a, #1a262b); box-shadow: 9px 9px 0 rgba(16,22,26,.48); }
.start-card h2, .modal-card h2 { margin: 0 0 12px; font-family: Georgia, "Noto Serif TC", serif; font-size: clamp(27px, 5vw, 43px); }
.start-card > p:not(.kicker):not(.fine-print), .modal-card p { color: #efe7d7; line-height: 1.7; }
.feature-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin: 18px 0; }
.feature-grid span { padding: 9px; border: 1px solid #708188; color: #efe6d3; font-size: 12px; text-align: center; background: #22333b; }
.primary-button, .secondary-button { min-height: 43px; padding: 8px 16px; }
.secondary-button { margin-left: 7px; border: 2px solid #6f8087; background: #32444d; color: #f0ebdd; cursor: pointer; box-shadow: 3px 3px 0 rgba(18,24,28,.45); }
.fine-print { margin: 16px 0 0; color: #d0d5d3 !important; font-size: 10px; }
.modal-close { position: absolute; top: 10px; right: 12px; width: 34px; height: 34px; border: 2px solid #6f8087; background: #2b3b43; cursor: pointer; }
.modal-card { position: relative; }
.dice-card { max-width: 460px; }
.dice-result { display: flex; align-items: center; gap: 18px; margin: 18px 0; }
.die-face { width: 82px; height: 82px; display: grid; place-items: center; border: 4px solid #d5a850; background: #294049; color: #fff1c8; box-shadow: 6px 6px 0 rgba(16,22,26,.48); font: 900 34px ui-monospace, monospace; }
.die-face.rolling { animation: dice .12s steps(2) infinite; }
@keyframes dice { 0% { transform: translate(-2px,1px) rotate(-2deg); } 50% { transform: translate(2px,-1px) rotate(2deg); } }
.dice-result p { margin: 7px 0 0; }
.toast { position: fixed; z-index: 80; left: 50%; bottom: calc(18px + var(--safe-bottom)); transform: translate(-50%, 20px); max-width: min(90vw, 420px); padding: 9px 12px; border: 2px solid var(--amber); background: #29414b; color: var(--amber-2); box-shadow: 4px 4px 0 rgba(16,22,26,.48); opacity: 0; pointer-events: none; transition: .2s; font-size: 12px; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 760px) {
  #app { grid-template-rows: minmax(260px, 45fr) minmax(360px, 55fr); }
  .game-header { min-height: 64px; padding: 10px 11px; }
  .kicker { font-size: 8px; }
  h1 { font-size: 17px; }
  .scene-chip { display: none; }
  .square-button { width: 32px; height: 30px; }
  .mission-card { top: 64px; left: 10px; width: calc(100% - 20px); padding: 7px 9px; }
  .mission-card strong { font-size: 11px; }
  .hud-row { left: 9px; right: 9px; bottom: 7px; gap: 3px; }
  .hud-row > div { min-width: 57px; padding: 4px 6px; border-width: 1px; }
  .hud-row .wide { min-width: 90px; }
  .hud-row strong { font-size: 10px; }
  #commandDeck { padding: 9px 10px calc(8px + var(--safe-bottom)); gap: 6px; grid-template-rows: auto minmax(96px,1fr) auto auto auto; }
  .deck-topline h2 { font-size: 17px; }
  .system-badges span:not(#saveStatus) { display: none; }
  .message-log { padding: 7px 8px; }
  .message { grid-template-columns: 55px minmax(0,1fr); font-size: 12px; }
  .choice-button { min-height: 36px; padding: 7px 10px; }
  .deck-footer > span { display: none; }
  .deck-footer { justify-content: flex-end; }
  .start-card, .modal-card { padding: 19px; }
  .feature-grid { gap: 5px; }
  .feature-grid span { padding: 7px 5px; font-size: 10px; }
}

@media (max-height: 650px) and (orientation: landscape) {
  #app { grid-template-columns: 58fr 42fr; grid-template-rows: 1fr; }
  #visualStage { border-bottom: 0; border-right: 3px solid #262c2f; }
  #commandDeck { grid-template-rows: auto minmax(90px,1fr) auto auto auto; }
}

/* daylight palette base */
:root {
  color-scheme: light;
  --bg: #b7d6d8;
  --panel: #edf0e8;
  --panel-2: #d8e5e3;
  --line: #42616b;
  --ink: #1d3038;
  --muted: #536c72;
  --amber: #a96616;
  --amber-2: #7b4304;
  --cyan: #176f78;
  --danger: #b84837;
}
body { background: #aacdd1; color: var(--ink); }
#app { background: #bad7d9; }
#visualStage { background: #9fc8cd; border-bottom-color: #315762; }
#viewport { background: #b1d3d6; }
.pixel-grid { opacity: .025; }
.scanlines { opacity: .045; }
.vignette { background: radial-gradient(circle at 50% 44%, transparent 48%, rgba(42,69,75,.08) 78%, rgba(28,48,54,.24) 118%); }
.game-header { background: linear-gradient(to bottom, rgba(235,242,234,.92), rgba(210,228,226,.56), transparent); color: #17313a; }
h1 { text-shadow: 1px 1px rgba(255,255,255,.7); }
.build-badge { display: inline-block; margin-top: 5px; padding: 3px 7px; border: 2px solid #2f6f70; background: #d8f1dd; color: #164b4e; box-shadow: 2px 2px 0 rgba(33,68,72,.3); font: 800 9px ui-monospace, monospace; letter-spacing: .08em; }
.scene-chip, .square-button { border-color: #4d6d75; background: rgba(244,244,231,.92); color: #2a4850; box-shadow: 3px 3px 0 rgba(38,66,72,.28); }
.mission-card { background: rgba(247,239,214,.94); color: #273c41; box-shadow: 4px 4px 0 rgba(43,71,77,.22); }
.mission-card span { color: #6f654d; }
.hud-row > div { border-color: #50737b; background: rgba(239,242,228,.94); box-shadow: 3px 3px 0 rgba(34,62,68,.25); }
.hud-row span { color: #56696b; }
.hud-row strong { color: #213941; }
#hotspotLabel { background: #fff1b8; color: #593600; box-shadow: 4px 4px 0 rgba(48,69,72,.28); }
.transition-label { background: rgba(206,227,226,.68); color: #5f3900; text-shadow: 1px 1px #fff4d0; }
#commandDeck { background: linear-gradient(180deg, #f0f2e9, #dce8e6); border-top-color: #466b75; color: #20363e; }
.system-badges span { border-color: #6b858b; background: rgba(255,255,255,.5); color: #3f5a60; }
#aiStatus { color: #096771; border-color: #2b858c; background: #d8f5ef; }
#aiStatus.offline { color: #8a4d12; border-color: #bc8b50; background: #fff0d3; }
#aiStatus.busy { color: #694400; border-color: #c18d24; background: #ffedaf; }
.message-log { border-color: #64828a; background: #fbfaf2; box-shadow: inset 0 0 0 2px #d3dfdd; scrollbar-color: #729198 #e7eeeb; }
.message + .message { border-top-color: rgba(38,74,82,.16); }
.message p { color: #263c43; }
.message.system .speaker { color: #68787b; }
.choice-row { min-height: 43px; transition: opacity .18s, max-height .18s, min-height .18s; }
.choice-row.hints-hidden { opacity: 0; max-height: 0; min-height: 0; overflow: hidden; padding: 0; pointer-events: none; }
.choice-button { border-color: #557680; background: #e2ece8; color: #263f47; box-shadow: 3px 3px 0 rgba(42,67,72,.24); }
.choice-button.primary { border-color: #9d661e; background: #e6b966; color: #382200; }
.command-form input { border-color: #476e79; background: #ffffff; color: #1b3038; box-shadow: inset 0 0 0 2px #e4ece9; }
.command-form input::placeholder { color: #71858a; }
.command-form button, .primary-button { border-color: #8d5916; background: #d29a45; color: #2c1a00; box-shadow: 3px 3px 0 rgba(66,72,59,.28); }
.command-form button:hover, .primary-button:hover { background: #e5ad55; }
.deck-footer { color: #4e656a; }
.text-button { color: #2f5b65; }
.overlay-screen, .modal { background: rgba(129,167,170,.72); }
.start-card, .modal-card { border-color: #4f737c; background: linear-gradient(145deg, #f8f3dd, #dce9e7); color: #223b43; box-shadow: 9px 9px 0 rgba(37,62,68,.3); }
.start-card > p:not(.kicker):not(.fine-print), .modal-card p { color: #334b51; }
.feature-grid span { border-color: #739096; color: #2a4249; background: #edf3eb; }
.secondary-button { border-color: #648087; background: #e4ece8; color: #2b4248; box-shadow: 3px 3px 0 rgba(38,62,67,.25); }
.fine-print { color: #536c71 !important; }
.modal-close { border-color: #648087; background: #e6efec; color: #29434a; }
.die-face { border-color: #a36a1e; background: #e8d5a3; color: #4e3000; box-shadow: 6px 6px 0 rgba(44,66,69,.25); }
.toast { background: #fff1ba; color: #593600; box-shadow: 4px 4px 0 rgba(44,66,69,.25); }

@media (max-width: 760px) {
  .build-badge { margin-top: 3px; font-size: 7px; padding: 2px 5px; }
}

/* v0.4 Goal Engine additions */
.hud-row .situation { min-width: 150px; border-color: #9a6a25; background: rgba(255,239,194,.95); }
.hud-row .situation span { color: #76521f; }
.hud-row .situation strong { color: #5a3907; }
.message .speaker { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message.system p { color: #53676c; }
.message.dm .speaker { color: #9a5c12; }
.choice-button { max-width: min(78vw, 300px); white-space: normal; text-align: left; }
.command-form input { font-weight: 600; }
.build-badge { background: #ffe9a9; border-color: #9a6a25; color: #5a3907; }

@media (max-width: 760px) {
  .hud-row .situation { min-width: 104px; }
  .hud-row .wide { display: none; }
}
