/*
 * /doctor — Longevity Coach chat client
 *
 * Web companion to the iOS LongevityDoctorView. Hits the same
 * `longevity-doctor` edge function, scoped to the user's JWT.
 *
 * Layout strategy: full-viewport flex column. Header + scrolling
 * conversation + sticky input bar. Mobile-first; on desktop the
 * conversation pane caps at 760px and centres in the container.
 */

/* ─── Boot state ─────────────────────────────────────────────── */
.doctor-main {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.doctor-boot {
  flex: 1 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 0;
  text-align: center;
}
.boot-spinner {
  width: 32px;
  height: 32px;
  margin: 0 auto 16px;
  border: 3px solid var(--line-strong);
  border-top-color: var(--teal-700);
  border-radius: 50%;
  animation: doctor-spin 0.8s linear infinite;
}
.boot-text {
  color: var(--ink-3);
  font-size: 0.95rem;
  margin: 0;
}
@keyframes doctor-spin { to { transform: rotate(360deg); } }

/* ─── Hidden helper ──────────────────────────────────────────── */
.hidden { display: none !important; }
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0);
  white-space: nowrap; border: 0;
}

/* ─── Sign-in gate ───────────────────────────────────────────── */
.doctor-gate {
  flex: 1 0 auto;
  display: flex;
  align-items: center;
  padding: 116px 0 80px;
}
.gate-card {
  max-width: 520px;
  margin: 0 auto;
  padding: clamp(24px, 4vw, 40px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-md);
  text-align: center;
}
.gate-eyebrow {
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--teal-700);
  font-weight: 700;
  margin-bottom: 0.6rem;
}
#gate-heading {
  font-size: clamp(1.6rem, 3.6vw, 2rem);
  letter-spacing: -0.015em;
  margin: 0 0 0.6rem;
  color: var(--ink);
}
.gate-lede {
  color: var(--ink-3);
  margin: 0 0 1.6rem;
  font-size: 1rem;
  line-height: 1.55;
}

.signin-form {
  display: grid;
  gap: 0.6rem;
  text-align: left;
}
.signin-label {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--ink-2);
}
.signin-form input[type="email"] {
  width: 100%;
  padding: 0.65rem 0.85rem;
  font-size: 1rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--ink);
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
.signin-form input[type="email"]:focus {
  outline: none;
  border-color: var(--teal-500);
  box-shadow: 0 0 0 3px rgba(14, 132, 121, 0.15);
}
.signin-status {
  margin: 0.4rem 0 0;
  font-size: 0.86rem;
  min-height: 1.2em;
  color: var(--ink-3);
}
.signin-status.is-error { color: var(--crimson-500); }
.signin-status.is-success { color: var(--teal-700); }

.gate-foot {
  margin-top: 1.6rem;
  padding-top: 1.2rem;
  border-top: 1px dashed var(--line);
  font-size: 0.86rem;
  color: var(--ink-4);
  line-height: 1.55;
}
.gate-foot a { font-weight: 600; }

/* ─── App shell ──────────────────────────────────────────────── */
.doctor-app {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  padding: 92px 0 24px;
  min-height: 100vh;
}
.doctor-shell {
  display: flex;
  flex-direction: column;
  flex: 1 0 auto;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}

.doctor-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 1rem;
}
.doctor-h1 {
  font-size: 1.5rem;
  letter-spacing: -0.015em;
  margin: 0;
  color: var(--ink);
}
.doctor-sub {
  font-size: 0.84rem;
  color: var(--ink-4);
  margin: 0.1rem 0 0;
}
.doctor-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

/* Cross-tab nav strip — appears between the title and the
 * action buttons. Lets the user jump to /home or /trends
 * without leaving the chat shell. */
.doctor-context {
  display: flex;
  gap: 4px;
  align-items: center;
  margin: 0 0.6rem;
}
.doctor-context-link {
  padding: 6px 12px;
  border-radius: var(--r-full);
  color: var(--ink-3);
  font-size: 0.86rem;
  font-weight: 500;
  text-decoration: none;
  transition: color 160ms ease, background 160ms ease;
}
.doctor-context-link:hover {
  color: var(--ink);
  background: var(--bg-2);
}
@media (max-width: 720px) {
  .doctor-context { margin: 0; flex-wrap: wrap; }
}
.btn-sm {
  padding: 0.4rem 0.8rem !important;
  font-size: 0.84rem !important;
}

/* ─── Conversation pane ──────────────────────────────────────── */
.conversation {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0.8rem 0 1.4rem;
  scrollbar-gutter: stable;
  /* iOS-style smooth scroll */
  -webkit-overflow-scrolling: touch;
}

/* Welcome state */
.welcome-state {
  text-align: center;
  padding: 2rem 1rem 1rem;
}
.welcome-eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--teal-700);
  font-weight: 700;
  margin-bottom: 0.5rem;
}
.welcome-title {
  font-size: clamp(1.4rem, 3vw, 1.8rem);
  letter-spacing: -0.015em;
  margin: 0 0 0.5rem;
  color: var(--ink);
}
.welcome-sub {
  color: var(--ink-3);
  font-size: 0.95rem;
  max-width: 28rem;
  margin: 0 auto 1.4rem;
  line-height: 1.5;
}
.welcome-chips {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
  max-width: 32rem;
  margin: 0 auto;
}
@media (min-width: 540px) {
  .welcome-chips { grid-template-columns: 1fr 1fr; }
}

/* Context card — "Loaded for this conversation" panel under the
   welcome title. Tells the user what the Coach actually has in
   context so they trust the answers are personalised. Each item
   is a compact pill: icon + label + tabular value. */
.context-card {
  max-width: 32rem;
  margin: 0 auto 1.4rem;
  padding: 1rem 1.1rem;
  background: var(--surface, #fff);
  border: 1px solid var(--line, rgba(14,30,30,0.08));
  border-radius: var(--r-md, 14px);
  box-shadow: 0 1px 2px rgba(14,30,30,0.04);
  text-align: left;
}
.context-card[hidden] { display: none; }
.context-card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
  margin: 0 0 0.6rem;
}
.context-card-head h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-3, #4A5757);
  font-weight: 700;
  margin: 0;
}
.context-card-link {
  font-size: 0.78rem;
  color: var(--teal-700, #07564F);
  text-decoration: none;
  font-weight: 600;
}
.context-card-link:hover { text-decoration: underline; }

.context-card-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.4rem 0.5rem;
}
.context-card-item {
  display: grid;
  grid-template-columns: 18px 1fr auto;
  gap: 0.4rem;
  align-items: center;
  padding: 0.45rem 0.6rem;
  background: var(--bg-2, #F3F1EA);
  border-radius: 8px;
}
.context-card-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--teal-700, #07564F);
}
.context-card-label {
  font-size: 0.78rem;
  color: var(--ink-3, #4A5757);
  font-weight: 600;
  letter-spacing: 0.01em;
}
.context-card-value {
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--ink, #0E1E1E);
  font-variant-numeric: tabular-nums;
}
.context-card-empty {
  font-size: 0.92rem;
  color: var(--ink-3, #4A5757);
  line-height: 1.5;
}
.context-card-empty a {
  color: var(--teal-700, #07564F);
  font-weight: 600;
}

.prompt-chip {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  padding: 0.7rem 0.9rem;
  font-size: 0.92rem;
  color: var(--ink-2);
  cursor: pointer;
  text-align: left;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
  font-family: inherit;
  line-height: 1.4;
}
.prompt-chip:hover {
  border-color: var(--teal-500);
  background: var(--teal-50);
  transform: translateY(-1px);
}
.prompt-chip:focus-visible {
  outline: 2px solid var(--teal-500);
  outline-offset: 2px;
}

/* ─── Messages ───────────────────────────────────────────────── */
.messages {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.bubble-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  animation: bubble-in 220ms ease-out;
}
.bubble-row.is-user {
  flex-direction: row-reverse;
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.bubble {
  max-width: min(82%, 580px);
  padding: 0.7rem 0.95rem;
  border-radius: 18px;
  font-size: 0.98rem;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.bubble.is-coach {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
}
.bubble.is-user {
  background: var(--teal-700);
  color: white;
  border-bottom-right-radius: 6px;
}

.bubble.is-crisis {
  border: 1px solid var(--crimson-500);
  background: #FFF5F4;
  color: var(--ink);
}
.bubble .crisis-resources {
  margin-top: 0.7rem;
  padding-top: 0.6rem;
  border-top: 1px dashed rgba(201, 52, 58, 0.3);
  display: grid;
  gap: 0.3rem;
}
.crisis-resource-row {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  font-size: 0.9rem;
}
.crisis-resource-row a {
  color: var(--crimson-500);
  font-weight: 600;
}

/* Markdown bits in coach bubbles */
.bubble.is-coach strong { font-weight: 600; }
.bubble.is-coach em { font-style: italic; }
.bubble.is-coach a {
  color: var(--teal-700);
  text-decoration: underline;
}
.bubble.is-coach ul,
.bubble.is-coach ol {
  margin: 0.4rem 0 0.2rem;
  padding-left: 1.2rem;
}
.bubble.is-coach li { margin: 0.15rem 0; }
.bubble.is-coach code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: var(--bg-2);
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
}

/* ─── Typing indicator ───────────────────────────────────────── */
.typing {
  margin-top: 0.4rem;
  padding-left: 0;
}
.typing-bubble {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  border-bottom-left-radius: 6px;
  padding: 0.7rem 1rem;
  display: inline-flex;
  gap: 0.3rem;
  align-items: center;
}
.typing-dot {
  width: 7px;
  height: 7px;
  background: var(--ink-4);
  border-radius: 50%;
  animation: typing-pulse 1.2s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.30s; }
@keyframes typing-pulse {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ─── Input bar ──────────────────────────────────────────────── */
.doctor-input-bar {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  padding: 0.7rem 0.7rem 0.7rem 1rem;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  position: sticky;
  bottom: 12px;
  margin-top: 0.6rem;
}
.doctor-input-bar:focus-within {
  border-color: var(--teal-500);
  box-shadow: 0 0 0 3px rgba(14, 132, 121, 0.12);
}

#doctor-input {
  flex: 1 1 auto;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 1rem;
  font-family: inherit;
  resize: none;
  max-height: 180px;
  min-height: 1.4em;
  line-height: 1.4;
  padding: 0.4rem 0;
  color: var(--ink);
}
#doctor-input::placeholder { color: var(--ink-4); }

.btn-send {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 0;
  background: var(--teal-700);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 120ms ease, transform 120ms ease;
}
.btn-send:hover:not(:disabled) {
  background: var(--teal-800);
  transform: translateY(-1px);
}
.btn-send:disabled {
  background: var(--line-strong);
  cursor: not-allowed;
  transform: none;
}
.btn-send:focus-visible {
  outline: 2px solid var(--teal-500);
  outline-offset: 2px;
}

.doctor-disclaimer {
  font-size: 0.78rem;
  color: var(--ink-4);
  text-align: center;
  margin: 0.7rem 0 0;
  line-height: 1.45;
}

/* ─── Error toast ────────────────────────────────────────────── */
.error-toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: white;
  padding: 0.7rem 1.1rem;
  border-radius: var(--r-md);
  font-size: 0.9rem;
  box-shadow: var(--shadow-lg);
  z-index: 999;
  max-width: 90vw;
  animation: toast-in 220ms ease-out;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ─── Mobile tweaks ──────────────────────────────────────────── */
@media (max-width: 640px) {
  .doctor-app { padding: 80px 0 16px; }
  .doctor-h1 { font-size: 1.25rem; }
  .doctor-sub { font-size: 0.78rem; }
  .doctor-actions .btn-sm { padding: 0.35rem 0.7rem !important; font-size: 0.8rem !important; }
  .bubble { max-width: 88%; font-size: 0.96rem; }
  .doctor-input-bar { bottom: 8px; }
}
