/* ==================================================================== *
 * Weave — design tokens
 *
 * Light is the base; dark arrives two ways, and the manual toggle has to beat
 * the OS in both directions:
 *   - @media (prefers-color-scheme: dark) guarded with :root:not([data-theme="light"])
 *     so picking light explicitly survives an OS that prefers dark;
 *   - :root[data-theme="dark"] afterwards so picking dark explicitly works on
 *     an OS that prefers light.
 * ==================================================================== */

:root {
  --bg: #F7F8FB;
  --surface: #FFFFFF;
  --surface-2: #F0F2F7;
  --line: #E2E6EF;
  --text: #10131A;
  --text-dim: #666E82;

  --accent: #00D1B2;
  --accent-ink: #0E0F13;

  /* Path palette, in order. Fixed — these are the game's identity. */
  --p1: #F2495C;
  --p2: #3B82F6;
  --p3: #F2A93B;
  --p4: #22C55E;
  --p5: #A855F7;
  --p6: #EC4899;
  --p7: #14B8A6;
  --p8: #FACC15;

  --radius-card: 16px;
  --radius-btn: 10px;
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 24px;
  --s6: 32px;

  --fast: 140ms;
  --shadow: 0 1px 2px rgba(16, 19, 26, 0.04), 0 8px 24px rgba(16, 19, 26, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0E0F13;
    --surface: #171922;
    --surface-2: #1F2230;
    --line: #2A2E3F;
    --text: #F2F4F8;
    --text-dim: #8A90A2;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 32px rgba(0, 0, 0, 0.36);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0E0F13;
  --surface: #171922;
  --surface-2: #1F2230;
  --line: #2A2E3F;
  --text: #F2F4F8;
  --text-dim: #8A90A2;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 32px rgba(0, 0, 0, 0.36);
  color-scheme: dark;
}

/* ---------------- base ---------------- */

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }

button { font: inherit; color: inherit; }

/* ---------------- top bar ---------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
  padding: var(--s3) var(--s4);
  padding-top: calc(var(--s3) + env(safe-area-inset-top));
  max-width: 560px;
  margin: 0 auto;
}

.brand { display: flex; align-items: center; gap: var(--s2); }
.brand-mark { display: flex; }
.brand-name { font-weight: 700; font-size: 19px; letter-spacing: -0.015em; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: var(--radius-btn);
  background: var(--surface);
  cursor: pointer;
  transition: background var(--fast) ease-out, border-color var(--fast) ease-out,
              transform var(--fast) ease-out;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn .ico-moon { display: none; }
.icon-btn.is-dark .ico-sun { display: none; }
.icon-btn.is-dark .ico-moon { display: block; }

:is(button, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------------- layout ---------------- */

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 var(--s4) calc(var(--s6) + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: var(--s3);
}

/* ---------------- segmented controls ---------------- */

.controls { display: flex; flex-direction: column; gap: var(--s2); }

.seg {
  display: grid;
  /* minmax(0, 1fr), never bare 1fr: a bare 1fr is minmax(auto, 1fr) and
     refuses to shrink under its content, which overflows narrow phones. */
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: var(--s1);
  padding: var(--s1);
  background: var(--surface-2);
  border-radius: var(--radius-btn);
}

.seg-btn {
  min-height: 36px;
  padding: 0 var(--s2);
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background var(--fast) ease-out, color var(--fast) ease-out;
}
.seg-btn:hover { color: var(--text); }
.seg-btn.on {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(16, 19, 26, 0.08);
}

/* ---------------- stats ---------------- */

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--s2);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: var(--s2) var(--s1);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-btn);
}

.stat-label {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.stat-value { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; }

/* ---------------- board ---------------- */

.board {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  /* Tight on phones on purpose: at 375px this is what keeps a 9x9 cell at
     36px, the minimum comfortable touch target. */
  padding: var(--s2);
  box-shadow: var(--shadow);
}

@media (min-width: 420px) {
  .board { padding: var(--s3); }
  .board-skeleton { inset: var(--s3); }
}

#boardSvg {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  /* Essential: without this the browser claims the gesture for scrolling and
     pointermove stops arriving mid-drag. */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
  border-radius: 6px;
}

.board .grid line {
  stroke: var(--line);
  stroke-width: 0.018;
}

.board .dots circle {
  fill: var(--text-dim);
  opacity: 0.4;
  transition: opacity var(--fast) ease-out;
}
.board .dots circle.covered { opacity: 0; }

.board .pipe { transition: none; }

/* The glow layer sits under the pipes and is invisible until the win. */
.board .glow { opacity: 0; }

.board .end-core {
  fill: var(--surface);
  opacity: 0;
  transition: opacity var(--fast) ease-out;
}
.board .end-core.linked { opacity: 0.85; }

.board .head-ring {
  stroke-width: 0.06;
  opacity: 0;
  transition: opacity var(--fast) ease-out;
}
.board .head-ring.on { opacity: 0.9; }

.board .kb-cursor {
  stroke: var(--accent);
  stroke-width: 0.05;
  opacity: 0;
}
.board .kb-cursor.on { opacity: 1; }

.board-skeleton {
  position: absolute;
  inset: var(--s2);
  border-radius: 6px;
  background: var(--surface-2);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--fast) ease-out;
}
.board.busy .board-skeleton { opacity: 1; animation: shimmer 1.1s ease-in-out infinite; }

@keyframes shimmer {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

/* Win: each pipe swells and flares in turn, then settles. */
.board.won .glow {
  animation: flare 520ms ease-out both;
  animation-delay: calc(var(--i, 0) * 90ms);
}
.board.won .pipe {
  animation: swell 520ms ease-out both;
  animation-delay: calc(var(--i, 0) * 90ms);
}
.board.won .end {
  animation: pop 520ms ease-out both;
  animation-delay: calc(var(--i, 0) * 90ms);
}

@keyframes flare {
  0% { opacity: 0; }
  40% { opacity: 0.45; }
  100% { opacity: 0.18; }
}
@keyframes swell {
  0% { stroke-width: 0.38; }
  40% { stroke-width: 0.46; }
  100% { stroke-width: 0.4; }
}
@keyframes pop {
  0% { r: 0.3; }
  40% { r: 0.36; }
  100% { r: 0.32; }
}

/* ---------------- status, actions ---------------- */

.status {
  margin: 0;
  min-height: 20px;
  text-align: center;
  font-size: 13px;
  color: var(--text-dim);
}

.actions {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: var(--s2);
}

.btn {
  min-height: 42px;
  padding: 0 var(--s4);
  border: 1px solid var(--line);
  border-radius: var(--radius-btn);
  background: var(--surface);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--fast) ease-out, border-color var(--fast) ease-out,
              transform var(--fast) ease-out;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(0.98); }

.btn-accent {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn-accent:hover { filter: brightness(1.06); }

.share-note {
  margin: 0;
  min-height: 16px;
  text-align: center;
  font-size: 12px;
  color: var(--accent);
  opacity: 0;
  transition: opacity var(--fast) ease-out;
}
.share-note.on { opacity: 1; }

/* ---------------- rules ---------------- */

.rules {
  margin-top: var(--s3);
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
}
.rules h2 {
  margin: 0 0 var(--s2);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.rules ul { margin: 0; padding-left: var(--s5); }
.rules li { margin-bottom: var(--s1); font-size: 13.5px; color: var(--text-dim); }
.rules li:last-child { margin-bottom: 0; }
.rules em { color: var(--text); font-style: normal; font-weight: 600; }

/* ---------------- win card ---------------- */

.win-card {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  justify-content: center;
  padding: var(--s4);
  padding-bottom: calc(var(--s4) + env(safe-area-inset-bottom));
  pointer-events: none;
}

/* A class rule setting `display` outruns the user-agent's [hidden] rule, so
   the hidden state has to be restated here or the card never goes away. */
.win-card[hidden], .btn[hidden] { display: none; }

.win-inner {
  pointer-events: auto;
  width: 100%;
  max-width: 420px;
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06), 0 16px 48px rgba(0, 0, 0, 0.18);
  text-align: center;
}

.win-card.enter .win-inner { animation: rise 460ms cubic-bezier(0.16, 1, 0.3, 1) both; }

@keyframes rise {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.win-kicker {
  display: inline-block;
  padding: 3px var(--s2);
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.win-meta { margin: var(--s2) 0 var(--s3); font-size: 13px; color: var(--text-dim); }

.win-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--s2);
  margin-bottom: var(--s4);
}
.win-stats > div {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--s2);
  background: var(--surface-2);
  border-radius: var(--radius-btn);
}
.win-num { font-size: 24px; font-weight: 700; letter-spacing: -0.02em; }

.win-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--s2);
}

/* ---------------- motion preference ---------------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .board.won .glow { opacity: 0.18; }
}

/* ---------------- wider screens ---------------- */

@media (min-width: 620px) {
  body { font-size: 16px; }
  .controls { flex-direction: row; }
  .controls .seg { flex: 1; }
  main { gap: var(--s4); }
}
