/* Heal360 Personal Coach — patient app
   Brand burgundy on warm neutrals, mobile-first: patients are on phones. */

:root {
  --brand: #882756;
  --brand-dark: #6d1f45;
  --brand-tint: #fdf2f7;
  --brand-border: #f0c8dc;

  --bg: #faf8f7;
  --surface: #ffffff;
  --surface-2: #f4f1ef;
  --border: #e6e0dd;
  --text: #241c20;
  --text-2: #6b5f66;
  --text-3: #9c9098;

  --user-bubble: #882756;
  --user-text: #ffffff;
  --coach-bubble: #ffffff;

  --danger: #b3261e;
  --danger-tint: #fdf0ef;
  --danger-border: #f3c9c6;
  --warn: #8a5a00;
  --warn-tint: #fff8e8;
  --warn-border: #f0ddb0;
  --ok: #1f6b4d;

  --radius: 12px;
  --shadow: 0 1px 3px rgba(36, 28, 32, .07), 0 8px 24px rgba(36, 28, 32, .05);

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #e091b4;
    --brand-dark: #c76d96;
    --brand-tint: #2e1522;
    --brand-border: #4d2338;

    --bg: #161214;
    --surface: #211b1e;
    --surface-2: #2a2227;
    --border: #3a3035;
    --text: #f2ecef;
    --text-2: #b3a7ad;
    --text-3: #7d7278;

    --user-bubble: #882756;
    --user-text: #ffffff;
    --coach-bubble: #2a2227;

    --danger: #f2b8b5;
    --danger-tint: #3a1c1a;
    --danger-border: #5c2b28;
    --warn: #f0cf8a;
    --warn-tint: #352a14;
    --warn-border: #54441f;
    --ok: #7fcfaa;

    --shadow: 0 1px 3px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .22);

    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --brand: #e091b4;
  --brand-dark: #c76d96;
  --brand-tint: #2e1522;
  --brand-border: #4d2338;

  --bg: #161214;
  --surface: #211b1e;
  --surface-2: #2a2227;
  --border: #3a3035;
  --text: #f2ecef;
  --text-2: #b3a7ad;
  --text-3: #7d7278;

  --user-bubble: #882756;
  --user-text: #ffffff;
  --coach-bubble: #2a2227;

  --danger: #f2b8b5;
  --danger-tint: #3a1c1a;
  --danger-border: #5c2b28;
  --warn: #f0cf8a;
  --warn-tint: #352a14;
  --warn-border: #54441f;
  --ok: #7fcfaa;

  --shadow: 0 1px 3px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .22);

  color-scheme: dark;
}

* { box-sizing: border-box; }

/* The screens are toggled with el.hidden, and .auth-screen/.chat-screen set
   display, which outranks the browser's own [hidden] rule. Without this every
   screen renders at once, stacked. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow-x: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.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;
}

.screen { height: 100%; }

/* ── Brand ───────────────────────────────────── */
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 28px;
}

.brand-mark {
  width: 38px; height: 38px;
  flex: none;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -.02em;
}

.brand-mark.sm { width: 32px; height: 32px; font-size: 15px; border-radius: 9px; }

.brand-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.brand-text strong { font-size: 15px; font-weight: 700; }
.brand-text span { font-size: 12px; color: var(--text-2); }

/* ── Auth screens ────────────────────────────── */
/* Flex rather than grid with place-items: a centred grid track sizes to
   max-content, so the card claimed its full max-width and overflowed a narrow
   viewport. Flex resolves the card's width against the space actually left. */
.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

.auth-card {
  width: 100%;
  max-width: 400px;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 28px 24px;
}

.auth-card h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -.02em;
  margin: 0 0 6px;
  text-wrap: balance;
}

.lede {
  font-size: 13.5px;
  color: var(--text-2);
  margin: 0 0 22px;
}

.lede strong { color: var(--text); font-weight: 600; }

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: 6px;
}

input[type=tel], input[type=text], select, textarea {
  font-family: inherit;
  font-size: 16px; /* 16px avoids iOS zoom-on-focus */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  width: 100%;
}

input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
  border-color: var(--brand);
}

.phone-row { display: flex; gap: 8px; }
/* ── Country picker ──────────────────────────── */
.cc-btn {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0 11px;
  cursor: pointer;
}
.cc-btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; border-color: var(--brand); }
.cc-btn svg { color: var(--text-3); }
.cc-flag { font-size: 17px; line-height: 1; }

.cc-sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(20, 14, 17, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.cc-panel {
  width: 100%;
  max-width: 440px;
  max-height: 78vh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  box-shadow: var(--shadow);
  padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 600px) {
  .cc-sheet { align-items: center; }
  .cc-panel { border-radius: 16px; max-height: 70vh; }
}
.cc-head { display: flex; gap: 8px; padding: 14px 14px 10px; border-bottom: 1px solid var(--border); }
.cc-head input { flex: 1; min-width: 0; }
.cc-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; overscroll-behavior: contain; }
.cc-list button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  font: inherit;
  color: var(--text);
  background: none;
  border: none;
  border-radius: 9px;
  padding: 10px;
  text-align: left;
  cursor: pointer;
}
.cc-list button:hover, .cc-list button:focus-visible { background: var(--surface-2); outline: none; }
.cc-list button[aria-selected="true"] { background: var(--brand-tint); }
.cc-list .cc-name { flex: 1; min-width: 0; }
.cc-list .cc-code { color: var(--text-2); font-variant-numeric: tabular-nums; }
.cc-empty { margin: 0; padding: 18px; text-align: center; color: var(--text-2); font-size: 14px; }
.phone-row input { flex: 1; min-width: 0; letter-spacing: .02em; }

.channel { border: none; padding: 0; margin: 18px 0 0; }
.channel legend {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  padding: 0;
  margin-bottom: 7px;
}

.channel-opts { display: flex; gap: 8px; }

.radio {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  margin: 0;
  padding: 9px;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-2);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}

.radio input { accent-color: var(--brand); margin: 0; }
.radio:has(input:checked) {
  background: var(--brand-tint);
  border-color: var(--brand);
  color: var(--brand);
}

.field-error {
  font-size: 12.5px;
  color: var(--danger);
  background: var(--danger-tint);
  border: 1px solid var(--danger-border);
  border-radius: 8px;
  padding: 8px 11px;
  margin: 14px 0 0;
}


/* ── Buttons ─────────────────────────────────── */
.btn-primary {
  width: 100%;
  margin-top: 20px;
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  background: var(--brand);
  border: none;
  border-radius: 10px;
  cursor: pointer;
}

.btn-primary:hover:not(:disabled) { background: var(--brand-dark); }
.btn-primary:disabled { opacity: .55; cursor: not-allowed; }

.btn-back, .btn-link {
  font-family: inherit;
  background: none;
  border: none;
  color: var(--brand);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 0;
}

.btn-back { margin-bottom: 16px; display: block; }

.btn-link {
  display: block;
  width: 100%;
  text-align: center;
  margin-top: 16px;
}

.btn-link:disabled { color: var(--text-3); cursor: default; font-weight: 500; }

.btn-ghost {
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  flex: none;
}

.btn-ghost:hover { color: var(--text); border-color: var(--text-3); }

.spinner {
  width: 15px; height: 15px;
  border: 2px solid rgba(255, 255, 255, .4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2s; }
}

/* ── OTP boxes ───────────────────────────────── */
.otp-row { display: flex; gap: 8px; }

.otp-box {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 21px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  padding: 12px 2px;
  letter-spacing: 0;
}

/* ── Chat screen ─────────────────────────────── */
.chat-screen {
  display: flex;
  flex-direction: column;
  height: 100%;
}

#chatHeader {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 16px;
  padding-top: max(11px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.chat-id { display: flex; align-items: center; gap: 10px; min-width: 0; }
.chat-id-text { display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.chat-id-text strong { font-size: 14.5px; font-weight: 700; }

.conn { font-size: 11.5px; color: var(--text-3); }
.conn.ok { color: var(--ok); }
.conn.err { color: var(--danger); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
}

.center-note {
  margin: auto;
  max-width: 280px;
  text-align: center;
  font-size: 13px;
  color: var(--text-3);
  line-height: 1.6;
}

.center-note strong { display: block; color: var(--text-2); margin-bottom: 4px; font-size: 14px; }

.row { display: flex; flex-direction: column; max-width: 78%; }
.row.user { align-self: flex-end; align-items: flex-end; }
.row.coach { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 13px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.row.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.row.coach .bubble {
  background: var(--coach-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.row.coach.failed .bubble {
  background: var(--danger-tint);
  border-color: var(--danger-border);
  color: var(--danger);
}

.row-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 5px;
  font-size: 11px;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text-2);
  border: 1px solid var(--border);
}

.tag.escalated {
  background: var(--warn-tint);
  color: var(--warn);
  border-color: var(--warn-border);
}

.tag.ref {
  background: var(--brand-tint);
  color: var(--brand);
  border-color: var(--brand-border);
  font-weight: 500;
}

.typing {
  font-size: 13px;
  color: var(--text-3);
  font-style: italic;
  padding: 4px 2px;
}

/* ── Banner (rate limit, offline) ────────────── */
.banner {
  flex: none;
  padding: 9px 16px;
  font-size: 12.5px;
  font-weight: 500;
  text-align: center;
  background: var(--warn-tint);
  color: var(--warn);
  border-top: 1px solid var(--warn-border);
}

.banner.error {
  background: var(--danger-tint);
  color: var(--danger);
  border-top-color: var(--danger-border);
}

/* ── Composer ────────────────────────────────── */
#composer {
  flex: none;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}

.composer-row { display: flex; gap: 9px; align-items: flex-end; }

#msgInput {
  flex: 1;
  min-width: 0;
  resize: none;
  max-height: 132px;
  line-height: 1.5;
  padding: 10px 13px;
}

.btn-send {
  flex: none;
  width: 42px; height: 42px;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--brand);
  border: none;
  border-radius: 10px;
  cursor: pointer;
}

.btn-send:hover:not(:disabled) { background: var(--brand-dark); }
.btn-send:disabled { opacity: .45; cursor: not-allowed; }

.composer-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
  min-height: 15px;
}

.char-count { font-size: 11px; color: var(--text-3); font-variant-numeric: tabular-nums; }
.char-count.near { color: var(--warn); font-weight: 600; }

.disclaimer {
  font-size: 10.5px;
  color: var(--text-3);
  margin-left: auto;
  text-align: right;
}

/* ── Wider screens ───────────────────────────── */
@media (min-width: 721px) {
  .chat-screen {
    max-width: 760px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    background: var(--bg);
  }

  .row { max-width: 68%; }
  #messages { padding: 24px 22px; }
  #chatHeader { padding-left: 22px; padding-right: 22px; }
  #composer { padding-left: 22px; padding-right: 22px; }
}
