:root {
  color: #f6edd5;
  background: #133d36;
  font-family: Georgia, "Noto Serif Thai", serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: #0b3028 url("assets/game/game-background.webp") center / cover fixed no-repeat;
  color: #fff8e7;
}

body.is-loading {
  overflow: hidden;
}

body.is-loading::before {
  content: "";
  position: fixed;
  z-index: 5000;
  inset: 0;
  background: #031a15d9;
}

body.is-loading::after {
  content: "";
  position: fixed;
  z-index: 5001;
  top: 50%;
  left: 50%;
  width: clamp(72px, 18vw, 128px);
  height: clamp(72px, 18vw, 128px);
  background: url("assets/game/loading.gif") center / contain no-repeat;
  transform: translate(-50%, -50%);
  animation: jade-loader-spin 1s linear infinite;
  pointer-events: none;
}

@keyframes jade-loader-spin {
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  body.is-loading::after {
    animation: none;
  }
}

.app {
  width: min(100%, 560px);
  height: 100%;
  min-height: 0;
  margin: auto;
  padding: 8px 6px 12px;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas: "title actions";
  gap: 8px;
  align-items: center;
  flex: none;
  padding: 0 2px 6px;
  border-bottom: 1px solid #d7b66e70;
}

.title-block {
  grid-area: title;
  min-width: 0;
}

.play-title {
  grid-area: title;
  display: block;
  width: min(100%, 440px);
  max-height: 56px;
  object-fit: contain;
  object-position: left center;
}

.eyebrow {
  font: 700 0.7rem ui-sans-serif, system-ui;
  letter-spacing: 0.2em;
  color: #e6bf67;
  margin: 0 0 4px;
}

h1 {
  font-size: clamp(1.45rem, 6vw, 2.2rem);
  margin: 0;
  line-height: 1.15;
  text-shadow: 0 2px 0 #09241f;
}

.stats {
  grid-area: stats;
  display: flex;
  gap: 14px;
  align-items: center;
}

.stat {
  min-width: 52px;
  text-align: right;
  font-family: ui-sans-serif, system-ui;
}

.stat span {
  display: block;
  text-transform: uppercase;
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  color: #d2c3a1;
}

.stat strong {
  font-size: 1.15rem;
  color: #fff3d2;
}

.actions {
  grid-area: actions;
  display: flex;
  align-items: center;
  margin: 0;
}

.icon-btn {
  position: relative;
  flex: 0 0 52px;
  width: 52px;
  height: 52px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: transparent;
  line-height: 1;
  cursor: pointer;
  box-shadow: none;
}

.icon-btn img {
  display: block;
  width: 52px;
  height: 52px;
  object-fit: contain;
}

.icon-btn:active {
  transform: translateY(2px);
}

.icon-btn em {
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 99px;
  background: #264f43;
  color: #fff;
  font: 700 0.62rem / 18px ui-sans-serif, system-ui;
  font-style: normal;
}

.game-shell {
  margin-top: 0;
  flex: 1;
  min-height: 0;
  display: flex;
  background: linear-gradient(135deg, #2b1610, #552b1d);
  padding: 0;
  border: 1px solid #d8af62;
  border-radius: 12px;
  box-shadow: 0 8px 0 #0a241f, 0 18px 36px #061c194f;
}

.board-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex: none;
  padding: 8px 4px;
  font: 700 0.79rem ui-sans-serif, system-ui;
  color: #f4d68e;
}

.board-toolbar p {
  margin: 0;
}

.board-viewport {
  display: grid;
  place-items: center;
  width: 100%;
  min-width: 0;
  min-height: 0;
  padding: 20px;
  overflow: clip;
  border-radius: 5px;
  background: #0b3b2c url("assets/game/board-background.webp") center / cover no-repeat;
}

.board {
  --unit: 0px; /* measured from the viewport content box by ResizeObserver */
  position: relative;
  width: calc(var(--unit) * var(--bw, 8));
  height: calc(var(--unit) * var(--bh, 10));
  margin: 0 auto;
}

.tile {
  width: calc(var(--unit) * 1.05);
  height: calc(var(--unit) * 1.42);
  padding: 0;
  border: 0;
  border-radius: 4px;
  position: absolute;
  left: 0;
  top: 0;
  cursor: pointer;
  transform: translate(calc(var(--x) * var(--unit)), calc(var(--y) * var(--unit)));
  /* Animating drop-shadow filters while several stacked tiles change state
     causes a full-layer repaint and visible flashing on mobile browsers. */
  transition: opacity 0.18s ease-out;
  filter: drop-shadow(2px 3px 0 #825b27) drop-shadow(4px 6px 4px #041d17b3);
}

.tile:focus {
  outline: none;
}

.tile::before,
.tile::after,
.tile .face {
  transition: transform 0.15s;
}

.tile::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 4px;
  background:
    radial-gradient(ellipse at 50% 0%, #ffffff 0%, #fffdf3 36%, #f3ead3 100%);
  border: 2px solid #bb8b32;
  box-shadow:
    inset 0 0 0 2px #fffdf4,
    inset 0 0 0 4px #286a50,
    inset 0 0 0 5px #d7b568;
}

.tile::after {
  content: "";
  position: absolute;
  inset: 7px;
  border: 1px solid #2a7156;
  border-radius: 3px;
  box-shadow: inset 0 0 0 1px #ead48f;
  pointer-events: none;
}

.tile .face {
  position: relative;
  z-index: 1;
  height: 100%;
  display: grid;
  place-content: center;
  line-height: 1;
  color: #1f241c;
  font: calc(var(--unit) * 0.72) / 1 "Noto Sans Symbols 2", "Apple Symbols", sans-serif;
  text-shadow: 0 1px #fff;
  background: none;
  pointer-events: none;
}

.tile .tile-image {
  width: 100%;
  padding: calc(var(--unit) * 0.15);
  object-fit: contain;
  object-position: center;
}

.tile.free:hover,
.tile.free:focus-visible {
  filter: brightness(1.08) drop-shadow(0 0 7px #ffe7a4) drop-shadow(3px 4px 0 #6f4a23);
}

.tile.free:hover::before,
.tile.free:hover::after,
.tile.free:hover .face,
.tile.free:focus-visible::before,
.tile.free:focus-visible::after,
.tile.free:focus-visible .face,
.tile.selected::before,
.tile.selected::after,
.tile.selected .face {
  transform: translateY(-4px);
}

.tile.selected {
  filter: drop-shadow(0 0 10px #ffe17b) drop-shadow(3px 4px 0 #6f4a23);
}

.tile.blocked {
  cursor: not-allowed;
  filter: drop-shadow(3px 4px 0 #6f4a23) drop-shadow(5px 7px 3px #06201b99);
}

/* The base layer is a flat playing surface, not a raised card. Keep only a
   soft contact shadow so a lone tile cannot be mistaken for a stacked one. */
.tile.tier-0,
.tile.tier-0.blocked {
  filter: drop-shadow(0 2px 2px #041d1770);
}

.tile.tier-0.free:hover,
.tile.tier-0.free:focus-visible {
  filter: brightness(1.08) drop-shadow(0 0 7px #ffe7a4) drop-shadow(0 2px 2px #041d1770);
}

.tile.tier-0.selected {
  filter: drop-shadow(0 0 10px #ffe17b) drop-shadow(0 2px 2px #041d1770);
}

.tile.removed {
  opacity: 0;
  pointer-events: none;
}

.tile.hinted {
  animation: glow 0.65s ease-in-out 3;
}

@keyframes glow {
  50% {
    filter: drop-shadow(0 0 13px #fff15e) drop-shadow(3px 4px 0 #6f4a23);
  }
}

.primary {
  border: 1px solid #ffe29a;
  border-radius: 5px;
  padding: 10px 14px;
  background: #d99c39;
  color: #20190d;
  font: 700 0.84rem ui-sans-serif, system-ui;
  cursor: pointer;
  box-shadow: 0 3px 0 #9b7134;
}

.landing {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 12vh 24px;
  text-align: center;
  background: linear-gradient(#06241bc4, #06241baa), url("assets/landing/landing-background.webp") center / cover;
}

.landing-content {
  width: min(100%, 600px);
}

.game-title {
  display: block;
  width: min(100%, 520px);
  height: auto;
  margin: 0 auto 20px;
}

.landing-actions {
  display: grid;
  grid-template-columns: 1fr;
  margin: auto;
  max-width: 340px;
}

.art-button {
  border: 0;
  padding: 0;
  background: none;
  cursor: pointer;
  filter: drop-shadow(0 5px 5px #041511aa);
  transition: filter 0.15s, transform 0.15s;
}

.art-button:hover,
.art-button:focus-visible {
  filter: brightness(1.1) drop-shadow(0 0 9px #ffe39b) drop-shadow(0 5px 5px #041511aa);
}

.art-button:active {
  transform: translateY(3px);
}

.art-button img {
  display: block;
  width: 100%;
  height: auto;
}

.landing h1 {
  font-size: clamp(2rem, 9vw, 3.4rem);
}

.intro {
  color: #ded1b4;
}

.level-picker {
  margin: 0 auto 12px;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  width: 100%;
  max-width: 360px;
  justify-content: center;
  align-content: center;
  gap: 8px;
}

.level-card {
  width: 100%;
  max-width: 64px;
  height: auto;
  aspect-ratio: 1;
  justify-self: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  cursor: pointer;
}

.level-card img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 3px 3px #031811aa);
}

.level-card.locked {
  cursor: not-allowed;
  filter: grayscale(1) brightness(0.55);
}

.advance-card {
  grid-column: 1 / -1;
  width: 172px;
  min-height: 58px;
  margin: 2px auto 0;
  padding: 8px 14px;
  border: 2px solid #f6d373;
  border-radius: 18px;
  background: linear-gradient(135deg, #0d4c42, #052a28 52%, #0f6252);
  box-shadow: inset 0 0 0 2px #123b34, 0 4px 9px #031811aa;
  color: #fff1bc;
  cursor: pointer;
  font-family: Georgia, serif;
  text-shadow: 0 2px 2px #021b17;
}

.advance-card strong,
.advance-card span {
  display: block;
}

.advance-card strong {
  font-size: 1.05rem;
  letter-spacing: 0.08em;
}

.advance-card span {
  margin-top: 2px;
  color: #cfe9c8;
  font-family: system-ui, sans-serif;
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.advance-card.locked {
  cursor: not-allowed;
  filter: grayscale(1) brightness(0.55);
}

.open-match-card {
  grid-column: 1 / -1;
  width: 172px;
  min-height: 52px;
  margin: 0 auto;
  padding: 7px 14px;
  border: 2px solid #9ee3d2;
  border-radius: 18px;
  background: linear-gradient(135deg, #125d67, #08353f 52%, #14615d);
  box-shadow: inset 0 0 0 2px #16474a, 0 4px 9px #031811aa;
  color: #e6ffef;
  cursor: pointer;
  font-family: Georgia, serif;
  text-shadow: 0 2px 2px #021b17;
}

.open-match-card strong,
.open-match-card span {
  display: block;
}

.open-match-card strong {
  font-size: 0.94rem;
  letter-spacing: 0.07em;
}

.open-match-card span {
  margin-top: 2px;
  color: #b8eee0;
  font-family: system-ui, sans-serif;
  font-size: 0.59rem;
  font-weight: 800;
  letter-spacing: 0.11em;
}

.start {
  font-size: 1rem;
  padding: 13px 32px;
}

.level-modal {
  display: none;
  position: fixed;
  z-index: 3000;
  inset: 0;
  place-items: center;
  padding: 12px;
  background: #041c18c9;
}

.level-modal.show {
  display: grid;
}

.level-modal-card {
  width: min(100%, 460px);
  padding: 52px 20px 24px;
  border: 0;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background: url("assets/modal/level-modal-frame.webp") center / 100% 100% no-repeat;
  box-shadow: 0 18px 48px #000a;
  text-align: center;
}

.back-button {
  display: block;
  width: min(100%, 240px);
  margin: auto;
}

.modal {
  display: none;
  position: fixed;
  z-index: 3000;
  inset: 0;
  background: #041c18c9;
  place-items: center;
  padding: 20px;
}

.modal.show {
  display: grid;
}

.modal-card {
  width: min(100%, 460px);
  padding: 104px 62px 82px;
  border: 0;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  background: url("assets/modal/level-modal-frame.webp") center / 100% 100% no-repeat;
  box-shadow: 0 18px 48px #000a;
}

.clear-title {
  display: block;
  width: min(100%, 290px);
  height: auto;
  margin: 0 auto 30px;
}

.next-level-button {
  display: block;
  width: min(100%, 340px);
  margin: auto;
}

.confirm-card {
  min-height: 390px;
  padding: 86px 62px 72px;
}

.confirm-title {
  display: block;
  width: min(100%, 310px);
  height: auto;
  margin: 0 auto 24px;
}

.confirm-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

.confirm-action {
  display: block;
  width: min(100%, 310px);
}

@media (max-width: 460px) {
  .stats {
    gap: 10px;
  }

  .stat {
    min-width: 44px;
  }

  .icon-btn {
    flex-basis: 46px;
    width: 46px;
    height: 46px;
  }

  .icon-btn img {
    width: 46px;
    height: 46px;
  }
}

/* The game occupies the visible viewport; neither axis is a scroll surface. */
body.is-playing {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: clip;
  overscroll-behavior: none;
}

.tile { touch-action: manipulation; }
.tile:disabled { opacity: 1; }
.tile.removed:disabled { opacity: 0; }
.tile.free:focus-visible { outline: 2px solid #fff1ac; outline-offset: 2px; }

@media (max-height: 600px) {
  .topbar { padding-bottom: 2px; }
  .play-title { max-height: 44px; }
  .board-toolbar { padding: 4px; font-size: 0.7rem; }
  .icon-btn, .icon-btn img { width: 44px; height: 44px; }
  .icon-btn { flex-basis: 44px; }
  .confirm-card { min-height: 0; padding: 50px 50px 40px; }
  .confirm-title { margin-bottom: 12px; }
}
