/* LadderWords — mobile-first. All feel-numbers are CSS vars for easy tuning. */
:root {
  --bg: #faf7f0;
  --ink: #23201b;
  --muted: #8a8378;
  --tile: #ffffff;
  --tile-border: #d9d2c5;
  --green: #4a9d6f;      /* letter matches END word in position (own palette) */
  --accent: #bf5f3b;     /* terracotta — accent, rails, goal */
  --key: #ece6da;
  --key-active: #dcd3c2;
  --sheet: #fffdf8;
  --shake-ms: 550ms;
  --flip-ms: 150ms;      /* one half of the two-phase flip */
  --stagger-ms: 100ms;
  --pop-ms: 110ms;
  --bounce-ms: 420ms;
  --sheet-ms: 320ms;
  --tile-size: clamp(42px, 15vw, 58px);  /* one source of truth: caps glyph too */
  --rail: #d9ac93;       /* ladder side-rails (clay) */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1814; --ink: #f0ebe1; --muted: #9c9384;
    --tile: #2a2620; --tile-border: #3d372e;
    --key: #2f2a23; --key-active: #423b31; --sheet: #211d17;
    --rail: #57402f;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  display: flex; flex-direction: column; align-items: center;
  min-height: 100dvh; overflow-x: hidden;
}
header { text-align: center; padding: 14px 0 6px; }
h1 { margin: 0; font-size: 1.5rem; letter-spacing: .14em; font-weight: 800;
     display: flex; align-items: center; justify-content: center; gap: 9px; }
h1 .logo { height: 1.12em; width: auto; }
h1 span { color: var(--accent); }
.sub { color: var(--muted); font-size: .8rem; margin-top: 4px; display: flex; align-items: center; justify-content: center; gap: 8px; }
#par {
  background: var(--accent); color: #fff; font-weight: 800;
  font-size: .74rem; letter-spacing: .08em; text-transform: uppercase;
  padding: 3px 12px; border-radius: 999px;
}

main { display: flex; flex-direction: column; gap: 6px; padding: 8px; flex: 1; width: 100%; align-items: center; position: relative; }
/* Ladder side-rails: span the whole climb, incl. the unclimbed gap to GOAL */
main::before, main::after {
  content: ""; position: absolute; top: 6px; bottom: 6px; width: 7px;
  border-radius: 4px; background: var(--rail);
}
main::before { left: calc(50% - (var(--tile-size) * 5 + 24px) / 2 - 19px); }
main::after  { right: calc(50% - (var(--tile-size) * 5 + 24px) / 2 - 19px); }
.row { display: flex; gap: 6px; justify-content: center; }
.tile {
  width: var(--tile-size); height: var(--tile-size);
  flex: 0 0 var(--tile-size);
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--tile-size) * .46); font-weight: 700; line-height: 1;
  text-transform: uppercase;
  background: var(--tile); border: 2px solid var(--tile-border); border-radius: 8px;
  color: var(--ink);
  transition: transform var(--flip-ms) ease-in;
}
.row.done .tile { border-color: transparent; }
.tile.green { background: var(--green); color: #fff; border-color: var(--green); }
.row.current .tile { border-color: var(--accent); }
.tile.pending { border-style: dashed; color: var(--muted); }

/* GOAL row pinned toward the bottom of the play area */
.goalwrap { margin-top: auto; display: flex; flex-direction: column; align-items: center; gap: 4px; padding-top: 12px; }
.goal-cap { font-size: .62rem; letter-spacing: .2em; color: var(--muted); font-weight: 700; }
.row.target .tile { background: transparent; border: 2px solid var(--accent); color: var(--ink); transition: background .25s, border-color .25s, color .25s, box-shadow .25s; }
/* goal-glow: goal letters your latest rung already matches */
.row.target .tile.lit { background: var(--green); border-color: var(--green); color: #fff; box-shadow: 0 0 12px rgba(74, 157, 111, .4); }

/* Letter-entry pop */
.tile.pop { animation: pop var(--pop-ms) ease; }
@keyframes pop { 50% { transform: scale(1.12); } }

/* Two-phase flip: JS adds .flip-half (fold to 90°), swaps color at the fold,
   then removes it (unfold). The .tile transition drives both halves. */
.tile.flip-half { transform: rotateX(90deg); }

/* Invalid-word shake */
.row.shake { animation: shake var(--shake-ms) ease; }
@keyframes shake { 10%,90%{transform:translateX(-2px)} 30%,70%{transform:translateX(5px)} 50%{transform:translateX(-7px)} }

/* Win bounce (staggered per tile via JS delay) */
.tile.bounce { animation: bounce var(--bounce-ms) ease; }
@keyframes bounce {
  20% { transform: translateY(-42%); }
  46% { transform: translateY(6%); }
  66% { transform: translateY(-16%); }
  84% { transform: translateY(2%); }
}

.msg { min-height: 1.2em; color: var(--accent); font-weight: 600; font-size: .9rem; padding: 2px; }
#controls { display: flex; align-items: center; gap: 14px; padding: 4px 0 8px; }
#controls button { font: inherit; padding: 6px 16px; border-radius: 20px; border: 1px solid var(--tile-border); background: var(--tile); color: var(--ink); }
#controls button:disabled { opacity: .4; }
#steps { color: var(--muted); font-size: .85rem; }

#keyboard { display: flex; flex-direction: column; gap: 6px; padding: 6px 4px calc(12px + env(safe-area-inset-bottom)); width: 100%; max-width: 500px; }
.krow { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1; min-width: 0; height: 48px; border-radius: 6px; border: none;
  background: var(--key); color: var(--ink); font: inherit; font-weight: 600;
  text-transform: uppercase; cursor: pointer; touch-action: manipulation;
}
.key.wide { flex: 1.6; font-size: .78rem; }
.key:active { background: var(--key-active); transform: scale(.92); transition: transform var(--pop-ms); }

/* Help button + how-to-play card */
#help {
  position: fixed; top: 12px; right: 12px; z-index: 5;
  width: 34px; height: 34px; border-radius: 50%;
  border: 1.5px solid var(--tile-border); background: var(--tile);
  color: var(--muted); font: inherit; font-weight: 700; cursor: pointer;
}
.ht { margin: 0; font-size: .92rem; line-height: 1.45; }
.ht b { color: var(--accent); }
.par-chip {
  background: var(--accent); color: #fff; font-weight: 800; font-size: .72rem;
  letter-spacing: .08em; text-transform: uppercase; padding: 2px 10px;
  border-radius: 999px; margin-right: 4px; vertical-align: 1px;
}
.mini-demo { display: flex; flex-direction: column; gap: 6px; align-items: center; }
.mini-line { display: flex; align-items: center; gap: 10px; }
.mini-label { width: 62px; text-align: right; font-size: .68rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.mini-row { display: flex; gap: 4px; }
.mtile {
  width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .95rem; border-radius: 6px;
  background: var(--tile); border: 1.5px solid var(--tile-border); color: var(--ink);
}
.mtile.green { background: var(--green); border-color: var(--green); color: #fff; }
.mtile.changed { outline: 2px solid var(--accent); outline-offset: 1.5px; }
.mtile.target { background: transparent; border: 1.5px solid var(--accent); }
.mtile.target.lit { background: var(--green); border-color: var(--green); color: #fff; }
#howto-start { background: var(--accent); color: #fff; }

/* Post-solve bottom sheet */
.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: flex-end; justify-content: center; z-index: 10; opacity: 1; transition: opacity var(--sheet-ms); }
.sheet.hidden { opacity: 0; pointer-events: none; }
.sheet-card {
  background: var(--sheet); width: 100%; max-width: 500px;
  border-radius: 18px 18px 0 0; padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 12px; text-align: center; position: relative;
  transform: translateY(0); transition: transform var(--sheet-ms) cubic-bezier(.2,.9,.3,1);
  box-shadow: 0 -8px 40px rgba(0,0,0,.18);
}
.sheet.hidden .sheet-card { transform: translateY(100%); }
.close { position: absolute; top: 8px; right: 12px; font-size: 1.5rem; background: none; border: none; color: var(--muted); padding: 4px 8px; cursor: pointer; }
.sheet h2 { margin: 4px 0 0; font-size: 1.6rem; }
#post-line { margin: 0; color: var(--ink); }
.daily-title { margin: 0; color: var(--muted); font-style: italic; font-size: .9rem; min-height: 0; }
.stats { display: flex; justify-content: center; gap: 8px; }
.stat { flex: 1; max-width: 90px; background: var(--tile); border: 1px solid var(--tile-border); border-radius: 10px; padding: 8px 4px; display: flex; flex-direction: column; gap: 2px; }
.stat b { font-size: 1.25rem; }
.stat span { font-size: .65rem; letter-spacing: .06em; color: var(--muted); text-transform: uppercase; }
.optimal { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; align-items: center; }
.chip { background: var(--tile); border: 1px solid var(--tile-border); border-radius: 6px; padding: 4px 8px; font-weight: 700; font-size: .85rem; letter-spacing: .05em; }
.chip.endpoint { border-color: var(--accent); }
.optimal .arrow { color: var(--muted); font-size: .8rem; }
.sheet-card button { font: inherit; font-weight: 700; padding: 12px 28px; border-radius: 24px; border: none; }
#share { background: var(--green); color: #fff; }
button.ghost { background: none; border: 1px solid var(--tile-border); color: var(--muted); font-weight: 600; padding: 8px 16px; }
.hidden { display: none; }
.sheet.hidden { display: flex; }  /* keep sheet in layout so the slide transition runs */

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