:root {
  color-scheme: dark;
  --ink: #f5ecdf;
  --muted: #b8acbe;
  --paper: #171321;
  --panel: #211a2d;
  --line: #463953;
  --gold: #ffb84d;
  --mint: #63d9cf;
  --orchid: #d88cff;
  --danger: #ed607b;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--paper);
  color: var(--ink);
}

* { box-sizing: border-box; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background:
    radial-gradient(circle at 14% 8%, rgba(216, 140, 255, .13), transparent 34rem),
    radial-gradient(circle at 90% 46%, rgba(99, 217, 207, .09), transparent 32rem),
    #171321;
}

button, input { font: inherit; }

button {
  color: var(--ink);
  background: #2b2337;
  border: 1px solid #5a4968;
  border-radius: .65rem;
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
}

button:hover:not(:disabled) { border-color: #9a7db0; background: #352a42; }
button:focus-visible, input:focus-visible { outline: 3px solid #ffcf77; outline-offset: 2px; }
button:disabled { opacity: .38; cursor: not-allowed; }

.game-shell { width: min(1180px, calc(100% - 32px)); margin: 0 auto; padding: 24px 0 40px; }
.masthead, .timeline-header, .actor-heading, .header-actions { display: flex; align-items: center; }
.masthead { justify-content: space-between; gap: 20px; margin-bottom: 18px; }
.masthead h1 { margin: 2px 0 0; font-family: Georgia, serif; font-size: clamp(1.55rem, 4vw, 2.65rem); letter-spacing: -.03em; }
.eyebrow { margin: 0; color: #c5a6d5; font-size: .67rem; letter-spacing: .18em; font-weight: 800; }
.header-actions { gap: 10px; }
.room-label { color: var(--muted); text-align: right; }
.icon-button { width: 46px; font-weight: 900; font-size: 1.05rem; }

.restore-banner {
  margin: 0 0 16px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  color: #27172d;
  background: #edc5f8;
  border-radius: .8rem;
}
.restore-banner[hidden] { display: none; }
.restore-banner button { margin-left: auto; min-height: 34px; padding: 4px 10px; color: #27172d; background: transparent; border-color: #76517e; }

.workspace { display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 16px; }
.stage-panel, .control-panel, .timeline-panel { background: rgba(31, 25, 42, .9); border: 1px solid var(--line); border-radius: 1rem; box-shadow: 0 22px 55px rgba(0,0,0,.18); }
.stage-panel { min-width: 0; padding: 10px; }
.canvas-wrap { position: relative; }
#room-canvas { display: block; width: 100%; height: clamp(340px, 52vw, 540px); border-radius: .75rem; touch-action: none; }
.status-chip { position: absolute; left: 12px; top: 12px; padding: 6px 9px; border: 1px solid rgba(255,255,255,.15); border-radius: 999px; background: rgba(12,9,18,.82); color: #eadff0; font: 700 .72rem ui-monospace, monospace; }
.room-hint { margin: 10px 8px 4px; color: #d8c9df; line-height: 1.4; }

.control-panel { padding: 18px; }
.actor-heading { gap: 12px; }
.actor-heading h2, .timeline-header h2 { margin: 2px 0 0; font-family: Georgia, serif; }
.actor-token { display: grid; place-items: center; width: 47px; height: 47px; color: #171321; font: 900 .85rem ui-monospace, monospace; }
.actor-token.actor-0 { border-radius: 50%; background: var(--gold); }
.actor-token.actor-1 { transform: rotate(45deg); border-radius: 4px; background: var(--mint); }
.actor-token.actor-1::first-line { transform: rotate(-45deg); }
.actor-token.actor-2 { border-radius: 9px; background: var(--orchid); }

.dpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin: 22px auto 10px; width: min(100%, 220px); }
.dpad button { font-size: 1.3rem; font-weight: 800; }
.dpad button:first-child { grid-column: 2; }
.dpad button:nth-child(2) { grid-column: 1; }
.dpad button:nth-child(5) { grid-column: 2; }
.primary-action { background: #ead7a3; border-color: #f3e6bd; color: #241a29; font-weight: 850; }
.primary-action:hover:not(:disabled) { background: #fff0bc; border-color: white; }
.finish-action, .primary-action { width: 100%; margin-top: 8px; }
.finish-action { border-color: #887496; }
.key-help { color: #93869d; font-size: .72rem; line-height: 1.5; }
.plan-note { display: block; margin-top: 18px; color: #c5b7cc; font-size: .75rem; font-weight: 750; }
.plan-note input { width: 100%; margin-top: 6px; min-height: 42px; padding: 8px 10px; border: 1px solid #55445f; border-radius: .55rem; color: var(--ink); background: #17131e; }

.timeline-panel { margin-top: 16px; padding: 17px; }
.timeline-header { justify-content: space-between; gap: 12px; }
.playback-controls { display: flex; gap: 6px; }
.playback-controls button { padding: 0 12px; }
.timeline-lanes { margin-top: 16px; display: grid; gap: 8px; }
.timeline-lane { display: grid; grid-template-columns: 82px minmax(0, 1fr); gap: 8px; align-items: center; }
.lane-label { display: flex; justify-content: space-between; align-items: center; padding: 0 9px; border-left: 4px solid var(--gold); }
.actor-1 .lane-label { border-left-color: var(--mint); }
.actor-2 .lane-label { border-left-color: var(--orchid); }
.lane-label span { font: 900 .8rem ui-monospace, monospace; }
.lane-label small { color: #a696ae; font-size: .64rem; }
.beat-cells { display: grid; grid-template-columns: repeat(12, minmax(24px, 1fr)); gap: 4px; }
.beat-cell { min-height: 38px; padding: 0; border-radius: .35rem; color: #c3b4cb; background: #18131f; }
.beat-cell.has-command { color: #201523; background: var(--gold); border-color: #ffd28a; font-weight: 900; }
.actor-1 .beat-cell.has-command { background: var(--mint); border-color: #a3fff7; }
.actor-2 .beat-cell.has-command { background: var(--orchid); border-color: #efc7ff; }
.beat-cell.playhead { outline: 3px solid white; outline-offset: -2px; }
.timeline-scroll-hint { display: none; }
.scrubber { display: grid; grid-template-columns: 52px 1fr 58px; gap: 10px; align-items: center; margin-top: 13px; color: #aa9bb3; font-size: .75rem; }
.scrubber input { width: 100%; accent-color: #e4c27c; }
.scrubber output { text-align: right; }

.game-dialog { width: min(570px, calc(100% - 26px)); padding: 24px; border: 1px solid #7a6489; border-radius: 1rem; color: var(--ink); background: #211a2c; box-shadow: 0 30px 100px rgba(0,0,0,.65); }
.game-dialog::backdrop { background: rgba(8,6,12,.72); backdrop-filter: blur(3px); }
.game-dialog h2 { margin: 7px 0; font: 700 1.65rem Georgia, serif; }
.game-dialog p:not(.eyebrow) { color: #cdbfd4; line-height: 1.55; }
.dialog-actions { display: flex; gap: 8px; margin-top: 20px; }
.dialog-actions button { flex: 1; margin-top: 0; padding: 0 13px; }
.dialog-actions.vertical { flex-direction: column; }
.causal-chain { display: grid; grid-template-columns: 1fr auto 1fr auto 1fr; gap: 7px; align-items: center; margin: 20px 0; }
.causal-chain span { padding: 10px 7px; text-align: center; border: 1px solid #665374; border-radius: .55rem; background: #18131f; }
.causal-chain small { display: block; margin-top: 3px; color: #a996b3; }
.causal-chain b { color: #ed8ba0; font-size: .65rem; text-align: center; }
.diagnosis-note { display: block; color: #d8c9df; font-size: .75rem; font-weight: 750; }
.diagnosis-note input { width: 100%; margin-top: 7px; min-height: 44px; padding: 8px 10px; border: 1px solid #665374; border-radius: .55rem; color: var(--ink); background: #18131f; }
.danger-button { color: #ffafbd; border-color: #874255; }

@media (min-width: 761px) and (max-height: 700px) {
  .game-shell { padding-top: 12px; }
  .masthead { margin-bottom: 10px; }
  #room-canvas { height: 280px; }
  .control-panel { padding: 10px 14px; }
  .actor-token { width: 40px; height: 40px; }
  .dpad { width: 180px; margin: 6px auto; gap: 4px; }
  .dpad button, .primary-action, .finish-action { min-height: 38px; }
  .primary-action, .finish-action { margin-top: 4px; }
  .key-help { display: none; }
  .plan-note { margin-top: 8px; }
  .plan-note input { min-height: 36px; }
  .timeline-panel { margin-top: 10px; padding: 9px 12px; }
  .timeline-header h2 { font-size: 1.25rem; }
  .timeline-lanes { margin-top: 8px; gap: 4px; }
  .lane-label, .beat-cell { min-height: 30px; }
}

@media (max-width: 760px) {
  .game-shell { width: min(100% - 18px, 620px); padding-top: 13px; }
  .room-label { display: none; }
  .workspace { grid-template-columns: 1fr; }
  #room-canvas { height: min(66vw, 420px); min-height: 245px; }
  .control-panel { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; padding: 13px; }
  .actor-heading { grid-column: 1 / -1; }
  .dpad { grid-row: span 4; margin: 4px auto 0; width: 100%; }
  .primary-action, .finish-action, .key-help { margin: 0; }
  .plan-note { grid-column: 1 / -1; margin-top: 4px; }
  .timeline-panel { padding: 12px 9px; }
  .timeline-lanes { overflow-x: auto; padding-bottom: 8px; scrollbar-color: #8f75a2 #18131f; scrollbar-width: auto; }
  .timeline-lane { grid-template-columns: 58px 548px; gap: 4px; width: max-content; }
  .lane-label { position: sticky; left: 0; z-index: 2; min-width: 58px; padding: 0 6px; background: #2b2337; box-shadow: 5px 0 8px rgba(16, 12, 23, .75); }
  .lane-label small { display: none; }
  .beat-cells { grid-template-columns: repeat(12, 44px); gap: 2px; }
  .beat-cell { min-width: 44px; min-height: 44px; }
  .timeline-scroll-hint { display: block; margin: 3px 2px 0 62px; color: #9f91aa; font-size: .68rem; }
  .causal-chain { grid-template-columns: 1fr; }
  .causal-chain b { display: block; height: auto; padding: 1px 0; transform: none; letter-spacing: .08em; }
}

@media (max-width: 420px) {
  .masthead h1 { font-size: 1.45rem; }
  .eyebrow { font-size: .59rem; }
  .control-panel { grid-template-columns: 1.2fr .8fr; }
  .key-help { display: none; }
  .timeline-header h2 { font-size: 1.05rem; }
  .playback-controls button { min-height: 38px; font-size: .72rem; padding: 0 8px; }
}

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }
