:root {
  --bg-top: #e8f6df;
  --bg-bottom: #cdeac0;
  --card-bg: rgba(255, 255, 255, 0.93);
  --card-border: rgba(58, 97, 50, 0.16);
  --ink: #243623;
  --ink-soft: #566e54;
  --accent: #f59e0b;
  --accent-dark: #d97706;
  --green: #22c55e;
  --green-dark: #16a34a;
  --orange: #f97316;
  --shadow-sm: 0 4px 12px rgba(32, 56, 28, 0.08);
  --shadow-lg: 0 20px 48px rgba(24, 42, 22, 0.28);
  --radius: 16px;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Nunito", "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg-top), var(--bg-bottom));
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

body.mode-battle {
  --card-bg: rgba(245, 250, 255, 0.94);
  --card-border: rgba(14, 116, 144, 0.22);
}

body.mode-lantern {
  --card-bg: rgba(248, 246, 255, 0.94);
  --card-border: rgba(124, 58, 237, 0.24);
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  padding: 10px 14px 8px;
  gap: 10px;
}

/* HUD */
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 10px;
  background: var(--card-bg);
  border: 2px solid var(--card-border);
  border-radius: var(--radius);
  padding: 7px 12px;
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

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

.brand-icon {
  font-size: 26px;
  line-height: 1;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.14));
}

.brand-text h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.level-subtitle {
  font-size: 11.5px;
  font-weight: 700;
  color: var(--ink-soft);
}

/* Mode Selector */
.mode-group {
  display: inline-flex;
  background: #e6f0f5;
  padding: 3px;
  border-radius: 12px;
  gap: 2px;
  border: 1.5px solid rgba(14, 116, 144, 0.18);
}

.mode-btn {
  border: none;
  background: transparent;
  color: #475569;
  font-family: inherit;
  font-weight: 800;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 9px;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.mode-btn:hover {
  color: #0f172a;
  background: rgba(255, 255, 255, 0.65);
}

.mode-btn.active[data-mode="classic"] {
  background: #ffffff;
  color: #15803d;
  box-shadow: 0 2px 6px rgba(21, 128, 61, 0.18);
}

.mode-btn.active[data-mode="lantern"] {
  background: linear-gradient(180deg, #312e81, #1e1b4b);
  color: #fde047;
  box-shadow: 0 2px 8px rgba(49, 46, 129, 0.4);
}

.mode-btn.active[data-mode="battle"] {
  background: linear-gradient(180deg, #0ea5e9, #0284c7);
  color: #ffffff;
  box-shadow: 0 2px 8px rgba(2, 132, 199, 0.34);
}

.difficulty-group {
  display: inline-flex;
  background: #edf4e8;
  padding: 3px;
  border-radius: 12px;
  gap: 2px;
  border: 1px solid rgba(58, 97, 50, 0.12);
}

.diff-btn {
  border: none;
  background: transparent;
  color: var(--ink-soft);
  font-family: inherit;
  font-weight: 700;
  font-size: 12px;
  padding: 5px 9px;
  border-radius: 9px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.diff-btn:hover {
  color: var(--ink);
  background: rgba(255, 255, 255, 0.65);
}

.diff-btn.active {
  background: #ffffff;
  color: #15803d;
  box-shadow: 0 2px 6px rgba(21, 128, 61, 0.16);
}

/* Stats */
.hud-stats {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.stat-pill {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 68px;
  padding: 4px 9px;
  border-radius: 12px;
  background: #f5f9f2;
  border: 1px solid rgba(58, 97, 50, 0.12);
}

.stat-timer {
  min-width: 102px;
  background: #eff6ff;
  border-color: rgba(37, 99, 235, 0.2);
  padding: 4px 9px 5px;
}

.stat-timer.strict {
  background: #fef3c7;
  border-color: rgba(217, 119, 6, 0.35);
}

.stat-timer.urgent {
  background: #fee2e2;
  border-color: rgba(220, 38, 38, 0.55);
  animation: timerPulse 0.65s ease-in-out infinite alternate;
}

.timer-bar-track,
.overdrive-bar-track,
.sonar-bar-track {
  width: 100%;
  height: 5px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-top: 2px;
}

.stat-lantern {
  min-width: 118px;
  background: linear-gradient(180deg, #eef2ff, #e0e7ff);
  border-color: rgba(79, 70, 229, 0.32);
  padding: 3px 9px 4px;
}

.stat-lantern.ready {
  background: linear-gradient(180deg, #fef9c3, #fef08a);
  border-color: rgba(202, 138, 4, 0.5);
}

.sonar-bar-fill {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #6366f1, #f59e0b, #fde047);
  transition: width 0.12s linear;
}

.lantern-sub {
  font-size: 9px;
  font-weight: 800;
  color: #4338ca;
  margin-top: 1px;
  white-space: nowrap;
}

.timer-bar-fill {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #22c55e, #4ade80);
  transition: width 0.2s linear, background 0.3s ease;
}

.timer-bar-fill.warn {
  background: linear-gradient(90deg, #f59e0b, #fbbf24);
}

.timer-bar-fill.danger {
  background: linear-gradient(90deg, #dc2626, #f97316);
}

/* Battle HUD Pills */
.stat-shields {
  background: #fff1f2;
  border-color: rgba(225, 29, 72, 0.24);
  min-width: 82px;
}

.stat-shields.hurt {
  animation: popShake 0.38s ease;
}

.shields-val {
  font-size: 13.5px;
  letter-spacing: 1px;
}

.stat-weapon {
  min-width: 126px;
  background: linear-gradient(180deg, #ecfeff, #cffafe);
  border-color: rgba(8, 145, 178, 0.32);
  padding: 3px 9px 4px;
}

.stat-weapon.tier-max {
  background: linear-gradient(180deg, #fef9c3, #fde047);
  border-color: rgba(202, 138, 4, 0.55);
}

.overdrive-bar-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #06b6d4, #3b82f6, #a855f7);
  transition: width 0.14s ease-out;
}

.weapon-sub {
  font-size: 9px;
  font-weight: 800;
  color: #0e7490;
  margin-top: 1px;
  white-space: nowrap;
}

.stat-cones {
  background: #fff7ed;
  border-color: rgba(249, 115, 22, 0.25);
}

.stat-cones.bump {
  animation: popShake 0.35s ease;
}

.stat-label {
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  white-space: nowrap;
}

.stat-value {
  font-size: 15px;
  font-weight: 800;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.star-preview {
  font-size: 14.5px;
  line-height: 1.25;
  letter-spacing: 1px;
}

/* Action Buttons */
.hud-actions {
  display: flex;
  align-items: center;
  gap: 5px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border: 2px solid rgba(58, 97, 50, 0.15);
  background: #ffffff;
  color: var(--ink);
  font-family: inherit;
  font-weight: 700;
  font-size: 12px;
  padding: 5px 8px;
  border-radius: 11px;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease;
}

.icon-btn:hover {
  background: #f6fbf3;
  border-color: rgba(58, 97, 50, 0.32);
}

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

.icon-btn.active-mode {
  background: #fef3c7;
  border-color: #f59e0b;
  color: #92400e;
}

.icon-btn[aria-pressed="false"] {
  opacity: 0.58;
}

/* Main Play Area */
.play-area {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 12px;
  align-items: stretch;
}

.canvas-stage {
  flex: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 42%, rgba(255, 255, 255, 0.62), rgba(255, 255, 255, 0.28));
  border: 2px solid var(--card-border);
  border-radius: 20px;
  padding: 4px;
  box-shadow: inset 0 2px 12px rgba(32, 56, 28, 0.06);
  overflow: hidden;
}

#mazeCanvas {
  display: block;
  touch-action: none;
  cursor: pointer;
}

.floating-fire-btn {
  position: absolute;
  right: 16px;
  bottom: 16px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 2.5px solid #dc2626;
  background: radial-gradient(circle at 35% 30%, #fca5a5, #ef4444 55%, #b91c1c 100%);
  color: #ffffff;
  font-size: 26px;
  cursor: pointer;
  box-shadow: 0 5px 0 #991b1b, 0 8px 22px rgba(239, 68, 68, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 12;
  touch-action: none;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
}

.floating-fire-btn:active,
.floating-fire-btn.pressed {
  transform: translateY(3px) scale(0.95);
  box-shadow: 0 2px 0 #991b1b, 0 0 18px rgba(56, 189, 248, 0.75);
}

.floating-sonar-btn {
  position: absolute;
  right: 16px;
  bottom: 16px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 2.5px solid #d97706;
  background: radial-gradient(circle at 35% 30%, #fef9c3, #fbbf24 55%, #b45309 100%);
  color: #451a03;
  font-size: 26px;
  cursor: pointer;
  box-shadow: 0 5px 0 #92400e, 0 8px 22px rgba(250, 204, 21, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 12;
  touch-action: none;
  transition: transform 0.06s ease, opacity 0.2s ease;
}

.floating-sonar-btn:active,
.floating-sonar-btn.pressed {
  transform: translateY(3px) scale(0.95);
}

.floating-sonar-btn.cooling,
.dpad-sonar-btn.cooling {
  opacity: 0.48;
  filter: saturate(0.5);
}

/* Parade Banner */
.parade-banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  pointer-events: auto;
}

.parade-banner-inner {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(255, 253, 240, 0.96);
  border: 2.5px solid #f59e0b;
  border-radius: 999px;
  padding: 7px 13px 7px 15px;
  box-shadow: 0 8px 24px rgba(245, 158, 11, 0.28);
  font-weight: 800;
  font-size: 14.5px;
  color: #78350f;
  white-space: nowrap;
}

.parade-emoji {
  font-size: 20px;
}

.pill-btn {
  border: none;
  background: linear-gradient(180deg, #fbbf24, #f59e0b);
  color: #451a03;
  font-family: inherit;
  font-weight: 800;
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(217, 119, 6, 0.3);
}

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

/* Side Controls Panel (Touch D-Pad) */
.controls-panel {
  width: 224px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
}

.controls-panel.hidden {
  display: none;
}

.panel-card {
  background: var(--card-bg);
  border: 2px solid var(--card-border);
  border-radius: 18px;
  padding: 13px;
  box-shadow: var(--shadow-sm);
}

.panel-title {
  text-align: center;
  font-size: 11.5px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  margin-bottom: 10px;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(3, 58px);
  gap: 6px;
  justify-content: center;
  align-items: center;
}

.dpad-btn {
  width: 58px;
  height: 58px;
  border-radius: 15px;
  border: 2px solid #d97706;
  background: linear-gradient(180deg, #fde68a 0%, #fbbf24 55%, #f59e0b 100%);
  color: #5c2802;
  font-size: 22px;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 5px 0 #b45309, 0 8px 14px rgba(180, 83, 9, 0.22);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
  touch-action: none;
}

.dpad-btn:active,
.dpad-btn.pressed {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #b45309, 0 3px 6px rgba(180, 83, 9, 0.2);
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-center {
  grid-column: 2;
  grid-row: 2;
  width: 50px;
  height: 50px;
  margin: auto;
  border-radius: 50%;
  background: #f2f8ee;
  border: 2px dashed rgba(58, 97, 50, 0.22);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}

.dpad-fire-btn {
  grid-column: 2;
  grid-row: 2;
  width: 56px;
  height: 56px;
  margin: auto;
  border-radius: 50%;
  border: 2.5px solid #dc2626;
  background: radial-gradient(circle at 35% 30%, #fca5a5, #ef4444 55%, #b91c1c 100%);
  color: #ffffff;
  font-size: 24px;
  cursor: pointer;
  box-shadow: 0 4px 0 #991b1b, 0 0 14px rgba(239, 68, 68, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
  touch-action: none;
}

.dpad-fire-btn:active,
.dpad-fire-btn.pressed {
  transform: translateY(3px) scale(0.95);
  box-shadow: 0 1px 0 #991b1b, 0 0 18px rgba(56, 189, 248, 0.7);
}

.dpad-sonar-btn {
  grid-column: 2;
  grid-row: 2;
  width: 56px;
  height: 56px;
  margin: auto;
  border-radius: 50%;
  border: 2.5px solid #d97706;
  background: radial-gradient(circle at 35% 30%, #fef9c3, #fbbf24 55%, #b45309 100%);
  color: #451a03;
  font-size: 24px;
  cursor: pointer;
  box-shadow: 0 4px 0 #92400e, 0 0 14px rgba(250, 204, 21, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.06s ease, opacity 0.2s ease;
  touch-action: none;
}

.dpad-sonar-btn:active,
.dpad-sonar-btn.pressed {
  transform: translateY(3px) scale(0.95);
}

.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

.panel-tip {
  margin: 11px 0 0;
  font-size: 11.5px;
  line-height: 1.4;
  color: var(--ink-soft);
  text-align: center;
}

.legend-card {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 11px 13px;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink);
}

.legend-badge {
  width: 22px;
  text-align: center;
  font-size: 15px;
  flex-shrink: 0;
}

/* Footer */
.footer-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 2px 8px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--ink-soft);
  flex-shrink: 0;
}

.footer-right {
  white-space: nowrap;
  color: #b45309;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(18, 34, 20, 0.54);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal-card {
  width: min(440px, 100%);
  background: #ffffff;
  border-radius: 26px;
  border: 3px solid #fbbf24;
  padding: 22px 24px 20px;
  box-shadow: var(--shadow-lg);
  text-align: center;
  animation: modalPop 0.35s cubic-bezier(0.22, 1.32, 0.36, 1);
}

.modal-timeout {
  border-color: #f97316;
}

.modal-ribbon {
  display: inline-block;
  background: linear-gradient(90deg, #f59e0b, #f97316);
  color: #ffffff;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
  padding: 5px 14px;
  border-radius: 999px;
  margin-bottom: 8px;
}

.ribbon-timeout {
  background: linear-gradient(90deg, #ef4444, #f97316);
}

.modal-card h2 {
  margin: 0;
  font-size: 27px;
  font-weight: 900;
  color: var(--ink);
}

.celeb-subtitle {
  margin: 5px 0 12px;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink-soft);
  line-height: 1.4;
}

.celeb-stars {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 8px 0 14px;
}

.big-star {
  font-size: 40px;
  display: inline-block;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.big-star.earned {
  animation: starBounce 0.55s cubic-bezier(0.22, 1.5, 0.36, 1) both;
}

.big-star.dim {
  opacity: 0.22;
  filter: grayscale(1);
}

.score-breakdown {
  background: #f7faf5;
  border: 1.5px solid rgba(58, 97, 50, 0.14);
  border-radius: 16px;
  padding: 11px 15px;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13.5px;
}

.bd-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
}

.bd-penalty strong {
  color: #ea580c;
}

.bd-perfect strong {
  color: #16a34a;
}

.bd-divider {
  height: 1px;
  background: rgba(58, 97, 50, 0.14);
  margin: 2px 0;
}

.bd-total {
  font-size: 17px;
  color: #15803d;
}

.bd-grand {
  font-size: 13px;
  color: var(--ink-soft);
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.primary-btn {
  width: 100%;
  border: none;
  background: linear-gradient(180deg, #4ade80, #22c55e);
  color: #052e16;
  font-family: inherit;
  font-size: 17px;
  font-weight: 900;
  padding: 13px 18px;
  border-radius: 15px;
  cursor: pointer;
  box-shadow: 0 5px 0 #15803d, 0 8px 18px rgba(21, 128, 61, 0.25);
  transition: transform 0.08s ease;
}

.primary-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #15803d;
}

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

.secondary-btn {
  flex: 1;
  border: 2px solid rgba(58, 97, 50, 0.18);
  background: #ffffff;
  color: var(--ink);
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  padding: 10px 10px;
  border-radius: 12px;
  cursor: pointer;
}

.secondary-btn:hover {
  background: #f5faf2;
}

.hidden {
  display: none !important;
}

@keyframes popShake {
  0% { transform: scale(1); }
  25% { transform: scale(1.12) rotate(-3deg); }
  60% { transform: scale(1.06) rotate(3deg); }
  100% { transform: scale(1); }
}

@keyframes timerPulse {
  0% { transform: scale(1); }
  100% { transform: scale(1.04); }
}

@keyframes modalPop {
  0% { opacity: 0; transform: scale(0.84) translateY(16px); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes starBounce {
  0% { opacity: 0; transform: scale(0.2) rotate(-25deg); }
  65% { opacity: 1; transform: scale(1.25) rotate(8deg); }
  100% { opacity: 1; transform: scale(1) rotate(0deg); }
}

@media (max-width: 980px) {
  .play-area {
    flex-direction: column;
  }

  .controls-panel {
    width: 100%;
    flex-direction: row;
    align-items: center;
  }

  .controls-panel .panel-card {
    flex: 1;
    padding: 9px 11px;
  }

  .panel-tip {
    display: none;
  }

  .dpad {
    grid-template-columns: repeat(3, 50px);
    grid-template-rows: repeat(3, 50px);
  }

  .dpad-btn {
    width: 50px;
    height: 50px;
    font-size: 20px;
  }

  .dpad-center,
  .dpad-fire-btn {
    width: 46px;
    height: 46px;
    font-size: 20px;
  }

  .btn-text {
    display: none;
  }
}
