:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #667085;
  --border: #e4e7ec;
  --primary: #0f766e;
  --primary-hover: #115e59;
  --success: #059669;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); line-height: 1.5; font-size: 15px; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 0.5em; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.95rem; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1.25rem 0; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.center { text-align: center; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.error, .neg { color: var(--danger); }
.pos { color: var(--success); }
.pre { white-space: pre-line; }
.loading, .empty { color: var(--muted); padding: 1.5rem; text-align: center; margin: 0; }
.label { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin-bottom: 0.25rem; }
.more { padding-top: 1rem; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.5rem 0.95rem; border-radius: 8px; border: 1px solid transparent;
  background: var(--primary); color: #fff; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--primary-hover); text-decoration: none; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { color: var(--danger); }
.btn-small { padding: 0.3rem 0.65rem; font-size: 0.85rem; }
.btn-block { width: 100%; }
.link { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; cursor: pointer; }
.link:hover { text-decoration: underline; }
.link.danger { color: var(--danger); }
.link.small { font-size: 0.85rem; margin: -0.4rem 0 1rem; }

/* Layout */
.topbar {
  display: flex; align-items: center; gap: 1.25rem; padding: 0.7rem 1.5rem;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.brand { font-weight: 800; font-size: 1.15rem; color: var(--text); letter-spacing: -0.02em; }
.brand::before { content: "■"; color: var(--primary); margin-right: 0.4rem; }
.topbar nav { display: flex; gap: 0.2rem; flex-wrap: wrap; }
.topbar nav a { padding: 0.4rem 0.7rem; border-radius: 6px; color: var(--muted); font-weight: 500; }
.topbar nav a:hover { background: var(--bg); text-decoration: none; }
.topbar nav a.active { color: var(--text); background: var(--bg); }
.topbar-right { margin-left: auto; display: flex; gap: 0.5rem; align-items: center; }
.topbar-right select { width: auto; max-width: 220px; padding: 0.3rem 0.5rem; font-size: 0.9rem; }
.container { max-width: 1180px; margin: 0 auto; padding: 1.5rem; }
.narrow { max-width: 760px; margin: 0 auto; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.page-head h1, .page-head h2 { margin: 0; display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem; margin-bottom: 1.25rem; }
.callout { border-left: 4px solid var(--primary); padding: 0.8rem 1.1rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
.grid-2 > .card { margin-bottom: 1.25rem; }
.grid-sidebar { display: grid; grid-template-columns: 1fr 340px; gap: 1.25rem; align-items: start; }
.stack > .card:last-child { margin-bottom: 0; }

/* Forms */
.form label { display: flex; flex-direction: column; gap: 0.3rem; font-weight: 500; margin-bottom: 0.9rem; }
.form label small { font-weight: 400; }
.form label.check, .filters label.check { flex-direction: row; align-items: center; gap: 0.5rem; font-weight: 500; }
.check input { width: auto; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 1rem; }
.form-grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
input, select, textarea {
  font: inherit; padding: 0.5rem 0.65rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input.invalid { border-color: var(--danger); }
input[readonly] { background: var(--bg); color: var(--muted); }
textarea { resize: vertical; }
.form-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.5rem; flex-wrap: wrap; }
.form-actions .spacer { flex: 1; }
.inline-form { margin-top: 1.25rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.filters { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: end; margin-bottom: 1rem; }
.filters label { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.filters label:first-child { flex: 1; min-width: 200px; }
.filters label.check { color: var(--text); padding-bottom: 0.55rem; font-size: 0.9rem; }
.filters select, .filters input { padding: 0.4rem 0.55rem; }

/* Document lines editor */
.lines { margin: 0.5rem 0 1rem; }
.line { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 2fr) 90px 110px 80px 110px 36px; gap: 0.5rem; align-items: center; margin-bottom: 0.5rem; }
.no-prices .line { grid-template-columns: minmax(0, 2fr) minmax(0, 2fr) 110px 36px; }
.line-head { font-size: 0.78rem; color: var(--muted); font-weight: 600; margin-bottom: 0.25rem; }
.l-amount { font-weight: 600; }
.l-remove { padding: 0.3rem; }

/* Auth */
.auth { max-width: 420px; margin: 7vh auto; padding: 0 1rem; display: flex; flex-direction: column; gap: 1.25rem; align-items: center; }
.auth .card { width: 100%; padding: 1.75rem; }
.auth-links { display: flex; justify-content: center; gap: 1.25rem; margin: 1rem 0 0; }
.auth-links:empty { display: none; }

/* Tables */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 0.78rem; color: var(--muted); font-weight: 600; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 0.6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th.num { text-align: right; }
.table tr:last-child td { border-bottom: 0; }
.row-link { cursor: pointer; }
.row-link:hover td { background: var(--bg); }
.details { display: grid; grid-template-columns: auto 1fr; gap: 0.4rem 1rem; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; text-align: right; }

/* Badges & tabs */
.badge { display: inline-block; padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600; vertical-align: middle; }
.badge-draft { background: #fffaeb; color: #b54708; }
.badge-posted { background: #ecfdf3; color: #027a48; }
.badge-void { background: #f2f4f7; color: #475467; }
.tabs { display: flex; gap: 0.25rem; margin-bottom: 1rem; flex-wrap: wrap; }
.tab { padding: 0.4rem 0.8rem; border-radius: 8px; color: var(--muted); border: 1px solid transparent; }
.tab:hover { text-decoration: none; color: var(--text); }
.tab.active { background: var(--surface); border-color: var(--border); color: var(--text); font-weight: 600; }

/* Dashboard */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.25rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; box-shadow: var(--shadow); }
.stat-label { color: var(--muted); font-size: 0.85rem; }
.stat-value { font-size: 1.45rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--muted); font-size: 0.78rem; }
.stat-alert { border-color: #fecdca; background: #fffbfa; }

/* Printable document */
.doc {
  position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 2.25rem; max-width: 860px; margin: 0 auto 1rem; overflow: hidden;
}
.doc-head { display: flex; justify-content: space-between; gap: 2rem; margin-bottom: 1.75rem; flex-wrap: wrap; }
.doc-org { font-size: 1.35rem; font-weight: 700; }
.doc-meta { text-align: right; }
.doc-meta .muted { display: inline-block; min-width: 4.5rem; }
.doc-title { font-size: 1.6rem; font-weight: 800; color: var(--primary); letter-spacing: -0.02em; }
.doc-info { display: flex; gap: 2.5rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
.doc-lines { width: 100%; border-collapse: collapse; margin-bottom: 1.25rem; }
.doc-lines th { text-align: left; font-size: 0.78rem; color: var(--muted); border-bottom: 2px solid var(--text); padding: 0.5rem 0.4rem; }
.doc-lines th.num { text-align: right; }
.doc-lines td { padding: 0.6rem 0.4rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.doc-totals { margin-left: auto; max-width: 300px; }
.doc-totals > div { display: flex; justify-content: space-between; padding: 0.25rem 0; font-variant-numeric: tabular-nums; }
.doc-totals .grand { border-top: 2px solid var(--text); margin-top: 0.3rem; padding-top: 0.55rem; font-weight: 700; font-size: 1.1rem; }
.doc-notes { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--border); }
.stamp {
  position: absolute; top: 2rem; right: -2.8rem; transform: rotate(35deg); padding: 0.3rem 3.5rem;
  font-weight: 800; letter-spacing: 0.1em; text-transform: uppercase; font-size: 0.85rem; color: #fff; background: var(--danger);
}

/* Toast */
.toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%); z-index: 10;
  padding: 0.7rem 1.1rem; border-radius: 8px; color: #fff; font-weight: 500; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  max-width: calc(100vw - 2rem);
}
.toast-ok { background: #101828; }
.toast-error { background: var(--danger); }

/* Responsive */
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid-2, .grid-sidebar { grid-template-columns: 1fr; }
  .form-grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .container { padding: 1rem; }
  .topbar { padding: 0.7rem 1rem; gap: 0.75rem; }
  .topbar nav { order: 3; width: 100%; overflow-x: auto; flex-wrap: nowrap; }
  .line-head { display: none; }
  .line { grid-template-columns: repeat(3, minmax(0, 1fr)); padding-bottom: 0.6rem; border-bottom: 1px solid var(--border); }
  .no-prices .line { grid-template-columns: minmax(0, 1fr) auto; }
  .line .l-item, .line .l-desc { grid-column: 1 / -1; }
  .line .l-amount { grid-column: 1 / 3; text-align: left; }
  .line .l-remove { justify-self: end; }
  .doc { padding: 1.25rem; }
  .doc-meta { text-align: left; }
}
@media (max-width: 560px) {
  .form-grid, .form-grid.cols-4 { grid-template-columns: 1fr; }
  .stats { grid-template-columns: 1fr; }
}

/* Print: just the document */
@media print {
  body { background: #fff; }
  .no-print, .topbar, .toast { display: none !important; }
  .container { padding: 0; }
  .doc { border: 0; box-shadow: none; padding: 0; max-width: none; }
}
