:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --text: #17212b;
  --muted: #5d6b78;
  --border: #dde3e9;
  --accent: #0b7a75;
  --accent-text: #ffffff;
  --bubble-user: #0b7a75;
  --bubble-bot: #eef2f5;
  --wa-bg: #e9e2d8;
  --wa-user: #d6f5c9;
  --danger: #b3261e;
  --ok: #1d7f3a;
  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1418;
    --surface: #172026;
    --surface-2: #1f2a32;
    --text: #e6edf2;
    --muted: #93a2ae;
    --border: #2a3740;
    --accent: #2bb3a9;
    --accent-text: #04201e;
    --bubble-user: #1f8f88;
    --bubble-bot: #22303a;
    --wa-bg: #0b141a;
    --wa-user: #005c4b;
    --danger: #f2827a;
    --ok: #6fd08c;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); }
h1, h2, p { margin: 0; }
button { font: inherit; cursor: pointer; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 14px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  display: grid; place-items: center; width: 38px; height: 38px; border-radius: 10px;
  background: var(--accent); color: var(--accent-text); font-size: 20px;
}
.brand h1 { font-size: 17px; }
.sub { font-size: 13px; color: var(--muted); }
.status { display: flex; gap: 8px; flex-wrap: wrap; }
.pill {
  font-size: 12px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--muted);
}
.pill.ok::before, .pill.warn::before { content: "●"; margin-right: 6px; }
.pill.ok::before { color: var(--ok); }
.pill.off::before { content: "○"; margin-right: 6px; }
.pill.warn::before { color: var(--danger); }

.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 20px;
  max-width: 1200px; margin: 20px auto; padding: 0 20px;
  height: calc(100vh - 110px); min-height: 520px;
}

.chat-card {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
}
.mode-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
.segmented { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.segmented button {
  border: 0; background: transparent; color: var(--muted); padding: 6px 12px; border-radius: 8px; font-size: 14px;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .12); }
.phone-field { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.phone-field input {
  width: 150px; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit;
}
.ghost {
  margin-left: auto; border: 1px solid var(--border); background: transparent; color: var(--text);
  padding: 6px 12px; border-radius: 8px; font-size: 14px;
}

.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 10px; }
.chat-card.patient .messages { background: var(--wa-bg); }
.msg {
  max-width: 78%; padding: 10px 13px; border-radius: 14px; line-height: 1.45; font-size: 15px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.user { align-self: flex-end; background: var(--bubble-user); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; background: var(--bubble-bot); border-bottom-left-radius: 4px; }
.chat-card.patient .msg.user { background: var(--wa-user); color: var(--text); }
.chat-card.patient .msg.bot { background: var(--surface); }
.msg.error { align-self: center; background: transparent; color: var(--danger); font-size: 14px; text-align: center; }
.msg .tools { display: block; margin-top: 6px; font-size: 12px; color: var(--muted); }
.typing { display: inline-flex; gap: 4px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.suggestions { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 14px 10px; }
.suggestions button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 6px 10px; border-radius: 999px; font-size: 13px;
}
.suggestions button:hover { border-color: var(--accent); }

.composer { display: flex; gap: 8px; padding: 12px 14px; border-top: 1px solid var(--border); }
.composer textarea {
  flex: 1; resize: none; max-height: 140px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-size: 15px;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.composer button {
  width: 44px; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center;
}
.composer button:disabled { opacity: .5; cursor: default; }

.side { display: flex; flex-direction: column; gap: 20px; min-height: 0; }
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; overflow-y: auto; min-height: 0;
}
.panel:first-child { flex: 0 1 45%; }
.panel:last-child { flex: 1 1 55%; }
.panel h2 { font-size: 14px; margin-bottom: 10px; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.small-btn { margin-left: 0; padding: 4px 10px; font-size: 12px; white-space: nowrap; }
.trace .name.reminder { color: var(--accent); }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 12px; margin-bottom: 10px; }

.schedule, .trace { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.schedule li { font-size: 13px; display: grid; grid-template-columns: 92px 1fr; gap: 8px; }
.schedule .when { color: var(--muted); font-variant-numeric: tabular-nums; }
.schedule .day { grid-column: 1 / -1; font-weight: 600; margin-top: 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.schedule li.flash { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: color-mix(in srgb, var(--accent) 25%, transparent); } to { background: transparent; } }

.trace li { font-size: 12px; border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; background: var(--surface-2); }
.trace .name { font-family: ui-monospace, Menlo, monospace; font-weight: 600; }
.trace .name.err { color: var(--danger); }
.trace details { margin-top: 4px; }
.trace summary { color: var(--muted); cursor: pointer; }
.trace pre { margin: 6px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 11px; max-height: 220px; overflow-y: auto; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; height: auto; padding: 0 16px; }
  .chat-card { height: 78vh; }
  .ghost { margin-left: 0; }
}

/* ---------- shared header bits ---------- */
.top-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.link-btn { margin-left: 0; text-decoration: none; display: inline-block; }

/* ---------- settings page ---------- */
.settings { max-width: 960px; margin: 24px auto 120px; padding: 0 16px; display: flex; flex-direction: column; gap: 20px; }
.card-s { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px 22px; display: flex; flex-direction: column; gap: 14px; }
.card-s h2 { font-size: 17px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.help { font-size: 14px; }
.switch-row { display: flex; gap: 10px; align-items: flex-start; font-size: 15px; }
.switch-row input { width: 18px; height: 18px; margin-top: 1px; accent-color: var(--accent); }
.field-row { display: flex; gap: 20px; flex-wrap: wrap; align-items: flex-end; }
.field-row label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.field-row .inline { display: flex; align-items: center; gap: 8px; color: var(--text); }
.settings input[type=time], .settings input[type=number], .settings input[type=tel], .settings select {
  font: inherit; font-size: 15px; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.settings input[type=number] { width: 80px; }
.settings input[type=tel] { width: 220px; }
.primary { border: 0; border-radius: 10px; background: var(--accent); color: var(--accent-text); padding: 10px 18px; font-size: 15px; font-weight: 600; }
.primary:disabled { opacity: .5; cursor: default; }

.templates { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.tpl { display: flex; gap: 10px; padding: 14px; border: 1px solid var(--border); border-radius: 12px; cursor: pointer; background: var(--surface); }
.tpl input { accent-color: var(--accent); margin-top: 3px; }
.tpl.active { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 30%, transparent); }
.tpl.disabled { cursor: not-allowed; background: var(--surface-2); }
.tpl.disabled .tpl-name, .tpl.disabled .tpl-body { color: var(--muted); }
.tpl-main { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.tpl-name { font-family: ui-monospace, Menlo, monospace; font-size: 14px; font-weight: 600; overflow-wrap: anywhere; }
.tpl-meta { font-size: 12px; color: var(--muted); text-transform: capitalize; }
.tpl-body { font-size: 13px; line-height: 1.4; }
.tpl-why { font-size: 12px; color: var(--danger); }
.tpl-why:empty { display: none; }

.fill { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 24px; }
.mapping { display: flex; flex-direction: column; gap: 10px; }
.map-row { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 10px; }
.map-row code { font-size: 14px; background: var(--surface-2); padding: 4px 8px; border-radius: 6px; overflow-wrap: anywhere; }
.small-label { font-size: 13px; margin-bottom: 8px; }
.wa-preview { background: var(--wa-bg); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.wa-bubble { background: var(--surface); border-radius: 12px; padding: 10px 12px; font-size: 15px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.wa-bubble .filled { font-weight: 600; color: var(--accent); }
.wa-bubble .missing { color: var(--danger); font-family: ui-monospace, Menlo, monospace; font-size: 13px; }
.wa-buttons { display: flex; flex-direction: column; gap: 4px; }
.wa-buttons span { background: var(--surface); border-radius: 10px; padding: 8px; text-align: center; color: var(--accent); font-size: 14px; font-weight: 600; }

.banner { border-radius: 12px; padding: 12px 16px; font-size: 14px; border: 1px solid var(--border); background: var(--surface-2); }
.banner.warn, .banner.error { border-color: var(--danger); }
.status { font-size: 14px; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.err { color: var(--danger); }
.savebar {
  position: sticky; bottom: 0; display: flex; align-items: center; justify-content: flex-end; gap: 16px;
  padding: 14px 0; background: linear-gradient(to top, var(--bg) 70%, transparent);
}

@media (max-width: 720px) {
  .fill { grid-template-columns: 1fr; }
  .map-row { grid-template-columns: 1fr; }
}
.map-row select { width: 100%; min-width: 0; }
.mapping, .preview { min-width: 0; }
.field-row label { min-width: 0; }

/* ---------- sign-in and setup ---------- */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 32px 28px; display: flex; flex-direction: column; gap: 14px; }
.auth-card.wide { max-width: 560px; }
.auth-card h1 { font-size: 22px; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.auth-card input { font: inherit; font-size: 15px; padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.auth-card form { display: flex; flex-direction: column; gap: 14px; }
.big-btn { display: block; text-align: center; text-decoration: none; padding: 12px 18px; font-size: 16px; }
.small-note { font-size: 13px; }
.steps-list { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 8px; font-size: 14px; }
code.copy { display: block; margin-top: 4px; padding: 6px 8px; border-radius: 6px; background: var(--surface-2); font-size: 13px; overflow-wrap: anywhere; user-select: all; }
.user-name { font-size: 13px; }

/* ---------- settings: tabs and forms ---------- */
.tabs { display: flex; gap: 4px; max-width: 960px; margin: 20px auto 0; padding: 0 16px; overflow-x: auto; }
.tabs button { border: 1px solid transparent; background: transparent; color: var(--muted); padding: 8px 14px; border-radius: 10px; font-size: 14px; white-space: nowrap; }
.tabs button.active { background: var(--surface); border-color: var(--border); color: var(--text); font-weight: 600; }
.tab-panel { display: flex; flex-direction: column; gap: 20px; }
.tab-panel[hidden] { display: none; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px 20px; }
.fld { display: flex; flex-direction: column; gap: 6px; font-size: 14px; min-width: 0; }
.fld-label { color: var(--muted); }
.fld-help { font-size: 12px; }
.fld input, .fld select, .settings textarea { font: inherit; font-size: 15px; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); min-width: 0; width: 100%; }
.fld .small-btn { align-self: flex-start; }
.switch-row.full { grid-column: 1 / -1; }
.actions { display: flex; align-items: center; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }
.actions .status { margin-right: auto; }
.actions .ghost { margin-left: 0; }
.copy-row { display: flex; flex-direction: column; gap: 2px; font-size: 13px; color: var(--muted); }
.sub-h { font-size: 15px; margin-top: 6px; }
.hours { display: flex; flex-direction: column; gap: 8px; }
.hour-row { display: grid; grid-template-columns: 150px 130px max-content 130px; justify-content: start; align-items: center; gap: 10px; font-size: 14px; }
.hour-row input[type=time] { font: inherit; font-size: 14px; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.hour-row input:disabled { opacity: .45; }
.pill.off::before { content: "○"; margin-right: 6px; }

@media (max-width: 720px) {
  .hour-row { grid-template-columns: 1fr max-content 1fr; }
  .hour-row .switch-row { grid-column: 1 / -1; }
}
