*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --brand: #1a56a0;
  --brand-dark: #123d7a;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --bg: #f1f5f9;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --xero-blue: #1ab4d7;
  --sm8-orange: #f97316;
  --dash-purple: #7c3aed;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

/* ── Header ── */
.header {
  background: var(--brand);
  color: #fff;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}
.header h1 { font-size: 18px; font-weight: 600; letter-spacing: -.3px; }
.header-right { display: flex; align-items: center; gap: 16px; font-size: 13px; }
.staff-pill {
  background: rgba(255,255,255,.15);
  border: 1px solid rgba(255,255,255,.3);
  border-radius: 20px;
  padding: 4px 12px;
  cursor: pointer;
  display: flex; align-items: center; gap: 6px;
}
.staff-pill:hover { background: rgba(255,255,255,.25); }
.refresh-info { opacity: .75; font-size: 12px; }

/* ── Connection banner ── */
.banner {
  background: #fef3c7;
  border-bottom: 1px solid #fcd34d;
  padding: 10px 24px;
  font-size: 13px;
  display: flex; align-items: center; gap: 8px;
}
.banner a { color: var(--brand); font-weight: 600; }
.banner.hidden { display: none; }

/* ── Main layout ── */
.main { max-width: 1200px; margin: 0 auto; padding: 24px 16px; }

/* ── Trend chart ── */
.chart-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 24px;
  margin-bottom: 24px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.chart-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.chart-header h2 { font-size: 16px; font-weight: 600; }
.chart-header .chart-legend { display: flex; gap: 16px; font-size: 12px; align-items: center; }
.legend-dot {
  width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 4px;
}
.days-select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  background: white;
  cursor: pointer;
}
#trendCanvas { width: 100% !important; height: 180px !important; }

/* ── Tabs ── */
.tabs { display: flex; gap: 0; margin-bottom: 20px; border-bottom: 2px solid var(--border); }
.tab-btn {
  padding: 10px 20px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all .15s;
}
.tab-btn.active { color: var(--brand); border-bottom-color: var(--brand); }
.tab-btn .badge {
  background: var(--border);
  color: var(--muted);
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 11px;
  margin-left: 6px;
}
.tab-btn.active .badge { background: var(--brand); color: white; }

/* ── Search bar ── */
.search-bar-wrap {
  display: flex; align-items: center; gap: 12px; margin-bottom: 16px;
}
.search-inner {
  position: relative; flex: 1;
  display: flex; align-items: center;
}
.search-icon {
  position: absolute; left: 12px; color: var(--muted); pointer-events: none;
}
.search-input {
  width: 100%;
  border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 36px 9px 36px;
  font-size: 14px; font-family: inherit;
  background: var(--card);
  transition: border-color .15s, box-shadow .15s;
}
.search-input:focus {
  outline: none; border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(26,86,160,.1);
}
.search-input::-webkit-search-cancel-button { display: none; }
.search-clear {
  position: absolute; right: 10px;
  background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 13px; padding: 2px 4px; border-radius: 4px;
}
.search-clear:hover { background: var(--border); }
.search-clear.hidden { display: none; }
.search-count {
  font-size: 13px; color: var(--muted); white-space: nowrap; flex-shrink: 0;
}
.search-count.hidden { display: none; }

/* ── Invoice grid ── */
.invoice-grid { display: flex; flex-direction: column; gap: 16px; }
.loading { text-align: center; padding: 60px; color: var(--muted); font-size: 16px; }
.empty { text-align: center; padding: 60px; color: var(--muted); }
.empty svg { margin: 0 auto 12px; display: block; opacity: .3; }

/* ── Invoice card ── */
.invoice-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  transition: box-shadow .15s;
}
.invoice-card:hover { box-shadow: 0 4px 16px rgba(0,0,0,.1); }
.invoice-card.called-today { border-left: 4px solid var(--success); }

.card-top {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  margin-bottom: 14px;
}
.card-left { flex: 1 1 auto; }
.invoice-ref { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.contact-name { font-size: 17px; font-weight: 700; color: var(--text); }
.amount-badge {
  font-size: 18px; font-weight: 700; color: var(--danger);
  white-space: nowrap;
}
.days-overdue {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  padding: 2px 8px; border-radius: 20px;
  background: #fee2e2; color: var(--danger);
}
.days-overdue.warn { background: #fef3c7; color: var(--warning); }

.contact-row {
  display: flex; flex-wrap: wrap; gap: 12px;
  margin-bottom: 14px; font-size: 13px;
}
.contact-item { display: flex; align-items: center; gap: 5px; color: var(--muted); }
.contact-item a { color: var(--brand); text-decoration: none; }
.contact-item a:hover { text-decoration: underline; }
.sm8-tag {
  font-size: 11px; background: #fff7ed; color: var(--sm8-orange);
  border: 1px solid #fed7aa; border-radius: 4px; padding: 1px 6px;
}
.inv-link { color: var(--muted); text-decoration: none; font-size: 12px; }
.inv-link:hover { color: var(--brand); text-decoration: underline; }

/* ── Xero warning ── */
.xero-warning {
  font-size: 11px; font-weight: 600;
  color: #92400e; background: #fef3c7;
  border: 1px solid #fcd34d; border-radius: 6px;
  padding: 4px 10px; margin-bottom: 10px;
  display: inline-block;
}

/* ── Action buttons ── */
.card-actions { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }

.btn {
  border: none; border-radius: 7px; padding: 8px 16px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  transition: all .15s;
}
.btn-call {
  background: var(--brand); color: white;
}
.btn-call:hover { background: var(--brand-dark); }
.btn-call.called { background: var(--success); }
.btn-call.called:hover { background: #15803d; }
.btn-paynow {
  background: #16a34a; color: white;
}
.btn-paynow:hover { background: #15803d; }
.btn-paynow:disabled { opacity: .6; cursor: not-allowed; }
.btn-email {
  background: #f1f5f9; color: var(--muted);
  border: 1px solid var(--border);
  cursor: not-allowed;
  opacity: .65;
}
.called-status {
  font-size: 12px; color: var(--success); font-weight: 500;
  display: flex; align-items: center; gap: 4px;
}

/* ── Undo call ── */
.btn-undo-call {
  background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 11px; padding: 0 0 0 8px;
  text-decoration: underline; font-weight: 500;
}
.btn-undo-call:hover { color: var(--danger); }

/* ── Expected payment ── */
.expect-badge {
  display: inline-block; font-size: 11px; font-weight: 600;
  background: #eff6ff; color: #1d4ed8; border: 1px solid #bfdbfe;
  border-radius: 4px; padding: 2px 8px; margin-bottom: 8px;
}
.expect-form {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 12px; flex-wrap: wrap;
}
.expect-label { font-size: 12px; color: var(--muted); }
.expect-date-input {
  border: 1px solid var(--border); border-radius: 6px;
  padding: 4px 8px; font-size: 12px; font-family: inherit;
  cursor: pointer;
}
.expect-date-input:focus { outline: none; border-color: var(--brand); }
.btn-clear-expect {
  background: none; border: 1px solid var(--border); border-radius: 5px;
  padding: 3px 8px; font-size: 11px; cursor: pointer; color: var(--muted);
}
.btn-clear-expect:hover { background: #fee2e2; color: var(--danger); border-color: #fca5a5; }

/* ── Notes section ── */
.notes-section { border-top: 1px solid var(--border); padding-top: 14px; }
.notes-toggle {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; background: none; border: none; cursor: pointer;
  padding: 0 0 10px 0; font-size: 13px; font-weight: 600;
  color: var(--muted); text-transform: uppercase; letter-spacing: .5px;
}
.notes-toggle:hover { color: var(--brand); }
.notes-toggle .chevron { transition: transform .2s; flex-shrink: 0; }
.notes-body { }
.notes-body.hidden { display: none; }

.notes-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.note-item {
  display: flex; gap: 8px; align-items: flex-start;
  font-size: 13px;
}
.note-source {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px;
  padding: 2px 6px; border-radius: 4px; white-space: nowrap; margin-top: 1px;
  flex-shrink: 0;
}
.note-source.xero { background: #e0f5fb; color: var(--xero-blue); }
.note-source.sm8  { background: #fff7ed; color: var(--sm8-orange); }
.note-source.dashboard { background: #f3f4ff; color: var(--dash-purple); }
.note-body { flex: 1; }
.note-text { color: var(--text); }
.note-meta { font-size: 11px; color: var(--muted); margin-top: 1px; }
.no-notes { font-size: 13px; color: var(--muted); font-style: italic; }

/* ── Add note form ── */
.add-note-form { display: flex; gap: 8px; align-items: flex-start; }
.note-input {
  flex: 1; border: 1px solid var(--border); border-radius: 7px;
  padding: 8px 12px; font-size: 13px; font-family: inherit;
  resize: vertical; min-height: 38px;
}
.note-input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(26,86,160,.1); }
.btn-save-note {
  background: var(--brand); color: white;
  padding: 8px 14px; flex-shrink: 0;
}
.btn-save-note:hover { background: var(--brand-dark); }
.btn-save-note:disabled { opacity: .5; cursor: not-allowed; }

/* ── Manual link ── */
.manual-link-form {
  display: flex; align-items: center; gap: 8px; margin-top: 8px;
  font-size: 12px; color: var(--muted);
}
.manual-link-input {
  border: 1px solid var(--border); border-radius: 5px;
  padding: 4px 8px; font-size: 12px; width: 200px;
}
.btn-link-sm8 {
  background: #fff7ed; color: var(--sm8-orange);
  border: 1px solid #fed7aa; border-radius: 5px;
  padding: 4px 10px; font-size: 12px; cursor: pointer; font-weight: 600;
}
.btn-link-sm8:hover { background: #ffe4c4; }

/* ── Staff dropdown ── */
.staff-pill-wrap { position: relative; }
.pill-chevron { opacity: .7; margin-left: 2px; }
.staff-dropdown {
  position: absolute; top: calc(100% + 8px); right: 0;
  background: white; border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
  min-width: 180px; overflow: hidden; z-index: 150;
  border: 1px solid var(--border);
}
.staff-dropdown.hidden { display: none; }
.dropdown-item {
  display: block; width: 100%; text-align: left;
  padding: 10px 16px; font-size: 13px; font-weight: 500;
  background: none; border: none; cursor: pointer; color: var(--text);
}
.dropdown-item:hover { background: var(--bg); }
.dropdown-item--danger { color: var(--danger); }
.dropdown-item--danger:hover { background: #fff5f5; }
.dropdown-divider { border-top: 1px solid var(--border); margin: 4px 0; }

/* ── Auth / modal ── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: white; border-radius: 12px; padding: 32px;
  width: 360px; box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal h2 { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.modal p { color: var(--muted); margin-bottom: 20px; font-size: 14px; }
.modal-input {
  width: 100%; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 14px; font-size: 15px; margin-bottom: 12px; display: block;
}
.modal-input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(26,86,160,.1); }
.modal-error {
  background: #fef2f2; border: 1px solid #fca5a5; color: var(--danger);
  border-radius: 7px; padding: 8px 12px; font-size: 13px; margin-bottom: 12px;
}
.modal-error.hidden { display: none; }
.modal-link-row { text-align: center; margin-top: 14px; font-size: 13px; color: var(--muted); }
.modal-link-row a { color: var(--brand); text-decoration: none; }
.modal-link-row a:hover { text-decoration: underline; }
.modal-link-row.hidden { display: none; }
.btn-modal-submit {
  width: 100%; background: var(--brand); color: white;
  border: none; border-radius: 8px; padding: 11px;
  font-size: 15px; font-weight: 600; cursor: pointer;
}
.btn-modal-submit:hover { background: var(--brand-dark); }
.btn-modal-submit:disabled { opacity: .6; cursor: not-allowed; }
.btn-modal-cancel {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 11px 18px; font-size: 14px; cursor: pointer; font-weight: 500;
  flex-shrink: 0;
}
.btn-modal-cancel:hover { background: var(--border); }

/* ── Toast notifications ── */
.toast-container {
  position: fixed; bottom: 24px; right: 24px;
  display: flex; flex-direction: column; gap: 8px; z-index: 300;
}
.toast {
  background: #1e293b; color: white;
  padding: 12px 18px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,.3);
  animation: slideIn .2s ease;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
@keyframes slideIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* ── SM8 contact diff ── */
.sm8-contact-row {
  margin-top: 2px;
  font-size: 12px;
}
.sm8-contact-label {
  color: var(--muted);
  font-size: 11px;
  font-style: italic;
  margin-right: 4px;
}
.sm8-diff a {
  color: var(--sm8-orange);
  text-decoration: none;
}
.sm8-diff a:hover { text-decoration: underline; }

@media (max-width: 640px) {
  .header h1 { font-size: 15px; }
  .card-top { flex-direction: column; }
  .amount-badge { font-size: 15px; }
  .add-note-form { flex-direction: column; }
  .btn-save-note { width: 100%; justify-content: center; }
}

/* ── AR Stage Badges ── */
.ar-stage-badge {
  display: inline-block;
  font-size: 10px; font-weight: 700; letter-spacing: .3px;
  padding: 2px 7px; border-radius: 20px;
  vertical-align: middle; margin-left: 4px;
  text-transform: uppercase;
}
.ar-stage-badge--stage1   { background: #fef3c7; color: #92400e; border: 1px solid #f59e0b; }
.ar-stage-badge--stage2   { background: #ffedd5; color: #9a3412; border: 1px solid #f97316; }
.ar-stage-badge--stage3   { background: #fee2e2; color: #991b1b; border: 1px solid #ef4444; }
.ar-stage-badge--stage4   { background: #fecaca; color: #7f1d1d; border: 1px solid #dc2626; }
.ar-stage-badge--collections { background: #1f2937; color: #f9fafb; border: 1px solid #374151; }

/* ── Email button ── */
.btn-email[data-email-num] {
  background: #fff8ed; color: #9a3412;
  border: 1px solid #f97316;
}
.btn-email[data-email-num]:hover {
  background: #ffedd5;
}

/* ── Last email badge ── */
.last-email-badge {
  display: block;
  font-size: 11px; color: #6366f1;
  margin-top: 4px;
  padding: 3px 8px; background: #eef2ff;
  border-radius: 4px; border: 1px solid #c7d2fe;
}

/* ── Call outcome modal ── */
.modal--narrow { max-width: 320px; }
.outcome-btns {
  display: flex; flex-direction: column; gap: 10px; margin-top: 8px;
}
.btn-outcome {
  padding: 14px 16px; border-radius: 8px; border: 2px solid transparent;
  font-size: 15px; font-weight: 600; cursor: pointer; transition: all .15s;
}
.btn-outcome--reached  { background: #f0fdf4; color: #166534; border-color: #22c55e; }
.btn-outcome--reached:hover  { background: #dcfce7; }
.btn-outcome--noanswer { background: #fff7ed; color: #9a3412; border-color: #f97316; }
.btn-outcome--noanswer:hover { background: #ffedd5; }

/* ── Email template modal ── */
.modal--wide { max-width: 640px; width: 96vw; }
.email-modal-header {
  display: flex; justify-content: space-between; align-items: flex-start;
  margin-bottom: 12px;
}
.email-modal-header h2 { margin: 0; }
.email-modal-stage { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.modal-close-x {
  background: none; border: none; font-size: 18px; cursor: pointer;
  color: var(--muted); padding: 4px 8px; line-height: 1;
}
.modal-close-x:hover { color: var(--danger); }
.email-field-row {
  display: flex; gap: 8px; align-items: center;
  padding: 6px 0; border-bottom: 1px solid #f1f5f9;
  font-size: 13px;
}
.email-field-label { color: var(--muted); width: 56px; flex-shrink: 0; font-weight: 600; }
.email-field-value { color: #1e293b; word-break: break-all; }
.email-body-textarea {
  width: 100%; margin-top: 12px;
  font-family: monospace; font-size: 12px; line-height: 1.55;
  border: 1px solid #e2e8f0; border-radius: 6px;
  padding: 10px 12px; resize: vertical; color: #1e293b;
  background: #f8fafc; box-sizing: border-box;
}
.email-modal-actions {
  display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap;
}
.btn-success { background: #22c55e; color: white; border: none; }
.btn-success:hover { background: #16a34a; }
.btn-secondary { background: #f1f5f9; color: #334155; border: 1px solid #cbd5e1; }
.btn-secondary:hover { background: #e2e8f0; }
.btn-primary { background: var(--primary); color: white; border: none; }
.btn-primary:hover { background: var(--primary-dark, #1a4a8a); }
.email-modal-note {
  font-size: 11px; color: var(--muted); margin-top: 8px; margin-bottom: 0;
}

/* ── AR Process Guide ── */
.ar-guide {
  background: #f8faff; border: 1px solid #c7d2fe;
  border-radius: 10px; margin-bottom: 20px; overflow: hidden;
}
.ar-guide-toggle {
  width: 100%; display: flex; align-items: center; gap: 8px;
  padding: 12px 16px; background: none; border: none; cursor: pointer;
  font-size: 13px; color: #1e3a8a; text-align: left;
}
.ar-guide-toggle svg { transition: transform .2s; flex-shrink: 0; }
.ar-guide-toggle:hover { background: #eff6ff; }
.ar-guide-hint { color: #6b7280; font-weight: 400; font-size: 12px; }
.ar-guide-body { padding: 0 16px 16px; }
.ar-guide-body.hidden { display: none; }
.ar-steps { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.ar-step {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 10px 12px; border-radius: 8px; border-left: 3px solid;
}
.ar-step--pre         { background: #f0f9ff; border-color: #7dd3fc; }
.ar-step--1           { background: #fef9c3; border-color: #f59e0b; }
.ar-step--2           { background: #fff7ed; border-color: #f97316; }
.ar-step--3           { background: #fef2f2; border-color: #ef4444; }
.ar-step--4           { background: #fdf2f8; border-color: #a855f7; }
.ar-step-icon         { font-size: 16px; flex-shrink: 0; margin-top: 1px; }
.ar-step-content      { display: flex; flex-direction: column; gap: 2px; }
.ar-step-content strong { font-size: 13px; color: #1e293b; }
.ar-step-content span  { font-size: 12px; color: #4b5563; }
.ar-guide-footer {
  font-size: 11px; color: var(--muted); padding-top: 10px;
  border-top: 1px solid #e0e7ff; margin: 0;
}
.ar-guide-footer a { color: #6366f1; }
