/* Chainword: the chain is a rail route; each word is a stop on a split-flap board. */

:root {
  --platform: #EEF0EC;
  --surface: #FFFFFF;
  --ink: #1C2430;
  --muted: #5B6672;
  --line: #C9CFC8;
  --flap: #233140;
  --flap-split: #1A2531;
  --flap-ink: #F4F1E6;
  --flap-empty: #DDE1DC;
  --signal: #F2B705;
  --signal-ink: #1C2430;
  --alert: #C8372D;
  --ok: #2F8F5B;
  --primary: #233140;
  --primary-ink: #F4F1E6;

  /* Tile size fits the width, and the height left over after the header and dock,
     so the whole chain sits above the keyboard on most phones. */
  --tile: max(20px, min(34px, calc((100vw - 96px) / var(--cols, 9)), calc((100dvh - var(--dock-h, 280px) - var(--ad-h) - 130px) / 11.6)));
  --ad-h: 0px;
  --display: "Barlow Condensed", "Arial Narrow", system-ui, sans-serif;
  --body: "Barlow", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --platform: #12171D;
    --surface: #1A2129;
    --ink: #E6E9EC;
    --muted: #8B96A1;
    --line: #2E3843;
    --flap: #24303D;
    --flap-split: #18212B;
    --flap-ink: #F4F1E6;
    --flap-empty: #1E2731;
    --alert: #F06B5F;
    --ok: #5BC48A;
    --primary: #F4F1E6;
    --primary-ink: #12171D;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
html { scroll-padding-bottom: calc(var(--dock-h, 280px) + 8px); }
body {
  min-height: 100dvh;
  background: var(--platform);
  color: var(--ink);
  font: 400 17px/1.45 var(--body);
  display: flex;
  flex-direction: column;
  padding-bottom: calc(var(--ad-h) + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
body.ads-on { --ad-h: 60px; }
/* Short phones: no banner during play (it would squeeze the board).
   Once the game is finished the keyboard is gone and the banner fits. */
@media (max-height: 739px) {
  body.ads-on:not(.finished) { --ad-h: 0px; }
  body.ads-on:not(.finished) .ad-slot { display: none; }
}

button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--signal); outline-offset: 2px; border-radius: 6px; }

/* ---------- top bar ---------- */
.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.wordmark {
  margin: 0;
  font: 700 26px/1 var(--display);
  letter-spacing: 0.01em;
}
.bar-right { display: flex; align-items: center; gap: 2px; }
.wordmark a { color: inherit; text-decoration: none; }
a.icon-btn { color: inherit; }
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; color: inherit; }
a.btn-primary { color: var(--primary-ink); }
.sheet-links { margin-top: 8px; }
.sheet-links .btn { white-space: nowrap; font-size: 16px; padding: 0 10px; min-height: 44px; }
.sheet .btn-wide + .next, .next + .btn-wide { margin-top: 12px; }
.clock {
  font: 600 18px/1 var(--display);
  font-variant-numeric: tabular-nums;
  min-width: 3.2em;
  text-align: right;
  margin-right: 6px;
  color: var(--muted);
}
.clock.running { color: var(--ink); }
.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 0; background: none; border-radius: 10px; cursor: pointer;
}
.icon-btn:hover { background: color-mix(in srgb, var(--ink) 8%, transparent); }
.icon-btn svg {
  width: 24px; height: 24px;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.icon-btn svg .fill { fill: currentColor; stroke: none; }

/* ---------- stage ---------- */
.stage {
  flex: 1;
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
  padding: 10px 16px calc(var(--dock-h, 280px) + 12px); /* room for the dock (body padding covers the ad slot) */
}
.dateline {
  margin: 0 0 12px 34px;
  font: 500 15px/1.2 var(--display);
  letter-spacing: 0.02em;
  color: var(--muted);
}

/* ---------- the route ---------- */
.route { list-style: none; margin: 0; padding: 0; }
.stop {
  position: relative;
  display: flex;
  align-items: center;
  min-height: calc(var(--tile) * 1.3 + 6px);
  padding-left: 34px;
}
/* track line between stops */
.stop::before {
  content: "";
  position: absolute;
  left: 11px;
  top: 0; bottom: 0;
  width: 4px;
  background: var(--line);
}
.stop:first-child::before { top: 50%; }
.stop:last-child::before { bottom: 50%; }
.stop.passed::before, .stop.passed + .stop::before { background: var(--ink); }
.stop:first-child.passed::before { top: 50%; }
/* station dot */
.stop::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 50%;
  width: 18px; height: 18px;
  margin-top: -9px;
  border-radius: 50%;
  background: var(--platform);
  border: 4px solid var(--line);
}
.stop.passed::after { border-color: var(--ink); background: var(--ink); }
.stop.current::after { border-color: var(--signal); background: var(--platform); }

.tiles { display: flex; gap: 3px; flex-wrap: nowrap; }
.tile {
  position: relative;
  width: var(--tile);
  height: calc(var(--tile) * 1.3);
  display: grid;
  place-items: center;
  border-radius: 4px;
  background: var(--flap);
  color: var(--flap-ink);
  font: 600 calc(var(--tile) * 0.78)/1 var(--display);
  text-transform: uppercase;
  overflow: hidden;
}
/* the split across the middle of a flap */
.tile::after {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 50%;
  height: 1px;
  background: var(--flap-split);
}
.tile.trail {
  width: calc(var(--tile) * 2.2);
  height: 4px;
  align-self: center;
  margin-left: 4px;
  border-radius: 2px;
  background: repeating-linear-gradient(90deg, var(--line) 0 8px, transparent 8px 14px);
}
.tile.trail::after { display: none; }
.tile.hinted { box-shadow: inset 0 -3px 0 var(--signal); }
.tile.typed { background: var(--surface); color: var(--ink); box-shadow: inset 0 0 0 2px var(--flap); }
.tile.typed::after { background: transparent; }
.tile.caret { background: transparent; box-shadow: inset 0 -3px 0 var(--signal); }
.stop.invalid .tile.typed { box-shadow: inset 0 0 0 2px var(--alert); color: var(--alert); }
.tile.caret::after { display: none; }
.tile.revealed { background: color-mix(in srgb, var(--flap) 55%, var(--platform)); }

.stop.shake .tiles { animation: shake 0.32s; }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
@media (prefers-reduced-motion: reduce) {
  .stop.shake .tiles { animation: none; }
  .tile.caret { animation: none; }
}
.tile.caret { animation: blink 1.1s steps(1) infinite; }
@keyframes blink { 50% { box-shadow: none; } }

/* ---------- message + controls ---------- */
/* ---------- dock ---------- */
.dock {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(var(--ad-h) + env(safe-area-inset-bottom));
  z-index: 5;
  background: var(--platform);
  border-top: 1px solid var(--line);
}
.ad-slot.preview { color: var(--muted); font-size: 14px; outline: 2px dashed var(--line); outline-offset: -8px; }
.dock-inner {
  max-width: 500px;
  margin: 0 auto;
  padding: 6px 8px 10px;
}
.dock .message, .dock .controls, .dock .done { padding: 0 8px; }
.message {
  min-height: 1.4em;
  margin: 0 0 6px;
  font-weight: 500;
  font-size: 16px;
}
.message.alert { color: var(--alert); }
.message.ok { color: var(--ok); }

.controls, .done {
  display: flex;
  gap: 8px;
}
.btn {
  flex: 1;
  min-height: 46px;
  padding: 0 14px;
  border-radius: 10px;
  border: 2px solid var(--flap);
  background: var(--surface);
  font: 600 18px/1 var(--display);
  letter-spacing: 0.02em;
  cursor: pointer;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-quiet { border-color: transparent; background: none; color: var(--muted); }
.btn-danger { background: var(--alert); border-color: var(--alert); color: #fff; }
.btn-wide { width: 100%; margin-top: 8px; }
.btn-small { min-height: 38px; font-size: 17px; flex: 0 1 auto; padding: 0 18px; }
.controls { justify-content: space-between; margin-bottom: 8px; }

/* ---------- on-screen keyboard ---------- */
.keyboard { display: flex; flex-direction: column; gap: 7px; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1 1 0;
  max-width: 44px;
  height: clamp(44px, 6.4dvh, 56px);
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: var(--flap-empty);
  color: var(--ink);
  font: 600 20px/1 var(--display);
  text-transform: uppercase;
  cursor: pointer;
}
.key:active { background: var(--flap); color: var(--flap-ink); }
.key-wide { flex: 1.6 1 0; max-width: 70px; font-size: 15px; }
.key svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }

/* ---------- sheets ---------- */
.sheet {
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100dvh - 32px);
  padding: 22px 20px 20px;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--ink);
}
.sheet::backdrop { background: rgb(18 23 29 / 0.55); }
.sheet h2 { margin: 0 0 8px; font: 700 28px/1.1 var(--display); }
.sheet p { margin: 0 0 12px; }
.sheet .close { position: absolute; top: 10px; right: 10px; }
.sheet-small { width: min(380px, calc(100vw - 24px)); }
.row { display: flex; gap: 8px; }

.example {
  list-style: none;
  margin: 4px 0 14px;
  padding: 0 0 0 14px;
  border-left: 4px solid var(--ink);
}
.example li { padding: 3px 0; }
.example b { font: 600 22px/1.2 var(--display); text-transform: uppercase; }
.example span { color: var(--muted); margin-left: 8px; }
.rules { margin: 0 0 12px; padding-left: 20px; }
.rules li { margin: 4px 0; }

.result-head { margin: 0 0 14px; }
.result-head .big { font: 700 44px/1 var(--display); font-variant-numeric: tabular-nums; }
.result-head .sub { color: var(--muted); }
.badge {
  display: inline-block;
  margin-top: 8px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--signal);
  color: var(--signal-ink);
  font: 600 16px/1.2 var(--display);
}
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 14px 0;
  text-align: center;
}
.stat-grid b { display: block; font: 700 30px/1 var(--display); font-variant-numeric: tabular-nums; }
.stat-grid span { font-size: 13px; color: var(--muted); line-height: 1.2; display: block; margin-top: 4px; }
.dist h3, .times h3 { margin: 12px 0 6px; font: 600 18px/1.2 var(--display); }
.bar-row { display: grid; grid-template-columns: 4.5em 1fr; align-items: center; gap: 8px; margin: 4px 0; font-size: 14px; }
.bar-fill {
  min-width: 26px;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--flap-empty);
  color: var(--ink);
  font-weight: 600;
  text-align: right;
}
.bar-fill.today { background: var(--signal); color: var(--signal-ink); }
.times p { margin: 0; color: var(--muted); }
.next { margin-top: 14px; color: var(--muted); }
.next b { color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  top: calc(64px + env(safe-area-inset-top));
  transform: translate(-50%, -12px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--flap);
  color: var(--flap-ink);
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- ad slot (off at launch; reserves height when on) ---------- */
.ad-slot {
  position: fixed;
  left: 0; right: 0;
  bottom: env(safe-area-inset-bottom);
  height: var(--ad-h);
  display: grid;
  place-items: center;
  background: var(--platform);
  border-top: 1px solid var(--line);
}
.ad-slot.preview { color: var(--muted); font-size: 14px; outline: 2px dashed var(--line); outline-offset: -8px; }

/* ---------- make your own ---------- */
.make-page .stage { padding-bottom: 32px; }
.make-title { margin: 6px 0 6px; font: 700 30px/1.1 var(--display); }
.make-intro, .made p { margin: 0 0 14px; color: var(--muted); }
.make-route .stop { min-height: 56px; }
.make-field { display: flex; flex-direction: column; width: 100%; padding: 4px 0; }
.make-field input, .name-field input, .link-out {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: 600 22px/1 var(--display);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.make-field input::placeholder { color: var(--muted); opacity: 0.6; letter-spacing: 0.02em; text-transform: none; font-weight: 500; }
.make-field input:focus, .name-field input:focus, .link-out:focus { outline: none; border-color: var(--signal); }
.make-stop.invalid input { border-color: var(--alert); }
.field-msg { min-height: 0; color: var(--alert); font-size: 14px; }
.field-msg:not(:empty) { margin-top: 4px; }
.name-field { display: block; margin: 16px 0 8px 34px; }
.name-field span { display: block; margin-bottom: 6px; font-weight: 500; }
.name-field em { font-style: normal; color: var(--muted); font-weight: 400; }
.name-field input { text-transform: none; letter-spacing: 0; font: 500 18px/1 var(--body); }
#make-form .btn-wide { margin-left: 34px; width: calc(100% - 34px); }
.link-out { font: 500 15px/1 var(--body); text-transform: none; letter-spacing: 0; margin-bottom: 10px; }
.made .row { margin-bottom: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- one-time tip for the make-your-own pencil ---------- */
.bar { position: relative; }
.tip {
  position: absolute;
  top: calc(100% + 10px);
  right: 12px;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 4px;
  max-width: min(300px, calc(100vw - 24px));
  padding: 10px 6px 10px 14px;
  border-radius: 10px;
  background: var(--primary);
  color: var(--primary-ink);
  font-weight: 500;
  font-size: 15px;
  line-height: 1.3;
  box-shadow: 0 6px 20px rgb(18 23 29 / 0.25);
  animation: tip-in 0.25s ease-out;
}
.tip::before {
  content: "";
  position: absolute;
  top: -7px;
  right: var(--arrow-right, 100px);
  width: 14px; height: 14px;
  background: var(--primary);
  transform: rotate(45deg);
  border-radius: 2px;
}
.tip a { color: inherit; text-decoration: none; }
.tip button {
  flex: none;
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border: 0; border-radius: 8px;
  background: none; color: inherit; cursor: pointer;
}
.tip button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
#make-btn.nudge::after {
  content: "";
  position: absolute;
  top: 7px; right: 7px;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--signal);
}
#make-btn { position: relative; }
@keyframes tip-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .tip { animation: none; } }

.fine { margin: 12px 0 0; font-size: 13px; color: var(--muted); text-align: center; }
