:root {
  color-scheme: light;
  --bg: #f7f0df;
  --surface: #fffaf0;
  --surface-strong: #fff4d7;
  --ink: #17212b;
  --muted: #637381;
  --pipe: #263746;
  --water: #12a8f5;
  --accent: #0b74c9;
  --accent-strong: #075a9d;
  --good: #0a8f5a;
  --warn: #b96800;
  --danger: #b42318;
  --line: rgba(23, 33, 43, 0.14);
  --shadow: 0 24px 60px rgba(42, 35, 21, 0.16);
  --board-gap: clamp(4px, 1.5vw, 8px);
  --safe-bottom-extra: 0px;
  font-family:
    "Microsoft YaHei",
    "PingFang SC",
    "Noto Sans SC",
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100svh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 50% -20%, rgba(18, 168, 245, 0.18), transparent 34rem),
    linear-gradient(180deg, #fff7e7 0%, var(--bg) 58%, #efe2c7 100%);
}

button,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

[hidden] {
  display: none !important;
}

.app-shell {
  width: min(100%, 720px);
  min-height: 100svh;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 14px calc(max(18px, env(safe-area-inset-bottom)) + var(--safe-bottom-extra));
}

.screen {
  min-height: calc(100svh - 36px);
}

.screen.is-screen-enter-forward {
  animation: screenEnterForward 260ms cubic-bezier(0.2, 0.9, 0.2, 1) both;
}

.screen.is-screen-enter-back {
  animation: screenEnterBack 260ms cubic-bezier(0.2, 0.9, 0.2, 1) both;
}

.start-screen {
  display: grid;
  align-content: center;
  gap: 22px;
  position: relative;
  overflow: hidden;
  padding: 28px 18px 42px;
}

.hero-art {
  position: relative;
  width: min(100%, 440px);
  height: 210px;
  margin: 0 auto 4px;
  border-radius: 34px;
  background:
    linear-gradient(90deg, transparent 0 16%, rgba(255, 255, 255, 0.68) 16% 84%, transparent 84%),
    radial-gradient(circle at 76% 32%, rgba(18, 168, 245, 0.35), transparent 8rem),
    var(--surface-strong);
  box-shadow: var(--shadow);
}

.hero-pipe,
.hero-pipe::before,
.hero-pipe::after {
  position: absolute;
  display: block;
  content: "";
  border-radius: 999px;
  background: var(--pipe);
}

.hero-pipe-a {
  width: 270px;
  height: 30px;
  left: 42px;
  top: 86px;
}

.hero-pipe-a::before {
  width: 30px;
  height: 104px;
  left: 96px;
  bottom: 0;
}

.hero-pipe-a::after {
  width: 132px;
  height: 30px;
  left: 126px;
  bottom: 74px;
  background: var(--water);
  box-shadow: 0 0 28px rgba(18, 168, 245, 0.48);
}

.hero-pipe-b {
  width: 30px;
  height: 132px;
  right: 70px;
  top: 40px;
}

.hero-pipe-b::before {
  width: 88px;
  height: 30px;
  right: 0;
  bottom: 0;
}

.hero-drop {
  position: absolute;
  right: 49px;
  bottom: 32px;
  width: 42px;
  height: 42px;
  border-radius: 50% 50% 50% 12px;
  background: var(--water);
  transform: rotate(-45deg);
  box-shadow: 0 10px 28px rgba(18, 168, 245, 0.35);
  animation: dropPulse 1.8s ease-in-out infinite;
}

.eyebrow {
  margin: 0;
  color: var(--accent-strong);
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.start-screen .eyebrow,
.start-screen h1,
.hero-copy {
  text-align: center;
}

.start-screen h1 {
  margin: 0;
  font-size: clamp(54px, 18vw, 96px);
  line-height: 0.92;
  letter-spacing: -0.08em;
}

.hero-copy {
  width: min(100%, 420px);
  margin: 0 auto;
  color: var(--muted);
  font-size: 20px;
  line-height: 1.55;
}

.mode-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  width: min(100%, 360px);
  margin: 0 auto;
}

.mode-button {
  position: relative;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto auto;
  align-content: center;
  align-items: baseline;
  row-gap: 6px;
  min-height: 108px;
  padding: 16px 128px 16px 20px;
  border-radius: 24px;
  color: var(--ink);
  background:
    radial-gradient(circle at 72% 22%, rgba(18, 168, 245, 0.22), transparent 5rem),
    rgba(255, 250, 240, 0.92);
  border: 2px solid rgba(23, 33, 43, 0.12);
  box-shadow: var(--shadow);
  text-align: left;
}

.mode-button span {
  grid-column: 1;
  color: var(--accent-strong);
  font-size: 20px;
  font-weight: 900;
}

.mode-button strong {
  position: absolute;
  top: 50%;
  right: 18px;
  transform: translateY(-50%);
  font-size: clamp(58px, 18vw, 76px);
  line-height: 0.9;
  letter-spacing: -0.08em;
}

.mode-button small {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 17px;
  font-weight: 800;
}

.mode-button.is-hard {
  background:
    radial-gradient(circle at 72% 22%, rgba(11, 116, 201, 0.28), transparent 5rem),
    #f3ead6;
}

.mode-button:active {
  transform: scale(0.98);
}

.level-screen {
  display: grid;
  grid-template-rows: auto auto auto auto;
  align-content: center;
  gap: 12px;
  overflow: hidden;
}

.level-title-block {
  text-align: right;
}

.level-nav-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.game-title-block {
  text-align: right;
}

.level-title-block h2 {
  margin: 2px 0 0;
  font-size: clamp(34px, 11vw, 52px);
  line-height: 1;
  letter-spacing: -0.06em;
}

.level-title-block h2.is-range-title {
  display: grid;
  justify-items: end;
  gap: 2px;
  font-size: clamp(30px, 9.6vw, 46px);
  line-height: 0.88;
}

.level-title-block h2.is-range-title span {
  display: block;
  white-space: nowrap;
}

.level-grid.is-slide-next {
  animation: levelSlideNext 180ms cubic-bezier(0.22, 0.72, 0.22, 1) both;
}

.level-grid.is-slide-prev {
  animation: levelSlidePrev 180ms cubic-bezier(0.22, 0.72, 0.22, 1) both;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
  align-content: start;
  width: min(100%, 430px);
  margin: 2px auto 0;
  gap: 8px;
  overflow: hidden;
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
  width: min(100%, 430px);
  margin: 2px auto 0;
  gap: 8px;
  overflow: hidden;
}

.level-button {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-height: 48px;
  border-radius: 17px;
  color: var(--ink);
  background: rgba(255, 250, 240, 0.9);
  border: 2px solid rgba(23, 33, 43, 0.12);
  box-shadow: 0 8px 14px rgba(42, 35, 21, 0.08);
  font-size: clamp(27px, 7.4vw, 40px);
  font-weight: 950;
  line-height: 1;
}

.level-button.is-cleared {
  color: #fff;
  background: var(--good);
  border-color: rgba(10, 143, 90, 0.36);
}

.level-button.is-next {
  color: #fff;
  background: var(--accent);
  border-color: rgba(11, 116, 201, 0.42);
  box-shadow: 0 12px 24px rgba(11, 116, 201, 0.24);
}

.level-button.is-locked {
  color: rgba(23, 33, 43, 0.42);
  background: rgba(255, 250, 240, 0.48);
  border-style: dashed;
  box-shadow: none;
}

.level-button:disabled {
  opacity: 1;
}

.chapter-card {
  display: grid;
  align-content: center;
  gap: 4px;
  min-height: 70px;
  padding: 8px 6px;
  border-radius: 18px;
  color: var(--ink);
  background: rgba(255, 250, 240, 0.9);
  border: 2px solid rgba(23, 33, 43, 0.12);
  box-shadow: 0 8px 14px rgba(42, 35, 21, 0.08);
  text-align: center;
}

.chapter-card strong {
  color: var(--accent-strong);
  font-size: clamp(20px, 5.8vw, 30px);
  line-height: 1;
}

.chapter-card span {
  color: var(--muted);
  font-size: clamp(12px, 3.4vw, 15px);
  font-weight: 850;
}

.chapter-card.is-current {
  color: #fff;
  background: var(--accent);
  border-color: rgba(11, 116, 201, 0.42);
  box-shadow: 0 12px 24px rgba(11, 116, 201, 0.24);
}

.chapter-card.is-current strong,
.chapter-card.is-current span {
  color: #fff;
}

.chapter-card.is-locked {
  background: rgba(255, 250, 240, 0.48);
  border-style: dashed;
  box-shadow: none;
}

.level-pager {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: center;
  width: min(100%, 430px);
  margin: 0 auto;
  gap: 10px;
}

.page-text {
  min-width: 64px;
  color: var(--muted);
  font-size: 18px;
  font-weight: 900;
  text-align: center;
}

.field-label {
  color: var(--muted);
  font-size: 16px;
  font-weight: 800;
}

.select-control,
.primary-button,
.secondary-button,
.ghost-button {
  min-height: 52px;
  border-radius: 18px;
  font-size: 18px;
  font-weight: 850;
}

.select-control {
  width: 100%;
  padding: 0 18px;
  color: var(--ink);
  border: 2px solid var(--line);
  background: rgba(255, 255, 255, 0.78);
}

.primary-button {
  color: #fff;
  background: var(--accent);
  box-shadow: 0 14px 28px rgba(11, 116, 201, 0.24);
}

.primary-button:active,
.secondary-button:active,
.ghost-button:active {
  transform: translateY(1px) scale(0.99);
}

.secondary-button {
  color: var(--ink);
  background: #fff7e6;
  border: 2px solid rgba(23, 33, 43, 0.12);
}

.ghost-button {
  min-width: 86px;
  padding: 0 18px;
  color: var(--accent-strong);
  background: rgba(255, 255, 255, 0.72);
  border: 2px solid rgba(11, 116, 201, 0.18);
}

.game-screen {
  display: grid;
  align-content: center;
  gap: 10px;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 2px 4px 0;
}

.game-header h2 {
  margin: 2px 0 0;
  font-size: clamp(30px, 8.8vw, 34px);
  line-height: 1;
  letter-spacing: -0.05em;
}

.status-panel {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stat-block {
  min-height: 64px;
  padding: 8px 6px;
  border-radius: 18px;
  background: rgba(255, 250, 240, 0.82);
  border: 1px solid rgba(23, 33, 43, 0.08);
  text-align: center;
}

.stat-block span {
  display: block;
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.stat-block strong {
  display: block;
  margin-top: 2px;
  font-size: clamp(21px, 7vw, 30px);
  line-height: 1.1;
}

.board-section {
  min-width: 0;
}

.board-frame {
  overflow: visible;
  padding: 4px 0 6px;
}

.board {
  --size: 5;
  --board-max: 430px;
  display: grid;
  grid-template-columns: repeat(var(--size), minmax(0, 1fr));
  grid-template-rows: repeat(var(--size), minmax(0, 1fr));
  gap: var(--board-gap);
  width: min(100%, var(--board-max), calc(100svh - 360px - var(--safe-bottom-extra)));
  aspect-ratio: 1;
  margin: 0 auto;
  padding: 7px;
  border-radius: 26px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.76), rgba(255, 250, 240, 0.6)),
    repeating-linear-gradient(45deg, transparent 0 16px, rgba(23, 33, 43, 0.035) 16px 17px);
  box-shadow: var(--shadow);
}

.board[data-size="8"] {
  --board-max: 620px;
}

.pipe-cell {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  overflow: visible;
  border-radius: clamp(9px, 3vw, 18px);
  color: var(--ink);
  background:
    radial-gradient(circle at 50% 44%, rgba(255, 255, 255, 0.92), rgba(255, 248, 229, 0.86)),
    #fff4d6;
  border: 2px solid rgba(23, 33, 43, 0.12);
  box-shadow:
    inset 0 -6px 14px rgba(54, 39, 12, 0.08),
    0 8px 14px rgba(42, 35, 21, 0.1);
  transition:
    transform 160ms ease,
    border-color 180ms ease,
    background 180ms ease,
    box-shadow 180ms ease;
}

.pipe-cell:focus-visible {
  outline: 4px solid rgba(18, 168, 245, 0.58);
  outline-offset: 3px;
}

.pipe-cell.is-popping {
  transform: scale(0.94);
}

.pipe-cell.is-hint {
  border-color: var(--warn);
  box-shadow:
    inset 0 -6px 14px rgba(54, 39, 12, 0.08),
    0 0 0 5px rgba(246, 174, 45, 0.32);
}

.pipe-cell.is-flowed {
  background:
    radial-gradient(circle at 50% 44%, rgba(231, 249, 255, 0.98), rgba(201, 239, 255, 0.86)),
    #e9f8ff;
  border-color: rgba(18, 168, 245, 0.7);
  box-shadow:
    inset 0 -6px 14px rgba(18, 168, 245, 0.14),
    0 0 24px rgba(18, 168, 245, 0.22);
}

.pipe-svg {
  width: 94%;
  height: 94%;
  transform: rotate(var(--rotation, 0deg));
  transition: transform 230ms cubic-bezier(0.22, 1, 0.36, 1);
}

.pipe-stroke {
  fill: none;
  stroke: var(--pipe);
  stroke-width: 25;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: stroke 180ms ease, filter 180ms ease;
}

.pipe-cell.is-flowed .pipe-stroke {
  stroke: var(--water);
  filter: drop-shadow(0 0 9px rgba(18, 168, 245, 0.75));
}

.cell-tag {
  position: absolute;
  z-index: 2;
  display: grid;
  place-items: center;
  min-width: clamp(20px, 5vw, 28px);
  height: clamp(20px, 5vw, 28px);
  padding: 0 6px;
  border-radius: 999px;
  color: #fff;
  font-size: clamp(11px, 3.2vw, 14px);
  font-weight: 900;
  box-shadow: 0 6px 14px rgba(23, 33, 43, 0.18);
}

.cell-tag.start {
  background: var(--good);
}

.cell-tag.end {
  background: var(--danger);
}

.cell-tag.up {
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
}

.cell-tag.right {
  top: 50%;
  right: -8px;
  transform: translateY(-50%);
}

.cell-tag.down {
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
}

.cell-tag.left {
  top: 50%;
  left: -8px;
  transform: translateY(-50%);
}

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: min(100%, 464px);
  margin: 0 auto;
  gap: 8px;
}

.message-text {
  min-height: 24px;
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  font-weight: 700;
  line-height: 1.45;
  text-align: center;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(23, 33, 43, 0.42);
  backdrop-filter: blur(8px);
}

.modal[hidden] {
  display: none;
}

.modal-card {
  position: relative;
  width: min(100%, 430px);
  padding: 28px;
  border-radius: 30px;
  background: var(--surface);
  box-shadow: 0 34px 88px rgba(23, 33, 43, 0.28);
}

.modal-card h2 {
  margin: 0 38px 14px 0;
  font-size: 34px;
  line-height: 1.05;
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  color: var(--muted);
  background: rgba(23, 33, 43, 0.06);
  font-size: 30px;
  line-height: 1;
}

.modal-body {
  color: var(--muted);
  font-size: 18px;
  line-height: 1.55;
}

.modal-body p {
  margin: 0 0 12px;
}

.modal-body .menu-grid {
  display: grid;
  gap: 14px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 54px;
}

.modal-actions {
  display: grid;
  gap: 10px;
  margin-top: 20px;
}

.danger-button {
  min-height: 52px;
  border-radius: 18px;
  color: #fff;
  background: var(--danger);
  font-size: 18px;
  font-weight: 850;
}

@keyframes dropPulse {
  0%,
  100% {
    transform: rotate(-45deg) scale(1);
  }
  50% {
    transform: rotate(-45deg) scale(1.12);
  }
}

@keyframes screenEnterForward {
  from {
    opacity: 0;
    transform: translateX(22px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

@keyframes screenEnterBack {
  from {
    opacity: 0;
    transform: translateX(-22px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

@keyframes levelSlideNext {
  from {
    opacity: 0.72;
    transform: translateX(14px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes levelSlidePrev {
  from {
    opacity: 0.72;
    transform: translateX(-14px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@media (max-width: 560px) {
  :root {
    --board-gap: 5px;
  }

  .app-shell {
    padding: max(8px, env(safe-area-inset-top)) 10px calc(max(8px, env(safe-area-inset-bottom)) + var(--safe-bottom-extra));
  }

  .screen {
    min-height: calc(100svh - 16px - var(--safe-bottom-extra));
  }

  .start-screen {
    gap: 12px;
    padding: 10px 6px 16px;
  }

  .hero-art {
    width: min(100%, 320px);
    height: 112px;
    border-radius: 24px;
  }

  .hero-pipe-a {
    left: 24px;
    top: 48px;
    width: 186px;
    height: 22px;
  }

  .hero-pipe-a::before {
    width: 22px;
    height: 58px;
    left: 70px;
  }

  .hero-pipe-a::after {
    width: 86px;
    height: 22px;
    left: 92px;
    bottom: 36px;
  }

  .hero-pipe-b {
    right: 40px;
    top: 24px;
    width: 22px;
    height: 70px;
  }

  .hero-pipe-b::before {
    width: 54px;
    height: 22px;
  }

  .hero-drop {
    right: 26px;
    bottom: 15px;
    width: 28px;
    height: 28px;
  }

  .start-screen h1 {
    font-size: clamp(44px, 15vw, 62px);
  }

  .hero-copy {
    font-size: 17px;
    line-height: 1.35;
  }

  .mode-grid {
    width: min(100%, 360px);
  }

  .status-panel {
    width: min(100%, 464px);
    margin: 0 auto;
    gap: 6px;
  }

  .stat-block {
    min-height: 58px;
    padding: 7px 4px;
    border-radius: 16px;
  }

  .stat-block span {
    font-size: 13px;
  }

  .board {
    border-radius: 22px;
    padding: 6px;
  }

  .pipe-cell {
    border-radius: 15px;
  }
}

@media (max-width: 390px) {
  :root {
    --board-gap: 4px;
  }

  .action-row {
    grid-template-columns: 1fr;
  }

  .mode-grid {
    gap: 8px;
  }

  .mode-button {
    min-height: 108px;
    padding: 14px 118px 14px 18px;
    border-radius: 22px;
  }

  .mode-button span {
    font-size: 18px;
  }

  .mode-button small {
    font-size: 15px;
  }

  .level-screen {
    gap: 8px;
  }

  .level-title-block h2 {
    font-size: 32px;
  }

  .level-title-block h2.is-range-title {
    font-size: clamp(28px, 9vw, 34px);
    line-height: 0.92;
  }

  .level-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(5, minmax(0, 1fr));
    width: min(100%, 350px);
    gap: 6px;
  }

  .chapter-grid {
    width: min(100%, 350px);
    gap: 6px;
  }

  .level-button {
    min-height: 42px;
    border-radius: 13px;
    font-size: clamp(24px, 7.5vw, 32px);
  }

  .chapter-card {
    min-height: 58px;
    border-radius: 13px;
  }
}

@media (min-width: 680px) {
  .level-grid.is-hard {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

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