:root {
  --bg: #FBF4E6; --paper: #FFF9EC; --ink: #3A2F27; --muted: #8C7B69; --line: #E6D8BC;
  --accent: #E2603B; --accent-d: #B8431F; --accent-ink: #FFFFFF;
  --teal: #2F8F83; --earth: #EADBC0; --earthline: #6B5844; --ball: #E8553A; --force: #4B5FA8; --halo: #FBF4E6;
  --chip: #F4E8CE; --shadow: rgba(90, 60, 30, .16);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: ui-rounded, "SF Pro Rounded", "Nunito", "Avenir Next", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --spring: cubic-bezier(.34, 1.56, .64, 1);
}
[data-theme="dark"] {
  --bg: #231E19; --paper: #2D2620; --ink: #F4E9D6; --muted: #B3A18B; --line: #43392E;
  --accent: #F27D58; --accent-d: #C75A36; --accent-ink: #2A1B12;
  --teal: #62C7B8; --earth: #3A3127; --earthline: #D2BC96; --ball: #FF7C58; --force: #A5B6F5; --halo: #231E19;
  --chip: #352D25; --shadow: rgba(0, 0, 0, .4);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--sans); overscroll-behavior: none; }
#app { height: 100dvh; display: grid; grid-template-rows: auto 1fr; max-width: 1400px; margin: 0 auto; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 3px solid var(--teal); outline-offset: 2px; }

.top { display: grid; grid-template-columns: 48px 1fr auto; align-items: center; padding: max(8px, env(safe-area-inset-top)) 10px 4px; min-height: 52px; }
.title { text-align: center; font-family: var(--serif); font-weight: 700; font-size: 17px; letter-spacing: .01em; opacity: .85; }
.tools { display: flex; gap: 2px; grid-column: 3; }
.quiet { width: 44px; height: 44px; display: grid; place-items: center; border: 0; background: transparent; border-radius: 22px; color: var(--muted); transition: transform .25s var(--spring), color .2s; }
.quiet:active { transform: scale(.88); }
.quiet[aria-pressed="true"] { color: var(--accent); }
#sndOn { display: none; } #snd[aria-pressed="true"] #sndOn { display: inline; } #snd[aria-pressed="true"] #sndOff { display: none; }

main { min-height: 0; position: relative; }
section { height: 100%; }

/* ---------- home ---------- */
.home { overflow-y: auto; display: grid; align-items: start; padding: 0 22px max(22px, env(safe-area-inset-bottom)); }
.homeinner { width: 100%; max-width: 560px; margin: 0 auto; padding-top: clamp(6px, 3vh, 28px); animation: rise .5s var(--spring) both; }
h1 { font-family: var(--serif); font-weight: 800; font-size: clamp(40px, 11.5vw, 60px); line-height: 1.02; margin: 12px 0 22px; letter-spacing: -.01em; }
.field { position: relative; }
.field input { width: 100%; height: 62px; border-radius: 20px; border: 2.5px solid var(--line); background: var(--paper); color: var(--ink); font: 600 19px var(--sans); padding: 0 66px 0 20px; transition: border-color .2s, box-shadow .2s; }
.field input::placeholder { color: var(--muted); opacity: .75; font-weight: 500; }
.field input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 5px color-mix(in srgb, var(--accent) 18%, transparent); }
.mic { position: absolute; right: 8px; top: 8px; width: 46px; height: 46px; border-radius: 23px; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; box-shadow: 0 3px 0 var(--accent-d); transition: transform .2s var(--spring); }
.mic:active { transform: translateY(2px) scale(.94); box-shadow: 0 1px 0 var(--accent-d); }
.mic.on { animation: pulse 1.1s ease-in-out infinite; }
.note { min-height: 24px; margin: 10px 4px 4px; color: var(--muted); font-size: 15px; font-weight: 600; }
.go { margin: 4px 0 10px; width: 100%; animation: rise .35s var(--spring) both; }
.chips { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.chip { min-height: 58px; padding: 10px 14px; border-radius: 18px; border: 2px solid transparent; background: var(--chip); font: 700 16.5px/1.15 var(--sans); text-align: left; transition: transform .25s var(--spring), border-color .2s; animation: rise .5s var(--spring) both; }
.chip:active { transform: scale(.96); }
.chip:hover { border-color: var(--line); }
.chip.draw { background: transparent; border: 2.5px dashed var(--accent); color: var(--accent-d); }
[data-theme="dark"] .chip.draw { color: var(--accent); }
.chip.pop { border-color: var(--accent); }

/* ---------- lab ---------- */
.lab { display: grid; grid-template-rows: auto auto 1fr; min-height: 0; }
.stagewrap { position: relative; height: min(calc((100vw - 20px) * 1.1), 54dvh); max-height: 560px; margin: 0 10px; border-radius: 26px; background: var(--paper); box-shadow: inset 0 0 0 2px var(--line); overflow: hidden; }
#stage { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: manipulation; }
#stage.drawing { touch-action: none; cursor: crosshair; }
.replay { position: absolute; right: 10px; top: 10px; width: 44px; height: 44px; border-radius: 22px; border: 0; background: var(--chip); color: var(--ink); display: grid; place-items: center; animation: pop .35s var(--spring) both; }
.replay:active { transform: scale(.9); }
.readout { min-height: 34px; display: flex; gap: 14px; justify-content: center; align-items: center; padding: 8px 14px 0; font: 700 15px var(--sans); color: var(--muted); font-variant-numeric: tabular-nums; }
.readout b { color: var(--ink); font-weight: 800; }
.readout .res.solo { font-size: 18px; }
.readout .res { color: var(--accent-d); }
[data-theme="dark"] .readout .res.solo { font-size: 18px; }
.readout .res { color: var(--accent); }
.card { padding: 6px 18px max(14px, env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.card .dock { margin-top: auto; }
.prog { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin: 6px 4px 2px; }
.prog i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width .6s var(--spring); }
.say { margin: 4px 2px; font-family: var(--serif); font-weight: 600; font-size: clamp(18px, 5.4vw, 24px); line-height: 1.32; min-height: 4.2em; animation: fade .35s ease both; text-wrap: pretty; }
.say.fin { font-size: clamp(18px, 4.8vw, 22px); }
.ctl { padding: 0 6px; animation: rise .3s var(--spring) both; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 44px; background: transparent; margin: 0; touch-action: none; }
input[type=range]::-webkit-slider-runnable-track { height: 12px; border-radius: 6px; background: var(--line); }
input[type=range]::-moz-range-track { height: 12px; border-radius: 6px; background: var(--line); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 36px; height: 36px; margin-top: -12px; border-radius: 50%; background: var(--accent); border: 4px solid var(--paper); box-shadow: 0 2px 0 var(--accent-d), 0 3px 10px var(--shadow); }
input[type=range]::-moz-range-thumb { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); border: 4px solid var(--paper); box-shadow: 0 2px 0 var(--accent-d); }
.ends { display: flex; justify-content: space-between; color: var(--muted); font-weight: 700; font-size: 13.5px; margin-top: -4px; }
.dock { display: flex; gap: 10px; min-height: 64px; align-items: stretch; }
.big { flex: 1; min-height: 62px; border-radius: 22px; border: 0; background: var(--accent); color: var(--accent-ink); font: 800 20px var(--sans); letter-spacing: .01em; box-shadow: 0 5px 0 var(--accent-d), 0 8px 18px var(--shadow); transition: transform .22s var(--spring), box-shadow .22s var(--spring); animation: pop .4s var(--spring) both; }
.big:active { transform: translateY(4px) scale(.985); box-shadow: 0 1px 0 var(--accent-d), 0 2px 6px var(--shadow); }
.big.alt { background: var(--chip); color: var(--ink); box-shadow: 0 5px 0 var(--line), 0 8px 18px var(--shadow); }
.big.alt.right { background: var(--accent); color: var(--accent-ink); box-shadow: 0 5px 0 var(--accent-d); }
.big.alt.wrong { opacity: .55; }
.big.soft { background: var(--teal); box-shadow: 0 5px 0 color-mix(in srgb, var(--teal) 60%, black), 0 8px 18px var(--shadow); color: #fff; }
.link { flex: 0 0 auto; align-self: center; border: 0; background: transparent; font: 700 15px var(--sans); color: var(--muted); padding: 10px 12px; border-radius: 14px; text-decoration: underline; text-underline-offset: 4px; }
.fin-row { display: flex; gap: 8px; width: 100%; }
.readout .fact { background: var(--chip); border-radius: 14px; padding: 7px 11px; color: var(--ink); font-size: 14px; animation: pop .4s var(--spring) both; }
.readout .fact:nth-child(2) { animation-delay: .1s; } .readout .fact:nth-child(3) { animation-delay: .2s; }
.maybe { margin: 4px 0 8px; }
#stage { transition: opacity .5s ease, filter .5s ease; }
.stagewrap.dim #stage { opacity: .5; filter: saturate(.55); }
.stagewrap.enter #stage { animation: fade .45s ease backwards; }
.big.nudge { animation: nudge 1.5s ease-in-out infinite; }
@keyframes nudge { 0%, 100% { transform: none; } 50% { transform: scale(1.03); } }
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.confetti i { position: absolute; bottom: -20px; width: 12px; height: 12px; border-radius: 50%; opacity: 0; animation: float 2.6s ease-out forwards; }

@keyframes rise { from { opacity: 0; transform: translateY(14px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes pop { from { opacity: 0; transform: scale(.88); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes pulse { 0%, 100% { box-shadow: 0 3px 0 var(--accent-d), 0 0 0 0 color-mix(in srgb, var(--accent) 45%, transparent); } 50% { box-shadow: 0 3px 0 var(--accent-d), 0 0 0 12px transparent; } }
@keyframes float { 0% { opacity: 0; transform: translateY(0) scale(.6); } 15% { opacity: 1; } 100% { opacity: 0; transform: translateY(-110vh) translateX(var(--dx, 0)) scale(1); } }

@media (min-width: 900px) and (min-aspect-ratio: 5/4), (max-height: 520px) and (orientation: landscape) {
  .lab { grid-template-columns: minmax(0, 1.6fr) minmax(340px, 1fr); grid-template-rows: minmax(0, 1fr) auto; column-gap: 8px; padding-bottom: 14px; }
  .stagewrap { grid-row: 1 / span 2; margin: 0 0 0 14px; height: auto; max-height: none; }
  .readout { grid-column: 2; grid-row: 1; align-self: end; justify-content: flex-start; padding: 0 24px; }
  .card { grid-column: 2; grid-row: 2; align-self: start; padding: 6px 24px 0; gap: 12px; }
  .say { font-size: clamp(22px, 2.1vw, 28px); min-height: 7em; }
  .home { place-items: start center; }
}
@media (max-height: 700px) and (max-width: 600px) {
  .stagewrap { height: min(calc((100vw - 20px) * .9), 40dvh); }
  .say { font-size: 17.5px; min-height: 4.6em; }
  .big { min-height: 54px; font-size: 18px; } .dock { min-height: 56px; }
  h1 { font-size: 38px; margin-bottom: 14px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .top { min-height: 40px; padding-top: 2px; } .quiet { width: 38px; height: 38px; }
  .lab { grid-template-columns: minmax(0, 1.5fr) minmax(220px, 1fr); padding-bottom: 6px; }
  .stagewrap { margin-left: 8px; border-radius: 18px; }
  .readout { padding: 0 12px; min-height: 0; font-size: 13px; flex-wrap: wrap; gap: 8px; }
  .card { padding: 2px 12px 0; gap: 6px; } .say { font-size: 15.5px; min-height: 0; line-height: 1.28; margin: 2px; }
  .big { min-height: 46px; font-size: 16px; border-radius: 16px; } .dock { min-height: 48px; }
  .say.fin { font-size: 14.5px; } .readout .fact { font-size: 12px; padding: 4px 8px; } .fin-row { gap: 8px; }
  .prog { margin: 2px; height: 5px; } input[type=range] { height: 34px; }
  h1 { font-size: 30px; margin: 4px 0 10px; } .field input { height: 50px; font-size: 17px; } .mic { top: 5px; width: 40px; height: 40px; }
  .chips { grid-template-columns: repeat(4, 1fr); } .chip { min-height: 46px; font-size: 14px; padding: 6px 10px; }
  .homeinner { padding-top: 0; max-width: 760px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
.readout .fact { white-space: nowrap; }
.readout:has(.fact) { gap: 6px; flex-wrap: nowrap; justify-content: center; }
@media (max-width: 430px) { .readout .fact { font-size: 13px; padding: 6px 9px; } }
@media (max-height: 520px) and (orientation: landscape) { .fin-row .big { white-space: nowrap; padding: 0 14px; } .readout:has(.fact) { flex-wrap: wrap; } }
@media (max-height: 520px) and (orientation: landscape) { .fin-row .link { font-size: 13px; white-space: nowrap; } .fin-row .big { font-size: 14px; padding: 0 10px; } }
@media (max-height: 520px) and (orientation: landscape) { .fin-row { flex-wrap: wrap; justify-content: center; } }
@media (max-height: 520px) and (orientation: landscape) { .readout .fact { font-size: 11px; padding: 3px 7px; } .readout:has(.fact) { gap: 4px; } .fin-row .big { min-height: 40px; } .fin-row { gap: 2px; } .say.fin { font-size: 13.5px; } }
