:root {
  --bg-void: #05070e;
  --bg-cabinet: #0b101d;
  --bg-screen: #060913;
  --border-subtle: rgba(130, 175, 255, 0.16);
  --cyan: #00f5ff;
  --cyan-soft: rgba(0, 245, 255, 0.25);
  --magenta: #ff2e93;
  --magenta-soft: rgba(255, 46, 147, 0.25);
  --lime: #7dffb3;
  --text-main: #eef6ff;
  --text-muted: #8b9bb8;
  --font-mono: 'SF Mono', 'Fira Code', 'JetBrains Mono', 'Menlo', 'Consolas', monospace;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

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

html {
  width: 100%;
  height: 100%;
  height: -webkit-fill-available;
  overflow: hidden;
  overscroll-behavior: none;
  background-color: var(--bg-void);
}

body {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  min-height: -webkit-fill-available;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  background-color: var(--bg-void);
  color: var(--text-main);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

body.is-playing {
  touch-action: none;
}

.ambient-bg {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 18% 25%, rgba(0, 245, 255, 0.11), transparent 42%),
    radial-gradient(circle at 82% 75%, rgba(255, 46, 147, 0.11), transparent 44%),
    linear-gradient(180deg, #070a14 0%, #04050a 100%);
  z-index: 0;
}

.cabinet-shell {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-right: max(10px, env(safe-area-inset-right));
  padding-bottom: max(36px, calc(env(safe-area-inset-bottom) + 30px));
  padding-left: max(10px, env(safe-area-inset-left));
}

.cabinet-bezel {
  position: relative;
  width: min(1080px, 100%);
  height: 100%;
  max-height: 780px;
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.4vh, 14px);
  padding: clamp(8px, 1.4vw, 18px);
  border-radius: 18px;
  background: linear-gradient(165deg, rgba(19, 27, 46, 0.94), rgba(9, 13, 24, 0.98));
  border: 1px solid rgba(126, 178, 255, 0.2);
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.78),
    0 0 40px rgba(0, 245, 255, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
  touch-action: none;
}

/* HUD Bar */
.hud {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  border-radius: 12px;
  background: rgba(5, 8, 16, 0.76);
  border: 1px solid var(--border-subtle);
}

.score-block {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.score-player {
  justify-content: flex-start;
}

.score-ai {
  justify-content: flex-end;
  flex-direction: row-reverse;
}

.score-label {
  font-family: var(--font-mono);
  font-size: clamp(0.65rem, 1.1vw, 0.82rem);
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--text-muted);
}

.score-value {
  font-family: var(--font-mono);
  font-size: clamp(1.5rem, 3.2vw, 2.4rem);
  font-weight: 800;
  line-height: 1;
}

.score-player .score-value {
  color: var(--cyan);
  text-shadow: 0 0 14px rgba(0, 245, 255, 0.7);
}

.score-ai .score-value {
  color: var(--magenta);
  text-shadow: 0 0 14px rgba(255, 46, 147, 0.7);
}

.hud-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

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

.badge {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(0, 245, 255, 0.12);
  color: var(--cyan);
  border: 1px solid rgba(0, 245, 255, 0.32);
}

.badge-subtle {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-muted);
  border-color: rgba(255, 255, 255, 0.12);
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  min-width: 44px;
  padding: 6px 12px;
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-main);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  cursor: pointer;
  touch-action: manipulation;
  transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
  border-color: rgba(0, 245, 255, 0.55);
  background: rgba(0, 245, 255, 0.1);
}

.icon-btn.is-active {
  border-color: var(--magenta);
  color: var(--magenta);
  background: rgba(255, 46, 147, 0.12);
  box-shadow: 0 0 12px rgba(255, 46, 147, 0.25);
}

kbd {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.65rem;
  font-weight: 700;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: var(--text-main);
}

/* Responsive 5:3 Screen Stage */
.screen-stage {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  container-type: size;
  touch-action: none;
}

.screen-frame {
  position: relative;
  width: min(100cqw, calc(100cqh * 5 / 3));
  height: min(100cqh, calc(100cqw * 3 / 5));
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg-screen);
  border: 2px solid rgba(0, 245, 255, 0.28);
  box-shadow:
    inset 0 0 36px rgba(0, 0, 0, 0.9),
    0 0 24px rgba(0, 245, 255, 0.12);
  touch-action: none;
}

#pongCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.crt-scanlines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    rgba(255, 255, 255, 0) 0px,
    rgba(255, 255, 255, 0) 2px,
    rgba(0, 0, 0, 0.14) 3px,
    rgba(0, 0, 0, 0.14) 4px
  );
  opacity: 0.42;
  z-index: 2;
}

/* Friendly non-blocking portrait hint */
.rotate-hint {
  display: none;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 10px;
  background: rgba(0, 245, 255, 0.08);
  border: 1px solid rgba(0, 245, 255, 0.22);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: rgba(215, 238, 255, 0.88);
  text-align: center;
  line-height: 1.3;
}

.rotate-icon {
  font-size: 0.95rem;
  color: var(--cyan);
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  border-radius: 18px;
  display: flex;
  justify-content: center;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: rgba(4, 6, 13, 0.82);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.overlay.hidden {
  display: none;
}

.panel {
  margin: auto;
  width: min(500px, 100%);
  padding: clamp(18px, 3vw, 28px);
  border-radius: 16px;
  background: linear-gradient(170deg, rgba(15, 21, 38, 0.96), rgba(8, 12, 24, 0.98));
  border: 1px solid rgba(0, 245, 255, 0.34);
  box-shadow:
    0 18px 48px rgba(0, 0, 0, 0.82),
    0 0 32px rgba(0, 245, 255, 0.14);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(10px, 1.8vh, 16px);
}

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

.eyebrow {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.24em;
  color: var(--cyan);
}

.title {
  font-family: var(--font-mono);
  font-size: clamp(1.85rem, 4.6vw, 2.85rem);
  font-weight: 900;
  letter-spacing: 0.1em;
  line-height: 1;
  color: #ffffff;
  text-shadow: 0 0 18px rgba(0, 245, 255, 0.6);
}

.title span {
  color: var(--magenta);
  text-shadow: 0 0 18px rgba(255, 46, 147, 0.65);
}

.overlay-title {
  font-family: var(--font-mono);
  font-size: clamp(1.55rem, 3.8vw, 2.3rem);
  font-weight: 900;
  letter-spacing: 0.12em;
  color: #ffffff;
  text-shadow: 0 0 18px rgba(0, 245, 255, 0.65);
}

.overlay-title.ai-win {
  color: var(--magenta);
  text-shadow: 0 0 20px rgba(255, 46, 147, 0.75);
}

.overlay-title.player-win {
  color: var(--cyan);
  text-shadow: 0 0 20px rgba(0, 245, 255, 0.75);
}

.subtitle {
  font-size: clamp(0.8rem, 1.4vw, 0.92rem);
  color: var(--text-muted);
  line-height: 1.4;
}

.difficulty-group {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.group-label {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.18em;
  color: var(--text-muted);
}

.difficulty-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.diff-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 54px;
  padding: 10px 6px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text-main);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.diff-btn strong {
  font-family: var(--font-mono);
  font-size: 0.84rem;
  letter-spacing: 0.08em;
}

.diff-btn span {
  font-size: 0.68rem;
  color: var(--text-muted);
}

.diff-btn:hover {
  border-color: rgba(0, 245, 255, 0.5);
  transform: translateY(-1px);
}

.diff-btn.active {
  border-color: var(--cyan);
  background: rgba(0, 245, 255, 0.14);
  box-shadow: 0 0 18px rgba(0, 245, 255, 0.25);
}

.diff-btn.active strong {
  color: var(--cyan);
}

.primary-btn,
.secondary-btn,
.ghost-btn {
  width: 100%;
  min-height: 48px;
  padding: 12px 18px;
  border-radius: 10px;
  font-family: var(--font-mono);
  font-size: 0.88rem;
  font-weight: 800;
  letter-spacing: 0.11em;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, box-shadow 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.primary-btn {
  border: 1px solid var(--cyan);
  background: linear-gradient(90deg, rgba(0, 245, 255, 0.24), rgba(0, 174, 255, 0.22));
  color: #ffffff;
  box-shadow: 0 0 22px rgba(0, 245, 255, 0.28);
}

.primary-btn:hover {
  transform: translateY(-1px);
  background: linear-gradient(90deg, rgba(0, 245, 255, 0.36), rgba(0, 174, 255, 0.32));
  box-shadow: 0 0 28px rgba(0, 245, 255, 0.45);
}

.secondary-btn {
  border: 1px solid rgba(255, 46, 147, 0.55);
  background: rgba(255, 46, 147, 0.14);
  color: #ffffff;
}

.secondary-btn:hover {
  background: rgba(255, 46, 147, 0.24);
  box-shadow: 0 0 18px rgba(255, 46, 147, 0.3);
}

.ghost-btn {
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-main);
}

.ghost-btn:hover {
  border-color: rgba(255, 255, 255, 0.38);
}

.overlay-actions {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.controls-hint {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 14px;
  font-size: 0.72rem;
  color: var(--text-muted);
}

.controls-hint strong {
  color: var(--text-main);
}

.final-score {
  font-family: var(--font-mono);
  font-size: clamp(1.5rem, 3.4vw, 2.1rem);
  font-weight: 800;
  letter-spacing: 0.08em;
  color: #ffffff;
}

.match-stats {
  width: 100%;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-muted);
}

.match-stats strong {
  color: var(--cyan);
}

/* Fixed bottom-right link */
.credit-link {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(8px, env(safe-area-inset-bottom));
  z-index: 40;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  color: rgba(185, 205, 235, 0.72);
  text-decoration: none;
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(6, 9, 19, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.08);
  touch-action: manipulation;
  transition: color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.credit-link:hover {
  color: var(--cyan);
  border-color: rgba(0, 245, 255, 0.45);
  box-shadow: 0 0 12px rgba(0, 245, 255, 0.2);
}

/* Mobile & small screens: overlays fill viewport safely */
@media (max-width: 920px), (max-height: 560px) {
  .overlay {
    position: fixed;
    inset: 0;
    border-radius: 0;
    z-index: 30;
  }

  .icon-btn kbd {
    display: none;
  }
}

/* Portrait phone layout */
@media (orientation: portrait) and (max-width: 900px) {
  .cabinet-shell {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-right: max(8px, env(safe-area-inset-right));
    padding-bottom: max(38px, calc(env(safe-area-inset-bottom) + 30px));
    padding-left: max(8px, env(safe-area-inset-left));
  }

  .cabinet-bezel {
    padding: 10px;
    gap: 10px;
    border-radius: 14px;
  }

  .hud {
    padding: 6px 8px;
    gap: 6px;
  }

  .score-block {
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
  }

  .score-ai {
    flex-direction: column;
    align-items: flex-end;
  }

  .rotate-hint {
    display: flex;
  }
}

/* Landscape phone layout: maximize playfield height */
@media (orientation: landscape) and (max-height: 520px) {
  .cabinet-shell {
    padding-top: max(4px, env(safe-area-inset-top));
    padding-right: max(8px, env(safe-area-inset-right));
    padding-bottom: max(28px, calc(env(safe-area-inset-bottom) + 22px));
    padding-left: max(8px, env(safe-area-inset-left));
  }

  .cabinet-bezel {
    padding: 6px 10px;
    gap: 6px;
    border-radius: 12px;
  }

  .hud {
    padding: 4px 10px;
    gap: 8px;
  }

  .hud-center {
    flex-direction: row;
    gap: 10px;
  }

  .badge-subtle {
    display: none;
  }

  .score-value {
    font-size: 1.55rem;
  }

  .icon-btn {
    min-height: 44px;
    padding: 4px 10px;
  }

  .panel {
    padding: 12px 16px;
    gap: 8px;
  }

  .eyebrow {
    font-size: 0.62rem;
  }

  .title {
    font-size: 1.55rem;
  }

  .overlay-title {
    font-size: 1.45rem;
  }

  .subtitle {
    font-size: 0.76rem;
  }

  .diff-btn {
    min-height: 46px;
    padding: 6px 4px;
  }

  .primary-btn,
  .secondary-btn,
  .ghost-btn {
    min-height: 44px;
    padding: 9px 14px;
    font-size: 0.82rem;
  }

  .overlay-actions {
    gap: 6px;
  }

  .controls-hint {
    font-size: 0.66rem;
  }

  .credit-link {
    font-size: 0.65rem;
    padding: 2px 6px;
    bottom: max(4px, env(safe-area-inset-bottom));
  }
}
