:root {
  color-scheme: light;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #1f2937;
  background: #f3f5f8;
}

* { box-sizing: border-box; }
body { margin: 0; }
a { color: #075985; }

.app-shell { min-height: 100vh; }
.app-shell--authenticated {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 24px 18px;
  display: flex;
  flex-direction: column;
  gap: 28px;
  color: #e5eef7;
  background: #17324a;
}
.sidebar a { color: inherit; }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
}
.brand__mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  color: #17324a;
  background: #fff;
}

.sidebar-nav { display: grid; gap: 4px; }
.sidebar-nav__link,
.sidebar-nav__item {
  padding: 9px 10px;
  border-radius: 7px;
  text-decoration: none;
}
.sidebar-nav__link:hover { background: rgba(255, 255, 255, .12); }
.sidebar-nav__item.is-disabled { color: #9db0c1; }

.sidebar-user {
  margin-top: auto;
  padding-top: 18px;
  border-top: 1px solid rgba(255, 255, 255, .18);
  display: grid;
  gap: 7px;
  font-size: .92rem;
}
.sidebar-user__tenant { font-weight: 700; }
.sidebar-user__agency { color: #bfd0df; }

.main-content {
  width: min(100%, 1440px);
  margin: 0 auto;
  padding: 28px;
}
.app-shell:not(.app-shell--authenticated) .main-content {
  display: grid;
  align-items: center;
}

.page-header { margin-bottom: 22px; }
.page-header h1 { margin: 4px 0 8px; font-size: 2rem; }
.eyebrow { margin: 0; color: #64748b; font-weight: 700; letter-spacing: .04em; }
.muted { color: #64748b; }

.flash {
  margin-bottom: 16px;
  padding: 11px 13px;
  border-radius: 8px;
  background: #e0f2fe;
  border: 1px solid #7dd3fc;
}
.flash--alert { background: #fef2f2; border-color: #fca5a5; }
.issued-key { background: #fffbeb; border: 1px solid #fcd34d; border-radius: 6px; padding: 12px 16px; margin: 12px 0; }
.issued-key__token { font-size: 1.05em; word-break: break-all; }

.login-panel {
  width: min(100%, 430px);
  margin: auto;
  padding: 30px;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 12px 35px rgba(15, 23, 42, .10);
}
.login-form, .field { display: grid; gap: 9px; }
.login-form { gap: 17px; margin-top: 22px; }
.field label { font-weight: 700; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 10px 11px;
  border: 1px solid #cbd5e1;
  border-radius: 7px;
  font: inherit;
  background: #fff;
}

.button {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  min-height: 40px;
  padding: 8px 14px;
  border: 0;
  border-radius: 7px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.button--primary { color: #fff; background: #0369a1; }
.button--secondary { color: #17324a; background: #e8eef4; }
.button--danger { color: #fff; background: #b91c1c; }
.button--small { min-height: 30px; padding: 4px 10px; font-size: .88rem; }
.button--full { width: 100%; }
a.button { text-decoration: none; }
/* Inside the dark sidebar, links inherit white text (.sidebar a), which made
   light secondary buttons unreadable (white on #e8eef4). Restore each
   button variant's own text color with higher specificity. */
.sidebar a.button--secondary { color: #17324a; }
.sidebar a.button--primary { color: #fff; }
.sidebar a.button--danger { color: #fff; }

.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.header-actions { display: flex; gap: 10px; flex-wrap: wrap; }

.stack-form { display: grid; gap: 16px; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
}
.form-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.form-panel { max-width: 860px; }
.form-errors {
  margin-bottom: 16px;
  padding: 12px 16px;
  border: 1px solid #fca5a5;
  border-radius: 8px;
  background: #fef2f2;
}
.form-errors ul { margin: 6px 0 0; padding-left: 18px; }

.filter-panel { margin-bottom: 0; }
.filter-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  align-items: end;
}
.filter-form__submit { display: flex; gap: 8px; }

.closing-box {
  margin-top: 16px;
  padding: 14px 16px;
  border: 1px solid #e2e8f0;
  border-radius: 9px;
  background: #fbfcfe;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.closing-box p { margin: 0; }

.pipeline-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.inline-form-box {
  margin-top: 18px;
  padding: 16px;
  border: 1px solid #e2e8f0;
  border-radius: 9px;
  background: #fbfcfe;
}
.inline-form-box h3 { margin: 0 0 10px; font-size: 1rem; }

.switch-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 10px 0;
  border-bottom: 1px solid #e5e7eb;
  cursor: pointer;
}
.switch-row input { width: 20px; height: 20px; margin-top: 2px; }
.switch-row span { display: grid; gap: 2px; }

.plain-list { margin: 8px 0 0; padding-left: 18px; }
.actions-cell { white-space: nowrap; }
.actions-cell form { display: inline-block; margin-left: 8px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 18px;
}
.stat-card, .panel {
  border: 1px solid #dde3ea;
  border-radius: 12px;
  background: #fff;
}
.stat-card { padding: 18px; }
.stat-card h2, .panel h2 { margin: 0; font-size: 1.05rem; }
.stat-card__value { margin: 10px 0 4px; font-size: 1.65rem; font-weight: 800; }
.stat-card__unit { margin-left: 4px; font-size: 1rem; }

.content-grid, .detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}
.panel { padding: 20px; overflow: hidden; }
.panel__header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  margin-bottom: 14px;
}
.section-spacing { margin-top: 18px; }

.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td {
  padding: 9px 8px;
  border-bottom: 1px solid #e5e7eb;
  text-align: left;
  vertical-align: top;
}
.data-table th { color: #475569; font-weight: 700; }
.data-table .numeric { text-align: right; white-space: nowrap; }
.table-scroll { overflow-x: auto; }

.tree-level { list-style: none; margin: 0; padding-left: 24px; }
.tree-level--root { padding-left: 0; }
.tree-node { margin: 8px 0; }
.tree-node__body {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 11px 12px;
  border: 1px solid #e2e8f0;
  border-radius: 9px;
  background: #fbfcfe;
}
.tree-node__name { font-weight: 700; text-decoration: none; }
.tree-node__name:hover { text-decoration: underline; }
.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 7px;
  border-radius: 999px;
  color: #075985;
  background: #e0f2fe;
  font-size: .78rem;
  font-weight: 700;
}
.badge--muted { color: #475569; background: #e2e8f0; }

.billing-month { padding: 16px 0 4px; border-top: 1px solid #e5e7eb; }
.billing-month:first-of-type { border-top: 0; }
.billing-month__header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: baseline;
}
.billing-month__header h3 { margin: 0 0 10px; }
.billing-month__header .muted { margin-left: 12px; }

.agency-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}
.agency-card {
  padding: 16px;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
}
.agency-card h3 { margin: 0 0 8px; }

@media (max-width: 760px) {
  .app-shell--authenticated { display: block; }
  .sidebar { position: static; height: auto; }
  .main-content { padding: 18px; }
  .stats-grid, .content-grid, .detail-grid { grid-template-columns: 1fr; }
  .tree-node__body, .billing-month__header { display: block; }
}

/* --- Payment statement sheet (A4) ------------------------------------- */
.statement-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  max-width: 820px;
  margin-bottom: 16px;
}
.statement-actions p { margin: 0; }

.statement-sheet {
  position: relative;
  max-width: 820px;
  padding: 40px 44px;
  background: #fff;
  border: 1px solid #cbd5e1;
  color: #111827;
}
.statement-sheet__draft {
  position: absolute;
  top: 18px;
  right: 24px;
  margin: 0;
  padding: 4px 14px;
  border: 2px solid #b91c1c;
  border-radius: 6px;
  color: #b91c1c;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: .3em;
}
.statement-sheet__header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  border-bottom: 3px double #334155;
  padding-bottom: 14px;
}
.statement-sheet__header h1 { margin: 6px 0 0; font-size: 1.7rem; letter-spacing: .2em; }
.statement-meta th { text-align: left; padding-right: 14px; font-weight: 600; white-space: nowrap; }
.statement-meta td { text-align: right; white-space: nowrap; }
.statement-parties {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 32px;
  margin: 22px 0 6px;
}
.statement-party h2 { margin: 0 0 8px; font-size: .85rem; color: #475569; }
.statement-party p { margin: 2px 0; }
.statement-party__name { font-size: 1.15rem; font-weight: 700; }
.statement-lead { margin: 14px 0; }
.statement-lines { width: 100%; border-collapse: collapse; margin-top: 10px; }
.statement-lines th, .statement-lines td {
  border: 1px solid #94a3b8;
  padding: 6px 8px;
  font-size: .9rem;
  text-align: left;
}
.statement-lines th { background: #f1f5f9; }
.statement-lines .numeric, .statement-totals .numeric { text-align: right; }
.statement-totals { margin: 14px 0 0 auto; border-collapse: collapse; min-width: 280px; }
.statement-totals th, .statement-totals td {
  border: 1px solid #94a3b8;
  padding: 6px 10px;
  text-align: left;
}
.statement-totals__total th, .statement-totals__total td { font-weight: 700; background: #f1f5f9; }
.statement-footer { margin-top: 22px; border-top: 1px solid #cbd5e1; padding-top: 10px; }
.statement-footer p { margin: 3px 0; }

/* Print only the statement sheet (browser print -> PDF on A4). */
@media print {
  @page { size: A4; margin: 12mm; }
  body * { visibility: hidden; }
  .statement-sheet, .statement-sheet * { visibility: visible; }
  .statement-sheet {
    position: absolute;
    inset: 0;
    max-width: none;
    border: 0;
    padding: 0;
  }
  .no-print { display: none !important; }
}
