:root {
  --bg: #f3ede0;
  --paper: #fbf7ec;
  --paper-edge: #e3d3ae;
  --ink: #2b2620;
  --ink-soft: #8a7f68;
  --line: #d9cfb8;

  --accent: #1f9974;
  --accent-dark: #146b53;
  --mark: #d9822b;
  --error: #c9503f;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh; /* iOS Safari sizes vh against the chrome-hidden viewport, so a plain
                     100% here can be taller than what's actually visible on load */
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.stage {
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: 2.4vh 4vw 2.2vh;
}

/* ---- top bar ---- */

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-shrink: 0;
}

.brand {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.back-link {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 0.8vh;
  color: var(--ink-soft);
  font-size: clamp(10px, 1.4vh, 12px);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-decoration: none;
}
.back-link:hover { color: var(--ink); }
.back-link svg { width: 13px; height: 13px; }

.title {
  margin: 0;
  font-size: clamp(16px, 2.6vh, 21px);
  font-weight: 800;
  letter-spacing: 0.04em;
  color: var(--ink);
}

.title-accent {
  background: linear-gradient(90deg, var(--accent), var(--accent-dark));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.brand-divider {
  display: flex;
  align-items: center;
  gap: 0.4em;
  margin: 0.5em 0 0.35em;
}

.brand-divider span {
  display: block;
  height: 1px;
  width: 1.2em;
  background: var(--line);
}

.brand-divider span.dot {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--line);
}

.subtitle {
  margin: 0;
  font-size: clamp(10px, 1.4vh, 12px);
  letter-spacing: 0.3em;
  color: var(--ink-soft);
}

.top-actions {
  display: flex;
  gap: 2vw;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: transparent;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

.icon-btn:active {
  background: rgba(31, 153, 116, 0.12);
  border-color: var(--accent);
  color: var(--accent-dark);
  transform: scale(0.94);
}

.icon-btn.disabled {
  opacity: 0.35;
  pointer-events: none;
}

/* ---- play area ---- */

.play-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 1.6vh 0 0;
}

.puzzle-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 2vw;
  padding-bottom: 1.6vh;
}

.puzzle-word {
  font-size: clamp(15px, 2.4vh, 19px);
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--ink);
}

#targetWord {
  color: var(--accent-dark);
}

.puzzle-arrow {
  color: var(--ink-soft);
}

.step-pill {
  margin-left: 1vw;
  padding: 0.5vh 2.6vw;
  border-radius: 999px;
  background: rgba(31, 153, 116, 0.1);
  color: var(--accent-dark);
  font-size: clamp(10px, 1.5vh, 12px);
  font-weight: 700;
  letter-spacing: 0.04em;
}

.ladder-wrap {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  mask-image: linear-gradient(to bottom, transparent 0, black 24px, black calc(100% - 6px), transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, black 24px, black calc(100% - 6px), transparent 100%);
}
.ladder-wrap::-webkit-scrollbar { display: none; }

.ladder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4vh;
  padding: 1.4vh 0 1vh;
}

.step-arrow {
  color: var(--ink-soft);
  font-size: 14px;
  line-height: 1;
  opacity: 0.7;
}

.step-card,
.step-row.active-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.8vw;
}

.tile,
.tile-active {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: var(--paper);
  border: 1.5px solid var(--line);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.6);
  font-size: clamp(16px, 2.6vh, 21px);
  font-weight: 800;
  color: var(--ink);
  text-align: center;
}

.tile.changed {
  color: var(--mark);
  border-color: var(--mark);
}

.tile-active {
  padding: 0;
  margin: 0;
  font-family: inherit;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 153, 116, 0.12);
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}
.tile-active:active {
  transform: scale(0.94);
}
.tile-active:focus-visible {
  outline: 2px solid var(--accent-dark);
  outline-offset: 2px;
}
.tile-active.picking {
  border-color: var(--accent-dark);
  box-shadow: 0 0 0 4px rgba(31, 153, 116, 0.22);
  transform: scale(1.06);
}

@keyframes tileBounce {
  0%, 100% { transform: translateY(0); }
  25% { transform: translateY(-7px); }
  45% { transform: translateY(0); }
  62% { transform: translateY(-3px); }
  80% { transform: translateY(0); }
}

.tile-active.hint-bounce {
  animation: tileBounce 1.6s ease-in-out infinite;
}

/* ---- letter picker sheet ---- */

.letter-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(30, 24, 16, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 10;
}
.letter-backdrop.show {
  opacity: 1;
  pointer-events: auto;
}

.letter-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -1vh 3vh rgba(0, 0, 0, 0.25);
  padding: 1.6vh 6vw calc(3vh + env(safe-area-inset-bottom, 0px));
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(.4,0,.2,1);
  z-index: 11;
}
.letter-sheet.show {
  transform: translateY(0);
}

.letter-sheet-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1.8vh;
}

.letter-sheet-title {
  margin: 0;
  font-size: clamp(13px, 1.9vh, 15px);
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  text-transform: uppercase;
}

.letter-close {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: transparent;
  color: var(--ink-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.letter-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1.6vw 1.8vw;
}

.letter-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1.5px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.15s ease, background 0.15s ease;
}
.letter-btn:active {
  transform: scale(0.9);
}
.letter-btn.current {
  background: rgba(31, 153, 116, 0.14);
  border-color: var(--accent);
  color: var(--accent-dark);
}

.hint-msg {
  position: fixed;
  top: 42%;
  left: 50%;
  z-index: 20;
  margin: 0;
  padding: 1.4vh 6vw;
  border-radius: 999px;
  background: var(--error);
  color: #fff;
  font-size: clamp(12px, 1.9vh, 15px);
  font-weight: 700;
  letter-spacing: 0.02em;
  white-space: nowrap;
  box-shadow: 0 1.2vh 3vh rgba(43, 38, 32, 0.25);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.9);
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.hint-msg.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

/* ---- how to play ---- */

.howto-link {
  align-self: center;
  flex-shrink: 0;
  border: none;
  background: none;
  color: var(--accent-dark);
  font-weight: 600;
  font-size: clamp(12px, 1.8vh, 14px);
  letter-spacing: 0.02em;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
  padding: 1vh 4px 0;
}

.seo-info {
  flex-shrink: 0;
  align-self: center;
  max-width: 90vw;
  margin-top: 0.6vh;
  font-size: 9px;
  color: var(--ink-soft);
  text-align: center;
}
.seo-info summary {
  cursor: pointer;
  letter-spacing: 0.06em;
  font-weight: 600;
  list-style: none;
}
.seo-info summary::-webkit-details-marker { display: none; }
.seo-info summary::after { content: " ▾"; }
.seo-info[open] summary::after { content: " ▴"; }
.seo-info p {
  margin: 4px 0 0;
  line-height: 1.5;
  text-align: left;
}

.howto-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(30, 24, 16, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 10;
}

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

.howto-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -1vh 3vh rgba(0, 0, 0, 0.25);
  padding: 1.6vh 6vw calc(4vh + env(safe-area-inset-bottom, 0px));
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(.4,0,.2,1);
  z-index: 11;
}

.howto-sheet.show {
  transform: translateY(0);
}

.howto-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--ink-soft);
  opacity: 0.4;
  margin: 0 auto 2vh;
}

.howto-title {
  margin: 0 0 1.6vh;
  font-size: clamp(14px, 2vh, 16px);
  font-weight: 600;
  letter-spacing: 0.1em;
  color: var(--ink);
}

.howto-list {
  margin: 0;
  padding: 0 0 0 1.1em;
  display: flex;
  flex-direction: column;
  gap: 1.2vh;
  font-size: clamp(13px, 1.9vh, 15px);
  color: var(--ink);
  line-height: 1.4;
}
.howto-list li::marker {
  color: var(--accent);
  font-weight: 700;
}

/* ---- solved overlay ---- */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: max(6vh, env(safe-area-inset-bottom));
  background: linear-gradient(to top, rgba(243, 237, 224, 0.94) 0%, rgba(243, 237, 224, 0.6) 40%, rgba(243, 237, 224, 0) 75%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

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

.overlay-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.1vh;
  max-width: 90vw;
  text-align: center;
}

.overlay-icon {
  color: var(--accent);
  margin-bottom: -0.4vh;
}

.overlay-title-row {
  display: flex;
  align-items: center;
  gap: 2.2vw;
}

.overlay-title {
  margin: 0;
  font-size: clamp(20px, 4vh, 30px);
  font-weight: 800;
  letter-spacing: 0.06em;
  background: linear-gradient(90deg, var(--accent), var(--accent-dark));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.deco {
  font-size: clamp(12px, 2vh, 16px);
  line-height: 1;
}
.deco-1, .deco-2 { color: var(--accent); }

.overlay-sub {
  margin: 0;
  font-size: clamp(14px, 2.1vh, 17px);
  font-weight: 700;
  color: var(--ink);
  letter-spacing: 0.06em;
}

.overlay-steps {
  margin: 0;
  font-size: clamp(12px, 1.8vh, 14px);
  color: var(--ink-soft);
}

.overlay-best {
  margin: 0 0 0.4vh;
  font-size: clamp(12px, 1.8vh, 14px);
  font-weight: 700;
  color: var(--accent-dark);
}

.overlay-actions {
  display: flex;
  gap: 3vw;
  margin-top: 0.6vh;
}

.again-btn,
.share-btn {
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 1.2vh 4vw;
  border-radius: 999px;
  font-size: clamp(12px, 1.8vh, 14px);
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: transform 0.1s ease;
}
.again-btn:active,
.share-btn:active { transform: scale(0.96); }

.again-btn {
  border: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
  box-shadow: 0 0.8vh 1.8vh rgba(31, 153, 116, 0.4);
}

.share-btn {
  border: 1.5px solid var(--accent);
  background: transparent;
  color: var(--accent-dark);
}

.share-note {
  margin: 0;
  font-size: clamp(11px, 1.5vh, 13px);
  color: var(--ink-soft);
  letter-spacing: 0.06em;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.share-note.show {
  opacity: 1;
}

.next-puzzle {
  display: flex;
  align-items: center;
  gap: 0.5em;
  margin: 1vh 0 0;
  padding: 1vh 5vw;
  border-radius: 999px;
  background: rgba(31, 153, 116, 0.12);
  border: 1px solid rgba(31, 153, 116, 0.3);
  font-size: clamp(12px, 1.9vh, 14px);
  font-weight: 700;
  color: var(--accent-dark);
  letter-spacing: 0.02em;
}
.next-puzzle span {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: clamp(13px, 2.1vh, 16px);
  color: var(--accent-dark);
}

@media (prefers-reduced-motion: reduce) {
  .icon-btn, .again-btn, .share-btn, .tile-active, .letter-btn { transition: none; }
}

/* ---- short viewports (phone landscape) ---- */
@media (max-height: 480px) {
  .stage { padding-top: 1.4vh; padding-bottom: 1.2vh; }
  .brand-divider, .subtitle, .seo-info { display: none; }
  .back-link { margin-bottom: 0.3vh; }
  .howto-link { padding-top: 0.4vh; }
}
