/* Mhyra Tier 0 — calm, warm, accessible UI for an older adult.
   Priorities: very large text, high contrast, big touch targets, minimal chrome.
   No framework, no build step. */

:root {
  --bg: #fbf6ee;          /* warm cream */
  --surface: #ffffff;
  --ink: #1f2430;          /* near-black, high contrast on cream */
  --ink-soft: #45506a;
  --me-bg: #e7eefc;        /* user bubble: soft blue */
  --me-ink: #14233f;
  --them-bg: #f3ebdd;      /* companion bubble: warm sand */
  --them-ink: #2a2316;
  --accent: #1d6f5c;       /* calm green, contrast ratio > 4.5 on white */
  --accent-ink: #ffffff;
  --warn: #8a3b12;         /* used only for the safety/help notice */
  --warn-bg: #fdefe3;
  --line: #d9cfbe;
  --focus: #0b5cab;
  font-size: 22px;         /* base — everything scales from here */

  /* ============================================================================
     MHYRA DESIGN SYSTEM — caregiver/admin surface (sign-in gate + settings).
     A purple-led, warm, dignified language built around the heart logo. These
     tokens style the ADMIN/CAREGIVER screens only; the patient conversation view
     above keeps its own calm large-text palette (--accent green, bubble colors).
     Eldercare, not enterprise: warm neutrals, soft elevation, generous radii.

     Contrast is verified for WCAG 2.1 AA — see the ratios noted per token.
     ============================================================================ */

  /* --- Brand: purple tonal scale (rich at top of logo → light) --- */
  --purple-50:  #f5f0fb;   /* tints, hovered rows                          */
  --purple-100: #eaddf7;   /* soft fills, halo, focus glow                 */
  --purple-200: #d6bff0;   /* borders on tinted surfaces                   */
  --purple-300: #bd97e6;   /* decorative only (2.4:1 — not for text)       */
  --purple-400: #9d6fd6;   /* decorative / large non-text (3.7:1)          */
  --purple-500: #7e4cc4;   /* AA text on white            (5.65:1)         */
  --purple-600: #6b3aad;   /* PRIMARY — buttons, brand    (7.41:1)         */
  --purple-700: #5a2f93;   /* primary hover, links, focus (9.28:1)         */
  --purple-800: #47276f;   /* headings on light           (11.75:1)        */
  --purple-900: #34204e;   /* deepest ink / shadow tint   (14.36:1)        */

  /* --- Semantic roles (admin) --- */
  --primary:        var(--purple-600);
  --primary-strong: var(--purple-700);
  --primary-soft:   var(--purple-100);
  --primary-tint:   var(--purple-50);
  --on-primary:     #ffffff;            /* 7.41:1 on --primary             */
  --brand-heading:  var(--purple-800);
  --brand-link:     var(--purple-700);

  /* --- Warm neutrals (admin surfaces) --- */
  --admin-bg:       #fbf6ee;            /* shared warm cream               */
  --admin-surface:  #ffffff;
  --admin-surface-2:#fdfbff;            /* faint lavender-white            */
  --admin-ink:      #2a2233;            /* warm near-black  (15.3:1)       */
  --admin-ink-soft: #5b5169;            /* secondary text   (7.4:1)        */
  --admin-ink-muted:#7a7088;            /* hints on white   (4.7:1)        */
  --admin-line:     #e7def2;            /* purple-tinted hairline (card edge, decorative) */
  --admin-line-2:   #8f7fb3;            /* input borders   (3.6:1 — passes 1.4.11) */

  /* --- Semantic status (all AA on white) --- */
  --ok:     #1f7a52;   /* 5.3:1  */
  --notice: #8a4b12;   /* 6.8:1  */
  --danger: #b3261e;   /* 6.5:1  */

  /* --- Typography scale (rem off the 22px base) --- */
  --fs-xs: 0.8rem;   --fs-sm: 0.95rem; --fs-md: 1.05rem;
  --fs-lg: 1.3rem;   --fs-xl: 1.6rem;  --fs-2xl: 2rem;
  --lh-tight: 1.25;  --lh-body: 1.5;
  --fw-regular: 400; --fw-medium: 600; --fw-bold: 700;

  /* --- Spacing scale (4px rhythm) --- */
  --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem;
  --space-4: 1rem;    --space-5: 1.5rem; --space-6: 2rem;

  /* --- Radius --- */
  --radius-sm: 8px;  --radius-md: 12px; --radius-lg: 16px;
  --radius-xl: 22px; --radius-pill: 999px;

  /* --- Elevation (soft, purple-tinted — rgba of --purple-900) --- */
  --elev-1: 0 1px 2px rgba(52,32,78,0.06), 0 2px 6px rgba(52,32,78,0.06);
  --elev-2: 0 4px 14px rgba(52,32,78,0.10);
  --elev-3: 0 14px 40px rgba(52,32,78,0.16);

  /* --- Focus + motion --- */
  --focus-ring: var(--purple-700);     /* 9.28:1 — well above 3:1         */
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --dur: 160ms;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  line-height: 1.5;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip {
  position: absolute; left: -999px; top: 0;
  background: var(--accent); color: var(--accent-ink);
  padding: 0.6rem 1rem; z-index: 10; border-radius: 0 0 8px 0;
}
.skip:focus { left: 0; }

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: 0.9rem 1.2rem;
  background: var(--surface); border-bottom: 2px solid var(--line);
}
.title { margin: 0; font-size: 1.7rem; font-weight: 700; letter-spacing: 0.2px; }

.icon-btn {
  font-size: 1.05rem; padding: 0.7rem 1.1rem;
  min-height: 56px; border: 2px solid var(--line); border-radius: 12px;
  background: var(--surface); color: var(--ink-soft); cursor: pointer;
}

/* ---- Conversation stage ---- */
.stage {
  flex: 1; width: 100%; max-width: 820px; margin: 0 auto;
  padding: 1.2rem; display: flex; flex-direction: column; gap: 1rem;
}

.transcript {
  flex: 1; min-height: 40vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 1rem;
  padding: 0.4rem;
}

.hint { color: var(--ink-soft); font-size: 1.2rem; text-align: center; margin: auto; }

.bubble {
  max-width: 90%; padding: 1rem 1.2rem; border-radius: 18px;
  font-size: 1.35rem; white-space: pre-wrap; word-wrap: break-word;
}
.bubble .who { display: block; font-size: 0.85rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.6px; opacity: 0.65; margin-bottom: 0.25rem; }

.from-them { align-self: flex-start; background: var(--them-bg); color: var(--them-ink);
  border-bottom-left-radius: 6px; }
.from-me { align-self: flex-end; background: var(--me-bg); color: var(--me-ink);
  border-bottom-right-radius: 6px; }

.bubble.safety {
  align-self: stretch; max-width: 100%;
  background: var(--warn-bg); color: var(--warn);
  border: 2px solid var(--warn); border-radius: 14px;
}

.status { min-height: 1.6rem; color: var(--ink-soft); font-size: 1.05rem; text-align: center; }
.status.error { color: var(--warn); font-weight: 700; }

/* ---- Composer (the always-available text box) ---- */
.composer { display: flex; gap: 0.7rem; align-items: stretch; flex-wrap: wrap; }

.text-input {
  flex: 1 1 60%; min-width: 12ch;
  font-size: 1.4rem; padding: 1rem 1.1rem;
  border: 3px solid var(--line); border-radius: 14px;
  background: var(--surface); color: var(--ink);
}
.text-input:focus { outline: 4px solid var(--focus); outline-offset: 2px; border-color: var(--focus); }

.btn {
  font-size: 1.3rem; font-weight: 700; cursor: pointer;
  min-height: 64px; padding: 0.8rem 1.4rem;
  border-radius: 14px; border: 3px solid transparent;
}
.btn:focus-visible { outline: 4px solid var(--focus); outline-offset: 3px; }

.btn-send { background: var(--accent); color: var(--accent-ink); }
.btn-mic { background: var(--surface); color: var(--accent); border-color: var(--accent); }
.btn-mic[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.btn-start { background: var(--accent); color: var(--accent-ink); flex: 1; min-height: 72px; font-size: 1.5rem; }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }

.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.actions { display: flex; gap: 0.7rem; }

.mic-note {
  margin: 0; padding: 0.8rem 1rem; font-size: 1.05rem;
  color: var(--ink-soft); background: var(--surface);
  border: 2px dashed var(--line); border-radius: 12px;
}

/* ---- Settings dialog (admin/caregiver management surface) ---- */
.settings {
  border: none; border-radius: var(--radius-xl); padding: 0; max-width: 560px; width: 92%;
  background: var(--admin-surface); color: var(--admin-ink); box-shadow: var(--elev-3);
}
.settings::backdrop { background: rgba(52, 32, 78, 0.42); }
.settings-form { padding: var(--space-6); display: flex; flex-direction: column; gap: var(--space-2); }

/* Header: mini heart mark beside the title to carry the brand into the panel. */
.settings-head { display: flex; align-items: center; gap: var(--space-3); margin-bottom: var(--space-3); }
.settings-head .logo-mark { width: 40px; height: 40px; }
.settings-form h2 {
  margin: 0; font-size: var(--fs-xl); line-height: var(--lh-tight);
  font-weight: var(--fw-bold); color: var(--brand-heading);
}
.settings-form label {
  font-size: var(--fs-md); font-weight: var(--fw-bold); color: var(--admin-ink);
  margin-top: var(--space-4);
}
.field-hint {
  margin: var(--space-1) 0 0; font-size: var(--fs-sm); font-weight: var(--fw-regular);
  color: var(--admin-ink-muted); line-height: var(--lh-body);
}
.settings-form input, .settings-form select {
  font-size: var(--fs-lg); padding: 0.75rem 0.85rem; margin-top: var(--space-2);
  border: 2px solid var(--admin-line-2); border-radius: var(--radius-md);
  background: var(--admin-surface); color: var(--admin-ink);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.settings-form input:focus, .settings-form select:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.settings-actions { display: flex; gap: var(--space-3); margin-top: var(--space-6); }
.settings-actions .btn { flex: 1; }

/* ---- App shell + caregiver/admin auth gate (Clerk, §1.10) ---- */
/* #app-root holds the whole companion UI; it stays a flex column so the stage layout is unchanged. */
#app-root { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.topbar-actions { display: flex; align-items: center; gap: 0.8rem; }
.user-button { display: inline-flex; align-items: center; min-height: 40px; }
.user-button:empty { display: none; }

/* While Clerk is configured AND no one is signed in, show only the gate. Set on <html> early to
   avoid a flash of the app; cleared by auth.js once a caregiver/admin signs in. */
#auth-gate { display: none; }
html.auth-gating #app-root { display: none; }
/* The gate sits on the warm cream field; a soft lavender wash adds calm depth. */
html.auth-gating #auth-gate {
  display: flex; align-items: center; justify-content: center;
  flex: 1; min-height: 100vh; padding: 1.5rem;
  background:
    radial-gradient(120% 80% at 50% -10%, var(--purple-100) 0%, rgba(234,221,247,0) 60%),
    var(--admin-bg);
}
.auth-card {
  background: var(--admin-surface);
  border: 1px solid var(--admin-line);
  border-radius: var(--radius-xl);
  padding: var(--space-6) var(--space-5) var(--space-5);
  max-width: 27rem; width: 100%; text-align: center;
  box-shadow: var(--elev-2);
}

/* Logo + halo. The logo's white-fading bottom would vanish on a plain surface, so it
   rests on a faint lavender disc (the "background treatment") that keeps the base soft
   but visible. On the cream app field elsewhere the bottom is meant to dissolve. */
.logo-halo {
  width: 116px; height: 116px; margin: 0 auto var(--space-4);
  display: grid; place-items: center; border-radius: var(--radius-pill);
  background: radial-gradient(closest-side, var(--purple-50) 0%, rgba(245,240,251,0.55) 62%, rgba(245,240,251,0) 100%);
}
.logo-mark { width: 76px; height: 76px; display: block; }

.auth-title {
  margin: 0 0 var(--space-2); font-size: var(--fs-xl); line-height: var(--lh-tight);
  font-weight: var(--fw-bold); color: var(--brand-heading); letter-spacing: 0.1px;
}
.auth-sub { margin: 0 0 var(--space-5); color: var(--admin-ink-soft); font-size: var(--fs-md); }
#sign-in { display: flex; justify-content: center; }

/* Quiet reassurance line — clarifies caregiver-only access (PROJECT-PLAN §1.10). */
.auth-foot {
  margin: var(--space-5) 0 0; padding-top: var(--space-4);
  border-top: 1px solid var(--admin-line);
  color: var(--admin-ink-muted); font-size: var(--fs-sm); line-height: var(--lh-body);
}
.auth-error { color: var(--danger); font-weight: var(--fw-bold); }

/* Best-effort cosmetic theming for the Clerk-mounted widget (purely visual; the gate
   works regardless of whether these .cl-* hooks match the loaded Clerk version). */
#sign-in .cl-formButtonPrimary { background: var(--primary); }
#sign-in .cl-formButtonPrimary:hover { background: var(--primary-strong); }
#sign-in a, #sign-in .cl-footerActionLink { color: var(--brand-link); }

/* ---- Primary action button (admin surfaces) ---- */
.btn-primary {
  background: var(--primary); color: var(--on-primary); border-color: transparent;
  transition: background var(--dur) var(--ease);
}
.btn-primary:hover { background: var(--primary-strong); }
/* White gap + purple ring stays visible on both the button and the page. */
.btn-primary:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--admin-surface), 0 0 0 6px var(--focus-ring);
}

/* Respect users who prefer less motion. */
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
  .btn-primary, .settings-form input, .settings-form select { transition: none; }
}

/* Larger phones / small screens: stack the composer. */
@media (max-width: 520px) {
  :root { font-size: 20px; }
  .text-input { flex-basis: 100%; }
  .btn-send, .btn-mic { flex: 1; }
}
