:root {
  color-scheme: light;
  --ink: #2f2a25;
  --muted: #766b62;
  --paper: #fff9ee;
  --paper-strong: #fffdf8;
  --line: #e8d8be;
  --leaf: #4cae67;
  --leaf-dark: #2f7c46;
  --moss: #77ba74;
  --berry: #e75f7c;
  --nut: #b26d3e;
  --bloom: #8b7ef6;
  --drop: #49b8d7;
  --gold: #ffd166;
  --coral: #ff7a6f;
  --shadow: 0 14px 36px rgba(74, 52, 35, .16);
  --soft-shadow: 0 7px 18px rgba(74, 52, 35, .12);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  overscroll-behavior: none;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background-color: #dff5ff;
  background-image:
    linear-gradient(180deg, rgba(223, 245, 255, .3) 0%, rgba(242, 251, 223, .12) 38%, rgba(255, 232, 225, .5) 100%),
    url("assets/forest-puzzle-bg.png"),
    linear-gradient(180deg, #dff5ff 0%, #f2fbdf 34%, #fff1dd 74%, #ffe8e1 100%);
  background-repeat: no-repeat;
  background-size: cover, cover, cover;
  background-position: center top, center center, center;
  background-attachment: fixed;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 50% 46%, rgba(255, 252, 234, .04), rgba(255, 252, 234, .34) 28%, rgba(50, 93, 47, .16) 100%),
    linear-gradient(180deg, rgba(255, 255, 255, .16), rgba(255, 244, 216, .08));
  z-index: -1;
}

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

.game-shell {
  position: relative;
  width: min(100%, 520px);
  min-height: 100svh;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 14px max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  isolation: isolate;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 4px 0;
  text-shadow: 0 2px 8px rgba(255, 255, 255, .78);
}

.stage-kicker {
  margin: 0 0 3px;
  color: var(--leaf-dark);
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  display: inline-block;
  padding: 4px 10px 5px;
  border: 1px solid rgba(255, 255, 255, .55);
  border-radius: 8px;
  font-size: 1.28rem;
  line-height: 1.2;
  background: linear-gradient(180deg, rgba(255,255,255,.72), rgba(255,250,238,.48));
  box-shadow: 0 8px 18px rgba(74, 52, 35, .11);
  backdrop-filter: blur(8px);
}

.round-button {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  display: inline-grid;
  place-items: center;
  border: 2px solid rgba(47, 124, 70, .3);
  border-radius: 50%;
  color: var(--leaf-dark);
  background: linear-gradient(180deg, rgba(255,255,255,.94), rgba(255,243,216,.86));
  box-shadow: var(--soft-shadow);
}

.round-button:active,
.control-button:active,
.primary-button:active {
  transform: translateY(1px) scale(.99);
}

.hud {
  display: grid;
  grid-template-columns: 70px 1fr;
  gap: 8px;
  align-items: stretch;
}

.hud-item,
.goal-strip {
  border: 1px solid rgba(255, 255, 255, .64);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .82), rgba(255, 248, 230, .72));
  backdrop-filter: blur(10px);
  box-shadow: var(--soft-shadow);
}

.hud-item {
  min-height: 64px;
  padding: 9px 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}

.hud-item span {
  color: var(--muted);
  font-size: .75rem;
  font-weight: 700;
}

.hud-item strong {
  font-size: 1.45rem;
  line-height: 1.05;
}

.hud-item.score {
  display: none;
}

.goal-strip {
  min-height: 64px;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.goal {
  min-width: 0;
  height: 48px;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: 1px solid rgba(225, 191, 132, .45);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(255,255,255,.88), rgba(255,245,222,.75));
}

.goal svg {
  width: 25px;
  height: 25px;
  flex: 0 0 25px;
}

.goal .sprite {
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
}

.goal strong {
  min-width: 0;
  font-size: .86rem;
  line-height: 1;
  white-space: nowrap;
}

.goal.complete {
  border-color: rgba(76, 174, 103, .6);
  background: #effbea;
}

.board-zone {
  position: relative;
  padding: 9px;
  border: 2px solid rgba(105, 83, 43, .26);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 253, 248, .72), rgba(255, 241, 207, .78)),
    radial-gradient(circle at 50% 0%, rgba(255,255,255,.55), transparent 42%);
  backdrop-filter: blur(10px);
  box-shadow:
    0 16px 38px rgba(74, 52, 35, .22),
    inset 0 1px 0 rgba(255, 255, 255, .75);
}

.forest-ribbon {
  height: 18px;
  display: flex;
  justify-content: center;
  gap: 9px;
  margin-bottom: 4px;
}

.forest-ribbon span {
  width: 20px;
  height: 16px;
  border-radius: 16px 16px 4px 4px;
  background: var(--moss);
}

.forest-ribbon span:nth-child(2) { background: var(--gold); }
.forest-ribbon span:nth-child(3) { background: var(--berry); }
.forest-ribbon span:nth-child(4) { background: var(--drop); }
.forest-ribbon span:nth-child(5) { background: var(--leaf); }

.board {
  width: min(100%, 480px, calc(100svh - 286px));
  min-width: min(100%, 284px);
  aspect-ratio: 1;
  margin: 0 auto;
  padding: 7px;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 5px;
  border: 5px solid rgba(115, 80, 42, .78);
  border-radius: 8px;
  background:
    radial-gradient(circle at 50% 0%, rgba(255, 239, 176, .45), transparent 58%),
    linear-gradient(135deg, #6aaa55, #3f8858);
  box-shadow:
    inset 0 0 0 2px rgba(255, 231, 157, .55),
    inset 0 10px 24px rgba(26, 80, 39, .28),
    0 9px 18px rgba(69, 42, 24, .23);
  touch-action: none;
  user-select: none;
}

.tile {
  position: relative;
  min-width: 0;
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 0;
  border-radius: 16px;
  background:
    radial-gradient(circle at 34% 24%, rgba(255,255,255,.96), rgba(255,255,255,.52) 34%, transparent 50%),
    linear-gradient(180deg, #fffaf0, #f6deb0);
  box-shadow:
    inset 0 -5px 0 rgba(120, 81, 41, .18),
    inset 0 1px 0 rgba(255, 255, 255, .88),
    0 3px 0 rgba(68, 113, 58, .28);
  cursor: pointer;
  touch-action: none;
  transition: transform .14s ease, filter .14s ease, box-shadow .14s ease;
}

.tile svg {
  width: 76%;
  height: 76%;
  pointer-events: none;
}

.sprite {
  width: 86%;
  height: 86%;
  display: block;
  background-image: url("assets/forest-puzzle-sprites.png");
  background-repeat: no-repeat;
  background-size: 500% 100%;
  filter: drop-shadow(0 4px 5px rgba(47, 42, 37, .24));
  pointer-events: none;
}

.sprite-leaf { background-position: 0% 50%; }
.sprite-berry { background-position: 25% 50%; }
.sprite-nut { background-position: 50% 50%; }
.sprite-bloom { background-position: 75% 50%; }
.sprite-drop { background-position: 100% 50%; }

.tile.selected {
  box-shadow: inset 0 -4px 0 rgba(83, 60, 35, .13), 0 0 0 4px rgba(255, 209, 102, .95);
  transform: translateY(-2px);
}

.tile.matched {
  animation: pop-out .26s ease forwards;
}

.tile.hint {
  animation: hint-pulse .8s ease-in-out infinite;
}

.tile:disabled {
  cursor: default;
}

.message {
  min-height: 34px;
  margin: 8px auto 0;
  padding: 5px 11px;
  display: grid;
  place-items: center;
  width: fit-content;
  max-width: 100%;
  border: 1px solid rgba(255,255,255,.55);
  border-radius: 999px;
  background: rgba(255, 252, 238, .72);
  color: var(--leaf-dark);
  font-size: .86rem;
  font-weight: 800;
  text-align: center;
  box-shadow: 0 6px 16px rgba(74, 52, 35, .11);
  backdrop-filter: blur(8px);
}

.controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  padding-bottom: max(0px, env(safe-area-inset-bottom));
}

.control-button,
.primary-button {
  min-height: 52px;
  border: 0;
  border-radius: 8px;
  font-weight: 800;
  color: #fff;
  box-shadow: var(--soft-shadow);
}

.control-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--leaf-dark);
}

.control-button:nth-child(2) {
  background: #c86446;
}

.control-icon {
  width: 24px;
  height: 24px;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  color: var(--ink);
  background: rgba(255, 255, 255, .9);
  font-weight: 900;
}

.result-backdrop {
  position: fixed;
  inset: 0;
  padding: 24px;
  display: grid;
  place-items: center;
  background: rgba(47, 42, 37, .4);
  z-index: 10;
}

.result-backdrop[hidden] {
  display: none;
}

.result-panel {
  width: min(100%, 360px);
  padding: 24px;
  border: 2px solid rgba(255, 209, 102, .9);
  border-radius: 8px;
  text-align: center;
  background: var(--paper-strong);
  box-shadow: var(--shadow);
}

.result-panel h2 {
  font-size: 1.8rem;
}

.result-panel p {
  margin: 10px 0;
}

.result-score {
  color: var(--muted);
  font-weight: 800;
}

.result-score strong {
  color: var(--ink);
  font-size: 1.4rem;
}

.primary-button {
  width: 100%;
  margin-top: 10px;
  background: var(--leaf-dark);
}

@keyframes pop-out {
  0% { transform: scale(1); opacity: 1; }
  60% { transform: scale(1.16) rotate(-3deg); opacity: .85; }
  100% { transform: scale(.2); opacity: 0; }
}

@keyframes hint-pulse {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-3px) scale(1.05); }
}

@media (min-width: 430px) {
  .hud {
    grid-template-columns: 72px 88px 1fr;
  }

  .hud-item.score {
    display: flex;
  }
}

@media (max-height: 690px) {
  .game-shell {
    gap: 8px;
    padding-top: 8px;
  }

  .hud-item,
  .goal-strip {
    min-height: 56px;
  }

  .goal {
    height: 42px;
  }

  .forest-ribbon {
    display: none;
  }

  .message {
    min-height: 28px;
    margin-top: 5px;
  }
}

@media (max-width: 360px) {
  .game-shell {
    padding-left: 10px;
    padding-right: 10px;
  }

  h1 {
    font-size: 1.12rem;
  }

  .goal {
    gap: 2px;
  }

  .goal strong {
    font-size: .78rem;
  }
}

@media (pointer: coarse) {
  .round-button {
    width: 48px;
    height: 48px;
    flex-basis: 48px;
  }
}
