:root {
  --bg: #0c142a;
  --panel: rgba(13, 21, 44, 0.88);
  --panel-border: rgba(218, 174, 86, 0.42);
  --ink: #f7f4ea;
  --muted: #a6b7d4;
  --gold: #f5cb66;
  --emerald: #3ef7ca;
  --crimson: #ff5d78;
  --sky: #6cc8ff;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

[hidden] {
  display: none !important;
}

canvas#game {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* ─── TOP HUD ─── */
.hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 14px 10px;
  background: linear-gradient(180deg, rgba(8, 14, 32, 0.88) 0%, rgba(8, 14, 32, 0.42) 72%, rgba(8, 14, 32, 0) 100%);
  pointer-events: none;
}

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

.hud-right {
  justify-content: flex-end;
}

.stat-pill {
  background: rgba(14, 24, 52, 0.84);
  border: 1px solid rgba(218, 174, 86, 0.34);
  border-radius: 11px;
  padding: 5px 10px;
  min-width: 64px;
  text-align: center;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.28);
}

.stat-label {
  display: block;
  font-size: 9px;
  letter-spacing: 0.14em;
  color: var(--muted);
  font-weight: 700;
}

.stat-pill strong {
  display: block;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  line-height: 1.15;
}

.lives-pill strong {
  color: #ff6e88;
  letter-spacing: 2px;
}

.combo-pill strong {
  color: var(--emerald);
}

.rule-banner {
  background: linear-gradient(180deg, rgba(28, 43, 84, 0.94), rgba(14, 23, 48, 0.94));
  border: 2px solid var(--gold);
  border-radius: 14px;
  padding: 7px 18px;
  text-align: center;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.42), inset 0 1px 0 rgba(255, 242, 200, 0.3);
  max-width: min(440px, 92vw);
}

.rule-title {
  font-size: clamp(15px, 2.2vw, 22px);
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--gold);
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.6);
  white-space: nowrap;
}

.rule-sub {
  font-size: 11px;
  color: #d8e5ff;
  margin-top: 2px;
}

.tool-group {
  display: flex;
  gap: 6px;
  pointer-events: auto;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  background: rgba(18, 30, 62, 0.88);
  color: var(--ink);
  font-size: 16px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.12s ease, background 0.12s ease;
}

.icon-btn:hover {
  background: rgba(34, 54, 102, 0.95);
  transform: translateY(-1px);
}

.icon-btn:focus-visible,
button:focus-visible {
  outline: 2px solid var(--emerald);
  outline-offset: 2px;
}

/* ─── FLOATING BANNERS ─── */
.center-banner {
  position: fixed;
  top: 78px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 15;
  padding: 7px 18px;
  border-radius: 999px;
  background: rgba(11, 20, 44, 0.92);
  border: 2px solid var(--emerald);
  color: var(--gold);
  font-size: clamp(13px, 1.8vw, 18px);
  font-weight: 800;
  letter-spacing: 0.05em;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.45);
  pointer-events: none;
  text-align: center;
  max-width: 88vw;
}

.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 14;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(14, 26, 56, 0.92);
  border: 1px solid rgba(62, 247, 202, 0.45);
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
  pointer-events: none;
  max-width: 86vw;
  text-align: center;
}

/* ─── BOTTOM CONTROLS & SKILL DOCK ─── */
.bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 11;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 16px;
  pointer-events: none;
}

.dpad {
  display: none;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 46px);
  gap: 4px;
  pointer-events: auto;
}

.pad-btn {
  border-radius: 11px;
  border: 1px solid rgba(154, 210, 255, 0.34);
  background: rgba(14, 26, 56, 0.84);
  color: var(--ink);
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
}

.pad-btn:active {
  background: rgba(62, 247, 202, 0.35);
  transform: scale(0.95);
}

.pad-up { grid-column: 2; grid-row: 1; }
.pad-left { grid-column: 1; grid-row: 2; }
.pad-down { grid-column: 2; grid-row: 3; }
.pad-right { grid-column: 3; grid-row: 2; }

.skill-dock {
  margin-left: auto;
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.skill-btn {
  min-width: 82px;
  height: 54px;
  padding: 6px 12px;
  border-radius: 13px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  background: rgba(14, 26, 54, 0.88);
  color: var(--ink);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.36);
  transition: transform 0.12s ease, filter 0.12s ease, border-color 0.15s ease;
}

.skill-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  filter: brightness(1.1);
}

.skill-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.skill-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.skill-name {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.skill-key {
  font-size: 10px;
  color: var(--muted);
  font-weight: 700;
}

.chomp-btn {
  border-color: var(--emerald);
  background: linear-gradient(180deg, rgba(24, 96, 86, 0.94), rgba(14, 54, 56, 0.94));
}

.chomp-btn .skill-key {
  color: #b6ffec;
}

.dash-btn {
  border-color: var(--sky);
}

.nova-btn.ready {
  border-color: var(--gold);
  background: linear-gradient(180deg, rgba(164, 114, 24, 0.96), rgba(96, 56, 12, 0.96));
  box-shadow: 0 0 22px rgba(245, 203, 102, 0.55);
  animation: novaPulse 1.1s ease-in-out infinite;
}

.nova-btn.ready .skill-key {
  color: #fff6c9;
}

@keyframes novaPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

/* ─── OVERLAYS ─── */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: radial-gradient(circle at 50% 35%, rgba(18, 30, 66, 0.66), rgba(7, 12, 26, 0.9));
  backdrop-filter: blur(6px);
  overflow-y: auto;
}

.modal-panel {
  width: min(560px, 100%);
  background: var(--panel);
  border: 2px solid var(--panel-border);
  border-radius: 20px;
  padding: 24px 22px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.62), inset 0 1px 0 rgba(255, 242, 200, 0.16);
}

.modal-panel.compact {
  width: min(440px, 100%);
}

.eyebrow {
  font-size: 11px;
  letter-spacing: 0.2em;
  color: var(--emerald);
  font-weight: 800;
  margin-bottom: 6px;
}

.modal-panel h1 {
  font-size: clamp(26px, 5vw, 38px);
  color: var(--gold);
  line-height: 1.08;
  margin-bottom: 10px;
  text-shadow: 0 3px 12px rgba(0, 0, 0, 0.5);
}

.modal-panel h2 {
  font-size: clamp(24px, 4.4vw, 32px);
  color: var(--gold);
  margin-bottom: 8px;
}

.card-subtitle {
  font-size: 14px;
  line-height: 1.5;
  color: #d7e2f8;
  margin-bottom: 18px;
}

.section-label {
  font-size: 10px;
  letter-spacing: 0.18em;
  color: var(--muted);
  font-weight: 800;
  margin-bottom: 8px;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.mode-btn {
  padding: 10px 6px;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.14);
  background: rgba(20, 32, 64, 0.84);
  color: var(--ink);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}

.mode-btn strong {
  font-size: 20px;
  color: var(--gold);
}

.mode-btn span {
  font-size: 11px;
  font-weight: 700;
}

.mode-btn.selected {
  border-color: var(--emerald);
  background: rgba(26, 78, 86, 0.92);
  box-shadow: 0 0 16px rgba(62, 247, 202, 0.28);
}

.order-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 18px;
}

.order-row .section-label {
  margin-bottom: 0;
}

.seg-toggle {
  display: inline-flex;
  padding: 3px;
  border-radius: 10px;
  background: rgba(9, 15, 34, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.16);
}

.seg-btn {
  padding: 6px 14px;
  border-radius: 7px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.seg-btn.selected {
  background: var(--gold);
  color: #131c36;
}

.cta-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.primary-btn,
.secondary-btn {
  min-height: 48px;
  padding: 12px 24px;
  border-radius: 12px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.primary-btn {
  border: 0;
  background: linear-gradient(180deg, #ffe28a, #ebb43e);
  color: #131a30;
  box-shadow: 0 6px 0 #9e7118, 0 12px 24px rgba(0, 0, 0, 0.35);
}

.primary-btn:hover:not(:disabled) {
  filter: brightness(1.07);
  transform: translateY(-1px);
}

.primary-btn:disabled {
  opacity: 0.55;
  cursor: wait;
}

.secondary-btn {
  border: 1px solid rgba(255, 255, 255, 0.26);
  background: rgba(24, 38, 76, 0.9);
  color: var(--ink);
}

.controls-note {
  margin-top: 14px;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.5;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}

.summary-box {
  background: rgba(22, 35, 70, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  padding: 10px;
}

.summary-box span {
  display: block;
  font-size: 10px;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.summary-box strong {
  display: block;
  font-size: 22px;
  color: var(--ink);
  margin-top: 2px;
}

/* ─── MOBILE & NARROW LAYOUT (390×844 ZERO HORIZONTAL OVERFLOW) ─── */
@media (pointer: coarse), (max-width: 780px) {
  .dpad {
    display: grid;
  }
}

@media (max-width: 720px) {
  .hud {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    padding: max(8px, env(safe-area-inset-top)) 10px 8px;
    gap: 6px;
  }

  .rule-banner {
    grid-column: 1 / -1;
    grid-row: 1;
    justify-self: stretch;
    max-width: 100%;
    padding: 5px 10px;
  }

  .rule-title {
    font-size: 15px;
  }

  .rule-sub {
    font-size: 10px;
  }

  .hud-left {
    grid-row: 2;
    gap: 4px;
  }

  .hud-right {
    grid-row: 2;
    gap: 4px;
  }

  .stat-pill {
    min-width: 48px;
    padding: 3px 6px;
  }

  .stat-pill strong {
    font-size: 13px;
  }

  .icon-btn {
    width: 32px;
    height: 32px;
    font-size: 14px;
  }

  .mode-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .bottom-bar {
    padding: 0 10px;
  }

  .skill-dock {
    flex-direction: column;
    gap: 6px;
  }

  .skill-btn {
    min-width: 74px;
    height: 44px;
    padding: 4px 8px;
  }

  .center-banner {
    top: 112px;
    font-size: 13px;
    padding: 6px 14px;
  }

  .toast {
    bottom: 168px;
    font-size: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
