:root {
  --brand: #1d4ed8;
  --brand-d: #1e3a8a;
  --accent: #f97316;
  --bg: #eef2f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --bubble-in: #f1f5f9;
  --bubble-ai: #eff6ff;
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220; --card: #111a2e; --text: #e2e8f0; --muted: #94a3b8; --line: #1f2a44;
    --bubble-in: #1a2540; --bubble-ai: #172554;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }

.shell {
  max-width: 560px;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--card);
  box-shadow: 0 0 40px rgba(15, 23, 42, .08);
}
@media (min-width: 600px) {
  body { padding: 24px 0; }
  .shell { height: calc(100dvh - 48px); border-radius: 24px; overflow: hidden; }
}

/* ---- Header ---- */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px;
  background: linear-gradient(135deg, var(--brand), var(--brand-d));
  color: #fff;
  padding-top: max(12px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand strong { display: block; font-size: 15px; }
.brand small { display: block; font-size: 12px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand__logo {
  flex: none; width: 42px; height: 42px; border-radius: 12px; background: #fff; color: var(--brand);
  font-weight: 700; font-size: 12px; display: grid; place-items: center; letter-spacing: .5px;
}
.online { display: flex; align-items: center; gap: 6px; font-size: 12px; flex: none; }
.online .avs { display: flex; }
.online .avs img, .online .avs span {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid #fff; margin-left: -8px; object-fit: cover;
  background: #93c5fd; color: var(--brand-d); display: grid; place-items: center; font-size: 11px; font-weight: 700;
}
.online .dot { width: 8px; height: 8px; border-radius: 50%; background: #4ade80; box-shadow: 0 0 0 3px rgba(74,222,128,.3); }

/* ---- Form bắt đầu ---- */
.start { flex: 1; overflow-y: auto; padding: 20px 20px 28px; }
.start__hero { text-align: center; margin-bottom: 18px; }
.start__hero h1 { margin: 8px 0 4px; font-size: 22px; }
.start__hero p { margin: 0; color: var(--muted); }
.form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; border: 0; padding: 0; margin: 0; }
.field > span, .field legend { font-weight: 600; font-size: 14px; padding: 0; margin-bottom: 6px; }
.field b { color: #dc2626; }
input, textarea {
  font: inherit; color: var(--text); background: var(--card);
  border: 1.5px solid var(--line); border-radius: 12px; padding: 12px 14px; width: 100%;
  font-size: 16px; /* tránh iOS tự zoom */
}
input:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(29,78,216,.15); }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; }
.seg span {
  display: block; text-align: center; padding: 12px; border: 1.5px solid var(--line); border-radius: 12px;
  cursor: pointer; font-weight: 500;
}
.seg input:checked + span { border-color: var(--brand); background: rgba(29,78,216,.08); color: var(--brand); font-weight: 600; }
.seg input:focus-visible + span { box-shadow: 0 0 0 3px rgba(29,78,216,.25); }
.more summary { cursor: pointer; color: var(--brand); font-weight: 500; font-size: 14px; margin-bottom: 10px; }
.more[open] { display: grid; gap: 12px; }
.consent {
  display: flex; gap: 10px; align-items: flex-start; font-size: 13px; line-height: 1.5; color: var(--muted);
  background: var(--bubble-in); padding: 12px; border-radius: 12px;
}
.consent input { width: 18px; height: 18px; margin-top: 2px; flex: none; accent-color: var(--brand); }
.consent strong { color: var(--text); font-weight: 600; }
.error { margin: 0; color: #dc2626; font-size: 14px; }
.btn {
  font: inherit; font-weight: 600; font-size: 16px; border: 0; border-radius: 14px; padding: 14px;
  background: var(--accent); color: #fff; cursor: pointer;
}
.btn:disabled { opacity: .6; cursor: wait; }

/* ---- Chat ---- */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.msgs { flex: 1; overflow-y: auto; padding: 16px 12px 8px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; font-size: 12px; color: var(--muted); margin: 10px 0 6px; }
.row { display: flex; gap: 8px; align-items: flex-end; max-width: 88%; }
.row.me { align-self: flex-end; flex-direction: row-reverse; }
.row .av { width: 32px; height: 32px; border-radius: 50%; flex: none; object-fit: cover; background: #bfdbfe;
  display: grid; place-items: center; font-weight: 700; color: var(--brand-d); font-size: 13px; }
.row.cont .av { visibility: hidden; }
.bubble-wrap { display: flex; flex-direction: column; min-width: 0; }
.sender { font-size: 12px; color: var(--muted); margin: 6px 0 3px 4px; font-weight: 500; }
.sender.ai { color: var(--brand); }
.row.cont .sender { display: none; }
.bubble {
  padding: 10px 14px; border-radius: var(--radius); background: var(--bubble-in); line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px;
}
.row.ai .bubble { background: var(--bubble-ai); }
.row.me .bubble { background: var(--brand); color: #fff; }
.row.me .bubble a { color: #fff; }
.bubble a { color: var(--brand); }
.time { font-size: 11px; color: var(--muted); margin: 2px 6px 0; }
.row.me .time { text-align: right; }

.typing { padding: 4px 18px 6px; font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 3px; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
.typing em { font-style: normal; font-weight: 600; margin-left: 4px; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 4px 12px 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none; font: inherit; font-size: 13px; border: 1.5px solid var(--brand); color: var(--brand);
  background: transparent; border-radius: 999px; padding: 7px 12px; cursor: pointer;
}
.composer { display: flex; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { resize: none; max-height: 120px; border-radius: 20px; padding: 10px 16px; line-height: 1.4; }
.composer button {
  flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--brand); color: #fff;
  display: grid; place-items: center; cursor: pointer;
}
.composer button:disabled { opacity: .5; }
.foot {
  display: flex; justify-content: space-between; align-items: center; padding: 2px 16px 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom)); font-size: 12px; color: var(--muted);
}
.link { font: inherit; background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; padding: 4px 0; }
.conn.off { color: #dc2626; }
