:root {
  --bg: #14121f;
  --surface: #1f1c2e;
  --surface-2: #2a2640;
  --text: #f4f2ff;
  --muted: #a39fc0;
  --accent: #fdd835;
  --radius: 18px;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

button { font: inherit; color: inherit; cursor: pointer; }

/* Navigation à la télécommande / au clavier : le focus doit se voir de loin. */
button:focus-visible,
.icon-btn:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}
button:disabled { cursor: not-allowed; opacity: 0.35; }

/* --- Réglages ------------------------------------------------------------- */

.setup {
  max-width: 640px;
  margin: 0 auto;
  padding: 24px 16px 48px;
  display: grid;
  gap: 16px;
}

.setup__head { text-align: center; }

.logo {
  margin: 0;
  font-size: clamp(3rem, 14vw, 5rem);
  font-weight: 900;
  letter-spacing: 0.02em;
}
.logo span { color: var(--c); }

.tagline { margin: 0; color: var(--muted); font-size: 1.1rem; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 20px 20px;
}

.card h2 {
  font-size: 1rem;
  margin: 16px 0 10px;
  color: var(--muted);
  font-weight: 700;
}
.card h2:first-child { margin-top: 0; }
.card h2 small { font-weight: 400; }

.segmented {
  display: flex;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 4px;
  gap: 4px;
}
.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  border-radius: 999px;
  padding: 10px 8px;
  font-weight: 700;
  font-size: 1.05rem;
}
.segmented button[aria-checked="true"] { background: var(--text); color: var(--bg); }

/* Grille commune à l'éditeur et au jeu : --side = 2 ou 3. */
.grid {
  display: grid;
  grid-template-columns: repeat(var(--side), 1fr);
  aspect-ratio: 1;
}
.cell { background: var(--c); }

.layout-editor { display: flex; align-items: center; gap: 16px; margin-top: 12px; }
.layout-editor .hint { margin: 0; }
.grid--editor { width: 132px; flex: none; gap: 6px; }
.grid--editor .cell {
  border-radius: 10px;
  cursor: pointer;
  outline-offset: 2px;
}
.grid--editor .cell.is-selected { outline: 3px solid var(--text); transform: scale(0.9); }
.grid--editor .cell:focus-visible { outline: 3px solid var(--accent); }

.levels { width: 100%; border-collapse: collapse; }
.levels th, .levels td { padding: 8px 4px; text-align: left; vertical-align: top; }
.levels thead th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.levels tbody th { font-size: 1.3rem; width: 2.5em; padding-top: 12px; }
.levels tbody tr + tr { border-top: 1px solid var(--surface-2); }

.field { display: inline-flex; align-items: center; gap: 6px; }
.field input {
  width: 5.5em;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--surface-2);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.hint { display: block; color: var(--muted); font-size: 0.8rem; margin-top: 4px; }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  padding: 8px;
  border-radius: 8px;
}
.icon-btn:not(:disabled):hover { background: var(--surface-2); color: var(--text); }

.levels__actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.total { margin: 12px 0 0; color: var(--muted); }

.btn {
  border: 0;
  border-radius: 999px;
  padding: 12px 20px;
  font-weight: 700;
}
.btn--ghost { background: var(--surface-2); }
.btn--start {
  background: var(--accent);
  color: #1b1600;
  font-size: 1.5rem;
  padding: 18px 28px;
  font-weight: 900;
}

/* --- Réglages sur écran large (télé 16:9) ----------------------------------
 * Deux colonnes qui tiennent dans la hauteur de l'écran : pas de défilement à la
 * télécommande. Tailles en vh pour rester identiques en 960x540 comme en 1920x1080.
 */
@media (min-width: 900px) and (min-aspect-ratio: 4/3) {
  html { font-size: clamp(14px, 2vh, 28px); }

  .setup {
    max-width: none;
    height: 100vh;
    padding: 4vh 4vw;
    gap: 3vh 3vw;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr);
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "head   levels"
      "colors levels"
      "start  levels";
  }

  .setup__head { grid-area: head; }
  .card--colors { grid-area: colors; }
  .btn--start { grid-area: start; }

  .card--levels {
    grid-area: levels;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }
  /* Au-delà de ce qui tient à l'écran (beaucoup de niveaux), seul le tableau défile. */
  .levels__scroll { flex: 1; min-height: 0; overflow-y: auto; }

  .logo { font-size: 10vh; line-height: 1; }
  .card { padding: 2.5vh 1.5vw; border-radius: 2vh; }
  .card h2 { margin: 2vh 0 1.2vh; }
  .grid--editor { width: 22vh; gap: 0.8vh; }
  .grid--editor .cell { border-radius: 1.2vh; }

  .segmented button { padding: 1.2vh 1em; }
  .levels th, .levels td { padding: 0.8vh 0.6vw; vertical-align: middle; }
  .levels tbody th { padding-top: 0.8vh; }
  .levels .hint { display: inline; margin: 0 0 0 1em; }
  .field input { padding: 0.8vh 0.6em; }
  .btn { padding: 1.2vh 1.4em; }
  .btn--start { padding: 2.5vh 2em; font-size: 2rem; }
}

/* --- Jeu ------------------------------------------------------------------ */

.play {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: #0b0a12;
}

.hud {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  color: var(--muted);
}
.hud__level { font-weight: 700; white-space: nowrap; }
.hud__bar { flex: 1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.hud__bar div { height: 100%; width: 0; background: var(--muted); }
.hud__btn {
  border: 0;
  background: var(--surface-2);
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.stage {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 2vmin;
  padding-bottom: max(2vmin, env(safe-area-inset-bottom));
  min-height: 0;
  container-type: size;
}

/* Grille carrée la plus grande possible ; cases éteintes = noir, comme sur la télé. */
.grid--play {
  width: min(100cqw, 100cqh);
  gap: 1.5vmin;
}
.grid--play .cell {
  background: #1a1826;
  border-radius: 2vmin;
}
.grid--play .cell.is-lit {
  background: var(--c);
  animation: pop 220ms cubic-bezier(0.2, 1.4, 0.4, 1);
}

@keyframes pop {
  from { transform: scale(0.85); opacity: 0.4; }
  to { transform: scale(1); opacity: 1; }
}

.announce {
  place-self: center;
  text-align: center;
}
.announce p { margin: 0; }
.announce__title { font-size: clamp(3rem, 14vmin, 9rem); font-weight: 900; }
.announce__count { font-size: clamp(5rem, 30vmin, 18rem); font-weight: 900; color: var(--accent); line-height: 1; }
.announce__sub { color: var(--muted); font-size: clamp(1rem, 3vmin, 1.6rem); }
.announce__actions { display: flex; gap: 12px; justify-content: center; margin-top: 24px; }

.play.is-paused .stage { filter: grayscale(0.7) brightness(0.5); }
.play.is-paused .stage::after {
  content: "Pause";
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: clamp(3rem, 14vmin, 9rem);
  font-weight: 900;
  color: var(--text);
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  .grid--play .cell.is-lit { animation: none; }
}
