:root {
  --bg: #73ad22;
  --leaf-dark: #254018;
  --leaf: #5f9829;
  --leaf-bright: #a8cf2e;
  --soil: #8f5b2b;
  --water: #21b9e7;
  --water-light: #8ee9ff;
  --cream: #f5dfb7;
  --cream-light: #fff1cf;
  --coral: #d96f43;
  --shadow: 0 18px 36px rgba(28, 57, 12, 0.34);
  --font: ui-rounded, "Nunito", "Arial Rounded MT Bold", "Trebuchet MS", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  color: var(--leaf-dark);
  font-family: var(--font);
  letter-spacing: 0;
  touch-action: manipulation;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
}

.game-shell {
  position: relative;
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  isolation: isolate;
}

.game-bg {
  position: fixed;
  inset: 0;
  z-index: -2;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(43, 76, 16, 0.08)),
    url("../assets/garden-background.png") center / cover no-repeat;
}

.game-card {
  width: min(100%, 580px);
  display: grid;
  gap: clamp(8px, 1.3svh, 14px);
  align-content: center;
}

.topbar {
  display: grid;
  gap: 10px;
}

h1 {
  margin: 0;
  color: #fff8d7;
  text-align: center;
  font-size: clamp(2.65rem, 10vw, 5.6rem);
  line-height: 0.92;
  font-weight: 950;
  letter-spacing: 0;
  text-shadow:
    0 4px 0 #335c1a,
    0 8px 18px rgba(24, 54, 12, 0.46);
}

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

.stat {
  min-width: 0;
  border: 3px solid rgba(37, 64, 24, 0.45);
  border-radius: 18px;
  padding: 8px 8px 9px;
  background: linear-gradient(180deg, rgba(106, 153, 43, 0.96), rgba(61, 105, 27, 0.98));
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.22), 0 8px 0 rgba(38, 69, 17, 0.25);
  color: #fffbe1;
  text-align: center;
}

.stat span {
  display: block;
  font-size: clamp(0.66rem, 2.2vw, 0.9rem);
  font-weight: 950;
  line-height: 1;
  text-transform: uppercase;
}

.stat strong {
  display: block;
  margin-top: 2px;
  font-size: clamp(1.4rem, 5.3vw, 2.4rem);
  line-height: 1;
  text-shadow: 0 2px 0 rgba(37, 64, 24, 0.5);
  white-space: nowrap;
}

.score-stat strong {
  font-size: clamp(1.3rem, 5vw, 2.22rem);
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(54px, 72px));
  justify-content: center;
  gap: 10px;
}

.icon-button {
  position: relative;
  width: clamp(54px, 16vw, 72px);
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 3px solid rgba(35, 68, 17, 0.48);
  border-radius: 18px;
  padding: 6px 5px 5px;
  color: #fffbe1;
  background: linear-gradient(180deg, #78aa30, #4d8224);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.26), 0 7px 0 rgba(38, 66, 17, 0.34);
  cursor: pointer;
}

.icon-button.accent {
  background: linear-gradient(180deg, #ed9561, var(--coral));
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.25), 0 7px 0 rgba(114, 58, 31, 0.36);
}

.icon-button:active,
.tray-tile:active,
.board-cell:not(.is-locked):active,
.text-action:active {
  transform: translateY(3px);
}

.icon-button:disabled {
  opacity: 0.5;
}

.icon-button svg {
  width: 44%;
  height: 44%;
  flex: 0 0 auto;
  filter: drop-shadow(0 2px 0 rgba(44, 66, 20, 0.34));
}

.icon-button span {
  max-width: 100%;
  color: currentColor;
  font-size: clamp(0.52rem, 1.65vw, 0.66rem);
  font-weight: 950;
  line-height: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}

.icon-button[data-tooltip]::after {
  position: absolute;
  left: 50%;
  top: calc(100% + 8px);
  z-index: 30;
  max-width: 120px;
  border-radius: 8px;
  padding: 5px 8px 6px;
  background: rgba(37, 64, 24, 0.92);
  color: #fffce7;
  content: attr(data-tooltip);
  font-size: 0.72rem;
  font-weight: 950;
  line-height: 1;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(-2px);
  transition: opacity 140ms ease, transform 140ms ease;
  white-space: nowrap;
}

.icon-button[data-tooltip]:hover::after,
.icon-button[data-tooltip]:focus-visible::after {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.status-line {
  min-height: 1.45em;
  margin: 0;
  border-radius: 999px;
  padding: 7px 13px;
  background: rgba(255, 248, 218, 0.76);
  box-shadow: 0 8px 20px rgba(34, 57, 12, 0.16);
  color: #31541a;
  text-align: center;
  font-size: clamp(0.9rem, 2.8vw, 1.05rem);
  font-weight: 850;
  line-height: 1.2;
}

.board-wrap {
  width: 100%;
  display: grid;
  place-items: center;
}

.board {
  width: min(100%, calc(100svh - 330px), 560px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  gap: clamp(2px, 0.7vw, 5px);
  border: clamp(7px, 2.2vw, 13px) solid #6a9825;
  border-radius: clamp(18px, 4vw, 28px);
  padding: clamp(6px, 1.6vw, 10px);
  background: linear-gradient(180deg, #9fca24, #4f8520);
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.22),
    0 18px 42px rgba(27, 53, 10, 0.38);
}

.board-cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 0;
  border-radius: clamp(8px, 2vw, 14px);
  padding: 0;
  overflow: visible;
  background: transparent;
  cursor: pointer;
  filter: drop-shadow(0 2px 0 rgba(65, 43, 18, 0.3));
}

.board-cell:focus-visible,
.tray-tile:focus-visible,
.icon-button:focus-visible,
.text-action:focus-visible {
  outline: 4px solid rgba(142, 233, 255, 0.95);
  outline-offset: 3px;
}

.board-cell.is-locked {
  cursor: default;
}

.cell-base,
.cell-preview-ground {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

.cell-tile,
.cell-preview-tile {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  user-select: none;
}

.cell-base {
  z-index: 1;
}

.cell-tile {
  z-index: 3;
  transition: filter 180ms ease;
}

.stream-tile {
  display: block;
  border-radius: inherit;
  overflow: hidden;
}

.stream-ground,
.water-arm,
.water-core {
  position: absolute;
  pointer-events: none;
}

.stream-ground {
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.water-arm {
  z-index: 2;
  background: #1dbbea;
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.28),
    inset 0 5px 9px rgba(142, 233, 255, 0.76),
    inset 0 -5px 8px rgba(10, 99, 161, 0.28);
}

.water-arm.n,
.water-arm.s {
  left: 38%;
  width: 24%;
  height: 50%;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.36), rgba(255, 255, 255, 0.02) 22%, rgba(19, 172, 226, 0.2) 50%, rgba(255, 255, 255, 0.42) 78%, rgba(255, 255, 255, 0.1)),
    #1dbbea;
}

.water-arm.e,
.water-arm.w {
  top: 38%;
  width: 50%;
  height: 24%;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0.02) 24%, rgba(19, 172, 226, 0.2) 50%, rgba(255, 255, 255, 0.36) 78%, rgba(255, 255, 255, 0.08)),
    #1dbbea;
}

.water-arm.n {
  top: 0;
  border-radius: 999px 999px 7px 7px;
}

.water-arm.s {
  bottom: 0;
  border-radius: 7px 7px 999px 999px;
}

.water-arm.e {
  right: 0;
  border-radius: 7px 999px 999px 7px;
}

.water-arm.w {
  left: 0;
  border-radius: 999px 7px 7px 999px;
}

.water-core {
  left: 35.5%;
  top: 35.5%;
  z-index: 3;
  width: 29%;
  height: 29%;
  border-radius: 999px;
  background:
    radial-gradient(circle at 38% 34%, rgba(255, 255, 255, 0.76), rgba(142, 233, 255, 0.36) 34%, rgba(29, 187, 234, 0.95) 70%),
    #1dbbea;
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.24),
    0 0 8px rgba(33, 185, 231, 0.28);
}

.cell-preview-ground {
  z-index: 2;
  opacity: 0;
  transform: scale(0.92);
  transition: opacity 140ms ease, transform 180ms ease;
}

.cell-preview-tile {
  z-index: 7;
  opacity: 0.64;
  transform: scale(0.92);
  transform-origin: center;
  filter: drop-shadow(0 0 10px rgba(142, 233, 255, 0.78));
  animation: preview-breathe 1100ms ease-in-out infinite;
}

.cell-preview-tile.will-flow {
  opacity: 0.82;
  filter:
    drop-shadow(0 0 8px rgba(142, 233, 255, 0.95))
    drop-shadow(0 0 18px rgba(33, 185, 231, 0.42));
}

.cell-object {
  position: absolute;
  z-index: 4;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

.stone-object {
  width: 68%;
  height: 68%;
  left: 16%;
  top: 17%;
}

.source-object {
  width: 104%;
  height: 104%;
  left: -2%;
  top: -2%;
  z-index: 5;
  transform: rotate(calc(var(--turn) * 90deg));
  transform-origin: center;
}

.bud-object {
  width: 78%;
  height: 78%;
  left: 11%;
  top: 12%;
  z-index: 6;
  filter: drop-shadow(0 5px 5px rgba(37, 57, 15, 0.28));
}

.bloom-object {
  width: 94%;
  height: 94%;
  left: 3%;
  top: 4%;
  z-index: 6;
  filter: drop-shadow(0 5px 5px rgba(37, 57, 15, 0.28));
}

.dew-object {
  width: 25%;
  height: 36%;
  left: 38%;
  top: 44%;
  z-index: 6;
}

.board-cell::before,
.board-cell::after {
  position: absolute;
  inset: -4px;
  border-radius: inherit;
  content: "";
  opacity: 0;
  pointer-events: none;
  z-index: 8;
}

.board-cell::before {
  background: radial-gradient(circle, rgba(142, 233, 255, 0.34), rgba(142, 233, 255, 0) 66%);
  transform: scale(0.82);
  transition: opacity 140ms ease, transform 180ms ease;
}

.board-cell::after {
  border: 3px solid rgba(142, 233, 255, 0.68);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.16), 0 0 16px rgba(33, 185, 231, 0.38);
  transition: opacity 140ms ease;
}

.board-cell:not(.is-locked):hover::before,
.board-cell:not(.is-locked):focus-visible::before,
.is-preview-target::before,
.preview-new-watered::before {
  opacity: 1;
  transform: scale(1);
}

.board-cell:not(.is-locked):hover::after,
.board-cell:not(.is-locked):focus-visible::after,
.is-preview-target::after {
  opacity: 1;
}

.preview-new-watered .cell-preview-ground {
  opacity: 0.56;
  transform: scale(1);
}

.preview-watered:not(.is-preview-target) .cell-base {
  filter: brightness(1.08) saturate(1.08);
}

.preview-bloom .bud-object {
  animation: bud-hint 900ms ease-in-out infinite;
  filter:
    drop-shadow(0 5px 5px rgba(37, 57, 15, 0.28))
    drop-shadow(0 0 10px rgba(255, 247, 220, 0.85));
}

.is-bud-watered .bud-object,
.is-bud-watered .bloom-object {
  filter:
    drop-shadow(0 5px 5px rgba(37, 57, 15, 0.28))
    drop-shadow(0 0 9px rgba(142, 233, 255, 0.68));
}

.just-bloomed::before {
  background:
    radial-gradient(circle at 50% 42%, rgba(255, 247, 220, 0.82), rgba(255, 247, 220, 0.18) 42%, rgba(142, 233, 255, 0) 72%);
  animation: bloom-ripple 760ms ease-out both;
}

.just-bloomed .bloom-object {
  animation: bloom-pop 620ms cubic-bezier(0.2, 1.45, 0.34, 1) both;
  filter:
    drop-shadow(0 5px 5px rgba(37, 57, 15, 0.28))
    drop-shadow(0 0 14px rgba(255, 247, 220, 0.88));
}

.just-dewed .dew-object {
  animation: dew-collect 560ms ease-out both;
}

.preview-dew .dew-object {
  animation: dew-hint 950ms ease-in-out infinite;
  filter: drop-shadow(0 0 10px rgba(142, 233, 255, 0.95));
}

.is-invalid-preview::after {
  border-color: rgba(217, 111, 67, 0.82);
  box-shadow: inset 0 0 0 3px rgba(255, 241, 207, 0.18), 0 0 14px rgba(217, 111, 67, 0.42);
  opacity: 1;
}

.placement-pulse::before {
  animation: placement-ripple 520ms ease-out both;
}

.placement-pulse .cell-tile {
  animation: tile-pop 360ms cubic-bezier(0.2, 1.35, 0.28, 1) both;
}

.tray {
  width: min(100%, 560px);
  justify-self: center;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(12px, 3.2vw, 22px);
  border: 3px solid rgba(129, 82, 35, 0.24);
  border-radius: 28px;
  padding: clamp(11px, 3.2vw, 20px);
  background: linear-gradient(180deg, rgba(255, 241, 207, 0.98), rgba(238, 204, 151, 0.98));
  box-shadow: var(--shadow), inset 0 3px 0 rgba(255, 255, 255, 0.48);
}

.tray-tile {
  position: relative;
  aspect-ratio: 1;
  min-width: 0;
  border: 4px solid transparent;
  border-radius: 20px;
  background: radial-gradient(circle at 50% 32%, rgba(255, 255, 255, 0.35), rgba(128, 84, 39, 0.11));
  box-shadow: 0 9px 0 rgba(101, 72, 35, 0.2), inset 0 2px 0 rgba(255, 255, 255, 0.32);
  cursor: pointer;
  transition: transform 160ms ease, border-color 160ms ease, box-shadow 160ms ease;
}

.tray-tile.is-selected {
  border-color: var(--water-light);
  box-shadow: 0 9px 0 rgba(43, 99, 111, 0.25), 0 0 0 5px rgba(33, 185, 231, 0.22), inset 0 2px 0 rgba(255, 255, 255, 0.42);
}

.icon-button:hover:not(:disabled),
.tray-tile:hover {
  transform: translateY(-2px);
}

.icon-button:hover:not(:disabled) {
  filter: brightness(1.06) saturate(1.05);
}

.tray-tile:hover {
  box-shadow: 0 11px 0 rgba(101, 72, 35, 0.18), 0 0 0 4px rgba(255, 255, 255, 0.22), inset 0 2px 0 rgba(255, 255, 255, 0.36);
}

.tray-stream {
  position: absolute;
  inset: 6%;
  width: 88%;
  height: 88%;
  pointer-events: none;
}

.rules-panel {
  width: min(100%, 560px);
  justify-self: center;
  border: 3px solid rgba(37, 64, 24, 0.3);
  border-radius: 18px;
  padding: 13px;
  background: rgba(255, 248, 218, 0.86);
  box-shadow: 0 12px 28px rgba(34, 57, 12, 0.18);
}

.rules-panel h2 {
  margin: 0 0 8px;
  color: #31541a;
  font-size: 1rem;
  line-height: 1;
  text-transform: uppercase;
}

.rules-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 10px;
}

.rules-grid p {
  min-width: 0;
  margin: 0;
  display: grid;
  gap: 2px;
  color: #4d6330;
  font-size: clamp(0.72rem, 2.2vw, 0.86rem);
  font-weight: 750;
  line-height: 1.16;
}

.rules-grid strong {
  color: #31541a;
  font-size: 0.76rem;
  line-height: 1;
  text-transform: uppercase;
}

.rules-grid span {
  overflow-wrap: anywhere;
}

.end-panel {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  width: min(calc(100vw - 28px), 540px);
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  transform: translateX(-50%);
  border: 3px solid rgba(37, 64, 24, 0.36);
  border-radius: 24px;
  padding: 12px;
  background: rgba(255, 244, 213, 0.96);
  box-shadow: 0 18px 46px rgba(24, 47, 10, 0.32);
  z-index: 20;
}

.end-panel p,
.end-panel span {
  margin: 0;
  font-weight: 850;
}

.end-panel p {
  font-size: 0.88rem;
  text-transform: uppercase;
  color: #537322;
}

.end-panel strong {
  display: block;
  font-size: 1.9rem;
  line-height: 1;
}

.end-panel span {
  color: #5d6532;
  font-size: 0.9rem;
}

.text-action {
  min-height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 0;
  border-radius: 15px;
  padding: 0 14px;
  background: linear-gradient(180deg, #78aa30, #4d8224);
  color: #fffce7;
  font-weight: 950;
  box-shadow: 0 5px 0 rgba(42, 76, 19, 0.28);
  cursor: pointer;
  white-space: nowrap;
}

.text-action.secondary {
  background: linear-gradient(180deg, #ed9561, var(--coral));
}

.text-action svg {
  width: 18px;
  height: 18px;
}

@keyframes bloom-pop {
  0% {
    transform: scale(0.72);
  }
  70% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes bloom-ripple {
  0% {
    opacity: 0.95;
    transform: scale(0.64);
  }
  100% {
    opacity: 0;
    transform: scale(1.58);
  }
}

@keyframes preview-breathe {
  0%,
  100% {
    transform: scale(0.91);
  }
  50% {
    transform: scale(0.98);
  }
}

@keyframes bud-hint {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
  }
}

@keyframes dew-hint {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-5%) scale(1.08);
  }
}

@keyframes dew-collect {
  0% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(-28%) scale(0.55);
  }
}

@keyframes placement-ripple {
  0% {
    opacity: 0.95;
    transform: scale(0.72);
  }
  100% {
    opacity: 0;
    transform: scale(1.72);
  }
}

@keyframes tile-pop {
  0% {
    transform: scale(0.82);
  }
  76% {
    transform: scale(1.06);
  }
  100% {
    transform: scale(1);
  }
}

@media (min-width: 720px) and (min-height: 760px) {
  .game-card {
    padding: 10px 0;
  }
}

@media (min-width: 820px) and (max-height: 850px) {
  .game-shell {
    place-items: center;
    padding: 16px;
  }

  .game-card {
    width: min(100%, 980px);
    grid-template-columns: minmax(420px, 1.45fr) minmax(286px, 0.8fr);
    grid-template-areas:
      "board topbar"
      "board status"
      "board tray"
      "board rules";
    align-items: center;
    gap: 14px 22px;
  }

  .topbar {
    grid-area: topbar;
  }

  h1 {
    font-size: clamp(2.7rem, 5.2vw, 3.85rem);
  }

  .stats {
    grid-template-columns: repeat(4, 1fr);
  }

  .controls {
    grid-template-columns: repeat(3, minmax(54px, 70px));
  }

  .status-line {
    grid-area: status;
  }

  .board-wrap {
    grid-area: board;
  }

  .board {
    width: min(100%, calc(100svh - 52px), 560px);
  }

  .tray {
    grid-area: tray;
    width: min(100%, 320px);
    grid-template-columns: repeat(3, 1fr);
    padding: 13px;
    gap: 12px;
  }

  .rules-panel {
    grid-area: rules;
    width: min(100%, 320px);
    padding: 12px;
  }

  .rules-grid {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 980px) and (min-height: 851px) {
  .game-card {
    width: min(100%, 980px);
    grid-template-columns: minmax(520px, 1fr) minmax(310px, 0.68fr);
    grid-template-areas:
      "board topbar"
      "board status"
      "board tray"
      "board rules";
    align-items: center;
    gap: 14px 24px;
  }

  .topbar {
    grid-area: topbar;
  }

  h1 {
    font-size: clamp(3rem, 5.4vw, 4.7rem);
  }

  .status-line {
    grid-area: status;
  }

  .board-wrap {
    grid-area: board;
  }

  .board {
    width: min(100%, 620px);
  }

  .tray {
    grid-area: tray;
    width: min(100%, 340px);
    padding: 14px;
    gap: 12px;
  }

  .rules-panel {
    grid-area: rules;
    width: min(100%, 340px);
  }

  .rules-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 440px) {
  .game-shell {
    padding-inline: 8px;
  }

  .stats {
    gap: 6px;
  }

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

  .board {
    width: min(100%, 54svh);
  }

  .end-panel {
    grid-template-columns: 1fr;
  }

  .text-action {
    width: 100%;
  }

  .rules-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-height: 720px) {
  .game-shell {
    align-items: start;
  }

  h1 {
    font-size: clamp(2.25rem, 8vw, 3.6rem);
  }

  .board {
    width: min(100%, calc(100svh - 280px), 500px);
  }

  .tray {
    padding-block: 9px;
  }
}

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