:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --ink: #1b1d21;
  --muted: #6b6f76;
  --line: #e2ded5;
  --tile: #ebe7de;
  --tile-ink: #1b1d21;
  --tile-empty: #e7e2d7;
  --key: #dedad1;
  --key-ink: #1b1d21;
  --accent: #e8772e;          /* Ladder orange */
  --accent-soft: #fde6d3;
  --accent-glow: rgba(232, 119, 46, 0.45);
  --match: #3fa34d;           /* a letter already matching the target */
  --match-soft: #dff1df;
  --bad: #e5484d;
  --primary: #1b1d21;
  --primary-ink: #ffffff;
  --good: #3fa34d;
  --ok: #e8b923;
  --rail: #d7cfbf;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
  --radius: 14px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1b1f24;
    --ink: #eef0f3;
    --muted: #9aa0a8;
    --line: #2b3037;
    --tile: #262b33;
    --tile-ink: #eef0f3;
    --tile-empty: #1d2128;
    --key: #3a404a;
    --key-ink: #eef0f3;
    --accent: #f08a45;
    --accent-soft: #4a2d1a;
    --accent-glow: rgba(240, 138, 69, 0.5);
    --match: #4cb85a;
    --match-soft: #1f3a23;
    --primary: #eef0f3;
    --primary-ink: #121417;
    --rail: #3a3f47;
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* No rubber-band bounce or pull-to-refresh while playing */
  overscroll-behavior: none;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  /* No double-tap zoom anywhere, no text-selection loupe on long-press */
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.modal-card p, .modal-card li { -webkit-user-select: text; user-select: text; }

button { font: inherit; color: inherit; }

/* ---------- Buttons ---------- */
.btn {
  border: 0;
  border-radius: 999px;
  padding: 14px 28px;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  min-width: 180px;
  transition: transform 0.1s ease, opacity 0.2s;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-ghost { background: transparent; border: 1.5px solid var(--ink); }
.icon-btn {
  width: 44px; height: 44px; /* Apple's minimum touch target */
  border: 0; background: transparent; border-radius: 10px;
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 24px; height: 24px; }
@media (hover: hover) { .icon-btn:hover { background: var(--line); } }

/* ---------- Splash ---------- */
.splash {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  text-align: center;
}
.splash-inner { display: flex; flex-direction: column; align-items: center; gap: 14px; max-width: 360px; }
.splash-logo { width: 84px; height: 84px; }
.splash-title { font-family: var(--serif); font-size: 44px; margin: 0; letter-spacing: -0.5px; }
.splash-tag { font-size: 20px; margin: 0 0 12px; line-height: 1.35; }
.splash-meta { color: var(--muted); font-size: 14px; line-height: 1.6; margin-top: 8px; }
.splash-meta span:first-child { font-weight: 700; color: var(--ink); }
.splash-foot { margin: 0; font-size: 12px; }
.splash-foot a { color: var(--muted); }

/* ---------- App layout ----------
 * The app fills exactly one screen: header, ladder (which scrolls if it gets long),
 * the target word, and the keyboard pinned at the bottom.
 */
.app {
  max-width: 560px;
  margin: 0 auto;
  /* Keep clear of the notch / Dynamic Island, home indicator, and landscape side insets */
  padding:
    env(safe-area-inset-top)
    max(10px, env(safe-area-inset-right))
    0
    max(10px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 0;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.brand { display: flex; flex-direction: column; align-items: center; }
.brand-name { font-family: var(--serif); font-weight: 700; font-size: 24px; }
.brand-sub { font-size: 12px; color: var(--muted); }

.hud {
  display: flex; align-items: baseline; justify-content: center; gap: 18px;
  padding: 8px 0 2px;
  font-size: 13px;
  flex: none;
}
.hud-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.hud-value { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.hud-pen { font-size: 11px; font-weight: 700; color: var(--ok); margin-left: 2px; vertical-align: 3px; }
.hud-giveup {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  font-size: 13px; font-weight: 600; text-decoration: underline; text-underline-offset: 3px;
  padding: 6px 4px; margin: -6px -4px;
}

/* ---------- The ladder ---------- */
.board-wrap {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  justify-content: center; /* a short ladder sits in the middle; a long one fills and scrolls */
  padding: 4px 0 0;
}
/* Tile size (--ts): as big as fits 5 letters plus the side labels, capped, smaller on short screens. */
.ladder, .target-row, .tut-ladder {
  --gap: 6px;
  --side: 44px;
  --ts: min(54px, calc((100vw - 20px - 2 * var(--side) - 4 * var(--gap)) / 5));
}
@media (min-height: 800px) { .ladder, .target-row { --ts: min(60px, calc((100vw - 20px - 2 * var(--side) - 4 * var(--gap)) / 5)); --gap: 7px; } }
@media (max-height: 740px) { .ladder, .target-row { --ts: min(46px, calc((100vw - 20px - 2 * var(--side) - 4 * var(--gap)) / 5)); } }
@media (max-height: 620px) { .ladder, .target-row { --ts: min(42px, calc((100vw - 20px - 2 * var(--side) - 4 * var(--gap)) / 5)); --gap: 5px; } }

.ladder {
  flex: 0 1 auto; min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  display: flex; flex-direction: column; align-items: center;
  gap: var(--gap);
  padding: 6px 0 0;
  /* Fade the top edge when the ladder scrolls. */
  mask-image: linear-gradient(to bottom, transparent 0, #000 10px);
}

.rung-row {
  display: grid;
  grid-template-columns: var(--side) auto var(--side);
  align-items: center;
  flex: none;
}
.rung-label { font-size: 11px; font-weight: 700; color: var(--muted); text-align: right; padding-right: 15px; font-variant-numeric: tabular-nums; text-transform: uppercase; }
.rung-row.start .rung-label, .target-row .rung-label { font-size: 9px; letter-spacing: 0.04em; }
.rung-mark { font-size: 14px; padding-left: 12px; }
.tiles { display: flex; gap: var(--gap); position: relative; }
/* The ladder's rails: a short piece beside every rung, joining up into one ladder. */
.ladder .tiles::before, .ladder .tiles::after, .board-wrap .target-row .tiles::before, .board-wrap .target-row .tiles::after {
  content: ""; position: absolute; width: 3px; border-radius: 2px; background: var(--rail);
  top: 0; bottom: calc(-1 * var(--gap));
  left: -8px;
}
.ladder .tiles::after, .board-wrap .target-row .tiles::after { left: auto; right: -8px; }
.ladder .rung-row.start .tiles::before, .ladder .rung-row.start .tiles::after { top: 30%; }
.board-wrap .target-row .tiles::before, .board-wrap .target-row .tiles::after { top: calc(-1 * var(--gap) - 4px); bottom: 30%; }

.tile {
  width: var(--ts); height: var(--ts);
  display: grid; place-items: center;
  border: 0; padding: 0; border-radius: 8px;
  background: var(--tile); color: var(--tile-ink);
  font-family: var(--sans); font-weight: 800;
  font-size: calc(var(--ts) * 0.5);
  line-height: 1;
  position: relative;
  transition: background 0.15s, transform 0.12s, box-shadow 0.15s;
}
.tile.changed { box-shadow: inset 0 -4px 0 var(--accent); }
.tile.match { background: var(--match-soft); color: var(--match); }
.tile.match.changed { box-shadow: inset 0 -4px 0 var(--match); }

/* The word you're working on: bigger presence, tappable tiles. */
.rung-row.current .tile {
  background: var(--surface);
  box-shadow: 0 0 0 2px var(--line), 0 2px 6px rgba(0, 0, 0, 0.06);
  cursor: pointer;
}
.rung-row.current .tile.match { background: var(--match-soft); }
.rung-row.current .tile.selected {
  background: var(--accent); color: #fff;
  box-shadow: 0 0 0 2px var(--accent), 0 6px 14px var(--accent-glow);
  transform: translateY(-4px);
}
.rung-row.current .tile:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.tile.bad { background: var(--bad) !important; color: #fff !important; }
.rung-row.typing .tile.typed { background: var(--surface); box-shadow: 0 0 0 2px var(--accent); }
.rung-row.typing .tile.empty { background: var(--surface); box-shadow: 0 0 0 2px var(--line); }

.rung-row.empty .tile { background: transparent; box-shadow: inset 0 0 0 2px var(--tile-empty); border-radius: 8px; }
.rung-row.empty .tile { border: 0; }

.target-row { justify-content: center; display: grid; justify-self: center; align-self: center; padding: 2px 0 4px; }
.target-row .tile.target { background: var(--ink); color: var(--bg); }
.target-row.reached .tile.target { background: var(--match); color: #fff; }
.target-row.gaveup.reached .tile.target { background: var(--muted); }

.hint-line { text-align: center; color: var(--muted); font-size: 13px; margin: 2px 0 4px; min-height: 1.3em; flex: none; }
@media (max-height: 620px) { .hint-line { font-size: 12px; margin: 0 0 2px; } }

/* Feedback animations */
.rung-row.shake .tiles { animation: shake 0.35s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.rung-row.pulse .tile { animation: pulse 0.6s; }
@keyframes pulse { 40% { box-shadow: 0 0 0 3px var(--accent), 0 0 14px var(--accent-glow); } }
.rung-row.pop .tile.changed { animation: pop 0.3s ease-out; }
@keyframes pop { 40% { transform: scale(1.14); } }
.nudge { animation: nudge 0.25s; }
@keyframes nudge { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

/* Win: rungs light up from the bottom of the ladder to the top word. */
.rung-row.rise .tile { animation: rise 0.5s ease-out both; animation-delay: var(--delay); }
@keyframes rise {
  0% { transform: scale(1); }
  40% { transform: scale(1.1); box-shadow: 0 0 0 3px var(--accent), 0 0 16px var(--accent-glow); }
  100% { transform: scale(1); }
}
.target-row.gaveup.rise .tile { animation-name: none; }

/* ---------- Keyboard ---------- */
.keyboard {
  flex: none;
  display: flex; flex-direction: column; gap: 7px;
  padding: 6px 0 max(8px, env(safe-area-inset-bottom));
  touch-action: manipulation;
}
.kb-row { display: flex; justify-content: center; gap: 5px; }
.key {
  flex: 1 1 0; max-width: 46px; min-width: 0;
  height: 54px;
  border: 0; border-radius: 6px;
  background: var(--key); color: var(--key-ink);
  font-size: 18px; font-weight: 700;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.1s, transform 0.08s;
}
.key.down, .key:active { transform: scale(0.94); background: var(--tile-empty); }
.key-wide { flex: 1.5 1 0; max-width: 70px; font-size: 11px; font-weight: 700; }
.key-wide svg { width: 20px; height: 20px; }
.key-hint svg { color: var(--ok); }
.key-hint.armed { background: var(--ok); color: #1b1d21; }
.key-hint.armed svg { color: #1b1d21; }
.key.same { opacity: 0.35; }
.key:disabled { opacity: 0.35; cursor: default; }
.key.glow { animation: target 1.1s ease-in-out infinite; background: var(--accent-soft); }
@media (max-height: 620px) { .key { height: 46px; } .keyboard { gap: 6px; } }
@media (min-width: 420px) { .kb-row { gap: 6px; } }

/* ---------- Below a finished puzzle ---------- */
.below { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 8px 0 max(16px, env(safe-area-inset-bottom)); flex: none; }
.done-actions { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.site-footer { font-size: 12px; color: var(--muted); }
.site-footer a { color: inherit; }

/* ---------- Modals ---------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overscroll-behavior: contain;
  z-index: 50;
  animation: fade 0.2s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal-card {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%; max-width: 440px;
  max-height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  padding: 28px 24px 24px;
  animation: rise-in 0.25s ease;
}
@keyframes rise-in { from { transform: translateY(16px); opacity: 0; } }
.modal-close {
  position: absolute; top: 8px; right: 10px;
  width: 36px; height: 36px; border: 0; background: transparent;
  font-size: 28px; line-height: 1; cursor: pointer; color: var(--muted);
}
.modal h2 { font-family: var(--serif); margin: 0 0 8px; font-size: 26px; }
.modal h3 { font-size: 15px; margin: 18px 0 6px; }
.modal p { line-height: 1.5; margin: 0 0 8px; }
.lead { font-size: 17px; }
.rules { padding-left: 20px; line-height: 1.55; margin: 8px 0 12px; }
.rules li { margin-bottom: 4px; }
.small-print { font-size: 13px; color: var(--muted); }
.swatch-match { color: var(--match); font-weight: 700; }
.help-example { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 10px 0 4px; font-weight: 800; letter-spacing: 0.06em; font-size: 15px; }
.help-example span { background: var(--tile); border-radius: 6px; padding: 5px 8px; }
.help-example span + span::before { content: "→ "; color: var(--muted); font-weight: 400; }
.help-example b { color: var(--accent); }

.result-kicker { text-transform: uppercase; letter-spacing: 0.1em; font-size: 12px; font-weight: 700; color: var(--accent); margin: 0; }
.result-title { margin-top: 2px; }
.result-line { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 14px 0 10px; text-align: center; }
.result-line > div { display: flex; flex-direction: column; background: var(--bg); border-radius: 12px; padding: 12px; }
.big { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; }
.small { font-size: 12px; color: var(--muted); }
.squares { font-size: 28px; text-align: center; letter-spacing: 4px; margin: 6px 0 10px; }
.result-path { font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.result-path p { margin: 0 0 4px; line-height: 1.45; }
.result-path strong { color: var(--ink); }
.btn-share { display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; background: var(--good); color: #fff; }
.btn-share svg { width: 20px; height: 20px; }
.next { text-align: center; color: var(--muted); font-size: 14px; margin-top: 12px !important; }
.next strong { color: var(--ink); font-variant-numeric: tabular-nums; }
hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }

.stats-heading { font-size: 18px !important; font-family: var(--sans) !important; text-transform: uppercase; letter-spacing: 0.06em; }
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.stat { display: flex; flex-direction: column; }
.stat-num { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-lbl { font-size: 11px; color: var(--muted); line-height: 1.2; }
.dist-heading { text-transform: uppercase; letter-spacing: 0.06em; font-size: 13px !important; }
.dist { display: flex; flex-direction: column; gap: 4px; }
.dist-row { display: grid; grid-template-columns: 76px 1fr; gap: 8px; align-items: center; font-size: 13px; }
.dist-bar { background: var(--muted); color: #fff; font-weight: 700; padding: 2px 8px; text-align: right; border-radius: 4px; min-width: 24px; font-size: 12px; }
.dist-bar.today { background: var(--accent); }
.stat-note { font-size: 12px; color: var(--muted); text-align: center; margin: 10px 0 0 !important; }

.a2hs {
  position: relative;
  display: flex; flex-direction: column; gap: 4px;
  background: var(--bg); border-radius: 12px;
  padding: 12px 40px 12px 14px; margin-top: 14px;
  font-size: 14px; line-height: 1.4;
}
.a2hs svg { width: 16px; height: 16px; vertical-align: -3px; color: var(--accent); }
.a2hs-close {
  position: absolute; top: 4px; right: 4px; width: 36px; height: 36px;
  border: 0; background: transparent; font-size: 22px; color: var(--muted); cursor: pointer;
}

.ad-slot { width: 100%; min-height: 100px; margin-top: 14px; display: flex; justify-content: center; }

.btn-wide { width: 100%; margin-top: 10px; }
.btn-danger { background: var(--bad); color: #fff; }
.modal-small { max-width: 340px; text-align: center; }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions .btn { flex: 1; min-width: 0; padding: 13px 10px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; top: calc(18px + env(safe-area-inset-top));
  transform: translate(-50%, -20px);
  background: var(--primary); color: var(--primary-ink);
  padding: 10px 16px; border-radius: 8px; font-weight: 600; font-size: 14px;
  max-width: calc(100vw - 32px); text-align: center;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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

/* ---------- Difficulty levels ---------- */
.level-picker { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 300px; margin-bottom: 4px; }
.level-btn {
  display: grid; grid-template-columns: 14px 1fr auto; align-items: center; gap: 12px;
  border: 0; border-radius: 999px; padding: 15px 22px;
  background: var(--primary); color: var(--primary-ink);
  font-size: 17px; font-weight: 700; text-align: left; cursor: pointer;
  transition: transform 0.1s ease;
}
.level-btn:active { transform: scale(0.97); }
.level-meta { font-size: 13px; font-weight: 500; opacity: 0.7; }
.level-dot { width: 12px; height: 12px; border-radius: 50%; }
[data-level="easy"] .level-dot { background: #3fa34d; }
[data-level="medium"] .level-dot { background: #e8b923; }
[data-level="hard"] .level-dot { background: #e5484d; }
.level-btn[data-status="solved"] .level-meta::before { content: "✓ "; }

.level-tabs {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  background: var(--tile); border-radius: 12px; padding: 4px; margin-top: 8px;
  flex: none;
}
.level-tab {
  position: relative; border: 0; background: transparent; border-radius: 9px;
  padding: 8px 0; min-height: 38px; font-size: 15px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.level-tab[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }
.level-tab[data-status="solved"]::after,
.level-tab[data-status="gaveup"]::after,
.level-tab[data-status="started"]::after {
  position: absolute; top: 50%; transform: translateY(-50%); margin-left: 6px;
  font-size: 12px;
}
.level-tab[data-status="solved"]::after { content: "✓"; color: var(--good); }
.level-tab[data-status="gaveup"]::after { content: "⚑"; color: var(--muted); }
.level-tab[data-status="started"]::after { content: "•"; color: var(--accent); font-size: 18px; }

/* ---------- Tutorial ---------- */
.tut-card { text-align: center; }
.tut-card h2 { margin-bottom: 6px; }
.tut-text { min-height: 4.5em; font-size: 16px; }
.tut-ladder { --ts: 44px; display: flex; flex-direction: column; align-items: center; gap: 6px; margin: 10px 0; }
.tut-ladder .rung-row.current .tile { background: var(--surface); box-shadow: 0 0 0 2px var(--line); cursor: pointer; }
.tut-ladder .rung-row.current .tile.match { background: var(--match-soft); }
.tut-ladder .rung-row.current .tile.selected { background: var(--accent); color: #fff; transform: translateY(-4px); box-shadow: 0 6px 14px var(--accent-glow); }
.tut-ladder .target-row .tile { background: var(--ink); color: var(--bg); }
.tile.glow { animation: target 1.1s ease-in-out infinite; }
@keyframes target {
  0%, 100% { box-shadow: 0 0 0 2px var(--accent); }
  50% { box-shadow: 0 0 0 5px var(--accent-glow), 0 0 16px var(--accent-glow); }
}
.tut-keys { display: flex; justify-content: center; gap: 6px; margin: 6px 0 4px; }
.tut-keys .key { flex: none; width: 46px; max-width: none; }
