:root {
  --navy: #1f3864;
  --navy-dark: #16274a;
  --paper: #faf9f6;
  --card: #ffffff;
  --ink: #1c2230;
  --ink-soft: #5b6472;
  --line: #e4e1da;
  --amber: #c9982a;
  --amber-soft: #fdf3dd;
  --green: #3f8f5f;
  --green-soft: #e6f4ea;
  --clay: #a8493f;
  --clay-soft: #f7e9e6;
  --blue-soft: #e4ecf7;
  --purple-soft: #ece3f4;
  --accent: var(--navy);
  --primary: var(--navy);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #14171c; --card: #1c2027; --ink: #eef0f3; --ink-soft: #9aa3b0;
    --line: #2c313a; --amber-soft: #3a2f16; --green-soft: #17301f;
    --clay-soft: #351f1c; --blue-soft: #1b2536; --purple-soft: #251d33;
    --accent: #9fb7e3; --primary: #3a5c9e;
  }
}
:root[data-theme="dark"] {
  --paper: #14171c; --card: #1c2027; --ink: #eef0f3; --ink-soft: #9aa3b0;
  --line: #2c313a; --amber-soft: #3a2f16; --green-soft: #17301f;
  --clay-soft: #351f1c; --blue-soft: #1b2536; --purple-soft: #251d33;
  --accent: #9fb7e3;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; padding: 0; min-height: 100%;
  background: var(--paper); color: var(--ink);
  font-family: 'IBM Plex Sans', -apple-system, sans-serif;
}
.app-body {
  display: flex;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* Sidebar */
#sidebar {
  width: 220px; flex-shrink: 0; background: var(--navy); color: #fff;
  display: flex; flex-direction: column; padding: 20px 0;
  min-height: 100vh; position: sticky; top: 0; max-height: 100vh; overflow-y: auto;
}
#sidebar .brand {
  padding: 0 20px 20px; font-weight: 700; font-size: 15px; letter-spacing: 0.2px;
  border-bottom: 1px solid rgba(255,255,255,0.14); margin-bottom: 10px;
}
#sidebar .brand small { display: block; font-weight: 400; opacity: 0.65; font-size: 11.5px; margin-top: 3px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 20px; cursor: pointer; font-size: 14px; font-weight: 500;
  color: rgba(255,255,255,0.78); border-left: 3px solid transparent;
  transition: background 0.12s ease;
}
.nav-item:hover { background: rgba(255,255,255,0.06); color: #fff; }
.nav-item.active { background: rgba(255,255,255,0.1); color: #fff; border-left-color: var(--amber); }
.nav-item .dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--amber);
  display: none; flex-shrink: 0;
}
.nav-item.has-badge .dot { display: inline-block; }

.sidebar-foot {
  margin-top: auto; padding: 14px 20px 4px; font-size: 11.5px; color: rgba(255,255,255,0.7);
  display: flex; flex-direction: column; gap: 8px;
}
.conn-status { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.conn-status .pip { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.signed-in strong { color: #fff; font-weight: 600; }
.foot-links { display: flex; gap: 12px; flex-wrap: wrap; }
.foot-links form { margin: 0; }
.link-btn {
  background: none; border: none; padding: 0; color: rgba(255,255,255,0.85);
  font-size: 11.5px; font-weight: 600; text-decoration: underline; text-underline-offset: 2px; cursor: pointer;
}
.link-btn:hover { color: #fff; }

#main { flex: 1; min-width: 0; padding: 28px 36px 60px; max-width: 1180px; }
.view { display: none; }
.view.active { display: block; }

.conn-banner {
  background: #d9704f; color: #fff; padding: 12px 20px; border-radius: 8px;
  margin-bottom: 20px; font-size: 13.5px; font-weight: 600;
}

h1 { font-size: 22px; margin: 0 0 4px; font-weight: 700; }
.subhead { color: var(--ink-soft); font-size: 14px; margin: 0 0 24px; }
.subhead:empty { display: none; }
h2 { font-size: 16px; margin: 28px 0 10px; font-weight: 600; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; margin-bottom: 16px;
}
.card.tight { padding: 14px 18px; }

label { font-size: 12.5px; font-weight: 600; color: var(--ink-soft); display: block; margin-bottom: 5px; }
input[type=text], input[type=tel], input[type=date], input[type=number], input[type=password], textarea, select {
  font-family: inherit; font-size: 14px; padding: 8px 10px; border: 1px solid var(--line);
  border-radius: 7px; background: var(--paper); color: var(--ink); width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { resize: vertical; min-height: 90px; }
input[type=file] { font-size: 13px; color: var(--ink); }

button {
  font-family: inherit; font-size: 13.5px; font-weight: 600; cursor: pointer;
  border: none; border-radius: 7px; padding: 9px 16px; transition: filter 0.12s ease;
}
button:hover { filter: brightness(1.06); }
button:active { filter: brightness(0.95); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: var(--card); color: var(--accent); border: 1px solid var(--line); }
.btn-ghost { background: transparent; color: var(--ink-soft); border: 1px solid var(--line); }
.btn-small { padding: 5px 10px; font-size: 12px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.row { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; }
.field { flex: 1; min-width: 160px; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th { text-align: left; color: var(--ink-soft); font-size: 11.5px;
     font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
td.nowrap, td.nowrap button { white-space: nowrap; }
tr:last-child td { border-bottom: none; }
tr.row-init { background: var(--amber-soft); }
tr.row-fu1 { background: var(--blue-soft); }
tr.row-call { background: var(--purple-soft); }
tr.row-fu2 { background: var(--green-soft); }
tr.row-done { background: var(--green-soft) !important; }
tr.row-done td { color: var(--ink-soft); }
tr.row-done .badge-done { background: var(--green); }
tr.checking { opacity: 0.5; transition: opacity 0.15s ease; }
tr.row-inactive td { color: var(--ink-soft); }

.badge {
  display: inline-block; font-size: 11.5px; font-weight: 600; padding: 3px 9px;
  border-radius: 20px; white-space: nowrap;
}
.badge-init { background: var(--amber); color: #fff; }
.badge-fu1 { background: #3d6fb0; color: #fff; }
.badge-call { background: #7a56a8; color: #fff; }
.badge-fu2 { background: var(--green); color: #fff; }
.badge-done { background: var(--ink-soft); color: #fff; }
.badge-neutral { background: var(--line); color: var(--ink-soft); }
.badge-warn { background: var(--clay-soft); color: var(--clay); }

.action-cell { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.msg-btn {
  background: var(--card); color: var(--accent); border: 1px solid var(--line);
  padding: 3px 9px; font-size: 11.5px;
}

.empty-state { text-align: center; padding: 50px 20px; color: var(--ink-soft); }
.empty-state .big { font-size: 32px; margin-bottom: 10px; }

.stat-row { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 6px; }
.stat {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 18px; min-width: 130px; flex: 1;
}
.stat .num { font-size: 24px; font-weight: 700; color: var(--accent); }
.stat .lbl { font-size: 12px; color: var(--ink-soft); margin-top: 2px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--navy); color: #fff; padding: 11px 20px; border-radius: 8px;
  font-size: 13.5px; font-weight: 500; opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease; z-index: 1100; box-shadow: 0 6px 20px rgba(0,0,0,0.2);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
.toast.error { background: var(--clay); }

.checkbox-cell { text-align: center; }
input[type=checkbox].big-check { width: 19px; height: 19px; cursor: pointer; accent-color: var(--navy); }

.interest-btns { display: flex; gap: 5px; }
.interest-btns button { padding: 4px 9px; font-size: 12px; white-space: nowrap; }
.btn-yes { background: var(--green-soft); color: var(--green); border: 1px solid var(--green); }
.btn-no { background: var(--clay-soft); color: var(--clay); border: 1px solid var(--clay); }
.btn-yes.picked { background: var(--green); color: #fff; }
.btn-no.picked { background: var(--clay); color: #fff; }

.template-block { margin-bottom: 22px; }
.template-block label { margin-bottom: 6px; }
.template-block textarea { min-height: 130px; font-size: 13.5px; line-height: 1.5; }

.muted { color: var(--ink-soft); font-size: 13px; }
.text-error { color: var(--clay) !important; }
.text-warn { color: var(--amber) !important; }
.text-ok { color: var(--green) !important; }
.hint-box {
  background: var(--blue-soft); border-radius: 8px; padding: 12px 14px; font-size: 13px;
  color: var(--ink); margin-bottom: 16px; line-height: 1.5;
}
.scroll-x { overflow-x: auto; }

.patient-name-link {
  cursor: pointer; color: var(--accent); text-decoration: underline dotted; text-underline-offset: 3px;
  background: none; border: none; padding: 0; font: inherit; font-weight: 700; text-align: left;
}
.patient-name-link:hover { color: var(--amber); }
.note-pip { font-size: 11px; margin-left: 4px; color: var(--ink-soft); }

/* Modals */
.modal-overlay {
  display: none; position: fixed; inset: 0; background: rgba(20,20,25,0.45);
  z-index: 1000; align-items: center; justify-content: center; padding: 20px;
}
.modal-overlay.show { display: flex; }
.modal {
  background: var(--card); color: var(--ink); border-radius: 12px; padding: 24px; width: 100%; max-width: 440px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.25); max-height: calc(100vh - 40px); overflow-y: auto;
}
.modal.modal-wide { max-width: 620px; }
.modal h3 { margin: 0 0 2px; font-size: 17px; }
.modal .modal-sub { color: var(--ink-soft); font-size: 13px; margin-bottom: 18px; }
.modal .modal-field { margin-bottom: 16px; }
.modal .modal-history { font-size: 12px; line-height: 1.6; }
.modal .modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.toggle-group { display: flex; gap: 8px; }
.toggle-group button {
  flex: 1; padding: 8px; background: var(--paper); color: var(--ink-soft); border: 1px solid var(--line);
}
.toggle-group button.picked { background: var(--primary); color: #fff; border-color: var(--primary); }
.check-label {
  display: flex; align-items: center; gap: 8px; font-size: 13.5px; font-weight: 500; color: var(--ink);
  margin-bottom: 10px; cursor: pointer;
}
.check-label input { width: 16px; height: 16px; accent-color: var(--navy); }
.message-text { min-height: 260px; font-size: 14px; line-height: 1.55; }

/* Login */
.login-body {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px;
  background: var(--navy);
}
.login-wrap { width: 100%; max-width: 380px; }
.login-brand { color: #fff; font-weight: 700; font-size: 18px; text-align: center; margin-bottom: 18px; }
.login-brand small { display: block; font-weight: 400; opacity: 0.7; font-size: 13px; margin-top: 3px; }
.login-card {
  background: var(--card); border-radius: 12px; padding: 28px; box-shadow: 0 12px 40px rgba(0,0,0,0.25);
}
.login-card h1 { font-size: 20px; margin-bottom: 18px; }
.login-field { margin-bottom: 14px; }
.login-field input { font-size: 15px; padding: 10px 12px; }
.login-submit { width: 100%; padding: 11px; font-size: 14.5px; margin-top: 6px; }
.login-help { text-align: center; margin: 16px 0 0; font-size: 12.5px; }
.login-error, .login-notice { border-radius: 8px; padding: 10px 12px; font-size: 13px; margin-bottom: 16px; font-weight: 500; }
.login-error { background: var(--clay-soft); color: var(--clay); }
.login-notice { background: var(--blue-soft); color: var(--ink); }

@media (max-width: 760px) {
  .app-body { flex-direction: column; }
  #sidebar { width: 100%; min-height: auto; max-height: none; position: static; flex-direction: column; padding: 10px 0 0; }
  #sidebar .brand { display: none; }
  #nav { display: flex; overflow-x: auto; }
  .nav-item { padding: 8px 14px; white-space: nowrap; border-left: none; border-bottom: 3px solid transparent; }
  .nav-item.active { border-left: none; border-bottom-color: var(--amber); }
  .sidebar-foot { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 4px 14px; padding: 8px 14px 10px; margin-top: 0; }
  #main { padding: 20px 16px 50px; }
}
.login-intro { margin: -8px 0 18px; line-height: 1.5; }
.login-help code { font-family: 'IBM Plex Mono', monospace; font-size: 12px; }
.login-help a { color: var(--accent); font-weight: 600; }
.invite-code {
  font-family: 'IBM Plex Mono', monospace; font-size: 22px; font-weight: 500; letter-spacing: 1px;
  text-align: center; padding: 14px; border: 1px dashed var(--line); border-radius: 8px;
  background: var(--paper); margin-bottom: 14px; user-select: all;
}

/* ---- Messaging ---- */
.modal.modal-xwide { max-width: 820px; }
.message-note { margin-top: 8px; font-size: 12.5px; }
.twilio-status { margin-top: 14px; font-size: 13px; line-height: 1.7; }
.twilio-status code, .muted code { font-family: 'IBM Plex Mono', monospace; font-size: 12px; word-break: break-all; }
tr.row-unread td { font-weight: 600; }

.inbox-btn {
  background: var(--card); color: var(--ink-soft); border: 1px solid var(--line);
  padding: 5px 12px; font-size: 12.5px; border-radius: 20px; white-space: nowrap;
}
.inbox-btn.has-unread { background: var(--primary); color: #fff; border-color: var(--primary); box-shadow: 0 0 0 3px var(--blue-soft); }
.nav-item.has-unread::after { content: '💬'; font-size: 12px; margin-left: auto; }

.reply-bubble {
  position: relative; background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  padding: 3px 9px; font-size: 15px; line-height: 1.2;
}
.reply-bubble.unread { border-color: var(--primary); background: var(--blue-soft); }
.reply-count {
  position: absolute; top: -7px; right: -7px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--clay); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px;
}

.convo-modal { display: flex; flex-direction: column; max-height: calc(100vh - 40px); padding-bottom: 16px; }
.convo-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.convo-head .modal-sub { margin-bottom: 12px; }
.convo-thread {
  flex: 1; min-height: 220px; max-height: 55vh; overflow-y: auto; padding: 12px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 10px;
  display: flex; flex-direction: column; gap: 10px;
}
.bubble-row { display: flex; flex-direction: column; max-width: 80%; }
.bubble-row.in { align-self: flex-start; align-items: flex-start; }
.bubble-row.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 9px 13px; border-radius: 16px; font-size: 14px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble-row.in .bubble { background: var(--card); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.bubble-row.out .bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.bubble-meta { font-size: 11px; color: var(--ink-soft); margin-top: 3px; }
.convo-compose { margin-top: 12px; }
.convo-compose textarea { min-height: 70px; }
.convo-compose-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 8px; font-size: 12px; }

/* ---- Calendar picker ---- */
.datepick { position: relative; }
.datepick-btn {
  display: inline-flex; align-items: center; gap: 8px; background: var(--paper); color: var(--ink);
  border: 1px solid var(--line); font-weight: 500; font-size: 14px; padding: 8px 12px; white-space: nowrap;
}
.datepick-pop {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 50; width: 286px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.18);
}
.datepick-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.datepick-nav { background: transparent; color: var(--ink); font-size: 18px; padding: 2px 10px; }
.datepick-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.datepick-dow { font-size: 11px; font-weight: 600; color: var(--ink-soft); padding: 4px 0; }
.datepick-day { background: transparent; color: var(--ink); padding: 7px 0; font-weight: 500; font-size: 13px; border-radius: 8px; }
.datepick-day:hover { background: var(--blue-soft); }
.datepick-day.weekend { color: var(--ink-soft); }
.datepick-day.today { box-shadow: inset 0 0 0 1.5px var(--amber); }
.datepick-day.selected { background: var(--primary); color: #fff; }
.datepick-foot { text-align: center; margin-top: 8px; }
.datepick-foot .link-btn { color: var(--accent); font-size: 12.5px; }

@media (max-width: 760px) {
  .bubble-row { max-width: 92%; }
  .datepick-pop { width: min(286px, calc(100vw - 32px)); }
}
.divider { border: none; border-top: 1px solid var(--line); margin: 18px 0; }
