:root {
  color-scheme: light;
  --bg: #f6f8fb;
  --surface: #ffffff;
  --ink: #151c2b;
  --muted: #66768d;
  --line: #dbe4ef;
  --cell: #eef3f8;
  --cell-border: #d6e0ec;
  --focus: #1ba9a5;
  --shadow: 0 20px 58px rgba(20, 28, 43, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background:
    linear-gradient(135deg, rgba(27, 169, 165, 0.12), transparent 32%),
    linear-gradient(315deg, rgba(240, 95, 115, 0.11), transparent 34%),
    var(--bg);
  color: var(--ink);
  font-family:
    Arial, "Helvetica Neue", Helvetica, system-ui, -apple-system, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
select {
  font: inherit;
}

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 160ms ease,
    border-color 160ms ease,
    background 160ms ease;
}

button:hover {
  transform: translateY(-1px);
  border-color: rgba(21, 28, 43, 0.28);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
  transform: none;
}

.primary {
  background: #151c2b;
  color: #fff;
  border-color: #151c2b;
}

.app-shell {
  width: min(1120px, calc(100% - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px 0 22px;
  display: grid;
  gap: 16px;
  grid-template-rows: auto 1fr auto;
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(190px, 1fr) minmax(360px, auto);
  gap: 14px;
  align-items: center;
}

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

.home-link {
  width: 66px;
  height: 44px;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
  font-size: 0.82rem;
  font-weight: 700;
}

.title-block {
  min-width: 0;
}

.kicker {
  margin: 0 0 4px;
  color: var(--focus);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.9rem, 3.8vw, 2.9rem);
  line-height: 1.05;
  font-weight: 800;
}

.level-picker {
  height: 50px;
  display: grid;
  grid-template-columns: auto auto;
  gap: 10px;
  align-items: center;
  padding: 0 10px 0 14px;
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.compact-picker select {
  min-width: 104px;
}

.daily-button {
  min-width: 82px;
  height: 50px;
  padding: 0 16px;
  background: #151c2b;
  color: #fff;
  border-color: #151c2b;
}

.daily-button.is-active {
  background: var(--focus);
  border-color: var(--focus);
}

.level-picker span {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}

select {
  height: 34px;
  min-width: 118px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 700;
  padding: 0 10px;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(124px, 0.38fr) minmax(340px, 1fr) minmax(124px, 0.38fr);
  gap: 20px;
  align-items: center;
  min-width: 0;
}

.panel {
  display: grid;
  gap: 10px;
}

.stats-panel {
  align-self: stretch;
  align-content: center;
}

.stat {
  min-height: 70px;
  padding: 12px;
  display: grid;
  gap: 8px;
  align-content: center;
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.stat span {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
}

.stat strong {
  font-size: clamp(1.28rem, 2.3vw, 1.78rem);
  line-height: 1;
}

.board-section {
  display: grid;
  justify-items: center;
  gap: 12px;
  min-width: 0;
  width: 100%;
}

.board-frame {
  width: min(74vh, 100%);
  max-width: 640px;
  min-width: 300px;
  aspect-ratio: 1;
  padding: clamp(10px, 2vw, 16px);
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.flow-board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(var(--size), minmax(0, 1fr));
  grid-template-rows: repeat(var(--size), minmax(0, 1fr));
  gap: clamp(4px, 0.8vw, 8px);
  touch-action: none;
  user-select: none;
}

.cell {
  position: relative;
  min-width: 0;
  border: 1px solid var(--cell-border);
  border-radius: 8px;
  background: var(--cell);
  overflow: hidden;
  padding: 0;
}

.cell:focus-visible {
  outline: 3px solid rgba(27, 169, 165, 0.32);
  outline-offset: 2px;
}

.pipe {
  position: absolute;
  inset: 0;
  color: var(--pipe, #151c2b);
  pointer-events: none;
}

.arm {
  position: absolute;
  display: none;
  background: currentColor;
}

.arm.show {
  display: block;
}

.arm.north,
.arm.south {
  left: 50%;
  width: 24%;
  height: 50%;
  transform: translateX(-50%);
}

.arm.north {
  top: 0;
}

.arm.south {
  bottom: 0;
}

.arm.east,
.arm.west {
  top: 50%;
  width: 50%;
  height: 24%;
  transform: translateY(-50%);
}

.arm.east {
  right: 0;
}

.arm.west {
  left: 0;
}

.core {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 40%;
  height: 40%;
  border-radius: 50%;
  background: currentColor;
  transform: translate(-50%, -50%);
  box-shadow: 0 6px 14px rgba(20, 28, 43, 0.14);
}

.cell.is-endpoint .core {
  width: 58%;
  height: 58%;
  border: max(3px, 0.35vw) solid #fff;
}

.cell.is-locked {
  background: #fff;
}

.status {
  min-height: 30px;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 700;
  text-align: center;
}

.level-meta {
  min-height: 24px;
  color: var(--focus);
  font-size: 0.78rem;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
}

.status.complete {
  color: #0f8f7d;
}

.control-panel {
  align-self: center;
}

.rules {
  display: flex;
  justify-content: center;
  color: var(--muted);
  text-align: center;
}

.rules p {
  max-width: 680px;
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.6;
}

@media (max-width: 900px) {
  .app-shell {
    width: min(100% - 20px, 720px);
    padding-top: 14px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
  }

  .home-link {
    grid-column: 1 / 2;
    grid-row: 1;
  }

  .title-block {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .top-actions {
    grid-column: 2;
    grid-row: 1;
    justify-content: end;
  }

  .game-layout {
    grid-template-columns: 1fr;
    gap: 14px;
    width: 100%;
  }

  .stats-panel {
    grid-template-columns: repeat(4, 1fr);
    order: 2;
  }

  .stat {
    min-height: 64px;
    padding: 10px;
  }

  .stat strong {
    font-size: clamp(1.05rem, 5vw, 1.55rem);
  }

  .board-section {
    order: 1;
  }

  .board-frame {
    width: 100%;
    max-width: 560px;
    min-width: 0;
  }

  .control-panel {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    order: 3;
  }
}

@media (max-width: 520px) {
  .app-shell {
    width: calc(100% - 20px);
    max-width: 370px;
  }

  .topbar {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .home-link,
  .title-block,
  .top-actions {
    grid-column: 1;
    grid-row: auto;
  }

  .top-actions {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr;
  }

  .daily-button {
    width: 100%;
    height: 48px;
  }

  .level-picker {
    height: 48px;
    width: 100%;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 8px;
    padding: 6px 8px;
  }

  select {
    width: 100%;
    min-width: 0;
    height: 28px;
    font-size: 0.82rem;
  }

  .stats-panel {
    grid-template-columns: repeat(2, 1fr);
  }

  .control-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  button {
    min-height: 46px;
  }
}
