:root {
  color-scheme: light dark;
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --ink: #1d1a24;
  --muted: #625c6b;
  --line: rgb(29 26 36 / 0.16);
  --edge: #1d1a24;
  --mark: #1d1a24;
  --accent: #2357d1;
  --danger: #c22f2a;
  --r0: oklch(0.8 0.09 300);
  --r1: oklch(0.85 0.1 60);
  --r2: oklch(0.83 0.08 240);
  --r3: oklch(0.86 0.1 140);
  --r4: oklch(0.89 0.005 280);
  --r5: oklch(0.76 0.11 25);
  --r6: oklch(0.93 0.11 100);
  --r7: oklch(0.85 0.08 190);
  --r8: oklch(0.87 0.07 350);
  --r9: oklch(0.76 0.06 70);
  --target-size: 2.25rem;
  /* The board fills the width, but no more of the height than leaves room
     for the controls, so it fits without scrolling. */
  --board-size: min(100%, max(18rem, 100vh - 19rem));

  @media (prefers-color-scheme: dark) {
    --bg: #17161a;
    --surface: #232229;
    --ink: #f1eef6;
    --muted: #a8a1b1;
    --line: rgb(0 0 0 / 0.28);
    --edge: #050407;
    --mark: #fbf9fe;
    --accent: #7ea6ff;
    --danger: #ff6b63;
    --r0: oklch(0.5 0.1 300);
    --r1: oklch(0.55 0.1 60);
    --r2: oklch(0.5 0.09 240);
    --r3: oklch(0.52 0.1 140);
    --r4: oklch(0.47 0.005 280);
    --r5: oklch(0.5 0.12 25);
    --r6: oklch(0.6 0.1 100);
    --r7: oklch(0.52 0.08 190);
    --r8: oklch(0.55 0.09 350);
    --r9: oklch(0.45 0.06 70);
  }

  @media (pointer: coarse) {
    --target-size: 2.75rem;
  }
}

@supports (height: 100svh) {
  :root {
    --board-size: min(100%, max(18rem, 100svh - 19rem));
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  accent-color: var(--accent);
}

body {
  margin: 0;
  padding: max(0.75rem, env(safe-area-inset-top))
    max(0.75rem, env(safe-area-inset-right))
    max(1rem, env(safe-area-inset-bottom))
    max(0.75rem, env(safe-area-inset-left));
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  font-size: 1.0625rem;
  line-height: 1.5;
}

.app {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: min(100%, 34rem);
  margin: 0 auto;
}

.masthead {
  display: grid;
  gap: 0.5rem;
}

.masthead h1 {
  margin: 0;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.15;
  text-wrap: balance;
}

.segmented {
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  margin: 0;
  padding: 0.2rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
}

.segmented label {
  position: relative;
  display: grid;
  cursor: pointer;
}

.segmented input {
  position: absolute;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}

.segmented span {
  display: grid;
  place-items: center;
  min-block-size: var(--target-size);
  padding: 0 0.5rem;
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
}

.segmented label:hover span {
  color: var(--ink);
}

.segmented input:checked + span {
  background: var(--ink);
  color: var(--surface);
}

.segmented input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.play {
  display: grid;
  justify-items: center;
  gap: 0.6rem;
}

.info {
  display: flex;
  justify-content: space-between;
  width: var(--board-size);
  color: var(--muted);
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}

.timer {
  font-weight: 600;
}

.board-frame {
  width: var(--board-size);
  container-type: inline-size;
}

.board {
  --n: 7;
  display: grid;
  grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  aspect-ratio: 1;
  overflow: hidden;
  border: 2px solid var(--edge);
  border-radius: 0.5rem;
  background: var(--surface);
  font-size: calc(100cqi / var(--n) * 0.45);
  line-height: 1;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.board[aria-busy="true"] {
  opacity: 0.45;
}

.row {
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
}

/* Each cell draws a thin line on its right and bottom inside a region,
   and a thick line on every side that borders another region, so a
   region edge is twice as thick. */
.cell {
  --top: 0px;
  --left: 0px;
  --right: 1px;
  --bottom: 1px;
  --right-color: var(--line);
  --bottom-color: var(--line);
  position: relative;
  display: grid;
  place-items: center;
  background: var(--region);
  box-shadow: inset 0 var(--top) var(--edge),
    inset var(--left) 0 var(--edge),
    inset calc(-1 * var(--right)) 0 var(--right-color),
    inset 0 calc(-1 * var(--bottom)) var(--bottom-color);
  color: var(--mark);
  cursor: pointer;
}

.cell.edge-top {
  --top: 1.5px;
}

.cell.edge-left {
  --left: 1.5px;
}

.cell.edge-right {
  --right: 1.5px;
  --right-color: var(--edge);
}

.cell.edge-bottom {
  --bottom: 1.5px;
  --bottom-color: var(--edge);
}

.cell.last-col {
  --right: 0px;
}

.cell.last-row {
  --bottom: 0px;
}

.cell:focus-visible {
  z-index: 1;
  outline: 3px solid var(--accent);
  outline-offset: -3px;
}

.cell svg {
  width: 62%;
  height: auto;
  aspect-ratio: 1;
  fill: currentcolor;
}

.cell[data-mark="x"]::before {
  content: "×";
  font-size: 1.1em;
  font-weight: 500;
  opacity: 0.75;
}

.cell.dot::before {
  content: "";
  width: 16%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: currentcolor;
  opacity: 0.4;
}

.cell.conflict {
  color: var(--danger);
  background-image: repeating-linear-gradient(
    -45deg,
    transparent 0 4px,
    color-mix(in oklch, var(--danger) 22%, transparent) 4px 7px
  );
}

.board.hinting .cell::after,
.cell.hint-target::after {
  content: "";
  position: absolute;
  pointer-events: none;
}

/* A hint fades the cells it isn't about, rather than tinting the ones it
   is, so region colors stay true where the player is looking. */
.board.hinting .cell:not(.hint-reason, .hint-target)::after {
  inset: 0;
  background: color-mix(in oklch, var(--bg) 62%, transparent);
}

.cell.hint-target::after {
  inset: 12%;
  border: 3px solid var(--accent);
  border-radius: 25%;
}

@media (prefers-reduced-motion: no-preference) {
  .cell.hint-target::after {
    animation: pulse 1.2s ease-in-out infinite alternate;
  }

  .board.won .cell[data-mark="star"] svg {
    animation: pop 500ms ease-out both;
    animation-delay: calc(var(--order) * 60ms);
  }
}

@keyframes pulse {
  from {
    scale: 1;
  }

  to {
    scale: 0.82;
  }
}

@keyframes pop {
  50% {
    scale: 1.35;
  }
}

.message {
  width: var(--board-size);
  min-block-size: 2.8em;
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.4;
  text-wrap: pretty;
}

.message.hint {
  color: var(--ink);
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.4rem;
  width: var(--board-size);
}

button {
  appearance: none;
  min-block-size: var(--target-size);
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  touch-action: manipulation;
}

button:hover {
  border-color: var(--ink);
}

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

button:disabled {
  opacity: 0.45;
  cursor: default;
}

button:disabled:hover {
  border-color: var(--line);
}

button.primary {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--surface);
}

button[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
}

.rules {
  width: var(--board-size);
  color: var(--muted);
  font-size: 0.95rem;
}

.rules summary {
  padding-block: 0.5rem;
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}

.rules p {
  margin: 0 0 0.6rem;
  text-wrap: pretty;
}

dialog {
  width: min(100% - 2rem, 20rem);
  padding: 1.25rem;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: var(--surface);
  color: var(--ink);
  text-align: center;
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background: rgb(0 0 0 / 0.4);
}

dialog h2 {
  margin: 0 0 0.25rem;
  font-size: 1.4rem;
}

dialog p {
  margin: 0 0 1rem;
  color: var(--muted);
}

.dialog-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.noscript {
  margin: 0;
}

/* Holds the symbols that cells draw with <use>. The hidden attribute
   doesn't hide an <svg>, and display: none can stop symbols drawing. */
.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
