*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  color: var(--ink);
  background: var(--surface);
  min-height: 100vh;
  display: flex;
}

/* ── Layout ───────────────────────────────────────── */

#app {
  display: flex;
  width: 100%;
  min-height: 100vh;
}

#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--paper);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: sticky;
  top: 0;
  overflow: hidden;
}

#main {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: var(--sp-8) var(--sp-6);
  gap: var(--sp-8);
}

/* ── Sidebar ──────────────────────────────────────── */

.sidebar-header {
  padding: var(--sp-5) var(--sp-5) var(--sp-4);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

.sidebar-logo img { height: 28px; width: auto; }

.sidebar-logo-text {
  font-family: var(--font-serif);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
  line-height: 1.2;
}

.btn-new {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  width: 100%;
  padding: var(--sp-2) var(--sp-3);
  background: var(--ink);
  color: var(--paper);
  border: none;
  border-radius: var(--r-md);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-out);
}
.btn-new:hover { background: oklch(30% 0 0); }

.sidebar-search {
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
}

.sidebar-search input {
  width: 100%;
  padding: var(--sp-2) var(--sp-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: var(--ink);
  outline: none;
  transition: border-color var(--dur-fast);
}
.sidebar-search input:focus { border-color: var(--ink-muted); }
.sidebar-search input::placeholder { color: var(--ink-faint); }

.invoice-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-2) 0;
}

.invoice-item {
  padding: var(--sp-3) var(--sp-4);
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background var(--dur-fast), border-color var(--dur-fast);
}
.invoice-item:hover { background: var(--surface); }
.invoice-item.active {
  background: var(--accent-dim);
  border-left-color: var(--accent);
}

.invoice-item-number {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--ink-muted);
  margin-bottom: 2px;
}
.invoice-item-client {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.invoice-item-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: var(--sp-1);
}
.invoice-item-amount {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--ink);
}

.status-pill {
  font-size: 0.6rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 2px 6px;
  border-radius: 20px;
  line-height: 1.6;
}
.status-pill.draft   { background: var(--surface-2); color: var(--ink-muted); }
.status-pill.sent    { background: oklch(85% 0.08 230 / 0.3); color: oklch(38% 0.1 230); }
.status-pill.paid    { background: oklch(85% 0.1 160 / 0.3); color: oklch(38% 0.12 160); }

.invoice-list-empty {
  padding: var(--sp-8) var(--sp-4);
  text-align: center;
  color: var(--ink-faint);
  font-size: var(--text-sm);
}

/* ── Toolbar ──────────────────────────────────────── */

#toolbar {
  width: 100%;
  max-width: var(--doc-w);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-wrap: wrap;
}

.toolbar-title {
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 600;
  flex: 1;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-4);
  border-radius: var(--r-md);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background var(--dur-fast), border-color var(--dur-fast), color var(--dur-fast);
  white-space: nowrap;
}

.btn-ghost {
  background: transparent;
  border-color: var(--border-2);
  color: var(--ink-muted);
}
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); border-color: var(--border-2); }

.btn-primary {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}
.btn-primary:hover { background: oklch(30% 0 0); }

.btn-danger {
  background: transparent;
  border-color: transparent;
  color: var(--danger);
}
.btn-danger:hover { background: oklch(95% 0.02 25); }

/* ── Editor sections ──────────────────────────────── */

.editor-section {
  width: 100%;
  max-width: var(--doc-w);
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-4) var(--sp-6);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  transition: background var(--dur-fast);
}
.section-header:hover { background: var(--surface); }

.section-title {
  font-size: var(--text-sm);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-muted);
}

.section-chevron {
  color: var(--ink-faint);
  transition: transform var(--dur-fast);
  font-size: var(--text-sm);
}
.section-header.collapsed .section-chevron { transform: rotate(-90deg); }

.section-body {
  padding: var(--sp-6);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-4) var(--sp-6);
}
.section-body.full { grid-template-columns: 1fr; }
.section-body.collapsed { display: none; }

/* ── Form fields ──────────────────────────────────── */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
}
.field.span-2 { grid-column: span 2; }

.field label {
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-muted);
}

.field input,
.field textarea,
.field select {
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--border-2);
  border-radius: var(--r-sm);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: var(--ink);
  background: var(--paper);
  outline: none;
  transition: border-color var(--dur-fast);
  width: 100%;
}
.field input:focus,
.field textarea:focus,
.field select:focus { border-color: var(--ink-muted); }
.field input::placeholder,
.field textarea::placeholder { color: var(--ink-faint); }

.field textarea { resize: vertical; min-height: 72px; }

.field input[type="date"] { font-family: var(--font-mono); font-size: var(--text-sm); }

/* ── Line items table ─────────────────────────────── */

#lines-section .section-body {
  padding: 0;
  display: block;
}

.lines-table {
  width: 100%;
  border-collapse: collapse;
}

.lines-table thead th {
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-muted);
  text-align: left;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.lines-table thead th.num { text-align: right; }
.lines-table thead th.center { text-align: center; }

.lines-table tbody tr { transition: background var(--dur-fast); }
.lines-table tbody tr:hover { background: var(--surface); }
.lines-table tbody tr + tr td { border-top: 1px solid var(--border); }

.lines-table td {
  padding: var(--sp-2) var(--sp-3);
  vertical-align: middle;
}
.lines-table td.num { text-align: right; }

.line-input {
  width: 100%;
  border: none;
  background: transparent;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: var(--ink);
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--r-sm);
  outline: none;
  transition: background var(--dur-fast);
}
.line-input:focus { background: var(--accent-dim); }
.line-input.mono { font-family: var(--font-mono); text-align: right; }
.line-input.desc { min-width: 200px; }

.line-amount {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  color: var(--ink);
  text-align: right;
  padding-right: var(--sp-2);
  white-space: nowrap;
}

.gst-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
}
.gst-toggle input { width: 14px; height: 14px; cursor: pointer; accent-color: var(--accent); }

.btn-remove-line {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--ink-faint);
  padding: var(--sp-1);
  border-radius: var(--r-sm);
  transition: color var(--dur-fast), background var(--dur-fast);
  font-size: 1rem;
  line-height: 1;
}
.btn-remove-line:hover { color: var(--danger); background: oklch(95% 0.02 25); }

.lines-footer {
  display: flex;
  align-items: center;
  padding: var(--sp-3) var(--sp-4);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.btn-add-line {
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: var(--ink-muted);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--r-sm);
  transition: color var(--dur-fast), background var(--dur-fast);
}
.btn-add-line:hover { color: var(--ink); background: var(--surface-2); }

/* ── Totals rail ──────────────────────────────────── */

.totals-section {
  width: 100%;
  max-width: var(--doc-w);
  display: flex;
  justify-content: flex-end;
}

.totals-card {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--sp-5) var(--sp-6);
  min-width: 300px;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.totals-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--sp-8);
}
.totals-label { font-size: var(--text-sm); color: var(--ink-muted); }
.totals-value { font-family: var(--font-mono); font-size: var(--text-sm); color: var(--ink); }

.totals-divider { border: none; border-top: 1px solid var(--border); margin: var(--sp-2) 0; }

.totals-row.grand .totals-label {
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--ink);
}
.totals-row.grand .totals-value {
  font-family: var(--font-mono);
  font-size: var(--text-xl);
  font-weight: 500;
  color: var(--ink);
}

/* ── Status selector in toolbar ───────────────────── */
#status-select {
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--border-2);
  border-radius: var(--r-md);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: var(--ink-muted);
  background: var(--paper);
  cursor: pointer;
  outline: none;
}

/* ── Toast ────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: var(--sp-6);
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--ink);
  color: var(--paper);
  padding: var(--sp-3) var(--sp-5);
  border-radius: var(--r-md);
  font-size: var(--text-sm);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-fast), transform var(--dur-fast);
  z-index: 100;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
