:root {
  --navy: #183349;
  --navy-2: #0f273a;
  --teal: #22a79a;
  --teal-dark: #15877d;
  --teal-soft: #e6f7f4;
  --bg: #f4f7f8;
  --surface: #ffffff;
  --border: #dce5e8;
  --text: #1e2d35;
  --muted: #6f7f87;
  --danger: #b33b45;
  --danger-soft: #fff0f1;
  --warning: #8d6517;
  --warning-soft: #fff8df;
  --shadow: 0 14px 40px rgba(24, 51, 73, 0.1);
  --radius: 16px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; margin: 0; }
body { background: var(--bg); }
button, input, textarea, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }
input, textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 11px 12px;
  color: var(--text);
  background: #fff;
  outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--teal); box-shadow: 0 0 0 3px rgba(34, 167, 154, 0.14); }
label { display: grid; gap: 6px; font-size: 0.88rem; font-weight: 650; color: var(--navy); }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 10px; font-size: clamp(1.8rem, 5vw, 2.6rem); line-height: 1.06; color: var(--navy); }
h2 { margin-bottom: 0; font-size: 1.15rem; color: var(--navy); }
h3 { margin-bottom: 4px; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.muted { color: var(--muted); }
.eyebrow { margin-bottom: 4px; color: var(--teal-dark); font-size: 0.72rem; font-weight: 800; letter-spacing: 0.09em; text-transform: uppercase; }
.stack { display: grid; gap: 16px; margin-top: 26px; }
.primary, .secondary, .ghost, .icon-button, .button-link {
  border-radius: 11px;
  border: 0;
  padding: 10px 14px;
  font-weight: 750;
  transition: transform 0.12s ease, background 0.12s ease;
}
.primary { color: #fff; background: var(--teal); }
.primary:hover { background: var(--teal-dark); }
.secondary { color: var(--navy); background: var(--teal-soft); border: 1px solid #c9ebe5; }
.ghost { color: var(--navy); background: transparent; border: 1px solid var(--border); }
.icon-button { width: 36px; height: 36px; padding: 0; color: var(--navy); background: #eef4f5; font-size: 1.35rem; line-height: 1; }
.button-link { display: block; text-decoration: none; text-align: center; }
.full-width { width: 100%; }

.global-error { position: fixed; z-index: 100; inset: 12px 12px auto; max-width: 720px; margin: auto; padding: 12px 16px; color: var(--danger); background: var(--danger-soft); border: 1px solid #f0c8cd; border-radius: 10px; box-shadow: var(--shadow); }
.form-error { margin: 0; padding: 9px 11px; color: var(--danger); background: var(--danger-soft); border-radius: 9px; font-size: 0.86rem; }
.notice, .warning { padding: 12px; border-radius: 10px; font-size: 0.88rem; line-height: 1.45; }
.notice { color: #315e5a; background: var(--teal-soft); }
.warning { color: var(--warning); background: var(--warning-soft); }

.auth-layout { min-height: 100vh; padding: 28px; display: grid; place-items: center; background: radial-gradient(circle at 10% 10%, #dff5f2, transparent 35%), linear-gradient(140deg, #f8fbfb 30%, #e8f0f3); }
.auth-card { width: min(100%, 470px); padding: clamp(26px, 6vw, 48px); background: rgba(255, 255, 255, 0.94); border: 1px solid rgba(255,255,255,0.9); border-radius: 24px; box-shadow: var(--shadow); }
.brand-mark { width: 58px; height: 58px; margin-bottom: 24px; display: grid; place-items: center; color: #fff; background: linear-gradient(145deg, var(--teal), var(--navy)); border-radius: 17px; font-size: 1.7rem; font-weight: 850; }
.brand-mark.small { width: 38px; height: 38px; margin: 0; border-radius: 11px; font-size: 1.1rem; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 210px 330px minmax(420px, 1fr) 280px; grid-template-rows: 66px calc(100vh - 66px); grid-template-areas: "top top top top" "channels inbox chat patient"; }
.topbar { grid-area: top; display: flex; align-items: center; justify-content: space-between; padding: 0 20px; color: #fff; background: var(--navy-2); }
.brand-inline, .topbar-actions { display: flex; align-items: center; gap: 12px; }
.brand-inline div { display: grid; }
.brand-inline span:not(.brand-mark) { color: #bcd0db; font-size: 0.76rem; }
.topbar .ghost { color: #dceaf0; border-color: #405a6d; }
.user-label { color: #dceaf0; font-size: 0.86rem; }

.channels-panel, .inbox-panel, .chat-panel, .patient-panel { min-width: 0; min-height: 0; background: var(--surface); border-right: 1px solid var(--border); }
.channels-panel { grid-area: channels; padding: 20px 14px; overflow-y: auto; background: #f8fbfb; }
.inbox-panel { grid-area: inbox; display: grid; grid-template-rows: auto auto auto 1fr; overflow: hidden; }
.chat-panel { grid-area: chat; position: relative; overflow: hidden; }
.patient-panel { grid-area: patient; padding: 22px; overflow-y: auto; border-right: 0; background: #f8fbfb; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 16px; }
.inbox-heading { padding: 20px 18px 0; margin-bottom: 12px; }
.search-field { padding: 0 16px 14px; }
.search-field input { padding-left: 14px; background: #f7f9fa; }

.channels-list { display: grid; gap: 9px; }
.channel-card { padding: 11px; background: #fff; border: 1px solid var(--border); border-radius: 12px; }
.channel-card-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.channel-card strong { display: block; font-size: 0.88rem; }
.channel-kind { margin-top: 3px; color: var(--muted); font-size: 0.73rem; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #9aa7ad; box-shadow: 0 0 0 3px #eef1f2; }
.status-dot.online { background: #25aa78; box-shadow: 0 0 0 3px #dff5ec; }
.status-dot.connecting, .status-dot.degraded { background: #e5a629; box-shadow: 0 0 0 3px #fff2d5; }
.status-dot.error, .status-dot.reauth_required { background: #d6515b; box-shadow: 0 0 0 3px #fde8ea; }
.channel-state { margin: 8px 0 0; color: var(--muted); font-size: 0.73rem; }
.qr-button { width: 100%; margin-top: 9px; padding: 7px 9px; font-size: 0.78rem; }

.state-card { margin: 12px 16px; padding: 16px; color: var(--muted); background: #f7f9fa; border: 1px dashed var(--border); border-radius: 12px; text-align: center; font-size: 0.88rem; }
.conversations-list { overflow-y: auto; }
.conversation-item { width: 100%; display: grid; grid-template-columns: 42px minmax(0, 1fr) auto; gap: 10px; padding: 13px 16px; color: inherit; background: #fff; border: 0; border-bottom: 1px solid #edf1f2; text-align: left; }
.conversation-item:hover, .conversation-item.active { background: var(--teal-soft); }
.conversation-avatar, .patient-avatar { display: grid; place-items: center; color: var(--navy); background: #dcebea; border-radius: 13px; font-weight: 850; }
.conversation-avatar { width: 42px; height: 42px; font-size: 0.8rem; }
.conversation-main { min-width: 0; }
.conversation-line { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.conversation-line strong, .conversation-preview { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.conversation-line time { flex: none; color: var(--muted); font-size: 0.68rem; }
.conversation-preview { margin: 4px 0 0; color: var(--muted); font-size: 0.78rem; }
.channel-badge { margin-top: 5px; display: inline-flex; color: var(--teal-dark); font-size: 0.68rem; font-weight: 750; }
.unread-badge { align-self: center; min-width: 22px; padding: 3px 6px; color: #fff; background: var(--teal); border-radius: 999px; font-size: 0.7rem; text-align: center; }

.chat-empty { height: 100%; display: grid; align-content: center; justify-items: center; padding: 40px; color: var(--muted); text-align: center; }
.chat-empty h2 { margin: 12px 0 6px; }
.empty-symbol { font-size: 2.4rem; }
.chat-content { height: 100%; display: grid; grid-template-rows: auto 1fr auto auto; }
.chat-header { min-height: 82px; display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 16px 22px; border-bottom: 1px solid var(--border); }
.chat-header h2 { font-size: 1.04rem; }
.phone-link { display: inline-block; margin-top: 5px; color: var(--teal-dark); text-decoration: none; font-size: 0.78rem; }
.conversation-status { padding: 6px 9px; color: var(--navy); background: #eef4f5; border-radius: 999px; font-size: 0.7rem; font-weight: 750; }
.messages-list { min-height: 0; padding: 24px; overflow-y: auto; background: linear-gradient(180deg, #f7fafb, #f2f6f7); }
.message-row { display: flex; margin-bottom: 12px; }
.message-row.outbound { justify-content: flex-end; }
.message-row.system { justify-content: center; }
.message-bubble { max-width: min(72%, 650px); padding: 10px 12px 7px; background: #fff; border: 1px solid var(--border); border-radius: 13px 13px 13px 3px; box-shadow: 0 3px 12px rgba(24, 51, 73, 0.04); }
.outbound .message-bubble { background: #dff5f1; border-color: #c9eae4; border-radius: 13px 13px 3px 13px; }
.system .message-bubble { max-width: 85%; color: var(--muted); background: #eef3f4; border: 0; border-radius: 999px; font-size: 0.78rem; text-align: center; }
.message-sender { margin-bottom: 4px; color: var(--teal-dark); font-size: 0.7rem; font-weight: 750; }
.message-body { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.42; }
.message-meta { display: flex; justify-content: flex-end; gap: 7px; margin-top: 5px; color: var(--muted); font-size: 0.64rem; }
.delivery-failed, .delivery-unknown { color: var(--danger); }
.compose-form { display: grid; grid-template-columns: 1fr auto; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--border); background: #fff; }
.compose-form textarea { resize: none; min-height: 47px; max-height: 140px; }
.send-button { align-self: stretch; }
.compose-error { margin: 0 18px 12px; }

.patient-avatar { width: 62px; height: 62px; margin: 24px 0 12px; font-size: 1.1rem; }
.patient-empty { margin-top: 20px; color: var(--muted); font-size: 0.88rem; line-height: 1.5; }
.patient-match { margin: 18px 0 12px; padding: 12px; color: var(--muted); background: #fff; border: 1px solid var(--border); border-radius: 11px; font-size: 0.82rem; line-height: 1.4; }
.patient-note { margin-top: 18px; color: var(--muted); font-size: 0.74rem; line-height: 1.5; }
.patient-panel .button-link { margin-bottom: 9px; }

dialog { width: min(92vw, 510px); padding: 0; border: 0; border-radius: 18px; box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(10, 31, 46, 0.5); backdrop-filter: blur(3px); }
.dialog-card { display: grid; gap: 16px; margin: 0; padding: 24px; }
.dialog-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 9px; }
.qr-card { justify-items: stretch; text-align: center; }
.qr-card .dialog-heading { width: 100%; }
.qr-card img { width: min(100%, 320px); margin: 0 auto; border-radius: 12px; }

@media (max-width: 1180px) {
  .app-shell { grid-template-columns: 180px 300px minmax(400px, 1fr); grid-template-areas: "top top top" "channels inbox chat"; }
  .patient-panel { display: none; }
}

@media (max-width: 820px) {
  .app-shell { min-height: 100dvh; grid-template-columns: 1fr; grid-template-rows: 58px auto auto minmax(55vh, 1fr); grid-template-areas: "top" "channels" "inbox" "chat"; }
  .topbar { padding: 0 12px; }
  .channels-panel { max-height: 190px; }
  .channels-list { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
  .inbox-panel { max-height: 310px; }
  .chat-panel { min-height: 55vh; }
  .user-label { display: none; }
  .message-bubble { max-width: 88%; }
}
