/* ========== Reset & Base ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --border: #e2e5ea;
  --text: #1e293b;
  --text-secondary: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #eef2ff;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fef2f2;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.08);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

/* ========== Header ========== */
header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-inner {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.02em;
}

nav {
  display: flex;
  gap: 4px;
}

.tab {
  background: none;
  border: none;
  padding: 8px 16px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.15s;
}

.tab:hover { color: var(--text); background: var(--bg); }
.tab.active { color: var(--primary); background: var(--primary-light); }

/* ========== Main ========== */
main {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px;
}

.tab-content { display: none; }
.tab-content.active { display: block; }

/* ========== Cards ========== */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 16px;
}

/* ========== Run Report ========== */
.run-controls {
  display: flex;
  gap: 16px;
  align-items: flex-end;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 0.8rem; font-weight: 500; color: var(--text-secondary); }

.field-check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  padding-bottom: 8px;
}
.field-check input[type="checkbox"] { cursor: pointer; }

select, input[type="number"], input[type="text"] {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.875rem;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  min-width: 140px;
}

select:focus, input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,0.1);
}

/* ========== Buttons ========== */
.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 9px 20px;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
  font-family: inherit;
}

.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 9px 20px;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}

.btn-secondary:hover { background: var(--bg); }
.btn-sm { padding: 6px 12px; font-size: 0.8rem; }

/* Send / Copy action bar */
.send-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.btn-send {
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid var(--border);
  font-family: inherit;
  transition: all 0.15s;
}

.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-send-email {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-send-email:hover { background: var(--primary-hover); }

.btn-send-whatsapp {
  background: #25d366;
  color: #fff;
  border-color: #25d366;
}
.btn-send-whatsapp:hover { background: #1da851; }

.btn-send-copy {
  background: var(--card);
  color: var(--text);
}
.btn-send-copy:hover { background: var(--bg); }

.send-actions span {
  font-size: 0.8rem;
  margin-left: 4px;
}

/* ========== Table ========== */
.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th {
  text-align: right;
  padding: 10px 14px;
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  border-bottom: 2px solid var(--border);
}

th:first-child { text-align: left; }

td {
  padding: 12px 14px;
  text-align: right;
  border-bottom: 1px solid var(--border);
}

td:first-child {
  text-align: left;
  font-weight: 500;
}

tbody tr:hover { background: #fafbfc; }

tfoot td {
  font-weight: 700;
  border-top: 2px solid var(--text);
  border-bottom: none;
}

.cash-cell {
  color: var(--green);
  font-weight: 600;
}

.referral-note {
  margin-top: 3px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
}

/* ========== Progress ========== */
.progress-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ========== Modal ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal {
  background: var(--card);
  border-radius: var(--radius);
  padding: 28px;
  width: 90%;
  max-width: 680px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}

.modal h3 { font-size: 1rem; font-weight: 600; margin-bottom: 4px; }
.modal-desc { font-size: 0.8rem; color: var(--text-secondary); margin-bottom: 20px; }

.manual-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1.5fr;
  gap: 12px;
  align-items: end;
  margin-bottom: 12px;
}

.manual-row .clinician-label {
  font-size: 0.8rem;
  font-weight: 500;
  padding-bottom: 10px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

/* ========== Breakdown modal ========== */
.btn-send-breakdown {
  background: var(--card);
  color: var(--text);
}
.btn-send-breakdown:hover { background: var(--bg); }

.modal-wide { max-width: 920px; }

#breakdown-body { max-height: 62vh; overflow-y: auto; }

.breakdown-clin {
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.breakdown-clin:last-child { margin-bottom: 0; }

.breakdown-clin-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 14px;
  cursor: pointer;
  user-select: none;
  background: var(--bg);
}
.breakdown-clin-head:hover { background: #eef0f3; }

.breakdown-caret {
  display: inline-block;
  transition: transform 0.15s;
  margin-right: 6px;
  color: var(--text-secondary);
  font-size: 0.7rem;
}
.breakdown-clin.open .breakdown-caret { transform: rotate(90deg); }

.breakdown-clin-name { font-weight: 600; font-size: 0.9rem; }

.breakdown-clin-stat {
  font-weight: 400;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.breakdown-clin-totals {
  font-size: 0.78rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Collapsed by default; only the open section shows its body. */
.breakdown-clin-body { display: none; padding: 4px 14px 14px; }
.breakdown-clin.open .breakdown-clin-body { display: block; }

.outlier-badge {
  background: var(--red-bg);
  color: var(--red);
  font-weight: 600;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.breakdown-units-summary {
  font-size: 0.8rem;
  color: var(--text);
  background: var(--bg);
  border-radius: 6px;
  padding: 8px 10px;
  margin: 8px 0 10px;
  line-height: 1.7;
}
.breakdown-units-summary strong { font-weight: 600; }
.breakdown-units-total {
  display: inline-block;
  margin-left: 8px;
  font-weight: 600;
  color: var(--text-secondary);
}

.breakdown-outliers {
  margin-top: 12px;
  background: var(--red-bg);
  border: 1px solid #f6c9c9;
  border-radius: 6px;
  padding: 10px 12px;
}
.breakdown-outliers-head { font-weight: 600; font-size: 0.82rem; color: var(--red); margin-bottom: 4px; }
.breakdown-outliers ul { margin: 4px 0 0 16px; padding: 0; }
.breakdown-outliers li { margin: 2px 0; font-size: 0.8rem; }
.breakdown-outliers li.unpaid { color: var(--red); font-weight: 500; }
.breakdown-outliers-note { font-size: 0.75rem; color: var(--text-secondary); margin: 6px 0 0; }

.breakdown-table tr.outlier-row td { background: #fff8f0; }

.breakdown-note {
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-style: italic;
  padding: 4px 0 2px;
}

.breakdown-table th, .breakdown-table td { white-space: nowrap; }

/* ========== History ========== */
.history-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow);
}

.history-card:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(79,70,229,0.1);
}

.history-card-title {
  font-weight: 600;
  font-size: 0.9rem;
}

.history-card-meta {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

.history-card-amount {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

/* ========== Settings ========== */
.settings-section {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.settings-section h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.settings-section .badge {
  font-size: 0.7rem;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--primary-light);
  color: var(--primary);
}

.settings-grid {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px 18px;
}

.settings-field { flex: 0 0 auto; }

.settings-field label {
  display: block;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.settings-field input {
  width: 84px;
  min-width: 0;            /* defeats the global min-width:140px */
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
  font-family: inherit;
}

/* Hide number-input spinners so percentage/rate boxes can be snug */
.settings-field input[type=number],
.exc-table input[type=number] { -moz-appearance: textfield; }
.settings-field input[type=number]::-webkit-outer-spin-button,
.settings-field input[type=number]::-webkit-inner-spin-button,
.exc-table input[type=number]::-webkit-outer-spin-button,
.exc-table input[type=number]::-webkit-inner-spin-button {
  -webkit-appearance: none; margin: 0;
}

.settings-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}

#settings-status {
  font-size: 0.8rem;
  color: var(--green);
}

/* Computed (read-only) aspier display */
.settings-field.readonly .computed-value,
.exc-aspier {
  display: block;
  padding: 7px 10px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-weight: 500;
}

/* Match the read-only Aspier box to the narrow input width */
.settings-field.readonly .computed-value { width: 84px; }

/* Intake sub-section header within clinician cards */
.settings-subsection-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
  margin: 16px 0 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

/* Miller exceptions table */
.exc-table { width: 100%; margin-top: 12px; }
.exc-table th { font-size: 0.7rem; padding: 6px 10px; }
.exc-table td { padding: 6px 10px; }
.exc-table input { width: 100%; min-width: 0; padding: 5px 8px; font-size: 0.8rem; }
.exc-table input[data-exc-field="payout"] { width: 80px; }
/* %, Aspier, Remove columns hug their content; Client Name takes the rest */
.exc-table th:not(:first-child),
.exc-table td:not(:first-child) { width: 1%; white-space: nowrap; }

.btn-add-exc, .btn-remove-exc {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 0.75rem;
  cursor: pointer;
  font-family: inherit;
}

.btn-add-exc:hover { background: var(--bg); }
.btn-remove-exc { color: var(--red); border-color: var(--red); }
.btn-remove-exc:hover { background: var(--red-bg); }

/* Ignored clients list */
#ignored-clients-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.ignored-client-row { display: flex; gap: 8px; align-items: center; }
.ignored-client-row input {
  flex: 1;
  padding: 6px 8px;
  font-size: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: inherit;
}
.ignored-client-row .btn-remove-exc { flex: none; }

/* ========== Exception Chat ========== */
.exception-chat {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  margin-bottom: 12px;
}

.exception-chat-name {
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.exception-chat-status {
  font-size: 0.7rem;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
}

.exception-chat-status.pending { background: #fef3c7; color: #92400e; }
.exception-chat-status.resolved { background: var(--green-bg); color: var(--green); }

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
  max-height: 200px;
  overflow-y: auto;
}

.chat-bubble {
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.8rem;
  max-width: 85%;
  line-height: 1.4;
}

.chat-bubble.user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 2px;
}

.chat-bubble.ai {
  align-self: flex-start;
  background: var(--bg);
  color: var(--text);
  border-bottom-left-radius: 2px;
}

.chat-result {
  background: var(--green-bg);
  border: 1px solid var(--green);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 0.8rem;
}

.chat-result-explanation { margin-bottom: 6px; }

.chat-result-amounts {
  display: flex;
  gap: 16px;
  font-weight: 600;
  font-size: 0.85rem;
}

.chat-result-amounts .positive { color: var(--green); }
.chat-result-amounts .negative { color: var(--red); }

.chat-result-cash {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  font-size: 0.85rem;
}

.chat-result-cash .cash-label { font-weight: 600; }

.chat-result-cash .cash-chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--accent-soft, rgba(99, 102, 241, 0.12));
  color: var(--accent, #4f46e5);
  font-weight: 600;
  font-size: 0.78rem;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.chat-input-row input {
  flex: 1;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.8rem;
  font-family: inherit;
}

.chat-input-row button {
  padding: 7px 14px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
  font-family: inherit;
}

.chat-input-row button:hover { background: var(--primary-hover); }
.chat-input-row button:disabled { opacity: 0.5; cursor: not-allowed; }

.chat-loading {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  padding: 4px 0;
}

.chat-loading .spinner { width: 16px; height: 16px; border-width: 2px; }

.btn-remove-chat {
  font-size: 0.7rem;
  color: var(--text-secondary);
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: underline;
  font-family: inherit;
}

.btn-remove-chat:hover { color: var(--red); }

/* ========== Utility ========== */
.hidden { display: none !important; }

.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-secondary);
}

.error-banner {
  background: var(--red-bg);
  border: 1px solid var(--red);
  border-radius: 6px;
  padding: 12px 16px;
  margin-top: 12px;
  font-size: 0.85rem;
  color: var(--red);
}

/* ========== Flagged-session review ========== */
.flag-banner {
  background: #fffbeb;
  border: 1px solid #f59e0b;
  border-radius: 6px;
  padding: 12px 16px;
  margin-top: 12px;
  font-size: 0.85rem;
  color: #92400e;
}

.flag-banner-head { font-weight: 600; margin-bottom: 6px; }

.flag-list {
  margin: 0 0 10px 18px;
  padding: 0;
  font-size: 0.82rem;
}

.flag-list li { margin: 2px 0; }

.flag-note { font-size: 0.8rem; color: var(--text-secondary); margin: 0; }

.cash-warning-banner {
  background: #fef2f2;
  border: 1px solid #dc2626;
  border-radius: 6px;
  padding: 12px 16px;
  margin-top: 12px;
  font-size: 0.85rem;
  color: #991b1b;
}

.cash-warning-head { font-weight: 600; margin-bottom: 6px; }

.cash-warning-list {
  margin: 0 0 8px 18px;
  padding: 0;
  font-size: 0.82rem;
}

.cash-warning-list li { margin: 2px 0; }

.cash-warning-note { font-size: 0.8rem; margin: 0 0 10px; color: #b91c1c; }

.chat-flagged {
  background: #fffbeb;
  border: 1px solid #fcd34d;
  border-radius: 6px;
  padding: 8px 10px;
  margin-bottom: 8px;
  font-size: 0.78rem;
  color: #92400e;
}

.chat-flagged ul { margin: 6px 0 0 16px; padding: 0; }
.chat-flagged li { margin: 2px 0; }

/* ========== Responsive ========== */
/* Tablet — gutters start to pinch below the 960px content cap */
@media (max-width: 900px) {
  .card { padding: 20px; }
  .settings-section { padding: 18px; }
  .manual-row { grid-template-columns: 1fr 1fr; }   /* 4-col -> 2-col */
}

/* Phone */
@media (max-width: 640px) {
  main { padding: 16px; }
  .run-controls { flex-direction: column; align-items: stretch; }
  .manual-row { grid-template-columns: 1fr; }
  .chat-result-amounts { flex-direction: column; gap: 4px; }

  /* header: let title + tabs wrap instead of overflowing */
  .header-inner { flex-wrap: wrap; height: auto; gap: 8px; padding: 10px 16px; }
  nav { width: 100%; }
  .tab { flex: 1; text-align: center; padding: 8px 10px; }

  /* tighter cards on small screens */
  .card, .settings-section { padding: 16px; }

  /* modals fill the viewport */
  .modal { width: 95%; padding: 18px; max-height: 90vh; }
  .modal-wide { max-width: none; }
  .modal-actions { flex-wrap: wrap; }
  .send-actions { flex-wrap: wrap; }

  /* wide breakdown tables scroll horizontally instead of overflowing the modal */
  .breakdown-clin-body { overflow-x: auto; }

  /* history rows wrap rather than squashing */
  .history-card { flex-wrap: wrap; gap: 4px; }

  /* comfortable tap targets: settings boxes grow back to full/half width */
  .settings-field input,
  .settings-field.readonly .computed-value { width: 100%; min-width: 0; }
  .settings-field { flex: 1 1 120px; }
}

/* Very narrow guard — keep the header tabs from crowding on small phones */
@media (max-width: 400px) {
  header h1 { font-size: 1rem; }
  .tab { font-size: 0.8rem; padding: 7px 6px; }
}
