/* ============================================================================
 * app.css — web-viewer screen layout. Built on design/tokens.css + components.css.
 * No inline styles anywhere in the JS (CSP: style-src 'self', no 'unsafe-inline') — every
 * visual state is a class toggle. Positions computed by JS use ONLY per-property CSSOM
 * assignment (element.style.transform = ...), which CSP's style-src does not govern; see
 * the handoff for the reasoning. Nothing here invents brand colors — tokens only.
 * ========================================================================== */

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }

/* 🔴 LOAD-BEARING, NOT A RESET NICETY. Every JS visibility toggle in this app is
 * `element.hidden = true/false` (the IDL property). The browser's UA stylesheet implements
 * that with `[hidden] { display: none }` at the *lowest* cascade origin. Almost every
 * component class here (`.ra-btn`, `.ra-live__status`, `.ra-cta-row`, ...) also sets
 * `display: flex` at the *author* origin with equal specificity — and author beats UA at
 * equal specificity, so `hidden` silently loses and the element stays visible. Caught by the
 * headless-browser smoke test (see the handoff), not by reading the code. This one rule fixes
 * every instance of it at once, permanently, instead of chasing it component-by-component. */
[hidden] { display: none !important; }
body {
  min-height: 100dvh;
  font-family: var(--font-sans);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
#app { min-height: 100dvh; display: flex; flex-direction: column; }

.ra-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 520px;
  width: 100%;
  margin: 0 auto;
  padding: var(--sp-6) var(--screen-pad) var(--sp-8);
  position: relative;
}

/* ------------------------- language toggle --------------------------- */
.ra-lang-toggle {
  position: fixed;
  top: var(--sp-3);
  right: var(--sp-3);
  z-index: 40;
  min-height: 40px;
  min-width: 40px;
  padding: 6px 12px;
  border-radius: var(--r-pill);
  border: 1px solid var(--border-strong);
  background: var(--surface-sheet);
  color: var(--text-dim);
  font-family: var(--font-sans);
  font-size: var(--fs-caption);
  font-weight: 700;
  cursor: pointer;
}
.ra-lang-toggle:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ------------------------------ loading -------------------------------- */
.ra-loading {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-4);
  text-align: center;
}
.ra-loading__pulse {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--accent-dim);
  border: 2px solid var(--accent);
  animation: ra-breathe var(--dur-calm) ease-in-out infinite;
}

/* ------------------------------ inactive -------------------------------- */
.ra-inactive, .ra-terminal {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding-top: 12vh;
  gap: var(--sp-4);
}
.ra-inactive__icon {
  position: relative;
  width: 96px; height: 96px;
  border-radius: 50%;
  border: 1.5px solid var(--border-strong);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: var(--sp-2);
}
.ra-inactive__icon::before {
  content: ""; position: absolute; inset: 18px;
  border-radius: 50%; border: 1.5px solid var(--accent);
}
.ra-inactive__dot { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); }
.ra-inactive__icon--warn { border-color: var(--info); color: var(--info-text); font-size: 34px; font-weight: 800; }
.ra-inactive__icon--warn::before { display: none; }
.ra-inactive h1 { font-size: var(--fs-h1); margin: 0; line-height: var(--lh-tight); }
.ra-inactive p { color: var(--text-dim); font-size: var(--fs-body); line-height: var(--lh-body); margin: 0; max-width: 34ch; }
.ra-inactive__note, .ra-terminal__note { width: 100%; text-align: left; color: var(--text-dim); font-size: var(--fs-small); margin-top: var(--sp-4); }

/* ------------------------------ terminal (safe/cancelled/expired) ------- */
.ra-terminal__icon {
  width: 96px; height: 96px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 40px; font-weight: 800;
}
.ra-terminal__icon--safe { background: var(--safe-dim); color: var(--safe-text); }
.ra-terminal__icon--neutral { background: var(--fill-ghost); color: var(--text-dim); border: 1.5px solid var(--border-strong); font-size: 30px; }
.ra-terminal__icon--expired { background: var(--info-dim); color: var(--info-text); font-size: 28px; }
.ra-terminal h1 { font-size: var(--fs-h1); margin: 0; }
.ra-terminal__sub { color: var(--text-dim); font-size: var(--fs-body); margin: 0; }
.ra-terminal .ra-map-wrap { margin-top: var(--sp-4); }
.ra-terminal__actions { width: 100%; margin-top: var(--sp-4); }

/* ------------------------------ consent (angel-consent, #20) ------------- */
.ra-consent {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  text-align: center; gap: var(--sp-3); padding-top: 6vh;
}
.ra-consent__avatar {
  width: 96px; height: 96px; border-radius: 50%;
  background: var(--accent-dim);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; margin-bottom: var(--sp-2);
}
.ra-consent__avatar img { width: 48px; height: 48px; object-fit: contain; opacity: 0.9; }
.ra-consent h1 { font-size: var(--fs-h1); margin: 0; line-height: var(--lh-tight); }
.ra-consent > p { color: var(--text-dim); font-size: var(--fs-body); line-height: var(--lh-body); margin: 0; max-width: 36ch; }
.ra-consent__card { width: 100%; text-align: left; margin-top: var(--sp-3); }
.ra-consent__list { display: flex; flex-direction: column; gap: var(--sp-2); margin: var(--sp-2) 0 var(--sp-3); }
.ra-consent__never-title { margin-top: var(--sp-3); }
.ra-consent__row { display: flex; align-items: flex-start; gap: var(--sp-2); font-size: var(--fs-body); }
.ra-consent__ok { color: var(--safe-text); font-weight: 700; flex: none; }
.ra-consent__fail { color: var(--danger-text); font-weight: 700; flex: none; }
.ra-consent__warning { width: 100%; text-align: left; margin-top: var(--sp-3); }
.ra-consent .ra-btn { margin-top: var(--sp-3); }
.ra-consent .ra-btn:last-of-type { margin-top: var(--sp-2); }

/* ------------------------------ live screen ----------------------------- */
.ra-live { flex: 1; display: flex; flex-direction: column; gap: var(--sp-4); }
.ra-live__status { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); flex-wrap: wrap; }
.ra-live__badge { display: inline-flex; align-items: center; gap: 8px; color: var(--danger-text); font-weight: 800; font-size: var(--fs-caption); letter-spacing: var(--ls-label); }
.ra-live__head { display: flex; flex-direction: column; gap: var(--sp-2); } /* title ↔ SOS-chip breathing room */
.ra-live__title { font-size: var(--fs-h1); margin: 0; line-height: var(--lh-tight); }
.ra-live__meta { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
.ra-live__time { color: var(--text-dim); }

/* ------------------------------ map ------------------------------------- */
.ra-map-wrap {
  position: relative;
  flex: 1;
  align-self: stretch; /* .ra-terminal sets align-items:center to center text/icons above it;
                           without this the map-wrap flex item shrinks to its (near-empty,
                           pre-render) content width instead of filling the row. Caught by the
                           headless-browser smoke test — the frozen map rendered as a sliver. */
  width: 100%;
  min-height: 320px;
  border-radius: var(--r-lg);
  overflow: hidden;
  background: var(--map-bg);
  border: 1px solid var(--border);
}
.ra-map { position: absolute; inset: 0; }
.ra-map-wrap--frozen .ra-map { filter: saturate(0.55) brightness(0.8); }
.ra-map-wrap--nomap { display: none; }

.ra-map__pill {
  position: absolute;
  right: var(--sp-3);
  background: var(--map-overlay);
  border: 1px solid var(--map-overlay-border);
  color: var(--text);
  font-size: var(--fs-small);
  font-weight: 700;
  padding: 8px 14px;
  border-radius: var(--r-pill);
  backdrop-filter: blur(6px);
  pointer-events: none;
}
.ra-map__pill--moving { top: var(--sp-3); }
.ra-map__pill--accuracy { top: calc(var(--sp-3) + 44px); }

.ra-map__marker-accuracy {
  position: absolute;
  left: 0; top: 0;
  width: 0; height: 0;
  border-radius: 50%;
  background: radial-gradient(circle, var(--danger-fill) 0%, var(--danger-fill) 70%, transparent 100%);
  border: 1px solid var(--danger-ring);
  pointer-events: none;
  transform: translate(-50%, -50%);
  will-change: transform, width, height;
}
.ra-map__marker-dot {
  position: absolute;
  left: 0; top: 0;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--danger);
  border: 4px solid #fff;
  box-shadow: var(--shadow-pin);
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.ra-map__marker-dot::after {
  content: "";
  position: absolute; inset: -8px;
  border-radius: 50%;
  border: 2px solid var(--danger);
  animation: ra-ping var(--dur-alert) ease-out infinite;
}
.ra-map__marker-dot--frozen { background: var(--safe); border-color: #fff; }
.ra-map__marker-dot--frozen::after { display: none; }
.ra-map__marker-dot--expired { background: var(--text-muted); }
.ra-map__marker-dot--expired::after { display: none; }

.ra-map__start-ring {
  position: absolute; left: 0; top: 0;
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2.5px solid var(--accent);
  background: transparent;
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.ra-map__start-label {
  position: absolute; left: 0; top: 0;
  transform: translate(6px, 10px);
  background: var(--map-overlay);
  border: 1px solid var(--map-overlay-border);
  color: var(--text-dim);
  font-size: var(--fs-caption);
  font-weight: 700;
  padding: 4px 10px;
  border-radius: var(--r-pill);
  white-space: nowrap;
  pointer-events: none;
}
.ra-map__ended-pill {
  position: absolute; left: 0; top: 0;
  transform: translate(10px, -50%);
  background: var(--surface-sheet);
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-size: var(--fs-caption);
  font-weight: 800;
  letter-spacing: var(--ls-label);
  text-transform: uppercase;
  padding: 8px 14px;
  border-radius: var(--r-pill);
  white-space: nowrap;
}
.ra-map__placeholder-note {
  position: absolute; left: var(--sp-3); bottom: var(--sp-3);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: .06em;
  text-transform: uppercase;
  pointer-events: none;
}

/* ------------------------------ CTA stack -------------------------------- */
.ra-cta-stack { display: flex; flex-direction: column; gap: var(--sp-3); margin-top: var(--sp-2); }
.ra-cta-row { display: flex; gap: var(--sp-3); }
.ra-cta-row > .ra-btn { flex: 1; }
.ra-link-action {
  background: none; border: none; color: var(--info-text);
  font-family: var(--font-sans); font-size: var(--fs-small); font-weight: 700;
  min-height: var(--hit-min); cursor: pointer; text-align: center;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.ra-link-action .ra-caption { color: var(--text-muted); font-weight: 500; }
.ra-disclosure { margin-top: var(--sp-2); }
.ra-disclosure[hidden] { display: none; }

.ra-footer { margin-top: var(--sp-4); }
.ra-wordmark { margin-top: var(--sp-2); margin-bottom: 0; }
.ra-toast {
  margin-top: var(--sp-2);
  text-align: center;
  color: var(--safe-text);
  font-size: var(--fs-caption);
  font-weight: 700;
}
.ra-toast[hidden] { display: none; }

/* ------------------------------ duress ----------------------------------- */
.ra-live--duress .ra-banner-duress { border-radius: var(--r-lg); margin: calc(var(--sp-6) * -1) calc(var(--screen-pad) * -1) 0; }
.ra-live__status--duress { margin-top: var(--sp-2); }

/* ------------------------------ dev state switcher ------------------------ */
.ra-devbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 100;
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 8px; background: rgba(0,0,0,0.85);
  border-top: 2px dashed var(--accent);
}
.ra-devbar__btn {
  font-family: var(--font-mono); font-size: 11px; font-weight: 700;
  color: var(--on-accent); background: var(--accent);
  border: none; border-radius: 6px; padding: 8px 10px; cursor: pointer;
  min-height: 32px;
}
.ra-devbar__btn[aria-pressed="true"] { outline: 2px solid #fff; }
.ra-devbar__label {
  color: var(--text-muted); font-family: var(--font-mono); font-size: 10px;
  align-self: center; margin-right: 4px; text-transform: uppercase; letter-spacing: .08em;
}

/* ------------------------------ offline / connection chip ----------------- */
.ra-conn-chip {
  position: fixed; top: var(--sp-3); left: var(--sp-3); z-index: 40;
  display: inline-flex; align-items: center; gap: 7px;
  background: var(--surface-sheet); border: 1px solid var(--border-strong);
  color: var(--text-dim); font-size: var(--fs-caption); font-weight: 700;
  padding: 6px 12px; border-radius: var(--r-pill);
}
.ra-conn-chip[hidden] { display: none; }

/* ------------------------------ accessibility ----------------------------- */
.ra-sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

@media (min-width: 640px) {
  .ra-view { padding-top: var(--sp-8); }
}

/* ============================================================================
 * iter-11 — a real product at every width (the Angel is often on a laptop).
 *
 * ≥920px, LIVE screen only: a dispatch-console layout. Info rail (status, title,
 * actions) on the left; THE MAP AS THE HERO filling the right column top-to-bottom.
 * The duress banner ALWAYS spans the full width above everything — it must be the
 * first thing an Angel sees, never tucked into a rail.
 *
 * `:has()` keeps this pure CSS over the existing flat DOM (no JS changes; older
 * browsers simply keep the mobile column — a harmless graceful fallback).
 * Terminal screens (safe/cancelled/expired) stay a calm centered column, just a
 * little wider so the frozen map breathes.
 * ========================================================================== */
@media (min-width: 920px) {
  .ra-view:has(.ra-live) {
    max-width: min(1280px, 100%);
    /* Real breathing room: the console must never sit flush against the viewport. */
    padding: var(--sp-8) var(--sp-11);
  }
  .ra-live {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(330px, 400px) minmax(0, 1fr);
    /* Rail groups status → head → actions together at the TOP; only the footer is pushed to the
       bottom (its row is the 1fr spacer). Previously the CTA row was the 1fr, which stranded the
       Call button far below the title. */
    grid-template-rows: auto auto auto auto auto 1fr;
    grid-template-areas:
      "banner   banner"
      "status   map"
      "dstatus  map"
      "head     map"
      "cta      map"
      "footer   map";
    column-gap: var(--sp-11);
    row-gap: var(--sp-4); /* the mobile column's inter-block margins don't exist in grid — restore them */
    align-items: start;
  }
  .ra-live > .ra-banner-duress { grid-area: banner; margin-bottom: var(--sp-5); }
  .ra-live > .ra-live__status { grid-area: status; }
  .ra-live > .ra-live__status--duress { grid-area: dstatus; }
  .ra-live > .ra-live__head { grid-area: head; }
  .ra-live > .ra-map-wrap {
    grid-area: map;
    align-self: stretch;
    height: 100%;
    min-height: 480px;
  }
  .ra-live > .ra-cta-stack { grid-area: cta; align-self: start; width: 100%; }
  .ra-live > .ra-footer { grid-area: footer; align-self: end; }
  .ra-live > .ra-wordmark { display: none; } /* the rail is working space; branding lives on calm screens */

  /* Terminal + inactive screens: calm centered column, wider map */
  .ra-view:has(.ra-terminal), .ra-view:has(.ra-inactive) { max-width: 640px; }
  .ra-terminal .ra-map-wrap { min-height: 380px; }
}

/* Very tall laptop/desktop: let the console breathe vertically instead of stretching forever. */
@media (min-width: 920px) and (min-height: 1000px) {
  .ra-view:has(.ra-live) { max-height: 1040px; margin-top: auto; margin-bottom: auto; }
}
