:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1a1f2c;
  --muted: #5d6677;
  --border: #e3e7ee;
  --accent: #2f6feb;
  --accent-ink: #ffffff;
  --error-bg: #fde8e8;
  --error-ink: #8a1a1a;
  --ok-bg: #e6f4ea;
  --ok-ink: #1c5b2a;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

.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); }
.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); }

main { max-width: 1100px; margin: 1.5rem auto; padding: 0 1.25rem; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem 1.5rem;
  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: 6px;
  background: #fff;
}
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-size: 1rem;
  border-radius: 6px;
  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: 6px; }
.flash { margin: 1rem auto; max-width: 1100px; padding: 0.5rem 0.75rem; border-radius: 6px; }
.flash-ok { background: var(--ok-bg); color: var(--ok-ink); }
.flash-error { background: var(--error-bg); color: var(--error-ink); }
.flash-info { background: #e7eefc; color: #1c3d80; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.55rem 0.75rem; 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; }
td.actions .link { padding: 0.15rem 0.4rem; }

.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; }

.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); }

.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: 6px; }
.chat-threads li.active { background: var(--bg); }
.chat-threads li a { text-decoration: none; color: var(--ink); }
.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: 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-user .bubble { background: var(--accent); color: #fff; }
.msg-assistant .bubble { background: var(--bg); border: 1px solid var(--border); }
.msg-tool .tool-note { font-size: 0.8rem; color: var(--muted); }
.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: 8px; padding: 0.75rem; margin: 0.5rem 0; }
.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;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  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.danger { color: var(--error-ink); }
button.danger { background: var(--error-ink); color: #fff; }

dialog.modal {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem 1.5rem;
  max-width: 420px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
dialog.modal h2 { margin-top: 0; }
dialog.modal::backdrop { background: rgba(0, 0, 0, 0.35); }

.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: 6px;
  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: 8px;
  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; }
