:root {
  --bg:        #0a0a0e;
  --panel:    #11121a;
  --panel-2:  #181923;
  --line:     #232531;
  --ink:      #e8e6e1;
  --ink-dim:  #6b6d7a;
  --ink-mute: #3f4150;
  --accent:   #f5c842;
  --warn:     #ff5c5c;
  --good:     #5cffaa;

  --p-i: #4cc9d6;
  --p-o: #e6c84a;
  --p-t: #b266e0;
  --p-s: #5fcf7a;
  --p-z: #e85a5a;
  --p-j: #4a7be8;
  --p-l: #f08a3c;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-weight: 400;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: none;
}

body {
  /* dvh prevents iOS URL-bar jitter; vh fallback for older WebViews */
  height: 100vh;
  height: 100dvh;
}

body::before {
  content: '';
  position: fixed; inset: 0;
  background:
    linear-gradient(var(--line) 1px, transparent 1px) 0 0/40px 40px,
    linear-gradient(90deg, var(--line) 1px, transparent 1px) 0 0/40px 40px;
  opacity: 0.14;
  pointer-events: none;
  mask-image: radial-gradient(ellipse at center, black 30%, transparent 75%);
  -webkit-mask-image: radial-gradient(ellipse at center, black 30%, transparent 75%);
}

#app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding:
    calc(var(--safe-top) + 8px)
    calc(var(--safe-right) + 10px)
    calc(var(--safe-bot) + 8px)
    calc(var(--safe-left) + 10px);
  gap: 8px;
}

/* ============== HUD TOP ============== */
#hud-top {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  padding-bottom: 6px;
}

.hud-cell {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.hud-stats {
  align-items: center;
  justify-content: center;
  text-align: center;
}

.hud-label {
  font-size: 9px;
  letter-spacing: 0.2em;
  color: var(--ink-dim);
}

.preview-canvas {
  background: var(--panel-2);
  border: 1px solid var(--line);
  display: block;
  image-rendering: pixelated;
}

.next-list {
  display: flex;
  gap: 3px;
}

#hold { width: 56px; height: 38px; }
.next-canvas { width: 44px; height: 30px; }

.stat-row {
  display: flex;
  gap: 6px;
  align-items: baseline;
  font-variant-numeric: tabular-nums;
}
.stat-row + .stat-row { margin-top: 2px; }
.stat-key { color: var(--ink-dim); font-size: 9px; letter-spacing: 0.15em; }
.stat-val { color: var(--ink); font-size: 14px; font-weight: 500; }
.stat-val.score { color: var(--accent); font-size: 18px; }

/* ============== BOARD ============== */
#board-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 0;
  min-width: 0;
  /* full-width gesture surface: tapping outside the playfield still rotates */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.board-frame {
  position: relative;
  border: 1px solid var(--line);
  background: var(--panel);
  padding: 4px;
  /* size set by JS to maintain 1:2 aspect and fit available area */
}
.board-frame::before {
  content: '';
  position: absolute;
  top: -1px; left: -1px; right: -1px;
  height: 2px;
  background: var(--accent);
  opacity: 0.6;
  pointer-events: none;
}

#board {
  display: block;
  background: #07070a;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: rgba(7,7,10,0.88);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: 10;
  padding: 16px;
  pointer-events: auto;
}
.overlay.hidden { display: none; }
.overlay h2 {
  font-family: 'IBM Plex Sans', sans-serif;
  font-weight: 300;
  font-size: 32px;
  letter-spacing: 0.3em;
  /* letter-spacing adds trailing space on the last char too, which
     visually shifts the text left of true center. Pad-left to compensate. */
  padding-left: 0.3em;
  text-align: center;
  color: var(--ink);
  margin-bottom: 14px;
}
.overlay h2 .accent { color: var(--accent); }
.overlay h2.warn { color: var(--warn); }
.overlay p {
  font-size: 11px;
  color: var(--ink-dim);
  letter-spacing: 0.2em;
  text-transform: uppercase;
  text-align: center;
  margin-bottom: 18px;
  animation: blink 1.4s infinite;
}
.hint {
  display: grid;
  gap: 4px;
  font-size: 10px;
  color: var(--ink-mute);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-align: center;
}
.accent { color: var(--accent); }

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0.3; }
}

/* ============== FOOTER ============== */
#hud-bottom {
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  gap: 8px;
  align-items: center;
  border-top: 1px solid var(--line);
  padding-top: 6px;
  font-size: 10px;
  color: var(--ink-mute);
  letter-spacing: 0.2em;
  text-transform: uppercase;
}

.ctrl-btn {
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink);
  font-family: inherit;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.1em;
  height: 44px;             /* iOS thumb-friendly minimum */
  min-width: 48px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.06s, border-color 0.06s, color 0.06s;
}
.ctrl-btn:active {
  background: var(--panel-2);
  border-color: var(--accent);
}

/* Primary action — DROP — sized for thumb, accent-colored, eats remaining space */
.ctrl-btn--primary {
  background: var(--accent);
  color: #1a1408;
  border-color: var(--accent);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.15em;
  height: 52px;
  width: 100%;
}
.ctrl-btn--primary:active {
  background: #f8d674;
  border-color: #f8d674;
  color: #1a1408;
  transform: translateY(1px);
}

/* ============== RESPONSIVE ============== */

/* small phones in portrait — tighten everything */
@media (max-height: 700px) and (orientation: portrait) {
  .hud-label { font-size: 8px; }
  #hold { width: 48px; height: 32px; }
  .next-canvas { width: 38px; height: 26px; }
  .stat-val.score { font-size: 16px; }
}

/* landscape phones — board centered, hud either side */
@media (orientation: landscape) and (max-height: 500px) {
  #app {
    grid-template-rows: 1fr;
    grid-template-columns: auto 1fr auto;
    align-items: center;
  }
  #hud-top {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    border-bottom: none;
    border-right: 1px solid var(--line);
    padding-bottom: 0;
    padding-right: 8px;
    height: 100%;
  }
  #hud-bottom {
    flex-direction: column;
    border-top: none;
    border-left: 1px solid var(--line);
    padding-top: 0;
    padding-left: 8px;
    gap: 12px;
    height: 100%;
    justify-content: center;
  }
  .next-list { flex-direction: column; }
}

/* tablet / desktop — three-column-ish */
@media (min-width: 900px) {
  #app {
    grid-template-rows: auto 1fr auto;
    max-width: 1100px;
    margin: 0 auto;
  }
  #hud-top {
    grid-template-columns: 200px 1fr 200px;
  }
  #hold { width: 120px; height: 80px; }
  .next-canvas { width: 80px; height: 50px; }
  .next-list { flex-direction: column; gap: 4px; }
  .hud-stats { gap: 6px; }
  .stat-val.score { font-size: 22px; }
}

/* respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  .overlay p { animation: none; }
}
