:root {
  color-scheme: light;
  --page-background:
    radial-gradient(circle at top, rgba(108, 197, 204, 0.22), transparent 36%),
    linear-gradient(160deg, #f8fbf6 0%, #e7f5f7 48%, #e9f7ef 100%);
  --grid-line: rgba(21, 86, 101, 0.08);
  --grid-opacity: 0.55;
  --panel: rgba(250, 255, 252, 0.94);
  --panel-soft: #e8f5f3;
  --tile: #f5fbf8;
  --tile-active: #dff7f0;
  --ink: #15343d;
  --muted: #42646a;
  --line: #9bc7c4;
  --path: #2f79b8;
  --path-active: #11a37f;
  --endpoint: #147c69;
  --accent: #147c69;
  --secondary: #2f79b8;
  --secondary-strong: #215984;
  --button-ink: #ffffff;
  --focus: #2fbf83;
  --shadow: rgba(31, 78, 89, 0.16);
}

body.dark {
  color-scheme: dark;
  --page-background:
    radial-gradient(circle at top, rgba(125, 228, 255, 0.16), transparent 35%),
    linear-gradient(160deg, #061018 0%, #0d2230 48%, #09141d 100%);
  --grid-line: rgba(255, 255, 255, 0.025);
  --grid-opacity: 0.35;
  --panel: rgba(9, 26, 38, 0.92);
  --panel-soft: rgba(15, 37, 52, 0.92);
  --tile: #0f2635;
  --tile-active: #123d3f;
  --ink: #edf7ff;
  --muted: #9db8c7;
  --line: rgba(131, 220, 255, 0.36);
  --path: #7de4ff;
  --path-active: #86efac;
  --endpoint: #ffd166;
  --accent: #7de4ff;
  --secondary: #7de4ff;
  --secondary-strong: #2b8ea8;
  --button-ink: #061018;
  --focus: #ffd166;
  --shadow: rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background: var(--page-background);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 28px 28px;
  opacity: var(--grid-opacity);
}

button {
  font: inherit;
}

.game-shell {
  display: grid;
  width: 100%;
  min-height: 100vh;
  margin: 0;
  padding: clamp(12px, 2vw, 24px);
  grid-template-columns: clamp(260px, 24vw, 320px) minmax(0, 1fr);
  gap: clamp(18px, 3vw, 34px);
  align-items: start;
}

.side-panel {
  position: sticky;
  top: 24px;
  display: grid;
  gap: 14px;
}

.topbar,
.stats,
.panel-section,
.game-result {
  padding: 16px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 8px 20px var(--shadow);
}

.topbar,
.stats,
.panel-section {
  display: grid;
  gap: 16px;
}

.site-back {
  display: inline-flex;
  margin-bottom: 14px;
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
}

.site-back:hover,
.site-back:focus-visible {
  color: var(--focus);
  outline: none;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.65rem, 3vw, 2.45rem);
  line-height: 1.05;
  letter-spacing: 0;
}

.top-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 48px;
  gap: 10px;
}

.restart-button {
  width: 100%;
  border: 0;
  border-radius: 8px;
  padding: 12px 16px;
  background: var(--secondary);
  color: var(--button-ink);
  cursor: pointer;
  font-weight: 700;
  box-shadow: 0 5px 0 var(--secondary-strong);
  transition: transform 140ms ease, box-shadow 140ms ease;
}

.restart-button:hover {
  transform: translateY(-1px);
}

.restart-button:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 var(--secondary-strong);
}

.icon-button,
.mode-button {
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--panel-soft);
  color: var(--ink);
  cursor: pointer;
  font-weight: 700;
  text-align: left;
}

.icon-button {
  display: grid;
  min-width: 48px;
  padding: 0;
  place-items: center;
  background: var(--secondary);
  color: var(--button-ink);
  font-size: 1.35rem;
  text-align: center;
}

.mode-buttons {
  display: grid;
  gap: 8px;
}

.mode-button.is-active {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--button-ink);
}

.panel-section.is-hidden {
  display: none;
}

.repair-tray {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.repair-piece {
  position: relative;
  display: grid;
  aspect-ratio: 1;
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  place-items: center;
  background: var(--panel-soft);
  cursor: pointer;
}

.repair-piece:disabled {
  cursor: default;
  opacity: 0.42;
}

.repair-piece.is-selected {
  border-color: var(--focus);
  box-shadow: 0 0 0 4px rgba(255, 209, 102, 0.24);
}

.repair-piece__shape {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
}

.repair-piece__count {
  position: absolute;
  right: 4px;
  bottom: 3px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.stats {
  grid-template-columns: 1fr 1fr;
}

.stats div {
  min-width: 74px;
}

.stat-label {
  display: block;
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.stats strong {
  font-size: 1.4rem;
}

.play-area {
  position: relative;
  display: grid;
  justify-items: center;
  padding-top: 4px;
}

.game-result {
  display: grid;
  gap: 12px;
  width: min(280px, 100%);
  border-color: var(--accent);
}

.game-result.is-hidden {
  display: none;
}

.game-result p {
  margin: 0;
  font-weight: 800;
}

.play-area .game-result {
  position: fixed;
  top: 24px;
  right: 24px;
  z-index: 3;
}

.flow-board {
  --board-size: 5;
  display: grid;
  width: min(100%, calc(var(--board-size) * 74px + (var(--board-size) - 1) * 8px));
  grid-template-columns: repeat(var(--board-size), 1fr);
  gap: 8px;
}

.tile {
  position: relative;
  aspect-ratio: 1;
  width: 100%;
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 0;
  background: var(--tile);
  cursor: pointer;
  box-shadow: 0 8px 0 var(--shadow);
}

.tile:focus-visible {
  outline: 4px solid var(--focus);
  outline-offset: 3px;
}

.tile.is-connected {
  background: var(--tile-active);
  border-color: var(--path-active);
}

.tile.is-broken {
  cursor: not-allowed;
  background:
    linear-gradient(135deg, transparent 0 42%, rgba(180, 83, 9, 0.72) 42% 48%, transparent 48% 100%),
    linear-gradient(45deg, transparent 0 47%, rgba(180, 83, 9, 0.58) 47% 53%, transparent 53% 100%),
    var(--tile);
}

.tile.is-broken .tile__line,
.tile.is-broken .tile__core {
  opacity: 0.32;
}

.tile.is-broken:focus-visible {
  outline-color: #f59e0b;
}

.tile__line {
  position: absolute;
  background: var(--path);
  border-radius: 999px;
  opacity: 0;
}

.tile.is-connected .tile__line {
  background: var(--path-active);
}

.tile.has-north .tile__line--north,
.repair-piece.has-north .tile__line--north,
.tile.has-east .tile__line--east,
.repair-piece.has-east .tile__line--east,
.tile.has-south .tile__line--south,
.repair-piece.has-south .tile__line--south,
.tile.has-west .tile__line--west,
.repair-piece.has-west .tile__line--west {
  opacity: 1;
}

.tile__line--north {
  top: 0;
  left: 50%;
  width: 16%;
  height: 50%;
  transform: translateX(-50%);
}

.tile__line--east {
  top: 50%;
  right: 0;
  width: 50%;
  height: 16%;
  transform: translateY(-50%);
}

.tile__line--south {
  bottom: 0;
  left: 50%;
  width: 16%;
  height: 50%;
  transform: translateX(-50%);
}

.tile__line--west {
  top: 50%;
  left: 0;
  width: 50%;
  height: 16%;
  transform: translateY(-50%);
}

.tile__core {
  position: absolute;
  inset: 36%;
  border-radius: 999px;
  background: var(--path);
}

.tile.is-connected .tile__core {
  background: var(--path-active);
}

.tile.is-start::before,
.tile.is-end::before {
  content: "";
  position: absolute;
  top: 50%;
  z-index: 1;
  width: 22%;
  height: 22%;
  border: 3px solid var(--endpoint);
  border-radius: 999px;
  background: var(--panel);
  transform: translateY(-50%);
}

.tile.is-start::before {
  left: -14%;
}

.tile.is-end::before {
  right: -14%;
}

.status {
  min-height: 24px;
  margin: 18px 0 0;
  color: var(--ink);
  font-weight: 700;
  text-align: center;
}

.status.is-win {
  color: var(--accent);
}

.status.is-error {
  color: #b45309;
}

@media (max-width: 860px) {
  .game-shell {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .side-panel {
    position: static;
  }

  .play-area {
    width: 100%;
  }
}

@media (max-width: 560px) {
  .game-shell {
    padding: 12px;
  }

  h1 {
    font-size: 1.8rem;
  }

  .stats {
    gap: 10px;
    padding: 12px;
  }

  .stats strong {
    font-size: 1.16rem;
  }

  .flow-board {
    gap: 6px;
  }

  .play-area .game-result {
    position: static;
    width: 100%;
    margin-bottom: 16px;
  }
}
