/* =============================================================================
 * app.css — Northledger application styles (shell + component system).
 * ========================================================================== */

/* ------------------------------- base ----------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text-primary);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
h1, h2, h3, h4 { font-family: var(--font-heading); margin: 0; color: var(--text-primary); font-weight: 600; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; }
::selection { background: var(--primary-soft); }
.num { font-variant-numeric: tabular-nums; font-feature-settings: 'tnum' 1; }
.muted { color: var(--text-muted); }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }
.hide { display: none !important; }

/* scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: rgba(91,100,122,.32); border-radius: 999px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: rgba(91,100,122,.5); background-clip: content-box; }

/* ------------------------------ app shell -------------------------------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }
.app.is-collapsed { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }

.sidebar {
  background: linear-gradient(180deg, var(--primary-dark), var(--primary));
  color: #fff; position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column; z-index: 30;
}
.sidebar__brand { display: flex; align-items: center; gap: var(--s3); padding: 18px var(--s5); height: var(--topbar-h); }
.sidebar__logo { width: 30px; height: 30px; border-radius: var(--r-md); background: #fff; display: grid; place-items: center; flex: 0 0 auto; box-shadow: var(--e1); }
.sidebar__logo svg { width: 20px; height: 20px; }
.sidebar__name { font-family: var(--font-heading); font-weight: 600; font-size: 17px; letter-spacing: -.01em; }
.is-collapsed .sidebar__name, .is-collapsed .nav__group-label, .is-collapsed .nav__item span { display: none; }
.is-collapsed .nav__item { justify-content: center; }

.nav { padding: var(--s3) var(--s3) var(--s6); flex: 1; }
.nav__group-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em; color: rgba(255,255,255,.5); padding: var(--s4) var(--s3) var(--s2); font-weight: 600; }
.nav__item {
  display: flex; align-items: center; gap: var(--s3); padding: 9px var(--s3); border-radius: var(--r-md);
  color: rgba(255,255,255,.82); cursor: pointer; font-size: 13.5px; font-weight: 500; margin-bottom: 2px; transition: background .12s, color .12s;
}
.nav__item:hover { background: rgba(255,255,255,.10); color: #fff; text-decoration: none; }
.nav__item.is-active { background: rgba(255,255,255,.16); color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.nav__item svg { width: 18px; height: 18px; flex: 0 0 auto; }
.nav__item .badge-count { margin-left: auto; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; border-radius: 999px; padding: 1px 7px; }
.is-collapsed .nav__item .badge-count { display: none; }

.sidebar__footer { padding: var(--s4) var(--s5); border-top: 1px solid rgba(255,255,255,.12); font-size: 11.5px; color: rgba(255,255,255,.6); }

/* topbar */
.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: var(--topbar-h); background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: var(--s4); padding: 0 var(--s6); position: sticky; top: 0; z-index: 20;
}
.topbar__toggle { background: none; border: none; cursor: pointer; color: var(--text-muted); padding: 6px; border-radius: var(--r-sm); display: grid; place-items: center; }
.topbar__toggle:hover { background: var(--surface-alt); }
.topbar__title { font-family: var(--font-heading); font-size: 18px; font-weight: 600; }
.topbar__crumb { color: var(--text-muted); font-size: 13px; }
.topbar__spacer { flex: 1; }
.topbar__vertical { font-size: 12px; color: var(--text-muted); background: var(--surface-alt); padding: 5px 12px; border-radius: var(--r-full); border: 1px solid var(--border); }
.topbar__org { display:flex; align-items:center; gap: var(--s2); font-size: 13px; font-weight: 600; }

.outlet { padding: var(--s6); max-width: calc(var(--content-max) + var(--s6) * 2); width: 100%; margin: 0 auto; }

/* ----------------------------- page header ------------------------------- */
.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s4); margin-bottom: var(--s5); flex-wrap: wrap; }
.page-header__title { font-size: 22px; letter-spacing: -.01em; }
.page-header__sub { color: var(--text-muted); font-size: 13.5px; margin-top: 2px; }
.page-header__actions { display: flex; gap: var(--s2); align-items: center; }

/* -------------------------------- panels --------------------------------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); box-shadow: var(--e1); }
.panel + .panel { margin-top: var(--s5); }
.panel__header { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); }
.panel__title { font-size: 15px; font-weight: 600; }
.panel__sub { font-size: 12.5px; color: var(--text-muted); }
.panel__body { padding: var(--s5); }
.panel__body--flush { padding: 0; }

.grid { display: grid; gap: var(--s5); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 1000px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

/* ------------------------------ stat cards ------------------------------- */
.stat-grid { display: grid; grid-template-columns: repeat(4, minmax(150px,1fr)); gap: var(--s4); margin-bottom: var(--s5); }
@media (max-width: 1100px) { .stat-grid { grid-template-columns: repeat(2,1fr); } }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s4) var(--s5); box-shadow: var(--e1); }
.stat__top { display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s2); }
.stat__icon { width: 28px; height: 28px; border-radius: var(--r-sm); display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); }
.stat__icon svg { width: 16px; height: 16px; }
.stat__label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); font-weight: 600; }
.stat__value { font-size: 25px; font-weight: 700; font-family: var(--font-heading); letter-spacing: -.02em; }
.stat__meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; display:flex; align-items:center; gap:6px; }
.stat__delta { display: inline-flex; align-items: center; gap: 3px; font-weight: 600; font-size: 12px; padding: 1px 7px; border-radius: var(--r-full); }
.stat__delta--up { color: var(--success); background: var(--success-soft); }
.stat__delta--down { color: var(--danger); background: var(--danger-soft); }
.stat__delta--flat { color: var(--text-muted); background: var(--surface-alt); }

/* -------------------------------- tables --------------------------------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); font-weight: 600; padding: 11px var(--s4); border-bottom: 1px solid var(--border); white-space: nowrap; position: sticky; top: 0; background: var(--surface); z-index: 1; }
.table td { padding: 12px var(--s4); border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr { transition: background .1s; }
.table tbody tr:hover { background: var(--surface-hover); }
.table.is-zebra tbody tr:nth-child(even) { background: var(--surface-alt); }
.table.is-zebra tbody tr:nth-child(even):hover { background: var(--surface-hover); }
.table .right, .table th.right { text-align: right; }
.table .clickable { cursor: pointer; }
.table.is-compact td { padding: 7px var(--s4); }
.table.is-compact th { padding: 8px var(--s4); }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-primary); }
th.sortable .caret { opacity: .4; margin-left: 4px; }
th.sortable.is-sorted .caret { opacity: 1; color: var(--primary); }
.col-id { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12.5px; color: var(--text-secondary); }

/* --------------------------------- pills --------------------------------- */
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: var(--r-full); font-size: 11.5px; font-weight: 700; line-height: 1.4; white-space: nowrap; }
.pill__dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: 0 0 auto; }
.pill--paid { background: var(--pill-paid-bg); color: var(--pill-paid-fg); }
.pill--sent { background: var(--pill-sent-bg); color: var(--pill-sent-fg); }
.pill--draft { background: var(--pill-draft-bg); color: var(--pill-draft-fg); }
.pill--due { background: var(--pill-due-bg); color: var(--pill-due-fg); }
.pill--overdue { background: var(--pill-overdue-bg); color: var(--pill-overdue-fg); }
.pill--partially_paid, .pill--partial { background: var(--pill-partial-bg); color: var(--pill-partial-fg); }
.pill--void { background: var(--pill-void-bg); color: var(--pill-void-fg); }
.pill--uncollectible { background: var(--pill-uncollectible-bg); color: var(--pill-uncollectible-fg); }

/* behavior band chips */
.band { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
.band__dot { width: 8px; height: 8px; border-radius: 50%; }
.band--Good .band__dot { background: var(--band-good); } .band--Good { color: var(--band-good); }
.band--Fair .band__dot { background: var(--band-fair); } .band--Fair { color: var(--band-fair); }
.band--Risky .band__dot { background: var(--band-risky); } .band--Risky { color: var(--band-risky); }

.tag { display: inline-block; padding: 2px 9px; border-radius: var(--r-full); font-size: 11.5px; font-weight: 600; background: var(--surface-alt); color: var(--text-secondary); border: 1px solid var(--border); }

/* -------------------------------- buttons -------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; height: 36px; padding: 0 16px;
  border-radius: var(--r-md); font-size: 13px; font-weight: 600; cursor: pointer; border: 1px solid transparent;
  transition: background .12s, border-color .12s, transform .04s; white-space: nowrap; text-decoration: none;
}
.btn:active { transform: scale(.985); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-dark); text-decoration: none; }
.btn--secondary { background: var(--surface); color: var(--primary); border-color: var(--border-strong); }
.btn--secondary:hover { background: var(--surface-alt); text-decoration: none; }
.btn--ghost { background: transparent; color: var(--text-secondary); }
.btn--ghost:hover { background: var(--surface-alt); text-decoration: none; }
.btn--danger { background: var(--danger); color: #fff; }
.btn--danger:hover { background: #a5301f; text-decoration: none; }
.btn--accent { background: var(--accent); color: #fff; }
.btn--accent:hover { background: #0c8678; text-decoration: none; }
.btn--sm { height: 30px; padding: 0 11px; font-size: 12.5px; }
.btn--icon { width: 36px; padding: 0; }
.btn--icon.btn--sm { width: 30px; }
.btn--block { width: 100%; }

/* ------------------------------- forms ----------------------------------- */
.field { margin-bottom: var(--s4); }
.field__label { display: block; font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 5px; }
.field__hint { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
.field__error { font-size: 11.5px; color: var(--danger); margin-top: 4px; }
.input, .select, .textarea {
  width: 100%; height: 36px; padding: 0 11px; border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  font-family: inherit; font-size: 13.5px; color: var(--text-primary); background: var(--surface); transition: border-color .12s, box-shadow .12s;
}
.textarea { height: auto; padding: 9px 11px; min-height: 80px; resize: vertical; line-height: 1.5; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--primary); box-shadow: var(--focus); }
.input::placeholder, .textarea::placeholder { color: #9aa3b6; }
select.select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235B647A' stroke-width='2.5'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; padding-right: 30px; }
.input-money { position: relative; }
.input-money::before { content: '$'; position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--text-muted); font-size: 13px; pointer-events: none; }
.input-money .input { padding-left: 22px; text-align: right; font-variant-numeric: tabular-nums; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--s4); }
.form-grid--3 { grid-template-columns: 1fr 1fr 1fr; }
.form-grid .col-span-2 { grid-column: span 2; }
@media (max-width: 720px){ .form-grid, .form-grid--3 { grid-template-columns: 1fr; } .form-grid .col-span-2 { grid-column: auto; } }
.checkbox { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.checkbox input { width: 16px; height: 16px; accent-color: var(--primary); }

/* segmented control */
.seg { display: inline-flex; background: var(--surface-alt); border: 1px solid var(--border); border-radius: var(--r-md); padding: 3px; gap: 2px; }
.seg button { border: none; background: transparent; padding: 6px 12px; border-radius: var(--r-sm); font-size: 12.5px; font-weight: 600; color: var(--text-muted); cursor: pointer; }
.seg button.is-active { background: var(--surface); color: var(--primary); box-shadow: var(--e1); }

/* filter bar */
.filterbar { display: flex; gap: var(--s2); align-items: center; flex-wrap: wrap; margin-bottom: var(--s4); }
.filterbar .input, .filterbar .select { height: 34px; width: auto; }
.filterbar .search { min-width: 240px; }
.filterbar__spacer { flex: 1; }

/* -------------------------------- tabs ----------------------------------- */
.tabs { display: flex; gap: var(--s5); border-bottom: 1px solid var(--border); margin-bottom: var(--s5); }
.tab { padding: 10px 2px; font-size: 13px; font-weight: 600; color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab:hover { color: var(--text-primary); }
.tab.is-active { color: var(--primary); border-bottom-color: var(--primary); }

/* ------------------------------ modal/drawer ----------------------------- */
.scrim { position: fixed; inset: 0; background: rgba(14,23,38,.45); z-index: 100; display: grid; place-items: center; padding: var(--s5); animation: fade .12s ease; }
.drawer-scrim { position: fixed; inset: 0; background: rgba(14,23,38,.45); z-index: 100; display: flex; justify-content: flex-end; animation: fade .12s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal { background: var(--surface); border-radius: var(--r-lg); box-shadow: var(--e3); width: 100%; max-width: 540px; max-height: 90vh; display: flex; flex-direction: column; animation: pop .14s ease; }
.modal--wide { max-width: 860px; }
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: 0; } to { transform: none; opacity: 1; } }
.drawer { background: var(--surface); box-shadow: var(--e3); width: 520px; max-width: 94vw; height: 100vh; display: flex; flex-direction: column; animation: slide .16s ease; }
.drawer--wide { width: 680px; }
@keyframes slide { from { transform: translateX(24px); opacity: .6; } to { transform: none; opacity: 1; } }
.modal__header, .drawer__header { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); flex: 0 0 auto; }
.modal__title, .drawer__title { font-size: 16px; font-weight: 600; }
.modal__body, .drawer__body { padding: var(--s5); overflow: auto; flex: 1; }
.modal__footer, .drawer__footer { display: flex; justify-content: flex-end; gap: var(--s2); padding: var(--s4) var(--s5); border-top: 1px solid var(--border); flex: 0 0 auto; background: var(--surface); }
.modal__close { background: none; border: none; cursor: pointer; color: var(--text-muted); padding: 6px; border-radius: var(--r-sm); display: grid; place-items: center; }
.modal__close:hover { background: var(--surface-alt); color: var(--text-primary); }

/* -------------------------------- toasts --------------------------------- */
.toast-stack { position: fixed; bottom: var(--s5); right: var(--s5); z-index: 200; display: flex; flex-direction: column; gap: var(--s2); }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--info); border-radius: var(--r-md); box-shadow: var(--e2); padding: 12px 14px; max-width: 360px; display: flex; gap: 10px; align-items: flex-start; animation: toastin .18s ease; }
@keyframes toastin { from { transform: translateX(16px); opacity: 0; } to { transform: none; opacity: 1; } }
.toast--success { border-left-color: var(--success); }
.toast--danger { border-left-color: var(--danger); }
.toast--warning { border-left-color: var(--warning); }
.toast__icon { color: var(--info); flex: 0 0 auto; margin-top: 1px; }
.toast--success .toast__icon { color: var(--success); }
.toast--danger .toast__icon { color: var(--danger); }
.toast--warning .toast__icon { color: var(--warning); }
.toast__msg { font-size: 13px; }
.toast__title { font-weight: 600; }

/* ------------------------------ empty state ------------------------------ */
.empty { text-align: center; padding: var(--s8) var(--s5); color: var(--text-muted); }
.empty__icon { width: 44px; height: 44px; margin: 0 auto var(--s4); color: var(--border-strong); }
.empty__icon svg { width: 44px; height: 44px; }
.empty__title { font-size: 16px; font-weight: 600; color: var(--text-secondary); margin-bottom: 6px; }
.empty__desc { font-size: 13.5px; max-width: 380px; margin: 0 auto var(--s4); }

/* ------------------------------ misc bits -------------------------------- */
.avatar { width: 32px; height: 32px; border-radius: var(--r-full); display: grid; place-items: center; font-size: 12px; font-weight: 700; color: #fff; flex: 0 0 auto; }
.cell-main { display: flex; align-items: center; gap: 10px; }
.cell-sub { font-size: 12px; color: var(--text-muted); }

.bar { height: 8px; background: var(--surface-alt); border-radius: var(--r-full); overflow: hidden; }
.bar__fill { height: 100%; border-radius: var(--r-full); background: var(--primary); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px var(--s4); font-size: 13px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; text-align: right; font-weight: 500; }

.divider { height: 1px; background: var(--border); margin: var(--s4) 0; border: none; }
.chip-row { display: flex; flex-wrap: wrap; gap: var(--s2); }

/* aging mini-legend */
.aging-legend { display: flex; gap: var(--s4); flex-wrap: wrap; font-size: 12px; }
.aging-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 5px; vertical-align: -1px; }

/* totals stack on invoice editor */
.totals { margin-left: auto; width: 280px; }
.totals .row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 13.5px; }
.totals .row--grand { border-top: 2px solid var(--text-primary); margin-top: 4px; padding-top: 10px; font-size: 16px; font-weight: 700; }
.totals .row--balance { color: var(--danger); font-weight: 700; }

/* line item editor */
.lineitems { width: 100%; border-collapse: collapse; }
.lineitems th { font-size: 11px; text-transform: uppercase; color: var(--text-muted); text-align: left; padding: 6px 8px; font-weight:600; }
.lineitems td { padding: 4px 8px; vertical-align: top; }
.lineitems .input, .lineitems .select { height: 34px; }
.lineitems input { border-color: var(--border); }

/* worklist priority bar */
.worklist-item { display: flex; align-items: center; gap: var(--s4); padding: 12px var(--s4); border-bottom: 1px solid var(--border); }
.worklist-item:last-child { border-bottom: none; }
.worklist-item:hover { background: var(--surface-hover); }

/* skeleton / loading */
.loading-screen { display: grid; place-items: center; height: 100vh; color: var(--text-muted); gap: var(--s4); }
.spinner { width: 32px; height: 32px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* responsive: stack sidebar on small screens */
@media (max-width: 760px) {
  .app, .app.is-collapsed { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; width: 230px; transform: translateX(-100%); transition: transform .2s; }
  .app.is-mobile-open .sidebar { transform: none; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .outlet { padding: var(--s4); }
}

/* chart sizing */
.chart-box { position: relative; height: 280px; }
.chart-box--sm { height: 220px; }
.donut-legend { display: flex; flex-direction: column; gap: 8px; font-size: 12.5px; }
.donut-legend .row { display: flex; align-items: center; gap: 8px; }
.donut-legend i { width: 10px; height: 10px; border-radius: 3px; }
.donut-legend .val { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 600; }
