:root {
  color-scheme: dark;
  --bg: #111318;
  --surface: #242830;
  --surface-2: #343a45;
  --grid: #555c68;
  --grid-heavy: #747d8b;
  --section-edge-light: #5d646f;
  --section-edge-dark: #363e47;
  --section-edge-width: 3px;
  --text: #f0f2f5;
  --muted: #a1a7b0;
  --accent: #6688ad;
  --accent-bright: #7fa1c3;
  --accent-dark: #4f7197;
  --piece-hue: 212;
  --piece-bright: hsl(var(--piece-hue) 38% 63%);
  --piece-dark: hsl(var(--piece-hue) 32% 43%);
  --valid: #8aa6bc;
  --danger: #9a7378;
  --board-rim: #363b44;
  --board-edge: #585f6b;
  --cell-empty: #22262d;
  --cell-tint: #2e323c;
  --cell-tint-light: #383d47;
  --board-size: min(calc(100vw - 10px), 58vh, 580px);
  --hex-points: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

*, *::before, *::after {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

html, body {
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}
button:focus:not(:focus-visible) { outline: none; }
button:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
[hidden] { display: none !important; }

.game-shell {
  width: min(100%, 720px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 0 max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
}

.topbar, .score-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.topbar { min-height: 52px; }
.topbar { padding: 0 18px; }
.topbar > button { flex-shrink: 0; }
.save-status {
  max-width: 16rem;
  margin: 0 8px;
  color: #f0c38a;
  font-size: 12px;
  text-align: center;
}
.square-mode { --hex-points: polygon(0 0, 100% 0, 100% 100%, 0 100%); }

.topbar-spacer { width: 44px; }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #c9d0e2;
  font-weight: 750;
  letter-spacing: .02em;
}

.icon-button, .undo-button {
  border: 1px solid #454b56;
  color: #c3c8d0;
  background: #252930;
  box-shadow: none;
}

.icon-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 50%;
  font-size: 25px;
  line-height: 1;
  cursor: pointer;
}

.icon-button svg,
.fullscreen-button svg {
  width: 21px;
  height: 21px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.score-panel {
  position: relative;
  width: var(--board-size);
  min-height: clamp(66px, 10vh, 105px);
  margin: 0 auto 12px;
  justify-content: center;
  text-align: center;
}

.score-panel > div:first-child { align-self: center; }

.score-label {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .01em;
}

.score-panel > div:first-child .score-label { display: none; }

.score {
  font-size: clamp(34px, 6vw, 48px);
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.timer-display {
  display: block;
  margin-bottom: 6px;
  color: #c6ccd5;
  font-size: clamp(34px, 8vw, 48px);
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.timer-display[hidden] { display: none; }
.timer-display.ending { color: #e3a5ac; }

.best-score {
  position: absolute;
  right: 0;
  bottom: 2px;
  text-align: right;
}
.best-score strong { color: #d1d5dc; font-size: 20px; font-weight: 600; }

.match-scoreboard { width: 100%; display: grid; grid-template-columns: repeat(2, 1fr); gap: 2px 16px; text-align: center; }
.player-score { border-bottom: 2px solid transparent; padding-bottom: 3px; }
.player-score .score-label { color: inherit; }
.red-player { color: #e89494; }
.blue-player { color: #93bce8; }
.active-player { border-bottom-color: currentColor; }
.turn-status { grid-column: 1 / -1; margin: 2px 0 0; color: var(--text); font-size: 13px; }
.cell[data-player="red"] { --piece-hue: 0; --piece-bright: hsl(0 55% 64%); --piece-dark: hsl(0 45% 43%); }
.cell[data-player="blue"] { --piece-bright: hsl(212 38% 63%); --piece-dark: hsl(212 32% 43%); }

.board-wrap {
  position: relative;
  width: var(--board-size);
  margin: 0 auto;
  padding: 10px;
}

.board {
  position: relative;
  width: 100%;
  isolation: isolate;
  background: transparent;
}

.cell {
  position: absolute;
  z-index: 2;
  clip-path: var(--hex-points);
  background: transparent;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: var(--cell-fill-points, var(--hex-points));
  background: var(--cell-empty);
}
.cell[data-section-shade="1"]::before { background: var(--cell-tint); }
.cell[data-section-shade="2"]::before { background: var(--cell-tint-light); }
.board-frame, .chunk-outlines { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.board-frame { z-index: 0; }
.board-frame path { vector-effect: non-scaling-stroke; stroke-linejoin: miter; }
.board-frame .frame-edge { fill: none; stroke: var(--board-edge); stroke-width: 22px; }
.board-frame .frame-rim { fill: none; stroke: var(--board-rim); stroke-width: 20px; }
.board-frame .frame-grid { fill: var(--grid); stroke: none; }
.chunk-outlines { z-index: 1; }
.chunk-outlines path { fill: none; stroke: var(--section-edge-light); stroke-width: var(--section-edge-width); vector-effect: non-scaling-stroke; stroke-linecap: round; }
.chunk-outlines path[data-filled="true"] { stroke: var(--section-edge-dark); }
.chunk-outlines path[data-filled="true"],
.chunk-outlines path.clear-fade[data-filled="false"] { transition: stroke 2s ease; }

.square-mode .board .cell::before, .square-mode .board .cell::after {
  inset: var(--cell-inset-top, 0.5px) var(--cell-inset-right, 0.5px) var(--cell-inset-bottom, 0.5px) var(--cell-inset-left, 0.5px);
}

.cell.filled::after, .piece-cell, .drag-cell {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: var(--cell-fill-points, var(--hex-points));
  background: linear-gradient(180deg, var(--piece-bright), var(--piece-dark));
  box-shadow: inset 1px 1px 0 rgba(255,255,255,.32), inset -1px -1px 0 rgba(40,64,90,.22);
}

.cell.preview-valid::after {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: var(--cell-fill-points, var(--hex-points));
  background: rgba(117,149,177,.5);
  box-shadow: inset 0 0 0 2px var(--valid);
}

.cell.preview-invalid::after {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: var(--cell-fill-points, var(--hex-points));
  background: rgba(255,101,119,.25);
  box-shadow: inset 0 0 0 2px rgba(255,101,119,.65);
}

.cell.preview-clear::after {
  animation: preview-clear-pulse 1.1s ease-in-out infinite;
}

.piece-cell.drag-preview-clear {
  animation: preview-clear-pulse 1.1s ease-in-out infinite;
}

@keyframes preview-clear-pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.28); }
}

.cell.clearing::after { animation: clear-cell .28s ease both; }
@keyframes clear-cell {
  0% { transform: scale(1); opacity: 1; }
  45% { transform: scale(.82); filter: brightness(1.55); }
  100% { transform: scale(0); opacity: 0; }
}

.tray-section {
  flex: 1;
  min-height: 190px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0 18px;
}

.tray-carousel {
  position: relative;
  width: min(100%, 650px);
  min-width: 0;
}

.tray-viewport {
  overflow: hidden;
}

.tray {
  width: 100%;
  min-height: 135px;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: calc((100% - (var(--tray-columns, 3) - 1) * 3px) / var(--tray-columns, 3));
  align-items: center;
  gap: 3px;
  transition: transform .22s ease-out;
}

.tray.tray-still { transition: none; }

.tray-arrow {
  position: absolute;
  z-index: 1;
  top: 50%;
  width: 28px;
  height: 44px;
  padding: 0;
  border: 0;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  transform: translateY(-50%);
}

.tray-arrow svg {
  display: block;
  width: 24px;
  height: 32px;
  margin: auto;
  fill: none;
  stroke: currentColor;
  stroke-width: var(--section-edge-width);
  stroke-linecap: round;
  stroke-linejoin: round;
}

#trayPreviousButton { left: -14px; }
#trayNextButton { right: -14px; }
.tray-arrow:disabled { opacity: .28; cursor: default; }

.tray-slot {
  position: relative;
  height: 132px;
  display: grid;
  place-items: center;
  border-radius: 22px;
  touch-action: none;
  cursor: grab;
}

.tray.new-tray .tray-slot { animation: tray-arrive .24s ease-out both; }
.tray.new-tray .tray-slot:nth-child(2) { animation-delay: .045s; }
.tray.new-tray .tray-slot:nth-child(3) { animation-delay: .09s; }

@keyframes tray-arrive {
  0% { transform: translateY(12px) scale(.94); opacity: 0; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

.tray-slot.restored-piece {
  animation: tray-undo .22s ease-out both;
}

@keyframes tray-undo {
  0% { transform: translateY(-8px) scale(.94); opacity: .35; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

.tray-slot::before {
  content: "";
  position: absolute;
  inset: 5px;
  border: 1px solid transparent;
  border-radius: inherit;
  transition: .18s ease;
}

.tray-slot.dragging { opacity: 0; cursor: grabbing; }
.tray-slot.disabled { opacity: .24; filter: grayscale(1); cursor: not-allowed; }

.piece {
  position: relative;
  transform-origin: center;
  pointer-events: none;
}

.piece-cell { inset: auto; }
.tray-slot.disabled .piece-cell { background: #62697a; box-shadow: none; }

.undo-button {
  min-width: 116px;
  min-height: 42px;
  padding: 0 20px;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .12s ease, background-color .12s ease;
}

.undo-button:active { transform: scale(.94); }
.undo-button.rewinding { animation: undo-button .24s ease-out; }

@keyframes undo-button {
  0%, 100% { transform: rotate(0) scale(1); }
  45% { transform: rotate(-16deg) scale(.92); }
}

.undo-button:disabled { opacity: .28; cursor: default; }
.undo-button.setting-hidden { visibility: hidden; pointer-events: none; }
.tray-actions { display: flex; align-items: center; justify-content: center; gap: 10px; }

.settings-panel {
  --settings-width: 580px;
  position: fixed;
  z-index: 120;
  inset: 0 0 auto;
  height: 100dvh;
  display: none;
  justify-content: flex-start;
  background: rgba(4,5,7,.74);
  backdrop-filter: blur(8px);
}

.settings-panel.is-open { display: flex; }

.settings-card {
  width: 100%;
  height: 100%;
  color: var(--text);
  background: #1b1e24;
  border-right: 0;
  box-shadow: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  scroll-padding-top: calc(max(60px, calc(52px + env(safe-area-inset-top))) + 12px);
  scroll-padding-bottom: 12px;
}

.settings-header {
  position: sticky;
  top: 0;
  z-index: 2;
  height: max(60px, calc(52px + env(safe-area-inset-top)));
  padding: env(safe-area-inset-top) max(14px, calc((100% - var(--settings-width)) / 2)) 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #383e48;
  background: #1b1e24;
}

.settings-header h2, .settings-section h3 { margin: 0; }
.settings-close {
  width: 42px;
  height: 42px;
  border: 0;
  color: #cbd0d8;
  background: transparent;
  font-size: 32px;
  cursor: pointer;
}

.settings-content {
  width: 100%;
  padding: 0;
}

.settings-content-inner {
  width: min(calc(100% - 28px), var(--settings-width));
  margin: 0 auto;
  padding: 8px 0 max(80px, calc(30px + env(safe-area-inset-bottom)));
}

.settings-section {
  padding: 18px 0 22px;
  border-bottom: 1px solid #343a44;
}

.settings-section h3 { font-size: 18px; }
.settings-section p { margin: 6px 0 16px; color: var(--muted); font-size: 14px; }
.setting-toggle { min-height: 48px; display: flex; align-items: center; justify-content: space-between; margin: 10px 0; }
.setting-toggle input { width: 24px; height: 24px; accent-color: var(--accent); }

.drag-settings {
  display: grid;
  gap: 8px;
  margin: 10px 0 18px;
}

.drag-settings label {
  min-height: 52px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(154px, 48%);
  align-items: center;
  gap: 12px;
}

.drag-settings input {
  width: 100%;
  min-height: 44px;
  padding: 5px 8px;
  border: 1px solid #505865;
  border-radius: 3px;
  color: var(--text);
  background: #171a1f;
  text-align: center;
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  font-size: 17px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.number-stepper {
  display: grid;
  grid-template-columns: 44px minmax(48px, 1fr) 44px;
  gap: 4px;
}

.number-stepper button {
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 1px solid #505865;
  border-radius: 3px;
  color: var(--text);
  background: #303640;
  font-size: 25px;
  line-height: 1;
}

.number-stepper button:active { background: #424a57; transform: scale(.95); }

.board-size-setting {
  display: grid;
  gap: 8px;
  margin: 4px 0 8px;
}

.segmented-control {
  --selected-index: 1;
  position: relative;
  height: 48px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 3px;
  border: 1px solid #505865;
  border-radius: 6px;
  background: #171a1f;
  overflow: hidden;
}

.segmented-indicator {
  position: absolute;
  z-index: 0;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc((100% - 6px) / var(--segment-count, 3));
  border-radius: 4px;
  background: var(--accent-dark);
  transform: translateX(calc(var(--selected-index) * 100%));
  transition: transform .18s ease;
}

.segmented-control button {
  position: relative;
  z-index: 1;
  min-width: 0;
  border: 0;
  color: var(--muted);
  background: transparent;
  font-weight: 700;
}

.segmented-control button.selected { color: white; }

.mode-segmented-control {
  grid-template-columns: repeat(var(--segment-count, 3), 1fr);
  margin-bottom: 12px;
}

.mode-segmented-control .segmented-indicator {
  width: calc((100% - 6px) / var(--segment-count, 3));
}

.tray[aria-busy="true"] .tray-slot { cursor: default; }

.board-mode-tabs { --segment-count: 2; grid-template-columns: repeat(2, 1fr); margin-bottom: 22px; }
.segmented-control button[aria-selected="true"] { color: white; }
.drag-settings [hidden], .mode-segmented-control [hidden] { display: none; }

.general-heading { margin-bottom: 12px !important; }

.multiplier-stepper { position: relative; }
.multiplier-stepper::after {
  content: "×";
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: 17px;
  transform: translate(-50%, -50%);
  color: var(--muted);
  pointer-events: none;
}
.multiplier-stepper input { padding-right: 18px; }

.settings-action {
  min-height: 46px;
  padding: 0 16px;
  border: 1px solid #505865;
  border-radius: 4px;
  color: #dce0e6;
  background: #292e36;
  cursor: pointer;
}
.danger-action { color: #e3a5ac; border-color: #704b51; }
.primary-setting-action { color: white; background: var(--accent-dark); border-color: var(--accent-bright); }

.reset-settings-section { padding-top: 10px; }
.reset-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.reset-confirmation {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid #704b51;
  background: #2b2328;
}
.reset-confirmation[hidden] { display: none; }
.reset-confirmation p { margin: 0 0 12px; color: #e1c4c8; }
.reset-confirmation > div { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.piece-settings-list { display: grid; gap: 8px; }
.piece-setting-row {
  min-height: 62px;
  display: grid;
  grid-template-columns: 30px 48px minmax(0, 1fr) minmax(142px, 45%) 34px;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  background: #242830;
  border: 1px solid #343a44;
}
.piece-setting-row > input[type="checkbox"] { width: 23px; height: 23px; accent-color: var(--accent); }
.settings-piece-preview { display: grid; place-items: center; }

.hue-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 48px;
  align-items: center;
  gap: 8px;
}

.drag-settings .hue-control input[type="range"] {
  width: 100%;
  min-height: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  accent-color: hsl(var(--piece-hue) 55% 58%);
}

.hue-control output {
  color: var(--text);
  text-align: right;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.piece-setting-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.frequency-input, .designer-fields input {
  width: 100%;
  min-height: 34px;
  padding: 5px 7px;
  border: 1px solid #505865;
  border-radius: 3px;
  color: var(--text);
  background: #171a1f;
}
.frequency-control {
  display: grid;
  grid-template-columns: 44px minmax(42px, 1fr) 44px;
  gap: 4px;
}
.frequency-input {
  min-width: 0;
  min-height: 44px;
  padding-inline: 2px;
  text-align: center;
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  font-size: 17px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* Remove browser number spinners and keep values visually centered. */
input[type="number"] {
  appearance: textfield;
  -moz-appearance: textfield;
}

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
}

.number-stepper input,
.frequency-control input {
  display: block;
  height: 44px;
  padding-top: 0;
  padding-bottom: 0;
}
.frequency-button {
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 1px solid #505865;
  border-radius: 3px;
  color: var(--text);
  background: #303640;
  font-size: 25px;
  line-height: 1;
  cursor: pointer;
}
.frequency-button:active { background: #424a57; transform: scale(.95); }
.remove-piece-button { width: 34px; height: 42px; padding: 0; border: 0; color: #d6959d; background: transparent; font-size: 22px; cursor: pointer; }

.shape-editor {
  position: relative;
  width: min(100%, 340px);
  margin: 12px auto 18px;
}
.shape-editor button {
  position: absolute;
  padding: 0;
  border: 0;
  clip-path: var(--hex-points);
  background: #292e36;
  box-shadow: inset 0 0 0 1px var(--grid);
  cursor: pointer;
}
.shape-editor button::after { content: ""; position: absolute; inset: 2px; clip-path: var(--hex-points); background: #343b47; }
.shape-editor button.selected::after { background: var(--accent-bright); }
.shape-editor button:focus-visible::after { background: #abb9cc; }
.shape-editor button.selected { background: var(--accent); box-shadow: inset 0 0 0 2px var(--accent-bright); }
.designer-fields { display: grid; grid-template-columns: minmax(0, 1fr) 100px; gap: 8px; }
.designer-fields label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
.designer-fields input { min-height: 44px; font-size: 16px; }
.designer-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}
.designer-actions .settings-action { flex: 1; }
.designer-message { min-height: 20px; margin-top: 10px; color: #d9a0a7; font-size: 13px; }

.settings-footer {
  padding: 28px 0 max(10px, env(safe-area-inset-bottom));
  color: var(--muted);
  text-align: center;
  font-size: 12px;
}

.settings-footer a { color: var(--accent-bright); }

#appVersion {
  padding: 4px;
  border: 0;
  background: none;
  color: var(--accent-bright);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.fullscreen-button {
  position: fixed;
  z-index: 40;
  left: max(14px, env(safe-area-inset-left));
  bottom: max(14px, env(safe-area-inset-bottom));
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid #454b56;
  border-radius: 6px;
  color: #c3c8d0;
  background: #252930;
  font-size: 25px;
  line-height: 1;
  cursor: pointer;
}

.fullscreen-button:hover { background: #30353e; }
.fullscreen-button:active { transform: scale(.94); }
.fullscreen-button .fullscreen-exit { display: none; }
.fullscreen-button.is-fullscreen .fullscreen-enter { display: none; }
.fullscreen-button.is-fullscreen .fullscreen-exit { display: block; }
.fullscreen-button.unsupported { display: none; }
html[data-native-fullscreen] .fullscreen-button { display: none; }

.drag-layer {
  position: fixed;
  z-index: 30;
  left: 0;
  top: 0;
  display: none;
  pointer-events: none;
  filter: drop-shadow(0 8px 7px rgba(49,57,67,.26));
}

.drag-layer.active { display: block; }
.drag-layer.returning { transition: transform .04s ease-out; }
.drag-layer.ai-moving { transition: transform .42s cubic-bezier(.3, .7, .25, 1); }
.drag-cell { inset: 0; border-radius: 0; }
.drag-layer.invalid { opacity: .6; filter: grayscale(.55) drop-shadow(0 8px 7px rgba(49,57,67,.2)); }

.score-burst {
  position: absolute;
  z-index: 10;
  left: 50%;
  top: 45%;
  color: #d9fff7;
  font-size: 28px;
  font-weight: 900;
  opacity: 0;
  pointer-events: none;
  text-shadow: 0 4px 18px #0fcba2;
}

.score-burst.show { animation: burst .7s ease-out; }
@keyframes burst { 0% { opacity: 0; transform: translate(-50%, 10px) scale(.7); } 30% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -60px) scale(1.15); } }

.modal {
  position: fixed;
  z-index: 100;
  inset: 0;
  display: none;
  place-items: center;
  padding: 24px;
  background: rgba(4,5,7,.7);
  backdrop-filter: blur(12px);
}

.modal.is-open { display: grid; }

.modal.game-over-modal {
  background: rgba(4,5,7,.3);
  backdrop-filter: blur(3px);
}

.modal.game-over-modal .modal-card {
  width: min(calc(100% - 28px), 390px);
  min-width: 0;
  padding: 18px 20px 20px;
}

.modal.game-over-modal .modal-actions { flex-wrap: wrap; }
.modal.game-over-modal .modal-card h1 { margin-bottom: 4px; font-size: 24px; }
.modal.game-over-modal .modal-card p { margin-bottom: 8px; font-size: 13px; }

.modal-score {
  display: block;
  margin: 2px 0 14px;
  font-size: 38px;
  font-weight: 700;
  line-height: 1;
}

.modal-score[hidden] { display: none; }

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.modal-undo-button {
  min-height: 50px;
  padding: 0 20px;
  border: 1px solid #505865;
  border-radius: 6px;
  color: #dce0e6;
  background: #292e36;
  font-weight: 700;
  cursor: pointer;
}

.modal-undo-button[hidden] { display: none; }

.modal-card {
  width: min(100%, 420px);
  padding: 38px 30px 30px;
  text-align: center;
  border: 1px solid #4a505b;
  border-radius: 10px;
  background: #22262d;
  box-shadow: 0 20px 60px rgba(0,0,0,.48);
}

.changelog-modal { z-index: 130; }
.changelog-modal .modal-card {
  max-height: calc(100dvh - 48px);
  overflow-y: auto;
}
.changelog-modal .modal-card p { margin-bottom: 14px; }
.changelog-list {
  margin: 0 auto 26px;
  padding-left: 20px;
  max-width: 330px;
  color: #c3c8d0;
  text-align: left;
  line-height: 1.5;
}
.changelog-list li + li { margin-top: 6px; }

.modal-card h1 { margin: 0 0 10px; font-size: 32px; }
.modal-card p { margin: 0 auto 26px; max-width: 330px; color: #a8aeb8; line-height: 1.55; }

.primary-button {
  min-width: 180px;
  min-height: 50px;
  padding: 0 24px;
  border: 0;
  border-radius: 6px;
  color: white;
  background: linear-gradient(145deg, var(--accent-bright), var(--accent-dark));
  box-shadow: 0 5px 14px rgba(62,89,118,.24);
  font-weight: 850;
  cursor: pointer;
}

@media (max-height: 760px) {
  :root { --board-size: min(calc(100vw - 10px), 52vh, 500px); }
  .game-shell { padding-top: 10px; }
  .score-panel { margin-top: 2px; margin-bottom: 8px; }
  .score { font-size: 32px; }
  .tray-section { min-height: 142px; }
  .tray, .tray-slot { min-height: 100px; }
  .tray-slot { height: 100px; }
}

@media (min-height: 761px) {
  .score-panel { margin-top: -10px; margin-bottom: 8px; }
}

@media (max-width: 420px) {
  .best-score { max-width: 34%; }
  .best-score .score-label { font-size: 12px; line-height: 1.2; }
  .piece-setting-row { grid-template-columns: 24px 48px minmax(0, 1fr) 34px; }
  .piece-setting-row > input { grid-column: 1; grid-row: 1; }
  .settings-piece-preview { grid-column: 2; grid-row: 1; }
  .piece-setting-name { grid-column: 3; grid-row: 1; }
  .piece-setting-row .frequency-control { grid-column: 2 / 5; grid-row: 2; }
  .remove-piece-button { grid-column: 4; grid-row: 1; }

  html:not([data-native-fullscreen]):not(:fullscreen):not(:-webkit-full-screen) .game-shell {
    padding-top: max(9px, env(safe-area-inset-top));
  }

  html:not([data-native-fullscreen]):not(:fullscreen):not(:-webkit-full-screen) .topbar {
    min-height: 48px;
  }

  html:not([data-native-fullscreen]):not(:fullscreen):not(:-webkit-full-screen) .score-panel {
    min-height: 60px;
    margin-top: -7px;
    margin-bottom: 7px;
  }
}

@media (min-width: 760px) {
  .tray-slot { height: 140px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  .game-shell {
    --landscape-controls-width: 200px;
    --board-size: min(
      calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 22px) * var(--board-aspect, 1) + 22px),
      calc(100vw - max(14px, env(safe-area-inset-left)) - max(14px, env(safe-area-inset-right)) - 104px - var(--landscape-controls-width))
    );
    width: 100%;
    height: 100dvh;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(104px, .6fr) var(--board-size) minmax(var(--landscape-controls-width), 1fr);
    grid-template-rows: minmax(0, 1fr);
    padding: env(safe-area-inset-top) max(14px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(14px, env(safe-area-inset-left));
  }
  .topbar {
    position: fixed;
    z-index: 3;
    top: max(8px, env(safe-area-inset-top));
    right: max(14px, env(safe-area-inset-right));
    left: max(14px, env(safe-area-inset-left));
    min-height: 44px;
    padding: 0;
    flex-direction: row;
    pointer-events: none;
  }
  .topbar button { pointer-events: auto; }
  .board-wrap { grid-column: 2; grid-row: 1 / -1; align-self: center; padding: 11px; }
  .score-panel {
    grid-column: 1;
    grid-row: 1;
    align-self: center;
    width: 100%;
    height: calc((var(--board-size) - 22px) / var(--board-aspect, 1) + 22px);
    min-width: 0;
    min-height: 0;
    margin: 0;
    padding: 14px 6px 14px 0;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-between;
    text-align: center;
  }
  .score-panel > div:first-child { align-self: flex-end; min-width: 44px; }
  .best-score { position: static; max-width: 100%; text-align: center; }
  .score-label { font-size: 12px; }
  .score { font-size: 28px; }
  .timer-display { font-size: 24px; }
  .match-scoreboard { grid-template-columns: 1fr; gap: 6px; margin-block: auto; }
  .turn-status { font-size: 12px; }
  .tray-section {
    grid-column: 3;
    grid-row: 1 / -1;
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    padding: 22px 0 14px 8px;
    gap: 8px;
  }
  .tray-carousel { height: calc(100% - 8px); align-self: start; min-height: 0; }
  .tray-viewport { height: 100%; min-height: 0; }
  .tray {
    height: 100%;
    min-height: 0;
    grid-auto-flow: row;
    grid-template-columns: minmax(0, 1fr);
    grid-auto-columns: auto;
    grid-auto-rows: calc((100% - (var(--tray-columns, 3) - 1) * 3px) / var(--tray-columns, 3));
  }
  .tray-slot { min-width: 0; min-height: 0; height: 100%; }
  .tray-arrow { width: 44px; height: 28px; transform: translateX(-50%); }
  .tray-arrow svg { width: 24px; height: 24px; transform: rotate(90deg); }
  #trayPreviousButton { top: -22px; left: 50%; }
  #trayNextButton { top: auto; right: auto; bottom: -22px; left: 50%; }
  .tray-actions { align-self: end; min-height: 94px; flex-direction: column; justify-content: flex-end; }
  .tray-actions .undo-button { min-width: 104px; width: 100%; padding: 0 8px; }
  #undoButton { order: 1; }
}

@media (orientation: landscape) and (max-height: 500px) and (max-width: 700px) {
  .game-shell { --landscape-controls-width: 136px; }
  .tray-section {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) 94px;
    padding: 52px 0 14px;
    gap: 22px;
  }
  .tray-carousel { height: 100%; }
  .tray-actions { justify-self: end; }
}

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