:root {
  --ink: #13475c;
  --muted: #52798a;
  --sea: #178c93;
  --sea-dark: #0a6972;
  --aqua: #65c9c1;
  --foam: #fffdf7;
  --sand: #ebc988;
  --shell: #fff8e9;
  --panel: rgba(255, 252, 238, .88);
  --line: rgba(17, 126, 151, .18);
  --shadow: 0 30px 80px rgba(12, 98, 116, .28);
}

* { box-sizing: border-box; }

html,
body {
  width: 100%; height: 100%; margin: 0; overflow: hidden;
  background: #9ee6ee; color: var(--ink);
  font-family: "Anuphan", "Noto Sans Thai", "Leelawadee UI", sans-serif;
  touch-action: none; user-select: none;
}

body {
  position: relative; display: grid; place-items: center; padding: 18px;
  background: #8edfe9;
}

.beach-scene::before,
.beach-scene::after {
  content: ""; position: absolute; z-index: 0; left: -8%; width: 116%;
  pointer-events: none;
}

.beach-scene::before {
  top: 57%; height: clamp(28px, 12vh, 65px); border-radius: 50%;
  background: rgba(255,255,255,.75); transform: rotate(-1.5deg);
  box-shadow: none;
}

.beach-scene::after {
  top: 60%; height: clamp(18px, 7vh, 36px); border-radius: 50%;
  background: rgba(199,247,241,.88); transform: rotate(-1.5deg);
}

.beach-scene {
  position: fixed; z-index: 1; inset: 0; overflow: hidden;
  border: 0; border-radius: 0;
  background: linear-gradient(180deg, #8edfe9 0%, #c8f1ef 47%, #28aab9 48%, #087d9a 62%, #f4d7a5 63%, #efd09b 100%);
  box-shadow: inset 0 0 38px rgba(255,255,255,.1);
  pointer-events: none;
}

.sun {
  position: absolute; top: 8%; right: 10%; width: clamp(40px, 7vw, 134px);
  aspect-ratio: 1; border-radius: 50%; background: #ffd064;
  box-shadow: 0 0 0 17px rgba(255,213,106,.16), 0 0 55px rgba(255,197,75,.55);
}

.cloud {
  position: absolute; width: 115px; height: 30px; border-radius: 999px;
  background: rgba(255,255,255,.7); filter: blur(.2px);
}

.cloud i {
  position: absolute; bottom: 7px; border-radius: 50%; background: inherit;
}

.cloud i:nth-child(1) { left: 20px; width: 45px; height: 45px; }
.cloud i:nth-child(2) { right: 18px; width: 58px; height: 58px; }
.cloud i:nth-child(3) { display: none; }
.cloud-left { top: 15%; left: 8%; transform: scale(.62); transform-origin: top left; }
.cloud-right { top: 27%; right: 20%; transform: scale(.43); opacity: .7; }

.birds {
  position: absolute; z-index: 2; top: 17%; right: 28%;
  width: 120px; height: 58px; opacity: .78;
}

.birds i {
  position: absolute; width: 24px; height: 10px;
  transform: rotate(-2deg);
}

.birds i::before,
.birds i::after {
  content: ""; position: absolute; top: 2px; width: 13px; height: 7px;
  border-top: 2.2px solid #1d6370;
  border-radius: 50% 50% 0 0;
}

.birds i::before {
  left: 0; transform: rotate(13deg);
  transform-origin: right top;
}

.birds i::after {
  right: 0; transform: rotate(-13deg);
  transform-origin: left top;
}

.birds i:nth-child(1) { top: 2px; left: 4px; transform: scale(.78); }
.birds i:nth-child(2) { top: 20px; left: 46px; transform: scale(1); }
.birds i:nth-child(3) { top: 4px; right: 2px; transform: scale(.6); opacity: .72; }

.game-shell {
  position: relative; z-index: 3; isolation: isolate;
  display: grid; grid-template-rows: auto 1fr auto;
  width: min(814px, calc(100vw - 30px), calc((100vh - 50px) * .735));
  height: min(1108px, calc(100vh - 50px));
  overflow: hidden; padding: clamp(8px, 1.45vw, 30px);
  border: 1px solid rgba(255,255,255,.92); border-radius: clamp(14px, 2.25vw, 46px);
  background:
    linear-gradient(145deg, rgba(255,255,248,.3), rgba(255,248,226,.16));
  box-shadow:
    0 32px 90px rgba(6,91,107,.2),
    0 8px 24px rgba(24,112,122,.08),
    inset 0 0 0 1px rgba(255,255,255,.22),
    inset 0 1px 0 rgba(255,255,255,.95);
  backdrop-filter: blur(18px) saturate(1.12);
  -webkit-backdrop-filter: blur(18px) saturate(1.12);
  transform: translateY(9px);
}

.game-shell::before {
  content: ""; position: absolute; z-index: -1; inset: 1px;
  border-radius: inherit; pointer-events: none;
  background:
    radial-gradient(circle at 14% 4%, rgba(255,255,255,.28), transparent 28%),
    linear-gradient(115deg, rgba(255,255,255,.1), transparent 42%);
}

.game-shell::after {
  content: ""; position: absolute; z-index: 4;
  top: 0; left: 12%; width: 76%; height: 1px;
  pointer-events: none;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,.95), transparent);
}

.hud {
  z-index: 2; display: grid; grid-template-columns: 1fr auto;
  align-items: center; gap: clamp(5px, .8vw, 16px);
  padding: clamp(4px, .8vw, 16px) clamp(5px, .55vw, 12px);
  border: 1px solid rgba(255,255,255,.34);
  border-radius: clamp(9px, .9vw, 18px);
  background: linear-gradient(145deg, rgba(255,255,255,.2), rgba(222,248,243,.08));
  box-shadow: inset 0 1px 0 rgba(255,255,255,.42);
}

.brand { display: flex; align-items: center; gap: 0; }

.brand-mark {
  display: none; place-items: center; width: 31px; height: 31px;
  border-radius: 10px; color: #fff;
  background: linear-gradient(145deg, #54c8bf, #0d7880);
  box-shadow: 0 6px 14px rgba(15,114,122,.18);
}

.brand-mark svg { width: 19px; height: 19px; }

.brand-copy strong {
  display: block; font-family: "DM Serif Display", Georgia, serif;
  color: #007e99; font-size: clamp(18px, 3.3vw, 68px);
  font-weight: 400; line-height: .9; letter-spacing: .01em;
  text-shadow: 0 2px 0 rgba(255,255,255,.82);
}

.brand-copy::before {
  content: "BEACH PUZZLE"; display: block; margin-bottom: 2px;
  color: #ee7e4f; font-size: clamp(5px, .72vw, 15px);
  font-weight: 800; letter-spacing: .18em;
}

.brand-copy span {
  display: block; margin-top: 3px; color: #607d7c;
  font-size: clamp(6px, .72vw, 15px); font-weight: 700; letter-spacing: .02em;
  text-shadow: 0 1px 0 rgba(255,255,255,.72);
}

.header-actions {
  display: flex; align-items: center; justify-content: flex-end;
  gap: clamp(4px, .5vw, 10px);
}

.sound-button {
  display: grid; place-items: center;
  width: clamp(24px, 2.5vw, 42px); aspect-ratio: 1;
  padding: 0; border-radius: 50%;
  color: var(--sea); background: rgba(236,253,248,.8);
  border: 1px solid rgba(13,137,158,.13);
  box-shadow: 0 4px 12px rgba(17,126,151,.08);
}

.sound-button svg {
  width: 48%; height: 48%;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

.sound-button .sound-off { display: none; }
.sound-button.muted .sound-on { display: none; }
.sound-button.muted .sound-off { display: block; }

.hud-stats {
  grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr;
  align-items: center; gap: clamp(6px, 1vw, 20px);
  padding: clamp(3px, .65vw, 13px) clamp(6px, 1vw, 20px);
  border: 1px solid rgba(255,255,255,.7);
  border-radius: clamp(7px, .8vw, 16px);
  background: rgba(224,248,243,.7);
  box-shadow: 0 5px 15px rgba(17,126,151,.08), inset 0 1px 0 rgba(255,255,255,.75);
}

.hud-box small {
  display: block; margin-bottom: 1px; color: var(--muted);
  font-size: clamp(5px, .72vw, 15px); font-weight: 700; letter-spacing: .03em;
}

.hud-box {
  min-width: 0; padding: 2px 4px; border: 0;
  border-radius: 8px; background: transparent; text-align: left;
}

.hud-box:last-child { text-align: right; }
.hud-box strong { color: #176c79; font-size: clamp(10px, 1.45vw, 30px); line-height: 1; }

.level-pill {
  padding: clamp(5px, .7vw, 14px) clamp(8px, 1vw, 20px);
  border: 1px solid rgba(13,137,158,.13);
  border-radius: 999px; background: rgba(236,253,248,.8);
  color: var(--sea); font-size: clamp(6px, .75vw, 16px); font-weight: 800; white-space: nowrap;
  box-shadow: 0 4px 12px rgba(17,126,151,.08);
}

.stage {
  z-index: 1; position: relative; min-height: 0; overflow: hidden;
  display: grid; place-items: center; padding: 0;
  container-type: size;
  border-radius: clamp(9px, 1.15vw, 23px);
  background: transparent;
}

.stage::before {
  content: none;
}

.stage-copy {
  display: none; position: absolute; z-index: 1; top: 4px; left: 50%; width: 100%;
  transform: translateX(-50%); text-align: center; pointer-events: none;
}

.stage-copy strong { display: block; color: #fff; font-size: 10px; letter-spacing: -.01em; }
.stage-copy span { color: rgba(255,255,255,.78); font-size: 7px; font-weight: 500; }

.tutorial {
  position: absolute; z-index: 4; top: clamp(8px, 1vw, 18px); left: 50%;
  width: max-content; max-width: calc(100% - 24px);
  padding: clamp(6px, .6vw, 11px) clamp(9px, .9vw, 18px);
  transform: translateX(-50%);
  border: 1px solid rgba(255,255,255,.78); border-radius: 999px;
  background: rgba(9,113,126,.84); color: #fff;
  box-shadow: 0 8px 22px rgba(7,83,95,.2);
  font-size: clamp(7px, .72vw, 14px); font-weight: 700;
  text-align: center; pointer-events: none;
  opacity: 0; translate: 0 -5px;
  transition: opacity .25s ease, translate .25s ease;
}

.tutorial.show { opacity: 1; translate: 0 0; }

.board-wrap {
  position: relative; width: 100%;
  width: min(100%, 100cqh); max-width: none; aspect-ratio: 1;
  padding: 0; border: 0;
  border-radius: clamp(11px, 1.5vw, 30px);
  background: transparent;
  box-shadow: none;
  translate: 0;
}

.board-wrap::after {
  content: none;
}

canvas {
  display: block; width: 100%; aspect-ratio: 1;
  border: 0; border-radius: clamp(11px, 1.5vw, 30px);
  background: #f4d6a3; box-shadow: none;
  cursor: crosshair;
}

.complete {
  position: absolute; inset: 0; display: grid; place-items: center;
  z-index: 5; padding: 22px; background: rgba(9,76,83,.38); backdrop-filter: blur(9px);
  opacity: 0; pointer-events: none; transition: opacity .2s ease;
}

.complete.show { opacity: 1; pointer-events: auto; }

.level-picker {
  position: absolute; z-index: 20; inset: 0;
  display: grid; place-items: center; padding: clamp(12px, 2vw, 30px);
  background: rgba(8,82,94,.28);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease;
}

.level-picker.show {
  opacity: 1; pointer-events: auto;
}

.level-panel {
  width: min(100%, 520px); max-height: 88%; overflow: auto;
  padding: clamp(14px, 1.5vw, 26px);
  border: 1px solid rgba(255,255,255,.88);
  border-radius: clamp(18px, 1.8vw, 34px);
  background: rgba(255,250,235,.94);
  box-shadow: 0 25px 70px rgba(5,72,84,.28);
}

.level-panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: clamp(12px, 1.3vw, 22px);
}

.level-panel-head small {
  color: #ef7c5e; font-size: clamp(7px, .65vw, 12px);
  font-weight: 800; letter-spacing: .18em;
}

.level-panel-head h2 {
  margin: 2px 0 0; color: var(--sea);
  font-size: clamp(20px, 2.1vw, 36px);
}

.close-levels {
  width: clamp(32px, 3vw, 46px); aspect-ratio: 1; padding: 0;
  border-radius: 50%; color: var(--sea);
  background: rgba(224,248,243,.82);
  font-size: clamp(20px, 2vw, 30px); line-height: 1;
}

.level-grid {
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: clamp(7px, .8vw, 12px);
}

.level-card {
  position: relative; display: grid; place-items: center;
  min-width: 0; aspect-ratio: 1; padding: 5px;
  border: 1px solid rgba(17,126,151,.16);
  border-radius: clamp(11px, 1vw, 17px);
  color: var(--sea); background: rgba(255,255,255,.72);
  box-shadow: 0 7px 16px rgba(20,96,105,.08);
}

.level-card strong { font-size: clamp(14px, 1.4vw, 24px); }
.level-card span {
  color: #eaa52b; font-size: clamp(7px, .75vw, 13px);
  letter-spacing: -.08em; white-space: nowrap;
}

.level-card.current {
  color: #fff; border-color: transparent;
  background: linear-gradient(145deg, #35beb5, #087f8c);
  box-shadow: 0 9px 20px rgba(8,127,140,.22);
}

.level-card.current span { color: #ffe385; }

.level-card.locked {
  color: #9aa9a6; background: rgba(235,236,226,.58);
}

.level-card .lock {
  color: #9aa9a6; font-size: clamp(11px, 1.1vw, 18px);
}

.panel {
  width: min(100%, 340px); padding: 30px 24px 24px;
  border: 1px solid rgba(255,255,255,.9); border-radius: 28px;
  background: rgba(255,249,234,.98); text-align: center;
  box-shadow: 0 26px 70px rgba(7,72,82,.3);
}

.win-icon {
  display: grid; place-items: center; width: 60px; height: 60px;
  margin: 0 auto 15px; border-radius: 20px;
  background: linear-gradient(145deg,#58d3c5,#087f8c);
  box-shadow: 0 12px 30px rgba(8,125,154,.25); font-size: 28px;
}

.win-icon svg {
  width: 33px; height: 33px; color: #fff7e7;
  stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round;
}

h2 { margin: 0 0 7px; font-size: 27px; }

.panel p {
  margin: 0 0 18px; color: #61808b;
  font-size: 13px; line-height: 1.65;
}

button {
  border: 1px solid var(--line); border-radius: 14px;
  background: rgba(255,255,255,.68); color: var(--ink); font: inherit;
  font-size: 12px; font-weight: 800; cursor: pointer;
  transition: transform .15s ease, background .15s ease, box-shadow .15s ease;
}

button:disabled {
  opacity: .38; cursor: default; box-shadow: none;
  filter: saturate(.55);
}

.primary {
  width: 100%; padding: 13px 20px; border: 0;
  color: #fff; background: linear-gradient(135deg,#35bdb4,#087f8c);
  box-shadow: 0 10px 25px rgba(8,125,154,.25);
}

.controls {
  z-index: 3; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px;
  padding: clamp(5px, .75vw, 15px) 1px 1px;
  border: 1px solid rgba(255,255,255,.3);
  border-radius: clamp(9px, .85vw, 17px);
  background: rgba(255,255,255,.1);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.35);
  translate: 0 4px;
}

.controls button {
  display: grid; place-items: center; align-content: center; gap: 2px;
  height: clamp(20px, 5vw, 60px); padding: 0 2px;
  border-radius: clamp(7px, .65vw, 13px);
  background: rgba(255,252,244,.8);
  border-color: rgba(17,126,151,.18);
  box-shadow: 0 5px 14px rgba(22,96,105,.08);
}

.controls svg {
  width: clamp(8px, 1.15vw, 24px); height: clamp(8px, 1.15vw, 24px); stroke-width: 1.65;
  stroke-linecap: round; stroke-linejoin: round;
}
.controls .shell-icon { color: #b97848; }
.controls .wave-icon { color: #278f9a; }
.controls .starfish-icon { color: #dc815d; }
.controls .sailboat-icon { color: currentColor; }
.controls span { font-size: clamp(5px, .82vw, 17px); }
.controls button:hover { background: #fff; box-shadow: 0 7px 18px rgba(22,95,99,.14); }
.controls button:active { transform: translateY(1px) scale(.98); }

.controls .accent {
  color: #fff; border-color: transparent;
  background: linear-gradient(145deg, #2ab8ae, #087f8c);
  box-shadow: 0 7px 18px rgba(8,127,140,.2);
}

.controls .accent:hover {
  background: linear-gradient(145deg, #35c7bc, #087f8c);
}

@media (max-height: 610px) {
  .hud { padding-block: 4px; }
  .hud-stats { padding-block: 2px; }
  .stage { padding: 0; }
  .stage-copy { top: 3px; }
  .controls { gap: 4px; padding-top: 4px; }
}

@media (max-width: 380px) {
  .hud { gap: 6px; padding-inline: 7px; }
  .brand-copy strong { font-size: 17px; }
  .hud-box { min-width: 0; padding-inline: 3px; }
  .level-pill { padding-inline: 8px; }
}

@media (max-width: 520px) {
  body { padding: 10px; }
  .beach-scene {
    inset: 0;
    border-radius: 0;
  }
  .game-shell {
    width: min(390px, calc(100vw - 20px), calc((100vh - 20px) * .735));
    height: auto;
    aspect-ratio: .735;
    padding: 9px;
    border-radius: 22px;
    transform: none;
  }
  .hud {
    padding: 5px 6px 6px;
  }
  .hud-stats {
    padding-block: 3px;
  }
  .stage {
    padding: 0;
  }
  .board-wrap {
    width: 100%;
    width: min(100%, 100cqh);
    translate: 0;
  }
  .controls {
    padding-top: 4px;
    translate: 0 2px;
  }
  .controls button {
    height: clamp(28px, 8vw, 34px);
  }
  .controls svg {
    width: 12px;
    height: 12px;
  }
  .controls span {
    font-size: 7px;
  }
  .sun {
    top: 5%;
    right: 4%;
    width: 54px;
  }
  .cloud-left {
    top: 13%;
    left: 3%;
    transform: scale(.5);
  }
  .cloud-right {
    display: none;
  }
  .birds {
    top: 12%;
    right: 8%;
    transform: scale(.72);
    transform-origin: top right;
  }
  .level-panel {
    max-height: 92%; padding: 14px;
  }
  .level-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
  }
}

@media (min-width: 900px) {
  .cloud-left {
    top: 15%; left: 8%; transform: scale(1.38);
    transform-origin: top left;
  }
  .cloud-right {
    top: 27%; right: 20%; transform: scale(.82);
    transform-origin: top right;
  }
  .birds {
    top: 17%;
    right: 20%;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .board-wrap { animation: boardIn .55s cubic-bezier(.2,.75,.25,1) both; }
  .birds { animation: birdsDrift 7s ease-in-out infinite alternate; }

  @keyframes boardIn {
    from { opacity: 0; transform: translateY(10px) scale(.985); }
    to { opacity: 1; transform: translateY(0) scale(1); }
  }

  @keyframes birdsDrift {
    from { translate: -5px 1px; }
    to { translate: 8px -4px; }
  }
}
