/* ============================================================
   Invoice & Billing — shared design system + app shell
   Bound to: Design System Inspired by Airtable
   Palette (verified rule — only these literals may appear):
     #ffffff bg · #181d26 fg (navy) · #1b61c9 accent (Airtable Blue)
     #f8fafc surface · #333333 muted · #e0e2e6 border · #006400 success
   Type: Inter for display + body (per stored system). Mono for numerics.
   ============================================================ */
:root {
  --bg:      #ffffff;
  --surface: #f8fafc;
  --fg:      #181d26;
  --muted:   #333333;
  --border:  #e0e2e6;
  --accent:  #1b61c9;
  --success: #006400;

  /* derived — never introduce raw hex below this line */
  --accent-soft:   color-mix(in oklch, var(--accent) 14%, transparent);
  --success-soft:  color-mix(in oklch, var(--success) 14%, transparent);
  --fg-soft:       color-mix(in oklch, var(--fg) 5%, transparent);
  --shadow-sm:     0 1px 2px color-mix(in oklch, var(--fg) 10%, transparent);
  --shadow-md:     0 4px 16px color-mix(in oklch, var(--fg) 12%, transparent);

  --font-display: 'Inter', system-ui, -apple-system, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  --font-body:    'Inter', system-ui, -apple-system, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  --font-mono:    ui-monospace, 'JetBrains Mono', 'SF Mono', Menlo, Consolas, monospace;

  --fs-h1: 28px;
  --fs-h2: 22px;
  --fs-h3: 17px;
  --fs-lead: 16px;
  --fs-body: 14px;
  --fs-meta: 12px;

  --radius: 8px;
  --radius-lg: 12px;
  --sidebar-w: 244px;
  --container: 1180px;
  --gutter: 24px;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-size: var(--fs-body);
  line-height: 1.55;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}
img, svg { display: block; max-width: 100%; }
a { color: inherit; text-decoration: none; }
button { font: inherit; cursor: pointer; }
p { text-wrap: pretty; }
h1, h2, h3, h4 { text-wrap: balance; margin: 0; }
input, select, textarea { font-family: inherit; }

/* ─── type helpers ─── */
.h1, h1 { font-family: var(--font-display); font-size: var(--fs-h1); line-height: 1.15; letter-spacing: -0.02em; font-weight: 700; }
.h2, h2 { font-family: var(--font-display); font-size: var(--fs-h2); line-height: 1.2; letter-spacing: -0.015em; font-weight: 700; }
.h3, h3 { font-size: var(--fs-h3); font-weight: 600; line-height: 1.3; }
.lead { font-size: var(--fs-lead); color: var(--muted); margin: 0; }
.eyebrow { font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent); margin: 0 0 12px; }
.meta { font-family: var(--font-mono); font-size: var(--fs-meta); color: var(--muted); }
.num  { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.center { text-align: center; }

/* ─── layout primitives ─── */
.row { display: flex; align-items: center; gap: 12px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.row-end { display: flex; align-items: center; justify-content: flex-end; gap: 12px; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; }
.stack > * + * { margin-top: 16px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1 1 auto; }
@media (max-width: 920px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* ─── buttons ─── */
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 16px; border-radius: var(--radius);
  border: 1px solid transparent; font-size: 14px; font-weight: 600;
  letter-spacing: -0.005em; min-height: 38px;
  transition: transform 0.05s ease, background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #ffffff; border-color: var(--accent); }
.btn-primary:hover { background: color-mix(in oklch, var(--accent) 88%, black); }
.btn-secondary { background: var(--surface); color: var(--fg); border-color: var(--border); }
.btn-secondary:hover { border-color: var(--fg); }
.btn-ghost { background: transparent; color: var(--muted); border-color: transparent; padding-inline: 8px; }
.btn-ghost:hover { color: var(--accent); }
.btn-danger { background: transparent; color: var(--muted); border-color: var(--border); }
.btn-danger:hover { color: var(--fg); border-color: var(--fg); }
.btn-sm { padding: 6px 12px; min-height: 32px; font-size: 13px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ─── card / surface ─── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.card-white { background: var(--bg); }
.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.rule { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

/* ─── app shell ─── */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex; flex-direction: column; gap: 4px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.sidebar .brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 16px; letter-spacing: -0.02em;
  padding: 6px 10px 18px;
}
.sidebar .brand .logo-mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: #ffffff;
  display: grid; place-items: center; font-weight: 800; font-size: 15px;
}
.nav-group { font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); padding: 16px 10px 6px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 8px;
  color: var(--muted); font-size: 14px; font-weight: 500;
  transition: background 0.12s ease, color 0.12s ease;
}
.nav-item:hover { background: var(--fg-soft); color: var(--fg); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item svg { width: 18px; height: 18px; flex: 0 0 auto; }
.sidebar-foot { margin-top: auto; padding: 12px 10px 0; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); }

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in oklch, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 16px 28px;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.topbar h1 { font-size: 20px; }
.topbar .sub { color: var(--muted); font-size: 13px; }
.content { padding: 28px; max-width: var(--container); width: 100%; margin-inline: auto; }
.mobile-bar { display: none; }

@media (max-width: 920px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: static; height: auto; flex-direction: row; align-items: center;
    border-right: 0; border-bottom: 1px solid var(--border);
    padding: 10px 12px; gap: 8px; overflow-x: auto;
  }
  .sidebar .brand { padding: 0 12px 0 4px; }
  .nav-group, .sidebar-foot { display: none; }
  .sidebar .nav { display: flex; gap: 4px; }
  .nav-item { white-space: nowrap; padding: 8px 12px; }
  .nav-item .label { display: inline; }
  .topbar { padding: 14px 16px; }
  .content { padding: 18px 16px; }
}

/* ─── status pills (palette-only colors) ─── */
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  font-family: var(--font-mono); font-size: 11px; font-weight: 600;
  letter-spacing: 0.04em; text-transform: uppercase; white-space: nowrap;
}
.pill::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.status-draft   { color: var(--muted);  border: 1px solid var(--border); background: transparent; }
.status-draft::before { background: var(--muted); }
.status-sent    { color: var(--accent); background: var(--accent-soft); }
.status-paid    { color: var(--success); background: var(--success-soft); }
.status-overdue { color: #ffffff; background: var(--fg); }
.status-overdue::before { background: #ffffff; }

/* ─── stat / KPI ─── */
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 18px 20px; }
.kpi .kpi-label { font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.kpi .kpi-num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; font-size: 30px; font-weight: 700; letter-spacing: -0.02em; margin-top: 8px; }
.kpi .kpi-num.accent { color: var(--accent); }
.kpi .kpi-num.success { color: var(--success); }
.kpi .kpi-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ─── table ─── */
.ds-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.ds-table th, .ds-table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.ds-table th { color: var(--muted); font-weight: 600; font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.04em; text-transform: uppercase; }
.ds-table tbody tr:hover { background: var(--fg-soft); }
.ds-table .num-col { font-family: var(--font-mono); font-variant-numeric: tabular-nums; text-align: right; }
.ds-table .row-link { cursor: pointer; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--bg); }
.table-wrap .ds-table th:first-child, .table-wrap .ds-table td:first-child { padding-left: 18px; }
.table-wrap .ds-table th:last-child, .table-wrap .ds-table td:last-child { padding-right: 18px; }

/* ─── forms ─── */
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 500; color: var(--fg); }
.field .hint { font-size: 12px; color: var(--muted); }
.input, .textarea, .select {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--fg); font: inherit; font-size: 14px;
}
.input:focus, .textarea:focus, .select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.textarea { min-height: 88px; resize: vertical; line-height: 1.5; }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.form-grid .col-2 { grid-column: span 2; }
@media (max-width: 720px) { .form-grid, .grid-2 { grid-template-columns: 1fr; } .form-grid .col-2 { grid-column: span 1; } }

/* ─── modal ─── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  background: color-mix(in oklch, var(--fg) 35%, transparent);
  display: none; align-items: flex-start; justify-content: center;
  padding: 48px 16px; overflow-y: auto;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius-lg); width: 100%; max-width: 620px;
  box-shadow: var(--shadow-md); padding: 24px;
}
.modal h2 { margin-bottom: 4px; }
.modal .modal-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; }
.modal .modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; }
.icon-btn { background: transparent; border: 0; color: var(--muted); font-size: 22px; line-height: 1; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--fg-soft); color: var(--fg); }

/* ─── toast ─── */
.toast-wrap { position: fixed; bottom: 22px; right: 22px; z-index: 200; display: flex; flex-direction: column; gap: 10px; }
.toast {
  background: var(--fg); color: #ffffff; padding: 12px 16px; border-radius: var(--radius);
  font-size: 14px; box-shadow: var(--shadow-md); display: flex; align-items: center; gap: 10px;
  animation: toast-in 0.18s ease;
}
.toast .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ─── misc ─── */
.empty { text-align: center; padding: 56px 20px; color: var(--muted); }
.empty svg { width: 40px; height: 40px; margin: 0 auto 12px; opacity: 0.5; }
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px; font-size: 12px; color: var(--muted); background: var(--bg); }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.seg button { border: 0; background: var(--bg); padding: 7px 12px; font-size: 13px; color: var(--muted); }
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.link { color: var(--accent); font-weight: 500; }
.link:hover { text-decoration: underline; }
.copybox { display: flex; gap: 8px; align-items: center; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; font-family: var(--font-mono); font-size: 13px; }
.copybox code { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ─── line items editor ─── */
.lineitems { width: 100%; border-collapse: collapse; }
.lineitems th { font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.lineitems td { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.lineitems .num-col { text-align: right; }
.lineitems input { width: 100%; }
.lineitems .li-desc { min-width: 200px; }
.lineitems .li-qty, .lineitems .li-rate, .lineitems .li-tax { width: 84px; }
.totals { margin-top: 14px; margin-left: auto; width: 280px; max-width: 100%; }
.totals .trow { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14px; }
.totals .trow.grand { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 12px; font-weight: 700; font-size: 17px; }
.totals .num { font-variant-numeric: tabular-nums; }

/* ─── print / PDF invoice ─── */
#print-area { display: none; }
@media print {
  body * { visibility: hidden; }
  #print-area, #print-area * { visibility: visible; }
  #print-area { display: block !important; position: absolute; left: 0; top: 0; width: 100%; padding: 32px 40px; color: var(--fg); }
  .app, .topbar, .sidebar, .toast-wrap, .modal-backdrop { display: none !important; }
  #print-area .inv-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 28px; }
  #print-area .inv-title { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; }
  #print-area table { width: 100%; border-collapse: collapse; margin-top: 18px; }
  #print-area th, #print-area td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 13px; }
  #print-area .num-col { text-align: right; font-family: var(--font-mono); }
  #print-area .inv-foot { margin-top: 28px; font-size: 12px; color: var(--muted); }
}
