:root {
  --bg: #0b0b1a;
  --panel: #13132e;
  --edge: #4b4b9a;
  --edge-dark: #1d1d44;
  --text: #e8e8ff;
  --dim: #8a8ac0;
  --yellow: #ffec27;
  --blue: #29adff;
  --pink: #ff77a8;
  --green: #00e436;
  --font: 'Press Start 2P', ui-monospace, 'Courier New', monospace;
  --u: clamp(7px, calc(1.1vmin + 3px), 13px);
  --b: clamp(44px, min(8.5vh, 15vw), 76px);
  --gap: clamp(6px, 1.2vmin, 12px);
  --cell: 24px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  position: fixed;
  inset: 0;
  touch-action: none;
  background:
    radial-gradient(ellipse at 50% 40%, #1a1a44 0%, var(--bg) 70%),
    var(--bg);
}

/* CRT scanlines + vignette */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 100;
  background:
    repeating-linear-gradient(to bottom, transparent 0 2px, rgba(0, 0, 0, .22) 2px 3px),
    radial-gradient(ellipse at center, transparent 60%, rgba(0, 0, 0, .55) 100%);
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

#app {
  position: absolute;
  inset: 0;
  margin: 0 auto;
  max-width: 100%;
  padding:
    max(var(--gap), env(safe-area-inset-top))
    max(var(--gap), env(safe-area-inset-right))
    max(var(--gap), env(safe-area-inset-bottom))
    max(var(--gap), env(safe-area-inset-left));
  display: grid;
  gap: var(--gap);
  font-size: var(--u);
  grid-template-columns: repeat(6, minmax(0, 1fr));
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "hold hold stats stats next next"
    "board board board board board board"
    "pad pad pad act act act";
}

/* ---------- panels ---------- */
.panel {
  background: var(--panel);
  border: 3px solid var(--edge);
  box-shadow: 0 0 0 3px var(--edge-dark), 4px 4px 0 3px #000;
  padding: calc(var(--u) * .8);
  min-width: 0;
  min-height: 0;
  margin: 3px;
}

.panel h2 {
  font-size: 1em;
  font-weight: normal;
  color: var(--pink);
  letter-spacing: .1em;
  margin-bottom: .6em;
  text-shadow: 2px 2px 0 #000;
}

#hold-panel { grid-area: hold; display: flex; flex-direction: column; }
#next-panel { grid-area: next; display: flex; flex-direction: column; }
#hold, #next {
  display: block;
  width: 100%;
  height: clamp(32px, 7.5vh, 80px);
}

#stats {
  grid-area: stats;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .7em .5em;
  align-content: start;
}
.stat h2 { margin-bottom: .35em; font-size: .85em; color: var(--dim); }
.stat output {
  display: block;
  color: var(--yellow);
  text-shadow: 2px 2px 0 #000;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}
.toolbar {
  grid-column: 1 / -1;
  display: flex;
  gap: .6em;
}
.tool {
  flex: 1;
  min-height: 28px;
  background: #26265a;
  border: 2px solid #000;
  box-shadow: inset -3px -3px 0 rgba(0,0,0,.45), inset 3px 3px 0 rgba(255,255,255,.12);
  color: var(--blue);
  padding: .3em;
}
.tool:active { box-shadow: inset 3px 3px 0 rgba(0,0,0,.45); }
.tool.off { color: #555; }

/* ---------- board ---------- */
#board-wrap {
  grid-area: board;
  position: relative;
  min-height: 0;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#board-box {
  position: relative;
  box-shadow:
    0 0 0 3px var(--edge),
    0 0 0 6px var(--edge-dark),
    0 0 0 9px #000,
    0 0 40px 8px rgba(41, 173, 255, .18);
  touch-action: none;
}
#board { display: block; width: 100%; height: 100%; }

/* ---------- overlay ---------- */
#overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 5, 22, .86);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: clamp(7px, calc(var(--cell) * .38), 14px);
  overflow: auto;
}
#overlay[hidden] { display: none; }
.screen {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 1.3em;
  padding: 1em;
  width: 100%;
}
#overlay[data-screen="title"] .title,
#overlay[data-screen="paused"] .paused,
#overlay[data-screen="over"] .over { display: flex; }

.screen h1 {
  font-size: clamp(16px, calc(var(--cell) * .95), 44px);
  font-weight: normal;
  line-height: 1.2;
  color: var(--yellow);
  text-shadow: 3px 3px 0 var(--pink), 6px 6px 0 #000;
  letter-spacing: .08em;
}
.screen button {
  background: #26265a;
  border: 3px solid #000;
  box-shadow: inset -4px -4px 0 rgba(0,0,0,.45), inset 4px 4px 0 rgba(255,255,255,.12);
  padding: .9em 1.2em;
  min-height: 36px;
  color: var(--text);
}
.screen button:hover, .screen button:focus-visible { color: var(--yellow); outline: none; }
.screen button:active { box-shadow: inset 4px 4px 0 rgba(0,0,0,.45); }
.screen button.big {
  font-size: 1.3em;
  background: #b0124a;
  color: #fff;
  padding: 1em 1.6em;
}
.screen button[hidden] { display: none; }
.opts { display: flex; flex-direction: column; gap: .6em; width: min(90%, 22em); }
.opts button { width: 100%; }
.hint { color: var(--dim); line-height: 1.6; }
.new-hi { color: var(--green); }
.final {
  display: grid;
  grid-template-columns: auto auto;
  gap: .7em 1.5em;
  text-align: left;
}
.final dt { color: var(--dim); }
.final dd { color: var(--yellow); text-align: right; }

.blink { animation: blink 1s steps(1) infinite; color: var(--blue); }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- touch controls ---------- */
.controls { display: grid; gap: calc(var(--b) * .12); align-content: center; }
#pad {
  grid-area: pad;
  justify-content: start;
  grid-template-columns: repeat(3, var(--b));
  grid-template-rows: repeat(2, var(--b));
  grid-template-areas: ". up ." "left down right";
}
#act {
  grid-area: act;
  justify-content: end;
  grid-template-columns: repeat(3, var(--b));
  grid-template-rows: repeat(2, var(--b));
  grid-template-areas: "hold . a" ". b .";
}
.btn {
  width: 100%;
  height: 100%;
  background: #2b2b66;
  border: 3px solid #000;
  box-shadow: inset -5px -5px 0 rgba(0,0,0,.45), inset 5px 5px 0 rgba(255,255,255,.14);
  color: var(--text);
  font-size: calc(var(--b) * .34);
  line-height: 1;
  touch-action: none;
}
.btn.small { font-size: calc(var(--b) * .18); color: var(--pink); }
.btn.round { border-radius: 50%; font-size: calc(var(--b) * .45); }
.btn.a { background: #b0124a; }
.btn.b { background: #1f6fb0; }
.btn.pressed, .btn:active {
  box-shadow: inset 5px 5px 0 rgba(0,0,0,.5);
  filter: brightness(1.25);
}

#keys { grid-area: act; display: none; }
#keys ul { list-style: none; display: grid; gap: .8em; line-height: 1.4; color: var(--dim); }
kbd { font-family: inherit; color: var(--blue); }

/* Desktop / mouse: hide touch pads and show key help. A real touch flips this back. */
@media (hover: hover) and (pointer: fine) {
  body:not(.touch) .controls { display: none; }
  body:not(.touch) #keys { display: block; }
  body:not(.touch) #app { grid-template-areas:
    "hold hold stats stats next next"
    "board board board board board board"
    "act act act act act act"; }
}

/* ---------- landscape / wide ---------- */
@media (min-aspect-ratio: 1/1) {
  :root { --b: clamp(44px, min(13vh, 10vw), 84px); }
  #app {
    max-width: calc(100vh * 1.5 + 200px);
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "hold  board next"
      "stats board next"
      ".     board next"
      "pad   board act";
  }
  @supports (height: 100dvh) {
    #app { max-width: calc(100dvh * 1.5 + 200px); }
  }
  #hold { height: auto; aspect-ratio: 2 / 1; }
  #next { flex: 1; height: auto; min-height: 60px; }
  #next-panel { align-self: stretch; }
  #pad { justify-content: center; }
  #act { justify-content: center; }
  #keys { align-self: end; }
}
@media (min-aspect-ratio: 1/1) and (hover: hover) and (pointer: fine) {
  body:not(.touch) #app {
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
      "hold  board next"
      "stats board next"
      "act   board next"; }
  body:not(.touch) #keys { align-self: start; }
}

/* very short landscape phones */
@media (min-aspect-ratio: 1/1) and (max-height: 430px) {
  :root { --u: clamp(6px, calc(1.6vh + 1px), 10px); --b: clamp(40px, 17vh, 64px); }
  .panel { padding: .5em; }
  #hold { aspect-ratio: 3 / 1; }
}

@media (prefers-reduced-motion: reduce) {
  .blink { animation: none; }
}

/* =====================================================================
   THEME: LCD — old black & white handheld LCD, single ink, no colour.
   ===================================================================== */
:root[data-theme="lcd"] {
  --bg: #d4d1c9;
  --panel: #c5cab4;
  --edge: #4a4d52;
  --edge-dark: #2e3034;
  --text: #22261d;
  --dim: #545a4a;
  --yellow: #22261d;
  --blue: #22261d;
  --pink: #22261d;
  --green: #22261d;
  --ink: #22261d;
  --lcd: #c5cab4;
  --ghost: rgba(34, 38, 29, .2);
  color-scheme: light;
}
[data-theme="lcd"] body {
  background:
    repeating-linear-gradient(90deg, rgba(0,0,0,.018) 0 2px, transparent 2px 4px),
    linear-gradient(160deg, #e2dfd8 0%, #cbc8c0 100%);
}
/* no CRT scanlines on an LCD — just a soft shade at the edges */
[data-theme="lcd"] body::after {
  background: radial-gradient(ellipse at center, transparent 70%, rgba(0, 0, 0, .12) 100%);
}

[data-theme="lcd"] .panel {
  background:
    repeating-linear-gradient(0deg, rgba(34,38,29,.035) 0 1px, transparent 1px 3px),
    repeating-linear-gradient(90deg, rgba(34,38,29,.035) 0 1px, transparent 1px 3px),
    var(--lcd);
  border: 4px solid var(--edge);
  box-shadow: 0 0 0 3px var(--edge-dark), inset 2px 2px 0 rgba(0,0,0,.18);
}
[data-theme="lcd"] .panel h2,
[data-theme="lcd"] .stat h2,
[data-theme="lcd"] .stat output,
[data-theme="lcd"] #keys ul,
[data-theme="lcd"] kbd {
  color: var(--ink);
  text-shadow: 2px 2px 0 var(--ghost);
}
[data-theme="lcd"] .stat h2 { opacity: .7; }

[data-theme="lcd"] #board-box {
  box-shadow:
    0 0 0 4px var(--edge),
    0 0 0 10px var(--edge-dark),
    0 0 0 12px #1c1d20,
    6px 8px 0 12px rgba(0, 0, 0, .18);
}

[data-theme="lcd"] #overlay { background: rgba(197, 202, 180, .93); }
[data-theme="lcd"] .screen h1 {
  color: var(--ink);
  text-shadow: 3px 3px 0 var(--ghost);
}
[data-theme="lcd"] .blink,
[data-theme="lcd"] .hint,
[data-theme="lcd"] .new-hi,
[data-theme="lcd"] .final dt,
[data-theme="lcd"] .final dd { color: var(--ink); }
[data-theme="lcd"] .screen button {
  background: var(--lcd);
  color: var(--ink);
  border: 3px solid var(--ink);
  box-shadow: 3px 3px 0 var(--ghost);
}
[data-theme="lcd"] .screen button:hover,
[data-theme="lcd"] .screen button:focus-visible { background: var(--ink); color: var(--lcd); }
[data-theme="lcd"] .screen button:active { transform: translate(2px, 2px); box-shadow: none; }
[data-theme="lcd"] .screen button.big { background: var(--ink); color: var(--lcd); }

/* hardware buttons: charcoal rubber on a grey case */
[data-theme="lcd"] .btn,
[data-theme="lcd"] .tool {
  background: #3b3e44;
  color: #d4d1c9;
  border: 3px solid #1c1d20;
  box-shadow: inset -4px -4px 0 rgba(0,0,0,.35), inset 4px 4px 0 rgba(255,255,255,.12), 2px 3px 0 rgba(0,0,0,.25);
}
[data-theme="lcd"] .btn.small { color: #d4d1c9; }
[data-theme="lcd"] .btn.a,
[data-theme="lcd"] .btn.b { background: #2a2c30; }
[data-theme="lcd"] .btn.pressed,
[data-theme="lcd"] .btn:active,
[data-theme="lcd"] .tool:active {
  box-shadow: inset 4px 4px 0 rgba(0,0,0,.45);
  filter: none;
  transform: translateY(2px);
}
[data-theme="lcd"] .tool.off { color: #77797d; }
