:root {
  --bg-deep: #090d1b;
  --bg-panel: #11182c;
  --bg-card: #131c33;
  --border-soft: rgba(148, 163, 184, 0.22);
  --border-glow: rgba(56, 189, 248, 0.5);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent-cyan: #38bdf8;
  --accent-emerald: #34d399;
  --accent-gold: #fbbf24;
  --accent-rose: #fb7185;
  --accent-violet: #c084fc;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100vw;
  min-height: 100dvh;
  overflow-x: hidden;
  background: radial-gradient(circle at 50% 0%, #1a2244 0%, #090d1b 70%);
  color: var(--text-main);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.hidden {
  display: none !important;
}

.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: 8px 12px env(safe-area-inset-bottom, 6px);
  gap: 8px;
}

/* ===== HUD BAR ===== */
.hud-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
  padding: 7px 11px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.36);
  flex-wrap: nowrap;
  min-width: 0;
}

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

.brand-badge {
  background: linear-gradient(135deg, #0284c7, #6366f1);
  color: #fff;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.08em;
  padding: 3px 7px;
  border-radius: 6px;
  flex-shrink: 0;
}

.brand-badge.daily-mode {
  background: linear-gradient(135deg, #d97706, #ef4444);
}

.brand-badge.speedrun-mode {
  background: linear-gradient(135deg, #059669, #0284c7);
}

.room-info {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.room-title {
  font-size: 13.5px;
  font-weight: 800;
  color: #f8fafc;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-sub {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.timer-pill {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  background: rgba(2, 6, 23, 0.85);
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 8px;
  padding: 4px 9px;
  font-variant-numeric: tabular-nums;
}

.timer-label {
  font-size: 9.5px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--accent-cyan);
}

.timer-val {
  font-size: 15px;
  font-weight: 900;
  color: #ffffff;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.timer-delta {
  font-size: 11px;
  font-weight: 800;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.timer-delta.ahead { color: var(--accent-emerald); }
.timer-delta.behind { color: var(--accent-rose); }

.stat-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(30, 41, 59, 0.82);
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  padding: 4px 7px;
  font-size: 12px;
  font-weight: 800;
  color: #e2e8f0;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

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

.hud-btn {
  background: rgba(30, 41, 59, 0.9);
  color: #f1f5f9;
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  transition: background-color 0.14s ease, border-color 0.14s ease, transform 0.1s ease;
  white-space: nowrap;
}

.hud-btn:hover {
  background: #334155;
  border-color: var(--accent-cyan);
}

.hud-btn:active {
  transform: scale(0.96);
}

.hud-btn-daily {
  border-color: rgba(251, 191, 36, 0.45);
  background: rgba(120, 53, 15, 0.35);
  color: #fde68a;
}

/* ===== CHALLENGE BANNER ===== */
.challenge-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 12px;
  border-radius: 10px;
  background: linear-gradient(90deg, rgba(124, 58, 237, 0.35), rgba(14, 165, 233, 0.25));
  border: 1px solid rgba(192, 132, 252, 0.55);
  font-size: 12.5px;
  font-weight: 700;
  color: #f3e8ff;
}

.chip-close {
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #f8fafc;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 800;
}

/* ===== MAIN STAGE ===== */
.stage-wrap {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 0;
}

.canvas-frame {
  position: relative;
  width: 100%;
  max-width: 1024px;
  aspect-ratio: 16 / 9;
  max-height: calc(100dvh - 120px);
  background: #060913;
  border-radius: 14px;
  border: 1.5px solid rgba(148, 163, 184, 0.28);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6), inset 0 0 0 1px rgba(255, 255, 255, 0.04);
  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;
  cursor: crosshair;
  touch-action: none;
}

.room-hint-bar {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(8, 12, 26, 0.74);
  border: 1px solid rgba(148, 163, 184, 0.22);
  font-size: 11.5px;
  color: #cbd5e1;
  pointer-events: none;
  backdrop-filter: blur(4px);
}

.par-strip {
  color: #fde68a;
  font-weight: 800;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.daily-hud-strip {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(120, 53, 15, 0.86);
  border: 1px solid rgba(251, 191, 36, 0.6);
  color: #fef3c7;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.04em;
  pointer-events: none;
}

/* ===== MOBILE TOUCH CONTROLS ===== */
.touch-controls {
  display: none;
  width: 100%;
  max-width: 560px;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 4px 2px 6px;
  touch-action: none;
}

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

.tbtn {
  width: 68px;
  height: 62px;
  border-radius: 14px;
  border: 1.5px solid rgba(148, 163, 184, 0.35);
  background: rgba(15, 23, 42, 0.92);
  color: #f8fafc;
  font-size: 22px;
  font-weight: 900;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  box-shadow: 0 5px 16px rgba(0, 0, 0, 0.42);
  touch-action: none;
}

.tbtn span {
  font-size: 9.5px;
  letter-spacing: 0.06em;
  font-weight: 800;
  opacity: 0.9;
}

.tbtn.active, .tbtn:active {
  transform: scale(0.94);
  filter: brightness(1.25);
}

.tbtn-grapple {
  width: 78px;
  background: linear-gradient(145deg, rgba(14, 116, 144, 0.95), rgba(30, 58, 138, 0.95));
  border-color: rgba(56, 189, 248, 0.72);
}

.tbtn-jump {
  width: 78px;
  background: linear-gradient(145deg, rgba(5, 150, 105, 0.95), rgba(217, 119, 6, 0.92));
  border-color: rgba(251, 191, 36, 0.72);
}

/* ===== FOOTER BAR (studio-bar.js docks here) ===== */
.footer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px 12px;
  padding: 6px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.82);
  border: 1px solid rgba(148, 163, 184, 0.18);
  font-size: 11.5px;
  color: var(--text-muted);
}

.footer-keys strong {
  color: #e2e8f0;
}

.footer-meta {
  color: #fde68a;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ===== MODALS (ALWAYS POSITION FIXED) ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(4, 7, 16, 0.78);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  overflow-y: auto;
}

.card {
  width: 100%;
  max-width: 480px;
  max-height: min(90dvh, 720px);
  overflow-y: auto;
  background: linear-gradient(180deg, #16213d 0%, #0d1427 100%);
  border: 1.5px solid rgba(148, 163, 184, 0.3);
  border-radius: 16px;
  padding: 18px 18px 16px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.68);
  display: flex;
  flex-direction: column;
  gap: 11px;
  text-align: center;
}

.wide-card {
  max-width: 680px;
  text-align: left;
}

.lb-card {
  max-width: 520px;
  text-align: left;
}

.ov-kicker {
  font-size: 10.5px;
  font-weight: 900;
  letter-spacing: 0.14em;
  color: var(--accent-cyan);
  text-transform: uppercase;
}

.ov-title {
  margin: 0;
  font-size: 28px;
  font-weight: 900;
  letter-spacing: 0.03em;
  background: linear-gradient(135deg, #ffffff 0%, #7dd3fc 52%, #fbbf24 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.ov-subtitle {
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
  color: #cbd5e1;
}

.ov-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  background: rgba(6, 10, 22, 0.65);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-radius: 12px;
  padding: 10px;
}

.ov-stat-box {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.ov-stat-lbl {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  text-transform: uppercase;
}

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

.ov-stat-val.gold { color: var(--accent-gold); }
.ov-stat-val.emerald { color: var(--accent-emerald); }
.ov-stat-val.cyan { color: var(--accent-cyan); }

.next-unlock-pill {
  background: rgba(124, 58, 237, 0.18);
  border: 1px solid rgba(192, 132, 252, 0.42);
  color: #e9d5ff;
  font-size: 12px;
  font-weight: 700;
  padding: 7px 10px;
  border-radius: 10px;
}

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

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

.lb-submit-row input:focus {
  border-color: var(--accent-gold);
}

.lb-submit-status {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-emerald);
}

.ov-btn-row, .ov-sub-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13.5px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.14s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

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

.btn-primary {
  background: linear-gradient(135deg, #0ea5e9, #6366f1);
  color: #ffffff;
  flex: 1 1 150px;
  box-shadow: 0 6px 20px rgba(14, 165, 233, 0.32);
}

.btn-secondary {
  background: linear-gradient(135deg, #d97706, #ea580c);
  color: #ffffff;
  flex: 1 1 150px;
}

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

.btn-accent {
  background: linear-gradient(135deg, #10b981, #06b6d4);
  color: #06131f;
}

.btn-ghost {
  background: rgba(30, 41, 59, 0.85);
  color: #e2e8f0;
  border: 1px solid rgba(148, 163, 184, 0.28);
  padding: 8px 12px;
  font-size: 12.5px;
}

.btn-sm {
  padding: 7px 12px;
  font-size: 12px;
}

.share-preview {
  margin: 0;
  padding: 10px;
  border-radius: 10px;
  background: rgba(2, 6, 23, 0.86);
  border: 1px solid rgba(56, 189, 248, 0.35);
  color: #e2e8f0;
  font-size: 11.5px;
  line-height: 1.45;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* ===== MODAL HEADERS & LISTS ===== */
.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.18);
  padding-bottom: 10px;
}

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

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

.close-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.28);
  background: rgba(30, 41, 59, 0.85);
  color: #e2e8f0;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  flex-shrink: 0;
}

.spire-run-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid rgba(251, 191, 36, 0.35);
  font-size: 12.5px;
}

.spire-run-bar .muted {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.chapters-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chapter-group {
  background: rgba(15, 23, 42, 0.65);
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 12px;
  padding: 10px;
}

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

.chapter-title {
  font-size: 13px;
  font-weight: 800;
  color: #f8fafc;
}

.chapter-badges {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 800;
}

.crown-badge {
  background: rgba(251, 191, 36, 0.2);
  border: 1px solid rgba(251, 191, 36, 0.55);
  color: #fde68a;
  padding: 2px 7px;
  border-radius: 999px;
}

.rooms-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
}

.room-tile {
  background: rgba(30, 41, 59, 0.85);
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 9px;
  padding: 7px 6px;
  color: #f8fafc;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: transform 0.12s ease, border-color 0.14s ease;
}

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

.room-tile.current {
  border-color: var(--accent-gold);
  background: rgba(251, 191, 36, 0.16);
}

.room-tile-num {
  font-size: 11px;
  font-weight: 900;
}

.room-tile-name {
  font-size: 9.5px;
  color: #cbd5e1;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.room-tile-icons {
  font-size: 11px;
  letter-spacing: 1px;
}

.room-tile-time {
  font-size: 10px;
  color: var(--accent-cyan);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* ===== CLOAKS GRID ===== */
.cloaks-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(185px, 1fr));
  gap: 9px;
}

.cloak-card {
  background: rgba(15, 23, 42, 0.8);
  border: 1.5px solid rgba(148, 163, 184, 0.24);
  border-radius: 12px;
  padding: 11px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cloak-card.equipped {
  border-color: var(--accent-emerald);
  background: rgba(16, 185, 129, 0.12);
}

.cloak-card.locked {
  opacity: 0.72;
}

.cloak-swatch {
  height: 22px;
  border-radius: 7px;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

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

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

/* ===== DAILY LEADERBOARD ===== */
.daily-banner-box {
  background: rgba(15, 23, 42, 0.8);
  border: 1px solid rgba(251, 191, 36, 0.35);
  border-radius: 12px;
  padding: 11px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.daily-meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
}

.streak-pill {
  background: rgba(234, 88, 12, 0.22);
  border: 1px solid rgba(251, 146, 60, 0.5);
  color: #fed7aa;
  font-size: 11.5px;
  font-weight: 800;
  padding: 3px 9px;
  border-radius: 999px;
}

.daily-rooms-pill {
  font-size: 11.5px;
  color: #cbd5e1;
  font-weight: 700;
}

.lb-table-wrap {
  background: rgba(6, 10, 22, 0.72);
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 10px;
  overflow: hidden;
}

.lb-table-head, .lb-row {
  display: grid;
  grid-template-columns: 44px 1fr 82px 58px;
  align-items: center;
  padding: 7px 10px;
  font-size: 12px;
}

.lb-table-head {
  background: rgba(30, 41, 59, 0.75);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.lb-entries {
  max-height: 220px;
  overflow-y: auto;
}

.lb-row {
  border-top: 1px solid rgba(148, 163, 184, 0.12);
  font-variant-numeric: tabular-nums;
}

.lb-row:nth-child(1) { color: #fde68a; font-weight: 800; }
.lb-row:nth-child(2) { color: #e2e8f0; font-weight: 700; }
.lb-row:nth-child(3) { color: #fdba74; font-weight: 700; }

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

/* ===== RESPONSIVE MOBILE (390x844 & COARSE POINTER) ===== */
@media (max-width: 860px), (pointer: coarse) {
  .touch-controls {
    display: flex;
  }
  .footer-keys {
    display: none;
  }
}

@media (max-width: 680px) {
  .app-shell {
    padding: 6px 6px max(6px, env(safe-area-inset-bottom));
    gap: 6px;
  }
  .hud-bar {
    flex-wrap: wrap;
    padding: 6px 8px;
    gap: 5px;
  }
  .hud-left {
    flex: 1 1 55%;
  }
  .hud-actions {
    gap: 4px;
  }
  .btn-lbl {
    display: none;
  }
  .hud-btn {
    padding: 5px 7px;
    font-size: 13px;
  }
  .hud-center {
    width: 100%;
    justify-content: space-between;
  }
  .hide-xs {
    display: none;
  }
  .room-hint-bar {
    font-size: 10px;
    padding: 3px 7px;
    bottom: 4px;
    left: 4px;
    right: 4px;
  }
  .rooms-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .ov-title {
    font-size: 24px;
  }
  .tbtn {
    width: 64px;
    height: 58px;
  }
  .tbtn-grapple, .tbtn-jump {
    width: 76px;
  }
}
