/* Frodo – patient pages (login + chat).
   When you change this file, bump ?v= in index.html/chat.html and CACHE_NAME in sw.js. */

/* ── reset & base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  /* Text and filled buttons use --accent (6.0:1 with white). The lighter
     #6C63FF is decorative only (borders/dots), never text. */
  --accent:        #574FD6;
  --accent-strong: #463EC0;
  --accent-soft:   #6C63FF;
  --accent-light:  #EAE9FF;
  --bot-bg:        #FFFFFF;
  --bot-text:      #2D2D3A;
  --user-bg:       #574FD6;
  --user-text:     #FFFFFF;
  --muted:         #5F5B73;   /* 6.5:1 on white */
  --border:        #D9D6F5;
  --danger:        #B3261E;   /* 6.5:1 on white */
  --page-bg:       #F0EEFF;
  --page-gradient: linear-gradient(160deg, #F0EEFF 0%, #E8F4FF 100%);
  --shadow:        0 2px 12px rgba(87, 79, 214, .15);
  --radius:        18px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif,
          "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji";
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
}

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  color: var(--bot-text);
  background-color: var(--page-bg);
  background-image: var(--page-gradient);
  background-attachment: fixed;
}

button, input, textarea, select { font-family: inherit; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

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

.btn-primary {
  display: inline-flex; align-items: center; justify-content: center;
  width: 100%; min-height: 52px; padding: 14px;
  background: var(--accent);
  color: #fff; border: none;
  border-radius: var(--radius);
  font-size: 1.1rem; font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: background .2s, transform .1s;
}
.btn-primary:hover { background: var(--accent-strong); }
.btn-primary:active { transform: scale(.97); }
.btn-primary:disabled { opacity: .7; cursor: wait; }

/* ── LOGIN PAGE ────────────────────────────────────────────────── */
.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(16px + var(--sat)) calc(16px + var(--sar)) calc(16px + var(--sab)) calc(16px + var(--sal));
}

.login-card {
  background: #fff;
  border-radius: 28px;
  padding: 40px 32px;
  max-width: 400px; width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
}

.frodo-logo { margin-bottom: 12px; }
.frodo-logo img { display: block; margin: 0 auto; }

.login-card h1 { font-size: 1.7rem; color: var(--accent); margin-bottom: 8px; }

.subtitle { color: var(--muted); margin-bottom: 28px; font-size: 1rem; }

.login-card label {
  display: block; text-align: left;
  font-weight: 600; color: #3A3848; margin-bottom: 8px;
}

.login-card input[type="text"],
.login-card input[type="password"] {
  width: 100%; padding: 14px 18px;
  border: 2px solid #C9C6E8;
  border-radius: var(--radius);
  font-size: 1.1rem;            /* ≥ 16px: no iOS zoom on focus */
  color: var(--bot-text);
  background: #fff;
  outline: none;
  transition: border-color .2s;
  margin-bottom: 20px;
}
.login-card input:focus { border-color: var(--accent); }
.login-card input::placeholder { color: #6B6880; opacity: 1; }

.link-btn {
  display: block;
  margin: -6px 0 18px;
  padding: 8px 0;
  min-height: 44px;
  background: none; border: none;
  color: var(--accent);
  font-size: 1rem; font-weight: 600;
  text-decoration: underline; text-underline-offset: 3px;
  cursor: pointer;
  text-align: left;
}

.enrollment-block { text-align: left; }
.field-hint { font-size: .9rem; color: var(--muted); margin: -2px 0 10px; line-height: 1.4; }

.login-error {
  color: var(--danger);
  font-size: .95rem;
  line-height: 1.4;
  margin-bottom: 14px;
  text-align: center;
}

/* ── CHAT PAGE LAYOUT ──────────────────────────────────────────── */
body.chat-page { overflow: hidden; overscroll-behavior: none; }

.app {
  position: fixed;
  left: 0; right: 0; top: 0;
  height: 100vh;
  height: 100dvh;
  display: flex; flex-direction: column;
  overflow: hidden;
}
/* JS pins the app to the visual viewport so the input stays above the
   on-screen keyboard (iOS ignores interactive-widget=resizes-content). */
.app.vv-sized {
  top: var(--app-top, 0px);
  height: var(--app-h, 100vh);
}

/* ── top bar ───────────────────────────────────────────────────── */
.topbar {
  position: relative;
  z-index: 10;
  display: flex; align-items: center; gap: 8px;
  min-height: 64px;
  padding: calc(8px + var(--sat)) calc(10px + var(--sar)) 8px calc(10px + var(--sal));
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .06);
  flex-shrink: 0;
}

.topbar-back {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 40px;
  flex-shrink: 0;
  font-size: 1.4rem; color: var(--accent);
  text-decoration: none;
  border-radius: 10px;
}
.topbar-back:hover { background: var(--accent-light); }

.topbar-avatar { flex-shrink: 0; }
.topbar-avatar img { display: block; border-radius: 50%; }

.topbar-info {
  flex: 1 1 auto; min-width: 0;
  display: flex; flex-direction: column; gap: 2px;
}
.topbar-name { font-weight: 700; font-size: 1.05rem; color: var(--bot-text); line-height: 1.2; }
.topbar-progress { font-size: .78rem; color: var(--muted); line-height: 1.25; }
.topbar-progress:empty { display: none; }

.progress-dots { display: inline-flex; gap: 4px; margin-top: 2px; }
.progress-dots .dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: transparent;
}
.progress-dots .dot.is-done { background: var(--accent); }
.progress-dots .dot.is-today { box-shadow: 0 0 0 2px #fff, 0 0 0 3.5px var(--accent-soft); }

.done-btn {
  flex-shrink: 0;
  max-width: 96px;
  min-height: 44px;
  padding: 5px 10px;
  border: 2px solid var(--accent);
  border-radius: 14px;
  background: #fff;
  color: var(--accent);
  font-size: .8rem; font-weight: 700; line-height: 1.15;
  cursor: pointer;
  touch-action: manipulation;
}
.done-btn:hover:not(:disabled) { background: var(--accent-light); }
.done-btn:disabled { opacity: .5; cursor: default; }

.menu-wrap { position: relative; flex-shrink: 0; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px;
  border: none; border-radius: 12px;
  background: transparent;
  color: var(--bot-text);
  font-size: 1.5rem; font-weight: 700;
  cursor: pointer;
}
.icon-btn:hover, .icon-btn[aria-expanded="true"] { background: var(--accent-light); }

.menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  z-index: 30;
  min-width: 220px; max-width: min(300px, calc(100vw - 24px));
  padding: 6px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 28px rgba(45, 45, 58, .2);
}
.menu-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; min-height: 48px;
  padding: 10px 12px;
  border: none; border-radius: 10px;
  background: none;
  color: var(--bot-text);
  font-size: 1rem; line-height: 1.3; text-align: left;
  text-decoration: none;
  cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--accent-light); }
.menu-icon { font-size: 1.15rem; flex-shrink: 0; }

/* Trial-mode week selector (clinician demo only) */
.trial-bar {
  display: flex; align-items: center; justify-content: center; gap: .5rem;
  padding: 6px calc(12px + var(--sar)) 6px calc(12px + var(--sal));
  background: linear-gradient(135deg, #FFF4D6 0%, #FFE5A8 100%);
  border-bottom: 1px solid #E6C56A;
  font-size: .85rem;
  color: #5C4400;
  flex-shrink: 0;
}
.trial-label { font-weight: 600; }
.trial-week-select {
  min-height: 32px;
  border: 1px solid #A8892A;
  border-radius: 6px;
  background: #fff;
  font-size: 1rem;
  padding: .1rem .4rem;
  color: var(--bot-text);
}
.trial-week-select:disabled { opacity: .5; cursor: wait; }

/* ── messages scroll area ──────────────────────────────────────── */
.messages {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px calc(12px + var(--sar)) 8px calc(12px + var(--sal));
  display: flex; flex-direction: column; gap: 10px;
  scroll-behavior: smooth;
}
.transcript { display: flex; flex-direction: column; gap: 10px; }
.transcript:empty { display: none; }

/* ── bubbles ───────────────────────────────────────────────────── */
.bubble-wrap {
  display: flex; align-items: flex-end; gap: 8px;
  max-width: 85%;
}
.bot-wrap  { align-self: flex-start; }
.user-wrap { align-self: flex-end; flex-direction: row-reverse; }

.avatar { width: 32px; height: 32px; flex-shrink: 0; margin-bottom: 2px; }
.avatar img { border-radius: 50%; display: block; }

.bubble {
  min-width: 0;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 1rem; line-height: 1.55;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .08);
}
.bubble p + p, .bubble p + ul, .bubble ul + p { margin-top: .55em; }
.bubble ul { padding-left: 1.25em; }
.bubble li + li { margin-top: .2em; }

.bot-bubble {
  background: var(--bot-bg);
  color: var(--bot-text);
  border-bottom-left-radius: 4px;
}
.user-bubble {
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.user-wrap.is-failed .user-bubble {
  background: #fff;
  color: var(--bot-text);
  outline: 2px dashed var(--danger);
  outline-offset: -2px;
}

.retry-row {
  align-self: center;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  max-width: 420px;
  padding: 4px 8px;
  text-align: center;
}
.retry-msg { color: var(--danger); font-size: .95rem; line-height: 1.4; }

/* ── widgets (check-in questions, medication flow) ─────────────── */
.widget {
  align-self: stretch;
  width: 100%; max-width: 620px;
  padding: 12px;
  background: #fff;
  border: 2px solid var(--accent-light);
  border-radius: var(--radius);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .06);
}
.widget-counter { font-size: .8rem; font-weight: 700; color: var(--muted); margin-bottom: 8px; }
.widget-title { font-weight: 700; font-size: 1rem; margin-bottom: 10px; line-height: 1.35; }
.widget-hint { font-size: .95rem; color: var(--muted); margin-bottom: 10px; line-height: 1.4; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-row + .chip-row, .scale-grid + .chip-row { margin-top: 10px; }

.chip {
  min-height: 48px; min-width: 48px;
  padding: 10px 16px;
  border: 2px solid var(--accent-soft);
  border-radius: 14px;
  background: var(--accent-light);
  color: var(--accent-strong);
  font-size: 1rem; font-weight: 700; line-height: 1.2;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .1s, background .15s;
}
.chip:hover:not(:disabled) { background: #DCDAFF; }
.chip:active:not(:disabled) { transform: scale(.95); }
.chip:disabled { opacity: .5; cursor: default; }
.chip.is-selected, .chip.is-selected:disabled { background: var(--accent); border-color: var(--accent); color: #fff; opacity: 1; }
.chip-big { flex: 1 1 0; font-size: 1.1rem; }
.chip-secondary { background: #fff; color: var(--accent-strong); }
.chip-decline {
  background: #fff;
  border-color: #B9B5D6;
  color: var(--muted);
  font-weight: 600; font-size: .95rem;
}
.chip-retry { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip-retry:hover:not(:disabled) { background: var(--accent-strong); }

.scale-grid { display: grid; column-gap: 6px; row-gap: 4px; align-items: start; }
.chip-scale { padding: 0; min-width: 0; width: 100%; font-size: 1.15rem; }
.scale-anchor { font-size: .78rem; line-height: 1.2; color: var(--muted); padding-bottom: 6px; }
.scale-anchor.al-left { text-align: left; }
.scale-anchor.al-right { text-align: right; }
.scale-anchor.al-center { text-align: center; }

/* ── typing indicator ──────────────────────────────────────────── */
.typing-indicator {
  display: flex; align-items: center; gap: 10px;
  padding: 4px calc(16px + var(--sar)) 6px calc(16px + var(--sal));
  flex-shrink: 0;
  font-size: .88rem; color: var(--muted);
}
.typing-indicator:not(.is-active) { height: 0; padding-top: 0; padding-bottom: 0; overflow: hidden; }

.typing-dots { display: inline-flex; gap: 5px; }
.typing-dots span {
  width: 9px; height: 9px;
  background: var(--accent-soft);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
  opacity: .8;
}
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-6px); }
}

/* ── quick-action buttons: 3 columns, icon above a 2-line label ─── */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 6px calc(12px + var(--sar)) 8px calc(12px + var(--sal));
  flex-shrink: 0;
}
.app.kb-open .quick-actions { display: none; }

.quick-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-width: 0; min-height: 64px;
  padding: 6px 2px;
  border: 2px solid transparent;
  border-radius: 16px;
  font-size: clamp(.75rem, 3.3vw, .9rem); font-weight: 700; line-height: 1.15;
  text-align: center;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .1s, opacity .15s;
}
.quick-btn:active:not(:disabled) { transform: scale(.95); }
.quick-btn:disabled { opacity: .5; cursor: default; }
.quick-icon { font-size: 1.35rem; line-height: 1.2; }
.quick-label {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: break-word;
}

.quick-btn.headache   { background: #FFF0F0; color: #A8281D; border-color: #F5AAAA; }
.quick-btn.stress     { background: #FFF6E8; color: #8F3A00; border-color: #F5C97A; }
.quick-btn.medication { background: #EAF9F0; color: #146C40; border-color: #91D5AA; }

/* ── input bar ─────────────────────────────────────────────────── */
.input-bar {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 10px calc(12px + var(--sar)) calc(10px + var(--sab)) calc(12px + var(--sal));
  background: #fff;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, .06);
  flex-shrink: 0;
}
.app.kb-open .input-bar { padding-bottom: 10px; }

.msg-input {
  flex: 1; min-width: 0;
  resize: none;
  border: 2px solid #C9C6E8;
  border-radius: var(--radius);
  padding: 11px 16px;
  font-size: max(16px, 1rem);   /* ≥ 16px: no iOS zoom on focus */
  color: var(--bot-text);
  background: #fff;
  outline: none;
  transition: border-color .2s;
  max-height: 120px;
  line-height: 1.45;
}
.msg-input:focus { border-color: var(--accent); }
.msg-input::placeholder { color: #6B6880; opacity: 1; }
.msg-input:disabled { background: #F4F4F7; }

.send-btn {
  width: 48px; height: 48px;
  flex-shrink: 0;
  background: var(--accent);
  color: #fff;
  border: none; border-radius: 50%;
  font-size: 1.2rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  touch-action: manipulation;
  transition: background .2s, transform .1s;
}
.send-btn:hover:not(:disabled) { background: var(--accent-strong); }
.send-btn:active:not(:disabled) { transform: scale(.9); }
.send-btn:disabled { background: #B9B6CF; cursor: default; }

.end-bar {
  display: flex; justify-content: center;
  padding: 12px calc(16px + var(--sar)) calc(14px + var(--sab)) calc(16px + var(--sal));
  background: #fff;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, .06);
  flex-shrink: 0;
}
.end-bar .btn-primary { max-width: 360px; }

/* ── onboarding overlay ────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0;
  z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: calc(16px + var(--sat)) calc(16px + var(--sar)) calc(16px + var(--sab)) calc(16px + var(--sal));
  background: rgba(45, 45, 58, .5);
}
.overlay-card {
  width: 100%; max-width: 420px;
  max-height: 100%;
  overflow-y: auto;
  padding: 24px 22px;
  background: #fff;
  border-radius: 24px;
  box-shadow: var(--shadow);
  text-align: center;
}
.overlay-avatar { display: block; margin: 0 auto 10px; }
.overlay-card h2 { font-size: 1.35rem; color: var(--accent); margin-bottom: 12px; }
.onboarding-body { text-align: left; font-size: 1rem; line-height: 1.55; margin-bottom: 20px; }
.onboarding-body p + p { margin-top: .7em; }

/* ── toast & noscript ──────────────────────────────────────────── */
.toast {
  position: fixed;
  z-index: 60;
  left: 50%; top: calc(12px + var(--sat));
  transform: translateX(-50%);
  width: max-content; max-width: calc(100vw - 32px);
  padding: 12px 18px;
  border-radius: 14px;
  background: #2D2D3A; color: #fff;
  font-size: .95rem; line-height: 1.4;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.noscript {
  position: fixed; left: 16px; right: 16px; bottom: 16px; z-index: 70;
  padding: 14px; border-radius: 14px;
  background: #fff; color: var(--danger); text-align: center;
}

/* ── responsive ────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .bubble-wrap { max-width: 92%; }
  .login-card  { padding: 32px 22px; }
}
/* The back arrow only leads to "/" (which returns here); on phones its
   space goes to the progress line. */
@media (max-width: 399px) {
  .topbar-back { display: none; }
  .topbar { padding-left: calc(12px + var(--sal)); }
}
@media (max-width: 339px) {
  .topbar-avatar { display: none; }
}

/* ── PWA standalone mode ──────────────────────────────────────── */
@media (display-mode: standalone) {
  /* No browser chrome to go back to. */
  .topbar-back { display: none; }
}

/* ── reduced motion ───────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
  .typing-dots span { animation: none; opacity: 1; }
}
