:root {
  --bg: #FAF7F0;
  --fg: #2A2620;
  --muted: #7A736A;
  --card: #FFFFFF;
  --border: #E5DFD2;
  --src-fill: #BA7517;
  --src-stroke: #854F0B;
  --snk-fill: #1D9E75;
  --snk-stroke: #0F6E56;
  --wire-off: #BFB8AC;
  --wire-on: #2C7CC2;
  --wire-maybe: #2C7CC2;
  --wire-maybeoff: #C9462E;
  --junction-fill: #FFFFFF;
  --junction-stroke: #BFB8AC;
  --accent: #2C7CC2;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1A1814;
    --fg: #ECE7DD;
    --muted: #8A847A;
    --card: #25221C;
    --border: #3A352E;
    --junction-fill: #25221C;
    --junction-stroke: #4A453D;
    --wire-off: #4A453D;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: var(--muted);
}
.streak { font-variant-numeric: tabular-nums; }

.iconbtn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  padding: 0;
}
.iconbtn:hover { background: var(--border); }

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 16px;
}

.board-wrap {
  width: 100%;
  max-width: 560px;
  display: flex;
  justify-content: center;
}
#board {
  width: 100%;
  height: auto;
  max-width: 560px;
  touch-action: manipulation;
  user-select: none;
}

.status {
  margin: 0;
  font-size: 14px;
  color: var(--muted);
  text-align: center;
  min-height: 1.2em;
}
.status.win { color: var(--snk-fill); font-weight: 600; }

.controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}
.btn {
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}
.btn:hover { border-color: var(--accent); }
.btn.primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { color: var(--muted); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
  gap: 8px;
}
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
}
.toggle input { accent-color: var(--accent); }

.hidden { display: none !important; }

/* SVG wire transitions */
.wire-line {
  transition: stroke 200ms ease;
  stroke-linecap: round;
}
@media (prefers-reduced-motion: reduce) {
  .wire-line { transition: none; }
}

.wire-line.off { stroke: var(--wire-off); stroke-dasharray: none; }
.wire-line.on { stroke: var(--wire-on); stroke-dasharray: none; }
.wire-line.maybe { stroke: var(--wire-maybe); stroke-dasharray: 6 5; }
.wire-line.maybeoff { stroke: var(--wire-maybeoff); stroke-dasharray: 6 5; }

.wire-hit {
  stroke: transparent;
  fill: none;
  cursor: pointer;
}
.wire-hit:focus-visible + .wire-line,
.wire-hit:focus-visible {
  outline: none;
}
.wire-hit:focus-visible ~ .wire-line {
  filter: drop-shadow(0 0 4px var(--accent));
}

.source-rect {
  fill: var(--src-fill);
  stroke: var(--src-stroke);
  stroke-width: 2;
}
.source-text, .sink-text {
  fill: white;
  font-weight: 700;
  font-family: inherit;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}
.sink-circle {
  fill: var(--snk-fill);
  stroke: var(--snk-stroke);
  stroke-width: 2;
}
.sink-circle.ok { stroke: #FFD24A; stroke-width: 3.5; }

.junction {
  fill: var(--junction-fill);
  stroke: var(--junction-stroke);
  stroke-width: 1.5;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}
.modal-card {
  background: var(--card);
  color: var(--fg);
  border-radius: 14px;
  padding: 22px 22px 18px;
  max-width: 380px;
  width: 100%;
  position: relative;
  border: 1px solid var(--border);
}
.modal-card h2 { margin-top: 0; }
.modal-card .close {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 22px;
}
.modal-card p { line-height: 1.5; }
.muted { color: var(--muted); font-size: 13px; }
.src-chip {
  background: var(--src-fill);
  color: white;
  padding: 1px 7px;
  border-radius: 4px;
  font-weight: 600;
}
.snk-chip {
  background: var(--snk-fill);
  color: white;
  padding: 1px 7px;
  border-radius: 4px;
  font-weight: 600;
}
.legend { list-style: none; padding: 0; margin: 8px 0; }
.legend li { display: flex; align-items: center; gap: 10px; padding: 4px 0; }
.wire-key {
  display: inline-block;
  width: 38px;
  height: 4px;
  border-radius: 2px;
  vertical-align: middle;
}
.wire-key.off { background: var(--wire-off); }
.wire-key.on { background: var(--wire-on); }
.wire-key.maybe {
  background: repeating-linear-gradient(to right, var(--wire-maybe) 0 6px, transparent 6px 11px);
  height: 4px;
}
.wire-key.maybeoff {
  background: repeating-linear-gradient(to right, var(--wire-maybeoff) 0 6px, transparent 6px 11px);
  height: 4px;
}

/* Level select */
.level-card {
  max-width: 460px;
}
.diff-tabs {
  display: flex;
  gap: 4px;
  margin: 14px 0 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
  flex-wrap: wrap;
}
.diff-tab {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 6px 9px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.diff-tab:hover { color: var(--fg); }
.diff-tab.active {
  background: var(--accent);
  color: white;
}
.level-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  max-height: 320px;
  overflow-y: auto;
  padding: 4px;
}
.level-tile {
  position: relative;
  aspect-ratio: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-weight: 600;
  font-size: 15px;
  color: var(--fg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.level-tile:hover {
  border-color: var(--accent);
}
.level-tile.solved {
  background: var(--snk-fill);
  color: white;
  border-color: var(--snk-stroke);
}
.level-tile.viewed:not(.solved) {
  background: repeating-linear-gradient(45deg, var(--bg) 0 6px, var(--border) 6px 7px);
}
.level-tile.current {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.level-tile .check {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 10px;
}
@media (max-width: 480px) {
  .level-grid { grid-template-columns: repeat(5, 1fr); }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 480px) {
  .title { font-size: 19px; }
  .meta { font-size: 13px; gap: 10px; }
  .btn { padding: 9px 14px; font-size: 14px; }
}
