/* ---------------------------------------------------------------------
   Tokens
--------------------------------------------------------------------- */
:root {
  --bg: oklch(1 0 0);
  --surface: oklch(0.975 0.006 70);
  --surface-2: oklch(0.955 0.008 70);
  --border: oklch(0.9 0.008 70);
  --border-strong: oklch(0.82 0.01 70);

  --ink: oklch(0.22 0.012 70);
  --muted: oklch(0.485 0.014 70);
  --muted-2: oklch(0.62 0.012 70);

  --primary: oklch(0.55 0.14 68);
  --primary-hover: oklch(0.49 0.15 68);
  --primary-active: oklch(0.44 0.15 68);
  --on-primary: oklch(1 0 0);

  --accent: oklch(0.46 0.1 210);
  --accent-soft-bg: oklch(0.95 0.03 210);
  --accent-soft-text: oklch(0.4 0.09 210);

  --status-paid-bg: oklch(0.94 0.05 150);
  --status-paid-text: oklch(0.32 0.11 150);
  --status-pending-bg: oklch(0.95 0.06 85);
  --status-pending-text: oklch(0.38 0.13 75);
  --status-overdue-bg: oklch(0.94 0.06 25);
  --status-overdue-text: oklch(0.4 0.17 25);

  --danger: oklch(0.5 0.17 25);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 2px oklch(0.22 0.01 70 / 0.07);
  --shadow-md: 0 8px 24px oklch(0.22 0.01 70 / 0.1);
  --shadow-lg: 0 24px 48px oklch(0.22 0.01 70 / 0.16);

  --focus-ring: 0 0 0 3px oklch(0.55 0.14 68 / 0.28);

  --z-dropdown: 100;
  --z-sticky: 200;
  --z-modal-backdrop: 300;
  --z-modal: 400;
  --z-toast: 500;

  --ease-out-expo: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-out-quart: cubic-bezier(0.25, 1, 0.5, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: oklch(0.16 0.01 70);
    --surface: oklch(0.2 0.012 70);
    --surface-2: oklch(0.14 0.01 70);
    --border: oklch(0.32 0.014 70);
    --border-strong: oklch(0.4 0.016 70);

    --ink: oklch(0.94 0.008 70);
    --muted: oklch(0.72 0.012 70);
    --muted-2: oklch(0.58 0.012 70);

    --primary: oklch(0.68 0.14 68);
    --primary-hover: oklch(0.74 0.14 68);
    --primary-active: oklch(0.6 0.15 68);
    --on-primary: oklch(0.16 0.01 70);

    --accent: oklch(0.7 0.09 210);
    --accent-soft-bg: oklch(0.28 0.05 210);
    --accent-soft-text: oklch(0.85 0.06 210);

    --status-paid-bg: oklch(0.28 0.06 150);
    --status-paid-text: oklch(0.86 0.1 150);
    --status-pending-bg: oklch(0.3 0.07 80);
    --status-pending-text: oklch(0.87 0.11 80);
    --status-overdue-bg: oklch(0.3 0.08 25);
    --status-overdue-text: oklch(0.86 0.13 25);

    --shadow-sm: 0 1px 2px oklch(0 0 0 / 0.3);
    --shadow-md: 0 8px 24px oklch(0 0 0 / 0.4);
    --shadow-lg: 0 24px 48px oklch(0 0 0 / 0.5);
  }
}

/* ---------------------------------------------------------------------
   Reset & base
--------------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", system-ui, "Helvetica Neue", Arial, sans-serif;
  font-size: 0.875rem;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; text-wrap: balance; }
p { margin: 0; }

button { font-family: inherit; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}

/* ---------------------------------------------------------------------
   Shell layout
--------------------------------------------------------------------- */
.shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px;
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: var(--on-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9375rem;
}

.brand-name {
  font-weight: 600;
  font-size: 1rem;
  letter-spacing: -0.01em;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 500;
  text-decoration: none;
  transition: background-color 150ms var(--ease-out-quart), color 150ms var(--ease-out-quart);
}

a.nav-item:hover {
  background: var(--surface);
  color: var(--ink);
}

.nav-item.active {
  background: var(--accent-soft-bg);
  color: var(--accent-soft-text);
  font-weight: 600;
}

.nav-item.soon {
  cursor: default;
  color: var(--muted-2);
}

.soon-pill {
  font-size: 0.6875rem;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted-2);
  border: 1px solid var(--border);
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 18px 32px;
}

.breadcrumb {
  font-size: 0.8125rem;
  color: var(--muted-2);
  margin-bottom: 4px;
}

.page-title {
  font-size: 1.375rem;
  font-weight: 650;
  letter-spacing: -0.015em;
}

.content {
  padding: 28px 32px 48px;
  max-width: 1280px;
  width: 100%;
}

.section-intro { margin-bottom: 20px; }

.section-subtitle {
  color: var(--muted);
  font-size: 0.9375rem;
}

/* ---------------------------------------------------------------------
   Stats
--------------------------------------------------------------------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-label {
  font-size: 0.8125rem;
  color: var(--muted);
  font-weight: 500;
}

.stat-figure {
  font-size: 1.5rem;
  font-weight: 650;
  letter-spacing: -0.015em;
  color: var(--ink);
}

.stat-figure--empty { color: var(--muted-2); font-weight: 500; }
.stat-figure--overdue { color: var(--danger); }

.stat-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 2px;
}

.chip {
  font-size: 0.75rem;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 9px;
}

/* ---------------------------------------------------------------------
   Toolbar
--------------------------------------------------------------------- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.toolbar-form {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.search-field {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 11px;
  width: 16px;
  height: 16px;
  color: var(--muted-2);
  pointer-events: none;
}

.search-field input {
  width: 240px;
  padding: 8px 12px 8px 33px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.875rem;
}

.search-field input::placeholder { color: var(--muted-2); }

.select-field select {
  padding: 8px 32px 8px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.875rem;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='none'%3E%3Cpath d='M5.5 8L10 12.5L14.5 8' stroke='%23767066' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

input:focus-visible, select:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: var(--focus-ring);
}

.btn-secondary, .btn-mark-paid, .btn-primary {
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 8px 14px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color 150ms var(--ease-out-quart), border-color 150ms var(--ease-out-quart), transform 150ms var(--ease-out-quart);
}

.btn-secondary {
  background: var(--bg);
  border-color: var(--border-strong);
  color: var(--ink);
}

.btn-secondary:hover { background: var(--surface); }

.btn-mark-paid {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--ink);
  white-space: nowrap;
}

.btn-mark-paid:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}

.btn-mark-paid:disabled {
  opacity: 0.6;
  cursor: progress;
}

.toolbar-count {
  font-size: 0.8125rem;
  color: var(--muted);
  white-space: nowrap;
}

/* ---------------------------------------------------------------------
   Table
--------------------------------------------------------------------- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg);
}

.payments-table {
  width: 100%;
  min-width: 860px;
  border-collapse: collapse;
  font-size: 0.8125rem;
}

.payments-table th {
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
}

.payments-table td {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  color: var(--ink);
}

.payment-row {
  cursor: pointer;
  transition: background-color 120ms var(--ease-out-quart);
}

.payment-row:hover { background: var(--surface); }
.payment-row:last-child td { border-bottom: none; }

.cell-client { display: flex; flex-direction: column; gap: 1px; }
.client-name { font-weight: 600; }
.client-type { font-size: 0.75rem; color: var(--muted-2); }

.cell-amount { font-variant-numeric: tabular-nums; font-weight: 600; }

.date-label {
  color: var(--muted-2);
  font-size: 0.75rem;
  margin-right: 4px;
}

.date-muted { color: var(--muted-2); }

.status-pill {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}

.status-pill--paid { background: var(--status-paid-bg); color: var(--status-paid-text); }
.status-pill--pending { background: var(--status-pending-bg); color: var(--status-pending-text); }
.status-pill--overdue { background: var(--status-overdue-bg); color: var(--status-overdue-text); }

.cell-actions { text-align: right; }

.action-done {
  color: var(--status-paid-text);
  font-weight: 700;
  display: inline-block;
  width: 100%;
  text-align: right;
}

.empty-row td { padding: 0; border-bottom: none; }

.empty-state {
  padding: 56px 24px;
  text-align: center;
}

.empty-title { font-weight: 600; margin-bottom: 4px; }
.empty-body { color: var(--muted); font-size: 0.8125rem; }

/* ---------------------------------------------------------------------
   Pagination
--------------------------------------------------------------------- */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 20px;
  font-size: 0.8125rem;
}

.pagination-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}

.pagination-link:hover { text-decoration: underline; }
.pagination-status { color: var(--muted); }

/* ---------------------------------------------------------------------
   Modal (native <dialog>)
--------------------------------------------------------------------- */
.payment-modal {
  border: none;
  border-radius: var(--radius-lg);
  padding: 0;
  width: min(480px, 92vw);
  box-shadow: var(--shadow-lg);
  background: var(--bg);
  color: var(--ink);
  z-index: var(--z-modal);
}

.payment-modal::backdrop {
  background: oklch(0.15 0.01 70 / 0.45);
}

@supports (animation-timeline: view()) {}

.payment-modal[open] {
  animation: modal-in 220ms var(--ease-out-quart);
}

.payment-modal[open]::backdrop {
  animation: backdrop-in 220ms var(--ease-out-quart);
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes backdrop-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content { padding: 24px; }

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.modal-title {
  font-size: 1.125rem;
  font-weight: 650;
  letter-spacing: -0.01em;
  margin-bottom: 6px;
}

.modal-close {
  border: none;
  background: transparent;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-sm);
}

.modal-close:hover { background: var(--surface); color: var(--ink); }

.detail-grid { margin: 0; }

.detail-row {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 12px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.8125rem;
}

.detail-row:last-child { border-bottom: none; }

.detail-row dt { color: var(--muted); }
.detail-row dd { margin: 0; color: var(--ink); }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.btn-mark-paid--modal {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}

.btn-mark-paid--modal:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

/* ---------------------------------------------------------------------
   Projects
--------------------------------------------------------------------- */
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
  text-decoration: none;
  display: inline-block;
}

.btn-primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

a.btn-secondary { text-decoration: none; display: inline-block; }

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.projects-table { min-width: 1180px; }
.projects-table--compact { min-width: 960px; }
.projects-table th, .projects-table td { padding-left: 12px; padding-right: 12px; }
.projects-table .cell-name { min-width: 200px; }
.projects-table .cell-client-name { min-width: 150px; }
.projects-table .date-muted { white-space: nowrap; }
.projects-search input { width: 280px; }
.projects-table th { vertical-align: bottom; white-space: nowrap; }
.projects-table th.col-num { white-space: normal; min-width: 110px; }
.projects-table .col-num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.projects-table .cell-date { white-space: nowrap; }
.project-row:hover { background: var(--surface); }
.project-row:last-child td { border-bottom: none; }

.breadcrumb a { color: inherit; text-decoration: none; }
.breadcrumb a:hover { color: var(--ink); text-decoration: underline; }

.project-form {
  max-width: 760px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px 20px;
}

.form-field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.form-field--wide { grid-column: 1 / -1; }

.form-field > label {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
}

.form-field input {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.875rem;
  font-family: inherit;
}

.select-field--block select { width: 100%; }
.form-field textarea {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.875rem;
  font-family: inherit;
  resize: vertical;
}
.project-filter { max-width: 300px; }
#status-filter { min-width: 140px; }
.empty-body a { color: var(--accent-soft-text); }
.input-group { display: flex; gap: 8px; }
.input-group .select-field { flex: none; }
.input-group select { height: 100%; min-width: 96px; }
.input-group input { flex: 1; min-width: 0; }

.form-help { font-size: 0.75rem; color: var(--muted-2); }
.form-error, .form-errors { font-size: 0.75rem; color: var(--danger); }

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 24px;
}

@media (max-width: 600px) {
  .form-grid { grid-template-columns: 1fr; }
  .project-form { padding: 16px; }
  .toolbar-actions { justify-content: space-between; }
}

.project-row { cursor: pointer; }
.row-link { color: inherit; text-decoration: none; }
.row-link:hover { text-decoration: underline; }
.btn-edit { text-decoration: none; display: inline-block; }
.btn-edit:hover { background: var(--surface-2, var(--surface)); color: var(--ink); border-color: var(--ink); }

.detail-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.detail-card {
  max-width: 760px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
}

.detail-card-title {
  font-size: 0.9375rem;
  font-weight: 650;
  margin-bottom: 8px;
}

.detail-card .detail-row { grid-template-columns: 180px 1fr; }

@media (max-width: 600px) {
  .detail-card .detail-row { grid-template-columns: 1fr; gap: 2px; }
}

/* ---------------------------------------------------------------------
   Overview
--------------------------------------------------------------------- */
.overview-heading {
  font-size: 0.9375rem;
  font-weight: 650;
  margin: 8px 0 12px;
}

.overview-note { margin: -4px 0 14px; }
.overview-note a, .empty-body a { color: var(--accent-soft-text); }

.stats-grid--compact { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.stats-grid--money { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.money-split { display: flex; justify-content: space-between; gap: 12px; margin: 4px 0 0; }
.money-split dt { font-size: 0.75rem; color: var(--muted-2); }
.money-split dd { margin: 0; font-size: 0.8125rem; font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; white-space: nowrap; }
.money-split div:last-child { text-align: right; }
.stats-grid--money .stat-figure { font-size: 1.1875rem; white-space: nowrap; }
.stat-card .meter { flex: none; margin-top: 4px; }
.stat-note { font-size: 0.75rem; color: var(--muted-2); }

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 999px;
  margin-right: 7px;
  vertical-align: 1px;
}
.status-dot--past_end { background: var(--status-overdue-text); }
.status-dot--due_soon { background: var(--status-pending-text); }
.status-dot--in_progress { background: var(--status-paid-text); }
.status-dot--not_started { background: var(--muted-2); }

.delivery-pill--past_end { background: var(--status-overdue-bg); color: var(--status-overdue-text); }
.delivery-pill--due_soon { background: var(--status-pending-bg); color: var(--status-pending-text); }
.delivery-pill--in_progress { background: var(--status-paid-bg); color: var(--status-paid-text); }
.delivery-pill--not_started { background: var(--surface); color: var(--muted); border: 1px solid var(--border); }
.status-pill { white-space: nowrap; }

.meter {
  height: 6px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
  flex: 1;
}
.meter-fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
}
.meter-fill--time { background: var(--muted-2); }
.meter-fill--behind { background: var(--status-overdue-text); }

.meter-row { display: flex; align-items: center; gap: 8px; min-width: 130px; }
.meter-value { font-size: 0.75rem; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 34px; text-align: right; }

.behind-flag {
  display: inline-block;
  margin-top: 4px;
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--status-overdue-text);
}

.overview-table { min-width: 900px; margin-bottom: 0; }
.overview-table .col-num { text-align: right; white-space: nowrap; }
.overview-table .cell-date { white-space: nowrap; }
.overview-table .cell-date .client-type { display: block; }
.table-wrap + .overview-heading { margin-top: 28px; }

/* ---------------------------------------------------------------------
   Invoice editor (styled as a paper document, light in both themes)
--------------------------------------------------------------------- */
.invoices-table { min-width: 980px; }
.invoices-table .col-num { text-align: right; white-space: nowrap; }

.invoice-editor { max-width: 960px; }

.invoice-toolbar {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 12px 16px;
  margin-bottom: 16px;
}
.invoice-toolbar-field { display: flex; flex-direction: column; gap: 4px; font-size: 0.75rem; font-weight: 600; color: var(--muted); }
.invoice-toolbar-field--project select { width: 460px; max-width: 100%; }
.invoice-toolbar-field select { min-width: 96px; }
.invoice-toolbar-actions { margin-left: auto; display: flex; gap: 10px; }

.invoice-errors {
  border: 1px solid var(--status-overdue-text);
  background: var(--status-overdue-bg);
  color: var(--status-overdue-text);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  margin-bottom: 16px;
  font-size: 0.8125rem;
}
.invoice-errors-title { font-weight: 600; margin-bottom: 4px; }
.invoice-errors ul { margin: 0; padding-left: 18px; }

.invoice-paper {
  --inv-ink: #1f2937;
  --inv-muted: #6b7280;
  --inv-border: #d1d5db;
  --inv-soft: #f3f4f6;
  --inv-head: #0f172a;
  --inv-link: #059669;
  background: #fff;
  color: var(--inv-ink);
  border: 1px solid #e5e7eb;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 4px 4px 0 -1px #fff, 4px 4px 0 0 #e5e7eb;
  padding: 30px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 0.875rem;
}

.invoice-paper input[type=text],
.invoice-paper input[type=number],
.invoice-paper input[type=date],
.invoice-paper textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--inv-border);
  border-radius: 4px;
  background: #fff;
  color: var(--inv-ink);
  font: inherit;
  transition: border-color 120ms, box-shadow 120ms;
}
.invoice-paper textarea { resize: vertical; min-height: 54px; }
.invoice-paper input::placeholder, .invoice-paper textarea::placeholder { color: #9ca3af; }
.invoice-paper input:hover, .invoice-paper textarea:hover { border-color: #9ca3af; }
.invoice-paper input:focus-visible, .invoice-paper textarea:focus-visible {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgb(37 99 235 / 0.15);
}

.inv-label { display: block; color: #374151; padding: 8px 10px 8px 12px; }

/* Header */
.inv-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; margin-bottom: 20px; }
.inv-logo-wrap { position: relative; }
.inv-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 180px;
  height: 117px;
  background: #f8f9fb;
  border: 1px solid #e5e7eb;
  border-radius: 3px;
  color: var(--inv-muted);
  font-size: 1.0625rem;
  cursor: pointer;
  overflow: hidden;
  transition: background-color 120ms;
}
.inv-logo:hover { background: #eef0f3; }
.inv-logo input[type=file] { position: absolute; width: 1px; height: 1px; opacity: 0; }
.inv-logo:focus-within { box-shadow: 0 0 0 3px rgb(37 99 235 / 0.2); }
.inv-logo img { max-width: 100%; max-height: 100%; object-fit: contain; }
.inv-logo.has-logo { background: #fff; border-style: dashed; border-color: transparent; }
.inv-logo.has-logo:hover { border-color: var(--inv-border); }
.inv-logo.has-logo .inv-logo-placeholder { display: none; }
.inv-logo-placeholder span { font-size: 1.375rem; vertical-align: -2px; margin-right: 4px; }
.inv-logo-remove {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  border: 1px solid var(--inv-border);
  background: #fff;
  color: var(--inv-muted);
  cursor: pointer;
  line-height: 1;
}
.inv-logo-remove:hover { color: #b91c1c; border-color: #b91c1c; }

.inv-title-block { display: flex; flex-direction: column; align-items: flex-end; gap: 10px; }
.inv-title {
  font-size: 2.5rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  color: #111827;
  line-height: 1;
  margin-right: 10px;
}

.invoice-paper input.inv-title-input {
  width: 100%;
  max-width: 420px;
  padding: 2px 8px;
  margin-right: -10px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  line-height: 1.1;
  text-align: right;
  cursor: text;
}
.invoice-paper input.inv-title-input:hover { border-color: var(--inv-border); }
.invoice-paper input.inv-title-input:focus-visible { border-color: #2563eb; background: #fff; }
.inv-title { width: 420px; max-width: 100%; text-align: right; }

.inv-prefixed {
  display: flex;
  align-items: center;
  border: 1px solid var(--inv-border);
  border-radius: 4px;
  background: #fff;
  transition: border-color 120ms, box-shadow 120ms;
}
.inv-prefixed:hover { border-color: #9ca3af; }
.inv-prefixed:focus-within { border-color: #2563eb; box-shadow: 0 0 0 3px rgb(37 99 235 / 0.15); }
.invoice-paper .inv-prefixed input { border: none; box-shadow: none; text-align: right; min-width: 0; }
.inv-prefix { padding-left: 8px; color: var(--inv-muted); white-space: nowrap; }
.inv-number { width: 152px; }

/* Parties + meta */
.inv-parties { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 0.9fr); gap: 24px; margin-bottom: 28px; }
.inv-parties-left > textarea { max-width: 334px; }
.inv-bill-ship { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 14px; }
.inv-meta {
  display: grid;
  grid-template-columns: 1fr 152px;
  align-items: center;
  gap: 4px 12px;
  align-content: start;
  margin-left: auto;
  width: 100%;
  max-width: 330px;
}
.inv-meta .inv-label { padding: 0; }

/* Items */
.inv-items { margin-bottom: 30px; }
.inv-items-head,
.inv-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 80px 120px 140px 26px;
  gap: 4px;
  align-items: center;
}
.inv-items-head {
  background: var(--inv-head);
  color: #fff;
  font-weight: 600;
  border-radius: 4px;
  padding: 8px 0 8px 20px;
  margin-bottom: 6px;
}
.invoice-paper .inv-items-head input.inv-head-input {
  width: 100%;
  min-width: 0;
  padding: 3px 6px;
  margin-left: -6px;
  border: 1px solid transparent;
  border-radius: 3px;
  background: transparent;
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: text;
}
.invoice-paper .inv-items-head input.inv-head-input:hover { border-color: rgb(255 255 255 / 0.25); }
.invoice-paper .inv-items-head input.inv-head-input:focus-visible {
  border-color: rgb(255 255 255 / 0.6);
  background: rgb(255 255 255 / 0.08);
  box-shadow: none;
}
.invoice-paper input.inv-label-input {
  width: auto;
  max-width: 100%;
  padding: 3px 6px;
  margin-left: -7px;
  border: 1px solid transparent;
  border-radius: 3px;
  background: transparent;
  color: #374151;
  cursor: text;
}
.invoice-paper input.inv-label-input:hover { border-color: var(--inv-border); }
.invoice-paper input.inv-label-input:focus-visible { border-color: #2563eb; background: #fff; }
.inv-notes .inv-label { padding-top: 4px; padding-bottom: 4px; }
.inv-head-amount input.inv-head-input { text-align: right; margin-left: 0; margin-right: 4px; }
.inv-items-head > input:nth-child(2), .inv-items-head > input:nth-child(3) { margin-left: 4px; }
.inv-row { margin-bottom: 4px; }
.invoice-paper .inv-qty { text-align: left; }
.inv-num { text-align: right; font-variant-numeric: tabular-nums; }
.inv-amount { padding-right: 10px; color: var(--inv-ink); white-space: nowrap; }
.inv-items-head .inv-num { padding-right: 10px; }
.inv-row-remove {
  width: 24px;
  height: 24px;
  border: none;
  background: none;
  color: #9ca3af;
  font-size: 1.125rem;
  cursor: pointer;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 120ms;
}
.inv-row:hover .inv-row-remove, .inv-row-remove:focus-visible { opacity: 1; }
.inv-row-remove:hover { color: #b91c1c; background: #fef2f2; }

.inv-add-row {
  width: 100%;
  margin-top: 4px;
  padding: 7px;
  border: none;
  border-radius: 4px;
  background: var(--inv-soft);
  color: #374151;
  font: inherit;
  cursor: pointer;
  transition: background-color 120ms;
}
.inv-add-row:hover { background: #e5e7eb; }
.inv-add-row span, .inv-link span { font-size: 1.0625rem; margin-right: 3px; }

/* Bottom */
.inv-bottom { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 28px; }
.inv-notes .inv-label:not(:first-child) { margin-top: 14px; }

.inv-totals { display: flex; flex-direction: column; }
.inv-total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 40px 8px 12px;
  color: #374151;
}
.inv-total-row[hidden] { display: none; }
.inv-total-row .inv-num { color: var(--inv-ink); }
.inv-adjust { padding-top: 4px; padding-bottom: 4px; }
.inv-adjust-input {
  display: flex;
  align-items: center;
  width: 133px;
  border: 1px solid var(--inv-border);
  border-radius: 4px;
  background: #fff;
}
.inv-adjust-input:focus-within { border-color: #2563eb; box-shadow: 0 0 0 3px rgb(37 99 235 / 0.15); }
.invoice-paper .inv-adjust-input input { border: none; box-shadow: none; text-align: right; padding: 7px 6px; min-width: 0; }
.inv-adjust-unit { color: var(--inv-muted); padding: 0 6px; }
.inv-adjust-toggle {
  align-self: stretch;
  border: none;
  border-left: 1px solid var(--inv-border);
  background: none;
  color: #374151;
  padding: 0 8px;
  cursor: pointer;
  font-size: 1rem;
  border-radius: 0 4px 4px 0;
}
.inv-adjust-toggle:hover { background: var(--inv-soft); }
.inv-adjust-remove {
  border: none;
  background: none;
  color: #9ca3af;
  cursor: pointer;
  padding: 0 4px;
  font-size: 1rem;
}
.inv-adjust-remove:hover { color: #b91c1c; }

.inv-add-links { display: flex; gap: 16px; padding: 4px 12px 10px; }
.inv-link {
  border: none;
  background: none;
  padding: 0;
  color: var(--inv-link);
  font: inherit;
  cursor: pointer;
}
.inv-link:hover { text-decoration: underline; }
.inv-link[hidden] { display: none; }

.inv-total-row--total {
  border-top: 1px solid var(--inv-border);
  margin-top: 4px;
  padding-top: 14px;
  font-weight: 700;
  color: #111827;
}
.inv-total-row--balance {
  border-top: 1px solid var(--inv-border);
  margin-top: 8px;
  padding-top: 18px;
  font-size: 1.125rem;
  font-weight: 800;
  color: #111827;
}

.invoice-danger { margin-top: 18px; }
.btn-link-danger {
  border: none;
  background: none;
  color: var(--danger);
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
}
.btn-link-danger:hover { text-decoration: underline; }

@media (max-width: 760px) {
  .invoice-paper { padding: 18px; }
  .inv-header, .inv-parties, .inv-bottom { grid-template-columns: 1fr; display: grid; }
  .inv-title-block { align-items: flex-start; }
  .inv-title, .invoice-paper input.inv-title-input { text-align: left; margin-left: -8px; }
  .inv-meta { margin-left: 0; max-width: none; grid-template-columns: 1fr 1fr; }
  .inv-bill-ship { grid-template-columns: 1fr; }
  .inv-items-head { grid-template-columns: 1fr 1fr; gap: 4px 8px; padding: 8px 12px; }
  .inv-items-head > :last-child { display: none; }
  .inv-row { grid-template-columns: 1fr 1fr; padding: 8px 0; border-bottom: 1px solid var(--inv-border); }
  .inv-row > input[name=item_description] { grid-column: 1 / -1; }
  .inv-row-remove { opacity: 1; }
  .inv-total-row { padding-right: 12px; }
}

@page { size: A4; margin: 12mm; }

@media print {
  .sidebar, .topbar, .invoice-toolbar, .invoice-danger, .invoice-errors,
  .inv-add-row, .inv-add-links, .inv-row-remove, .inv-adjust-remove,
  .inv-adjust-toggle, .inv-logo-remove { display: none !important; }
  body, .main, .content { background: #fff !important; padding: 0 !important; margin: 0 !important; }
  .invoice-editor { max-width: none; }
  .invoice-paper { border: none; box-shadow: none; padding: 0; }
  .invoice-paper .inv-items-head input.inv-head-input,
  .invoice-paper input.inv-label-input,
  .invoice-paper input.inv-title-input { border-color: transparent !important; background: transparent !important; }
  .invoice-paper input, .invoice-paper textarea, .inv-prefixed, .inv-adjust-input {
    border-color: transparent !important;
    box-shadow: none !important;
    resize: none;
  }
  .inv-logo { border: none; background: none; }
  .inv-logo:not(.has-logo) { visibility: hidden; }
  .inv-row:has(input[name=item_description]:placeholder-shown) { display: none; }
}

/* ---------------------------------------------------------------------
   Sign-in, current user, users & roles
--------------------------------------------------------------------- */
.sidebar { position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.sidebar-nav { flex: 1; }
.nav-item--settings { margin-top: 10px; position: relative; }
.nav-item--settings::before {
  content: "";
  position: absolute;
  top: -6px;
  left: 12px;
  right: 12px;
  border-top: 1px solid var(--border);
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px 0;
  border-top: 1px solid var(--border);
}
.user-avatar {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: var(--accent-soft-bg);
  color: var(--accent-soft-text);
  font-weight: 700;
  font-size: 0.8125rem;
}
.user-meta { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.user-name { font-size: 0.8125rem; font-weight: 600; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 0.75rem; color: var(--muted-2); }
.user-signout {
  border: none;
  background: none;
  color: var(--muted);
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.user-signout svg { width: 18px; height: 18px; }
.user-signout:hover { background: var(--surface); color: var(--ink); }

.flash-stack { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.flash {
  margin: 0;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  font-weight: 500;
  background: var(--status-paid-bg);
  color: var(--status-paid-text);
}
.flash--error { background: var(--status-overdue-bg); color: var(--status-overdue-text); }

/* Sign-in page */
.auth-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background: var(--surface-2);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
}
.auth-brand { padding: 0; margin-bottom: 22px; }
.auth-title { font-size: 1.25rem; font-weight: 650; margin: 0 0 4px; }
.auth-subtitle { color: var(--muted); font-size: 0.8125rem; margin: 0 0 18px; }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-error { margin: 0 0 14px; padding: 10px 12px; border-radius: var(--radius-md); background: var(--status-overdue-bg); }
.auth-submit { width: 100%; padding: 10px 14px; font-size: 0.875rem; }

/* Users & roles */
.roles-toolbar { margin-top: 32px; }
.toolbar .overview-heading { margin: 0; }
.role-pill { background: var(--accent-soft-bg); color: var(--accent-soft-text); }
.role-pill--owner { background: var(--status-pending-bg); color: var(--status-pending-text); }
.users-table, .roles-table { min-width: 760px; }
.roles-table .col-num { text-align: right; }
.level { font-size: 0.75rem; font-weight: 600; white-space: nowrap; }
.level--edit { color: var(--status-paid-text); }
.level--view { color: var(--accent-soft-text); }
.level--none { color: var(--muted-2); font-weight: 500; }

.form-note {
  margin: 0 0 18px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.8125rem;
}
.form-check label { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--ink); }
.form-check input[type=checkbox], .access-toggle input { width: 16px; height: 16px; accent-color: var(--primary); }

.role-heading { margin-top: 24px; }
.access-grid { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: var(--radius-md); }
.access-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px 20px;
  margin: 0;
  padding: 12px 14px;
  border: none;
  border-bottom: 1px solid var(--border);
}
.access-row:last-child { border-bottom: none; }
.access-section { display: flex; flex-direction: column; padding: 0; float: left; }
.access-name { font-weight: 600; color: var(--ink); font-size: 0.875rem; }
.access-help { font-size: 0.75rem; color: var(--muted-2); }
.access-options { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--radius-md); overflow: hidden; }
.access-option { position: relative; cursor: pointer; }
.access-option input { position: absolute; opacity: 0; pointer-events: none; }
.access-option span {
  display: block;
  padding: 6px 12px;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--muted);
  border-left: 1px solid var(--border-strong);
  transition: background-color 120ms, color 120ms;
}
.access-option:first-child span { border-left: none; }
.access-option:hover span { background: var(--surface); color: var(--ink); }
.access-option input:checked + span { background: var(--accent-soft-bg); color: var(--accent-soft-text); font-weight: 650; }
.access-option--none input:checked + span { background: var(--surface); color: var(--ink); }
.access-option--edit input:checked + span { background: var(--status-paid-bg); color: var(--status-paid-text); }
.access-option input:focus-visible + span { box-shadow: inset 0 0 0 2px var(--primary); }
.access-toggle { display: flex; align-items: center; gap: 8px; font-size: 0.8125rem; font-weight: 500; cursor: pointer; }
.role-delete { margin-right: auto; }

/* Read-only invoice */
.readonly-badge {
  align-self: center;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 600;
}
.is-readonly .inv-add-row,
.is-readonly .inv-add-links,
.is-readonly .inv-row-remove,
.is-readonly .inv-adjust-toggle,
.is-readonly .inv-adjust-remove,
.is-readonly .inv-logo-remove { display: none !important; }
.is-readonly .inv-logo { cursor: default; }
.is-readonly .inv-logo:not(.has-logo) { visibility: hidden; }
.invoice-paper input:disabled, .invoice-paper textarea:disabled { background: #fff; color: var(--inv-ink); opacity: 1; cursor: default; }

@media (max-width: 880px) {
  .user-meta { display: none; }
  .sidebar-user { flex-direction: column; gap: 6px; padding-left: 0; padding-right: 0; }
  .nav-item--settings::before { left: 6px; right: 6px; }
}

@media print {
  .sidebar-user, .flash-stack, .readonly-badge { display: none !important; }
}

/* ---------------------------------------------------------------------
   Invoice approvals
--------------------------------------------------------------------- */
.nav-badge {
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--status-pending-bg);
  color: var(--status-pending-text);
  font-size: 0.6875rem;
  font-weight: 700;
  text-align: center;
}

.approval-pill--pending { background: var(--status-pending-bg); color: var(--status-pending-text); }
.approval-pill--rejected { background: var(--status-overdue-bg); color: var(--status-overdue-text); }
.invoices-table .client-type { display: block; margin-top: 3px; }
.level--submit { color: var(--status-pending-text); }

.approval-banner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: var(--status-pending-bg);
  color: var(--status-pending-text);
  text-decoration: none;
  font-size: 0.875rem;
}
.approval-banner span { font-weight: 600; white-space: nowrap; }
.approval-banner:hover span { text-decoration: underline; }

.approval-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 20px;
  margin-bottom: 14px;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
}
.approval-box--pending { background: var(--status-pending-bg); color: var(--status-pending-text); }
.approval-box--rejected { background: var(--status-overdue-bg); color: var(--status-overdue-text); }
.approval-box-title { margin: 0 0 2px; font-weight: 700; font-size: 0.9375rem; }
.approval-box-meta { margin: 0; font-size: 0.8125rem; line-height: 1.5; }
.approval-actions { display: flex; gap: 8px; }
.btn-approve { background: var(--status-paid-text); border-color: var(--status-paid-text); }
.btn-approve:hover { filter: brightness(1.1); background: var(--status-paid-text); border-color: var(--status-paid-text); }

.reject-panel {
  margin: -6px 0 14px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.reject-panel[hidden] { display: none; }
.reject-panel label { font-size: 0.8125rem; font-weight: 600; color: var(--muted); }
.reject-panel textarea {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.875rem;
  resize: vertical;
}
.reject-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn-reject {
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 8px 14px;
  cursor: pointer;
  border: 1px solid var(--status-overdue-text);
  background: var(--status-overdue-text);
  color: #fff;
}
.btn-reject:hover { filter: brightness(1.1); }

.approval-note { margin: 0 0 14px; font-size: 0.8125rem; color: var(--muted); }
.role-footnote { margin-top: 10px; }
.missing-link { text-decoration: none; }
.missing-link:hover .status-pill { text-decoration: underline; }
.stat-note--warn { color: var(--status-pending-text); font-weight: 600; }
.project-form .form-note.form-field--wide { margin: 0; }
.detail-toolbar .toolbar-actions { gap: 8px; }

@media print {
  .approval-box, .reject-panel, .approval-note { display: none !important; }
}

/* ---------------------------------------------------------------------
   Row-update flash (mark-as-paid feedback)
--------------------------------------------------------------------- */
.payment-row.just-updated {
  animation: row-flash 900ms var(--ease-out-quart);
}

@keyframes row-flash {
  0% { background: var(--status-paid-bg); }
  100% { background: transparent; }
}

/* ---------------------------------------------------------------------
   Responsive
--------------------------------------------------------------------- */
@media (max-width: 880px) {
  .sidebar {
    width: 76px;
    padding: 16px 10px;
  }
  .brand-name, .nav-label, .soon-pill { display: none; }
  .nav-item { justify-content: center; padding: 10px; }
  .sidebar-brand { justify-content: center; padding: 4px 0; }
  .topbar, .content { padding-left: 20px; padding-right: 20px; }
  .search-field input { width: 180px; }
}

@media (max-width: 600px) {
  .toolbar { align-items: stretch; }
  .toolbar-form { width: 100%; }
  .search-field { flex: 1; }
  .search-field input { width: 100%; }
  .toolbar-count { order: -1; }
}

/* ---------------------------------------------------------------------
   Reduced motion
--------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
