:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --accent: #f59e0b;
  --hero: #e11d48;
  --exit: #22c55e;
  --asphalt: #182234;
  --cell-line: rgba(148, 163, 184, 0.14);
  --radius: 14px;
}

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

html, body {
  height: 100%;
  background: radial-gradient(circle at top, #172554 0%, var(--bg) 62%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Inter, sans-serif;
  overflow: hidden;
  user-select: none;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  padding: env(safe-area-inset-top, 10px) 14px env(safe-area-inset-bottom, 12px);
  gap: 10px;
}

/* Top Bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 6px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-badge {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, #e11d48, #be123c);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 0.95rem;
  letter-spacing: 0.5px;
  box-shadow: 0 4px 14px rgba(225, 29, 72, 0.35);
}

.brand-title {
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.brand-sub {
  font-size: 0.72rem;
  color: var(--muted);
}

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

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease;
}

.icon-btn:hover {
  background: #273549;
}

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

/* HUD */
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(17, 24, 39, 0.85);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  gap: 10px;
}

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

.tier-pill {
  align-self: flex-start;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: rgba(245, 158, 11, 0.14);
  padding: 2px 7px;
  border-radius: 999px;
}

.level-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.level-name {
  font-size: 1rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-arrow {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  width: 24px;
  height: 24px;
  border-radius: 6px;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.nav-arrow:hover {
  background: var(--surface-2);
}

.grid-meta {
  font-size: 0.72rem;
  color: var(--muted);
}

.hud-stats {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.stat-box {
  background: var(--surface-2);
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 10px;
  padding: 5px 10px;
  min-width: 52px;
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.stat-value {
  font-size: 1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* Stage & Board */
.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 4px 8px;
}

.board-frame {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
}

.board {
  position: relative;
  background: var(--asphalt);
  border: 6px solid #334155;
  border-radius: 16px;
  box-shadow:
    0 20px 50px rgba(0, 0, 0, 0.55),
    inset 0 0 0 2px rgba(255, 255, 255, 0.04);
  touch-action: none;
  overflow: visible;
}

.grid-cells {
  position: absolute;
  inset: 0;
  display: grid;
  border-radius: 10px;
  overflow: hidden;
}

.grid-cell {
  border: 1px dashed var(--cell-line);
  background: radial-gradient(circle at center, rgba(255, 255, 255, 0.015), transparent 70%);
}

.grid-cell.exit-lane {
  background: linear-gradient(90deg, rgba(225, 29, 72, 0.04), rgba(34, 197, 94, 0.07));
}

/* Exit Gate on Right Wall */
.exit-gate {
  position: absolute;
  right: -12px;
  width: 18px;
  background: linear-gradient(90deg, var(--asphalt) 0%, rgba(34, 197, 94, 0.35) 100%);
  border-top: 3px solid var(--exit);
  border-bottom: 3px solid var(--exit);
  border-radius: 0 6px 6px 0;
  display: grid;
  place-items: center;
  z-index: 3;
  box-shadow: 0 0 18px rgba(34, 197, 94, 0.45);
}

.exit-label {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-size: 0.62rem;
  font-weight: 900;
  letter-spacing: 0.14em;
  color: #4ade80;
}

/* Vehicles Layer */
.vehicles-layer {
  position: absolute;
  inset: 0;
  z-index: 5;
}

.vehicle {
  position: absolute;
  padding: 3px;
  cursor: grab;
  transition: left 0.16s cubic-bezier(0.22, 1, 0.36, 1), top 0.16s cubic-bezier(0.22, 1, 0.36, 1);
  will-change: left, top;
  z-index: 5;
}

.vehicle.dragging {
  cursor: grabbing;
  transition: none;
  z-index: 20;
  filter: drop-shadow(0 10px 16px rgba(0, 0, 0, 0.55));
  transform: scale(1.02);
}

.vehicle.selected {
  filter: drop-shadow(0 0 8px rgba(248, 250, 252, 0.45));
}

.vehicle.hint-active {
  animation: hintPulse 1s ease-in-out infinite;
  z-index: 15;
}

.vehicle.escaped {
  transition: left 0.55s cubic-bezier(0.3, 0, 0.2, 1);
  z-index: 25;
}

@keyframes hintPulse {
  0%, 100% { filter: drop-shadow(0 0 4px rgba(245, 158, 11, 0.5)); }
  50% { filter: drop-shadow(0 0 16px rgba(245, 158, 11, 1)); }
}

.hint-ghost {
  position: absolute;
  border: 2.5px dashed var(--accent);
  background: rgba(245, 158, 11, 0.16);
  border-radius: 10px;
  z-index: 4;
  pointer-events: none;
  animation: ghostBlink 1.1s ease-in-out infinite;
}

@keyframes ghostBlink {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 0.95; }
}

/* Bottom Controls */
.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  padding-bottom: 4px;
}

.ctrl-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, opacity 0.15s ease;
}

.ctrl-btn:hover:not(:disabled) {
  background: #273549;
}

.ctrl-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.ctrl-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ctrl-btn-accent {
  background: rgba(245, 158, 11, 0.16);
  border-color: rgba(245, 158, 11, 0.45);
  color: #fbbf24;
}

.ctrl-btn-accent:hover:not(:disabled) {
  background: rgba(245, 158, 11, 0.25);
}

.ctrl-btn-primary {
  background: linear-gradient(135deg, #22c55e, #16a34a);
  border-color: #22c55e;
  color: #ffffff;
}

.ctrl-btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, #4ade80, #16a34a);
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 50;
}

.hidden {
  display: none !important;
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  width: 100%;
  max-width: 440px;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  padding: 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
}

.modal-wide {
  max-width: 560px;
}

.modal-center {
  text-align: center;
  align-items: center;
  gap: 12px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modal-header h2 {
  font-size: 1.2rem;
  font-weight: 800;
}

.modal-close {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}

.levels-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-right: 4px;
}

.tier-group-title {
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--accent);
  margin-bottom: 8px;
}

.tier-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
}

.level-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  text-align: left;
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: border-color 0.15s ease, transform 0.1s ease;
}

.level-card:hover {
  border-color: var(--accent);
}

.level-card.active {
  border-color: var(--hero);
  box-shadow: 0 0 0 1px var(--hero);
}

.level-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.72rem;
  color: var(--muted);
}

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

.level-card-name {
  font-size: 0.9rem;
  font-weight: 700;
}

.win-stars {
  font-size: 2.4rem;
  color: #fbbf24;
  letter-spacing: 4px;
}

.win-subtitle {
  color: var(--muted);
  font-size: 0.95rem;
}

.modal-actions {
  display: flex;
  gap: 10px;
  width: 100%;
  margin-top: 8px;
}

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

.help-body {
  overflow-y: auto;
  min-height: 0;
  padding-right: 4px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 0.92rem;
  line-height: 1.5;
  color: #cbd5e1;
}

.help-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.help-list li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.legend-art {
  width: 84px;
  height: 28px;
  flex-shrink: 0;
  display: flex;
  justify-content: flex-start;
}

.legend-art > span {
  display: block;
  height: 28px;
}

.help-note {
  font-size: 0.84rem;
  color: var(--muted);
}

.help-tip {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.84rem;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.key-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.help-small {
  color: var(--muted);
  font-size: 0.78rem;
}

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface);
  color: var(--text);
  font: 600 0.74rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
}

/* Keyboard focus */
.vehicle:focus {
  outline: none;
}

.vehicle:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 10px;
}

button:focus-visible,
.level-card:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.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;
}

@media (max-width: 480px) {
  .btn-label {
    display: none;
  }
  .hud {
    padding: 8px 10px;
  }
  .stat-box {
    min-width: 44px;
    padding: 4px 7px;
  }
  .ctrl-btn {
    padding: 11px 8px;
    font-size: 0.85rem;
  }
}
