/* Flow — pipes of liquid light. Theme colours arrive as custom properties
   from themes.js (neon / ink / aurora); the defaults below match Neon. */

:root {
  --bg-a: #0b0f1e; --bg-b: #161b36;
  --blob1: rgba(124, 92, 255, 0.35); --blob2: rgba(46, 196, 255, 0.28); --blob3: rgba(255, 77, 109, 0.22);
  --board: #0d1126; --board-edge: rgba(255, 255, 255, 0.08); --cell: #151b36; --cell-line: rgba(255, 255, 255, 0.045);
  --ink: #eef0ff; --ink-soft: #9398bf; --paper: #161b33; --paper-2: #1f2545; --line: rgba(255, 255, 255, 0.1);
  --accent: #7c8cff; --accent-2: #4cc2ff; --accent-ink: #0b0f1e; --chip: rgba(255, 255, 255, 0.06); --chip-on: rgba(124, 140, 255, 0.25);
  --toast: rgba(238, 240, 255, 0.95); --toast-ink: #0b0f1e; --shadow: rgba(0, 0, 0, 0.55);
  --tint: 0.16; --glow-alpha: 0.7;
  --radius: 18px;
  --ease: cubic-bezier(0.4, 0.1, 0.2, 1);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  font-family: var(--sans);
  color: var(--ink);
  background: linear-gradient(165deg, var(--bg-a), var(--bg-b));
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  transition: background 0.8s, color 0.4s;
}

/* ---------------------------------------------------------- background */
.bg { position: fixed; inset: 0; overflow: hidden; z-index: 0; pointer-events: none; }
.bg .blob { position: absolute; border-radius: 50%; filter: blur(70px); will-change: transform; transition: background 0.8s; }
.bg .b1 { width: 62vmax; height: 62vmax; left: -22vmax; top: -28vmax; background: var(--blob1); animation: drift 52s ease-in-out infinite alternate; }
.bg .b2 { width: 54vmax; height: 54vmax; right: -22vmax; bottom: -22vmax; background: var(--blob2); animation: drift 64s ease-in-out infinite alternate-reverse; }
.bg .b3 { width: 42vmax; height: 42vmax; left: 28vw; bottom: -26vmax; background: var(--blob3); animation: drift 76s ease-in-out infinite alternate; }
.bg .veil { position: absolute; inset: 0; background: radial-gradient(ellipse at 50% 110%, rgba(255, 255, 255, 0.06), transparent 60%); }
@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to { transform: translate(9vmax, 7vmax) scale(1.15); }
}

/* -------------------------------------------------------------- layout */
.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  max-width: 760px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.top { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px 2px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { width: 40px; height: 40px; border-radius: 11px; filter: drop-shadow(0 3px 6px var(--shadow)); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; }
.brand b { font-family: var(--serif); font-weight: 600; font-size: 23px; letter-spacing: 0.03em; }
.brand small { font-size: 11px; color: var(--ink-soft); letter-spacing: 0.1em; text-transform: uppercase; margin-top: 2px; }
.menu { display: flex; gap: 2px; }

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}
.icon-btn:hover { background: var(--chip); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg, .btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 6px 16px 4px; }
.stat {
  background: var(--chip);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 5px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
}
.stat span { font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.stat b { font-family: var(--serif); font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }

.sizes { display: flex; justify-content: center; gap: 6px; padding: 6px 16px 2px; }
.size {
  appearance: none;
  border: 1px solid transparent;
  background: var(--chip);
  color: var(--ink-soft);
  font: 600 12.5px var(--sans);
  font-variant-numeric: tabular-nums;
  padding: 6px 0;
  width: 54px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.25s, color 0.25s, border-color 0.25s, transform 0.15s;
}
.size:hover { color: var(--ink); }
.size:active { transform: scale(0.95); }
.size[aria-checked="true"] { background: var(--chip-on); color: var(--ink); border-color: color-mix(in oklab, var(--accent) 50%, transparent); }

.stage { flex: 1; min-height: 0; container-type: size; display: grid; place-items: center; padding: 8px 16px; }

.actions { display: flex; gap: 10px; padding: 6px 16px 14px; }
.btn {
  appearance: none;
  flex: 1;
  border: 1px solid var(--line);
  background: var(--chip);
  color: var(--ink);
  font: 500 15px var(--sans);
  padding: 11px 12px;
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: transform 0.15s, background 0.2s, opacity 0.2s, box-shadow 0.2s;
}
.btn svg { width: 19px; height: 19px; }
.btn:hover { background: var(--chip-on); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--accent-ink);
  border-color: transparent;
  box-shadow: 0 6px 18px -6px color-mix(in oklab, var(--accent) 70%, transparent), inset 0 1px 0 rgba(255, 255, 255, 0.3);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.subtle { flex: 0 0 auto; background: transparent; border-color: transparent; box-shadow: none; color: var(--ink-soft); font-size: 13.5px; margin: 18px auto 0; display: flex; }

/* --------------------------------------------------------------- board */
.fl-board {
  width: min(100cqw, 100cqh);
  aspect-ratio: 1;
  position: relative;
  border-radius: 6.5%;
  padding: 3.2%;
  background:
    radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.05), transparent 55%),
    var(--board);
  box-shadow:
    inset 0 0 0 1px var(--board-edge),
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 30px 60px -20px var(--shadow),
    0 8px 20px -6px var(--shadow);
  touch-action: none;
  transition: background 0.6s, opacity 0.3s;
}
.fl-board.fl-thinking { opacity: 0.6; }
.fl-svg { display: block; width: 100%; height: 100%; overflow: visible; }

.fl-cell { fill: var(--cell); stroke: var(--cell-line); stroke-width: 0.02; transition: fill 0.6s; }
.fl-tintcell { opacity: 0; transition: opacity 0.25s; pointer-events: none; }
.fl-tintcell.on { opacity: var(--tint); }

.fl-glow { fill: none; stroke-width: 0.62; stroke-linecap: round; stroke-linejoin: round; opacity: var(--glow-alpha); pointer-events: none; }
.fl-glow .fl-halo { stroke: none; }
.fl-pipes { fill: none; stroke-width: 0.34; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.fl-pipe { transition: stroke-dashoffset 0.1s linear; }
.fl-pipe.fl-jump { transition: none; }
[data-theme="ink"] .fl-glow { display: none; }
[data-theme="ink"] .fl-pipes { stroke-width: 0.3; }
[data-theme="ink"] .fl-cell { stroke-width: 0.025; }

.fl-dots { pointer-events: none; }
.fl-dot { transform-box: fill-box; transform-origin: center; }
.fl-dot-ring { opacity: 0.3; transition: opacity 0.3s; }
.fl-dot-core { filter: drop-shadow(0 0.03px 0.05px rgba(0, 0, 0, 0.45)); }
.fl-dot.fl-done .fl-dot-ring { opacity: 0.55; }
.fl-dot.fl-kb .fl-dot-ring { opacity: 0.9; animation: fl-ring 1.1s ease-in-out infinite; }
.fl-dot.fl-pulse { animation: fl-pop 0.6s var(--ease); }
@keyframes fl-pop { 35% { transform: scale(1.35); } 100% { transform: scale(1); } }
@keyframes fl-ring { 50% { opacity: 0.45; } }
.fl-sym { display: none; font: 700 0.34px var(--sans); fill: var(--board); pointer-events: none; }
html.symbols .fl-sym { display: block; }
[data-theme="ink"] .fl-sym { fill: #fff; }

.fl-board.fl-won .fl-pipes { animation: fl-shine 1.6s ease-in-out; }
.fl-board.fl-won .fl-glow { animation: fl-flare 1.6s ease-in-out; }
@keyframes fl-shine { 30% { filter: brightness(1.5) saturate(1.2); } }
@keyframes fl-flare { 30% { opacity: 1; } }

/* -------------------------------------------------------- sheets, dialogs */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(5, 8, 20, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fade 0.25s;
}
[data-theme="ink"] .backdrop { background: rgba(60, 45, 30, 0.35); }
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  z-index: 21;
  max-width: 560px;
  max-height: 86dvh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -10px 40px var(--shadow), inset 0 1px 0 var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  animation: rise 0.32s var(--ease);
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 22px; flex: 0 0 auto; }
.sheet h2, .dialog h2 { font-family: var(--serif); font-weight: 600; font-size: 24px; margin: 0; letter-spacing: 0.01em; }
.sheet-body {
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 2px 22px 26px;
  font-size: 15px;
  line-height: 1.55;
}
.sheet-body h3 { font-family: var(--serif); font-weight: 600; font-size: 17px; margin: 20px 0 8px; color: var(--ink); }
.prose p { margin: 0 0 12px; }
.prose ul { margin: 0; padding-left: 20px; }
.prose li { margin: 4px 0; }
.muted { color: var(--ink-soft); }
.about { font-size: 13px; margin-top: 22px; text-align: center; }

@media (min-width: 700px) and (min-height: 560px) {
  .sheet { bottom: auto; top: 50%; transform: translateY(-50%); border-radius: 24px; max-height: 80dvh; animation: pop 0.28s var(--ease); }
}
@keyframes pop { from { opacity: 0; transform: translateY(-50%) scale(0.96); } }

.dialog {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 22;
  width: min(92vw, 400px);
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px;
  padding: 26px 24px 22px;
  box-shadow: 0 20px 60px var(--shadow), inset 0 1px 0 var(--line);
  text-align: center;
  animation: popc 0.3s var(--ease);
  overflow: hidden;
}
@keyframes popc { from { opacity: 0; transform: translate(-50%, -50%) scale(0.94); } }
.dialog p { margin: 8px 0 18px; color: var(--ink-soft); font-size: 15.5px; line-height: 1.5; }
.dialog .row { display: flex; gap: 10px; }
.result .glow {
  position: absolute;
  inset: -40% -20% auto;
  height: 70%;
  background: radial-gradient(ellipse at 50% 40%, color-mix(in oklab, var(--accent) 45%, transparent), transparent 65%);
  pointer-events: none;
  opacity: 0.8;
}
.result h2 { position: relative; font-size: 30px; }
.result[data-kind="perfect"] h2 { animation: shimmer 2.4s ease-in-out infinite; }
@keyframes shimmer { 50% { text-shadow: 0 0 18px color-mix(in oklab, var(--accent-2) 80%, white); } }
.result-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.result-stats div, .summary div { display: flex; flex-direction: column; align-items: center; background: var(--paper-2); border-radius: 12px; padding: 8px 4px; line-height: 1.2; }
.result-stats span, .summary span { font-size: 9.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); }
.result-stats b, .summary b { font-family: var(--serif); font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }
.total { display: flex; flex-direction: column; align-items: center; margin: 4px 0 18px; }
.total span { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.total b { font-family: var(--serif); font-size: 44px; font-weight: 600; line-height: 1.05; background: linear-gradient(135deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }

/* ------------------------------------------------------------- results */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
th { text-align: left; vertical-align: bottom; font-weight: 500; color: var(--ink-soft); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; padding: 4px 4px; border-bottom: 1px solid var(--line); }
td { padding: 7px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; vertical-align: middle; }
th.games, th.bestTime, th.bestScore, th.time, th.moves, th.hints, th.score,
td.games, td.bestTime, td.bestScore, td.time, td.moves, td.hints, td.score { text-align: right; }
td.score, td.bestScore { font-family: var(--serif); font-weight: 600; font-size: 15px; }
tr[data-perfect] td.score { color: var(--accent-2); }
@media (max-width: 420px) { th.hints, td.hints { display: none; } td { padding: 7px 3px; } }

/* ------------------------------------------------------------- settings */
.opts { display: flex; flex-wrap: wrap; gap: 8px; }
.opt {
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  font: 500 14px var(--sans);
  padding: 8px 14px 8px 10px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, transform 0.15s;
}
.opt:active { transform: scale(0.97); }
.opt[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent) 25%, transparent); }
.opts.lang .opt { padding-left: 14px; min-width: 96px; justify-content: center; }
.swatch { display: inline-block; width: 24px; height: 24px; border-radius: 50%; flex: 0 0 auto; }
.swatch.theme {
  background:
    radial-gradient(circle at 30% 30%, var(--sw-a) 0 22%, transparent 23%),
    radial-gradient(circle at 70% 68%, var(--sw-b) 0 22%, transparent 23%),
    var(--sw-bg);
  box-shadow: inset 0 0 0 1px rgba(127, 127, 127, 0.35);
}
.toggle { display: flex; align-items: center; gap: 12px; margin-top: 22px; cursor: pointer; font-size: 15px; line-height: 1.35; }
.toggle input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle .track { flex: 0 0 auto; width: 46px; height: 28px; border-radius: 999px; background: var(--line); position: relative; transition: background 0.25s; }
.toggle .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.25s var(--ease); }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::after { transform: translateX(18px); }
.toggle input:focus-visible + .track { outline: 2px solid var(--accent-2); outline-offset: 2px; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  z-index: 15;
  background: var(--toast);
  color: var(--toast-ink);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  max-width: min(88vw, 420px);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ------------------------------------------------------- small screens */
@media (max-height: 640px) {
  .top { padding-top: 6px; }
  .brand small { display: none; }
  .brand b { font-size: 19px; }
  .brand img { width: 32px; height: 32px; }
  .stat b { font-size: 16px; }
  .stat { padding: 3px 6px; }
  .sizes { padding-top: 4px; }
  .size { padding: 4px 0; }
  .stage { padding: 6px 16px; }
  .actions { padding-bottom: 10px; }
  .btn { padding: 8px 10px; }
}
@media (max-width: 520px) {
  .btn { flex-direction: column; gap: 3px; padding: 8px 6px 7px; font-size: 12.5px; }
  .btn span { white-space: nowrap; }
  .btn svg { width: 21px; height: 21px; }
}
@media (max-width: 520px) and (max-height: 640px) {
  .btn { flex-direction: row; gap: 6px; padding: 8px 6px; }
}
@media (min-width: 900px) and (min-height: 700px) {
  .brand b { font-size: 26px; }
  .stage { padding: 14px 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .bg .blob { animation: none; }
  .fl-pipe, .fl-dot, .sheet, .dialog, .backdrop, .fl-board.fl-won .fl-pipes, .fl-board.fl-won .fl-glow { animation: none; transition-duration: 0.01s; }
}
