/* ----------------------------------------------------------------------------
   Loomi — waiting-room kiosk
   Target: Android 9 WebView, Chrome 70+, 1280x800 landscape, 60fps.
   ------------------------------------------------------------------------- */

:root {
  --bg: #FBEFE0;
  --bg-warm: #F9D9C2;
  --bg-cool: #DDE8F3;
  --bg-mint: #D8ECD8;
  --bg-lilac: #E6D7EE;
  --ink: #3A2A22;
  --accent: #E89979;
  --accent-soft: #F4C7A6;
  --mic: #E45A5A;
  --blush: #FFB4BE;

  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

  /* Timings */
  --t-fast: 180ms;
  --t-med:  280ms;
  --t-slow: 420ms;
  --ease:   cubic-bezier(.4,.0,.2,1);
  --bounce: cubic-bezier(.34,1.56,.64,1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

/* ---------- Ambient background ----------
   Calm, pediatric-friendly: cream base + two huge very soft colored blobs
   drifting on long cycles. Behind the eyes, never draws attention.         */
.ambient {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
  background:
    radial-gradient(1400px 900px at 15% 105%, var(--bg-warm) 0%, transparent 55%),
    radial-gradient(1100px 800px at 90% -10%, var(--bg-lilac) 0%, transparent 60%),
    radial-gradient(900px 700px at 55% 40%, var(--bg-cool) 0%, transparent 70%),
    var(--bg);
}
.ambient__blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(70px);
  opacity: 0.65;
  will-change: transform;
}
.ambient__blob--a {
  width: 560px; height: 560px;
  left: -140px; top: 35%;
  background: radial-gradient(circle, #F5B89A 0%, transparent 70%);
  animation: drift-a 38s ease-in-out infinite alternate;
}
.ambient__blob--b {
  width: 680px; height: 680px;
  right: -180px; top: -24%;
  background: radial-gradient(circle, #C7D8EA 0%, transparent 70%);
  animation: drift-b 46s ease-in-out infinite alternate;
}
.ambient__blob--c {
  width: 420px; height: 420px;
  left: 50%; bottom: -120px;
  background: radial-gradient(circle, #D9C2E8 0%, transparent 70%);
  opacity: 0.55;
  animation: drift-c 52s ease-in-out infinite alternate;
}
.ambient::after {
  content: "";
  position: absolute; inset: 0;
  background: radial-gradient(ellipse at center, transparent 35%, rgba(58,42,34,0.08) 100%);
  pointer-events: none;
}
@keyframes drift-a { to { transform: translate3d(140px, -60px, 0) scale(1.08); } }
@keyframes drift-b { to { transform: translate3d(-120px, 80px, 0) scale(1.05); } }
@keyframes drift-c { to { transform: translate3d(80px, -40px, 0) scale(1.1); } }

/* Very fine paper grain, SVG noise inlined as data URI */
.ambient__grain {
  position: absolute; inset: 0;
  opacity: 0.06;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/></filter><rect width='100%25' height='100%25' filter='url(%23n)' opacity='0.8'/></svg>");
}
body.ambient-off .ambient__blob,
body.ambient-off .ambient__grain { display: none; }

/* ---------- Stage + eyes ---------- */
.stage {
  position: fixed; inset: 0;
  z-index: 1;
  display: grid;
  place-items: center;
  /* Block any touch except where we explicitly re-enable it */
  touch-action: none;
  pointer-events: none;
}
.eyes {
  width: min(100vw, 170vh);     /* keeps eyes large on any tablet */
  height: auto;
  max-height: 92vh;
  will-change: transform;
  /* A very subtle bob — not animation, literally a CSS animation so the
     "head" appears to breathe. Amplitude tiny, period 6s. */
  animation: head-breathe 6.2s ease-in-out infinite;
}
@keyframes head-breathe {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-6px); }
}

/* Lids animate through CSS transform — their geometry is parked above the
   sclera and translateY brings them down for a blink. JS also sets
   custom properties --lid-upper and --lid-lower for state-specific
   resting positions (listening wider, thinking squintier, etc). */
.lid {
  transform: translateY(-230px);
  transform-origin: center;
  transition: transform var(--t-med) var(--ease);
  will-change: transform;
}
.lid--lower {
  transform: translateY(230px);
}
/* The eye group itself receives --pupil-x / --pupil-y via JS for gaze. */
#pupilGroupL, #pupilGroupR {
  transition: transform var(--t-med) var(--ease);
  will-change: transform;
}

/* Pupil scale is modulated for dilation (idle breathing, listening, speaking).
   We apply it to the pupil circle itself via a CSS variable. */
#pupilL, #pupilR {
  transform-box: fill-box;
  transform-origin: center;
  transform: scale(var(--pupil-scale, 1));
  transition: transform var(--t-med) var(--ease);
}

/* Happy arc (laughing): hidden by default, revealed on state */
.happy-arc {
  opacity: 0;
  transition: opacity var(--t-fast) var(--ease);
  pointer-events: none;
}

/* Halo glow — slow pulse around each orb */
.halo {
  transform-box: fill-box;
  transform-origin: center;
  animation: halo-pulse 5.4s ease-in-out infinite;
  transition: opacity var(--t-med) var(--ease);
}
@keyframes halo-pulse {
  0%, 100% { opacity: 0.7; transform: scale(1); }
  50%      { opacity: 1;   transform: scale(1.08); }
}

/* Orb striation rays — slow counter-rotation for life */
.orb-rays {
  transform-box: fill-box;
  transform-origin: center;
  animation: rays-drift 22s linear infinite;
  opacity: 0.9;
}
@keyframes rays-drift {
  to { transform: rotate(360deg); }
}

/* Orbiting sparkle motes */
.motes {
  transform-box: fill-box;
  transform-origin: center;
}
.motes--L { animation: mote-orbit-L 9s linear infinite; }
.motes--R { animation: mote-orbit-R 11s linear infinite; }
@keyframes mote-orbit-L { to { transform: rotate(360deg); } }
@keyframes mote-orbit-R { to { transform: rotate(-360deg); } }
.mote {
  transform-box: fill-box;
  transform-origin: center;
}
.mote--1 { transform: translate(0, -138px); }
.mote--2 { transform: translate(118px, 68px); }
.mote--3 { transform: translate(-118px, 70px); animation: mote-twinkle 2.4s ease-in-out infinite; }
@keyframes mote-twinkle {
  0%, 100% { opacity: 0.3; }
  50%      { opacity: 1; }
}

/* Mouth: hidden by default */
.mouth {
  opacity: 0;
  transition: opacity var(--t-fast) var(--ease);
}
#mouthPath {
  transition: d var(--t-med) var(--ease);
}

/* Sleep Z's */
.zleep {
  opacity: 0;
  transition: opacity var(--t-slow) var(--ease);
}
.zleep__z {
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 44px;
  fill: var(--ink);
  opacity: 0.55;
}
.zleep__z--1 { animation: zfloat 2.6s ease-in-out infinite; }
.zleep__z--2 { animation: zfloat 2.6s ease-in-out -0.85s infinite; }
.zleep__z--3 { animation: zfloat 2.6s ease-in-out -1.7s infinite; font-size: 56px; }
@keyframes zfloat {
  0%   { transform: translate(0, 0) rotate(-6deg); opacity: 0; }
  20%  { opacity: 0.6; }
  80%  { opacity: 0.6; }
  100% { transform: translate(-12px, -60px) rotate(6deg); opacity: 0; }
}

/* ---------- State classes on the <svg> drive the visuals ----------
   Lids use translateY amounts that correspond to the SVG coord system.      */

/* idle — eyes open normally (lids fully stowed) */
.eyes.state-idle .lid--upper { transform: translateY(-240px); }
.eyes.state-idle .lid--lower { transform: translateY(240px); }

/* listening — slightly wider, pupils dilated */
.eyes.state-listening .lid--upper { transform: translateY(-260px); }
.eyes.state-listening .lid--lower { transform: translateY(260px); }
.eyes.state-listening #pupilL,
.eyes.state-listening #pupilR { transform: scale(1.14); }

/* thinking — upper lids drop a touch, pupils narrow slightly */
.eyes.state-thinking .lid--upper { transform: translateY(-170px); }
.eyes.state-thinking .lid--lower { transform: translateY(210px); }
.eyes.state-thinking #pupilL,
.eyes.state-thinking #pupilR { transform: scale(0.92); }

/* speaking — relaxed, near-normal, pupils dilate with TTS energy (JS) */
.eyes.state-speaking .lid--upper { transform: translateY(-230px); }
.eyes.state-speaking .lid--lower { transform: translateY(230px); }

/* laughing — squint-arc + mouth, orb hides behind arc */
.eyes.state-laughing .lid--upper { transform: translateY(-100px); }
.eyes.state-laughing .lid--lower { transform: translateY(130px); }
.eyes.state-laughing .happy-arc  { opacity: 1; }
.eyes.state-laughing #pupilL,
.eyes.state-laughing #pupilR,
.eyes.state-laughing circle[id^="shine"],
.eyes.state-laughing ellipse[id^="shine"] { opacity: 0; transition: opacity var(--t-fast) var(--ease); }
.eyes.state-laughing #scleraL,
.eyes.state-laughing #scleraR,
.eyes.state-laughing .orb-rays,
.eyes.state-laughing .motes,
.eyes.state-laughing .halo { opacity: 0; transition: opacity var(--t-fast) var(--ease); }
.eyes.state-laughing .mouth { opacity: 1; }

/* surprised — wide open, pupils tiny */
.eyes.state-surprised .lid--upper { transform: translateY(-290px); }
.eyes.state-surprised .lid--lower { transform: translateY(290px); }
.eyes.state-surprised #pupilL,
.eyes.state-surprised #pupilR { transform: scale(0.7); }
.eyes.state-surprised .mouth { opacity: 1; }

/* sleeping — closed, halo dims */
.eyes.state-sleeping .lid--upper { transform: translateY(30px); }
.eyes.state-sleeping .lid--lower { transform: translateY(-30px); }
.eyes.state-sleeping .zleep { opacity: 1; }
.eyes.state-sleeping .halo { opacity: 0.35; }

/* "speaking" mouth: small arc line by default. JS sets it to an open ellipse path. */
.eyes.state-speaking .mouth { opacity: 0.85; }

/* Long-blink (applied briefly by JS inside idle/listening) */
.eyes.blink .lid--upper { transform: translateY(30px)  !important; transition-duration: 110ms; }
.eyes.blink .lid--lower { transform: translateY(-30px) !important; transition-duration: 110ms; }

/* Squint (mid-blink, shorter) */
.eyes.squint .lid--upper { transform: translateY(-60px) !important; transition-duration: 160ms; }
.eyes.squint .lid--lower { transform: translateY(70px) !important;  transition-duration: 160ms; }

/* Wink: only the right eye closes */
.eyes.wink #lidUpperR { transform: translateY(30px)  !important; transition-duration: 140ms; }
.eyes.wink #lidLowerR { transform: translateY(-30px) !important; transition-duration: 140ms; }

/* ---------- Mic-dot ---------- */
.mic-dot {
  position: fixed;
  right: 18px; bottom: 18px;
  z-index: 10;
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 14px 8px 10px;
  background: rgba(255,255,255,0.78);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(0,0,0,0.06);
  border-radius: 999px;
  font-size: 13px;
  color: var(--ink);
  letter-spacing: 0.02em;
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
}
.mic-dot.on { display: flex; }
.mic-dot__pulse {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--mic);
  position: relative;
}
.mic-dot__pulse::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%;
  background: var(--mic); opacity: 0.6;
  animation: mic-pulse 1.4s ease-out infinite;
}
@keyframes mic-pulse {
  0%   { transform: scale(1);   opacity: 0.6; }
  100% { transform: scale(2.6); opacity: 0;   }
}

/* ---------- State badge (visible in demo, subtle in prod) ---------- */
.state-badge {
  position: fixed;
  left: 18px; bottom: 18px;
  z-index: 10;
  padding: 6px 12px;
  background: rgba(42,32,24,0.06);
  border-radius: 999px;
  font-size: 12px;
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  letter-spacing: 0.06em;
  color: rgba(42,32,24,0.55);
  text-transform: uppercase;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--t-med) var(--ease);
}
body.debug .state-badge { opacity: 1; }

/* ---------- Long-press hotspot ---------- */
.long-press-zone {
  position: fixed;
  left: 0; top: 0;
  width: 110px; height: 110px;
  z-index: 20;
  pointer-events: auto;
  touch-action: manipulation;
}
.long-press-zone__ring {
  position: absolute;
  left: 14px; top: 14px;
  width: 36px; height: 36px;
  border: 2px solid var(--accent);
  border-radius: 50%;
  opacity: 0;
  transform: scale(0.4);
  transition: transform var(--t-med) var(--ease), opacity var(--t-med) var(--ease);
}
.long-press-zone.active .long-press-zone__ring {
  opacity: 0.9;
  transform: scale(1);
  animation: press-fill 4s linear forwards;
}
@keyframes press-fill {
  0%   { box-shadow: inset 0 0 0 0 var(--accent); }
  100% { box-shadow: inset 0 0 0 18px var(--accent); }
}

/* ---------- Settings panel ---------- */
.settings { position: fixed; inset: 0; z-index: 30; }
.settings[hidden] { display: none; }
.settings__scrim {
  position: absolute; inset: 0;
  background: rgba(42,32,24,0.35);
  backdrop-filter: blur(4px);
  animation: fade-in var(--t-med) var(--ease);
}
.settings__sheet {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: min(520px, 90vw);
  background: #fff;
  box-shadow: -20px 0 60px rgba(0,0,0,0.2);
  display: flex;
  flex-direction: column;
  animation: slide-in var(--t-med) var(--ease);
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-in { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }

.settings__header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px; border-bottom: 1px solid #EFE8DC;
}
.settings__header h2 { margin: 0; font-size: 18px; font-weight: 600; }
.settings__close {
  background: none; border: none; font-size: 28px; line-height: 1;
  cursor: pointer; color: var(--ink); padding: 2px 8px;
}
.settings__body {
  flex: 1; overflow-y: auto; padding: 16px 20px;
  display: flex; flex-direction: column; gap: 18px;
}
.settings__footer {
  padding: 14px 20px; border-top: 1px solid #EFE8DC;
  display: flex; justify-content: flex-end; gap: 10px;
}

.settings__group {
  border: 1px solid #EFE8DC; border-radius: 10px; padding: 14px 14px 6px;
}
.settings__group legend {
  font-weight: 600; font-size: 13px; padding: 0 6px;
  color: var(--ink);
  letter-spacing: 0.02em;
}
.settings__field {
  display: flex; flex-direction: column; gap: 6px;
  margin-bottom: 12px;
}
.settings__field label {
  font-size: 12px; font-weight: 500; color: #6B5C4E; letter-spacing: 0.02em;
}
.settings__field input[type="text"],
.settings__field input[type="password"],
.settings__field input[type="number"],
.settings__field select,
.settings__field textarea {
  width: 100%;
  border: 1px solid #E3DACD;
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
  background: #FBF7EF;
  color: var(--ink);
}
.settings__field textarea {
  min-height: 110px; resize: vertical;
  font-family: ui-monospace, Menlo, monospace; font-size: 12px;
}
.settings__field--check {
  flex-direction: row; align-items: center; gap: 10px;
}
.settings__field--check label { font-weight: 500; color: var(--ink); font-size: 13px; }

.btn {
  border: none; border-radius: 999px; padding: 9px 18px;
  font: inherit; font-size: 13px; font-weight: 600;
  cursor: pointer;
}
.btn--primary { background: var(--ink); color: #fff; }
.btn--primary:hover { background: #1e1710; }
.btn--ghost   { background: transparent; color: var(--ink); }
.btn--ghost:hover   { background: #F1EADC; }
.btn--block { display: block; width: 100%; padding: 11px; margin-top: 8px; }

/* ---------- Demo / Tweaks panel ---------- */
.demo-panel {
  position: fixed; right: 16px; bottom: 60px;
  width: 320px; max-height: 80vh; overflow: auto;
  z-index: 25;
  background: rgba(255,255,255,0.96);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0,0,0,0.06);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.12);
  font-size: 13px;
  animation: slide-up var(--t-med) var(--ease);
}
@keyframes slide-up { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }
.demo-panel[hidden] { display: none; }
.demo-panel__header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px; border-bottom: 1px solid #EFE8DC;
}
.demo-panel__header h3 { margin: 0; font-size: 14px; font-weight: 600; }
.demo-panel__header button {
  background: none; border: none; font-size: 20px; line-height: 1;
  cursor: pointer; color: #6B5C4E;
}
.demo-panel__body { padding: 10px 14px 14px; display: flex; flex-direction: column; gap: 12px; }
.demo-panel__body section { display: flex; flex-direction: column; gap: 8px; }
.demo-panel__body h4 { margin: 0; font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: #9A8A78; }
.demo-panel__hint { margin: 0; font-size: 11px; color: #9A8A78; line-height: 1.4; }

.row {
  display: grid; grid-template-columns: 1fr auto; align-items: center;
  gap: 10px;
}
.row input[type="range"] { grid-column: 1 / 3; width: 100%; }
.row output { font-variant-numeric: tabular-nums; color: #6B5C4E; font-size: 12px; }

.state-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px;
}
.state-grid button {
  border: 1px solid #E3DACD; background: #FBF7EF; color: var(--ink);
  padding: 7px 8px; border-radius: 8px; font: inherit; font-size: 12px;
  cursor: pointer; text-transform: capitalize;
  transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}
.state-grid button:hover  { background: #F1EADC; }
.state-grid button.active { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ---------- prefers-reduced-motion: disable long ambient anims only;
     keep FSM transitions because they're short and informative. ---------- */
@media (prefers-reduced-motion: reduce) {
  .ambient__blob { animation: none; }
  .eyes { animation: none; }
}
