:root {
  color-scheme: light;
  --canvas: #f8fafc;
  --paper: #f1f2f3;
  --paper-strong: #ffffff;
  --ink: #0f172a;
  --ink-soft: #334155;
  --muted: #586579;
  --quiet: #6b778a;
  --rule: #e4e5e7;
  --rule-strong: #cbd1d9;
  --brand: #1e293b;
  --brand-hover: #0f172a;
  --brand-soft: #e8edf4;
  --copper: #2563eb;
  --copper-soft: #e6efff;
  --success: #2f6e51;
  --success-soft: #e1eee6;
  --warning: #8a5208;
  --warning-soft: #f7ead4;
  --danger: #a2382b;
  --danger-soft: #f8e3df;
  --focus: #1e293b;
  --font-ui: "IBM Plex Sans", "Avenir Next", Avenir, "Helvetica Neue", Helvetica, Arial, sans-serif;
  --font-figures: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  --radius-sm: 0.25rem;
  --radius-md: 0.375rem;
  --radius-lg: 0.625rem;
  --shadow-paper: 0 1px 2px rgb(24 34 29 / 8%);
  --shell: 74rem;
  font-family: var(--font-ui);
  font-synthesis: none;
}

* { box-sizing: border-box; }

html {
  min-width: 20rem;
  background: var(--canvas);
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  margin: 0;
  display: flex;
  flex-direction: column;
  color: var(--ink);
  background: var(--canvas);
  font-size: 0.975rem;
  line-height: 1.55;
}

body::before {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  z-index: 1000;
  height: 3px;
  background: linear-gradient(90deg, var(--brand) 0 74%, var(--copper) 74% 100%);
}

a {
  color: var(--brand-hover);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
}

a:hover { color: var(--copper); }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}

::selection { color: white; background: var(--brand); }

.shell {
  width: min(100% - 2.5rem, var(--shell));
  margin-inline: auto;
}

.skip-link {
  position: fixed;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 1100;
  padding: 0.65rem 0.9rem;
  color: white;
  background: var(--brand-hover);
  transform: translateY(-180%);
}

.skip-link:focus { transform: translateY(0); }

.request-progress {
  position: fixed;
  inset: 0 auto auto 0;
  z-index: 1200;
  width: 100%;
  height: 3px;
  background: var(--copper);
  transform-origin: left;
  animation: request-progress 1.1s ease-in-out infinite;
}

.htmx-indicator { display: none; }
.htmx-request.htmx-indicator,
.htmx-request .htmx-indicator { display: block; }

@keyframes request-progress {
  0% { transform: scaleX(0.08); opacity: 0.45; }
  55% { transform: scaleX(0.72); opacity: 1; }
  100% { transform: scaleX(1); opacity: 0; }
}

.site-header {
  border-bottom: 1px solid var(--rule);
  background: rgb(252 252 248 / 94%);
  backdrop-filter: blur(12px);
}

.header-layout,
.footer-layout {
  min-height: 4.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--ink);
  text-decoration: none;
}

.brand:hover { color: var(--ink); }

.brand-mark {
  width: 2.5rem;
  height: 2.5rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
  border: 1px solid var(--brand);
  border-radius: var(--radius-sm);
  font-family: var(--font-figures);
  font-size: 0.7rem;
  font-weight: 800;
  line-height: 2.5rem;
  text-align: center;
}

.brand-mark span:first-child { color: white; background: var(--brand); }
.brand-mark span:last-child { color: var(--brand); background: var(--paper); }

.brand-copy { display: grid; line-height: 1.05; }
.brand-copy strong { font-size: 0.9rem; letter-spacing: -0.01em; }
.brand-copy small { margin-top: 0.25rem; color: var(--muted); font-size: 0.65rem; letter-spacing: 0.08em; text-transform: uppercase; }

.primary-navigation {
  display: flex;
  align-items: stretch;
  gap: 0.15rem;
}

.primary-navigation a,
.nav-sign-out {
  min-height: 2.75rem;
  padding: 0 0.8rem;
  display: inline-flex;
  align-items: center;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  color: var(--muted);
  background: transparent;
  font-size: 0.78rem;
  font-weight: 700;
  text-decoration: none;
}

.primary-navigation a:hover,
.nav-sign-out:hover { color: var(--ink); background: transparent; }

.primary-navigation a[aria-current="page"] {
  border-bottom-color: var(--copper);
  color: var(--ink);
}

.nav-form { margin: 0; }

button,
.button {
  min-height: 2.75rem;
  padding: 0.68rem 1rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  border: 1px solid var(--brand);
  border-radius: var(--radius-sm);
  color: white;
  background: var(--brand);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 750;
  line-height: 1.1;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

button:hover,
.button:hover { border-color: var(--brand-hover); color: white; background: var(--brand-hover); }

button:disabled { cursor: not-allowed; opacity: 0.55; }

.button-secondary {
  border-color: var(--rule-strong);
  color: var(--ink);
  background: transparent;
}

.button-secondary:hover { border-color: var(--ink); color: var(--ink); background: var(--paper); }

.text-button {
  min-height: 2.75rem;
  padding: 0 0.8rem;
  border: 0;
  color: var(--brand-hover);
  background: transparent;
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

.danger-button { border-color: var(--danger); color: var(--danger); background: transparent; }
.danger-button:hover { border-color: var(--danger); background: var(--danger); }

.message-stack { display: grid; gap: 0.5rem; padding-top: 1rem; }

.message,
.error-summary {
  padding: 0.85rem 1rem;
  border: 1px solid var(--rule-strong);
  border-left-width: 4px;
  border-radius: var(--radius-sm);
  background: var(--paper-strong);
}

.message-success { border-left-color: var(--success); background: var(--success-soft); }
.message-warning { border-left-color: var(--warning); background: var(--warning-soft); }
.message-error,
.error-summary { border-left-color: var(--danger); background: var(--danger-soft); }

.error-summary { margin-bottom: 1.25rem; }
.error-summary > :last-child { margin-bottom: 0; }

.page-content {
  flex: 1;
  padding-block: clamp(2.5rem, 5vw, 4.5rem);
}

h1, h2, h3, p { text-wrap: pretty; }
h1, h2, h3 { margin-top: 0; color: var(--ink); line-height: 1.08; letter-spacing: -0.035em; }
h1 { max-width: 22ch; margin-bottom: 1rem; font-size: clamp(2rem, 3vw, 2.5rem); font-weight: 650; }
h2 { margin-bottom: 0.8rem; font-size: clamp(1.4rem, 2.5vw, 2rem); font-weight: 650; }
h3 { margin-bottom: 0.6rem; font-size: 1.1rem; }

.eyebrow,
.panel-index,
.micro-label,
.metric-index {
  margin: 0 0 0.65rem;
  display: block;
  color: var(--brand);
  font-family: var(--font-figures);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

.eyebrow a { color: inherit; }
.hero-copy,
.section-copy { max-width: 44rem; margin: 0; color: var(--muted); font-size: 1.05rem; }

.compact-hero {
  margin-bottom: clamp(2.75rem, 6vw, 5rem);
  padding-bottom: 2rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 3rem;
  border-bottom: 1px solid var(--rule-strong);
}

.hero-copy-block { max-width: 48rem; }
.hero-action-block { padding-left: 2rem; border-left: 1px solid var(--rule); }
.hero-action-block .micro-label { color: var(--quiet); }

.section-heading {
  max-width: none;
  margin-bottom: 1.25rem;
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 2rem;
}

.section-heading h2:last-child { margin-bottom: 0; }
.count-badge { color: var(--quiet); font-family: var(--font-figures); font-size: 0.78rem; font-weight: 500; vertical-align: super; }

.editor-card,
.auth-panel {
  margin-bottom: 1.5rem;
  padding: clamp(1.25rem, 3vw, 2rem);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  background: var(--paper-strong);
  box-shadow: var(--shadow-paper);
}

.card-heading {
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(14rem, 0.8fr);
  align-items: end;
  gap: 2rem;
  border-bottom: 1px solid var(--rule);
}

.card-heading h2 { margin-bottom: 0; font-size: 1.45rem; }
.card-heading p { margin: 0; color: var(--muted); font-size: 0.85rem; }

.notice {
  margin: 0 0 1.5rem;
  padding: 1.5rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(15rem, 0.8fr);
  align-items: end;
  gap: 2rem;
  border: 1px solid var(--brand);
  border-left: 5px solid var(--brand);
  border-radius: var(--radius-sm);
  background: var(--brand-soft);
}

.notice h2, .notice p { margin-bottom: 0; }
.notice > p { color: var(--ink-soft); }
.locked-notice { border-color: var(--success); background: var(--success-soft); }

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem 1.25rem;
  margin-bottom: 1.25rem;
}

.form-field { display: grid; align-content: start; gap: 0.4rem; }
.form-field label { color: var(--ink-soft); font-size: 0.78rem; font-weight: 750; }

.form-field input,
.form-field select {
  width: 100%;
  min-height: 2.8rem;
  padding: 0.58rem 0.72rem;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  color: var(--ink);
  background: var(--paper-strong);
  font: inherit;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

.form-field input:hover,
.form-field select:hover { border-color: var(--muted); }
.form-field input:focus,
.form-field select:focus { border-color: var(--focus); box-shadow: 0 0 0 3px rgb(21 91 218 / 12%); outline: none; }
.has-error input,
.has-error select { border-color: var(--danger); }
.field-hint { color: var(--quiet); font-size: 0.75rem; }
.field-error { display: block; margin-top: 0.2rem; color: var(--danger); font-size: 0.78rem; font-weight: 700; }

.form-actions { display: flex; align-items: center; gap: 0.75rem; }
.save-note { color: var(--quiet); font-size: 0.75rem; }

.table-scroll { overflow-x: auto; outline-offset: 2px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.9rem 0.75rem; border-bottom: 1px solid var(--rule); text-align: left; vertical-align: middle; }
thead th { color: var(--muted); background: #f1f5f9; font-size: 0.68rem; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; }
tbody tr { transition: background-color 120ms ease; }
tbody tr:hover { background: #f8fafc; }
tbody th { font-weight: 650; }

.numeric-cell,
.line-table th:nth-child(n+2):nth-child(-n+6),
.document-table th:last-child {
  font-variant-numeric: tabular-nums lining-nums;
  text-align: right;
}

.total-cell { font-family: var(--font-figures); font-weight: 750; }
.discount-cell,
.discount-value { color: #1d4ed8; }
.row-actions { display: flex; align-items: center; justify-content: flex-end; gap: 0.35rem; white-space: nowrap; }
.row-actions a,
.row-actions button { min-height: 2.5rem; padding-inline: 0.6rem; }

.status-badge {
  width: max-content;
  min-height: 1.65rem;
  padding: 0.25rem 0.55rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid currentColor;
  border-radius: 999px;
  font-family: var(--font-figures);
  font-size: 0.65rem;
  font-weight: 750;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.status-badge::before { content: ""; width: 0.35rem; height: 0.35rem; border-radius: 50%; background: currentColor; }
.status-finalized { color: var(--success); background: var(--success-soft); }
.status-draft { color: var(--warning); background: var(--warning-soft); }

.totals-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin: 1.25rem 0 0;
  border: 1px solid var(--rule);
  border-radius: var(--radius-sm);
  background: var(--paper);
}

.totals-grid > div { min-width: 0; padding: 1rem 1.1rem; border-right: 1px solid var(--rule); }
.totals-grid > div:last-child { border-right: 0; }
.totals-grid dt { color: var(--muted); font-size: 0.72rem; }
.totals-grid dd { margin: 0.3rem 0 0; font-family: var(--font-figures); font-size: 1.2rem; font-weight: 750; letter-spacing: -0.035em; }
.totals-grid .metric-index { margin-bottom: 0.8rem; color: var(--quiet); }
.totals-grid .metric-primary { color: white; background: var(--brand); }
.totals-grid .metric-primary dt,
.totals-grid .metric-primary .metric-index { color: rgb(255 255 255 / 72%); }
.totals-grid .metric-primary dd { color: white; font-size: 1.45rem; }

.document-heading { align-items: end; margin-bottom: 2rem; padding-bottom: 1.5rem; border-bottom: 1px solid var(--rule-strong); }
.document-heading h1 { overflow-wrap: anywhere; }
.document-meta { margin: 0; display: flex; gap: 1rem; color: var(--muted); }
.document-meta span { font-family: var(--font-figures); font-size: 0.82rem; }
.document-heading-actions { display: flex; align-items: center; gap: 0.75rem; }
.ledger-heading { margin-bottom: 1rem; }
.ledger-rule { color: var(--quiet); font-family: var(--font-figures); font-size: 0.68rem; letter-spacing: 0.08em; text-transform: uppercase; }
.print-action { margin: 1.5rem 0 0; }

.finalize-zone,
.danger-zone {
  margin-top: 1.5rem;
  padding: 1.2rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  border: 1px solid var(--rule);
  border-left: 5px solid var(--warning);
  border-radius: var(--radius-sm);
  background: var(--warning-soft);
}

.finalize-zone p { margin: 0; color: var(--ink-soft); }
.finalize-zone .panel-index { margin-bottom: 0.25rem; color: var(--warning); }
.danger-zone { border-left-color: var(--danger); background: transparent; }
.danger-zone div { display: grid; }
.danger-zone span { color: var(--muted); font-size: 0.78rem; }

.status-line { margin: 1.25rem 0 0; color: var(--success); font-size: 0.82rem; font-weight: 700; }

.auth-shell {
  min-height: min(43rem, calc(100vh - 13rem));
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(25rem, 0.82fr);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--paper-strong);
  box-shadow: var(--shadow-paper);
}

.auth-intro {
  position: relative;
  padding: clamp(2rem, 5vw, 4.5rem);
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;
  color: white;
  background: var(--brand-hover);
}

.auth-intro::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.13;
  background-image: linear-gradient(rgb(255 255 255 / 30%) 1px, transparent 1px), linear-gradient(90deg, rgb(255 255 255 / 30%) 1px, transparent 1px);
  background-size: 3rem 3rem;
  pointer-events: none;
}

.auth-intro > * { position: relative; z-index: 1; }
.auth-intro .eyebrow { color: #bfdbfe; }
.auth-intro h1 { max-width: 11ch; color: white; }
.auth-intro > p:not(.eyebrow) { max-width: 35rem; margin: 0; color: rgb(255 255 255 / 72%); font-size: 1.02rem; }

.auth-proof { margin-top: auto; padding-top: 4rem; display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.auth-proof div { padding-top: 0.8rem; display: grid; border-top: 1px solid rgb(255 255 255 / 26%); }
.auth-proof span { color: #bfdbfe; font-family: var(--font-figures); font-size: 0.65rem; }
.auth-proof strong { margin-top: 0.45rem; color: white; font-size: 0.78rem; }
.auth-proof small { color: rgb(255 255 255 / 58%); font-size: 0.68rem; }

.auth-panel {
  width: min(100%, 32rem);
  margin: auto;
  padding: clamp(2rem, 5vw, 4rem);
  border: 0;
  box-shadow: none;
}

.auth-panel-heading { margin-bottom: 1.5rem; }
.auth-panel-heading p { margin: 0; color: var(--muted); }
.auth-switch { margin-bottom: 1.75rem; padding: 0.25rem; display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--rule); border-radius: var(--radius-sm); background: var(--canvas); }
.auth-switch a { min-height: 2.5rem; padding: 0.5rem 0.75rem; display: grid; place-items: center; color: var(--muted); font-size: 0.78rem; font-weight: 750; text-decoration: none; }
.auth-switch a[aria-current="page"] { color: var(--ink); background: var(--paper-strong); box-shadow: var(--shadow-paper); }
.auth-panel form { display: grid; gap: 1rem; }
.auth-submit { width: 100%; margin-top: 0.5rem; }

.report-page .section-heading h1 { margin-bottom: 0.7rem; }
.report-filter { display: grid; grid-template-columns: 0.65fr 1.6fr auto; align-items: end; gap: 1.5rem; }
.report-filter .form-grid { margin: 0; }
.filter-intro p { margin: 0; color: var(--muted); font-size: 0.8rem; }
.report-metrics { margin-top: 2rem; }

.visually-hidden {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.site-footer { border-top: 1px solid var(--rule); color: var(--muted); background: var(--paper); font-family: var(--font-figures); font-size: 0.66rem; }
.site-footer strong { color: var(--ink); }

@media (max-width: 56rem) {
  .header-layout { padding-block: 0.75rem; align-items: flex-start; flex-direction: column; gap: 0.6rem; }
  .primary-navigation { width: 100%; overflow-x: auto; }
  .primary-navigation a, .nav-sign-out { padding-inline: 0.65rem; white-space: nowrap; }
  .auth-shell { grid-template-columns: 1fr; }
  .auth-intro { min-height: auto; padding-block: 2.5rem; order: 2; }
  .auth-panel { order: 1; }
  .auth-proof { padding-top: 2.5rem; }
  .report-filter { grid-template-columns: 1fr; align-items: stretch; }
  .report-filter button { width: max-content; }
}

@media (max-width: 44rem) {
  .shell { width: min(100% - 1.5rem, var(--shell)); }
  .page-content { padding-block: 2rem 3.5rem; }
  .primary-navigation { display: grid; grid-template-columns: repeat(3, max-content); overflow: visible; }
  .compact-hero { grid-template-columns: 1fr; align-items: start; gap: 1.5rem; }
  .hero-action-block { padding: 0; border: 0; }
  .section-heading, .card-heading { grid-template-columns: 1fr; align-items: start; flex-direction: column; gap: 0.75rem; }
  .form-grid { grid-template-columns: 1fr; }
  .totals-grid { grid-template-columns: 1fr 1fr; }
  .totals-grid > div:nth-child(2) { border-right: 0; }
  .totals-grid > div:nth-child(-n+2) { border-bottom: 1px solid var(--rule); }
  .notice { grid-template-columns: 1fr; align-items: start; gap: 0.75rem; }
  .finalize-zone, .danger-zone { align-items: stretch; flex-direction: column; }
  .finalize-zone button, .danger-zone button { width: 100%; }
  .document-heading-actions { align-items: flex-start; flex-direction: column; }
  .document-meta { flex-direction: column; gap: 0.15rem; }
  .footer-layout { padding-block: 1.25rem; align-items: flex-start; flex-direction: column; gap: 0.35rem; }

  .document-table thead { display: none; }
  .document-table, .document-table tbody, .document-table tr, .document-table th, .document-table td { display: block; width: 100%; }
  .document-table tr { padding: 1rem; border-bottom: 1px solid var(--rule); }
  .document-table th, .document-table td { padding: 0.35rem 0; border: 0; white-space: normal; text-align: left; }
  .document-table td { display: flex; justify-content: space-between; gap: 1rem; }
  .document-table td::before { content: attr(data-label); color: var(--quiet); font-size: 0.68rem; font-weight: 750; letter-spacing: 0.05em; text-transform: uppercase; }
  .document-table .status-badge { margin-left: auto; }
}

@media (max-width: 31rem) {
  .brand-copy small { display: none; }
  h1 { font-size: 2.15rem; }
  .auth-intro, .auth-panel { padding: 1.5rem; }
  .auth-proof { grid-template-columns: 1fr; gap: 0.5rem; }
  .auth-proof div { grid-template-columns: auto 1fr; gap: 0 0.65rem; }
  .auth-proof span { grid-row: 1 / 3; }
  .totals-grid { grid-template-columns: 1fr; }
  .totals-grid > div { border-right: 0; border-bottom: 1px solid var(--rule); }
  .totals-grid > div:last-child { border-bottom: 0; }
  .form-actions { align-items: stretch; flex-direction: column; }
}

@media print {
  @page { margin: 16mm; }
  body { display: block; color: #000; background: #fff; font-size: 10pt; }
  body::before, .site-header, .site-footer, .skip-link, .message-stack, .no-print, .print-action, .request-progress { display: none !important; }
  .shell { width: 100%; }
  .page-content { padding: 0; }
  .editor-card { padding: 0; border: 0; box-shadow: none; }
  .table-scroll { overflow: visible; }
  table { font-size: 9pt; }
  h1 { max-width: none; font-size: 24pt; }
  thead { display: table-header-group; }
  tr, .totals-grid > div { break-inside: avoid; }
  .totals-grid .metric-primary { color: #000; background: #fff; }
  .totals-grid .metric-primary dt, .totals-grid .metric-primary dd, .totals-grid .metric-primary .metric-index { color: #000; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

@media (forced-colors: active) {
  .brand-mark, .status-badge { border: 1px solid CanvasText; }
  .totals-grid .metric-primary { border: 2px solid CanvasText; }
}
