:root {
  --brand: #d7263d;
  --brand-ink: #ffffff;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --ink: #0d1b2e;
  --muted: #5b6b85;
  --line: #dde3ee;
  --agent-bubble: #eef1f7;
  --danger: #b42318;
  --ok: #067647;
  --radius: 16px;
  --shadow: 0 10px 30px rgba(13, 27, 46, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220;
    --surface: #111a2c;
    --surface-2: #17223a;
    --ink: #e8edf6;
    --muted: #9aa8bf;
    --line: #24314d;
    --agent-bubble: #1a2640;
    --danger: #f97066;
    --ok: #47cd89;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #111a2c;
  --surface-2: #17223a;
  --ink: #e8edf6;
  --muted: #9aa8bf;
  --line: #24314d;
  --agent-bubble: #1a2640;
  --danger: #f97066;
  --ok: #47cd89;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

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

.shell {
  display: flex;
  flex-direction: column;
  min-height: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px 16px;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 0;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand strong { display: block; font-size: 16px; line-height: 1.2; }
.brand small { color: var(--muted); font-size: 12px; letter-spacing: .04em; text-transform: uppercase; }
.brand-mark {
  display: grid; place-items: center; flex: none;
  width: 36px; height: 36px; border-radius: 10px;
  background: var(--brand); color: var(--brand-ink);
}

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.link-button {
  color: var(--muted); text-decoration: none; font-size: 13px;
  padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line);
}
.link-button:hover { color: var(--ink); }

.icon-button {
  display: grid; place-items: center; width: 34px; height: 34px;
  border-radius: 8px; border: 1px solid var(--line); background: var(--surface);
}

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  gap: 16px;
  min-height: 0;
}

.call-card, .chat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- Call card ---------- */
.call-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 28px 22px;
  text-align: center;
  align-self: start;
}

.call-visual {
  position: relative;
  display: grid;
  place-items: center;
  width: 132px;
  height: 132px;
}
.call-visual .avatar {
  position: relative; z-index: 1;
  display: grid; place-items: center;
  width: 76px; height: 76px; border-radius: 50%;
  background: var(--brand); color: var(--brand-ink);
  font-size: 30px; font-weight: 700;
}
.call-visual .ring {
  position: absolute; inset: 28px; border-radius: 50%;
  border: 2px solid var(--brand); opacity: 0;
}
.call-visual[data-state="connecting"] .ring,
.call-visual[data-state="speaking"] .ring { animation: pulse 1.8s ease-out infinite; }
.call-visual[data-state="listening"] .ring.r1 { opacity: .35; inset: 18px; }
.call-visual[data-state="thinking"] .avatar { animation: breathe 1.2s ease-in-out infinite; }
.call-visual .r2 { animation-delay: .6s !important; }
.call-visual .r3 { animation-delay: 1.2s !important; }

@keyframes pulse {
  0% { inset: 28px; opacity: .6; }
  100% { inset: -4px; opacity: 0; }
}
@keyframes breathe { 50% { transform: scale(.92); } }
@media (prefers-reduced-motion: reduce) {
  .call-visual .ring, .call-visual .avatar { animation: none !important; }
}

.call-copy h1 { margin: 0 0 4px; font-size: 22px; line-height: 1.25; }
.call-status { margin: 0; color: var(--muted); font-size: 14px; min-height: 21px; }

.call-controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

.call-button {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 11px 22px; border: 0; border-radius: 999px;
  background: var(--ok); color: #fff; font-weight: 600;
}
.call-button.active { background: var(--danger); }
.call-button:disabled { opacity: .55; cursor: not-allowed; }

.secondary-button {
  padding: 11px 18px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface-2); font-weight: 600;
}
.secondary-button[aria-pressed="true"] { background: var(--ink); color: var(--surface); }

.quick-actions { width: 100%; display: grid; gap: 8px; margin-top: 4px; text-align: left; }
.quick-actions h2 { margin: 6px 0 2px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.quick-actions button {
  padding: 10px 14px; border-radius: 10px; text-align: left;
  border: 1px solid var(--line); background: var(--surface-2);
}
.quick-actions button:hover, .chips button:hover { border-color: var(--brand); }

.contact { margin: 0; font-size: 13px; color: var(--muted); }
.contact a { color: inherit; }

/* ---------- Chat ---------- */
.chat {
  display: flex;
  flex-direction: column;
  min-height: 560px;
  max-height: calc(100vh - 90px);
  overflow: hidden;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overscroll-behavior: contain;
}

.msg {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.assistant { align-self: flex-start; background: var(--agent-bubble); border-bottom-left-radius: 4px; }
.msg.user { align-self: flex-end; background: var(--brand); color: var(--brand-ink); border-bottom-right-radius: 4px; }
.msg.voice::before {
  content: "🎙 "; font-size: 12px;
}
.msg.notice {
  align-self: center; max-width: 100%; padding: 4px 10px;
  background: none; color: var(--muted); font-size: 13px; text-align: center;
}
.msg.error { align-self: center; color: var(--danger); background: none; font-size: 13px; }
.msg a { color: inherit; }

.typing { display: inline-flex; gap: 4px; padding: 14px; }
.typing span {
  width: 7px; height: 7px; 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; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* Inline form */
.form-card {
  align-self: stretch;
  border: 1px solid var(--line);
  border-left: 4px solid var(--brand);
  border-radius: 12px;
  padding: 16px;
  background: var(--surface);
}
.form-card h3 { margin: 0 0 2px; font-size: 16px; }
.form-card > p { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.form-card form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-card label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
.form-card label.wide { grid-column: 1 / -1; }
.form-card label small { color: var(--muted); font-weight: 400; }
.form-card input, .form-card select, .form-card textarea {
  width: 100%; padding: 9px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); font-weight: 400;
}
.form-card textarea { min-height: 90px; resize: vertical; }
.form-card .form-actions { grid-column: 1 / -1; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.form-card .form-actions button[type="submit"] {
  padding: 10px 18px; border: 0; border-radius: 8px;
  background: var(--brand); color: var(--brand-ink); font-weight: 600;
}
.form-card .form-actions button[type="button"] {
  padding: 10px 14px; border-radius: 8px; border: 1px solid var(--line); background: none;
}
.form-card .form-error { grid-column: 1 / -1; margin: 0; color: var(--danger); font-size: 13px; }
.form-card.done { border-left-color: var(--ok); }
.form-card.done p { margin: 0; color: var(--ink); }

.chips { display: flex; gap: 6px; padding: 0 12px 8px; flex-wrap: wrap; }
.chips button {
  padding: 6px 12px; border-radius: 999px; font-size: 13px;
  border: 1px solid var(--line); background: var(--surface-2);
}

.composer {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px;
  padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg);
}
.composer textarea:focus, .form-card :is(input, select, textarea):focus {
  outline: 2px solid var(--brand); outline-offset: -1px;
}
.composer button {
  flex: none; width: 44px; border: 0; border-radius: 12px;
  background: var(--brand); color: var(--brand-ink);
  display: grid; place-items: center;
}
.composer button:disabled { opacity: .5; }

.disclaimer { margin: 0; padding: 0 12px 10px; color: var(--muted); font-size: 11px; text-align: center; }

/* ---------- Modes ---------- */
body:not(.embed) .embed-only { display: none !important; }
body.embed .full-only { display: none !important; }

body.embed { background: var(--surface); }
body.embed .shell { padding: 0; height: 100vh; max-width: none; }
body.embed .topbar { padding: 10px 12px; border-bottom: 1px solid var(--line); }
body.embed .layout { display: flex; flex-direction: column; gap: 0; }
body.embed .call-card {
  flex-direction: row; text-align: left; gap: 12px;
  padding: 10px 12px; border: 0; border-bottom: 1px solid var(--line);
  border-radius: 0; box-shadow: none; align-self: stretch;
}
body.embed .call-visual { width: 48px; height: 48px; flex: none; }
body.embed .call-visual .avatar { width: 40px; height: 40px; font-size: 17px; }
body.embed .call-visual .ring { inset: 4px; }
body.embed .call-copy { flex: 1; min-width: 0; }
body.embed .call-copy h1 { font-size: 14px; margin: 0; }
body.embed .call-status { font-size: 12px; min-height: 0; }
body.embed .call-controls { flex-wrap: nowrap; }
body.embed .call-button { padding: 8px 14px; font-size: 13px; }
body.embed .call-button span { display: none; }
body.embed .secondary-button { padding: 8px 12px; font-size: 13px; }
body.embed .chat { flex: 1; min-height: 0; max-height: none; border: 0; border-radius: 0; box-shadow: none; }
body.embed .messages { padding: 14px 12px; }
body.embed .form-card form { grid-template-columns: 1fr; }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .call-card { padding: 20px 16px; }
  .chat { min-height: 70vh; max-height: none; }
  .form-card form { grid-template-columns: 1fr; }
  .msg { max-width: 90%; }
}

body.embed .brand small { display: none; }
body.embed .link-button { white-space: nowrap; padding: 6px 8px; font-size: 12px; }
