/* =====================================================================
   General Access · style.css            owner: design-layout
   Tokens, page shell, and the layout of every state.
   Component looks  -> components.css    Motion -> motion.css
   States are driven ONLY by body[data-*] (see CONTRACT.md).

   SLOTS: class names that exist in index.html
   ---------------------------------------------------------------------
   Screens        .screen  (#gate #ready #call #ended)
   Panel shell    .panel  .panel-top  .panel-body  .panel-actions
                  .title (h1)  .lead (one-line subline)
                  .brand  .brand-name
   Signature      .finder  .finder-sm  .finder-lg  .finder-corner x4
                  .finder-dot  .is-resting (ended: dot off)
   Buttons        .btn  .btn-primary  .btn-stop  .btn-icon  .btn-lg  .btn-xl
                  .btn-label-ready  .btn-label-starting  (inside #start)
   Forms          .gate-form  .field  .field-code  .form-status
                  .starting-hint (shown only in data-state=starting)
                  .type-form (#typeForm)  .type-send
   Call           .call-frame  (#stage #video #box)  .box-corner x4
                  .scrim  .call-hud  .hud-top  .hud-bottom
                  .status-chip  .status-dot          (wrap #status)
                  .heard-row  .heard-icon            (wrap #heard)
                  .caption-wrap  .caption-mark  .thinking-mark (wrap #caption)
   Utilities      .glass  .icon  .visually-hidden

   CUSTOM PROPERTIES added on top of the shared tokens
   ---------------------------------------------------------------------
   --accent-media   accent used over the camera (same in light + dark)
   --stop --on-stop semantic end-call red (Stop only)
   --fs-title --fs-caption --fs-btn --fs-btn-xl --fs-small
   --tap-lg (64px)  --tap-xl (72px)
   --hud-bg --hud-border --hud-text --hud-text-2   glass/text over camera
   --scrim-bottom --scrim-top                       caption readability
   --safe-t --safe-r --safe-b --safe-l              padded safe areas
   --finder-size --c-len --c-w --c-r                corner-bracket geometry
   ===================================================================== */

:root {
  color-scheme: dark light;

  /* Type: SF on Apple devices, Inter if installed, system UI elsewhere. */
  --font-sans: -apple-system, BlinkMacSystemFont, "Inter", system-ui, sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, "Inter Display", "Inter", system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;

  /* Scale tuned for an elderly reader: body 18px, nothing readable below 16px. */
  --fs-title: clamp(2rem, 1.55rem + 1.9vw, 2.75rem);   /* 32px phone -> 44px laptop */
  --lh-title: 1.1;  --ls-title: -0.022em;
  --fs-lead: 1.25rem;                                    /* 20px */
  --fs-body: 1.125rem;                                   /* 18px */
  --lh-body: 1.47;
  --fs-small: 1rem;                                      /* 16px: status, heard */
  --fs-xs: 0.8125rem;                                    /* 13px: debug only */
  --fs-caption: clamp(1.375rem, 1.1rem + 1.2vw, 1.75rem);/* 22px phone -> 28px laptop */
  --lh-caption: 1.32;
  --fs-btn: 1.25rem;                                     /* 20px */
  --fs-btn-xl: 1.5rem;                                   /* 24px: Start, Stop */
  --measure: 68ch;

  /* Spacing: 4px grid. Use only these. */
  --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px; --s-5: 24px; --s-6: 32px;
  --s-7: 48px; --s-8: 64px; --s-9: 96px; --s-10: 128px; --s-11: 160px;
  --content-max: 1200px;
  --column-max: 28rem;     /* gate / ready / ended column */

  /* Tap targets */
  --tap: 48px; --tap-lg: 64px; --tap-xl: 72px;

  /* Corners */
  --r-sm: 10px; --r-md: 14px; --r-lg: 20px; --r-xl: 28px; --r-pill: 999px;

  /* Motion (motion.css uses these) */
  --ease-out: cubic-bezier(.22, 1, .36, 1);
  --dur-fast: 150ms; --dur-base: 250ms; --dur-slow: 400ms; --dur-reveal: 600ms;

  /* Safe areas, never closer than 16 / 24px to an edge */
  --safe-t: max(var(--s-4), env(safe-area-inset-top));
  --safe-r: max(var(--s-5), env(safe-area-inset-right));
  --safe-b: max(var(--s-5), env(safe-area-inset-bottom));
  --safe-l: max(var(--s-5), env(safe-area-inset-left));

  /* Dark (default) */
  --bg: #08090a;
  --bg-2: #101113;
  --surface: #16171a;
  --text: #f5f5f7;         /* 18.3:1 on --bg */
  --text-2: #a1a1a6;       /* 7.7:1 */
  --text-3: #86868b;       /* 5.5:1 */
  --border: rgba(255, 255, 255, .08);
  --border-strong: rgba(255, 255, 255, .16);

  /* Accent: warm amber, "a lamp left on for you". */
  --accent: #ffb340;       /* 11.2:1 on #08090a */
  --on-accent: #000;       /* 11.8:1 on #ffb340 */
  --link: var(--accent);
  --accent-media: #ffb340; /* over the camera, both themes */

  /* End-call red: semantic, only on #stop. White text 5.4:1. */
  --stop: #d70015;
  --on-stop: #fff;

  --glass-bg: rgba(22, 23, 26, .55);
  --glass-border: rgba(255, 255, 255, .10);
  --glass-hl: inset 0 1px 0 rgba(255, 255, 255, .08);
  --blur-glass: 24px;
  --shadow-lg: 0 24px 80px -24px rgba(0, 0, 0, .6);
  --glow: radial-gradient(70% 45% at 30% 0%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 70%);
  --focus: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);

  /* Over the camera (always dark, whatever the theme) */
  --hud-bg: rgba(12, 12, 14, .62);
  --hud-border: rgba(255, 255, 255, .14);
  --hud-text: #fff;
  --hud-text-2: rgba(255, 255, 255, .78);
  --scrim-bottom: linear-gradient(to top, rgba(0, 0, 0, .6) 0%, rgba(0, 0, 0, .34) 22%, rgba(0, 0, 0, 0) 40%);
  --scrim-top: linear-gradient(to bottom, rgba(0, 0, 0, .38) 0%, rgba(0, 0, 0, 0) 18%);

  /* Corner-bracket geometry (finder + #box) */
  --finder-size: 88px; --c-len: 30%; --c-w: 3px; --c-r: 10px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --bg-2: #f5f5f7;
    --surface: #ffffff;
    --text: #1d1d1f;       /* 16.8:1 */
    --text-2: #515154;     /* 7.9:1 */
    --text-3: #6e6e73;     /* 4.7:1 on --bg-2 */
    --border: rgba(0, 0, 0, .08);
    --border-strong: rgba(0, 0, 0, .16);
    --accent: #b35500;     /* white on it 5.0:1; as text 5.0:1 on #fff, 4.6:1 on #f5f5f7 */
    --on-accent: #fff;
    --link: #b35500;
    --glass-bg: rgba(255, 255, 255, .62);
    --glass-border: rgba(255, 255, 255, .7);
    --glass-hl: inset 0 1px 0 rgba(255, 255, 255, .9);
    --shadow-lg: 0 20px 60px -20px rgba(0, 0, 0, .18);
    --glow: radial-gradient(70% 45% at 30% 0%, rgba(255, 179, 64, .14), transparent 70%);
  }
}

@media (prefers-reduced-motion: reduce) {
  :root { --dur-fast: 0ms; --dur-base: 0ms; --dur-slow: 0ms; --dur-reveal: 0ms; }
}

/* ---------- Base ---------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 400 var(--fs-body)/var(--lh-body) var(--font-sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

h1, p { margin: 0; }
button, input { font: inherit; color: inherit; }
label { cursor: inherit; }

.icon {
  width: 24px; height: 24px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

.glass {
  background: var(--glass-bg);
  -webkit-backdrop-filter: blur(var(--blur-glass)) saturate(180%);
  backdrop-filter: blur(var(--blur-glass)) saturate(180%);
  border: 1px solid var(--glass-border);
  box-shadow: var(--glass-hl);
}
@media (prefers-reduced-transparency: reduce) {
  .glass { -webkit-backdrop-filter: none; backdrop-filter: none; background: rgba(10, 10, 12, .88); }
}

/* ---------- Which section shows -------------------------------------- */

.screen { display: none; }
body[data-state="gate"] #gate,
body[data-state="ready"] #ready,
body[data-state="starting"] #ready,
body[data-state="ended"] #ended { display: grid; }
body[data-state="call"] #call { display: block; }
body[data-state="call"] { background: #000; overflow: hidden; }

/* ---------- Panel shell: gate / ready / ended ------------------------ */
/* Phone: brand on top, content in the middle, main action in the thumb zone. */

.panel {
  min-height: 100dvh;
  grid-template-columns: minmax(0, var(--column-max));
  grid-template-rows: auto 1fr auto;
  justify-content: center;
  row-gap: var(--s-6);
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
  background: var(--glow), var(--bg);
}

.brand {
  display: flex; align-items: center; gap: var(--s-3);
  min-height: var(--tap);
}
.brand-name { font-size: var(--fs-small); font-weight: 500; color: var(--text-2); }

.panel-body {
  align-self: center;
  display: grid; justify-items: start; gap: var(--s-3);
}
.panel-body > .finder-lg { margin-bottom: var(--s-5); }

.title {
  font: 600 var(--fs-title)/var(--lh-title) var(--font-display);
  letter-spacing: var(--ls-title);
  text-wrap: balance;
  max-width: 16ch;
}
.lead {
  font-size: var(--fs-lead); line-height: 1.4;
  color: var(--text-2);
  max-width: 30ch;
  text-wrap: pretty;
}

.panel-actions { display: grid; gap: var(--s-3); }

.form-status { min-height: 1.47em; color: var(--text-2); }
.form-status:empty { min-height: 0; }

/* Gate: the button sits right under the field, so the phone keyboard never hides it. */
.gate-form { justify-items: stretch; }
.gate-form > .title, .gate-form > .lead { justify-self: start; }
.gate-form > .field { margin-top: var(--s-5); }

/* Ready vs starting */
.btn-label-starting, .starting-hint { display: none; }
body[data-state="starting"] .btn-label-ready { display: none; }
body[data-state="starting"] .btn-label-starting { display: inline; }
body[data-state="starting"] .starting-hint { display: block; }
.starting-hint { color: var(--text-2); }

/* ---------- Signature: the viewfinder corners ----------------------- */

.finder {
  position: relative; display: block; flex: none;
  width: var(--finder-size); aspect-ratio: 1;
  color: var(--accent);
}
.finder-sm { --finder-size: 22px; --c-len: 38%; --c-w: 2px; --c-r: 4px; }
.finder-lg { --finder-size: 88px; }

.finder-corner, .box-corner {
  position: absolute; width: var(--c-len); height: var(--c-len);
  border: 0 solid currentColor;
}
.finder-corner:nth-child(1), .box-corner:nth-child(1) { top: 0; left: 0; border-top-width: var(--c-w); border-left-width: var(--c-w); border-top-left-radius: var(--c-r); }
.finder-corner:nth-child(2), .box-corner:nth-child(2) { top: 0; right: 0; border-top-width: var(--c-w); border-right-width: var(--c-w); border-top-right-radius: var(--c-r); }
.finder-corner:nth-child(3), .box-corner:nth-child(3) { bottom: 0; right: 0; border-bottom-width: var(--c-w); border-right-width: var(--c-w); border-bottom-right-radius: var(--c-r); }
.finder-corner:nth-child(4), .box-corner:nth-child(4) { bottom: 0; left: 0; border-bottom-width: var(--c-w); border-left-width: var(--c-w); border-bottom-left-radius: var(--c-r); }

.finder-dot {
  position: absolute; left: 50%; top: 50%;
  width: 14px; height: 14px; margin: -7px 0 0 -7px;
  border-radius: 50%; background: currentColor;
}
.finder.is-resting { color: var(--text-3); }

/* ---------- Buttons and fields (minimal; components.css refines) ---- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-3);
  min-height: var(--tap); padding: 0 var(--s-5);
  border: 1px solid transparent; border-radius: var(--r-pill);
  font: 600 var(--fs-body)/1 var(--font-sans);
  cursor: pointer; user-select: none; touch-action: manipulation;
  background: transparent;
}
.btn:focus-visible { outline: none; box-shadow: var(--focus); }
.btn:disabled { cursor: default; opacity: .6; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-stop { background: var(--stop); color: var(--on-stop); }
.btn-lg { width: 100%; min-height: var(--tap-lg); font-size: var(--fs-btn); }
.btn-xl { width: 100%; min-height: var(--tap-xl); font-size: var(--fs-btn-xl); }
.btn-icon { width: var(--tap); height: var(--tap); padding: 0; flex: none; }

.field {
  width: 100%; min-height: 56px; padding: 0 var(--s-4);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--r-md);
  font-size: var(--fs-body);
}
.field:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.field-code { min-height: var(--tap-lg); font-size: 1.5rem; letter-spacing: .14em; }

/* ---------- Call: the camera fills the screen ----------------------- */

#call {
  position: fixed; inset: 0;
  background: #000; color: var(--hud-text);
  color-scheme: dark;
  /* Over the camera everything is dark, in both themes. */
  --accent: var(--accent-media);
  --on-accent: #000;
  --text: var(--hud-text);
  --text-2: var(--hud-text-2);
  --surface: var(--hud-bg);
  --border-strong: var(--hud-border);
  --glass-bg: var(--hud-bg);
  --glass-border: var(--hud-border);
  --glass-hl: inset 0 1px 0 rgba(255, 255, 255, .08);
  --focus: 0 0 0 2px #000, 0 0 0 4px var(--accent-media);
}

.call-frame { position: absolute; inset: 0; overflow: hidden; isolation: isolate; }

#stage {
  position: absolute; inset: 0; overflow: hidden;
  background: #1c1d20;   /* neutral dark until the camera picture arrives */
}
#video {
  position: absolute; inset: 0; display: block;
  width: 100%; height: 100%; object-fit: cover;
}
#box {
  position: absolute; left: 0; top: 0;
  --c-len: 24px; --c-w: 3px; --c-r: 10px;
  color: var(--accent-media);
  border-radius: var(--c-r);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent-media) 35%, transparent);
  pointer-events: none;
}

.scrim {
  position: absolute; inset: 0; pointer-events: none;
  background: var(--scrim-top), var(--scrim-bottom);
}

.call-hud {
  position: absolute; inset: 0;
  display: grid; grid-template-rows: auto 1fr auto;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
  pointer-events: none;
}
.call-hud :is(button, input, form) { pointer-events: auto; }

.hud-top {
  display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s-3);
}
.hud-bottom { grid-row: 3; display: grid; gap: var(--s-4); }

.status-chip {
  display: inline-flex; align-items: center; gap: var(--s-2);
  min-height: 40px; max-width: 100%;
  padding: var(--s-2) var(--s-4);
  border-radius: var(--r-pill);
  font-size: var(--fs-small); font-weight: 500; line-height: 1.25;
}
.status-chip:has(#status:empty) { visibility: hidden; }
.status-dot { width: 8px; height: 8px; flex: none; border-radius: 50%; background: var(--accent-media); }

#typeToggle { color: var(--hud-text); }

.heard-row {
  display: flex; align-items: flex-start; gap: var(--s-2);
  color: var(--hud-text-2); font-size: var(--fs-small); line-height: 1.4;
}
.heard-row:has(#heard:empty) { display: none; }
.heard-icon { width: 18px; height: 18px; margin-top: 2px; }
#heard { overflow-wrap: anywhere; }

.caption-wrap { position: relative; padding-left: var(--s-4); }
.caption-mark {
  position: absolute; left: 0; top: .3em; bottom: .3em; width: 3px;
  border-radius: 2px; background: var(--accent-media);
}
.caption-wrap:has(#caption:empty) .caption-mark { display: none; }
#caption {
  font: 500 var(--fs-caption)/var(--lh-caption) var(--font-sans);
  letter-spacing: -0.01em;
  text-wrap: pretty; overflow-wrap: break-word;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .55), 0 0 18px rgba(0, 0, 0, .35);
  max-width: 34ch;
}
.thinking-mark { display: none; gap: 6px; align-items: center; height: 1.5rem; }
.thinking-mark i { width: 8px; height: 8px; border-radius: 50%; background: var(--hud-text-2); }
body[data-thinking="true"] .thinking-mark { display: flex; }

.type-form { display: grid; grid-template-columns: 1fr auto; gap: var(--s-2); }
.type-form .field { min-height: 56px; border-radius: var(--r-pill); padding: 0 var(--s-5); }
.type-send { width: 56px; height: 56px; }

/* ---------- Debug ----------------------------------------------------- */

#debug { display: none; }
#player { display: none; }
body[data-debug="true"] #debug {
  display: block; position: fixed; z-index: 50;
  top: calc(var(--safe-t) + 56px); left: var(--safe-l);
  max-width: min(calc(100vw - 48px), 28rem); max-height: 40dvh; overflow: auto;
  margin: 0; padding: var(--s-3);
  background: rgba(0, 0, 0, .7); color: rgba(255, 255, 255, .9);
  border-radius: var(--r-sm);
  font: 400 var(--fs-xs)/1.45 var(--font-mono);
  white-space: pre-wrap; pointer-events: none;
}

/* ---------- Phone held sideways --------------------------------------- */

@media (orientation: landscape) and (max-height: 520px) {
  :root { --fs-caption: 1.25rem; }
  .hud-bottom {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end; column-gap: var(--s-5);
  }
  .hud-bottom > :not(#stop) { grid-column: 1; }
  #stop { grid-column: 2; grid-row: 1 / span 3; align-self: end; width: auto; min-height: var(--tap-lg); }
}

/* ---------- Laptop ---------------------------------------------------- */

@media (min-width: 768px) {
  /* Content and action grouped in the middle, brand stays top-left. */
  .panel {
    grid-template-rows: auto 1fr auto auto 1.4fr;
    row-gap: 0;
    padding: var(--s-6) var(--s-7);
  }
  .panel-top { grid-row: 1; }
  .panel-body { grid-row: 3; }
  .panel-actions { grid-row: 4; margin-top: var(--s-7); }
  .gate-form { grid-row: 3 / span 2; }
}

@media (min-width: 900px) {
  /* The camera becomes a large, centred stage; controls stay a calm column. */
  #call { padding: var(--s-5); background: #08090a; }
  .call-frame {
    position: relative; inset: auto;
    width: 100%; max-width: var(--content-max);
    height: calc(100dvh - 2 * var(--s-5));
    margin: 0 auto;
    border-radius: var(--r-xl);
    box-shadow: 0 0 0 1px var(--hud-border);
  }
  .call-hud { padding: var(--s-5) var(--s-6) var(--s-6); }
  .hud-bottom {
    width: 100%; max-width: 42rem; justify-self: center;
  }
  #caption { max-width: none; }
  body[data-debug="true"] #debug { top: calc(var(--s-5) + 88px); left: calc(var(--s-5) + var(--s-6)); }
  #stop { justify-self: center; width: auto; min-width: 15rem; padding: 0 var(--s-7); }
}
