/* Fallback token defaults. base.html emits the *derived* palette inline in <head>
   (from services/theming.derive_tokens) on every page, which overrides this block —
   so these values only apply if that inline block is ever absent. Keep them in step
   with derive_tokens(None) so the fallback matches the live render. Never hardcode a
   colour/radius/font below; add a token here and derive it there. */
:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1a1f2c;
  --muted: #5c606a;
  --border: #dcdde0;
  --field-bg: #ffffff;
  --accent: #2f6feb;
  --accent-hover: #2962cf;
  --accent-tint: #eef3fd;
  --accent-ink: #ffffff;
  --focus: rgba(47, 111, 235, 0.40);
  --accent-2: #2f6feb;
  --accent-2-ink: #ffffff;
  --accent-2-tint: rgba(47, 111, 235, 0.14);
  --ok-bg: #e6f4ea;
  --ok-ink: #1c5b2a;
  --info-bg: #eef3fd;
  --info-ink: #2962cf;
  --error-bg: #fde8e8;
  --error-ink: #8a1a1a;
  --danger-ink: #ffffff;
  --shadow: rgba(47, 111, 235, 0.18);
  --backdrop: rgba(26, 31, 44, 0.45);
  --radius: 8px;
  --radius-lg: 12px;
  --row-pad: 0.55rem 0.75rem;
  --card-pad: 1.25rem 1.5rem;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-heading: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}
h1, h2, h3 { font-family: var(--font-heading); }

.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 1.25rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; text-decoration: none; color: var(--ink); display: flex; align-items: center; gap: 0.6rem; }
.brand-logo { height: 34px; width: auto; max-width: 220px; display: block; }
.topbar nav { display: flex; gap: 1rem; flex: 1; }
.topbar nav a { color: var(--muted); text-decoration: none; }
.topbar nav a:hover { color: var(--ink); }
.topbar .user-menu { display: flex; gap: 0.75rem; align-items: center; color: var(--muted); }
.topbar .user-menu a { color: var(--muted); text-decoration: none; }
.topbar .user-menu a:hover { color: var(--ink); }
.topbar .user-menu .debug-toggle { margin: 0; }
.topbar .user-menu .debug-toggle label { display: inline-flex; align-items: center; gap: 0.3rem; font-size: 0.8rem; cursor: pointer; }
.topbar .user-menu .debug-toggle input { width: auto; margin: 0; }
/* When a developer toggles debug off, hide the agent tool-use notes. */
body.debug-off .msg-tool { display: none; }

/* Reminders calendar view. */
.view-toggle .btn-link.active { font-weight: 700; color: var(--ink); }
table.cal { width: 100%; table-layout: fixed; border-collapse: collapse; }
table.cal th { text-align: center; font-size: 0.8rem; }
table.cal td.cal-cell { border: 1px solid var(--border); vertical-align: top; height: 5.5rem; padding: 0.2rem; }
table.cal td.cal-other { background: color-mix(in srgb, var(--border) 30%, transparent); }
table.cal td.cal-today { outline: 2px solid var(--accent); outline-offset: -2px; }
.cal-day { font-size: 0.78rem; color: var(--muted); text-align: right; }
.cal-item { font-size: 0.75rem; background: var(--info-bg); color: var(--info-ink); border-radius: 4px;
  padding: 0.05rem 0.3rem; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-item.overdue { background: var(--error-bg); color: var(--error-ink); }

/* Reminder link rows: [type] [searchable instance] [remove]. */
.link-row { display: grid; grid-template-columns: 12rem minmax(0, 1fr) auto; gap: 0.5rem; align-items: center; margin-bottom: 0.4rem; }
.link-row select, .link-row input[type="text"] { width: 100%; margin: 0; }
.link-row [data-remove-link] { padding: 0.2rem 0.55rem; line-height: 1; }
@media (max-width: 600px) { .link-row { grid-template-columns: 1fr 1fr auto; } }

/* Reminder sharing controls. */
fieldset.sharing { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.75rem 1rem; }
fieldset.sharing legend { padding: 0 0.4rem; font-weight: 600; }
.share-list { margin-top: 0.5rem; }
.share-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr)); gap: 0.25rem 1rem; margin-top: 0.4rem; }
.share-grid .check { display: flex; align-items: center; gap: 0.4rem; font-weight: 400; }
.share-grid .check input { width: auto; margin: 0; }

/* Use the full browser width on desktop; a modest gutter, not big empty margins.
   Forms/auth stay readable via `.card.narrow`, which caps its own width. */
main { max-width: none; margin: 1.5rem 0; padding: 0 1.5rem; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--card-pad);
  margin-bottom: 1rem;
}
.card.narrow { max-width: 420px; margin: 3rem auto; }
.card header.row { margin-bottom: 0.75rem; }
.card h1 { margin-top: 0; }

label { display: block; margin: 0.75rem 0; font-size: 0.95rem; }
input, select, textarea {
  display: block;
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.5rem 0.65rem;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--field-bg);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button, .btn {
  appearance: none;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 0.55rem 1rem;
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;
  box-sizing: border-box;
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}
button.link, .btn-link {
  background: transparent;
  color: var(--accent);
  padding: 0.55rem 0.25rem;
}

.row { display: flex; gap: 0.75rem; align-items: center; }
.row.between { justify-content: space-between; }
.inline { display: inline; }
.muted { color: var(--muted); }
.error { background: var(--error-bg); color: var(--error-ink); padding: 0.5rem 0.75rem; border-radius: var(--radius); }
.flash { margin: 1rem 1.5rem; padding: 0.5rem 0.75rem; border-radius: var(--radius); }
.flash-ok { background: var(--ok-bg); color: var(--ok-ink); }
.flash-error { background: var(--error-bg); color: var(--error-ink); }
.flash-info { background: var(--info-bg); color: var(--info-ink); }

table { width: 100%; border-collapse: collapse; }
th, td { padding: var(--row-pad); border-bottom: 1px solid var(--border); text-align: left; }
th { font-size: 0.85rem; color: var(--muted); font-weight: 600; }
td.actions { white-space: nowrap; }

/* Sortable/filterable browse table */
.table-scroll { overflow-x: auto; }
table.data-table th[data-key] { cursor: pointer; user-select: none; white-space: nowrap; }
table.data-table th[data-key]:hover { color: var(--ink); }
table.data-table th[data-key]:focus { outline: 2px solid var(--accent); outline-offset: -2px; }
table.data-table .filter-row td { padding: 0.2rem 0.3rem; border-bottom: 2px solid var(--border); }
table.data-table .filter-row input { width: 100%; min-width: 6rem; font-size: 0.82rem; padding: 0.2rem 0.35rem; }
.sort-ind { color: var(--accent); font-size: 0.75rem; }
/* Website column is just a compact "link" — keep it narrow. */
th.col-web, td.col-web { width: 1%; white-space: nowrap; }
td.actions .link { padding: 0.15rem 0.4rem; }
/* Providers table: stack the row actions vertically so the column stays narrow. */
#providers-table td.actions { display: flex; flex-direction: column; align-items: flex-start;
  gap: 2px; width: 1%; white-space: nowrap; }
#providers-table td.actions .link { padding: 0.1rem 0; }

/* Row selection + bulk-action bar (providers/documents tables). */
th.col-select, td.col-select { width: 1%; white-space: nowrap; text-align: center; }
.col-select input { display: inline; width: auto; margin: 0; }
.bulk-bar { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: 0.5rem 0.85rem; margin: 0.5rem 0; background: var(--accent-tint);
  border: 1px solid var(--border); border-radius: var(--radius); }
/* A `display` rule overrides the plain [hidden] attribute, so restore it explicitly:
   the bar (and its count) only appear once a row is selected. */
.bulk-bar[hidden] { display: none; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
@media (max-width: 640px) { .grid2 { grid-template-columns: 1fr; } }
.stack > * { margin-top: 0.5rem; }

dl.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0.5rem 1.5rem; margin: 0; }
dl.facts dt { font-size: 0.8rem; color: var(--muted); }
dl.facts dd { margin: 0; }
/* Provider detail: on desktop cap at 5 columns so the 9 fields sit on two roomy
   rows instead of one cramped row that wraps. */
@media (min-width: 900px) { dl.provider-facts { grid-template-columns: repeat(5, minmax(0, 1fr)); } }

.badge { display: inline-block; font-size: 0.78rem; padding: 0.05rem 0.4rem; border-radius: 4px; background: var(--border); color: var(--muted); }
.badge.overdue { background: var(--error-bg); color: var(--error-ink); }
.badge.ok { background: var(--ok-bg); color: var(--ok-ink); }
/* Engagement status badges. */
.badge.status-reported { background: var(--info-bg); color: var(--info-ink); }
.badge.status-converted { background: var(--ok-bg); color: var(--ok-ink); }
.badge.status-declined { background: var(--error-bg); color: var(--error-ink); }

/* Tags: coloured pills. In the providers table they stack one per line to keep the
   column narrow (tag-stack); elsewhere they flow inline (tag-row). */
.tag-pill { display: inline-block; font-size: 0.75rem; line-height: 1.3; padding: 0.05rem 0.45rem;
  border-radius: 999px; background: var(--border); color: #fff; white-space: nowrap; }
.tag-stack { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; }
.tag-row { display: flex; flex-wrap: wrap; gap: 4px; }
.tag-editor { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; margin: 0; }
.tag-editor input[type="color"] { width: 2.5rem; height: 2rem; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); background: none; }
/* Tag checkboxes on the provider form. */
.tag-choice { display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.15rem 0.5rem;
  border: 1px solid var(--border); border-radius: 999px; cursor: pointer; }
.tag-choice input { width: auto; margin: 0; }
.tag-choices { display: flex; flex-wrap: wrap; gap: 0.4rem; }

/* Provider attachments list. */
.attach-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.4rem; }
.attach-list li { display: flex; align-items: center; gap: 0.6rem; padding: 0.4rem 0; border-bottom: 1px solid var(--border); }
.attach-list .doc-thumb { flex: none; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; }
.attach-list .doc-thumb img { width: 40px; height: 40px; object-fit: cover; border-radius: var(--radius); }
.attach-meta { display: flex; align-items: center; gap: 0.5rem; flex: 1; min-width: 0; flex-wrap: wrap; }

.attention-list { list-style: none; padding: 0; margin: 0; }
.attention-list li { padding: 0.35rem 0; border-bottom: 1px solid var(--border); }

details summary { cursor: pointer; color: var(--accent); margin-top: 1rem; }
.done-row td, .snoozed-row td { font-style: italic; }

h2 { font-size: 1.1rem; margin-top: 1.5rem; }
h2:first-of-type { margin-top: 0.5rem; }

.chat-layout { display: grid; grid-template-columns: 220px 1fr; gap: 1rem; min-height: 60vh; }
.chat-threads ul { list-style: none; padding: 0; margin: 1rem 0; }
.chat-threads li { padding: 0.3rem 0.5rem; border-radius: var(--radius); }
.chat-threads li.active { background: var(--bg); }
.chat-threads li a { text-decoration: none; color: var(--ink); }
.chat-threads .thread-item { display: flex; align-items: center; justify-content: space-between; gap: 0.4rem; }
.chat-threads .thread-item > a { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-threads .thread-item.archived > a { color: var(--muted); font-style: italic; }
.chat-threads .thread-item form { margin: 0; }
.chat-rename { display: flex; align-items: center; gap: 0.4rem; flex: 1; }
.chat-rename input { width: auto; flex: 1; max-width: 22rem; margin: 0; padding: 0.2rem 0.4rem; }
.chat-main { display: flex; flex-direction: column; min-width: 0; }
.chat-bar { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.5rem; }
.chat-threads-toggle { display: none; }  /* shown only on narrow screens */
.messages { flex: 1; overflow-y: auto; padding: 0.5rem; display: flex; flex-direction: column; gap: 0.5rem; }
.msg { display: flex; }
.msg-user { justify-content: flex-end; }
.msg .bubble { max-width: 80%; padding: 0.5rem 0.75rem; border-radius: var(--radius-lg); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-user .bubble { background: var(--accent); color: var(--accent-ink); }
.msg-assistant .bubble { background: var(--bg); border: 1px solid var(--border); }

/* Rendered-markdown assistant bubbles: block layout, not pre-wrap. */
.bubble.md { white-space: normal; }
.bubble.md > :first-child { margin-top: 0; }
.bubble.md > :last-child { margin-bottom: 0; }
.bubble.md p { margin: 0.5em 0; }
.bubble.md ul, .bubble.md ol { margin: 0.4em 0; padding-left: 1.3em; }
.bubble.md li { margin: 0.15em 0; }
.bubble.md a { color: var(--accent); }
.bubble.md code { background: var(--panel); border: 1px solid var(--border); border-radius: 4px; padding: 0.05em 0.3em; font-size: 0.9em; }
.bubble.md pre { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.5em 0.65em; overflow-x: auto; }
.bubble.md pre code { background: none; border: 0; padding: 0; }
.bubble.md h1, .bubble.md h2, .bubble.md h3 { font-size: 1rem; margin: 0.5em 0 0.25em; }
.bubble.md blockquote { margin: 0.5em 0; padding-left: 0.75em; border-left: 3px solid var(--border); color: var(--muted); }
.bubble.md table { width: auto; margin: 0.5em 0; }
.msg-tool .tool-note { font-size: 0.8rem; color: var(--muted); }

/* Multiple-choice clarifying question rendered by the agent in chat. */
.msg .bubble.choice-card { white-space: normal; }
.choice-card .choice-q { margin: 0 0 0.5rem; font-weight: 600; }
.choice-card .choice-options { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.choice-card .choice-opt:disabled { opacity: 0.5; cursor: default; }

/* Selectable provider list rendered by the agent in chat. */
/* Beat `.msg .bubble { white-space: pre-wrap }` (higher specificity) — otherwise the
   template's newlines/indentation render as huge vertical gaps. */
.msg .bubble.provider-picker { white-space: normal; }
/* Reset the app-wide label/input styling that would otherwise pad each row out. */
.provider-picker p, .provider-picker div, .provider-picker label { margin: 0; }
.provider-picker .picker-hint { margin: 0 0 0.4rem; font-weight: 600; }
.provider-picker .picker-group { margin: 0.3rem 0 0; }
.provider-picker .picker-cat { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0.4rem 0 0.1rem; }
.provider-picker .picker-row { display: flex; align-items: baseline; gap: 0.4rem; margin: 0; padding: 0.06rem 0 0.06rem 0.4rem; font-size: 0.9rem; line-height: 1.3; cursor: pointer; }
.provider-picker .picker-row input[type="checkbox"] {
  appearance: auto; -webkit-appearance: auto; display: inline; width: auto; min-width: 0;
  margin: 0; padding: 0; border: 0; background: none; box-shadow: none; flex: 0 0 auto;
}
.provider-picker .picker-actions { margin-top: 0.6rem; display: flex; gap: 0.5rem; flex-wrap: wrap; }
.chat-input { display: flex; gap: 0.5rem; margin-top: 0.75rem; }
.chat-input input { flex: 1; }

/* Typing indicator (three bouncing dots) shown while the agent works. */
.bubble.typing { display: inline-flex; gap: 0.25rem; align-items: center; }
.bubble.typing span {
  width: 0.5rem; height: 0.5rem; border-radius: 50%;
  background: var(--muted); opacity: 0.5; animation: typing-bounce 1s infinite ease-in-out;
}
.bubble.typing span:nth-child(2) { animation-delay: 0.15s; }
.bubble.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce { 0%, 80%, 100% { transform: translateY(0); opacity: 0.4; } 40% { transform: translateY(-0.25rem); opacity: 0.9; } }

/* Phone portrait: single column, collapsible thread list, chat fills the screen. */
@media (max-width: 760px) {
  .chat-layout { grid-template-columns: 1fr; min-height: 0; }
  .chat-threads-toggle { display: inline-block; }
  .chat-threads {
    display: none;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.5rem;
    margin-bottom: 0.5rem;
  }
  .chat-layout.show-threads .chat-threads { display: block; }
  .messages { height: 62vh; }
}

/* General phone-portrait polish. */
@media (max-width: 760px) {
  main { margin: 1rem auto; padding: 0 0.85rem; }
  .topbar { flex-wrap: wrap; gap: 0.6rem 1rem; padding: 0.6rem 0.85rem; }
  .topbar nav { order: 3; flex-basis: 100%; flex-wrap: wrap; gap: 0.4rem 0.9rem; }
  /* Wide tables scroll horizontally instead of breaking the layout. */
  table { display: block; overflow-x: auto; white-space: nowrap; }
  .card { padding: 1rem; }
}

.draft { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.75rem; margin: 0.5rem 0; }
/* Email-action modal: equal-width stacked buttons + a separated cancel. */
.email-actions { display: grid; gap: 0.5rem; margin: 0.75rem 0 0; }
.email-actions .btn { width: 100%; text-align: center; box-sizing: border-box; }
.modal-foot { margin-top: 0.75rem; display: flex; justify-content: flex-end; }

/* Duplicate-on-ingest comparison (new vs existing). */
.dup-compare { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin: 0.75rem 0; }
.dup-col { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.6rem 0.8rem; }
.dup-col h3 { margin: 0 0 0.4rem; font-size: 0.95rem; }
.dup-col.dup-new { border-color: var(--accent); }

.draft-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; margin: 0.75rem 0; }
.draft-card h2 { margin: 0 0 0.5rem; }
/* Opened in the mail app — dim it so the next un-opened card stands out. */
.draft-card.intro-opened { opacity: 0.55; }
.draft-card.intro-opened h2::after { content: " ✓"; color: var(--ok-ink); }
.draft-body { white-space: pre-wrap; color: var(--muted); margin: 0.4rem 0; font-size: 0.95rem; }

/* Busy-on-submit feedback (e.g. document upload + AI extraction) */
.spinner {
  display: inline-block;
  width: 0.85em;
  height: 0.85em;
  /* Sits on an accent-filled button, so it tracks the ink colour chosen for that button. */
  border: 2px solid color-mix(in srgb, var(--accent-ink) 45%, transparent);
  border-top-color: var(--accent-ink);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
  margin-right: 0.35rem;
}
.spinner.dark { border-color: var(--border); border-top-color: var(--accent); }
@keyframes spin { to { transform: rotate(360deg); } }
button.busy { opacity: 0.85; cursor: progress; }
.upload-status { color: var(--muted); margin-top: 0.5rem; }

/* Link-style danger action (e.g. the row "Delete"): red text, no box. */
.link.danger { background: transparent; color: var(--error-ink); padding: 0.15rem 0.4rem; }
/* Solid danger action (e.g. the confirm button in the delete dialog). */
button.danger { background: var(--error-ink); color: var(--danger-ink); }

dialog.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  max-width: 420px;
  box-shadow: 0 10px 40px var(--shadow);
}
dialog.modal h2 { margin-top: 0; }
dialog.modal::backdrop { background: var(--backdrop); }
dialog.modal.wide { max-width: min(760px, 92vw); width: 760px; }
dialog.modal.wide textarea { width: 100%; }

.temp-password code {
  display: inline-block;
  font-size: 1.15rem;
  padding: 0.4rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  letter-spacing: 0.02em;
}

/* Provider document gallery (business cards / multi-page brochures) */
.doc-gallery { display: flex; flex-wrap: wrap; gap: 0.6rem; margin: 0.75rem 0 0.25rem; }
.doc-thumb {
  display: block;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg);
}
.doc-thumb img { display: block; width: 140px; height: 140px; object-fit: cover; }
.doc-thumb .doc-file { display: flex; align-items: center; justify-content: center; width: 140px; height: 140px; padding: 0.5rem; font-size: 0.85rem; color: var(--muted); text-align: center; }
