/* Tetris — game-specific styles. Layered on common.css.
   Design tokens (var(--bg-1), var(--gold), etc.) come from common.css. */

/* ── Tile sizing ───────────────────────────────────────────── */

:root {
  /* Playfield cell size: bounded by viewport height (since the 20-row
     playfield is the binding vertical constraint) and viewport width
     (so we don't overflow on phones). */
  --tile: clamp(14px, min(3.7vh, 6.1vw), 29px);
  --mini-tile: calc(var(--tile) * 0.58);
  --cell-gap: 1px;
  --piece-color: rgba(255, 255, 255, 0.04);
}

/* ── Per-piece colour custom-property assignment ───────────── */
/* Both locked-cell (.piece-X) and overlay (.overlay-X) use the same
   --piece-color so we only need one rule to actually paint. */

.cell.piece-I, .cell.overlay-I { --piece-color: #00b7eb; }
.cell.piece-O, .cell.overlay-O { --piece-color: #f0d000; }
.cell.piece-T, .cell.overlay-T { --piece-color: #a000a0; }
.cell.piece-S, .cell.overlay-S { --piece-color: #00a000; }
.cell.piece-Z, .cell.overlay-Z { --piece-color: #a00000; }
.cell.piece-J, .cell.overlay-J { --piece-color: #2030c0; }
.cell.piece-L, .cell.overlay-L { --piece-color: #f08000; }

/* ── main / game-area ──────────────────────────────────────── */

main#gameMain {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 12px 12px;
  position: relative;
  z-index: 2;
  min-height: 0;
}

main#gameMain[inert] {
  filter: blur(2px);
  pointer-events: none;
}

.game-area {
  display: grid;
  grid-template-columns: auto auto auto;
  justify-content: center;
  align-items: start;
  column-gap: 10px;
  margin-bottom: 10px;
}

/* ── Sidebars ──────────────────────────────────────────────── */

.sidebar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 0 2px;
}

.panel-label {
  font-family: var(--serif);
  font-style: italic;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--gold-dim);
}

.sidebar .mini-grid + .mini-grid { margin-top: 4px; }

.sidebar .meta {
  margin-top: 8px;
  display: grid;
  grid-template-columns: 1fr;
  text-align: center;
}
.sidebar .meta dt {
  font-family: var(--serif);
  font-style: italic;
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--gold-dim);
}
.sidebar .meta dd {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: var(--card);
  font-size: 16px;
  margin-top: 2px;
}

/* ── Mini-grid (used by Hold + 3× Next) ────────────────────── */

.mini-grid {
  display: grid;
  grid-template-columns: repeat(4, var(--mini-tile));
  grid-template-rows:    repeat(4, var(--mini-tile));
  gap: 1px;
  background: var(--bg-3);
  border: 1px solid var(--gold-faint);
  padding: 2px;
  border-radius: 2px;
}
.mini-grid .cell {
  /* Mini-grid cells are smaller; inherit colour rules from .cell. */
  background: var(--piece-color);
  border-radius: 1px;
}

/* ── Playfield ─────────────────────────────────────────────── */

.playfield-wrap {
  position: relative;
  padding-left: 8px; /* room for the left-edge flag stripe */
}

/* American flag — slim red/white/blue stripe running the full
   height of the playfield border. Standing design requirement. */
.playfield-wrap::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 6px;
  border-radius: 2px;
  background: linear-gradient(
    180deg,
    #B22234 0%, #B22234 33.33%,
    #ffffff 33.33%, #ffffff 66.66%,
    #3C3B6E 66.66%, #3C3B6E 100%
  );
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
}

.playfield {
  display: grid;
  grid-template-columns: repeat(10, var(--tile));
  grid-template-rows:    repeat(20, var(--tile));
  gap: var(--cell-gap);
  background: var(--bg-3);
  border: 2px solid var(--gold-faint);
  padding: 2px;
  border-radius: 2px;
  box-shadow:
    inset 0 0 30px rgba(0, 0, 0, 0.4),
    0 8px 30px rgba(0, 0, 0, 0.45);
}

/* ── Cells (shared between playfield + mini-grids) ─────────── */

.cell {
  background: var(--piece-color);
  border-radius: 2px;
  position: relative;
}

/* Empty cells get the subtle indigo wash; the piece overrides this. */
.cell:not(.piece-I):not(.piece-O):not(.piece-T):not(.piece-S):not(.piece-Z):not(.piece-J):not(.piece-L):not(.active):not(.ghost) {
  background: rgba(255, 255, 255, 0.03);
}

/* Locked piece cells — full colour + inset highlight + 3D bevel. */
.cell[class*="piece-"] {
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.35),
    inset -1px -1px 0 rgba(0, 0, 0, 0.35);
}

/* Active piece overlay — same colour as locked, slightly brighter. */
.cell.active {
  background: var(--piece-color);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.5),
    inset -1px -1px 0 rgba(0, 0, 0, 0.25),
    0 0 6px rgba(255, 255, 255, 0.18);
}

/* Ghost overlay — outline only, no fill. Wins where active also lives
   gets overridden because the JS removes 'ghost' before adding 'active'. */
.cell.ghost {
  background: transparent;
  box-shadow:
    inset 0 0 0 1.5px var(--piece-color);
  opacity: 0.85;
}

/* ── HUD (header) ──────────────────────────────────────────── */

/* HUD: shared .hud row of stacked .stat (label + tabular Oswald value),
   matching the card games. */
.flag-mark { display: inline-flex; align-items: center; }
.flag-mark svg { width: 26px; height: 17px; border-radius: 1px; }

/* ── Centre banner (T-spin / B2B / LEVEL flashes) ──────────── */

.banner {
  position: absolute;
  top: 24%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.85);
  font-family: var(--serif);
  font-style: italic;
  font-size: clamp(18px, 4.5vh, 30px);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--gold-bright);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8), 0 0 12px rgba(201, 169, 97, 0.4);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.18s ease;
  white-space: nowrap;
  z-index: 5;
}
.banner.visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

/* ── Transient effects (lock pulse, line-clear flash, level flash) ─── */

.cell.flash-clear {
  animation: tetris-flash-clear 150ms ease-out;
}
.cell.pulse-lock {
  animation: tetris-pulse-lock 120ms ease-out;
}
.hud .value.flash-level {
  animation: tetris-flash-level 700ms ease-out;
}

@keyframes tetris-flash-clear {
  0%   { background: #ffffff; box-shadow: 0 0 16px rgba(255, 255, 255, 0.9); }
  100% { background: rgba(255, 255, 255, 0.03); box-shadow: none; }
}
@keyframes tetris-pulse-lock {
  0%   { filter: brightness(1.6); }
  100% { filter: brightness(1); }
}
@keyframes tetris-flash-level {
  0%   { color: var(--gold-bright); transform: scale(1); text-shadow: 0 0 12px var(--gold-bright); }
  30%  { transform: scale(1.4); text-shadow: 0 0 24px var(--gold-bright); }
  100% { color: var(--card); transform: scale(1); text-shadow: none; }
}

@media (prefers-reduced-motion: reduce) {
  .cell.flash-clear, .cell.pulse-lock, .hud .value.flash-level,
  .banner, .overlay, .overlay-panel {
    animation: none !important;
    transition: none !important;
  }
}

/* ── Mobile controls ───────────────────────────────────────── */

.mobile-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin: 4px 0 8px;
  width: 100%;
  max-width: 360px;
}

.mc-row {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.mc-btn {
  min-width: 44px;
  min-height: 44px;
  padding: 0 10px;
  background: var(--bg-2);
  color: var(--card);
  border: 1px solid var(--gold-faint);
  border-radius: 6px;
  font-family: var(--sans);
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.04em;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.08s ease, transform 0.08s ease;
  touch-action: manipulation;
}
.mc-btn:active,
.mc-btn.pressed {
  background: var(--gold);
  color: var(--bg-1);
  transform: scale(0.95);
  border-color: var(--gold);
}

.mc-row-top .mc-hold,
.mc-row-top .mc-drop {
  flex: 1 1 0;
  min-height: 48px;
  font-size: 14px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.mc-row-bottom .mc-btn {
  flex: 1 1 0;
  max-width: 60px;
}

/* ── Game controls bar ─────────────────────────────────────── */

.game-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.game-controls .btn {
  padding: 9px 18px;
  font-size: 10px;
}
.game-controls .btn.confirming {
  background: var(--red);
  border-color: var(--red);
  color: var(--card);
}

/* Inline start-level selector in the controls bar */
.ctrl-level {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--sans);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold-bright);
  cursor: default;
}
.ctrl-level select {
  background: var(--bg-3);
  color: var(--card);
  border: 1px solid var(--gold-faint);
  padding: 4px 6px;
  border-radius: 3px;
  font-family: var(--sans);
  font-size: 13px;
  cursor: pointer;
}

/* Settings <details>/<summary> */
.settings {
  position: relative;
}
.settings summary {
  list-style: none;
  cursor: pointer;
  padding: 9px 14px;
  border: 1px solid var(--gold-faint);
  background: transparent;
  color: var(--gold-bright);
  font-family: var(--sans);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}
.settings summary::-webkit-details-marker { display: none; }
.settings[open] summary { background: rgba(201, 169, 97, 0.1); }

.settings-panel {
  position: absolute;
  right: 0;
  margin-top: 6px;
  background: var(--bg-2);
  border: 1px solid var(--gold-faint);
  border-radius: 4px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 200px;
  /* Stay inside the viewport on narrow phones — without this clamp
     the absolute-positioned panel can overflow the right edge. */
  max-width: calc(100vw - 16px);
  z-index: 30;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}
.setting-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--card);
  cursor: pointer;
}
.setting-row select {
  margin-left: auto;
  background: var(--bg-3);
  color: var(--card);
  border: 1px solid var(--gold-faint);
  padding: 4px 8px;
  border-radius: 3px;
  font-family: var(--sans);
}

/* ── Overlays (pause + game-over) ──────────────────────────── */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 38, 0.78);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 20px;
  /* Re-assert visibility over common.css's shared .overlay base
     (which hides via opacity for its .show-toggled overlays).
     Tetris toggles pause / game-over with [hidden]. */
  opacity: 1;
  pointer-events: auto;
}
.overlay[hidden] { display: none; }

.overlay-panel {
  background:
    radial-gradient(ellipse at top, rgba(86, 64, 138, 0.42) 0%, transparent 70%),
    var(--panel);
  border: 1px solid var(--gold);
  padding: 36px 40px;
  border-radius: var(--r-lg);
  text-align: center;
  max-width: 92%;
  position: relative;
  box-shadow: var(--shadow-3);
  min-width: 280px;
}
.overlay-panel h2 {
  font-family: var(--serif);
  font-size: clamp(28px, 6vw, 44px);
  font-style: italic;
  font-weight: 500;
  color: var(--gold-bright);
  margin-bottom: 6px;
}
.overlay-panel h3 {
  font-family: var(--serif);
  font-style: italic;
  font-size: 13px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--gold-dim);
  margin: 18px 0 8px;
}
.overlay-panel p {
  font-family: var(--serif);
  font-style: italic;
  font-size: 16px;
  color: rgba(245, 237, 224, 0.85);
  margin-bottom: 16px;
}
.overlay-panel .btn { margin-top: 16px; }
.overlay-panel .reason {
  font-style: italic;
  color: var(--gold-bright);
  margin-bottom: 12px;
  font-size: 15px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.overlay-dismiss {
  position: absolute;
  top: 10px; right: 12px;
  background: none;
  border: none;
  color: rgba(245, 237, 224, 0.5);
  font-size: 18px;
  cursor: pointer;
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  line-height: 1;
}
.overlay-dismiss:hover { color: var(--gold-bright); }

.final-stats {
  display: grid;
  grid-template-columns: auto auto;
  gap: 6px 18px;
  justify-content: center;
  margin: 8px 0 4px;
}
.final-stats dt {
  font-family: var(--serif);
  font-style: italic;
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--gold-dim);
  text-align: right;
}
.final-stats dd {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: var(--card);
  font-size: 18px;
  text-align: left;
}

.bests-list {
  max-height: 180px;
  overflow-y: auto;
  text-align: left;
  font-size: 12px;
  font-family: var(--sans);
  color: rgba(245, 237, 224, 0.85);
  line-height: 1.5;
}
.bests-list ol {
  list-style: decimal inside;
  padding-left: 4px;
}
.bests-list li { padding: 2px 0; }

/* ── Install banner (ADR-003) — same pattern as Word Jumbles ── */

/* The install banner is now the shared #pwa-banner (common.css), injected by
   ../pwa-install.js; the old .install-banner toast styles + per-game
   install-visible padding were removed (the shared rule covers it). (BL-022) */

/* ── Responsive ────────────────────────────────────────────── */

@media (max-width: 640px) {
  :root { --tile: clamp(13px, min(3.6vh, 6.1vw), 23px); }
  main#gameMain { padding: 4px 8px 8px; }
  .game-area { column-gap: 6px; }
  .panel-label { font-size: 10px; }
  .hud { gap: 10px; }
  .hud .value { font-size: 14px; }
  .overlay-panel { padding: 28px 22px; }
  .game-controls .btn { padding: 8px 12px; font-size: 9px; }
}

@media (max-width: 360px) {
  :root { --tile: clamp(11px, 5vw, 20px); }
}

@media (orientation: landscape) and (max-height: 540px) {
  /* Landscape phones: keep the touch bar visible (no physical keyboard
     on most phones) but compress it. Smaller tile + tighter padding +
     more compact buttons so the playfield + controls both fit.
     Trade-off: buttons drop to 36 × 36 px (below WCAG 2.5.5's 44 px
     recommendation) — accepted in this orientation so phones aren't
     locked out entirely. (v0.4.0 hid the bar with display:none, which
     left landscape phone users unable to play.) */
  :root { --tile: clamp(9px, 3.2vh, 16px); }
  main#gameMain { padding: 2px 6px 4px; }
  .game-area { column-gap: 4px; margin-bottom: 4px; }
  .panel-label, .sidebar .meta dt { font-size: 9px; }
  .sidebar .meta dd { font-size: 12px; }
  .mobile-controls { gap: 4px; margin: 2px 0; max-width: 280px; }
  .mc-row { gap: 6px; }
  .mc-btn {
    min-width: 36px;
    min-height: 36px;
    padding: 0 6px;
    font-size: 14px;
  }
  .mc-row-top .mc-hold,
  .mc-row-top .mc-drop {
    min-height: 36px;
    font-size: 10px;
    letter-spacing: 0.08em;
  }
  .mc-row-bottom .mc-btn { max-width: 48px; }
  .game-controls { gap: 6px; }
  .game-controls .btn { padding: 6px 10px; font-size: 9px; }
}

/* Pointer-fine devices (desktop with mouse) don't need a touch bar
   either — keyboard is the primary input. Still useful for testing
   touch flows in DevTools, so we keep it visible by default. */
@media (hover: hover) and (pointer: fine) and (min-width: 720px) {
  .mobile-controls { display: none; }
}
