:root {
  --bg: #070b16;
  --panel: #0f172a;
  --panel-2: #162138;
  --border: rgba(148, 163, 184, 0.24);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-glow: rgba(56, 189, 248, 0.35);
  --emerald: #34d399;
  --amber: #fbbf24;
  --rose: #fb7185;
  --violet: #c084fc;
}

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

html, body {
  width: 100%;
  height: 100%;
  max-width: 100vw;
  overflow: hidden;
  background: radial-gradient(circle at 50% 18%, #101b33 0%, var(--bg) 72%);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.hidden {
  display: none !important;
}

.app-shell {
  width: 100%;
  height: 100dvh;
  max-width: 100vw;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Top HUD */
.hud-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 12px;
  background: rgba(10, 16, 30, 0.94);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-width: 0;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1 1 auto;
}

.brand-tag {
  background: linear-gradient(135deg, #0284c7, #6366f1);
  color: #fff;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.08em;
  padding: 4px 7px;
  border-radius: 6px;
  flex-shrink: 0;
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.35);
}

.room-meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.room-title {
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.03em;
  color: #f8fafc;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-sub {
  font-size: 10.5px;
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-stats {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.stat-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(30, 41, 59, 0.85);
  border: 1px solid rgba(148, 163, 184, 0.22);
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #e2e8f0;
  white-space: nowrap;
}

.stat-core {
  border-color: rgba(251, 191, 36, 0.42);
  color: #fde68a;
}

.stat-streak {
  border-color: rgba(251, 146, 60, 0.45);
  color: #fdba74;
}

.stat-ico {
  font-size: 12px;
  line-height: 1;
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}

.hud-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.28);
  background: rgba(30, 41, 59, 0.88);
  color: #f8fafc;
  font-size: 14px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}

.hud-btn:hover {
  border-color: var(--accent);
  background: #1e293b;
  transform: translateY(-1px);
}

.hud-btn:active {
  transform: translateY(0);
}

/* Main Stage */
.stage-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 12px 6px;
  gap: 6px;
  overflow: hidden;
}

.canvas-frame {
  position: relative;
  width: 100%;
  max-width: min(960px, calc((100dvh - 130px) * 1.6));
  aspect-ratio: 16 / 10;
  max-height: 100%;
  border-radius: 12px;
  border: 2px solid var(--accent);
  box-shadow: 0 0 28px var(--accent-glow), 0 12px 32px rgba(0, 0, 0, 0.65);
  background: #060a14;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.challenge-pill {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid rgba(251, 191, 36, 0.6);
  color: #fde68a;
  font-size: 11px;
  font-weight: 800;
  padding: 4px 10px;
  border-radius: 999px;
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
}

.hint-strip {
  width: 100%;
  max-width: 960px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 12px;
  color: #cbd5e1;
  min-height: 22px;
  padding: 0 6px;
  text-align: center;
  flex-shrink: 0;
}

.hint-badge {
  background: rgba(56, 189, 248, 0.16);
  color: var(--accent);
  border: 1px solid rgba(56, 189, 248, 0.4);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.06em;
  padding: 2px 6px;
  border-radius: 5px;
  flex-shrink: 0;
}

/* Mobile Touch Controls */
.touch-controls {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px 10px;
  background: rgba(9, 14, 26, 0.94);
  border-top: 1px solid rgba(148, 163, 184, 0.16);
  flex-shrink: 0;
  width: 100%;
}

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

.touch-btn {
  border-radius: 14px;
  border: 2px solid rgba(148, 163, 184, 0.35);
  background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
  color: #f8fafc;
  font-weight: 900;
  cursor: pointer;
  touch-action: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  transition: transform 0.06s ease, border-color 0.08s ease, background 0.08s ease;
}

.touch-btn.pressed,
.touch-btn:active {
  transform: scale(0.95);
  border-color: var(--accent);
  background: linear-gradient(180deg, #0369a1 0%, #0c4a6e 100%);
}

.touch-dir {
  width: 68px;
  height: 58px;
  font-size: 24px;
}

.touch-flip {
  min-width: 128px;
  height: 58px;
  padding: 0 18px;
  gap: 8px;
  font-size: 17px;
  letter-spacing: 0.06em;
  border-color: rgba(56, 189, 248, 0.65);
  background: linear-gradient(180deg, #0284c7 0%, #0369a1 100%);
  box-shadow: 0 0 18px rgba(56, 189, 248, 0.32);
}

.flip-arrows {
  font-size: 20px;
}

/* Footer Bar (studio-bar.js docks inline here) */
.footer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 12px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  background: rgba(6, 10, 20, 0.96);
  border-top: 1px solid var(--border);
  font-size: 11.5px;
  color: var(--muted);
  flex-shrink: 0;
  min-height: 36px;
}

.footer-spacer {
  flex: 1;
}

/* Modals (position: fixed to avoid clipping) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 500;
  background: rgba(4, 8, 18, 0.78);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
}

.card {
  width: 100%;
  max-width: 480px;
  max-height: 90dvh;
  overflow-y: auto;
  background: linear-gradient(180deg, #111c33 0%, #0b1222 100%);
  border: 1px solid rgba(56, 189, 248, 0.38);
  border-radius: 16px;
  padding: 18px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.7), 0 0 32px rgba(56, 189, 248, 0.16);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

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

.modal-head h2 {
  font-size: 18px;
  font-weight: 900;
  color: #f8fafc;
}

.modal-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.icon-close {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: rgba(30, 41, 59, 0.85);
  color: #cbd5e1;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-close:hover {
  background: #334155;
  color: #fff;
}

.ov-kicker {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--accent);
  text-transform: uppercase;
  text-align: center;
}

.ov-title {
  font-size: 28px;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-align: center;
  background: linear-gradient(90deg, #38bdf8, #a78bfa, #34d399);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  line-height: 1.1;
}

.ov-sub {
  font-size: 13px;
  color: #cbd5e1;
  text-align: center;
  line-height: 1.4;
}

.ov-challenge-box {
  background: rgba(251, 191, 36, 0.12);
  border: 1px solid rgba(251, 191, 36, 0.45);
  color: #fde68a;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.ov-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.ov-stat-box {
  background: rgba(30, 41, 59, 0.72);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.ov-stat-label {
  font-size: 10.5px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 700;
}

.ov-stat-val {
  font-size: 16px;
  font-weight: 900;
  color: #f8fafc;
}

.ov-unlocks {
  background: rgba(52, 211, 153, 0.14);
  border: 1px solid rgba(52, 211, 153, 0.45);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12.5px;
  font-weight: 700;
  color: #a7f3d0;
  text-align: center;
}

.ov-cliffhanger {
  background: rgba(99, 102, 241, 0.14);
  border: 1px solid rgba(129, 140, 248, 0.4);
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 12px;
  color: #c7d2fe;
  text-align: center;
}

.ov-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.btn {
  font-family: inherit;
  border: none;
  border-radius: 10px;
  padding: 10px 15px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.03em;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.btn:hover {
  transform: translateY(-1px);
  filter: brightness(1.08);
}

.btn:active {
  transform: translateY(0);
}

.btn-primary {
  background: linear-gradient(135deg, #0284c7, #2563eb);
  color: #fff;
  box-shadow: 0 4px 16px rgba(37, 99, 235, 0.4);
}

.btn-emerald {
  background: linear-gradient(135deg, #059669, #10b981);
  color: #fff;
  box-shadow: 0 4px 14px rgba(16, 185, 129, 0.34);
}

.btn-secondary {
  background: linear-gradient(135deg, #7c3aed, #db2777);
  color: #fff;
}

.btn-amber {
  background: linear-gradient(135deg, #d97706, #f59e0b);
  color: #0f172a;
}

.btn-ghost {
  background: rgba(30, 41, 59, 0.85);
  color: #e2e8f0;
  border: 1px solid var(--border);
}

.ov-progress-strip {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 11.5px;
  color: var(--muted);
  padding-top: 4px;
  border-top: 1px solid rgba(148, 163, 184, 0.14);
}

.share-preview {
  background: rgba(2, 6, 23, 0.85);
  border: 1px solid rgba(56, 189, 248, 0.32);
  border-radius: 10px;
  padding: 9px 11px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 11px;
  color: #e2e8f0;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.4;
}

/* Sector Select */
.sectors-scroll {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 64dvh;
  overflow-y: auto;
  padding-right: 2px;
}

.sector-block {
  background: rgba(15, 23, 42, 0.82);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
}

.sector-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.sector-block-title {
  font-size: 13px;
  font-weight: 800;
}

.sector-badge {
  font-size: 10.5px;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(52, 211, 153, 0.15);
  color: #6ee7b7;
  border: 1px solid rgba(52, 211, 153, 0.4);
}

.sector-rooms-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
}

.room-tile-btn {
  text-align: left;
  background: rgba(30, 41, 59, 0.8);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-radius: 9px;
  padding: 8px;
  color: #f1f5f9;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: transform 0.1s ease, border-color 0.1s ease;
}

.room-tile-btn:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.room-tile-btn.cleared {
  border-color: rgba(52, 211, 153, 0.45);
  background: rgba(16, 42, 46, 0.72);
}

.room-tile-btn.active-room {
  border-color: var(--amber);
  box-shadow: 0 0 12px rgba(251, 191, 36, 0.25);
}

.room-tile-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 800;
}

.room-tile-name {
  font-size: 10.5px;
  color: #cbd5e1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-tile-time {
  font-size: 10px;
  color: var(--muted);
}

/* Daily Leaderboard */
.daily-brief {
  background: rgba(16, 185, 129, 0.11);
  border: 1px solid rgba(52, 211, 153, 0.35);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 12px;
  color: #d1fae5;
  line-height: 1.45;
}

.lb-submit-row {
  display: flex;
  gap: 8px;
}

.lb-submit-row input {
  flex: 1;
  min-width: 0;
  background: #0b1324;
  border: 1px solid rgba(148, 163, 184, 0.35);
  border-radius: 9px;
  padding: 8px 10px;
  color: #f8fafc;
  font-size: 13px;
  font-weight: 700;
}

.lb-submit-status {
  font-size: 11.5px;
  text-align: center;
  color: #6ee7b7;
  font-weight: 700;
}

.lb-section-title {
  font-size: 12px;
  font-weight: 800;
  color: #fde68a;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.lb-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-height: 220px;
  overflow-y: auto;
}

.lb-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 8px;
  background: rgba(30, 41, 59, 0.72);
  border: 1px solid rgba(148, 163, 184, 0.16);
  font-size: 12px;
}

.lb-rank {
  width: 24px;
  font-weight: 900;
  color: var(--amber);
}

.lb-name {
  flex: 1;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lb-score {
  font-weight: 900;
  color: #38bdf8;
  font-variant-numeric: tabular-nums;
}

.lb-empty {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  padding: 14px 8px;
}

/* Suit Palettes */
.suits-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.suit-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  border-radius: 10px;
  background: rgba(30, 41, 59, 0.72);
  border: 1px solid var(--border);
}

.suit-row.equipped {
  border-color: var(--accent);
  background: rgba(14, 48, 78, 0.72);
}

.suit-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.suit-swatch {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.45);
  flex-shrink: 0;
}

.suit-name {
  font-size: 13px;
  font-weight: 800;
}

.suit-req {
  font-size: 11px;
  color: var(--muted);
}

/* Responsive Mobile (390×844 & touch screens) */
@media (max-width: 900px), (pointer: coarse) {
  .touch-controls {
    display: flex;
  }
  .controls-legend {
    display: none;
  }
}

@media (max-width: 640px) {
  .hud-bar {
    flex-wrap: wrap;
    padding: 6px 8px;
    gap: 5px;
  }
  .hud-left {
    width: 100%;
    order: 1;
  }
  .hud-stats {
    order: 2;
    gap: 4px;
  }
  .hud-actions {
    order: 3;
    gap: 4px;
  }
  .stat-pill {
    padding: 3px 6px;
    font-size: 11px;
  }
  .hud-btn {
    width: 30px;
    height: 30px;
    font-size: 13px;
  }
  .stage-wrap {
    padding: 6px 6px 4px;
    gap: 4px;
  }
  .canvas-frame {
    border-radius: 8px;
    max-width: 100%;
  }
  .hint-strip {
    font-size: 11px;
    min-height: 18px;
  }
  .touch-controls {
    padding: 6px 10px 8px;
  }
  .touch-dir {
    width: 64px;
    height: 54px;
    font-size: 22px;
  }
  .touch-flip {
    min-width: 118px;
    height: 54px;
    font-size: 16px;
  }
  .sector-rooms-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .ov-title {
    font-size: 23px;
  }
  .card {
    padding: 14px;
  }
}
