:root {
  --bg: #090a0f;
  --surface: #10131a;
  --surface-2: #171b25;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text: #edf0f7;
  --text-dim: rgba(237, 240, 247, 0.62);
  --text-muted: rgba(237, 240, 247, 0.34);
  --accent: #6f76ff;
  --accent-soft: rgba(111, 118, 255, 0.16);
  --accent-2: #4fc3f7;
  --success: #55d68d;
  --danger: #ff6c86;
  --mono: 'Space Mono', monospace;
  --sans: 'DM Sans', sans-serif;
  --app-max-width: 1100px;
  --touch-board-max-width: 100%;
}

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

html,
body {
  min-height: 100%;
}

body {
  min-height: 100svh;
  overflow-x: hidden;
  background:
    radial-gradient(circle at top, rgba(79, 195, 247, 0.08), transparent 28%),
    radial-gradient(circle at bottom right, rgba(111, 118, 255, 0.12), transparent 24%),
    var(--bg);
  color: var(--text);
  font-family: var(--sans);
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(rgba(111, 118, 255, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(111, 118, 255, 0.045) 1px, transparent 1px);
  background-size: 42px 42px;
  pointer-events: none;
}

.app-shell {
  position: relative;
  z-index: 1;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 12px;
}

.status-strip,
.game-wrap,
.action-bar {
  width: min(var(--app-max-width), 100%);
  margin: 0 auto;
}

.status-strip {
  padding: 14px 16px;
  border: 1px solid var(--border);
  background: rgba(16, 19, 26, 0.88);
  backdrop-filter: blur(10px);
  border-radius: 12px;
}

.status-strip.pending {
  border-color: rgba(79, 195, 247, 0.38);
  box-shadow: 0 0 0 1px rgba(79, 195, 247, 0.12) inset;
}

.status-strip.success {
  border-color: rgba(85, 214, 141, 0.4);
  box-shadow: 0 0 0 1px rgba(85, 214, 141, 0.12) inset;
}

.status-strip.error {
  border-color: rgba(255, 108, 134, 0.42);
  box-shadow: 0 0 0 1px rgba(255, 108, 134, 0.12) inset;
}

.status-title {
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.status-detail {
  margin-top: 6px;
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1.45;
}

.status-detail[hidden] {
  display: none;
}

.game-wrap {
  flex: 0 0 auto;
  display: flex;
  gap: 28px;
  justify-content: center;
  align-items: flex-start;
}

.board-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 12px;
  flex: 0 0 auto;
}

.mobile-hud {
  display: none;
}

.side-column {
  width: 240px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 0 0 auto;
}

.board-container {
  position: relative;
  height: min(calc(100svh - 168px), 760px);
  max-height: 760px;
  aspect-ratio: 14 / 20;
  line-height: 0;
}

#board-canvas {
  display: block;
  width: 100%;
  height: 100%;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  image-rendering: pixelated;
  background: rgba(10, 11, 18, 0.7);
  touch-action: none;
}

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(7, 8, 12, 0.86);
  border-radius: 8px;
  transition: opacity 0.2s ease;
}

#overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.overlay-title {
  font-family: var(--mono);
  font-size: 24px;
  font-weight: 700;
  letter-spacing: 0.15em;
  line-height: 1;
}

.overlay-sub {
  margin-top: 10px;
  padding: 0 18px;
  text-align: center;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  line-height: 1.45;
}

.overlay-score {
  margin-top: 14px;
  color: var(--accent);
  font-family: var(--mono);
  font-size: 34px;
  font-weight: 700;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 14px;
}

.ctrl-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: 11px;
}

.key-badge {
  padding: 1px 6px;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  background: rgba(16, 19, 26, 0.9);
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 10px;
}

.side {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 0 0 auto;
}

.panel {
  min-width: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(16, 19, 26, 0.92);
}

.panel-label {
  margin-bottom: 6px;
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.panel-value {
  font-family: var(--mono);
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
}

.panel-value-sm {
  font-size: 20px;
}

.panel-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

.metric {
  min-width: 0;
}

.panel-next {
  display: flex;
  flex-direction: column;
}

#next-canvas {
  display: block;
  width: 120px;
  height: auto;
  margin-top: 4px;
}

.combo-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: 8px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  border: 1px solid rgba(111, 118, 255, 0.35);
  color: #b9bcff;
  font-family: var(--mono);
  font-size: 11px;
}

.action-bar {
  display: flex;
  justify-content: stretch;
  gap: 10px;
  padding-bottom: 0;
}

.btn {
  width: 240px;
  padding: 12px 0;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: rgba(16, 19, 26, 0.84);
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s;
}

.btn:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: rgba(111, 118, 255, 0.45);
  color: #cdd0ff;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: rgba(111, 118, 255, 0.16);
  border-color: rgba(111, 118, 255, 0.38);
  color: #d2d4ff;
}

.btn-primary:hover:not(:disabled) {
  background: rgba(111, 118, 255, 0.24);
  border-color: rgba(111, 118, 255, 0.58);
}

.mobile-action-row {
  display: none;
}

.btn-icon {
  display: none;
}

.btn-control {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 0;
  aspect-ratio: 1 / 1;
  border-radius: 14px;
}

.btn-control-glyph {
  display: block;
  font-size: 24px;
  line-height: 1;
}

.btn-icon-glyph {
  display: block;
  font-size: 22px;
  line-height: 1;
  transform: translateY(-1px);
}

.mobile-toast {
  position: fixed;
  left: 50%;
  bottom: 88px;
  z-index: 20;
  width: min(360px, calc(100vw - 24px));
  padding: 14px 16px;
  border: 1px solid rgba(85, 214, 141, 0.34);
  border-radius: 16px;
  background: rgba(10, 14, 20, 0.96);
  box-shadow: 0 18px 42px rgba(0, 0, 0, 0.36);
  transform: translate(-50%, 16px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.mobile-toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.mobile-toast-title {
  color: #dfffe9;
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.mobile-toast-body {
  margin-top: 6px;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.4;
}

@keyframes scoreFlash {
  0% {
    color: var(--accent-2);
    transform: scale(1.12);
  }

  100% {
    color: var(--text);
    transform: scale(1);
  }
}

.score-flash {
  animation: scoreFlash 0.32s ease-out;
}

@media (max-width: 980px) {
  .game-wrap {
    width: min(var(--app-max-width), 100%);
    flex: 1 1 auto;
    flex-direction: column;
    align-items: center;
  }

  .board-container {
    height: min(calc(100svh - 300px), 76vw);
  }

  .side-column {
    width: min(720px, 100%);
  }

  .side {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1.3fr) repeat(2, minmax(0, 1fr));
    gap: 12px;
  }

  .action-bar {
    width: 100%;
  }
}

html.touch-layout body {
  overflow: hidden;
}

html.touch-layout .app-shell {
  height: 100svh;
  gap: 8px;
  padding:
    max(8px, env(safe-area-inset-top, 0px))
    10px
    max(8px, env(safe-area-inset-bottom, 0px));
}

html.touch-layout .status-strip,
html.touch-layout .game-wrap,
html.touch-layout .action-bar {
  width: 100%;
}

html.touch-layout .status-strip {
  display: none;
}

html.touch-layout .status-title {
  font-size: 11px;
}

html.touch-layout .status-detail {
  font-size: 12px;
  line-height: 1.35;
}

html.touch-layout .game-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  align-items: stretch;
  align-content: stretch;
  justify-items: stretch;
  gap: 8px;
}

html.touch-layout .side-column {
  display: contents;
}

html.touch-layout .side {
  display: none;
}

html.touch-layout .mobile-hud {
  width: 100%;
  grid-row: 1;
  display: grid;
  grid-template-columns: minmax(0, 1.36fr) minmax(0, 1.04fr) minmax(0, 1.04fr) minmax(80px, 0.92fr);
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: rgba(16, 19, 26, 0.94);
  backdrop-filter: blur(10px);
}

html.touch-layout .mobile-metric {
  min-width: 0;
  padding: 7px 9px;
}

html.touch-layout .mobile-metric + .mobile-metric,
html.touch-layout .mobile-preview {
  border-left: 1px solid rgba(255, 255, 255, 0.06);
}

html.touch-layout .mobile-metric-label {
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: 7px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

html.touch-layout .mobile-metric-value {
  margin-top: 3px;
  color: var(--text);
  font-family: var(--mono);
  font-size: clamp(16px, 4.4vw, 20px);
  font-weight: 700;
  line-height: 1;
}

html.touch-layout .mobile-metric-score .mobile-metric-value {
  font-size: clamp(15px, 4.1vw, 19px);
}

html.touch-layout .mobile-metric-compact {
  padding-left: 9px;
  padding-right: 9px;
}

html.touch-layout .mobile-metric-compact .mobile-metric-value {
  font-size: clamp(14px, 3.7vw, 18px);
}

html.touch-layout .mobile-preview {
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 6px 7px;
}

html.touch-layout #next-touch-canvas {
  display: block;
  width: 100%;
  height: auto;
  max-width: 88px;
  margin-top: 3px;
}

html.touch-layout .board-area {
  display: contents;
}

html.touch-layout .board-container {
  grid-row: 2;
  width: min(100%, var(--touch-board-max-width));
  height: auto;
  max-height: none;
  margin: 0;
  justify-self: center;
  align-self: center;
}

html.touch-layout .controls {
  display: none;
}

html.touch-layout .overlay-title {
  font-size: clamp(20px, 6vw, 28px);
}

html.touch-layout .overlay-sub {
  font-size: 12px;
  line-height: 1.35;
}

html.touch-layout .overlay-score {
  font-size: clamp(28px, 8vw, 36px);
}

html.touch-layout .action-bar {
  width: 100%;
  grid-row: 4;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  align-items: stretch;
  gap: 8px;
  justify-self: stretch;
}

html.touch-layout .btn {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  min-height: 54px;
  border-radius: 14px;
}

html.touch-layout .mobile-action-row {
  width: 100%;
  grid-row: 3;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  justify-self: stretch;
}

html.touch-layout .btn-primary {
  grid-column: 1 / span 3;
}

html.touch-layout .btn-control {
  min-width: 0;
  aspect-ratio: auto;
}

html.touch-layout .btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-width: 0;
  min-height: 54px;
  padding: 0;
  aspect-ratio: auto;
}

html.touch-layout .mobile-toast {
  top: max(10px, calc(env(safe-area-inset-top, 0px) + 10px));
  bottom: auto;
  width: min(420px, calc(100vw - 24px));
}
