:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #d9dce1;
  --text: #1b1f24;
  --muted: #5b6572;
  --accent: #2f5fa3;
  --success: #1f7a3d;
  --error: #b3261e;
  --warning: #8a6100;
  --radius: 6px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar .brand {
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
}

.topbar nav { display: flex; gap: 1rem; flex: 1; flex-wrap: wrap; align-items: center; }
.topbar nav a { color: var(--accent); text-decoration: none; }
.topbar nav a:hover { text-decoration: underline; }
.topbar nav .nav-label { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; margin-left: 0.25rem; }

.logout-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
}

.current-client-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
}

.current-client-form label { display: flex; align-items: center; gap: 0.4rem; font-size: 0.9rem; color: var(--muted); }

.actor { color: var(--muted); font-size: 0.9rem; }

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

h1 { font-size: 1.4rem; margin-bottom: 1rem; }
h2 { font-size: 1.1rem; margin-top: 2rem; margin-bottom: 0.5rem; }

.flash {
  padding: 0.6rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
}
.flash-success { background: #e5f5ea; color: var(--success); border: 1px solid #b6e0c3; }
.flash-error { background: #fbe9e7; color: var(--error); border: 1px solid #f3bcb6; }
.flash-warning { background: #fdf3d9; color: var(--warning); border: 1px solid #ecd48c; }

.cash-inflow { color: var(--success); }
.cash-outflow { color: var(--error); }

.login-card {
  max-width: 320px;
  margin: 4rem auto;
  padding: 2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.login-card h1 { font-size: 1.1rem; text-align: center; }
.login-card form { display: flex; flex-direction: column; gap: 0.75rem; }
.login-card label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; color: var(--muted); }

input, textarea, select {
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-family: inherit;
}

button {
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  font-size: 0.9rem;
  cursor: pointer;
}
button:hover { opacity: 0.9; }
button:disabled { background: var(--border); border-color: var(--border); color: var(--muted); cursor: not-allowed; }
button.danger { background: var(--error); border-color: var(--error); }
button.success { background: var(--success); border-color: var(--success); }

.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.data-table th, .data-table td {
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.data-table th { background: #eef1f5; color: var(--muted); font-weight: 600; }
.data-table a { color: var(--accent); text-decoration: none; }
.data-table a:hover { text-decoration: underline; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: #eef1f5;
  color: var(--muted);
  font-size: 0.8rem;
}
.badge-success { background: var(--success); color: white; }
.badge-error { background: var(--error); color: white; }
.badge-info { background: var(--accent); color: white; }
.badge-warning { background: var(--warning); color: white; }

.row-overdue { background: rgba(179, 38, 30, 0.12); }

/* AP due-date color coding (2026-08-03, user-requested; renamed to
   text-danger/text-warning/text-success same day, second pass; !important
   + literal hex third pass, user-requested -- some other rule elsewhere
   was winning the cascade against these, so var(--error)/var(--warning)/
   var(--success) were rendering but not visibly distinct enough) */
.text-danger { color: #ff4444 !important; font-weight: 600; }
.text-warning { color: #ffbb00 !important; font-weight: 600; }
.text-success { color: #44cc44 !important; }

/* Compact per-row action forms (AP "Pay Now", 2026-08-03) */
.inline-form { display: flex; gap: 0.35rem; align-items: center; }
.inline-form select { max-width: 160px; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  font-size: 0.9rem;
}
.detail-grid strong { color: var(--muted); font-weight: 500; font-size: 0.8rem; text-transform: uppercase; }

.actions { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: flex-start; }
.actions form { display: flex; flex-direction: column; gap: 0.4rem; }
.resolve-form textarea { width: 240px; min-height: 3rem; }

.hint { color: var(--muted); font-size: 0.85rem; }

.filter-form { margin-bottom: 1rem; }
.filter-form label { display: flex; align-items: center; gap: 0.5rem; font-size: 0.9rem; color: var(--muted); }

.module-list { list-style: none; padding: 0; }
.module-list a { color: var(--accent); text-decoration: none; font-size: 1rem; }
.module-list a:hover { text-decoration: underline; }

.button {
  display: inline-block;
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  font-size: 0.9rem;
  text-decoration: none;
}
.button:hover { opacity: 0.9; }

/* Create/edit forms reuse .detail-grid's card look; this adds the
   vertical label-above-input stacking every such form across modules
   shares, so a new module's create screen only needs to add
   `.form-grid` alongside `.detail-grid`, not redefine field layout. */
.form-grid label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; color: var(--muted); }
.form-grid .actions { grid-column: 1 / -1; }

/* Budget vs Actual variance table: unfavorable (>10%, directional) rows. */
.row-unfavorable { background: #fbe9e7; }
.row-unfavorable td { color: var(--error); }

/* AR/AP Aging bucket summary tiles -- green=current, yellow=31-60,
   orange=61-90, red=91+, matching the ticket's own color-coding. */
.aging-buckets {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.aging-bucket {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  color: white;
}
.aging-bucket .bucket-label { font-size: 0.8rem; opacity: 0.9; text-transform: uppercase; }
.aging-bucket .bucket-total { font-size: 1.3rem; font-weight: 600; }
.aging-bucket-current { background: var(--success); }
.aging-bucket-31_60 { background: #b58900; }
.aging-bucket-61_90 { background: #c2650f; }
.aging-bucket-91_120, .aging-bucket-over_120 { background: var(--error); }

/* KPI Dashboard per Client: cards, green if healthy / red if warning per
   the ticket's own thresholds (KPIService.is_healthy()); neutral surface
   for metrics with no threshold (net_profit, cash_balance). */
.kpi-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem;
  margin: 1rem 0;
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem 1rem;
}
.kpi-card-label { font-size: 0.8rem; opacity: 0.9; text-transform: uppercase; color: var(--muted); }
.kpi-card-value { font-size: 1.3rem; font-weight: 600; }
.kpi-card-healthy { border-color: var(--success); }
.kpi-card-healthy .kpi-card-value { color: var(--success); }
.kpi-card-warning { border-color: var(--error); }
.kpi-card-warning .kpi-card-value { color: var(--error); }
.kpi-card-benchmark { font-size: 0.75rem; color: var(--muted); margin-top: 0.15rem; }

/* Compliance Deadline Tracker: row color-coding by urgency
   (ComplianceService.deadline_urgency()). */
.row-urgency-red { background: #fbe9e7; }
.row-urgency-red td { color: var(--error); }
.row-urgency-orange { background: #fff3e0; }
.row-urgency-orange td { color: #b58900; }
.row-urgency-grey td { color: var(--muted); }

/* Contract/Subscription Management: list-page tabs (All/Vendor/Customer/
   Expiring Soon). */
.tabs { display: flex; gap: 0.5rem; margin: 0.5rem 0 1rem; }
.tabs a { padding: 0.35rem 0.75rem; border-radius: 4px; border: 1px solid var(--border); color: var(--text); text-decoration: none; }
.tabs a.active { background: var(--accent); color: white; border-color: var(--accent); }

/* AI Natural Language Queries: quick-question buttons + answer panel. */
.quick-questions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.5rem 0 1rem; }
.quick-question-button { background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 0.4rem 0.9rem; font-size: 0.85rem; cursor: pointer; color: var(--text); }
.quick-question-button:hover { border-color: var(--accent); }
.ai-answer { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 1rem; margin-top: 1rem; }
.ai-answer-question { color: var(--muted); }
.ai-answer-text { white-space: pre-wrap; }

/* Batch 1, Task 2: AP invoice line GL Account inline editor (ap/detail.html).
   Task 3: vendor quick-create form on the same page. */
.gl-account-select { max-width: 220px; }
.gl-account-status { margin-left: 0.4rem; font-size: 0.85rem; }
.gl-account-success { color: var(--success); }
.gl-account-error { color: var(--error); }
.quick-add-vendor { margin-top: 1rem; padding: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f24;
    --border: #2c3038;
    --text: #e6e8eb;
    --muted: #9aa2ad;
    --accent: #6ea3e8;
  }
  .data-table th { background: #232730; }
  .badge { background: #232730; }
}
