:root {
  --sand: #f6e2b8;
  --sand-deep: #e8c48a;
  --wood: #8b5324;
  --wood-dark: #5c3310;
  --wood-light: #c48a4a;
  --cream: #fff6e8;
  --ink: #3a2412;
  --coral: #e85d4c;
  --coral-deep: #c43c2e;
  --teal: #1f9aa8;
  --teal-bright: #3ec1c8;
  --gold: #f0c14a;
  --leaf: #2f7a3e;
  --lilac: #c4a0ef;
  --lilac-deep: #8b5fc7;
  --lagoon: #1f7a82;
  --lagoon-deep: #11484e;
  --sky-peach: #f4b183;
  --panel: rgba(255, 248, 235, 0.92);
  --shadow: 0 18px 40px rgba(60, 30, 10, 0.28);
  --radius: 28px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  color: var(--ink);
  font-family: "Nunito", system-ui, sans-serif;
  background: #f4b183;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font: inherit;
  border: none;
  cursor: pointer;
  color: inherit;
}

.sky {
  position: fixed;
  inset: 0;
  z-index: 0;
  background:
    linear-gradient(180deg, rgba(255, 210, 160, 0.18), rgba(255, 248, 230, 0.55) 55%, rgba(246, 226, 184, 0.72)),
    url("images/palm-grove-bg.jpg") center 42% / cover no-repeat;
}

.screen {
  position: fixed;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  overflow: auto;
}

.screen.hidden { display: none; }

.loading {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-content: center;
  gap: 12px;
  background: rgba(244, 177, 131, 0.92);
  text-align: center;
  font-weight: 800;
  color: var(--ink);
  transition: opacity 0.35s ease, visibility 0.35s ease;
}
.loading img {
  margin: 0 auto;
  filter: drop-shadow(0 8px 14px rgba(60, 30, 10, 0.25));
  animation: bob 1.2s ease-in-out infinite;
}
.loading.done {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.35s ease, visibility 0s linear 0.35s;
}

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-4deg); }
  50% { transform: translateY(-10px) rotate(4deg); }
}

.depot-link {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: 12px;
  z-index: 5;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(255, 248, 235, 0.85);
  color: var(--wood-dark);
  text-decoration: none;
  font-weight: 800;
  font-size: 14px;
  box-shadow: 0 4px 14px rgba(60, 30, 10, 0.18);
  backdrop-filter: blur(6px);
}

.splash-card,
.panel {
  width: min(440px, 100%);
  background: var(--panel);
  border: 3px solid rgba(139, 83, 36, 0.35);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 22px 22px;
  text-align: center;
  backdrop-filter: blur(10px);
}

.cluster {
  display: block;
  width: 140px;
  height: 140px;
  object-fit: contain;
  margin: -8px auto 4px;
  filter: drop-shadow(0 10px 16px rgba(60, 30, 10, 0.28));
}
.cluster.small {
  width: 88px;
  height: 88px;
  margin-bottom: 2px;
}

.eyebrow {
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--teal);
  margin-bottom: 4px;
}

h1, h2 {
  font-family: "Pacifico", cursive;
  font-weight: 400;
  line-height: 1.15;
  color: var(--wood-dark);
  text-shadow: 0 2px 0 rgba(255, 255, 255, 0.5);
}

h1 { font-size: clamp(34px, 8vw, 48px); margin-bottom: 10px; }
h2 { font-size: clamp(28px, 6vw, 36px); margin-bottom: 8px; }

.tag {
  font-size: 15px;
  line-height: 1.4;
  font-weight: 700;
  color: rgba(58, 36, 18, 0.82);
  margin: 0 auto 18px;
  max-width: 34ch;
}

.timer-picks {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.chip {
  background: rgba(255, 255, 255, 0.65);
  border: 2px solid rgba(139, 83, 36, 0.22);
  border-radius: 16px;
  padding: 10px 6px;
  font-weight: 800;
  font-size: 13px;
  line-height: 1.2;
  color: var(--ink);
  transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}
.chip span {
  display: block;
  margin-top: 3px;
  font-size: 11px;
  font-weight: 700;
  color: rgba(58, 36, 18, 0.65);
}
.chip.on {
  background: linear-gradient(160deg, #ffe08a, #f0c14a);
  border-color: #d4a017;
  transform: translateY(-1px);
  box-shadow: 0 6px 14px rgba(212, 160, 23, 0.35);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 48px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 16px;
  letter-spacing: 0.02em;
  transition: transform 0.12s ease, filter 0.12s ease;
}
.btn + .btn { margin-top: 10px; }
.btn:active { transform: scale(0.98); }

.btn-primary {
  background: linear-gradient(160deg, #ff8a6a, var(--coral) 45%, var(--coral-deep));
  color: #fffaf3;
  box-shadow: 0 8px 0 #9e2f22, 0 14px 24px rgba(196, 60, 46, 0.35);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.15);
}
.btn-primary:active { box-shadow: 0 3px 0 #9e2f22, 0 8px 16px rgba(196, 60, 46, 0.3); }

.btn-ghost {
  background: rgba(255, 255, 255, 0.55);
  border: 2px solid rgba(139, 83, 36, 0.25);
  color: var(--wood-dark);
}

.btn-tiny {
  width: 40px;
  min-height: 40px;
  border-radius: 14px;
  background: rgba(255, 248, 235, 0.9);
  border: 2px solid rgba(139, 83, 36, 0.22);
  font-weight: 800;
  flex: 0 0 auto;
}
.btn-rotate {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 48px;
  min-height: 48px;
  padding: 0;
  overflow: visible;
}
.btn-rotate img {
  width: 30px;
  height: 30px;
  object-fit: contain;
  display: block;
  pointer-events: none;
  filter: drop-shadow(0 2px 3px rgba(60, 30, 10, 0.28));
}
.btn-rotate .rotate-mark {
  position: absolute;
  right: 1px;
  bottom: 0;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: linear-gradient(160deg, #ffe08a, #f0c14a);
  border: 1.5px solid rgba(139, 83, 36, 0.35);
  color: var(--wood-dark);
  font-size: 12px;
  font-weight: 800;
  line-height: 13px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(60, 30, 10, 0.2);
}
.btn-rotate:active img {
  transform: rotate(20deg);
}

.best-line {
  margin-top: 14px;
  font-size: 14px;
  font-weight: 800;
  color: var(--leaf);
}

.howto-panel { text-align: left; }
.howto-panel h2 { text-align: center; }
.howto-list {
  margin: 10px 0 14px 1.1em;
  display: grid;
  gap: 10px;
  font-weight: 700;
  line-height: 1.35;
  color: rgba(58, 36, 18, 0.9);
}
.score-key {
  text-align: center;
  font-size: 13px;
  font-weight: 800;
  color: var(--teal);
  margin-bottom: 14px;
}

/* ---- Play HUD ---- */
#play {
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  gap: 10px;
  padding-top: calc(10px + var(--safe-top));
}

.hud {
  display: grid;
  grid-template-columns: auto 1fr 1fr 1fr auto;
  gap: 8px;
  align-items: center;
  width: min(920px, 100%);
  margin: 0 auto;
}

.stat {
  background: rgba(255, 248, 235, 0.9);
  border: 2px solid rgba(139, 83, 36, 0.22);
  border-radius: 18px;
  padding: 8px 10px;
  text-align: center;
  box-shadow: 0 6px 16px rgba(60, 30, 10, 0.12);
  backdrop-filter: blur(6px);
}
.stat-label {
  display: block;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--teal);
}
.stat-value {
  display: block;
  font-family: "Baloo 2", sans-serif;
  font-size: 26px;
  font-weight: 800;
  line-height: 1.05;
  color: var(--wood-dark);
}
.coconut-clock.urgent .stat-value { color: var(--coral-deep); animation: pulse 0.8s ease-in-out infinite; }

@keyframes pulse {
  50% { transform: scale(1.06); }
}

.play-main {
  width: min(920px, 100%);
  margin: 0 auto;
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.board-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 0;
}

.current-word {
  min-height: 42px;
  margin-bottom: 8px;
  padding: 6px 18px;
  border-radius: 999px;
  background: rgba(255, 248, 235, 0.92);
  border: 2px solid rgba(139, 83, 36, 0.2);
  font-family: "Baloo 2", sans-serif;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--wood-dark);
  box-shadow: 0 6px 14px rgba(60, 30, 10, 0.12);
}
.current-word.valid { color: var(--leaf); border-color: rgba(47, 122, 62, 0.45); }
.current-word.short { color: rgba(58, 36, 18, 0.45); }

.toast {
  min-height: 22px;
  margin-bottom: 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--coral-deep);
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  opacity: 0;
  transition: opacity 0.2s ease;
}
.toast.show { opacity: 1; }
.toast.good { color: var(--leaf); }

.tray {
  position: relative;
  touch-action: none;
  width: min(440px, calc(100vw - 32px), calc(100dvh - 220px));
  aspect-ratio: 1;
  padding: clamp(10px, 2.4vw, 14px);
  border-radius: 32px;
  background:
    linear-gradient(160deg, rgba(255, 252, 245, 0.42), rgba(255, 240, 220, 0.22) 55%, rgba(255, 248, 235, 0.34));
  border: 1px solid rgba(255, 255, 255, 0.55);
  box-shadow:
    var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.65),
    inset 0 -1px 0 rgba(139, 83, 36, 0.08);
  -webkit-backdrop-filter: blur(18px) saturate(1.25);
  backdrop-filter: blur(18px) saturate(1.25);
  transform-origin: center center;
}
.tray.is-rotating {
  pointer-events: none;
}
.btn-rotate:disabled {
  opacity: 0.65;
  cursor: default;
}

.path-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
  overflow: visible;
}
.path-svg polyline {
  fill: none;
  stroke: rgba(196, 160, 239, 0.95);
  stroke-width: 10;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 2px 2px rgba(70, 30, 110, 0.35));
}

.grid {
  position: relative;
  z-index: 3;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(6px, 1.6vw, 12px);
  width: 100%;
  height: 100%;
}

.tile {
  position: relative;
  border: none;
  border-radius: 0;
  overflow: visible;
  isolation: isolate;
  touch-action: none;
  -webkit-touch-callout: none;
  display: grid;
  place-items: center;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  background: transparent;
  box-shadow: none;
  transition: transform 0.14s ease;
  z-index: 1;
}
/* Coconut body on ::before so drop-shadow follows PNG alpha (no filled disc). */
.tile::before {
  content: "";
  position: absolute;
  inset: -4%;
  background: url("images/coconut-husk.png") center center / contain no-repeat;
  filter: drop-shadow(0 7px 5px rgba(40, 18, 6, 0.38));
  pointer-events: none;
  z-index: 1;
  transition: filter 0.14s ease;
}
.tile-letter {
  position: relative;
  z-index: 2;
  font-family: "Rowdies", "Baloo 2", system-ui, sans-serif;
  font-weight: 700;
  font-size: clamp(34px, 9.2vw, 54px);
  line-height: 1;
  letter-spacing: 0.02em;
  color: #fff8ee;
  transform: translateY(2%);
  text-shadow:
    0 -1px 1px rgba(20, 8, 2, 0.92),
    0 1px 0 rgba(255, 255, 255, 0.28),
    0 2px 3px rgba(0, 0, 0, 0.45);
}
.tile.qu .tile-letter {
  font-size: clamp(26px, 7.2vw, 40px);
  letter-spacing: -0.02em;
}
.tile.on {
  transform: translateY(-3px) scale(1.04);
  z-index: 4;
}
/* Selected uses the dedicated purple husk art (true PNG alpha). */
.tile.on::before {
  background-image: url("images/coconut-husk-purple.png");
  filter: drop-shadow(0 7px 5px rgba(70, 30, 110, 0.35));
}
.tile.tumbling {
  animation: tumble 0.65s cubic-bezier(0.2, 0.8, 0.2, 1) both;
  pointer-events: none;
}

@keyframes tumble {
  0% { transform: translateY(0) rotate(0) scale(1); }
  35% { transform: translateY(-46%) rotate(180deg) scale(1.08); }
  100% { transform: translateY(0) rotate(360deg) scale(1); }
}

.play-actions {
  margin-top: 10px;
  width: min(280px, 100%);
}
.play-actions .btn.hidden { display: none; }

.harvest {
  background: rgba(255, 248, 235, 0.9);
  border: 2px solid rgba(139, 83, 36, 0.22);
  border-radius: 22px;
  padding: 12px 12px 10px;
  box-shadow: 0 8px 18px rgba(60, 30, 10, 0.12);
  backdrop-filter: blur(6px);
  min-height: 0;
  max-height: 160px;
  display: flex;
  flex-direction: column;
}
.harvest h3 {
  font-size: 12px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--teal);
  margin-bottom: 8px;
}
.word-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  overflow: auto;
  min-height: 0;
}
.word-list li {
  background: rgba(255, 255, 255, 0.75);
  border: 1px solid rgba(139, 83, 36, 0.18);
  border-radius: 999px;
  padding: 4px 10px;
  font-weight: 800;
  font-size: 13px;
  color: var(--wood-dark);
}
.word-list li span {
  margin-left: 6px;
  color: var(--coral);
}

/* ---- Results ---- */
.results-panel { max-height: min(92dvh, 860px); overflow: auto; }
.rank {
  font-family: "Baloo 2", sans-serif;
  font-size: 22px;
  font-weight: 800;
  color: var(--coral-deep);
  margin-bottom: 4px;
}
.results-score {
  font-size: 18px;
  font-weight: 800;
  color: var(--wood-dark);
}
.results-meta {
  font-size: 13px;
  font-weight: 700;
  color: rgba(58, 36, 18, 0.7);
  margin: 4px 0 14px;
}
.results-actions {
  display: grid;
  gap: 8px;
  margin-bottom: 14px;
}

.miss-box {
  text-align: left;
  background: rgba(255, 255, 255, 0.45);
  border: 1px solid rgba(139, 83, 36, 0.18);
  border-radius: 16px;
  padding: 10px 12px;
  margin-top: 8px;
}
.miss-box summary {
  cursor: pointer;
  font-weight: 800;
  color: var(--wood-dark);
}
.chip-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.chip-cloud span {
  background: rgba(255, 255, 255, 0.8);
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 12px;
  font-weight: 800;
  color: var(--wood-dark);
}
.chip-cloud.muted span {
  opacity: 0.75;
  background: rgba(240, 230, 210, 0.8);
}

@media (min-width: 820px) {
  .play-main {
    grid-template-columns: minmax(0, 1fr) 240px;
    align-items: stretch;
  }
  .harvest {
    max-height: none;
    height: 100%;
  }
  .tile-letter { font-size: 52px; }
  .tile.qu .tile-letter { font-size: 40px; }
  .tray {
    width: min(440px, calc(100dvh - 160px), calc(100vw - 300px));
  }
}

@media (max-width: 420px) {
  .splash-card { padding: 22px 16px 18px; }
  .timer-picks { gap: 6px; }
  .chip { font-size: 12px; padding: 8px 4px; }
  .hud { gap: 6px; }
  .stat-value { font-size: 22px; }
}

/* Phone / short landscape: board beside harvest, tray sized by height */
@media (orientation: landscape) and (max-height: 560px) {
  #play {
    padding: calc(4px + var(--safe-top)) 10px calc(4px + var(--safe-bottom));
    gap: 6px;
    overflow: hidden;
  }

  .hud {
    width: 100%;
    gap: 6px;
  }

  .stat {
    padding: 4px 8px;
    border-radius: 14px;
  }

  .stat-label {
    font-size: 9px;
    letter-spacing: 0.1em;
  }

  .stat-value { font-size: 18px; }

  .btn-tiny {
    width: 34px;
    min-height: 34px;
    border-radius: 12px;
  }
  .btn-rotate {
    width: 40px;
    min-height: 40px;
  }
  .btn-rotate img {
    width: 24px;
    height: 24px;
  }
  .btn-rotate .rotate-mark {
    width: 14px;
    height: 14px;
    font-size: 10px;
    line-height: 11px;
  }

  .play-main {
    width: 100%;
    grid-template-columns: minmax(0, 1fr) minmax(120px, 28vw);
    gap: 8px;
    align-items: stretch;
  }

  .board-column {
    justify-content: center;
    min-height: 0;
  }

  .current-word {
    min-height: 28px;
    font-size: clamp(18px, 4.8vh, 24px);
    margin-bottom: 2px;
    padding: 2px 12px;
  }

  .toast {
    min-height: 14px;
    margin-bottom: 2px;
    font-size: 12px;
  }

  .tray {
    width: min(380px, calc(100dvh - 78px), calc(100vw - 160px));
    border-radius: 24px;
    padding: clamp(8px, 1.8vh, 14px);
  }

  .grid {
    gap: clamp(5px, 1.4vh, 10px);
  }

  .tile-letter {
    font-size: clamp(28px, 8vmin, 44px);
  }

  .tile.qu .tile-letter {
    font-size: clamp(22px, 6.5vmin, 34px);
  }

  .path-svg polyline { stroke-width: 8; }

  .harvest {
    max-height: none;
    height: 100%;
    min-height: 0;
    padding: 8px;
  }

  .harvest h3 {
    font-size: 10px;
    margin-bottom: 6px;
  }

  .word-list li {
    font-size: 12px;
    padding: 3px 8px;
  }

  .play-actions {
    margin-top: 4px;
    width: min(200px, 100%);
  }

  .play-actions .btn {
    min-height: 40px;
    font-size: 14px;
  }

  .splash-card,
  .panel {
    max-height: calc(100dvh - 16px);
    overflow: auto;
    padding: 14px 16px;
  }

  .cluster {
    width: 72px;
    height: 72px;
    margin: -4px auto 0;
  }

  h1 { font-size: clamp(26px, 7vh, 36px); }
  .tag { margin-bottom: 10px; font-size: 13px; }
  .timer-picks { margin-bottom: 10px; }
}
