:root {
  --bg: #f6efe6;
  --paper: #fffaf4;
  --white: #fffdf9;
  --ink: #342d28;
  --muted: #8a7c70;
  --brand: #b86b53;
  --brand-dark: #8d4f3b;
  --brand-soft: #f2ded4;
  --green: #55705f;
  --line: #eadfd3;
  --shadow: 0 12px 40px rgba(75, 53, 38, 0.08);
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  color: var(--ink);
  background:
    radial-gradient(900px 500px at 85% -10%, rgba(184, 107, 83, 0.12), transparent 60%),
    radial-gradient(700px 500px at -10% 100%, rgba(85, 112, 95, 0.10), transparent 60%),
    linear-gradient(160deg, #f8f2e9 0%, #f2e9dd 100%);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
a { color: var(--brand-dark); }
[hidden] { display: none !important; }

/* ---------- chat page ---------- */

.chat-page { min-height: 100vh; min-height: 100dvh; }

.app-shell {
  width: 100%;
  max-width: 760px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: rgba(255, 250, 244, 0.94);
  box-shadow: var(--shadow);
}

@media (min-width: 760px) {
  .chat-page { padding: 24px 0; }
  .app-shell {
    min-height: calc(100vh - 48px);
    min-height: calc(100dvh - 48px);
    border: 1px solid var(--line);
    border-radius: 28px;
    overflow: hidden;
  }
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) 18px 14px;
  background: rgba(255, 250, 244, 0.88);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }

.avatar {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #c9826a, #a85b44);
  color: #fff;
  font-size: 19px;
  font-weight: 600;
  box-shadow: 0 6px 16px rgba(168, 91, 68, 0.22);
}

.avatar.large { width: 58px; height: 58px; font-size: 24px; margin: 0 auto 14px; }

.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status {
  margin: 3px 0 0;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 11.5px;
}
.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #6d9b7d;
  box-shadow: 0 0 0 3px rgba(109, 155, 125, 0.15);
}

.ghost-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--brand);
  color: var(--brand-dark);
  background: transparent;
  padding: 8px 13px;
  border-radius: 999px;
  font-size: 13px;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.2s ease, color 0.2s ease;
}
.ghost-btn:hover { background: var(--brand); color: #fff; }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.day-divider {
  align-self: center;
  padding: 4px 11px;
  border-radius: 999px;
  background: rgba(138, 124, 112, 0.08);
  color: var(--muted);
  font-size: 11px;
}

.msg { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; }
.msg.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--brand-soft);
  color: var(--brand-dark);
  font-size: 12.5px;
  font-weight: 600;
}

.bubble {
  max-width: min(82%, 560px);
  padding: 12px 14px;
  border-radius: 18px 18px 18px 6px;
  background: #fff;
  border: 1px solid var(--line);
  box-shadow: 0 2px 10px rgba(75, 53, 38, 0.045);
  font-size: 15px;
  line-height: 1.68;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.user .bubble {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  border-radius: 18px 18px 6px 18px;
  box-shadow: none;
}

.bubble.error { border-color: #d9a49a; color: #8f3e31; background: #fff7f4; }

.typing { display: inline-flex; gap: 4px; padding: 5px 2px; }
.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: 0.18s; }
.typing i:nth-child(3) { animation-delay: 0.36s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

.quick-wrap { display: flex; flex-wrap: wrap; gap: 8px; padding: 2px 0 4px 40px; }

.chip {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--brand-dark);
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 13px;
  line-height: 1.2;
  transition: border-color 0.2s ease, transform 0.2s ease;
}
.chip:hover { border-color: var(--brand); transform: translateY(-1px); }

.cta-card {
  margin: 0 14px 4px;
  padding: 13px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: linear-gradient(135deg, #fff8f2, #f6e6da);
  border: 1px solid #efd6c6;
  border-radius: 16px;
}
.cta-card strong { font-size: 13.5px; }
.cta-card p { margin: 3px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }

.primary-btn {
  border: 0;
  border-radius: 999px;
  background: var(--brand);
  color: #fff;
  padding: 12px 18px;
  font-weight: 600;
  font-size: 14.5px;
  box-shadow: 0 8px 20px rgba(184, 107, 83, 0.18);
  transition: transform 0.15s ease, background 0.2s ease;
}
.primary-btn:hover { background: var(--brand-dark); }
.primary-btn:active { transform: scale(0.98); }
.primary-btn.small { padding: 9px 14px; font-size: 13px; flex: 0 0 auto; }
.primary-btn:disabled { opacity: 0.55; cursor: default; }

.composer {
  position: sticky;
  bottom: 0;
  z-index: 4;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: rgba(255, 250, 244, 0.94);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  min-height: 42px;
  max-height: 120px;
  resize: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 20px;
  padding: 11px 14px;
  font-size: 15px;
  line-height: 1.45;
  color: var(--ink);
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.composer textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(184, 107, 83, 0.12);
}

.composer button {
  height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: 21px;
  background: var(--brand);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  transition: background 0.2s ease;
}
.composer button:hover { background: var(--brand-dark); }
.composer button:disabled { opacity: 0.5; cursor: default; }

.disclaimer {
  margin: 0;
  padding: 0 18px calc(10px + env(safe-area-inset-bottom));
  background: rgba(255, 250, 244, 0.94);
  color: #a29488;
  font-size: 10.5px;
  line-height: 1.5;
  text-align: center;
}
.disclaimer a { color: inherit; text-decoration: underline; }

/* ---------- modal & forms ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(45, 36, 30, 0.45);
  backdrop-filter: blur(4px);
}

.modal-card {
  position: relative;
  width: min(440px, 100%);
  max-height: 90vh;
  max-height: 90dvh;
  overflow-y: auto;
  padding: 24px 22px;
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 0 30px 80px rgba(45, 36, 30, 0.25);
}
.modal-card.wide { width: min(720px, 100%); }

.modal-close {
  position: absolute;
  top: 8px;
  right: 12px;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 26px;
  line-height: 1;
}
.modal-close:hover { background: rgba(138, 124, 112, 0.1); }

.eyebrow {
  margin: 0 0 6px;
  color: var(--brand);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 1.4px;
  text-transform: uppercase;
}
.modal-card h2 { margin: 0 0 8px; font-size: 23px; letter-spacing: 0.3px; }
.modal-sub { margin: 0 0 6px; color: var(--muted); font-size: 13px; line-height: 1.65; }

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 13px 0;
  color: var(--muted);
  font-size: 12.5px;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 11px 12px;
  color: var(--ink);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(184, 107, 83, 0.12);
}
textarea { resize: vertical; min-height: 70px; }

.consent {
  flex-direction: row;
  align-items: flex-start;
  gap: 8px;
  color: var(--muted);
  font-size: 11.5px;
  line-height: 1.55;
}
.consent input { width: 16px; height: 16px; flex: 0 0 auto; margin-top: 2px; accent-color: var(--brand); }

.form-error { margin: 10px 0 0; color: #a63f31; font-size: 12.5px; line-height: 1.5; }

.qr {
  display: block;
  width: min(240px, 70vw);
  height: auto;
  margin: 16px auto 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
}
.qr-tip { margin: 8px 0 0; color: var(--muted); font-size: 11.5px; text-align: center; line-height: 1.5; }

/* ---------- document pages ---------- */

.doc-page { min-height: 100vh; min-height: 100dvh; padding: 24px 16px 60px; }
.doc {
  max-width: 760px;
  margin: 0 auto;
  padding: clamp(22px, 4vw, 40px);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
}
.back-link { display: inline-block; margin-bottom: 18px; font-size: 13px; text-decoration: none; }
.doc h1 { margin: 0 0 8px; font-size: 28px; }
.doc h2 { margin: 26px 0 8px; font-size: 17px; }
.doc p, .doc li { color: #5c5149; font-size: 14px; line-height: 1.85; }
.doc ul { padding-left: 20px; margin: 8px 0; }
.muted { color: var(--muted); }
.doc-note {
  margin-top: 30px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #fbf3ec;
  color: var(--muted);
  font-size: 12px;
}

/* ---------- admin ---------- */

.admin-page { min-height: 100vh; min-height: 100dvh; background: #f4f1ec; }

.login-wrap { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login-card {
  width: min(380px, 100%);
  padding: 30px 26px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-card h1 { margin: 0 0 6px; font-size: 20px; }
.login-card .muted { margin: 0 0 18px; font-size: 12.5px; }
.login-card label { text-align: left; }
.login-card .primary-btn { width: 100%; margin-top: 8px; }

.admin-topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: 22px clamp(16px, 4vw, 40px);
  background: var(--white);
  border-bottom: 1px solid var(--line);
}
.admin-topbar h1 { margin: 2px 0 0; font-size: 21px; }
.admin-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.admin-main { max-width: 1200px; margin: 0 auto; padding: 22px clamp(16px, 4vw, 40px) 60px; }

.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.stat-card {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 16px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
}
.stat-card span { color: var(--muted); font-size: 12px; }
.stat-card strong { font-size: 27px; font-weight: 650; }
.stat-card.alert strong { color: #b34a3c; }

.tabs { display: flex; gap: 8px; margin: 22px 0 12px; }
.tab {
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 13px;
}
.tab.active { background: var(--brand); border-color: var(--brand); color: #fff; }

.panel { background: #fff; border: 1px solid var(--line); border-radius: 18px; overflow: hidden; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 12px 14px; border-bottom: 1px solid #f1ebe4; text-align: left; vertical-align: top; }
th { background: #fbf7f2; color: var(--muted); font-weight: 600; white-space: nowrap; }
td { color: var(--ink); }
tbody tr:last-child td { border-bottom: 0; }
.empty { padding: 34px !important; text-align: center; color: var(--muted); }
.crisis-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  background: #fbe4e0;
  color: #a63f31;
  font-size: 11px;
}
.link-btn { border: 0; background: transparent; color: var(--brand-dark); padding: 0; font-size: 13px; text-decoration: underline; }

.transcript { display: flex; flex-direction: column; gap: 12px; margin-top: 14px; }
.transcript-item { padding: 10px 12px; border-radius: 12px; background: #fbf7f2; }
.transcript-item.user { background: #f7e7df; }
.transcript-item.crisis { border: 1px solid #e3a79c; background: #fff5f2; }
.transcript-meta { margin-bottom: 4px; color: var(--muted); font-size: 10.5px; }
.transcript-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; line-height: 1.65; }

@media (max-width: 720px) {
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .admin-topbar { flex-direction: column; align-items: flex-start; }
  .quick-wrap { padding-left: 0; }
  .cta-card { align-items: flex-start; }
}
