/* Pipes: pipes on tiles; water fills what is connected. */

:root {
  --tile: #ffffff;
  --pipe: #b9b3a6;
  --water: #2f8fe0;
  --source: #1f6fc0;
  --end: #f2c230;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --tile: #23272c;
    --pipe: #555b63;
    --water: #4aa8ff;
    --source: #7cc4ff;
  }
}
:root[data-theme='dark'] {
  --tile: #23272c;
  --pipe: #555b63;
  --water: #4aa8ff;
  --source: #7cc4ff;
}
:root[data-theme='hallows'] {
  --tile: #1c1430;
  --pipe: #4a3d66;
  --water: #8fd16a;
  --source: #b6f07a;
  --end: #f08a24;
}
:root[data-theme='hallows'] .hw-candle {
  display: none;
}
:root[data-theme='hallows'] .title-main {
  font-family: var(--font-display);
  letter-spacing: 0;
}

body {
  min-height: var(--app-height);
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}
#announce {
  margin: 0;
  top: 0;
}
.app {
  display: flex;
  flex-direction: column;
  height: calc(var(--app-height) - var(--safe-top) - var(--safe-bottom));
  min-height: 460px;
  max-width: 640px;
  margin: 0 auto;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px 6px;
}
.title-btn {
  appearance: none;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 12px;
  color: var(--text);
  min-width: 0;
}
.title-btn:hover {
  background: var(--accent-soft);
}
.title-btn:focus-visible {
  outline: 3px solid var(--accent);
}
.title-main {
  display: block;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.title-main::after {
  content: ' ▾';
  font-size: 0.7em;
  color: var(--text-muted);
}
.title-sub {
  display: block;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.score {
  display: flex;
  gap: 8px;
}
.chip {
  min-width: 64px;
  text-align: center;
  padding: 6px 12px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.chip small {
  display: block;
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}
.chip b {
  display: block;
  font-size: 1.3rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.chip.bump b {
  animation: bump 0.25s ease;
}
.chip.over b {
  color: var(--danger);
}
@keyframes bump {
  40% {
    transform: scale(1.25);
  }
}
.challenge {
  margin: 2px auto 0;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-weight: 700;
  font-size: 0.85rem;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  align-self: center;
}
.challenge[hidden] {
  display: none;
}
.status {
  margin: 2px 16px 0;
  min-height: 2.6em;
  font-size: 0.9rem;
  color: var(--text-muted);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  text-wrap: balance;
}

/* ---------- Board ---------- */

.stage {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 8px 12px;
  container-type: size;
}
.board {
  --size: min((100cqw - 2px * var(--w) - 4px) / var(--w), (100cqh - 2px * var(--h) - 4px) / var(--h), 64px);
  display: grid;
  grid-template-columns: repeat(var(--w), var(--size));
  grid-template-rows: repeat(var(--h), var(--size));
  gap: 2px;
  padding: 2px;
  border-radius: 12px;
  background: var(--border);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.tile {
  appearance: none;
  border: 0;
  padding: 0;
  background: var(--tile);
  border-radius: 6px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
.tile svg {
  display: block;
  width: 100%;
  height: 100%;
  transition: transform 0.16s ease;
}
.tile path,
.tile circle {
  stroke: var(--pipe);
  fill: none;
  transition: stroke 0.2s ease, fill 0.2s ease;
}
.tile .cap {
  fill: var(--tile);
  stroke-width: 7;
}
.tile.on path,
.tile.on circle {
  stroke: var(--water);
}
.tile.on .cap {
  fill: var(--end);
}
.tile.src .hub {
  fill: var(--source);
  stroke: var(--source);
}
.tile.hinted {
  box-shadow: inset 0 0 0 3px var(--accent);
}
.board.won .tile.on path {
  animation: glow 0.9s ease both;
  animation-delay: calc(var(--d) * 1ms);
}
@keyframes glow {
  40% {
    stroke: #fff;
  }
}
@media (prefers-reduced-motion: reduce) {
  .tile svg {
    transition: none;
  }
  .board.won .tile.on path {
    animation: none;
  }
}

/* ---------- Toolbar ---------- */

.toolbar {
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 8px 8px 12px;
}
.tool {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  min-width: 64px;
  min-height: 52px;
  padding: 6px 8px;
  border-radius: 14px;
  font-size: 0.76rem;
  font-weight: 600;
  cursor: pointer;
}
.tool svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tool:hover:not(:disabled) {
  background: var(--accent-soft);
}
.tool:disabled {
  opacity: 0.38;
  cursor: default;
}
.tool:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 1px;
}

/* ---------- Menus ---------- */

.menu-list {
  display: grid;
  gap: 10px;
}
.menu-card {
  appearance: none;
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  text-align: left;
  padding: 14px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.12s ease;
}
.menu-card:active {
  transform: scale(0.98);
}
.menu-card:focus-visible {
  outline: 3px solid var(--accent);
}
.menu-card .menu-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
}
.menu-card b {
  display: block;
  font-size: 1.05rem;
}
.menu-card small {
  color: var(--text-muted);
}
.menu-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}
.menu-row .btn {
  flex: 1;
  padding-inline: 0.8em;
  white-space: nowrap;
}
.level-picker {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 6px;
}
.level-picker input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 1.1rem;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.help ul {
  padding-left: 1.2em;
}

