:root {
  --ink: #07111f;
  --ink-deep: #040a13;
  --panel: rgba(14, 28, 48, 0.86);
  --panel-strong: #0d2036;
  --line: rgba(149, 190, 221, 0.2);
  --line-bright: rgba(149, 190, 221, 0.42);
  --text: #ecf6ff;
  --muted: #89a8be;
  --cyan: #69e8f5;
  --lime: #c8f56a;
  --violet: #a891ff;
  --orange: #ffb16a;
  --space: 'Space Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  --display: 'Chakra Petch', 'Arial Narrow', sans-serif;
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--ink-deep); }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 12% 10%, rgba(49, 101, 139, 0.26), transparent 28rem),
    radial-gradient(circle at 92% 82%, rgba(58, 46, 117, 0.24), transparent 30rem),
    linear-gradient(135deg, #06101c 0%, #091a2b 48%, #07111f 100%);
  font-family: var(--space);
}

button { font: inherit; }

button:focus-visible { outline: 2px solid var(--lime); outline-offset: 3px; }

.app-shell { width: min(1120px, calc(100% - 40px)); margin: 0 auto; padding: 42px 0 28px; }

.topbar { display: flex; justify-content: space-between; align-items: flex-end; gap: 24px; padding-bottom: 30px; }

.eyebrow, .stat-label, .section-heading, .caption, .footer-note, .status-line, .board-head, .topbar-note { font-size: 10px; letter-spacing: .16em; }

.eyebrow { margin: 0 0 11px; color: var(--cyan); }

h1 { margin: 0; font-family: var(--display); font-size: clamp(42px, 7vw, 76px); letter-spacing: .04em; line-height: .88; font-weight: 700; }
h1 span { color: var(--cyan); text-shadow: 0 0 26px rgba(105, 232, 245, .23); }

.topbar-note { color: var(--muted); text-align: right; line-height: 1.8; }

.game-layout { display: grid; grid-template-columns: minmax(300px, 1fr) minmax(280px, 350px); gap: 42px; align-items: start; }

.board-column { min-width: 0; }
.board-frame { padding: 16px; border: 1px solid var(--line-bright); background: linear-gradient(145deg, rgba(20, 45, 70, .72), rgba(6, 14, 25, .94)); box-shadow: 0 20px 60px rgba(0, 0, 0, .34), inset 0 0 0 1px rgba(255,255,255,.03); }
.board-head { display: flex; justify-content: space-between; align-items: center; height: 22px; color: var(--muted); }
.board-light { width: 7px; height: 7px; border-radius: 999px; background: var(--lime); box-shadow: 0 0 13px var(--lime); }
.board-stage { position: relative; display: grid; place-items: center; overflow: hidden; border: 1px solid rgba(105, 232, 245, .16); background: #071321; }
#gameCanvas { display: block; width: min(100%, 420px); height: auto; aspect-ratio: 1 / 2; image-rendering: pixelated; }
.board-stage::after { content: ''; position: absolute; inset: 0; pointer-events: none; background: linear-gradient(120deg, rgba(255,255,255,.07), transparent 18%, transparent 80%, rgba(105,232,245,.04)); mix-blend-mode: screen; }
.board-overlay { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 14px; padding: 24px; text-align: center; background: rgba(4, 10, 19, .74); backdrop-filter: blur(5px); transition: opacity .25s ease; }
.board-overlay.is-hidden { opacity: 0; pointer-events: none; }
.overlay-kicker { margin: 0; color: var(--cyan); font-size: 10px; letter-spacing: .18em; }
.overlay-title { margin: 0; font-family: var(--display); font-size: clamp(25px, 5vw, 38px); font-weight: 600; letter-spacing: .04em; }
.caption { margin: 15px 2px 0; color: var(--muted); }

.side-panel { display: flex; flex-direction: column; gap: 15px; }
.board-topline { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 22px; margin-bottom: 14px; }
.board-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; }
.board-stats .stat-card { min-height: 0; padding: 0; border: 0; background: transparent; }
.board-stat strong { display: block; margin-top: 7px; font-size: 28px; }
.score-card strong { color: var(--cyan); text-shadow: 0 0 22px rgba(105, 232, 245, .15); }
.next-card-top { display: grid; grid-template-columns: auto auto; align-items: center; justify-content: start; gap: 8px; min-height: 0; margin: 0; padding: 0; border: 0; background: transparent; }
.next-card-top .section-heading { flex-direction: row; align-items: center; gap: 8px; }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-card, .next-card, .controls-card { border: 1px solid var(--line); background: var(--panel); }
.stat-card { min-height: 92px; display: flex; flex-direction: column; justify-content: space-between; padding: 14px 15px; }
.stat-card-wide { grid-column: span 2; min-height: 100px; }
.stat-label { color: var(--muted); }
.stat-card strong { color: var(--text); font-family: var(--display); font-size: 34px; letter-spacing: .06em; line-height: 1; }
.stat-card-wide strong { color: var(--cyan); font-size: 44px; text-shadow: 0 0 22px rgba(105, 232, 245, .15); }
.high-score-card strong { color: var(--lime); text-shadow: 0 0 22px rgba(200, 245, 106, .12); }

.next-card, .controls-card { padding: 15px; }
.board-topline .next-card-top { padding: 0; border: 0; background: transparent; }
.side-panel .controls-card { padding: 0; border: 0; background: transparent; }
.side-panel .controls-card .section-heading { padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.skin-options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; margin-top: 10px; }
.skin-option { min-height: 42px; padding: 6px 4px; border: 1px solid var(--line); color: var(--muted); background: transparent; cursor: pointer; font-size: 9px; letter-spacing: .08em; transition: border-color .16s ease, color .16s ease, background .16s ease; }
.skin-option:hover:not(:disabled) { border-color: var(--cyan); color: var(--text); background: rgba(16, 39, 61, .5); }
.skin-option.is-selected { border-color: var(--skin-accent, var(--cyan)); color: var(--text); background: rgba(16, 39, 61, .65); }
.skin-option:disabled { cursor: not-allowed; color: #90a8bb; }
.skin-option canvas { display: block; width: 96px; height: 48px; max-width: 100%; margin: 0 auto 7px; }
.skin-option { padding: 10px 6px; font-size: 11px; }
.skin-option-lock { display: block; margin-top: 3px; font-size: 10px; letter-spacing: .04em; }
.section-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; color: var(--text); }
.section-index { color: var(--muted); }
#nextCanvas { display: block; width: 68px; height: 68px; margin: 0; }
.board-pause-button { position: absolute; top: 10px; right: 10px; z-index: 5; display: grid; place-items: center; width: 42px; height: 42px; padding: 0; border: 1px solid rgba(105, 232, 245, .42); color: var(--cyan); background: rgba(7, 19, 33, .82); cursor: pointer; font-family: var(--display); font-size: 18px; line-height: 1; transition: border-color .16s ease, background .16s ease, transform .16s ease; }
.board-pause-button:hover:not(:disabled) { border-color: var(--cyan); background: rgba(16, 50, 70, .95); }
.board-pause-button:active:not(:disabled) { transform: translateY(1px); }
.board-pause-button:disabled { cursor: not-allowed; opacity: .35; }
.board-menu-button { position: absolute; top: 10px; right: 60px; z-index: 5; display: grid; place-items: center; width: 42px; height: 42px; padding: 0; border: 1px solid rgba(149, 190, 221, .3); color: var(--muted); background: rgba(7, 19, 33, .82); cursor: pointer; font-size: 18px; line-height: 1; transition: border-color .16s ease, color .16s ease, background .16s ease; }
.board-menu-button:hover { border-color: var(--cyan); color: var(--cyan); background: rgba(16, 50, 70, .95); }
.game-menu { position: absolute; top: 60px; right: 10px; z-index: 7; display: grid; min-width: 132px; padding: 5px; border: 1px solid var(--line-bright); background: rgba(7, 19, 33, .97); box-shadow: 0 16px 30px rgba(0, 0, 0, .35); }
.game-menu[hidden] { display: none; }
.game-menu button { min-height: 34px; padding: 0 12px; border: 0; color: var(--text); background: transparent; cursor: pointer; text-align: left; font-size: 10px; letter-spacing: .1em; }
.game-menu button:hover:not(:disabled), .game-menu button:focus-visible { color: var(--cyan); background: rgba(16, 50, 70, .75); outline: none; }
.game-menu button:disabled { color: #496174; cursor: not-allowed; }
.settings-panel { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 20px; background: rgba(4, 10, 19, .7); backdrop-filter: blur(8px); }
.settings-panel[hidden] { display: none; }
.settings-dialog { width: min(390px, 100%); padding: 20px; border: 1px solid var(--line-bright); background: linear-gradient(145deg, rgba(20, 45, 70, .96), rgba(6, 14, 25, .98)); box-shadow: 0 24px 70px rgba(0, 0, 0, .45); }
.settings-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; margin-bottom: 22px; }
.settings-header .eyebrow { margin-bottom: 8px; }
.settings-header h2 { margin: 0; font-family: var(--display); font-size: 32px; letter-spacing: .04em; }
.settings-close { display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border: 1px solid var(--line); color: var(--muted); background: transparent; cursor: pointer; font-size: 24px; line-height: 1; }
.settings-close:hover { border-color: var(--cyan); color: var(--cyan); }
.settings-dialog .section-heading { padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.settings-dialog { max-height: calc(100dvh - 40px); overflow-y: auto; }
.audio-settings { margin-top: 24px; padding-top: 18px; border-top: 1px solid var(--line); }
.audio-settings h3 { margin: 0 0 12px; font-size: 16px; }
.audio-row { display: flex; justify-content: space-between; align-items: center; min-height: 44px; gap: 12px; font-size: 13px; }
.audio-row output { color: var(--cyan); font-variant-numeric: tabular-nums; }
#masterVolume, #effectsVolume { display: block; width: 100%; min-height: 44px; margin: 0; accent-color: var(--cyan); cursor: pointer; }
#soundEnabled { width: 44px; height: 44px; margin: 0; accent-color: var(--cyan); cursor: pointer; }
.audio-settings input:focus-visible { outline: 2px solid var(--lime); outline-offset: 3px; }
.audio-hint { margin: 8px 0 16px; color: var(--muted); font-size: 12px; line-height: 1.7; }
#testSoundButton { width: 100%; }
#testSoundButton:disabled { opacity: .5; cursor: not-allowed; }
@keyframes settings-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .45; } }
@keyframes settings-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .45; } }

.touch-controls { display: flex; align-items: center; justify-content: center; gap: clamp(18px, 3vw, 32px); margin: 24px 0 18px; }
.direction-pad { display: grid; grid-template-columns: repeat(3, 52px); grid-template-rows: repeat(3, 52px); gap: 0; filter: drop-shadow(0 6px 0 #030a12) drop-shadow(0 10px 10px rgba(0,0,0,.3)); }
.control-button { display: grid; place-items: center; min-width: 44px; min-height: 44px; padding: 0; border: 1px solid #39546b; color: var(--cyan); background: linear-gradient(#243c51, #132536); cursor: pointer; font-size: 22px; touch-action: manipulation; user-select: none; -webkit-user-select: none; transition: transform .1s ease, color .1s ease, background .1s ease; }
.control-button:hover { color: var(--text); background: #28485f; }
.control-button:active { transform: translateY(3px); background: #0b1b29; color: var(--lime); }
.control-button:focus-visible { position: relative; z-index: 2; }
.direction-up { grid-area: 1 / 2; border-bottom: 0; border-radius: 8px 8px 0 0; }
.direction-left { grid-area: 2 / 1; border-right: 0; border-radius: 8px 0 0 8px; }
.direction-right { grid-area: 2 / 3; border-left: 0; border-radius: 0 8px 8px 0; }
.direction-down { grid-area: 3 / 2; border-top: 0; border-radius: 0 0 8px 8px; }
.direction-center { grid-area: 2 / 2; display: grid; place-items: center; background: #192e41; }
.direction-center span { width: 18px; height: 18px; border-radius: 50%; background: #102232; box-shadow: inset 0 2px 3px #07121f, 0 1px 0 #3a5368; }
.drop-control { display: grid; justify-items: center; gap: 12px; padding-top: 22px; }
.control-hard-drop { width: 64px; height: 64px; border-radius: 50%; border-color: #8b6354; color: var(--orange); font-size: 30px; background: linear-gradient(#513d38, #2c2529); box-shadow: 0 5px 0 #16131b, 0 9px 16px rgba(0,0,0,.3); }
.control-hard-drop:active { box-shadow: 0 2px 0 #16131b; }
.drop-control > span { font-size: 11px; color: var(--muted); }
.control-hint { margin: 0; text-align: center; color: var(--muted); font-size: 11px; line-height: 1.8; }

.action-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.action-row-single { grid-template-columns: 1fr; }
.button { min-height: 46px; border: 1px solid var(--line-bright); cursor: pointer; letter-spacing: .1em; font-size: 11px; transition: transform .16s ease, background .16s ease, border-color .16s ease; }
.button:active { transform: translateY(1px); }
.button-primary { padding: 0 24px; border-color: var(--lime); color: var(--ink); background: var(--lime); font-weight: 700; }
.button-primary:hover { background: #dcff92; }
.button-secondary { color: var(--cyan); background: rgba(16, 39, 61, .84); }
.button-secondary:hover, .button-ghost:hover { border-color: var(--cyan); background: rgba(22, 60, 83, .9); }
.button-ghost { color: var(--muted); background: transparent; }
.status-line { margin: 0; color: var(--muted); line-height: 1.6; }

.footer-note { display: flex; justify-content: space-between; margin-top: 42px; padding-top: 16px; border-top: 1px solid var(--line); color: #58758b; }

/* Fit the entire desktop game to the visible window, including browser chrome. */
@media (min-width: 761px) {
  .app-shell {
    height: 100svh; height: 100dvh;
    padding: 16px 0 12px;
    display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 12px;
  }
  .topbar { padding: 0; align-items: center; }
  .topbar h1 { font-size: clamp(32px, 6vh, 56px); }
  .topbar .eyebrow { margin-bottom: 6px; }
  .game-layout { min-height: 0; grid-template-columns: minmax(0, 1fr) 280px; gap: 28px; align-items: stretch; }
  .board-column { min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 8px; }
  .board-topline { margin: 0; gap: 12px; }
  .board-stats { grid-template-columns: 1.5fr .7fr .7fr 1.5fr; gap: 8px; }
  .board-stats .stat-label { font-size: 9px; letter-spacing: .04em; white-space: nowrap; }
  .board-stats .board-stat strong { font-size: clamp(18px, 2.3vw, 28px); letter-spacing: 0; margin-top: 4px; }
  .next-card-top .section-index { display: none; }
  .next-card-top .section-heading { font-size: 9px; letter-spacing: .04em; }
  #nextCanvas { width: 48px; height: 48px; }
  .board-frame { position: relative; min-height: 0; display: grid; grid-template-rows: 44px minmax(0, 1fr); padding: 4px 8px 8px; }
  .board-head { height: 44px; padding-left: 4px; }
  .board-light { display: none; }
  .board-stage { position: static; min-height: 0; }
  #gameCanvas { width: 100%; height: 100%; min-height: 0; object-fit: contain; aspect-ratio: auto; }
  .board-stage::after { display: none; }
  .board-overlay { top: 48px; bottom: 8px; left: 8px; right: 8px; }
  .board-pause-button, .board-menu-button { top: 4px; width: 40px; height: 40px; }
  .board-pause-button { right: 8px; }
  .board-menu-button { right: 56px; }
  .game-menu { top: 48px; right: 8px; max-height: calc(100% - 56px); overflow-y: auto; }
  .caption { margin: 0; letter-spacing: .04em; }
  .side-panel { align-self: center; }
  .footer-note { margin: 0; padding-top: 8px; }
}

@media (min-width: 761px) and (max-width: 1000px) {
  .app-shell { width: calc(100% - 24px); }
  .game-layout { grid-template-columns: minmax(0, 1fr) 240px; gap: 16px; }
  .touch-controls { gap: 18px; }
  .direction-pad { grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 44px); }
}

@media (min-width: 1001px) and (max-height: 500px) {
  .app-shell { padding: 8px 0; gap: 6px; }
  .topbar .eyebrow, .topbar-note, .caption, .footer-note { display: none; }
  .app-shell { grid-template-rows: auto minmax(0, 1fr); }
  .board-column { grid-template-rows: auto minmax(0, 1fr); gap: 4px; }
  .topbar h1 { font-size: 28px; }
}

/* Reserve the controls first; the playfield uses the remaining viewport height. */
@media (max-width: 760px), (max-width: 1000px) and (max-height: 500px) {
  body { min-height: 100svh; }
  .app-shell {
    width: 100%; height: 100svh; height: 100dvh; margin: 0;
    padding: calc(6px + env(safe-area-inset-top, 0px)) calc(12px + env(safe-area-inset-right, 0px)) calc(10px + env(safe-area-inset-bottom, 0px)) calc(12px + env(safe-area-inset-left, 0px));
    display: grid; grid-template-rows: 26px minmax(0, 1fr); gap: 6px;
  }
  .topbar { align-items: center; padding: 0; }
  .topbar h1 { font-size: 24px; line-height: 1; }
  .topbar .eyebrow, .topbar-note, .footer-note, .caption { display: none; }
  .game-layout { min-height: 0; grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; gap: 8px; align-items: stretch; }
  .board-column { min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 4px; }
  .board-topline { grid-template-columns: minmax(0, 1fr) auto; gap: 8px; margin: 0; }
  .board-stats { grid-template-columns: 1.5fr .7fr .7fr 1.5fr; gap: 5px; }
  .board-stats .stat-label { font-size: 8px; letter-spacing: .03em; white-space: nowrap; }
  .board-stats .board-stat strong { margin-top: 4px; font-size: clamp(13px, 4vw, 20px); letter-spacing: 0; }
  .board-topline .next-card-top { gap: 2px; margin: 0; }
  .next-card-top .section-heading { font-size: 8px; letter-spacing: 0; }
  .next-card-top .section-index { display: none; }
  #nextCanvas { width: 48px; height: 48px; }
  .board-frame { position: relative; min-height: 0; display: grid; grid-template-rows: 44px minmax(0, 1fr); padding: 4px 6px 6px; }
  .board-head { height: 44px; padding-left: 4px; }
  .board-light { display: none; }
  .board-stage { position: static; min-height: 0; border: 0; }
  #gameCanvas { width: 100%; height: 100%; min-height: 0; object-fit: contain; aspect-ratio: auto; }
  .board-stage::after { display: none; }
  .board-overlay { top: 48px; bottom: 6px; left: 6px; right: 6px; padding: 10px; gap: 10px; }
  .overlay-kicker { font-size: 8px; }
  .overlay-title { font-size: 24px; }
  .board-pause-button, .board-menu-button { top: 4px; width: 44px; height: 44px; }
  .board-pause-button { right: 6px; }
  .board-menu-button { right: 54px; }
  .game-menu { top: 48px; right: 6px; }
  .game-menu button { min-height: 44px; }
  .side-panel { min-height: 0; display: block; }
  .controls-card > .section-heading, .side-panel > .status-line { display: none; }
  .touch-controls { margin: 0 0 8px; gap: 40px; }
  .direction-pad { grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 44px); }
  .control-hint { font-size: 10px; line-height: 16px; }
  .drop-control { padding-top: 0; gap: 8px; }
  .settings-panel { padding: max(12px, env(safe-area-inset-top, 0px)) 12px max(12px, env(safe-area-inset-bottom, 0px)); }
  .settings-dialog { max-height: 100%; overscroll-behavior: contain; }
}

/* Short landscape screens put the controller beside the board. */
@media (max-width: 1000px) and (max-height: 500px) and (orientation: landscape) {
  .game-layout { grid-template-columns: minmax(0, 1fr) 230px; grid-template-rows: minmax(0, 1fr); gap: 16px; }
  .side-panel { align-self: center; }
  .touch-controls { gap: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

/* Instructions use their own scrolling panel without extending the playfield. */
.help-dialog { width: min(520px, 100%); }
.help-dialog .settings-close { flex: 0 0 44px; height: 44px; }
.help-intro, .help-section p, .help-section li, .help-note { font-size: 13px; line-height: 1.8; }
.help-intro { margin: 0 0 22px; color: var(--text); }
.help-section { margin-top: 20px; }
.help-section h3 { margin: 0 0 10px; color: var(--cyan); font-size: 14px; }
.help-section p { margin: 8px 0; color: var(--muted); }
.help-section ul { margin: 0; padding-left: 19px; color: var(--muted); }
.help-section li + li { margin-top: 7px; }
.help-keys { margin: 0; }
.help-keys > div { display: grid; grid-template-columns: 104px minmax(0, 1fr); align-items: center; min-height: 38px; gap: 8px; }
.help-keys dt { color: var(--muted); }
.help-keys dd { margin: 0; font-size: 12px; line-height: 1.6; }
.help-keys kbd { display: inline-block; min-width: 28px; padding: 3px 7px; border: 1px solid var(--line-bright); border-radius: 4px; background: #0b1b29; box-shadow: 0 2px 0 #030a12; color: var(--text); font: 12px var(--display); text-align: center; }
.help-note { margin: 24px 0 14px; color: var(--muted); }
.help-done { width: 100%; }

.notice-stack { position: fixed; top: calc(10px + env(safe-area-inset-top, 0px)); left: 50%; transform: translateX(-50%); width: min(430px, calc(100% - 24px)); z-index: 30; pointer-events: none; }
.game-notice { margin: 0 0 8px; padding: 12px 16px; border: 1px solid var(--cyan); background: #102536; color: var(--text); box-shadow: 0 6px 24px #0008; font-size: 13px; line-height: 1.6; }
.game-notice[hidden] { display: none; }
.confirm-dialog h2 { margin: 0 0 16px; font-size: 24px; }
.confirm-dialog p { margin: 0 0 24px; color: var(--muted); font-size: 13px; line-height: 1.8; }
.confirm-dialog .button { padding: 10px 8px; letter-spacing: 0; }
.direction-pad .control-button { touch-action: none; -webkit-touch-callout: none; }
