:root {
  --bg: #0c1219;
  --bg-outer: #070b10;
  --surface: #151e2a;
  --surface2: #1c2737;
  --border: rgba(255, 255, 255, 0.11);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #2cb67d;
  --accent-fg: #051610;
  --accent-soft: rgba(44, 182, 125, 0.18);
  --danger: #f43f5e;
  --warn: #f59e0b;
  --ok: #22c55e;
  --p1: #ff595e;
  --p2: #f59e0b;
  --p3: #38bdf8;
  --kit-bg: var(--bg);
  --kit-sheet: #161f2c;
  --kit-fg: var(--text);
}

*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-outer);
  color: var(--text);
  font-family: 'Apple SD Gothic Neo', 'Malgun Gothic', 'Noto Sans KR', system-ui, -apple-system, sans-serif;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  width: 100%;
  max-width: 480px;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 10px calc(env(safe-area-inset-bottom, 0px) + 34px);
  background: radial-gradient(circle at 50% 0%, rgba(44, 182, 125, 0.08), transparent 46%), var(--bg);
  position: relative;
  box-shadow: 0 0 48px rgba(0, 0, 0, 0.55);
}

/* Top Bar — reserve right 52px so #kit-gear never overlaps */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding-right: 52px;
  flex-shrink: 0;
}

.icon-pill {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0 11px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: 700 13.5px/1.2 inherit;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s ease, border-color 0.15s ease;
}

.icon-pill:active, .icon-btn:active, .ctrl-btn:active {
  transform: scale(0.96);
}

.icon-pill #hdr-stage-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  text-align: left;
}

.line-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
  flex-shrink: 0;
}

.chev {
  color: var(--muted);
  font-size: 12px;
}

.topbar-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s ease, background 0.15s ease;
}

.icon-btn.muted {
  opacity: 0.65;
}

/* Subway LED Signboard */
.signboard {
  background: linear-gradient(180deg, #101824 0%, #0b111a 100%);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 14px;
  padding: 8px 11px 7px;
  flex-shrink: 0;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 4px 12px rgba(0, 0, 0, 0.3);
}

.sign-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.sign-line {
  font-size: 11px;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-fg);
  white-space: nowrap;
  flex-shrink: 0;
}

.sign-station-wrap {
  display: flex;
  align-items: baseline;
  gap: 5px;
  min-width: 0;
  flex: 1;
  justify-content: center;
}

.sign-prefix {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.sign-station {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: #ffffff;
  text-shadow: 0 0 12px rgba(255, 255, 255, 0.2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sign-door {
  font-size: 11.5px;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 8px;
  background: rgba(34, 197, 94, 0.18);
  border: 1px solid rgba(34, 197, 94, 0.45);
  color: #86efac;
  white-space: nowrap;
  flex-shrink: 0;
}

.sign-door.side-both {
  background: rgba(250, 204, 21, 0.18);
  border-color: rgba(250, 204, 21, 0.5);
  color: #fde047;
}

.sign-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 6px;
}

.route-track {
  display: flex;
  align-items: center;
  gap: 3px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

.route-node {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 10.5px;
  color: var(--muted);
  white-space: nowrap;
  min-width: 0;
}

.route-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 2px solid rgba(148, 163, 184, 0.5);
  background: transparent;
  flex-shrink: 0;
}

.route-node.done .route-dot {
  background: var(--accent);
  border-color: var(--accent);
}

.route-node.cur {
  color: #fff;
  font-weight: 700;
}

.route-node.cur .route-dot {
  background: #fff;
  border-color: var(--accent);
  box-shadow: 0 0 8px var(--accent);
  transform: scale(1.2);
}

.route-line {
  flex: 1;
  min-width: 8px;
  max-width: 24px;
  height: 2px;
  background: rgba(148, 163, 184, 0.25);
  border-radius: 2px;
}

.route-line.done {
  background: var(--accent);
}

.next-preview {
  font-size: 10.5px;
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}

/* Telemetry Strip */
.hud-strip {
  display: grid;
  grid-template-columns: 76px 1fr 76px;
  gap: 6px;
  flex-shrink: 0;
}

.hud-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 5px 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.hud-moves, .hud-score {
  align-items: center;
}

.hud-lbl {
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  line-height: 1.1;
}

.hud-val {
  font-size: 19px;
  font-weight: 800;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.hud-moves.low .hud-val {
  color: var(--warn);
}

.hud-moves.zero {
  border-color: rgba(244, 63, 94, 0.55);
  background: rgba(244, 63, 94, 0.12);
}

.hud-moves.zero .hud-val {
  color: #fda4af;
}

.cong-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 5px;
}

.cong-num {
  font-size: 11px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.cong-track {
  position: relative;
  height: 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.09);
  overflow: hidden;
}

.cong-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #22c55e, #f59e0b);
  transition: width 0.28s cubic-bezier(0.2, 0.8, 0.2, 1), background 0.25s ease;
}

.cong-fill.warn {
  background: linear-gradient(90deg, #f59e0b, #f97316);
}

.cong-fill.danger {
  background: linear-gradient(90deg, #f97316, #f43f5e);
  box-shadow: 0 0 10px rgba(244, 63, 94, 0.65);
}

.cong-ticks {
  position: absolute;
  inset: 0;
  display: flex;
  pointer-events: none;
}

.cong-ticks > i {
  flex: 1;
  border-right: 1px solid rgba(12, 18, 25, 0.55);
}

.cong-ticks > i:last-child {
  border-right: none;
}

/* Main Subway Car Board */
.board-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: 16px;
  background: linear-gradient(180deg, #0b1018 0%, #0f1622 50%, #0b1018 100%);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

#car-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}

#car-canvas:active {
  cursor: grabbing;
}

.toast {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translate(-50%, -10px);
  padding: 6px 13px;
  border-radius: 999px;
  background: rgba(12, 18, 28, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 12.5px;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 5;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast.ok {
  border-color: rgba(34, 197, 94, 0.6);
  color: #bbf7d0;
}

.toast.warn {
  border-color: rgba(244, 63, 94, 0.65);
  color: #fecdd3;
}

/* Legend Strip */
.legend {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 2px 4px;
  font-size: 11px;
  color: var(--muted);
  flex-shrink: 0;
}

.leg-item {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  white-space: nowrap;
}

.leg-dot {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-style: normal;
  font-size: 10px;
  font-weight: 800;
  color: #fff;
}

.leg-dot.d1 { background: var(--p1); }
.leg-dot.d2 { background: var(--p2); color: #1b1200; }
.leg-dot.d3 { background: var(--p3); color: #051824; }
.leg-dot.dh { background: #334155; border: 1.5px solid #cbd5e1; font-size: 9px; }

.leg-status {
  font-weight: 800;
  font-size: 11.5px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  white-space: nowrap;
}

.leg-status.all-clear {
  background: rgba(34, 197, 94, 0.22);
  color: #86efac;
}

/* Bottom Controls */
.controls {
  display: flex;
  align-items: stretch;
  gap: 7px;
  flex-shrink: 0;
}

.ctrl-btn {
  min-height: 48px;
  min-width: 44px;
  padding: 0 11px;
  border-radius: 13px;
  border: 1px solid var(--border);
  font: 800 13.5px/1.1 inherit;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s ease, background 0.2s ease, box-shadow 0.2s ease, opacity 0.15s ease;
}

.ctrl-sub {
  background: var(--surface2);
  color: var(--text);
}

.ctrl-hint {
  padding: 0 10px;
}

.ctrl-btn[disabled] {
  opacity: 0.42;
  cursor: not-allowed;
}

.ctrl-pri {
  flex: 1;
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  box-shadow: 0 4px 14px rgba(44, 182, 125, 0.28);
}

.ctrl-pri.ready {
  background: #22c55e;
  color: #041b0c;
  animation: pulse-ready 1.4s ease-in-out infinite;
}

.ctrl-pri.urgent {
  background: #f59e0b;
  color: #1c1000;
  animation: pulse-urgent 1.0s ease-in-out infinite;
}

@keyframes pulse-ready {
  0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55); }
  50% { box-shadow: 0 0 0 8px rgba(34, 197, 94, 0); }
}

@keyframes pulse-urgent {
  0%, 100% { box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.65); }
  50% { box-shadow: 0 0 0 9px rgba(245, 158, 11, 0); }
}

/* Modals */
.modal-bd {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: rgba(5, 9, 15, 0.76);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.modal-bd.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  max-height: 86dvh;
  overflow-y: auto;
  background: #141c27;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 18px;
  padding: 16px;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.6);
  transform: translateY(14px) scale(0.98);
  transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.modal-bd.open .modal-card {
  transform: translateY(0) scale(1);
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.modal-head h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
}

.modal-sub {
  margin: 3px 0 0;
  font-size: 12.5px;
  color: var(--muted);
}

.stage-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.stage-tile {
  min-height: 76px;
  padding: 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 4px;
  transition: border-color 0.15s ease, transform 0.1s ease;
}

.stage-tile:active {
  transform: scale(0.97);
}

.stage-tile.current {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.stage-tile.cleared {
  background: linear-gradient(160deg, rgba(44, 182, 125, 0.14), var(--surface2));
}

.st-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
}

.st-num {
  font-weight: 800;
  color: var(--accent);
}

.st-name {
  font-size: 13px;
  font-weight: 800;
  line-height: 1.25;
}

.st-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
}

.st-stars {
  color: #fbbf24;
  letter-spacing: 1px;
}

/* Result Card */
.result-card {
  text-align: center;
  padding: 22px 18px 16px;
}

.res-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  margin-bottom: 8px;
}

.res-badge.fail {
  background: rgba(244, 63, 94, 0.2);
  color: #fda4af;
}

.result-card h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
}

.res-sub {
  margin: 6px 0 12px;
  font-size: 13.5px;
  color: var(--muted);
  line-height: 1.4;
}

.res-stars {
  font-size: 30px;
  letter-spacing: 6px;
  margin-bottom: 14px;
  color: #fbbf24;
  min-height: 36px;
}

.res-stars .off {
  color: rgba(255, 255, 255, 0.18);
}

.res-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.res-stat {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 6px;
}

.res-stat span {
  display: block;
  font-size: 11px;
  color: var(--muted);
}

.res-stat strong {
  display: block;
  font-size: 16px;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

.res-actions {
  display: flex;
  gap: 8px;
}

.res-actions .ctrl-btn {
  flex: 1;
}

.text-btn {
  margin-top: 10px;
  min-height: 44px;
  padding: 0 16px;
  background: none;
  border: none;
  color: var(--muted);
  font: 700 13px/1 inherit;
  text-decoration: underline;
  cursor: pointer;
  touch-action: manipulation;
}

/* Narrow 320px phone optimizations */
@media (max-width: 355px) {
  .app {
    padding-left: 6px;
    padding-right: 6px;
    gap: 5px;
  }
  .topbar {
    padding-right: 50px;
  }
  .icon-pill {
    padding: 0 8px;
    font-size: 12px;
  }
  .sign-station {
    font-size: 15px;
  }
  .sign-prefix {
    display: none;
  }
  .hud-strip {
    grid-template-columns: 64px 1fr 64px;
  }
  .hud-val {
    font-size: 17px;
  }
  .ctrl-btn {
    padding: 0 8px;
    font-size: 12.5px;
  }
  .ctrl-hint .btn-tx {
    display: none;
  }
  .legend {
    font-size: 10px;
  }
  .stage-grid {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 520px) {
  body {
    background: radial-gradient(circle at 50% 25%, #162233 0%, #070b10 70%);
  }
  .app {
    border-left: 1px solid rgba(255, 255, 255, 0.08);
    border-right: 1px solid rgba(255, 255, 255, 0.08);
  }
}
