/* ═══════════════════════════════════════════════════════════════════════════
   Breakout — black and green, with the classic brick palette on the court.
   The court is the interface, so everything around it stays small and quiet.
   ═══════════════════════════════════════════════════════════════════════════ */

:root {
  --font-display: 'Bebas Neue', 'DM Sans', system-ui, sans-serif;
  --font-body: 'DM Sans', system-ui, -apple-system, sans-serif;
  --font-mono: 'DM Mono', ui-monospace, 'SF Mono', Menlo, monospace;

  --bg: #000000;
  --surface: #0b0b0b;
  --surface2: #131313;
  --surface3: #1c1c1c;
  --border: #232323;
  --border2: #2f2f2f;

  --text: #f5f5f5;
  --muted: #8a8a8a;

  --green: #1db954;
  --green-bright: #1ed760;
  --green-dim: #0f5c2a;
  --red: #e91429;

  --court-bg: #050505;
  --court-line: #1a3a25;
  --paddle: #1db954;
  --ball: #ffffff;
  --hud-text: #2c4a36;
}

html[data-theme='light'] {
  --bg: #f4f6f4;
  --surface: #ffffff;
  --surface2: #f0f3f0;
  --surface3: #e6eae6;
  --border: #d8ded8;
  --border2: #c6cec6;

  --text: #101410;
  --muted: #5c665c;

  --court-bg: #eaf2ec;
  --court-line: #bcd6c4;
  --paddle: #12833c;
  --ball: #101410;
  --hud-text: #a9c4b3;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

.shell { max-width: 1180px; margin: 0 auto; padding: 14px 16px 44px; }

/* ── Header ────────────────────────────────────────────────────────────── */

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.home-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: .08em;
  color: var(--muted);
  text-decoration: none;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  transition: color .15s, border-color .15s;
}
.home-btn svg { width: 14px; height: 14px; }
.home-btn:hover { color: var(--green); border-color: var(--green-dim); }

.brand { display: flex; align-items: center; gap: 8px; }
.brand-mark { color: var(--green); font-size: 19px; line-height: 1; }
.brand-text { font-family: var(--font-display); font-size: 24px; letter-spacing: 4px; }

.header-right { display: flex; align-items: center; gap: 10px; }
.user-badge { font-family: var(--font-mono); font-size: 11px; color: var(--muted); }

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  color: var(--muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 9px;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn:hover { color: var(--green); border-color: var(--green-dim); }

/* ── Screens ───────────────────────────────────────────────────────────── */

.screen { display: none; }
.screen.active { display: block; }

.screen-title { font-family: var(--font-display); font-size: 34px; letter-spacing: 3px; margin: 24px 0 6px; }
.screen-sub { max-width: 760px; color: var(--muted); font-size: 13.5px; line-height: 1.65; margin: 0 0 20px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.page-head .screen-title { margin-bottom: 0; }

.menu-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; align-items: start; }
@media (max-width: 900px) { .menu-layout { grid-template-columns: minmax(0, 1fr); } }

/* ── Menu ──────────────────────────────────────────────────────────────── */

.menu-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

.mode-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
}
.mode-card:hover { border-color: var(--green-dim); }
.mode-icon { font-size: 26px; line-height: 1; }
.mode-name { font-family: var(--font-display); font-size: 22px; letter-spacing: 2px; }
.mode-desc { flex: 1; color: var(--muted); font-size: 13px; line-height: 1.6; }
.mode-form { display: flex; flex-direction: column; gap: 10px; }

.record { margin-top: 20px; font-family: var(--font-mono); font-size: 12px; color: var(--muted); line-height: 1.9; }

/* The menu demo. Sized in CSS and the canvas resolution matched to that box, so
   the drawing can never feed back into the layout. */
.demo-card { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 10px; }
.demo-canvas { display: block; width: 100%; height: auto; aspect-ratio: 4 / 3; border-radius: 10px; background: var(--court-bg); }
.demo-label { display: block; margin-top: 8px; font-family: var(--font-mono); font-size: 10px; letter-spacing: .1em; color: var(--muted); text-transform: uppercase; }
@media (max-width: 900px) { .demo-card { display: none; } }

/* ── Game ──────────────────────────────────────────────────────────────── */

.status-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 0 14px; }
.status-mode { font-family: var(--font-display); font-size: 19px; letter-spacing: 2px; white-space: nowrap; }
.status-note { flex: 1; min-width: 140px; font-family: var(--font-mono); font-size: 11px; color: var(--muted); }
.status-controls { display: flex; gap: 7px; }

.court-wrap { position: relative; display: flex; justify-content: center; }

/*
  THE COURT CANVAS. CSS owns its size, never its width/height attributes.

  Why `aspect-ratio` is here: a <canvas> has no intrinsic ratio of its own, so
  `height: auto` with no aspect-ratio would take its layout height from the HEIGHT
  ATTRIBUTE — which app.js rewrites from the measured size every frame. That is a
  feedback loop, and it is the bug that once made Tetris's preview boxes grow until
  they covered the screen. Declaring the ratio makes the box depend on the
  stylesheet alone, so the two can never chase each other.

  4 / 3 because the logical court is 800x600.
*/
.court {
  display: block;
  height: min(62vh, 560px);
  width: auto;
  aspect-ratio: 4 / 3;
  background: var(--court-bg);
  border: 1px solid var(--border2);
  border-radius: 10px;
  cursor: none;
  touch-action: none; /* a drag on the court moves the paddle, it does not scroll */
}
@media (max-width: 900px) { .court { width: 100%; height: auto; aspect-ratio: 4 / 3; } }

.centre-message {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-size: 38px;
  letter-spacing: 4px;
  color: var(--green);
  pointer-events: none;
  text-align: center;
  padding: 0 20px;
}

.hint { margin: 12px 0 0; font-family: var(--font-mono); font-size: 11px; color: var(--muted); text-align: center; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ── Buttons and fields ────────────────────────────────────────────────── */

.btn {
  font-family: var(--font-body);
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface3);
  border: 1px solid var(--border2);
  border-radius: 9px;
  padding: 10px 14px;
  cursor: pointer;
  transition: transform .08s, background .15s, border-color .15s;
}
.btn:hover { border-color: var(--green-dim); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--green); outline-offset: 1px; }
.btn-primary { color: #04150a; background: var(--green); border-color: var(--green); }
.btn-primary:hover { background: var(--green-bright); border-color: var(--green-bright); }
.btn-sm { font-size: 12px; padding: 7px 11px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > span {
  font-family: var(--font-mono);
  font-size: 9.5px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--muted);
}

.text-input {
  font-family: var(--font-body);
  font-size: 14px;
  color: var(--text);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 11px;
  width: 100%;
}
.text-input:focus-visible { outline: 2px solid var(--green); outline-offset: 1px; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.panel-k { font-family: var(--font-mono); font-size: 9.5px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.panel-note { font-family: var(--font-mono); font-size: 11px; color: var(--muted); }
.row-controls { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── Level select ──────────────────────────────────────────────────────── */

.level-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); }

.level-btn {
  display: flex;
  flex-direction: column;
  gap: 3px;
  text-align: left;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 11px;
  color: var(--text);
  cursor: pointer;
  font-family: var(--font-body);
  transition: border-color .15s, transform .08s;
}
.level-btn:hover:not(:disabled) { border-color: var(--green); }
.level-btn:active:not(:disabled) { transform: translateY(1px); }
.level-btn:disabled { opacity: .4; cursor: not-allowed; }
.level-num { font-family: var(--font-display); font-size: 20px; letter-spacing: 1px; }
.level-name { font-size: 12px; color: var(--muted); }
.level-tag { font-family: var(--font-mono); font-size: 9.5px; letter-spacing: .1em; text-transform: uppercase; color: var(--green); }

/* ── Builder ───────────────────────────────────────────────────────────── */

.builder-layout { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(260px, .8fr); gap: 18px; align-items: start; }
@media (max-width: 900px) { .builder-layout { grid-template-columns: minmax(0, 1fr); } }

.builder-grid-wrap { background: var(--court-bg); border: 1px solid var(--border2); border-radius: 12px; padding: 12px; }
.builder-grid { display: grid; gap: 2px; grid-template-columns: repeat(8, 1fr); }
.builder-cell {
  aspect-ratio: 7 / 2;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--surface2);
  cursor: crosshair;
  padding: 0;
}
.builder-cell:hover { border-color: var(--green); }

.palette { display: flex; flex-wrap: wrap; gap: 6px; }
.palette-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 9px;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.palette-btn.active { border-color: var(--green); box-shadow: 0 0 0 1px var(--green) inset; }
.palette-swatch { width: 14px; height: 10px; border-radius: 2px; }

/* ── Library ───────────────────────────────────────────────────────────── */

.library-list { display: flex; flex-direction: column; gap: 10px; }
.library-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
}
.library-name { font-family: var(--font-display); font-size: 18px; letter-spacing: 1.5px; }
.library-meta { flex: 1; min-width: 120px; font-family: var(--font-mono); font-size: 11px; color: var(--muted); }
.library-actions { display: flex; gap: 8px; }
.empty-note { font-family: var(--font-mono); font-size: 12px; color: var(--muted); padding: 10px 0; }

/* ── Overlay ───────────────────────────────────────────────────────────── */

.overlay {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  background: rgba(0, 0, 0, .72);
  z-index: 40;
  padding: 20px;
}
.overlay.show { display: grid; }

.overlay-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 16px;
  padding: 26px 22px;
  text-align: center;
}
.overlay-icon { font-size: 38px; line-height: 1; }
.overlay-card h2 { font-family: var(--font-display); font-size: 30px; letter-spacing: 3px; margin: 10px 0 4px; }
.overlay-card h2.win { color: var(--green); }
.overlay-card h2.lose { color: var(--red); }
.overlay-card p { color: var(--muted); font-size: 13px; line-height: 1.6; margin: 0 0 18px; }
.overlay-actions { display: flex; gap: 9px; justify-content: center; flex-wrap: wrap; }

/* ── Settings and toast ────────────────────────────────────────────────── */

.settings-dropdown {
  position: fixed;
  top: 58px;
  right: max(16px, calc(50vw - 590px + 16px));
  display: none;
  flex-direction: column;
  min-width: 190px;
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 12px;
  overflow: hidden;
  z-index: 50;
}
.settings-dropdown.open { display: flex; }

.dropdown-item { font-size: 13px; padding: 11px 14px; cursor: pointer; border-bottom: 1px solid var(--border); }
.dropdown-item:last-child { border-bottom: 0; }
.dropdown-item:hover { background: var(--surface2); color: var(--green); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 14px);
  max-width: calc(100vw - 32px);
  padding: 11px 16px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface3);
  border: 1px solid var(--border2);
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 60;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-color: var(--red); color: #ffd9dd; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
