:root {
  --bg-outer: #04060b;
  --bg-panel: #0b1019;
  --bg-card: rgba(13, 19, 30, 0.92);
  --surface: rgba(255, 255, 255, 0.05);
  --border: rgba(148, 163, 184, 0.18);
  --fg: #edf2f7;
  --fg-muted: #94a3b8;
  --accent: #38bdf8;
  --green: #22c55e;
  --green-glow: rgba(34, 197, 94, 0.35);
  --amber: #f59e0b;
  --amber-glow: rgba(245, 158, 11, 0.32);
  --red: #ef4444;
  --red-glow: rgba(239, 68, 68, 0.38);
  --blue-amb: #3b82f6;
  --kit-bg: #070a10;
  --kit-sheet: #131a26;
  --kit-fg: #edf2f7;
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: radial-gradient(circle at 50% 0%, #101828 0%, var(--bg-outer) 72%);
  color: var(--fg);
  font-family: system-ui, -apple-system, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button {
  font-family: inherit;
  color: inherit;
  touch-action: manipulation;
}

.app-shell {
  width: 100%;
  max-width: 480px;
  min-width: 320px;
  height: 100%;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, rgba(11, 16, 25, 0.96) 0%, #070a10 100%);
  box-shadow: 0 0 64px rgba(0, 0, 0, 0.85);
  border-left: 1px solid rgba(255, 255, 255, 0.04);
  border-right: 1px solid rgba(255, 255, 255, 0.04);
  padding-top: calc(env(safe-area-inset-top, 0px) + 8px);
  padding-bottom: max(30px, calc(env(safe-area-inset-bottom, 0px) + 26px));
  padding-left: calc(env(safe-area-inset-left, 0px) + 10px);
  padding-right: calc(env(safe-area-inset-right, 0px) + 10px);
  gap: 8px;
}

/* Top HUD - leave right clearance for Kit's fixed #kit-gear (44x44 at top:10 right:10) */
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  min-height: 48px;
  padding-right: 52px;
  flex-shrink: 0;
}

.hud-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 68px;
}

.hud-clock-row {
  display: flex;
  align-items: center;
  gap: 5px;
}

.hud-live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 8px var(--green);
  animation: pulse-dot 1.8s ease-in-out infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.82); }
}

.hud-time {
  font-size: 16px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.03em;
}

.hud-wave {
  font-size: 11px;
  color: var(--fg-muted);
  white-space: nowrap;
}

.hud-stats {
  display: flex;
  align-items: center;
  gap: 5px;
  flex: 1;
  justify-content: center;
}

.stat-pill {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px 8px;
  min-width: 56px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.stat-label {
  font-size: 10px;
  color: var(--fg-muted);
  line-height: 1.1;
}

.stat-val {
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.25;
}

.icon-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(15, 22, 34, 0.84);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.14s ease, border-color 0.14s ease;
}

.icon-btn:active {
  transform: scale(0.95);
}

.icon-btn.is-muted {
  color: var(--fg-muted);
  border-color: rgba(239, 68, 68, 0.35);
}

/* Status / Alert Strip */
.alert-strip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
  padding: 5px 10px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.72);
  border: 1px solid var(--border);
  font-size: 12px;
  font-weight: 600;
  color: #cbd5e1;
  flex-shrink: 0;
  transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease;
}

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

.alert-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.alert-strip.mode-yellow {
  border-color: rgba(245, 158, 11, 0.45);
  color: #fde68a;
}

.alert-strip.mode-yellow .alert-dot {
  background: var(--amber);
  box-shadow: 0 0 8px var(--amber);
}

.alert-strip.mode-danger {
  background: rgba(127, 29, 29, 0.48);
  border-color: rgba(239, 68, 68, 0.75);
  color: #fecaca;
}

.alert-strip.mode-danger .alert-dot {
  background: var(--red);
  box-shadow: 0 0 10px var(--red);
  animation: pulse-dot 0.45s ease-in-out infinite;
}

.alert-strip.mode-amb {
  background: linear-gradient(90deg, rgba(185, 28, 28, 0.52), rgba(29, 78, 216, 0.52));
  border-color: rgba(248, 113, 113, 0.9);
  color: #ffffff;
  animation: amb-strip-flash 0.6s linear infinite;
}

.alert-strip.mode-amb .alert-dot {
  background: #ffffff;
  box-shadow: 0 0 10px #60a5fa;
}

@keyframes amb-strip-flash {
  0%, 100% { border-color: rgba(239, 68, 68, 0.95); }
  50% { border-color: rgba(59, 130, 246, 0.95); }
}

/* Main Stage */
.stage-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-frame {
  position: relative;
  height: 100%;
  max-width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 100%;
  width: min(100%, 456px);
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid rgba(148, 163, 184, 0.2);
  background: #080b11;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.65), inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

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

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) 16px max(34px, env(safe-area-inset-bottom, 0px));
  background: rgba(5, 8, 14, 0.78);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  overflow-y: auto;
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  width: 100%;
  max-width: 344px;
  background: var(--bg-card);
  border: 1px solid rgba(148, 163, 184, 0.24);
  border-radius: 18px;
  padding: 18px 16px 16px;
  box-shadow: 0 18px 46px rgba(0, 0, 0, 0.7);
  text-align: center;
}

.logo-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  border-radius: 16px;
  background: radial-gradient(circle, rgba(56, 189, 248, 0.16) 0%, rgba(15, 23, 42, 0.9) 75%);
  border: 1px solid rgba(56, 189, 248, 0.35);
  margin-bottom: 8px;
}

.game-title {
  margin: 0;
  font-size: 28px;
  font-weight: 900;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #f8fafc 20%, var(--accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.game-sub {
  margin: 4px 0 12px;
  font-size: 13px;
  color: var(--fg-muted);
}

.rule-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 7px;
  text-align: left;
}

.rule-list li {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 9px;
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.07);
  font-size: 12px;
  line-height: 1.35;
  color: #dbe4ee;
}

.rule-ico {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 900;
  flex-shrink: 0;
}

.ico-green {
  background: rgba(34, 197, 94, 0.2);
  color: #4ade80;
  border: 1px solid rgba(34, 197, 94, 0.45);
}

.ico-red {
  background: rgba(239, 68, 68, 0.2);
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.45);
}

.ico-amb {
  background: linear-gradient(135deg, rgba(239, 68, 68, 0.3), rgba(59, 130, 246, 0.35));
  color: #ffffff;
  font-size: 10px;
  border: 1px solid rgba(96, 165, 250, 0.6);
}

.best-line {
  margin-bottom: 10px;
  font-size: 12px;
  color: var(--fg-muted);
}

.best-line strong {
  color: var(--amber);
  font-size: 14px;
}

.primary-btn {
  width: 100%;
  min-height: 52px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--green) 0%, #0ea5e9 100%);
  color: #041019;
  font-size: 16px;
  font-weight: 900;
  letter-spacing: 0.01em;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(14, 165, 233, 0.35);
  transition: transform 0.1s ease, filter 0.14s ease;
}

.primary-btn:active {
  transform: scale(0.97);
  filter: brightness(0.94);
}

.key-hint {
  margin: 8px 0 0;
  font-size: 11px;
  color: var(--fg-muted);
}

/* End Card */
.end-badge {
  display: inline-block;
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 800;
  margin-bottom: 6px;
  background: rgba(34, 197, 94, 0.2);
  color: #86efac;
  border: 1px solid rgba(34, 197, 94, 0.5);
}

.end-badge.is-crash {
  background: rgba(239, 68, 68, 0.22);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.6);
}

.end-reason {
  margin: 0 0 12px;
  font-size: 12px;
  color: #cbd5e1;
}

.score-hero {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 10px;
}

.grade-chip {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 900;
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.25), rgba(34, 197, 94, 0.25));
  border: 1.5px solid var(--accent);
  color: #f8fafc;
}

.score-hero-meta {
  text-align: left;
}

.score-hero-label {
  font-size: 11px;
  color: var(--fg-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.new-best {
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--amber);
  color: #0b1019;
  font-size: 10px;
  font-weight: 900;
}

.score-hero-val {
  font-size: 28px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.result-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
  margin-bottom: 12px;
}

.res-cell {
  padding: 8px 10px;
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.07);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.res-cell span {
  font-size: 11px;
  color: var(--fg-muted);
}

.res-cell strong {
  font-size: 15px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* One-Tap 2x2 Signal Control Deck */
.signal-deck {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  flex-shrink: 0;
}

.sig-btn {
  position: relative;
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 58px;
  padding: 8px 11px;
  border-radius: 14px;
  border: 1.5px solid rgba(148, 163, 184, 0.22);
  background: linear-gradient(180deg, rgba(20, 28, 42, 0.92) 0%, rgba(12, 17, 27, 0.96) 100%);
  cursor: pointer;
  text-align: left;
  transition: transform 0.09s ease, border-color 0.14s ease, box-shadow 0.14s ease, background 0.14s ease;
}

.sig-btn:active {
  transform: scale(0.97);
}

.sig-led {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #475569;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.6);
  flex-shrink: 0;
  transition: background 0.14s ease, box-shadow 0.14s ease;
}

.sig-ico {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.05);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #94a3b8;
  flex-shrink: 0;
}

.sig-copy {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.sig-title {
  font-size: 14px;
  font-weight: 800;
  line-height: 1.2;
  white-space: nowrap;
}

.sig-meta {
  font-size: 11px;
  color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.sig-tag {
  position: absolute;
  top: -7px;
  right: 8px;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.03em;
  background: linear-gradient(90deg, var(--red), var(--blue-amb));
  color: #fff;
  box-shadow: 0 2px 10px rgba(239, 68, 68, 0.55);
  animation: amb-strip-flash 0.5s linear infinite;
}

.sig-btn.is-green {
  border-color: var(--green);
  background: linear-gradient(180deg, rgba(16, 48, 34, 0.94) 0%, rgba(10, 28, 22, 0.96) 100%);
  box-shadow: 0 0 18px var(--green-glow), inset 0 0 14px rgba(34, 197, 94, 0.16);
}

.sig-btn.is-green .sig-led {
  background: var(--green);
  box-shadow: 0 0 10px var(--green);
}

.sig-btn.is-green .sig-ico {
  background: rgba(34, 197, 94, 0.2);
  color: #4ade80;
}

.sig-btn.is-yellow {
  border-color: var(--amber);
  background: linear-gradient(180deg, rgba(58, 40, 12, 0.92) 0%, rgba(30, 21, 9, 0.96) 100%);
  box-shadow: 0 0 14px var(--amber-glow);
}

.sig-btn.is-yellow .sig-led {
  background: var(--amber);
  box-shadow: 0 0 10px var(--amber);
}

.sig-btn.is-yellow .sig-ico {
  background: rgba(245, 158, 11, 0.22);
  color: #fbbf24;
}

.sig-btn.is-queued {
  border-color: var(--accent);
  box-shadow: 0 0 12px rgba(56, 189, 248, 0.35);
}

.sig-btn.has-danger:not(.is-green) {
  border-color: rgba(239, 68, 68, 0.85);
  box-shadow: 0 0 14px var(--red-glow);
}

.sig-btn.has-danger:not(.is-green) .sig-meta {
  color: #fca5a5;
  font-weight: 700;
}

.sig-btn.has-amb {
  border-color: #f87171;
  box-shadow: 0 0 18px rgba(239, 68, 68, 0.48), 0 0 12px rgba(59, 130, 246, 0.4);
}

/* Narrow 320px and short screens */
@media (max-width: 360px) {
  .app-shell {
    padding-left: 7px;
    padding-right: 7px;
    gap: 6px;
  }
  .hud {
    padding-right: 48px;
  }
  .hud-time {
    font-size: 14px;
  }
  .stat-pill {
    min-width: 46px;
    padding: 3px 5px;
  }
  .stat-val {
    font-size: 12px;
  }
  .sig-btn {
    min-height: 52px;
    padding: 6px 8px;
    gap: 6px;
  }
  .sig-ico {
    width: 28px;
    height: 28px;
  }
  .sig-title {
    font-size: 12.5px;
  }
  .sig-meta {
    font-size: 10px;
  }
}

@media (max-height: 640px) {
  .app-shell {
    gap: 5px;
    padding-top: calc(env(safe-area-inset-top, 0px) + 5px);
  }
  .sig-btn {
    min-height: 48px;
    padding: 5px 9px;
  }
  .overlay-card {
    padding: 12px 12px 11px;
  }
  .logo-badge {
    width: 46px;
    height: 46px;
    margin-bottom: 4px;
  }
  .game-title {
    font-size: 22px;
  }
  .game-sub {
    margin: 2px 0 8px;
    font-size: 12px;
  }
  .rule-list {
    gap: 5px;
    margin-bottom: 8px;
  }
  .rule-list li {
    padding: 5px 7px;
    font-size: 11px;
  }
  .primary-btn {
    min-height: 46px;
    font-size: 15px;
  }
}
