/* Conflux — night-rain watershed theme */

:root {
  --bg0: #0b1220;
  --bg1: #101b31;
  --well: #080e1a;
  --panel: rgba(19, 30, 54, 0.62);
  --panel-edge: rgba(196, 221, 253, 0.1);
  --tile: #1a2742;
  --tile-edge: rgba(159, 220, 235, 0.07);
  --moon: #f2e8d5;
  --water: #9fdceb;
  --ink: #e7eef9;
  --dim: #8fa0bc;
  --given: #75879f;
  --hint: #f0c86b;
  --bad: #e4604b;
  --loop: #b98bf2;
  --serif: "Fraunces", "Iowan Old Style", Georgia, serif;
  --ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --r-panel: 16px;
  --r-btn: 11px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--ui);
  color: var(--ink);
  background:
    radial-gradient(120% 90% at 18% -10%, #16263f 0%, rgba(22, 38, 63, 0) 55%),
    radial-gradient(100% 70% at 100% 110%, #12283a 0%, rgba(18, 40, 58, 0) 60%),
    linear-gradient(180deg, var(--bg0), var(--bg1) 70%, #0d1930);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 1080px;
  margin: 0 auto;
  padding: clamp(10px, 2.4vw, 26px);
  gap: clamp(10px, 2vw, 22px);
}

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

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand { display: flex; align-items: center; gap: 13px; min-width: 0; }

.brand-mark {
  width: clamp(30px, 4.4vw, 40px);
  height: auto;
  color: var(--water);
  flex: none;
}

.brand-text h1 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 560;
  font-size: clamp(21px, 3.4vw, 29px);
  letter-spacing: 0.14em;
  color: var(--moon);
  line-height: 1.05;
}

.tagline {
  margin: 2px 0 0;
  font-family: var(--serif);
  font-style: italic;
  font-weight: 340;
  font-size: clamp(11.5px, 1.6vw, 13.5px);
  color: var(--dim);
  letter-spacing: 0.02em;
}

.top-actions { display: flex; gap: 8px; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--panel-edge);
  background: var(--panel);
  color: var(--dim);
  cursor: pointer;
  transition: color .2s, border-color .2s, background-color .2s;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:hover { color: var(--ink); border-color: rgba(196, 221, 253, 0.26); }
.icon-btn[aria-pressed="false"] { color: #5d6d87; }

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

.layout {
  flex: 1;
  display: grid;
  grid-template-areas:
    "hud    stage"
    "console stage";
  grid-template-columns: minmax(230px, 290px) minmax(0, 1fr);
  grid-template-rows: auto 1fr;
  align-content: start;
  gap: clamp(12px, 2vw, 22px);
}

.hud { grid-area: hud; }
.console { grid-area: console; }
.stage { grid-area: stage; display: flex; flex-direction: column; align-items: center; }

/* ---------- hud ---------- */

.hud {
  display: flex;
  gap: clamp(14px, 2vw, 26px);
  align-items: baseline;
  padding: 14px 18px;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: var(--r-panel);
  backdrop-filter: blur(6px);
}

.stat { display: flex; flex-direction: column; gap: 3px; min-width: 0; }

.stat-label {
  font-size: 11.5px;
  color: var(--dim);
  letter-spacing: 0.04em;
}

.stat-value {
  font-family: var(--serif);
  font-weight: 500;
  font-size: clamp(19px, 2.6vw, 24px);
  color: var(--moon);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

/* ---------- console ---------- */

.console {
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-self: start;
}

.difficulty {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.chip {
  font-family: var(--ui);
  font-size: 13px;
  padding: 8px 13px;
  border-radius: 999px;
  border: 1px solid var(--panel-edge);
  background: transparent;
  color: var(--dim);
  cursor: pointer;
  transition: color .2s, border-color .2s, background-color .2s;
}
.chip:hover { color: var(--ink); }
.chip[aria-checked="true"] {
  color: var(--water);
  border-color: rgba(159, 220, 235, 0.55);
  background: rgba(159, 220, 235, 0.1);
}

.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.btn {
  font-family: var(--ui);
  font-size: 14px;
  padding: 10px 16px;
  min-height: 42px;
  border-radius: var(--r-btn);
  border: 1px solid var(--panel-edge);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  transition: background-color .2s, border-color .2s, transform .06s, opacity .2s;
}
.btn:hover { border-color: rgba(196, 221, 253, 0.28); background: rgba(19, 30, 54, 0.85); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.42; cursor: default; transform: none; }

.btn-primary {
  background: var(--moon);
  border-color: var(--moon);
  color: #17233c;
  font-weight: 600;
}
.btn-primary:hover { background: #fff6e4; border-color: #fff6e4; }

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

.board-frame {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.board-wrap {
  position: relative;
  width: min(100%, 620px, calc(100dvh - 320px));
  min-width: min(92vw, 300px);
  aspect-ratio: 1;
  border-radius: 20px;
  outline: none;
  touch-action: manipulation;
}
.board-wrap:focus-visible {
  box-shadow: 0 0 0 2px var(--bg0), 0 0 0 4px rgba(159, 220, 235, 0.55);
}

#board { display: block; width: 100%; height: 100%; }

.board-caption {
  margin: 0;
  font-size: 12.5px;
  color: var(--dim);
  text-align: center;
  max-width: 60ch;
  min-height: 1.4em;
}

/* cells */

.cell .tile {
  fill: var(--tile);
  stroke: var(--tile-edge);
  stroke-width: 0.02;
  transition: fill .4s ease, stroke .3s;
  cursor: pointer;
}

/* turnable slopes promise interaction; carved ones don't */
.cell:not(.has-given):hover .tile { stroke: rgba(196, 221, 253, 0.35); }

.cell.sel .tile { stroke: var(--moon); stroke-width: 0.045; }

.pool .tile { cursor: default; }

/* carved givens: solid, anchored, inert */
.has-given .tile { fill: #151f36; cursor: default; }
.has-given .arrow-main { fill: rgba(125, 142, 166, 0.62); stroke: none; }
.has-given .arrow-echo { display: none; }

/* an unset slope shows its empty, turnable slot */
.slot-ring {
  fill: none;
  stroke: rgba(143, 160, 188, 0.38);
  stroke-width: 0.035;
  stroke-dasharray: 0.075 0.1;
  pointer-events: none;
  transition: opacity .3s;
}
.cell:not(.empty) .slot-ring { display: none; }

/* tapping a carved slope gets a visible refusal */
.cell.deny { animation: deny-shake 0.34s ease; }
.cell.deny .tile { stroke: rgba(228, 96, 75, 0.55); stroke-width: 0.05; }
@keyframes deny-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-0.055); }
  40% { transform: translateX(0.055); }
  60% { transform: translateX(-0.035); }
  80% { transform: translateX(0.035); }
}

.arrow-path {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
  transition: stroke .3s, opacity .3s;
}
.arrow-echo { opacity: 0.28; }
.has-user .arrow-main { stroke: var(--water); stroke-width: 0.085; }
.has-user .arrow-echo { stroke: var(--water); stroke-width: 0.085; }
.has-given .arrow-main { stroke: var(--given); stroke-width: 0.085; }
.has-given .arrow-echo { stroke: var(--given); stroke-width: 0.085; }
.has-hint .arrow-main { stroke: var(--hint); stroke-width: 0.085; }
.has-hint .arrow-echo { stroke: var(--hint); stroke-width: 0.085; }
.cell.empty .arrow-path { stroke: #46567a; stroke-width: 0.05; opacity: 0.55; }
.cell.empty .arrow-echo { display: none; }

.cell.loop .tile { stroke: var(--loop); stroke-width: 0.05; animation: loopshimmer 1.1s ease-in-out infinite; }
.cell.loop .arrow-path { stroke: var(--loop); }
@keyframes loopshimmer {
  0%, 100% { stroke-opacity: 0.45; }
  50% { stroke-opacity: 1; }
}

.flow-group { pointer-events: none; }

/* pools */

.pool { cursor: default; }

.pool .pool-disc {
  fill: #0d1930;
  stroke: rgba(242, 232, 213, 0.16);
  stroke-width: 0.03;
}

.pool .ring-track {
  fill: none;
  stroke: rgba(242, 232, 213, 0.12);
  stroke-width: 0.05;
}

.pool .ring-prog {
  fill: none;
  stroke-width: 0.075;
  stroke-linecap: round;
  transition: stroke-dashoffset .45s ease, stroke .3s;
  transform: rotate(-90deg);
  transform-origin: center;
  transform-box: fill-box;
}

.pool .cap-num {
  font-family: var(--serif);
  font-weight: 520;
  fill: var(--moon);
  text-anchor: middle;
  dominant-baseline: central;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.pool.over .cap-num { fill: var(--bad); }
.pool.over { animation: overpulse 0.9s ease-in-out infinite; }
@keyframes overpulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.66; }
}

.droplet { pointer-events: none; }

.splash-ring {
  fill: none;
  stroke-width: 0.05;
  pointer-events: none;
}

/* generating veil */

/* The [hidden] guard must win over .veil's own display:flex — author
   styles outrank the browser's [hidden] rule, so without this the veil
   would stay on screen forever after generation finished. */
.veil[hidden] { display: none; }

.veil {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  border-radius: 20px;
  background: rgba(8, 14, 26, 0.78);
  backdrop-filter: blur(3px);
  z-index: 3;
}
.veil p {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  color: var(--dim);
  font-size: 15px;
}

.rain-dots { display: flex; gap: 14px; }
.rain-dots i {
  width: 6px;
  height: 12px;
  border-radius: 40% 40% 60% 60%;
  background: var(--water);
  opacity: 0.8;
  animation: drip 0.9s ease-in infinite;
}
.rain-dots i:nth-child(2) { animation-delay: 0.3s; }
.rain-dots i:nth-child(3) { animation-delay: 0.6s; }
@keyframes drip {
  0% { transform: translateY(-16px); opacity: 0; }
  35% { opacity: 0.8; }
  100% { transform: translateY(16px); opacity: 0; }
}

/* ---------- footer ---------- */

.foot { text-align: center; padding: 4px 0 8px; }
.foot p {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  font-weight: 330;
  font-size: 12.5px;
  color: #5f7090;
  letter-spacing: 0.05em;
}

/* ---------- dialogs ---------- */

.dlg {
  border: 1px solid var(--panel-edge);
  border-radius: 20px;
  background: linear-gradient(180deg, #121e36, #0d1729);
  color: var(--ink);
  padding: 0;
  max-width: min(480px, calc(100vw - 32px));
  width: 100%;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.55);
}
.dlg::backdrop { background: rgba(5, 9, 18, 0.66); backdrop-filter: blur(4px); }

.dlg article {
  padding: clamp(20px, 4vw, 30px);
  max-height: min(84dvh, 720px);
  overflow: auto;
}

.dlg h2 {
  margin: 0 0 10px;
  font-family: var(--serif);
  font-weight: 540;
  font-size: 24px;
  color: var(--moon);
  letter-spacing: 0.01em;
}

.dlg p { font-size: 14.5px; line-height: 1.6; color: #c6d3e8; margin: 0 0 12px; }
.dlg p.fine { font-size: 13px; color: var(--dim); }

.rules {
  margin: 6px 0 12px;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.rules li { font-size: 14px; line-height: 1.55; color: #c6d3e8; }
.rules strong { color: var(--moon); font-weight: 600; }

.demo {
  margin: 4px 0 16px;
  padding: 10px;
  background: rgba(8, 14, 26, 0.55);
  border: 1px solid var(--tile-edge);
  border-radius: 14px;
}
.demo svg { display: block; width: 100%; max-width: 300px; margin: 0 auto; }

.dlg-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
  flex-wrap: wrap;
}

/* win dialog */

.dlg-win article { text-align: center; overflow: visible; position: relative; }

.win-ripple {
  position: absolute;
  top: -46px;
  left: 50%;
  width: 92px;
  height: 92px;
  transform: translateX(-50%);
  border-radius: 50%;
  border: 2px solid rgba(159, 220, 235, 0.65);
  animation: winripple 2.4s ease-out infinite;
  pointer-events: none;
}
@keyframes winripple {
  0% { transform: translateX(-50%) scale(0.35); opacity: 0.9; }
  100% { transform: translateX(-50%) scale(1.5); opacity: 0; }
}

.win-sub { color: var(--dim); font-style: italic; font-family: var(--serif); }

.win-stats {
  display: flex;
  justify-content: center;
  gap: clamp(20px, 6vw, 44px);
  margin: 18px 0 4px;
}
.win-stats > div { display: flex; flex-direction: column; gap: 4px; }
.win-value {
  font-family: var(--serif);
  font-size: 26px;
  font-weight: 540;
  color: var(--moon);
  font-variant-numeric: tabular-nums;
}
.win-stats .stat-label { font-size: 11.5px; }

.dlg-win .dlg-actions { justify-content: center; }

.record-badge {
  display: inline-block;
  margin-top: 2px;
  padding: 3px 12px;
  border-radius: 999px;
  border: 1px solid rgba(240, 200, 107, 0.6);
  color: var(--hint);
  font-size: 12.5px;
  letter-spacing: 0.03em;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(22px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(16px);
  background: #182741;
  border: 1px solid rgba(196, 221, 253, 0.18);
  color: var(--ink);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13.5px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s;
  z-index: 50;
  max-width: min(90vw, 480px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- responsive ---------- */

@media (max-width: 899px) {
  .layout {
    grid-template-areas: "hud" "stage" "console";
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 12px;
  }

  .hud { justify-content: space-between; padding: 10px 16px; }

  .console { flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px; }
  .difficulty { flex: 1 1 100%; justify-content: center; }
  .actions { justify-content: center; }
  .btn { padding: 10px 14px; }

  .board-wrap { width: min(100%, calc(100dvh - 374px), 560px); }
}

@media (max-width: 420px) {
  .brand-text h1 { letter-spacing: 0.1em; }
  .board-caption { font-size: 11.5px; }
}

/* ---------- motion & a11y ---------- */

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