:root {
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);

  --glass-bg: rgba(11, 21, 44, 0.76);
  --glass-bg-strong: rgba(11, 21, 44, 0.88);
  --glass-border: rgba(154, 220, 255, 0.44);
  --glass-border-hi: rgba(255, 214, 102, 0.9);
  --ink-dark: #091124;
  --text-main: #f3f9ff;
  --text-muted: #b3d0ee;
  --accent-gold: #ffd166;
  --accent-red: #ef4755;
  --accent-green: #2cb672;
  --accent-blue: #4cc9f0;

  --yard-left: 0px;
  --yard-top: 48px;
  --yard-width: 640px;
  --yard-height: 480px;
  --yard-bottom: 528px;
  --arena-left: 54px;
  --arena-top: 134px;
  --arena-right: 586px;
  --arena-bottom: 470px;
  --apron-top: 528px;
  --apron-height: 200px;
  --gutter-left: 0px;
  --gutter-right: 0px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  background: #d8eefc;
  color: var(--text-main);
  font-family: "Courier New", Courier, monospace;
  overflow: hidden;
  overscroll-behavior: none;
}

img,
canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* Full-Bleed Stage Shell (Zero boxy outer borders!) */
.arcade-shell,
.stage-frame {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
}

#game-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
  touch-action: none;
}

body.touch-mode #game-canvas {
  cursor: default;
}

/* =========================================================
   INTEGRATED FLOATING TOP HUD
   ========================================================= */
.top-hud {
  position: fixed;
  top: max(6px, var(--sat));
  left: max(8px, var(--sal));
  right: max(8px, var(--sar));
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

body.screen-title .top-hud {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.hud-group {
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  min-width: 0;
}

.hud-glass-pill {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 10px;
  background: var(--glass-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1.5px solid var(--glass-border);
  border-radius: 12px;
  box-shadow: 0 4px 14px rgba(9, 17, 36, 0.22);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.hearts-pill {
  padding: 0 8px;
}

.hearts-row {
  display: flex;
  align-items: center;
  gap: 3px;
}

.hud-heart {
  width: 20px;
  height: 18px;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.hud-heart.empty {
  opacity: 0.22;
  filter: grayscale(1);
}

.wave-tag {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 900;
}

.wave-tag strong {
  color: #ffffff;
}

.phase-badge {
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.4px;
  padding: 3px 7px;
  border-radius: 7px;
}

.phase-badge.prep {
  background: rgba(33, 138, 206, 0.9);
  color: #eef9ff;
}

.phase-badge.combat {
  background: rgba(217, 56, 72, 0.94);
  color: #fff5f6;
}

.phase-badge.clear {
  background: rgba(34, 152, 92, 0.94);
  color: #edfff5;
}

.timer-val {
  font-size: 12px;
  font-weight: 900;
  color: #ffffff;
  min-width: 28px;
  text-align: right;
}

.economy-pill .snow-stat {
  color: #82e6ff;
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

.economy-pill .snow-icon {
  font-size: 12px;
}

.economy-pill .score-stat strong {
  color: #ffffff;
}

.economy-pill .best-stat strong {
  color: var(--accent-gold);
}

.economy-pill .stat-label {
  color: var(--text-muted);
  font-size: 10px;
}

.mobile-only {
  display: none;
}

.economy-pill .score-icon {
  color: var(--accent-gold);
  font-size: 11px;
  margin-right: 2px;
}

.hud-sep {
  color: rgba(179, 208, 238, 0.45);
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 5px;
  pointer-events: auto;
}

.hud-pill-btn {
  height: 36px;
  min-width: 36px;
  padding: 0 10px;
  border-radius: 11px;
  border: 1.5px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 4px 12px rgba(9, 17, 36, 0.22);
  color: var(--text-main);
  font-family: inherit;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.08s ease;
}

.hud-pill-btn:hover {
  background: rgba(24, 44, 84, 0.88);
}

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

.hud-pill-btn.active-toggle {
  background: rgba(29, 111, 165, 0.9);
  border-color: var(--accent-gold);
  color: #fffbe6;
}

/* =========================================================
   FLOATING CRISP WAVE TOAST & BOSS HEALTH BAR
   ========================================================= */
.wave-toast {
  position: fixed;
  top: calc(max(6px, var(--sat)) + 41px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 7;
  max-width: min(350px, calc(100vw - 24px));
  padding: 5px 14px;
  border-radius: 12px;
  background: rgba(11, 21, 44, 0.86);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1.5px solid rgba(140, 224, 255, 0.65);
  box-shadow: 0 6px 18px rgba(8, 16, 36, 0.32);
  text-align: center;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
}

.wave-toast.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(-6px);
  visibility: hidden;
}

.wave-toast-title {
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.5px;
  line-height: 1.15;
  color: var(--accent-gold);
  text-shadow: 1px 1px 0 var(--ink-dark);
  text-wrap: balance;
}

.wave-toast-sub {
  margin-top: 1px;
  font-size: 10px;
  font-weight: 700;
  line-height: 1.15;
  color: #dff4ff;
  text-wrap: balance;
}

.boss-hud-bar {
  position: fixed;
  top: calc(max(6px, var(--sat)) + 41px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 7;
  width: min(286px, calc(100vw - 32px));
  padding: 5px 11px 6px;
  border-radius: 11px;
  background: rgba(14, 22, 46, 0.88);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1.5px solid rgba(255, 209, 102, 0.85);
  box-shadow: 0 6px 18px rgba(8, 16, 36, 0.34);
  pointer-events: none;
}

.boss-hud-bar.hidden {
  display: none;
}

.boss-hud-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 10px;
  font-weight: 900;
  color: var(--accent-gold);
  margin-bottom: 4px;
}

.boss-hp-track {
  height: 7px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.14);
  overflow: hidden;
}

.boss-hp-fill {
  height: 100%;
  background: linear-gradient(90deg, #ff4d5a 0%, #ff8c42 100%);
  border-radius: 99px;
  transition: width 0.1s linear;
}

/* =========================================================
   TOUCH CONTROLS (SNOWY APRON / SIDE GUTTERS, ZERO ARENA OVERLAP)
   ========================================================= */
.touch-controls {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 7;
}

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

.touch-joystick-zone {
  position: fixed;
  left: max(8px, var(--sal));
  bottom: max(12px, var(--sab));
  width: 144px;
  height: 144px;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  touch-action: none;
}

.touch-joystick {
  position: relative;
  width: 112px;
  height: 112px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  touch-action: none;
  will-change: transform;
}

.joystick-ring {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(11, 22, 48, 0.58);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 2.5px solid rgba(190, 234, 255, 0.85);
  box-shadow:
    0 8px 20px rgba(8, 16, 36, 0.36),
    inset 0 0 18px rgba(6, 14, 34, 0.42);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.joystick-cross-h,
.joystick-cross-v {
  position: absolute;
  background: rgba(255, 255, 255, 0.22);
  pointer-events: none;
}

.joystick-cross-h {
  width: 58%;
  height: 2px;
}

.joystick-cross-v {
  width: 2px;
  height: 58%;
}

.joystick-knob {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 30%, #a5ebff 0%, #2c78c8 66%, #163b6e 100%);
  border: 2.5px solid #0b1630;
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, 0.85),
    0 4px 12px rgba(8, 16, 36, 0.42);
  transform: translate(0px, 0px);
  will-change: transform;
  pointer-events: none;
}

.touch-joystick.active .joystick-ring {
  border-color: var(--accent-gold);
  background: rgba(14, 30, 64, 0.54);
}

.touch-label {
  position: absolute;
  bottom: -6px;
  background: rgba(11, 21, 44, 0.82);
  border: 1px solid rgba(154, 220, 255, 0.55);
  border-radius: 99px;
  color: #e6f6ff;
  font-size: 9px;
  font-weight: 900;
  letter-spacing: 0.8px;
  padding: 1px 7px;
  pointer-events: none;
}

.touch-action-btn {
  position: fixed;
  right: max(14px, var(--sar));
  bottom: max(18px, var(--sab));
  width: 98px;
  height: 98px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 28%, #ff7a81 0%, #db3648 64%, #911a2a 100%);
  border: 3px solid #0b1630;
  box-shadow:
    0 0 0 2px rgba(255, 232, 155, 0.85),
    0 8px 20px rgba(9, 17, 36, 0.36);
  color: #ffffff;
  font-family: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  pointer-events: auto;
  touch-action: none;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.12s ease;
}

.charge-ring-svg {
  position: absolute;
  inset: -6px;
  width: calc(100% + 12px);
  height: calc(100% + 12px);
  transform: rotate(-90deg);
  pointer-events: none;
}

.charge-ring-track {
  fill: none;
  stroke: rgba(255, 255, 255, 0.18);
  stroke-width: 4;
}

.charge-ring-fill {
  fill: none;
  stroke: #ffe066;
  stroke-width: 5;
  stroke-linecap: round;
  stroke-dasharray: 283;
  stroke-dashoffset: 283;
  transition: stroke-dashoffset 0.04s linear;
}

.touch-action-btn.prep-mode {
  background: radial-gradient(circle at 34% 28%, #63e6a0 0%, #229257 64%, #155c35 100%);
  box-shadow:
    0 0 0 2px rgba(170, 232, 255, 0.9),
    0 8px 18px rgba(9, 17, 36, 0.32);
}

.touch-action-btn.charging {
  transform: scale(0.95);
  background: radial-gradient(circle at 34% 28%, #ffe885 0%, #f59f00 66%, #b35c00 100%);
  box-shadow:
    0 0 0 3px #ffffff,
    0 0 24px rgba(255, 209, 102, 0.85);
}

.touch-action-btn:active {
  transform: scale(0.94);
}

.touch-btn-title {
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0.8px;
  text-shadow: 1.5px 1.5px 0 var(--ink-dark);
}

.touch-btn-sub {
  font-size: 9px;
  font-weight: 800;
  color: #fff3d1;
  text-shadow: 1px 1px 0 var(--ink-dark);
}

/* =========================================================
   FLOATING PHASE-AWARE BUILD DOCK
   ========================================================= */
.bottom-bar {
  position: fixed;
  left: 50%;
  bottom: max(10px, var(--sab));
  transform: translateX(-50%);
  z-index: 8;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--glass-bg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1.5px solid var(--glass-border);
  border-radius: 16px;
  box-shadow: 0 10px 26px rgba(8, 16, 36, 0.3);
  max-width: calc(100vw - 16px);
  transition: opacity 0.22s ease, transform 0.22s ease, visibility 0.22s ease;
}

body.screen-title .bottom-bar {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

/* On Touch Mode / Mobile, collapse the build dock completely out of the way during Combat & Wave Clear! */
body.touch-mode.phase-combat .bottom-bar,
body.touch-mode.phase-wave_clear .bottom-bar {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateX(-50%) translateY(28px);
}

.build-cards {
  display: flex;
  align-items: center;
  gap: 6px;
}

.build-card {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(25, 43, 82, 0.78);
  border: 1.5px solid rgba(154, 220, 255, 0.28);
  border-radius: 11px;
  padding: 5px 9px;
  color: var(--text-main);
  font-family: inherit;
  cursor: pointer;
  min-width: 90px;
  min-height: 46px;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.08s ease;
}

.build-card:hover:not(:disabled) {
  background: rgba(38, 64, 118, 0.9);
}

.build-card:active:not(:disabled) {
  transform: scale(0.96);
}

.build-card.selected {
  border-color: var(--accent-gold);
  background: rgba(36, 74, 134, 0.95);
  box-shadow: 0 0 0 1px rgba(255, 209, 102, 0.55);
}

.build-card.refund-card.selected {
  border-color: #ff8b8b;
  background: rgba(105, 34, 52, 0.92);
}

.build-card.unaffordable {
  opacity: 0.52;
}

.build-card:disabled {
  opacity: 0.42;
  cursor: default;
}

.build-card img {
  width: 26px;
  height: 26px;
  object-fit: contain;
  flex-shrink: 0;
}

.refund-glyph {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 900;
  color: var(--accent-gold);
  flex-shrink: 0;
}

.build-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
}

.build-name {
  font-size: 11px;
  font-weight: 900;
  line-height: 1.05;
}

.build-cost {
  font-size: 11px;
  font-weight: 800;
  color: #82e6ff;
  line-height: 1.05;
}

.refund-card .build-cost {
  color: #ffe87a;
}

.hotkey {
  position: absolute;
  top: -6px;
  left: -5px;
  background: #0c1730;
  border: 1.5px solid rgba(154, 220, 255, 0.7);
  border-radius: 5px;
  color: var(--accent-gold);
  font-size: 9px;
  font-weight: 900;
  width: 17px;
  height: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
}

body.touch-mode .hotkey {
  display: none;
}

.action-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.combat-tip {
  font-size: 9px;
  font-weight: 700;
  color: var(--text-muted);
  text-align: center;
  white-space: nowrap;
}

body.touch-mode .combat-tip {
  display: none;
}

/* =========================================================
   BUTTONS
   ========================================================= */
.pixel-btn {
  font-family: inherit;
  font-weight: 900;
  font-size: 13px;
  letter-spacing: 0.5px;
  color: #ffffff;
  background: #274478;
  border: 2px solid var(--ink-dark);
  border-radius: 11px;
  box-shadow:
    inset 0 -3px 0 rgba(0, 0, 0, 0.3),
    inset 0 1.5px 0 rgba(255, 255, 255, 0.2),
    0 4px 12px rgba(8, 16, 36, 0.28);
  padding: 9px 15px;
  min-height: 44px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease, background 0.12s ease;
}

.pixel-btn:hover {
  background: #33589a;
}

.pixel-btn:active {
  transform: translateY(1px) scale(0.98);
}

.pixel-btn.small {
  font-size: 11px;
  padding: 6px 11px;
  min-height: 36px;
}

.pixel-btn.ghost-btn {
  background: rgba(16, 30, 62, 0.66);
  border-color: rgba(154, 220, 255, 0.4);
  color: #cfe9ff;
}

.pixel-btn.primary {
  background: linear-gradient(180deg, #32c97a 0%, #1f8e53 100%);
}

.pixel-btn.primary:hover {
  background: linear-gradient(180deg, #3bd685 0%, #25a15f 100%);
}

.pixel-btn.large {
  width: 100%;
  font-size: 17px;
  letter-spacing: 1px;
  min-height: 54px;
  border-radius: 14px;
}

.pixel-btn.fight-btn {
  background: linear-gradient(180deg, #f25764 0%, #c82e3d 100%);
  color: #ffffff;
  padding: 8px 16px;
  font-size: 13px;
  min-height: 46px;
  white-space: nowrap;
}

.pixel-btn.fight-btn:hover {
  background: linear-gradient(180deg, #ff6975 0%, #dc3848 100%);
}

.pixel-btn.fight-btn.hidden {
  display: none;
}

.pixel-btn.dock-place-btn {
  display: none;
}

.pixel-btn.dock-place-btn.refund-active {
  background: linear-gradient(180deg, #e05a6d 0%, #9b2638 100%);
}

.pixel-btn.active-toggle {
  background: #1d6fa5;
  border-color: var(--accent-gold);
}

/* =========================================================
   FULL-VIEWPORT TITLE & MODAL OVERLAYS (INTEGRATED GLASS)
   ========================================================= */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 16, 36, 0.62);
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(12px, var(--sat)) max(12px, var(--sar)) max(14px, var(--sab)) max(12px, var(--sal));
  z-index: 20;
}

.overlay.hidden {
  display: none;
}

.title-card {
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: var(--glass-bg-strong);
  border: 2px solid var(--glass-border);
  border-radius: 20px;
  box-shadow: 0 22px 48px rgba(4, 10, 26, 0.55);
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

.title-art-wrap {
  position: relative;
  width: 100%;
  overflow: hidden;
  background: #122142;
  flex-shrink: 0;
}

.title-art {
  width: 100%;
  aspect-ratio: 16 / 9;
  max-height: 240px;
  object-fit: cover;
  display: block;
}

.title-logo-bar {
  position: absolute;
  inset: auto 0 0 0;
  background: linear-gradient(transparent, rgba(9, 17, 36, 0.95) 42%);
  padding: 24px 14px 10px;
  text-align: center;
}

.title-logo-bar h1 {
  font-size: clamp(26px, 6vw, 38px);
  line-height: 1;
  letter-spacing: 5px;
  color: var(--accent-gold);
  text-shadow:
    3px 3px 0 var(--ink-dark),
    -1.5px -1.5px 0 var(--ink-dark);
}

.subtitle {
  margin-top: 4px;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.8px;
  color: #cceaff;
  text-wrap: balance;
}

.title-body {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 11px;
}

.mode-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.mode-btn {
  background: rgba(19, 34, 68, 0.78);
  border: 2px solid rgba(154, 220, 255, 0.25);
  border-radius: 13px;
  color: var(--text-main);
  padding: 10px 8px;
  min-height: 54px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-family: inherit;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.mode-btn:hover {
  background: rgba(29, 52, 100, 0.88);
}

.mode-btn.active {
  background: rgba(31, 68, 124, 0.94);
  border-color: var(--accent-gold);
  box-shadow: 0 0 0 1px rgba(255, 209, 102, 0.45);
}

.mode-name {
  font-size: 13px;
  font-weight: 900;
  color: var(--accent-gold);
}

.mode-desc {
  font-size: 10px;
  color: var(--text-muted);
}

.title-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.title-meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.title-best-box {
  font-size: 12px;
  font-weight: 800;
  color: var(--text-muted);
  background: rgba(14, 24, 48, 0.72);
  border: 1.5px solid rgba(154, 220, 255, 0.26);
  border-radius: 10px;
  padding: 7px 12px;
}

.title-best-box strong {
  color: var(--accent-gold);
}

.controls-legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
  font-size: 11px;
  color: var(--text-muted);
  background: rgba(12, 22, 44, 0.7);
  border: 1.5px solid rgba(154, 220, 255, 0.22);
  border-radius: 12px;
  padding: 9px 11px;
}

.controls-legend.collapsed {
  display: none;
}

.controls-legend strong {
  color: #ffffff;
}

/* Modal cards (Pause / Game Over / Victory) */
.modal-card {
  width: min(400px, 100%);
  background: var(--glass-bg-strong);
  border: 2px solid var(--glass-border);
  border-radius: 20px;
  box-shadow: 0 20px 44px rgba(4, 10, 26, 0.52);
  padding: 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-card.danger {
  border-color: rgba(255, 95, 109, 0.78);
}

.modal-card.victory {
  border-color: rgba(255, 209, 102, 0.85);
}

.modal-card h2 {
  font-size: 26px;
  letter-spacing: 2.5px;
  color: var(--accent-gold);
  text-shadow: 2px 2px 0 var(--ink-dark);
}

.modal-card.danger h2 {
  color: #ff6b76;
}

.modal-sub {
  font-size: 13px;
  color: #d6ecff;
}

.pause-settings-row {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
}

.pause-best-pill {
  padding: 7px 11px;
  border-radius: 10px;
  background: rgba(16, 28, 56, 0.78);
  border: 1.5px solid rgba(154, 220, 255, 0.28);
  font-size: 11px;
  font-weight: 800;
  color: var(--text-muted);
}

.pause-best-pill strong {
  color: var(--accent-gold);
}

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

.stats-grid > div {
  background: rgba(14, 24, 50, 0.78);
  border: 1.5px solid rgba(154, 220, 255, 0.22);
  border-radius: 12px;
  padding: 9px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.stats-grid span {
  font-size: 10px;
  color: var(--text-muted);
}

.stats-grid strong {
  font-size: 18px;
  color: #ffffff;
}

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

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

/* =========================================================
   PORTRAIT PHONE (390x844, 375x667) — FULL-BLEED APRON LAYOUT
   ========================================================= */
body[data-layout-mode="portrait-phone"] .desktop-only {
  display: none !important;
}

body[data-layout-mode="portrait-phone"] .mobile-only {
  display: inline;
}

body[data-layout-mode="portrait-phone"] .top-hud {
  gap: 5px;
}

body[data-layout-mode="portrait-phone"] .hud-glass-pill {
  height: 34px;
  padding: 0 7px;
  gap: 4px;
  font-size: 11px;
  border-radius: 10px;
}

body[data-layout-mode="portrait-phone"] .hud-heart {
  width: 16px;
  height: 15px;
}

body[data-layout-mode="portrait-phone"] .phase-badge {
  font-size: 10px;
  padding: 2px 5px;
  letter-spacing: 0.2px;
}

body[data-layout-mode="portrait-phone"] .timer-val {
  font-size: 11px;
  min-width: 24px;
}

body[data-layout-mode="portrait-phone"] .hud-pill-btn {
  height: 34px;
  min-width: 38px;
  padding: 0 8px;
  font-size: 11px;
  border-radius: 10px;
}

/* Full-screen integrated title on Portrait Phones (no floating card void!) */
body[data-layout-mode="portrait-phone"] .overlay.title-overlay {
  padding: 0;
  background: var(--glass-bg-strong);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}

body[data-layout-mode="portrait-phone"] .title-card {
  width: 100%;
  height: 100dvh;
  max-height: none;
  border-radius: 0;
  border: none;
  box-shadow: none;
  background: linear-gradient(180deg, #0b1630 0%, #0d1d3d 55%, #091329 100%);
}

body[data-layout-mode="portrait-phone"] .title-art-wrap {
  flex: 1 1 56%;
  min-height: 240px;
  max-height: none;
  display: flex;
}

body[data-layout-mode="portrait-phone"] .title-art {
  width: 100%;
  height: 100%;
  max-height: none;
  aspect-ratio: auto;
  object-fit: cover;
}

body[data-layout-mode="portrait-phone"] .title-logo-bar {
  padding: 44px 16px 14px;
  background: linear-gradient(transparent, rgba(11, 22, 48, 0.96) 54%, #0b1630 100%);
}

body[data-layout-mode="portrait-phone"] .title-logo-bar h1 {
  font-size: 36px;
  letter-spacing: 4px;
}

body[data-layout-mode="portrait-phone"] .title-body {
  flex: 0 0 auto;
  padding: 12px 16px calc(max(20px, var(--sab)) + 16px);
  justify-content: flex-start;
  gap: 14px;
}

body[data-layout-mode="portrait-phone"] .mode-btn {
  min-height: 64px;
  padding: 12px 10px;
}

body[data-layout-mode="portrait-phone"] .pixel-btn.large {
  min-height: 58px;
  font-size: 18px;
}

/* Prep Phase Build Dock anchored cleanly at the top of the snowy apron (below the fence!) */
body[data-layout-mode="portrait-phone"] .bottom-bar {
  top: calc(var(--apron-top) + 10px);
  bottom: auto;
  left: 8px;
  right: 8px;
  transform: none;
  max-width: none;
  flex-direction: column;
  align-items: stretch;
  gap: 7px;
  padding: 6px;
  border-radius: 14px;
}

body[data-layout-mode="portrait-phone"].touch-mode.phase-combat .bottom-bar,
body[data-layout-mode="portrait-phone"].touch-mode.phase-wave_clear .bottom-bar {
  transform: translateY(20px);
}

body[data-layout-mode="portrait-phone"] .build-cards {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 5px;
}

body[data-layout-mode="portrait-phone"] .build-card {
  min-width: 0;
  min-height: 54px;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  padding: 4px 2px;
  border-radius: 10px;
}

body[data-layout-mode="portrait-phone"] .build-card img {
  width: 24px;
  height: 24px;
}

body[data-layout-mode="portrait-phone"] .refund-glyph {
  width: 22px;
  height: 22px;
  font-size: 17px;
}

body[data-layout-mode="portrait-phone"] .build-meta {
  align-items: center;
}

body[data-layout-mode="portrait-phone"] .build-name {
  font-size: 9px;
}

body[data-layout-mode="portrait-phone"] .build-cost {
  font-size: 10px;
}

body[data-layout-mode="portrait-phone"] .action-col {
  width: 100%;
  align-items: stretch;
}

body[data-layout-mode="portrait-phone"] .pixel-btn.fight-btn {
  width: 100%;
  min-height: 44px;
  font-size: 14px;
  letter-spacing: 1px;
}

/* Portrait Phone Announcements: wide & balanced so no lonely 3rd-line orphan words */
body[data-layout-mode="portrait-phone"] .wave-toast {
  top: calc(max(6px, var(--sat)) + 39px);
  width: max-content;
  max-width: calc(100vw - 24px);
  padding: 4px 12px;
}

body[data-layout-mode="portrait-phone"] .wave-toast-title {
  font-size: 11px;
}

body[data-layout-mode="portrait-phone"] .wave-toast-sub {
  font-size: 9.5px;
}

body[data-layout-mode="portrait-phone"] .boss-hud-bar {
  top: calc(max(6px, var(--sat)) + 39px);
  width: min(300px, calc(100vw - 24px));
}

/* Portrait Phone Touch Controls: IDENTICAL size & rock-solid thumb arc across Prep and Combat! */
body[data-layout-mode="portrait-phone"] .touch-joystick-zone {
  top: calc(var(--apron-top) + 148px);
  bottom: max(8px, var(--sab));
  left: 0;
  width: 52%;
  height: auto;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 4px 0 0 22px;
}

body[data-layout-mode="portrait-phone"].phase-combat .touch-joystick-zone,
body[data-layout-mode="portrait-phone"].phase-wave_clear .touch-joystick-zone {
  top: calc(var(--apron-top) + 24px);
  padding-top: 128px;
}

body[data-layout-mode="portrait-phone"] .touch-joystick {
  width: 106px;
  height: 106px;
}

body[data-layout-mode="portrait-phone"] .touch-action-btn {
  right: 22px;
  top: calc(var(--apron-top) + 156px);
  bottom: auto;
  width: 98px;
  height: 98px;
}

body[data-layout-mode="portrait-phone"].phase-prep .touch-btn-title {
  font-size: 14px;
}

body[data-layout-mode="portrait-phone"].phase-prep .touch-btn-sub {
  font-size: 8.5px;
}

/* Extra-compact adjustments for iPhone SE (375x667) — consistent size & zero jump across phases! */
@media (max-height: 700px) and (max-width: 600px) {
  body[data-layout-mode="portrait-phone"] .title-art-wrap {
    flex: 1 1 48%;
    max-height: none;
    min-height: 180px;
  }
  body[data-layout-mode="portrait-phone"] .title-logo-bar h1 {
    font-size: 30px;
  }
  body[data-layout-mode="portrait-phone"] .title-body {
    padding: 12px 14px calc(max(12px, var(--sab)) + 8px);
    gap: 10px;
  }
  body[data-layout-mode="portrait-phone"] .mode-btn {
    min-height: 54px;
    padding: 8px;
  }
  body[data-layout-mode="portrait-phone"] .pixel-btn.large {
    min-height: 50px;
    font-size: 16px;
  }
  body[data-layout-mode="portrait-phone"] .bottom-bar {
    top: calc(var(--apron-top) + 6px);
    gap: 5px;
    padding: 5px;
  }
  body[data-layout-mode="portrait-phone"] .build-card {
    min-height: 48px;
  }
  body[data-layout-mode="portrait-phone"] .pixel-btn.fight-btn {
    min-height: 38px;
    font-size: 13px;
  }
  body[data-layout-mode="portrait-phone"] .touch-joystick-zone {
    top: calc(var(--apron-top) + 126px);
    padding: 4px 0 0 20px;
  }
  body[data-layout-mode="portrait-phone"].phase-combat .touch-joystick-zone,
  body[data-layout-mode="portrait-phone"].phase-wave_clear .touch-joystick-zone {
    top: calc(var(--apron-top) + 18px);
    padding-top: 112px;
  }
  body[data-layout-mode="portrait-phone"] .touch-joystick {
    width: 92px;
    height: 92px;
  }
  body[data-layout-mode="portrait-phone"] .touch-action-btn {
    top: calc(var(--apron-top) + 133px);
    bottom: auto;
    width: 86px;
    height: 86px;
  }
}

/* Portrait phone modals act like comfortable bottom sheets */
body[data-layout-mode="portrait-phone"] .overlay:not(.title-overlay) {
  align-items: flex-end;
}

body[data-layout-mode="portrait-phone"] .modal-card {
  border-radius: 22px 22px 16px 16px;
}

/* =========================================================
   LANDSCAPE PHONE (844x390, 667x375) — SIDE GUTTER CONTROLS, 1:1 YARD
   ========================================================= */
body[data-layout-mode="landscape-phone"] .desktop-only {
  display: none !important;
}

body[data-layout-mode="landscape-phone"] .mobile-only {
  display: inline;
}

body[data-layout-mode="landscape-phone"] .top-hud {
  top: max(4px, var(--sat));
  left: max(10px, var(--sal));
  right: max(10px, var(--sar));
}

body[data-layout-mode="landscape-phone"] .hud-glass-pill,
body[data-layout-mode="landscape-phone"] .hud-pill-btn {
  height: 32px;
  font-size: 11px;
  border-radius: 10px;
}

body[data-layout-mode="landscape-phone"] .hud-heart {
  width: 16px;
  height: 15px;
}

/* Dock wave toast & boss bar dynamically to the measured HUD center gap (second-row fallback if tight!) */
body[data-layout-mode="landscape-phone"] .wave-toast {
  top: max(4px, var(--sat));
  padding: 3px 10px;
  border-radius: 10px;
  max-width: min(280px, max(160px, var(--hud-gap-width, 240px)));
}

body[data-layout-mode="landscape-phone"] .wave-toast-title {
  font-size: 10.5px;
}

body[data-layout-mode="landscape-phone"] .wave-toast-sub {
  font-size: 9px;
}

body[data-layout-mode="landscape-phone"] .boss-hud-bar {
  top: max(4px, var(--sat));
  width: min(248px, max(172px, var(--hud-gap-width, 240px)));
  padding: 4px 10px 5px;
  border-radius: 10px;
}

body[data-layout-mode="landscape-phone"] .boss-hud-head {
  font-size: 9.5px;
  margin-bottom: 3px;
}

body[data-layout-mode="landscape-phone"] .boss-hp-track {
  height: 6px;
}

body[data-layout-mode="landscape-phone"].hud-tight-gap .wave-toast,
body[data-layout-mode="landscape-phone"].hud-tight-gap .boss-hud-bar {
  top: calc(max(4px, var(--sat)) + 36px);
  max-width: min(280px, calc(100vw - 28px));
  width: min(240px, calc(100vw - 28px));
}

/* Left gutter: strictly bounded outside arenaRect at ANY safe-area inset! */
body[data-layout-mode="landscape-phone"] .touch-joystick-zone {
  top: 42px;
  bottom: max(6px, var(--sab));
  left: max(4px, var(--sal));
  right: calc(100vw - var(--arena-left) + 8px);
  width: auto;
  height: auto;
  align-items: center;
  justify-content: center;
}

body[data-layout-mode="landscape-phone"] .touch-joystick {
  width: 102px;
  height: 102px;
}

/* Right gutter in Combat: dedicated THROW button centered cleanly inside the right gutter */
body[data-layout-mode="landscape-phone"] .touch-action-btn {
  right: max(12px, calc(var(--sar) + max(0px, (var(--gutter-right) - 96px) / 2)));
  bottom: calc(50% - 56px);
  width: 96px;
  height: 96px;
}

/* Right gutter in Prep: compact centered 2-col build dock + PLACE & FIGHT buttons */
body[data-layout-mode="landscape-phone"].phase-prep .touch-action-btn {
  display: none;
}

body[data-layout-mode="landscape-phone"] .bottom-bar {
  left: auto;
  right: max(8px, var(--sar));
  top: 54%;
  bottom: auto;
  width: max(142px, min(180px, calc(var(--gutter-right) - 14px)));
  max-width: 180px;
  transform: translateY(-50%);
  flex-direction: column;
  justify-content: center;
  align-items: stretch;
  gap: 5px;
  padding: 6px;
  border-radius: 14px;
}

body[data-layout-mode="landscape-phone"].touch-mode.phase-combat .bottom-bar,
body[data-layout-mode="landscape-phone"].touch-mode.phase-wave_clear .bottom-bar {
  transform: translate(24px, -50%);
}

body[data-layout-mode="landscape-phone"] .build-cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
}

body[data-layout-mode="landscape-phone"] .build-card {
  min-width: 0;
  min-height: 44px;
  flex-direction: column;
  justify-content: center;
  gap: 1px;
  padding: 3px 2px;
  border-radius: 9px;
}

body[data-layout-mode="landscape-phone"] .build-card.refund-card {
  grid-column: span 2;
  flex-direction: row;
  min-height: 32px;
  gap: 6px;
}

body[data-layout-mode="landscape-phone"] .build-card img {
  width: 21px;
  height: 21px;
}

body[data-layout-mode="landscape-phone"] .build-meta {
  align-items: center;
}

body[data-layout-mode="landscape-phone"] .build-name {
  font-size: 9px;
}

body[data-layout-mode="landscape-phone"] .build-cost {
  font-size: 9px;
}

body[data-layout-mode="landscape-phone"] .action-col {
  width: 100%;
  display: grid;
  grid-template-columns: 0.78fr 1.22fr;
  align-items: stretch;
  gap: 5px;
}

body[data-layout-mode="landscape-phone"].touch-mode.phase-prep .pixel-btn.dock-place-btn {
  display: inline-flex;
  min-width: 0;
  min-height: 38px;
  font-size: clamp(8.5px, 1.35vw, 10.5px);
  letter-spacing: 0.2px;
  padding: 5px 4px;
}

body[data-layout-mode="landscape-phone"] .pixel-btn.fight-btn {
  min-width: 0;
  min-height: 38px;
  font-size: clamp(8.5px, 1.35vw, 10.5px);
  letter-spacing: 0.2px;
  padding: 5px 4px;
}

body[data-layout-mode="landscape-phone"]:not(.touch-mode) .action-col,
body[data-layout-mode="landscape-phone"]:not(.phase-prep) .action-col {
  grid-template-columns: 1fr;
}

/* Landscape phone Title Screen: horizontal 2-column split so art + START fit cleanly without scrolling! */
body[data-layout-mode="landscape-phone"] .title-card {
  width: min(740px, 100%);
  flex-direction: row;
  align-items: stretch;
}

body[data-layout-mode="landscape-phone"] .title-art-wrap {
  width: 44%;
  display: flex;
}

body[data-layout-mode="landscape-phone"] .title-art {
  height: 100%;
  max-height: none;
  aspect-ratio: auto;
}

body[data-layout-mode="landscape-phone"] .title-logo-bar h1 {
  font-size: 24px;
  letter-spacing: 3px;
}

body[data-layout-mode="landscape-phone"] .title-body {
  width: 56%;
  justify-content: center;
  padding: 12px;
  gap: 8px;
}

body[data-layout-mode="landscape-phone"] .pixel-btn.large {
  min-height: 46px;
  font-size: 15px;
}

/* =========================================================
   PORTRAIT TABLET (iPad 768x1024 / 820x1180)
   ========================================================= */
body[data-layout-mode="portrait-tablet"] .tablet-hide {
  display: none !important;
}

body[data-layout-mode="portrait-tablet"] .hud-glass-pill,
body[data-layout-mode="portrait-tablet"] .hud-pill-btn {
  height: 34px;
  padding: 0 9px;
  font-size: 11px;
}

body[data-layout-mode="portrait-tablet"] .bottom-bar {
  top: calc(var(--apron-top) + 12px);
  bottom: auto;
  left: 50%;
  transform: translateX(-50%);
  width: min(720px, calc(100vw - 24px));
  justify-content: space-between;
}

body[data-layout-mode="portrait-tablet"] .build-cards {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  flex: 1;
}

body[data-layout-mode="portrait-tablet"] .build-card {
  min-width: 0;
  justify-content: center;
  min-height: 52px;
}

/* Portrait Tablet Touch Controls: rock-solid consistent position & size across Prep & Combat */
body[data-layout-mode="portrait-tablet"] .touch-joystick-zone {
  top: calc(var(--apron-top) + 96px);
  bottom: max(14px, var(--sab));
  left: 16px;
  width: 46%;
  height: auto;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 8px 0 0 22px;
}

body[data-layout-mode="portrait-tablet"].phase-combat .touch-joystick-zone,
body[data-layout-mode="portrait-tablet"].phase-wave_clear .touch-joystick-zone {
  top: calc(var(--apron-top) + 16px);
  padding-top: 88px;
}

body[data-layout-mode="portrait-tablet"] .touch-joystick {
  width: 118px;
  height: 118px;
}

body[data-layout-mode="portrait-tablet"] .touch-action-btn {
  right: 38px;
  top: calc(var(--apron-top) + 108px);
  bottom: auto;
  width: 110px;
  height: 110px;
}

/* =========================================================
   LANDSCAPE TABLET (iPad 1024x768) & DESKTOP
   ========================================================= */
body[data-layout-mode="landscape"]:not(.hud-tight-gap) .wave-toast,
body[data-layout-mode="landscape"]:not(.hud-tight-gap) .boss-hud-bar {
  top: max(6px, var(--sat));
  max-width: min(340px, max(190px, var(--hud-gap-width, 280px)));
}

/* Left gutter: strictly bounded outside arenaRect (zero overlap even on 1024px iPad!) */
body[data-layout-mode="landscape"] .touch-joystick-zone {
  top: 64px;
  bottom: max(16px, var(--sab));
  left: max(4px, var(--sal));
  right: calc(100vw - var(--arena-left) + 8px);
  width: auto;
  height: auto;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 18px;
}

body[data-layout-mode="landscape"].phase-prep .touch-joystick-zone {
  padding-bottom: 72px;
}

body[data-layout-mode="landscape"] .touch-joystick {
  width: 108px;
  height: 108px;
}

body[data-layout-mode="landscape"] .touch-action-btn {
  right: max(12px, calc(var(--sar) + max(0px, (var(--gutter-right) - 102px) / 2)));
  bottom: calc(max(16px, var(--sab)) + 23px);
  width: 98px;
  height: 98px;
}

body[data-layout-mode="landscape"].phase-prep .touch-action-btn {
  bottom: calc(max(16px, var(--sab)) + 77px);
}
