:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #f3eeee;
  background: #090b0e;
}

* { box-sizing: border-box; }
body { margin: 0; font-size: 14px; }
[hidden] { display: none !important; }

.game {
  width: 100%;
  max-width: 456px;
  margin: 0 auto;
  padding: 20px;
  background: radial-gradient(ellipse at 90% 0%, var(--accent-wash) 0, transparent 46%), #141317;
  border: 1px solid #35343e;
  border-radius: 20px;
  overflow: hidden;
}

.scores {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 44px;
  padding: 5px 12px 5px 6px;
  background: #1b1d25;
  border: 1px solid #373943;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 650;
  line-height: 20px;
  font-variant-numeric: tabular-nums;
  text-align: left;
  color: #f0f1f5;
}
.score-apple { display: block; flex: 0 0 32px; object-fit: contain; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.toolbar button { display: inline-flex; align-items: center; justify-content: center; gap: 5px; white-space: normal; line-height: 1.2; }
.toolbar svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.board-shell { padding: 7px; background: #090b0e; border: 1px solid #3a3c47; border-radius: 12px; }

#board {
  --board-color: #e6eff9;
  --board-grid: rgb(37 64 91 / 11%);
  --grid-size: 20;
  display: block;
  width: 100%;
  aspect-ratio: 1;
  background-color: var(--board-color);
  background-image: none;
  outline: 1px solid #ccd2d8;
  touch-action: none;
}

#board[data-grid="on"] {
  background-image:
    linear-gradient(to right, var(--board-grid) 1px, transparent 1px),
    linear-gradient(to bottom, var(--board-grid) 1px, transparent 1px);
  background-size:
    calc(100% / var(--grid-size)) calc(100% / var(--grid-size));
}

#board:focus-visible { outline: 2px solid #52667e; }

.game-footer { display: flex; justify-content: center; padding: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

button:active { background: #3a2732; }
#options-button { width: 44px; height: 44px; padding: 10px; border-color: #373943; background: #1b1d25; color: #c3c8d2; border-radius: 12px; }

.d-pad {
  display: none;
  justify-content: center;
  flex: 0 0 auto;
}

.d-pad button { display: flex; align-items: center; justify-content: center; padding: 0; background: #1c1f27; border-color: #3b3e49; color: #edf1f7; border-radius: 16px; line-height: 1; user-select: none; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.d-pad svg { width: 30px; height: 30px; fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
@media (pointer: coarse) {
  .game-footer { padding: 22px 0 4px; }
  .d-pad {
    --d-pad-size: clamp(60px, 17vw, 68px);
    --d-pad-gap: clamp(20px, 6vw, 28px);
    display: grid;
    grid-template-columns: repeat(3, var(--d-pad-size));
    grid-template-rows: repeat(2, var(--d-pad-size));
    gap: var(--d-pad-gap);
  }
}
.d-pad button:active { border-color: var(--accent-border); background: var(--accent-deep); }
.up { grid-column: 2; grid-row: 1; }
.left { grid-column: 1; grid-row: 2; }
.down { grid-column: 2; grid-row: 2; }
.right { grid-column: 3; grid-row: 2; }

.start-screen {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: #f3eeee;
  text-align: center;
  pointer-events: none;
}
.start-panel { padding: 4px; border: 1px solid #41444f; border-radius: 16px; background: #17191ff2; }
.winner-screen { padding: 12px; background: rgb(9 11 14 / 32%); }
.winner-card { width: min(100%, 300px); padding: 20px 16px; pointer-events: auto; }
.winner-card h2 { margin: 0; font-size: clamp(20px, 6vw, 24px); line-height: 1.25; }
.winner-result { margin: 12px 0 18px; font-size: 15px; font-variant-numeric: tabular-nums; }
.winner-actions { display: grid; gap: 8px; }
.winner-actions button { min-height: 44px; }
#winner-play { background: var(--accent-strong); color: var(--accent-ink, #fff); border-color: var(--accent-border); font-weight: 700; }
.completion-pending { position: absolute; z-index: 2; bottom: 6px; left: 6px; right: 6px; margin: 0; padding: 8px; border: 1px solid #785462; border-radius: 8px; background: #201a21ed; color: #f3eeee; text-align: center; font-size: 12px; pointer-events: none; }
#start-button { min-width: 128px; min-height: 48px; padding: 10px 20px; border: 1px solid var(--accent-border); border-radius: 12px; background: var(--accent-strong); color: var(--accent-ink, #fff); font-size: 16px; font-weight: 700; line-height: 1.3; pointer-events: auto; }
#start-button:active { background: var(--accent-deep); color: var(--accent-soft); }
@media (hover: hover) {
  #options-button:hover, .d-pad button:hover { background: #292d38; color: #fff; }
  #start-button:hover { filter: brightness(1.08); }
  .theme-choice:hover, .options-link:hover { background: #302530; }
}
@media (max-width: 360px) {
  .game { padding: 12px; border-radius: 14px; }
  .board-shell { padding: 5px; }
}

.board-wrap { position: relative; }
.portal {
  --portal-boost: 0;
  --portal-active-color: var(--portal-color);
  --portal-flow: repeating-linear-gradient(0deg, transparent 0 10px, rgb(255 255 255 / 19%) 11px 13px, transparent 14px 24px);
  position: absolute;
  top: var(--portal-top);
  height: var(--portal-height);
  width: clamp(8px, 3vw, 12px);
  border: 0;
  border-radius: 999px;
  background: #293b4c;
  box-shadow:
    inset 0 0 0 1px #182735,
    0 0 calc(5px + var(--portal-boost) * 3px) color-mix(in srgb, var(--portal-active-color) 38%, transparent);
  filter: brightness(calc(1 + var(--portal-boost) * 0.45));
  z-index: 1;
  pointer-events: none;
}
.portal::before {
  content: "";
  position: absolute;
  /* The inset rim does not affect box geometry, keeping the overhang exact. */
  inset: -2px 3px;
  border-radius: inherit;
  background:
    linear-gradient(90deg, transparent, rgb(255 255 255 / 22%), transparent),
    var(--portal-flow),
    var(--portal-active-color);
  background-size: 100% 100%, 100% 24px, auto;
  animation: portal-flow 3.2s linear infinite;
}
.portal::after {
  content: "";
  position: absolute;
  /* The dark collar meets the exact top and bottom of the four-cell opening. */
  top: 0;
  bottom: 0;
  width: 4px;
  background: #293b4c;
  z-index: -1;
}
.portal[data-portal$="-left"] { right: calc(100% + 4px); }
.portal[data-portal$="-right"] { left: calc(100% + 4px); }
.portal[data-portal$="-left"]::after { left: 100%; border-radius: 0 2px 2px 0; }
.portal[data-portal$="-right"]::after { right: 100%; border-radius: 2px 0 0 2px; }
/* Blue is the default: retain the original colour pairs and inner flow exactly.
   Other themes use an accent/secondary pair from the existing theme palette.
   Single/double bands remain an additional cue for matching the two pairs. */
.game {
  --portal-secondary-color: #0285ff;
  --portal-secondary-ink: #141317;
}
.game[data-theme="yellow"] {
  --portal-secondary-color: #924ff7;
  --portal-secondary-ink: #ffffff;
}
.game[data-theme="pink"] { --portal-secondary-color: #04b84c; }
.game[data-theme="purple"] { --portal-secondary-color: #ffc300; }
.game:not([data-theme="blue"]) .portal {
  --portal-active-color: var(--accent-strong);
  --portal-flow-ink: color-mix(in srgb, var(--accent-ink) 28%, transparent);
  --portal-flow: repeating-linear-gradient(0deg, transparent 0 10px, var(--portal-flow-ink) 10px 12px, transparent 12px 24px);
}
.game:not([data-theme="blue"]) .portal[data-portal^="green-"] {
  --portal-active-color: var(--portal-secondary-color);
  --portal-flow-ink: color-mix(in srgb, var(--portal-secondary-ink) 28%, transparent);
  --portal-flow: repeating-linear-gradient(0deg, transparent 0 6px, var(--portal-flow-ink) 6px 8px, transparent 8px 11px, var(--portal-flow-ink) 11px 13px, transparent 13px 24px);
}
@keyframes portal-flow {
  from { background-position: 0 0, 0 0, 0 0; }
  to { background-position: 0 0, 0 24px, 0 0; }
}
@media (prefers-reduced-motion: reduce) {
  .portal::before { animation: none; }
}
