:root {
  color-scheme: light;
  --paper: #f4efe5;
  --paper-deep: #e9e0d2;
  --ink: #18332f;
  --muted: #7d8178;
  --board: #d9cdbb;
  --board-line: rgba(65, 55, 43, 0.16);
  --shadow: 0 18px 45px rgba(68, 55, 38, 0.14);
  --red: #df624e;
  --yellow: #e5b84a;
  --blue: #4d80a8;
  --ivory: #eee9dc;
  font-family: Inter, ui-rounded, "SF Pro Rounded", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

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

body {
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 6%, rgba(255, 255, 255, 0.78), transparent 24rem),
    linear-gradient(150deg, var(--paper) 0%, #f7f2e9 48%, var(--paper-deep) 100%);
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
.piece:focus-visible {
  outline: 3px solid rgba(24, 51, 47, 0.35);
  outline-offset: 3px;
}

.app-shell {
  width: min(100%, 610px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 18px calc(98px + env(safe-area-inset-bottom));
}

.topbar,
.level-summary,
.dock-heading,
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.eyebrow,
.level-kicker {
  margin: 0 0 3px;
  color: #a4674e;
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.6rem, 7vw, 2.2rem);
  line-height: 1;
  letter-spacing: -0.055em;
}

h1 span {
  color: #b36d50;
}

.level-button {
  display: flex;
  align-items: baseline;
  gap: 3px;
  min-width: 70px;
  padding: 10px 14px;
  border: 1px solid rgba(24, 51, 47, 0.12);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.48);
  box-shadow: 0 5px 16px rgba(61, 48, 30, 0.06);
  cursor: pointer;
}

.level-button span {
  font-size: 1.05rem;
  font-weight: 850;
}

.level-button small {
  color: var(--muted);
  font-weight: 700;
}

.level-summary {
  margin-top: 24px;
}

.level-summary h2 {
  margin: 0;
  font-size: clamp(1.15rem, 5vw, 1.45rem);
  letter-spacing: -0.03em;
}

.stats {
  display: flex;
  gap: 7px;
}

.stats span {
  min-width: 60px;
  padding: 8px 10px;
  border-radius: 11px;
  color: #5f655e;
  background: rgba(255, 255, 255, 0.43);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.level-track {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 6px;
  margin: 17px 0 16px;
}

.level-dot {
  height: 4px;
  padding: 0;
  border: 0;
  border-radius: 99px;
  background: rgba(24, 51, 47, 0.12);
  cursor: pointer;
  transition: transform 180ms ease, background 180ms ease;
}

.level-dot.completed {
  background: rgba(24, 51, 47, 0.34);
}

.level-dot.active {
  background: var(--ink);
  transform: scaleY(1.8);
}

.game-stage {
  display: grid;
  place-items: center;
}

.board-wrap {
  width: min(100%, 520px);
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border: 9px solid #342f2b;
  border-radius: clamp(17px, 5vw, 25px);
  background-color: var(--board);
  background-image:
    linear-gradient(to right, var(--board-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--board-line) 1px, transparent 1px);
  background-size: 12.5% 12.5%;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.22),
    inset 0 0 30px rgba(70, 48, 28, 0.12),
    var(--shadow);
  touch-action: none;
  user-select: none;
}

.board-sheen {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.2), transparent 38%);
}

.board-caption {
  margin: 10px 4px 0;
  color: var(--muted);
  font-size: 0.72rem;
  text-align: center;
}

.obstacle,
.board-piece,
.hint-outline {
  position: absolute;
}

.obstacle {
  z-index: 1;
  border: 2px solid #161a18;
  border-radius: 8px;
  background:
    radial-gradient(circle at 25% 20%, rgba(255,255,255,0.14) 0 1px, transparent 1.5px),
    linear-gradient(145deg, #29312f, #111615);
  background-size: 10px 10px, auto;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.08), 0 3px 5px rgba(20, 20, 18, 0.25);
}

.piece {
  position: relative;
  display: grid;
  place-items: center;
  border: 1px solid rgba(36, 34, 29, 0.14);
  border-radius: 8px;
  color: rgba(30, 37, 34, 0.7);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.54),
    inset 0 -2px 4px rgba(60, 45, 28, 0.12),
    0 4px 8px rgba(56, 43, 28, 0.2);
  cursor: grab;
  touch-action: none;
  user-select: none;
  transition: filter 150ms ease, box-shadow 150ms ease, opacity 150ms ease, transform 150ms ease;
}

.piece::after {
  content: "";
  position: absolute;
  inset: 4px;
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 5px;
  pointer-events: none;
}

.piece-red { background: linear-gradient(145deg, #ef7966, var(--red)); }
.piece-yellow { background: linear-gradient(145deg, #f2cb62, var(--yellow)); }
.piece-blue { background: linear-gradient(145deg, #6f9abb, var(--blue)); }
.piece-ivory { background: linear-gradient(145deg, #faf6ed, var(--ivory)); }

.board-piece {
  position: absolute;
  z-index: 3;
  border-radius: clamp(7px, 2vw, 12px);
}

.piece.selected {
  z-index: 5;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.6),
    0 0 0 3px #18332f,
    0 8px 14px rgba(38, 34, 25, 0.26);
  filter: saturate(1.06) brightness(1.03);
}

.piece.is-dragging {
  opacity: 0.3;
}

.drag-ghost {
  position: fixed;
  z-index: 100;
  margin: 0;
  pointer-events: none;
  opacity: 0.92;
  transform: scale(1.03);
  box-shadow: 0 16px 25px rgba(37, 31, 23, 0.28);
}

.hint-outline {
  z-index: 2;
  border: 3px dashed rgba(255, 255, 255, 0.88);
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 2px rgba(24, 51, 47, 0.42);
  animation: hintPulse 1.2s ease-in-out infinite;
  pointer-events: none;
}

.piece-dock {
  margin-top: 14px;
  padding: 13px;
  border: 1px solid rgba(24, 51, 47, 0.08);
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.38);
  box-shadow: 0 10px 28px rgba(63, 49, 31, 0.07);
}

.dock-heading {
  margin: 0 2px 10px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 750;
  letter-spacing: 0.04em;
}

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

.tray-slot {
  position: relative;
  display: grid;
  place-items: center;
  height: 62px;
  overflow: hidden;
  border-radius: 13px;
  background: rgba(112, 97, 76, 0.07);
}

.tray-slot.placed::after {
  content: "✓";
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  color: white;
  background: rgba(24, 51, 47, 0.76);
  font-size: 0.76rem;
  font-weight: 900;
}

.tray-piece {
  min-width: 18px;
  min-height: 18px;
}

.piece-label {
  font-size: 0.58rem;
  font-weight: 900;
  letter-spacing: -0.02em;
  pointer-events: none;
}

.action-bar {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  width: min(100%, 610px);
  margin: 0 auto;
  padding: 9px 16px calc(9px + env(safe-area-inset-bottom));
  border-top: 1px solid rgba(24, 51, 47, 0.1);
  background: rgba(247, 242, 233, 0.9);
  box-shadow: 0 -12px 30px rgba(61, 49, 33, 0.08);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.action-bar button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px;
  border: 0;
  border-radius: 13px;
  color: #646861;
  background: transparent;
  font-size: 0.65rem;
  font-weight: 750;
  cursor: pointer;
}

.action-bar button:disabled {
  opacity: 0.3;
}

.action-bar button:not(:disabled):active {
  transform: scale(0.95);
}

.action-bar .primary-action {
  color: white;
  background: var(--ink);
}

.action-icon {
  font-size: 1.35rem;
  font-weight: 500;
  line-height: 1;
}

.toast {
  position: fixed;
  bottom: calc(89px + env(safe-area-inset-bottom));
  left: 50%;
  z-index: 120;
  max-width: calc(100vw - 36px);
  padding: 10px 15px;
  border-radius: 999px;
  color: white;
  background: rgba(24, 51, 47, 0.94);
  box-shadow: 0 10px 25px rgba(25, 35, 31, 0.23);
  font-size: 0.78rem;
  font-weight: 700;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: opacity 180ms ease, transform 180ms ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

dialog {
  width: min(calc(100% - 30px), 480px);
  max-height: min(86dvh, 720px);
  padding: 22px;
  overflow-y: auto;
  border: 0;
  border-radius: 27px;
  color: var(--ink);
  background: #f8f4eb;
  box-shadow: 0 35px 90px rgba(25, 31, 27, 0.3);
}

dialog::backdrop {
  background: rgba(25, 35, 31, 0.42);
  backdrop-filter: blur(5px);
}

.dialog-head h2,
.win-dialog h2 {
  margin-bottom: 0;
  font-size: 1.6rem;
  letter-spacing: -0.04em;
}

.close-button {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: rgba(24, 51, 47, 0.08);
  font-size: 1.25rem;
  cursor: pointer;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 20px;
}

.level-group-title {
  grid-column: 1 / -1;
  margin: 10px 2px 0;
  color: var(--muted);
  font-size: 0.76rem;
  letter-spacing: 0.12em;
}

.level-group-title:first-child {
  margin-top: 0;
}

.level-card {
  position: relative;
  min-height: 86px;
  padding: 13px;
  overflow: hidden;
  border: 1px solid rgba(24, 51, 47, 0.1);
  border-radius: 17px;
  background: rgba(255, 255, 255, 0.5);
  text-align: left;
  cursor: pointer;
}

.level-card.current {
  border-color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ink);
}

.level-card b,
.level-card span {
  display: block;
}

.level-card b {
  margin-bottom: 4px;
  font-size: 0.88rem;
}

.level-card span {
  color: var(--muted);
  font-size: 0.7rem;
}

.level-card small {
  position: absolute;
  right: 10px;
  bottom: 8px;
  color: rgba(24, 51, 47, 0.18);
  font-size: 1.65rem;
  font-weight: 900;
}

.level-card.completed small {
  color: #6d957b;
}

.clear-progress {
  display: block;
  margin: 17px auto 0;
  padding: 8px 12px;
  border: 0;
  color: var(--muted);
  background: transparent;
  font-size: 0.72rem;
  cursor: pointer;
}

.clear-progress.armed {
  color: #b04e3d;
  font-weight: 800;
}

.win-dialog {
  text-align: center;
}

.win-mark {
  display: grid;
  place-items: center;
  width: 68px;
  height: 68px;
  margin: 0 auto 15px;
  border-radius: 23px;
  color: white;
  background: var(--ink);
  box-shadow: 0 15px 30px rgba(24, 51, 47, 0.22);
  font-size: 2rem;
  transform: rotate(-4deg);
}

.win-dialog > p:not(.eyebrow) {
  color: var(--muted);
}

.win-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 21px 0;
}

.win-stats div {
  padding: 14px;
  border-radius: 16px;
  background: rgba(24, 51, 47, 0.06);
}

.win-stats b,
.win-stats span {
  display: block;
}

.win-stats b {
  font-size: 1.35rem;
  font-variant-numeric: tabular-nums;
}

.win-stats span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.68rem;
}

.continue-button,
.text-button {
  width: 100%;
  padding: 13px;
  border: 0;
  border-radius: 15px;
  font-weight: 800;
  cursor: pointer;
}

.continue-button {
  color: white;
  background: var(--ink);
}

.text-button {
  margin-top: 5px;
  color: var(--muted);
  background: transparent;
}

.shake {
  animation: shake 280ms ease;
}

@keyframes shake {
  25% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-3px); }
}

@keyframes hintPulse {
  50% { opacity: 0.5; transform: scale(0.98); }
}

@media (min-width: 700px) {
  body { padding: 20px 0; }
  .app-shell {
    min-height: calc(100dvh - 40px);
    border: 1px solid rgba(24, 51, 47, 0.08);
    border-radius: 30px;
    background: rgba(255, 255, 255, 0.2);
    box-shadow: 0 20px 70px rgba(67, 51, 32, 0.1);
  }
}

@media (max-height: 760px) and (max-width: 500px) {
  .app-shell { padding-top: 12px; }
  .level-summary { margin-top: 15px; }
  .level-track { margin: 12px 0; }
  .board-wrap { width: min(82vw, 390px); }
  .piece-dock { margin-top: 9px; padding: 9px; }
  .tray-slot { height: 50px; }
}

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