/* Puppet lab. Phone-first: a big face at the top, a bottom sheet of controls that never covers it,
   and the "how it's made" section below. From 900px up the sheet becomes a fixed right-hand panel.
   Strict CSP: no inline styles anywhere — everything dynamic comes through custom properties that
   lab.js sets with style.setProperty. */

:root {
  --linen: #FBF8F2;
  --card: #FFFDF9;
  --ink: #332E29;
  --muted: #756B61;          /* 4.9:1 on linen — every use of it is normal-size text */
  --line: #E4DCCD;
  --sage: #5E6C51;           /* 5.3:1 as text, 5.6:1 behind the white on a selected pill */
  --sage-soft: #EDF0E6;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Fraunces", Georgia, "Times New Roman", serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --stage-w: min(100vw, 520px);
  --sheet-max: 60vh;              /* lab.js narrows this to whatever is left below the stage */
  --sheet-h: min(30vh, var(--sheet-max));
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--linen);
  color: var(--ink);
  font: 400 16px/1.5 var(--sans);
  overflow-x: hidden;
}
h1, h2, h3, .picker-title { font-family: var(--serif); font-weight: 600; letter-spacing: -0.01em; }
a { color: var(--sage); }
[hidden] { display: none !important; }

/* ---- top ---------------------------------------------------------------- */
.top {
  display: flex; align-items: flex-end; gap: 12px;
  padding: 14px 16px 10px;
  max-width: var(--stage-w); margin: 0 auto;
}
.top-text { flex: 1 1 auto; min-width: 0; }
#name { margin: 0; font-size: clamp(26px, 8vw, 34px); line-height: 1.1; }
.hint { margin: 3px 0 0; color: var(--muted); font-size: 13px; }
.share {
  flex: 0 0 auto; min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 22px;
  background: var(--card); color: var(--ink); font: 500 15px var(--sans); cursor: pointer;
}
.share:active { background: var(--sage-soft); }

/* ---- stage -------------------------------------------------------------- */
/* 11:8 — the atlas cell shape, so the canvas fills the stage exactly. touch-action:none so a drag
   across the face turns the head instead of scrolling the page. */
.stage {
  position: relative;
  width: var(--stage-w); aspect-ratio: 11 / 8;
  margin: 0 auto;
  touch-action: none;
  outline: none;
}
.stage:focus-visible { outline: 3px solid var(--sage); outline-offset: 3px; border-radius: 10px; }
.stage-inner {
  position: absolute; inset: 0;
  transform-style: preserve-3d; will-change: transform;
}
#still, #face {
  position: absolute; inset: 0; width: 100%; height: 100%;
}
#still { object-fit: contain; }
#face { transform-origin: 50% 60%; will-change: transform; }

.progress {
  position: absolute; left: 10%; right: 10%; bottom: 6px;
  text-align: center; pointer-events: none;
}
.progress-track { height: 5px; border-radius: 3px; background: var(--line); overflow: hidden; }
.progress-track > i { display: block; height: 100%; width: var(--p, 0%); background: var(--sage); transition: width .12s linear; }
.progress-label { margin: 5px 0 0; font: 12px var(--mono); color: var(--muted); }
.progress.is-error .progress-track { display: none; }
.progress.is-error .progress-label { color: #A2503C; }

/* A strip under the stage, the stage's own width: the face stays fully visible while the numbers update. */
.debug {
  position: relative; z-index: 5;
  width: var(--stage-w); margin: 8px auto 0; padding: 7px 10px;
  font: 10px/1.45 var(--mono); letter-spacing: .01em;
  color: #fff; background: rgba(28, 25, 22, .86);
  border-radius: 8px;
  white-space: pre; overflow-x: auto; pointer-events: none;
}
.debug:empty { display: none; }   /* nothing to say yet: no empty black pill over the face */

/* ---- sheet -------------------------------------------------------------- */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: min(calc(var(--sheet-h) + var(--safe-b)), var(--sheet-max));   /* the inset must fit inside the cap, not on top of it */
  display: flex; flex-direction: column;
  background: var(--card);
  border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 24px rgba(51, 46, 41, .09);
  padding-bottom: var(--safe-b);
  overscroll-behavior-y: contain;
  transition: height .22s ease;
}
.sheet.is-open { --sheet-h: min(70vh, var(--sheet-max)); }
.sheet-toggle {
  flex: 0 0 auto; height: 44px; width: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 5px;
  border: 0; background: none; color: var(--muted); font: 500 13px var(--sans); cursor: pointer;
}
.grab { display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--line); }

.tabs {
  flex: 0 0 auto;
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  padding: 2px 12px 10px;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto; min-height: 44px; padding: 0 13px;
  border: 1px solid var(--line); border-radius: 22px;
  background: var(--linen); color: var(--muted); font: 500 14px var(--sans); cursor: pointer;
  white-space: nowrap;
}
.tab[aria-current="true"] { background: var(--sage); border-color: var(--sage); color: #fff; }

.sheet-body {
  flex: 1 1 auto; overflow-y: auto; overscroll-behavior-y: contain; -webkit-overflow-scrolling: touch;
  padding: 0 16px 20px;
}
.group {
  border: 0; border-top: 1px solid var(--line);
  margin: 0; padding: 12px 0 16px;
}
.group:first-child { border-top: 0; }
.group legend {
  padding: 0; font: 600 13px var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
}
.row {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  margin: 14px 0 2px; font-size: 15px;
}
.row small { display: block; color: var(--muted); font-size: 12px; font-weight: 400; }
.row output { font: 500 15px var(--mono); color: var(--sage); white-space: nowrap; }

/* big thumbs: 28px, easy on a thumb */
input[type=range] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 34px; margin: 0; background: none; cursor: pointer;
}
input[type=range]::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: var(--line); }
input[type=range]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--line); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 28px; height: 28px; margin-top: -11px;
  border-radius: 50%; background: var(--sage); border: 3px solid var(--card);
  box-shadow: 0 1px 4px rgba(51, 46, 41, .3);
}
input[type=range]::-moz-range-thumb {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--sage); border: 3px solid var(--card); box-shadow: 0 1px 4px rgba(51, 46, 41, .3);
}
input[type=range]:focus-visible { outline: 2px solid var(--sage); outline-offset: 4px; border-radius: 6px; }

.check {
  display: flex; align-items: flex-start; gap: 10px;
  min-height: 44px; padding: 8px 0; font-size: 15px; cursor: pointer;
}
.check input { width: 26px; height: 26px; margin: 0; flex: 0 0 auto; accent-color: var(--sage); }
.check small { display: block; color: var(--muted); font-size: 12px; }

.btn {
  min-height: 44px; width: 100%; margin-top: 12px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--linen); color: var(--ink); font: 500 15px var(--sans); cursor: pointer;
}
.btn.is-on { background: var(--sage); border-color: var(--sage); color: #fff; }
.btn:disabled { opacity: .45; cursor: default; }

/* segmented radio groups */
.seg { position: relative; display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.seg input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.seg label {
  min-height: 44px; display: flex; align-items: center; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--linen); font: 500 15px var(--sans); cursor: pointer;
}
.seg input:checked + label { background: var(--sage); border-color: var(--sage); color: #fff; }
.seg input:focus-visible + label { outline: 2px solid var(--sage); outline-offset: 3px; }
.seg-expr label { flex: 1 1 auto; justify-content: center; }
.seg-cell label { flex: 1 1 0; justify-content: center; }

/* ---- how it's made ------------------------------------------------------ */
.made {
  max-width: 640px; margin: 26px auto 0; padding: 0 16px;
}
.made h2 { font-size: 24px; margin: 0 0 4px; }
.made h3 { font-size: 14px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: 26px 0 10px; font-weight: 600; }
.made p { margin: 0 0 14px; }
.photos {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
}
.photos li, .photos figure { margin: 0; }
.photos img { width: 100%; height: auto; display: block; border-radius: 12px; background: var(--card); border: 1px solid var(--line); }
.photos figcaption, .frontal-fig figcaption { margin-top: 5px; font-size: 12px; color: var(--muted); }
.tag { display: inline-block; margin-left: 5px; padding: 1px 6px; border-radius: 8px; background: var(--sage-soft); color: var(--sage); font-size: 11px; font-weight: 600; }
.frontal-fig { margin: 0; max-width: 200px; }
.frontal-fig img { width: 100%; height: auto; display: block; border-radius: 12px; background: var(--card); border: 1px solid var(--line); }

.strip-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 6px; }
.strip { --strip-w: calc(84px * 1980 / 96); width: var(--strip-w); }
.strip img { width: var(--strip-w); height: auto; display: block; }
.strip-labels { width: var(--strip-w); padding-top: 6px; display: grid; grid-template-columns: repeat(15, 1fr); font: 11px var(--mono); color: var(--muted); text-align: center; }

.foot {
  max-width: 640px; margin: 30px auto 0; padding: 0 16px calc(var(--sheet-h) + 28px);
  color: var(--muted); font-size: 13px; text-align: center;
}

/* ---- picker ------------------------------------------------------------- */
.picker { max-width: 560px; margin: 0 auto; padding: 32px 16px 40px; text-align: center; }
.picker-title { font-size: clamp(30px, 9vw, 40px); margin: 0; }
.picker-sub { color: var(--muted); margin: 8px 0 26px; }
.picker-grid { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.picker-grid a {
  display: block; padding: 12px; border-radius: 18px;
  background: var(--card); border: 1px solid var(--line); text-decoration: none; color: inherit;
}
.picker-grid img { width: 100%; height: auto; display: block; }
.picker-grid strong { display: block; font-family: var(--serif); font-size: 20px; font-weight: 600; margin-top: 6px; }
.picker-grid span { display: block; font: 12px var(--mono); color: var(--muted); word-break: break-all; }
.picker .foot { padding-bottom: 0; }

/* ---- desktop: the sheet becomes a right-hand panel ---------------------- */
@media (min-width: 900px) {
  .lab { padding-right: 380px; }
  .top, .stage { --stage-w: min(calc(100vw - 400px), 520px); }
  .sheet {
    top: 0; left: auto; right: 0; bottom: 0; width: 360px; height: auto;
    border-radius: 0; border-top: 0; border-left: 1px solid var(--line);
    box-shadow: -8px 0 24px rgba(51, 46, 41, .06);
    transition: none;
  }
  .sheet-toggle { display: none; }
  .tabs { padding-top: 14px; flex-wrap: wrap; overflow: visible; }   /* 360px is too narrow for one row */
  .foot { padding-bottom: 28px; }
}

@media (prefers-reduced-motion: reduce) {
  .sheet, .progress-track > i { transition: none; }
}
