:root {
  --navy: #14213d;
  --navy-2: #1f3160;
  --gold: #c9a227;
  --gold-soft: #f6efd6;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --text: #1b1f2a;
  --muted: #5d6475;
  --border: #e2e5ee;
  --user-bubble: #14213d;
  --user-text: #ffffff;
  --bot-bubble: #ffffff;
  --error: #b42318;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 33, 61, 0.06), 0 4px 16px rgba(20, 33, 61, 0.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1422;
    --surface: #171e31;
    --text: #e8ebf3;
    --muted: #9aa3b8;
    --border: #2a3350;
    --user-bubble: #c9a227;
    --user-text: #14213d;
    --bot-bubble: #1b2339;
    --gold-soft: #2b2a1f;
    --error: #ff8a80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
button { font: inherit; cursor: pointer; }
.primary {
  background: var(--navy);
  color: #fff;
  border: 0;
  border-radius: 12px;
  padding: 12px 18px;
  font-weight: 600;
}
.primary:disabled { opacity: 0.5; cursor: default; }
.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 0.85rem;
}
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; margin-top: 14px; }
.error { color: var(--error); font-size: 0.9rem; margin: 0; }

/* Sign-in */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px;
  padding: 32px 24px; box-shadow: var(--shadow); text-align: center;
}
.login-logo { border-radius: 50%; object-fit: cover; box-shadow: 0 0 0 3px var(--gold); }
.login-photo { display: flex; align-items: center; gap: 10px; }
.login-photo .avatar { width: 40px; height: 40px; }

/* Avatars beside each message */
.avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; flex: none; display: block; }
.avatar.initials {
  display: grid; place-items: center; background: var(--navy-2); color: #fff;
  font-size: 0.75rem; font-weight: 700; letter-spacing: 0.02em;
}
.row { display: flex; gap: 8px; align-items: flex-end; max-width: 92%; }
.row.user { flex-direction: row-reverse; align-self: flex-end; }
.row.bot { align-self: flex-start; }
.row .msg { max-width: none; align-self: auto; min-width: 0; }
.row.bot .avatar { box-shadow: 0 0 0 2px var(--gold); }
.me { padding: 0; border: 0; background: none; border-radius: 50%; line-height: 0; }
.me .avatar { width: 34px; height: 34px; box-shadow: 0 0 0 2px var(--border); }
.brand .avatar { width: 36px; height: 36px; box-shadow: 0 0 0 2px var(--gold); }
.login-card h1 { margin: 12px 0 4px; font-size: 1.6rem; }
.login-card form { display: grid; gap: 14px; margin-top: 20px; text-align: left; }
.login-card label { display: grid; gap: 6px; font-size: 0.9rem; font-weight: 600; }
.login-card input {
  font: inherit; padding: 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}

/* App shell */
.app { height: 100dvh; display: flex; flex-direction: column; max-width: 860px; margin: 0 auto; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand div { display: grid; line-height: 1.2; }
.top-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.welcome { margin: auto 0; text-align: center; padding: 16px 0; }
.welcome h2 { margin: 0 0 6px; font-size: 1.35rem; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 8px 14px; font-size: 0.9rem; text-align: left;
}
.chip:hover { border-color: var(--gold); }

.msg { max-width: 88%; padding: 10px 14px; border-radius: var(--radius); box-shadow: var(--shadow); overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.bot { align-self: flex-start; background: var(--bot-bubble); border-bottom-left-radius: 6px; }
.msg.bot p { margin: 0 0 8px; }
.msg.bot p:last-child { margin-bottom: 0; }
.msg.bot ul, .msg.bot ol { margin: 4px 0 8px; padding-left: 22px; }
.msg.bot li { margin: 2px 0; }
.msg.bot h3, .msg.bot h4 { margin: 10px 0 4px; font-size: 1rem; }
.msg.bot a { color: inherit; }
.msg.bot code { background: var(--bg); padding: 1px 5px; border-radius: 5px; font-size: 0.9em; }
.msg.bot table { border-collapse: collapse; margin: 6px 0; font-size: 0.9rem; display: block; overflow-x: auto; }
.msg.bot th, .msg.bot td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.status { font-size: 0.85rem; color: var(--muted); font-style: italic; }
.typing span { display: inline-block; width: 7px; height: 7px; margin-right: 4px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.msg-actions { display: flex; gap: 6px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.msg-actions button {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  border-radius: 8px; padding: 3px 8px; font-size: 0.8rem;
}
.msg-actions button.on { border-color: var(--gold); color: var(--text); background: var(--gold-soft); }
.escalate {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 10px;
  background: var(--gold); color: #14213d; text-decoration: none; font-weight: 700;
  padding: 8px 14px; border-radius: 10px;
}

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px 4px;
  background: var(--bg);
}
.composer textarea {
  flex: 1; resize: none; font: inherit; padding: 12px 14px; border-radius: 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  max-height: 160px;
}
.send { width: 48px; height: 48px; padding: 0; display: grid; place-items: center; border-radius: 14px; }
.mic {
  width: 48px; height: 48px; padding: 0; display: grid; place-items: center; flex: none;
  border-radius: 14px; border: 1px solid var(--border); background: var(--surface); color: var(--navy);
}
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .mic { color: var(--gold); } }
.mic.listening { background: #d92d20; border-color: #d92d20; color: #fff; animation: pulse 1.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(217, 45, 32, 0.45); } 70% { box-shadow: 0 0 0 12px rgba(217, 45, 32, 0); } 100% { box-shadow: 0 0 0 0 rgba(217, 45, 32, 0); } }
#voice-toggle[aria-pressed="true"] { background: var(--gold-soft); border-color: var(--gold); }
.msg-actions button.speaking { border-color: var(--gold); color: var(--text); background: var(--gold-soft); }
.voice-note { font-size: 0.72rem; color: var(--muted); }
.disclaimer { text-align: center; margin: 2px 16px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }

@media (max-width: 520px) {
  .brand .muted { display: none; }
  .msg { max-width: 94%; }
}
