:root {
  --bg-ink: #241714;
  --bg-plum: #59302d;
  --bg-cream: #f8eedf;
  --panel: rgba(255, 246, 229, 0.78);
  --panel-strong: rgba(255, 246, 229, 0.92);
  --line: rgba(72, 40, 30, 0.12);
  --text-main: #4d3028;
  --text-soft: rgba(77, 48, 40, 0.62);
  --tile-empty: #f4e6d5;
  --tile-filled: #e8d5c1;
  --tile-absent: #c9bbab;
  --tile-present: #c89258;
  --tile-correct: #7bcf7f;
  --tile-invalid: #ef9d9d;
  --accent: #c86b3a;
  --accent-soft: #e7b68c;
  --shadow-lg: 0 28px 60px rgba(58, 25, 14, 0.26);
  --shadow-md: 0 14px 26px rgba(58, 25, 14, 0.18);
  --radius-xl: 30px;
  --radius-lg: 22px;
  --radius-md: 16px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

html,
body {
  margin: 0;
  min-height: 100%;
  touch-action: manipulation;
  overscroll-behavior: none;
  background:
    radial-gradient(circle at 18% 18%, rgba(255, 220, 170, 0.26), transparent 22%),
    radial-gradient(circle at 82% 20%, rgba(255, 153, 120, 0.16), transparent 20%),
    radial-gradient(circle at 50% 88%, rgba(255, 233, 196, 0.18), transparent 24%),
    linear-gradient(180deg, #8f5143 0%, #6e3e35 28%, #472822 66%, #241714 100%);
  color: var(--text-main);
  font-family: "Afacad", "Noto Sans TC", "PingFang TC", sans-serif;
}

body {
  position: relative;
  min-height: 100svh;
  padding:
    max(14px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(14px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
  display: grid;
  place-items: start center;
  overflow-x: hidden;
  overflow-y: auto;
}

.ambient {
  position: fixed;
  inset: auto;
  border-radius: 999px;
  filter: blur(14px);
  opacity: 0.42;
  pointer-events: none;
  animation: drift 16s ease-in-out infinite alternate;
}

.ambient-a {
  width: 180px;
  height: 180px;
  top: 10%;
  left: -36px;
  background: radial-gradient(circle, rgba(255, 214, 164, 0.88), rgba(255, 214, 164, 0));
}

.ambient-b {
  width: 220px;
  height: 220px;
  right: -70px;
  bottom: 12%;
  background: radial-gradient(circle, rgba(248, 173, 117, 0.82), rgba(248, 173, 117, 0));
  animation-duration: 19s;
}

.fireworks {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.app-shell {
  position: relative;
  z-index: 1;
  width: min(94vw, 420px);
  height: min(860px, calc(100svh - 28px));
  min-height: min(860px, calc(100svh - 28px));
  max-height: calc(100svh - 28px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  border-radius: 34px;
  background:
    linear-gradient(180deg, rgba(255, 247, 234, 0.78), rgba(255, 241, 219, 0.88)),
    linear-gradient(135deg, rgba(255, 255, 255, 0.24), transparent 42%);
  border: 1px solid rgba(255, 250, 242, 0.46);
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(14px);
}

.app-shell::before {
  content: "";
  position: absolute;
  inset: 10px;
  border-radius: 26px;
  pointer-events: none;
  border: 1px dashed rgba(132, 76, 49, 0.12);
}

.topbar,
.difficulty-tabs,
.board,
.keyboard {
  position: relative;
  z-index: 1;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand-lockup {
  min-width: 0;
}

.game-title {
  font-size: clamp(2rem, 5.4vw, 2.35rem);
  line-height: 0.9;
  font-weight: 800;
  letter-spacing: 0.03em;
}

.game-subtitle {
  margin-top: 4px;
  color: var(--text-soft);
  font-size: 1rem;
  letter-spacing: 0.02em;
}

.icon-button,
.action-button,
.key {
  appearance: none;
  border: 0;
  cursor: pointer;
  font: inherit;
}

.icon-button {
  width: 44px;
  height: 44px;
  border-radius: 999px;
  background:
    linear-gradient(180deg, rgba(255, 251, 244, 0.96), rgba(244, 219, 189, 0.92));
  color: var(--text-main);
  box-shadow:
    0 10px 18px rgba(84, 43, 26, 0.16),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.icon-button:active,
.action-button:active,
.key:active {
  transform: translateY(1px) scale(0.98);
}

.difficulty-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.difficulty-tab {
  padding: 11px 8px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: rgba(255, 248, 236, 0.64);
  color: var(--text-soft);
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  transition: transform 180ms ease, background 180ms ease, color 180ms ease, box-shadow 180ms ease;
}

.difficulty-tab.is-active {
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.85), rgba(255, 235, 206, 0.96)),
    linear-gradient(180deg, rgba(255, 251, 242, 0.92), rgba(249, 223, 192, 0.96));
  color: var(--text-main);
  border-color: rgba(200, 107, 58, 0.16);
  box-shadow: 0 12px 20px rgba(92, 43, 23, 0.12);
  transform: translateY(-1px);
}

.board {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-radius: var(--radius-xl);
  background:
    linear-gradient(180deg, rgba(252, 243, 230, 0.92), rgba(246, 225, 198, 0.88));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.78);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
}

.board::-webkit-scrollbar {
  display: none;
}

.guess-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 68px;
  align-items: center;
  gap: 8px;
}

.guess-row.is-invalid {
  animation: shake 420ms ease;
}

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

.tile {
  aspect-ratio: 1;
  border-radius: 16px;
  background: var(--tile-empty);
  border: 1px solid rgba(87, 51, 38, 0.08);
  box-shadow:
    inset 0 -2px 0 rgba(119, 71, 49, 0.05),
    inset 0 1px 0 rgba(255, 255, 255, 0.78);
  display: grid;
  place-items: center;
  font-size: clamp(1.22rem, 5vw, 1.65rem);
  font-weight: 800;
  text-transform: uppercase;
  transition: transform 180ms ease, background 180ms ease, box-shadow 180ms ease, border-color 180ms ease;
  transform-style: preserve-3d;
}

.tile.is-filled {
  background: var(--tile-filled);
  transform: translateY(-1px);
}

.tile.is-active {
  border-color: rgba(200, 107, 58, 0.42);
  box-shadow:
    inset 0 -2px 0 rgba(119, 71, 49, 0.08),
    0 0 0 3px rgba(200, 107, 58, 0.12);
}

.tile.is-correct {
  background: var(--tile-correct);
  color: #173a18;
}

.tile.is-present {
  background: var(--tile-present);
  color: #fff7ef;
}

.tile.is-absent {
  background: var(--tile-absent);
  color: #fffaf4;
}

.tile.is-invalid {
  background: var(--tile-invalid);
  color: #6d1e1e;
}

.tile.is-animating {
  animation: reveal 520ms cubic-bezier(0.2, 0.85, 0.2, 1) both;
  animation-delay: calc(var(--index) * 72ms);
}

.tile.is-animating-invalid {
  animation: invalid-pop 300ms ease both;
  animation-delay: calc(var(--index) * 42ms);
}

.row-target {
  display: grid;
  place-items: center;
  min-height: 46px;
  border-radius: 16px;
  background: rgba(255, 249, 240, 0.92);
  color: rgba(77, 48, 40, 0.72);
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.82);
}

.keyboard {
  flex-shrink: 0;
  display: grid;
  gap: 8px;
}

.key-row {
  display: grid;
  gap: 8px;
}

.key-row.digits-a,
.key-row.digits-b {
  grid-template-columns: repeat(5, 1fr);
}

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

.key-row.tools {
  grid-template-columns: 1fr;
}

.key {
  min-height: 52px;
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(255, 252, 246, 0.96), rgba(243, 220, 190, 0.98));
  color: var(--text-main);
  box-shadow:
    0 12px 22px rgba(83, 42, 25, 0.12),
    inset 0 1px 0 rgba(255, 255, 255, 0.82);
  font-size: 1.24rem;
  font-weight: 700;
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.key.is-tool {
  min-height: 56px;
  background: linear-gradient(180deg, #e6a775, #ca6f3b);
  color: #fff7f0;
  font-size: 1.08rem;
  letter-spacing: 0.03em;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(max(18px, env(safe-area-inset-bottom)) + 8px);
  transform: translate(-50%, 18px);
  padding: 11px 16px;
  border-radius: 999px;
  background: rgba(61, 34, 26, 0.92);
  color: #fff4eb;
  font-size: 0.98rem;
  letter-spacing: 0.02em;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 9;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(35, 18, 12, 0.52);
  backdrop-filter: blur(6px);
}

.modal-card {
  position: relative;
  z-index: 1;
  width: min(90vw, 390px);
  padding: 22px 18px 18px;
  border-radius: 28px;
  background:
    linear-gradient(180deg, rgba(255, 249, 239, 0.98), rgba(248, 229, 203, 0.98));
  box-shadow: 0 32px 70px rgba(44, 20, 12, 0.3);
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 38px;
  height: 38px;
  border-radius: 999px;
  border: 0;
  background: rgba(255, 255, 255, 0.7);
  color: var(--text-main);
  font-size: 1.5rem;
}

.modal-eyebrow {
  color: var(--accent);
  font-size: 0.92rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 800;
}

.modal-title {
  margin: 8px 0 6px;
  font-size: 1.9rem;
  line-height: 0.95;
}

.modal-body {
  margin: 0 0 14px;
  color: var(--text-soft);
  font-size: 1.04rem;
}

.share-text {
  margin: 0;
  padding: 16px;
  border-radius: 22px;
  background: rgba(255, 252, 246, 0.82);
  color: var(--text-main);
  font-family: "Afacad", "Noto Sans TC", monospace;
  font-size: 1rem;
  line-height: 1.35;
  white-space: pre-wrap;
}

.modal-actions {
  margin-top: 14px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.action-button {
  min-height: 50px;
  border-radius: 18px;
  font-size: 1.04rem;
  font-weight: 700;
  letter-spacing: 0.03em;
}

.action-button.primary {
  background: linear-gradient(180deg, #e4a66e, #c76c38);
  color: #fff8f1;
}

.action-button.secondary {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.84), rgba(244, 226, 201, 0.92));
  color: var(--text-main);
}

@keyframes drift {
  0% {
    transform: translate3d(0, 0, 0) scale(1);
  }
  100% {
    transform: translate3d(18px, -20px, 0) scale(1.08);
  }
}

@keyframes reveal {
  0% {
    transform: rotateX(0deg) translateY(0);
  }
  45% {
    transform: rotateX(88deg) translateY(-2px);
  }
  100% {
    transform: rotateX(0deg) translateY(-1px);
  }
}

@keyframes invalid-pop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.06);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-7px);
  }
  40% {
    transform: translateX(7px);
  }
  60% {
    transform: translateX(-4px);
  }
  80% {
    transform: translateX(4px);
  }
}

@media (max-height: 760px) {
  body {
    padding:
      max(10px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }

  .app-shell {
    gap: 10px;
    padding: 12px;
  }

  .board {
    gap: 6px;
    padding: 10px;
  }

  .row-cells,
  .guess-row,
  .keyboard,
  .key-row {
    gap: 6px;
  }

  .row-target {
    min-height: 42px;
    font-size: 1rem;
  }

  .key {
    min-height: 46px;
  }

  .key.is-tool {
    min-height: 50px;
  }
}

@media (max-height: 680px) {
  .game-title {
    font-size: clamp(1.7rem, 4.8vw, 2rem);
  }

  .game-subtitle {
    font-size: 0.9rem;
  }

  .difficulty-tab {
    padding: 9px 8px;
    font-size: 0.95rem;
  }

  .row-target {
    min-height: 38px;
    font-size: 0.92rem;
  }

  .key {
    min-height: 42px;
    border-radius: 15px;
    font-size: 1.08rem;
  }

  .key.is-tool {
    min-height: 46px;
    font-size: 1rem;
  }
}

@media (max-width: 360px) {
  .app-shell {
    padding: 12px;
  }

  .row-target {
    font-size: 1.02rem;
  }
}
