:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #14202b;
  --muted: #5e6a76;
  --border: #e3e7ec;
  --accent: #0f6e6e;
  --accent-strong: #0b5555;
  --accent-soft: #e5f4f4;
  --on-accent: #ffffff;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --ok: #067647;
  --ok-soft: #e7f6ee;
  --warn: #b54708;
  --warn-soft: #fef4e6;
  --info: #175cd3;
  --info-soft: #e8f0fe;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px rgba(16, 24, 40, 0.05);
  --radius: 14px;
  --top: 56px;
  --tab: calc(62px + env(safe-area-inset-bottom));
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1418;
    --surface: #1a2128;
    --text: #e7edf2;
    --muted: #9aa8b5;
    --border: #2c3844;
    --accent: #3dbebf;
    --accent-strong: #8fe3e3;
    --accent-soft: #143233;
    --on-accent: #062224;
    --danger: #ff8b80;
    --danger-soft: #3a1d1b;
    --ok: #6ce9a6;
    --ok-soft: #123224;
    --warn: #fdb022;
    --warn-soft: #3a2a10;
    --info: #84adff;
    --info-soft: #17233a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100vh;
}

img { max-width: 100%; }

button, input, select, textarea { font: inherit; color: inherit; }

button { cursor: pointer; }

a { color: var(--accent-strong); }

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

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--top);
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  text-decoration: none;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.brand svg { width: 28px; height: 28px; color: var(--accent); }

.nav {
  display: none;
}

.nav a, .nav button {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  text-align: left;
}

.nav a.active { background: var(--accent-soft); color: var(--accent-strong); }

.btn-new { margin-left: auto; }

.icon-btn, .btn, .tabbar a {
  touch-action: manipulation;
}

.icon-btn {
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
}

.icon-btn svg, .btn svg, .tabbar svg { width: 22px; height: 22px; display: block; }

.icon-btn.danger { color: var(--danger); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-weight: 650;
  font-weight: 600;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

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

.btn-small { min-height: 44px; padding: 0 12px; }

.btn:disabled { opacity: 0.55; }

main {
  width: min(1100px, 100%);
  margin: 0 auto;
  padding: 16px 16px calc(var(--tab) + 16px);
}

h1 { font-size: 1.45rem; line-height: 1.2; margin: 0; letter-spacing: -0.02em; }
h2 { font-size: 1.05rem; margin: 0 0 10px; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.page-head .actions, .head-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }

.card, .stat, .list, .paper, .modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card { padding: 16px; margin-bottom: 14px; }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.stat { padding: 12px 14px; min-height: 92px; }
.stat-label { margin: 0 0 6px; color: var(--muted); font-size: 0.78rem; font-weight: 700; letter-spacing: 0.02em; text-transform: uppercase; }
.stat-value { margin: 0; font-size: 1.25rem; font-weight: 750; font-weight: 700; letter-spacing: -0.03em; overflow-wrap: anywhere; }
.stat-sub { margin: 4px 0 0; color: var(--muted); font-size: 0.82rem; }

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.field, .filters label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--muted);
}

.field input, .field select, .field textarea,
.filters input, .filters select,
.modal input, .modal select, .modal textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  font-weight: 450;
  font-weight: 400;
}

textarea { min-height: 96px; resize: vertical; }

.filters .grow { grid-column: 1 / -1; }

.list { padding: 4px 12px 8px; }

.inv-head { display: none; }

.inv-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.inv-row:last-child { border-bottom: 0; }

.inv-main {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "num total"
    "co status"
    "date date";
  gap: 2px 12px;
  min-height: 44px;
  color: inherit;
  text-decoration: none;
}

.c-num { grid-area: num; font-weight: 750; font-weight: 700; }
.c-total { grid-area: total; text-align: right; font-weight: 700; }
.c-co { grid-area: co; color: var(--muted); }
.c-date { grid-area: date; color: var(--muted); font-size: 0.88rem; }
.c-status { grid-area: status; text-align: right; }

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  text-transform: capitalize;
  background: var(--border);
  color: var(--text);
}

.badge.draft { background: var(--border); }
.badge.sent { background: var(--info-soft); color: var(--info); }
.badge.paid { background: var(--ok-soft); color: var(--ok); }
.badge.overdue { background: var(--warn-soft); color: var(--warn); margin-left: 4px; }

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 12px;
}

.pager p { margin: 0; color: var(--muted); }
[aria-disabled="true"] { opacity: 0.45; pointer-events: none; }

.item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
}
.item p { margin: 2px 0 0; }

.empty {
  text-align: center;
  padding: 28px 12px;
  color: var(--muted);
}

.empty svg { width: 56px; height: 56px; margin-bottom: 8px; color: var(--accent); }
.empty h2 { color: var(--text); }
.empty p { margin: 6px 0 14px; }

.back {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  margin-bottom: 4px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0;
}

.actions .btn { flex: 1 1 calc(50% - 8px); }

.paper { padding: 16px; }

.meta-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 16px;
  margin: 12px 0;
}

.meta-grid span { display: block; color: var(--muted); font-size: 0.78rem; font-weight: 700; text-transform: uppercase; }
.meta-grid strong { font-weight: 650; font-weight: 600; }

.lines { width: 100%; border-collapse: collapse; }
.lines th { text-align: left; font-size: 0.75rem; color: var(--muted); text-transform: uppercase; padding: 8px 4px; }
.lines td { padding: 8px 4px; border-top: 1px solid var(--border); vertical-align: top; }
.lines .right, .totals .right { text-align: right; }

.totals { margin-top: 8px; margin-left: auto; max-width: 280px; }
.totals div { display: flex; justify-content: space-between; gap: 16px; padding: 4px 0; }
.totals .grand { font-size: 1.15rem; font-weight: 800; border-top: 2px solid var(--border); margin-top: 4px; padding-top: 8px; }

.pre { white-space: pre-wrap; }

.line {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  align-items: end;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.line .line-desc { grid-column: 1 / -1; }
.line-total { min-height: 44px; display: flex; align-items: center; justify-content: flex-end; font-weight: 700; }

.grid-2 { display: grid; gap: 12px; }

.logo-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.logo-row img { width: 72px; height: 72px; object-fit: contain; border: 1px solid var(--border); border-radius: 12px; background: #fff; }
.logo-ph {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  border: 1px dashed var(--border);
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 0.75rem;
}

.entity h2 { margin-bottom: 4px; }
.entity p { margin: 2px 0; }
.row-actions { display: flex; gap: 8px; margin-top: 10px; }

.help { margin: 8px 0 0; color: var(--muted); font-size: 0.85rem; font-weight: 500; }

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar a {
  flex: 1;
  min-height: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: 11px;
  font-weight: 650;
  font-weight: 600;
}

.tabbar a.active { color: var(--accent-strong); }

#toasts {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(var(--tab) + 8px);
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  background: var(--text);
  color: var(--bg);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.toast.error { background: var(--danger); color: white; }

.modal-back {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(8, 12, 16, 0.48);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal {
  width: min(560px, 100%);
  max-height: min(92vh, 820px);
  overflow: auto;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}

.modal h2 { margin-bottom: 12px; }

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.modal-actions .btn { flex: 1; }

.loading { color: var(--muted); padding: 24px 0; }

.login-card { width: min(420px, 100%); margin: 8vh auto 0; }
.login-card h1 { margin-bottom: 8px; }
.login-card .field { margin-top: 14px; }

body.locked .topbar,
body.locked .tabbar { display: none; }
body.locked main { padding-bottom: 32px; }
body.locked #toasts { bottom: 16px; }

@media (min-width: 640px) {
  .grid-2 { grid-template-columns: 1fr 1fr; }
  .modal-back { align-items: center; padding: 16px; }
  .modal { border-radius: var(--radius); }
  .actions .btn { flex: 0 0 auto; }
}

@media (min-width: 800px) {
  .stats { grid-template-columns: repeat(4, 1fr); }
  .filters { grid-template-columns: 110px 150px 1fr 130px 1.3fr; align-items: end; }
  .filters .grow { grid-column: auto; }
  .inv-head { display: block; padding: 10px 0 4px; }
  .inv-head-row, .inv-main {
    display: grid;
    grid-template-columns: 1.1fr 0.9fr 1.3fr 1fr 0.9fr;
    grid-template-areas: none;
    gap: 8px 12px;
    align-items: center;
  }
  .inv-head-row {
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }
  .c-num, .c-date, .c-co, .c-total, .c-status { grid-area: auto; text-align: left; color: inherit; font-size: 1rem; }
  .c-co, .c-date { color: var(--text); }
  .c-total, .c-status { text-align: left; }
  .inv-row { padding: 8px 0; }
  .line {
    grid-template-columns: 1fr 88px 120px 120px 44px;
  }
  .line .line-desc { grid-column: auto; }
}

@media (min-width: 900px) {
  #nav-toggle { display: none; }
  .nav {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-left: 8px;
  }
  .tabbar { display: none; }
  main { padding-bottom: 32px; }
  #toasts { left: auto; width: min(360px, calc(100% - 24px)); bottom: 16px; }
}

@media (max-width: 899px) {
  .nav.open {
    display: flex;
    flex-direction: column;
    position: absolute;
    top: var(--top);
    left: 0;
    right: 0;
    padding: 8px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
  }
}

@media print {
  .topbar, .tabbar, .no-print, #toasts, #modal-root { display: none !important; }
  body { background: white; color: black; }
  main { padding: 0; max-width: none; }
  .card, .paper, .list { box-shadow: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
