:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --border: #e3e7ee;
  --primary: #1f5fbf;
  --primary-text: #ffffff;
  --pos: #12804a;
  --neg: #c0362c;
  --warn-bg: #fff6e0;
  --warn-text: #8a5a00;
  --ok-bg: #e7f6ee;
  --ok-text: #12623b;
  --row-alt: #f9fafc;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b;
    --surface: #171e27;
    --text: #e6eaf0;
    --muted: #93a0b2;
    --border: #273141;
    --primary: #4c8df0;
    --primary-text: #0b1220;
    --pos: #3ccf86;
    --neg: #ff7a6e;
    --warn-bg: #3a2f12;
    --warn-text: #ffd27a;
    --ok-bg: #12301f;
    --ok-text: #7fe0a9;
    --row-alt: #1b232e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
h3 { font-size: .95rem; margin: 1.25rem 0 .5rem; }
code { background: var(--row-alt); padding: 0 .3em; border-radius: 4px; }

.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: .7rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10; flex-wrap: wrap;
}
.brand { font-weight: 700; color: var(--text); }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.topbar nav a { padding: .35rem .7rem; border-radius: 6px; color: var(--muted); }
.topbar nav a.active, .topbar nav a:hover { background: var(--row-alt); color: var(--text); text-decoration: none; }
.logout { margin-left: auto; }

.wrap { max-width: 1200px; margin: 0 auto; padding: 1.25rem 1rem 3rem; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head p { margin: 0; flex-basis: 100%; }
.actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.card.empty { text-align: center; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 1rem; align-items: start; }
.grid2 > .card { margin-bottom: 0; }
.stack { display: flex; flex-direction: column; gap: .8rem; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; }
.kpi span { display: block; color: var(--muted); font-size: .85rem; }
.kpi strong { font-size: 1.45rem; font-variant-numeric: tabular-nums; }

.pos { color: var(--pos); }
.neg { color: var(--neg); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.note { margin-top: 1.5rem; }
.warn-text { color: var(--warn-text); }
.nowrap { white-space: nowrap; }
.upper { text-transform: uppercase; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
tbody tr:nth-child(even) { background: var(--row-alt); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.desc { min-width: 180px; }
.table-scroll { overflow-x: auto; }
.row-error td { color: var(--neg); }
.row-dup td { opacity: .6; }

.tag { font-size: .7rem; padding: .05rem .35rem; border-radius: 4px; border: 1px solid var(--border); color: var(--muted); }
.kind { font-size: .8rem; padding: .1rem .45rem; border-radius: 999px; white-space: nowrap; }
.kind.out { background: var(--row-alt); border: 1px solid var(--border); }
.kind.in { background: var(--ok-bg); color: var(--ok-text); }

.totals { display: flex; flex-wrap: wrap; gap: 1.25rem; margin: 0 0 1rem; align-items: baseline; }

.filters { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.filters label { min-width: 130px; }
.filters .grow { flex: 1; min-width: 200px; }

label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 7px; padding: .5rem .6rem; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input[type=checkbox] { width: auto; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .9rem 1rem; }
.form-grid.tight { grid-template-columns: repeat(3, 1fr); gap: .6rem; }
.span2 { grid-column: 1 / -1; }
.inline { display: flex; gap: .5rem; align-items: center; }
.inline input, .inline select { width: auto; flex: 1; }
.choice { flex-direction: row; align-items: center; justify-content: space-between; gap: 1rem; padding: .5rem 0; border-bottom: 1px solid var(--border); color: var(--text); }
.choice select { max-width: 340px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  font: inherit; font-size: .9rem; padding: .5rem .9rem; border-radius: 7px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:hover { text-decoration: none; filter: brightness(.97); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { color: var(--neg); border-color: var(--neg); background: transparent; }
.btn.small { padding: .3rem .6rem; font-size: .8rem; }
button.link { background: none; border: 0; color: var(--primary); cursor: pointer; font: inherit; padding: 0; }
.danger-zone { margin-top: .5rem; text-align: right; }
.sticky-actions { position: sticky; bottom: 0; background: var(--bg); padding: .75rem 0; }

.flash { padding: .7rem .9rem; border-radius: 8px; margin-bottom: 1rem; }
.flash p { margin: 0 0 .5rem; }
.flash.ok { background: var(--ok-bg); color: var(--ok-text); }
.flash.warn { background: var(--warn-bg); color: var(--warn-text); }

.recent { list-style: none; margin: 0; padding: 0; }
.recent a { display: grid; grid-template-columns: 90px 1fr auto; gap: .5rem; padding: .45rem 0; border-bottom: 1px solid var(--border); color: var(--text); }
.recent a:hover { text-decoration: none; background: var(--row-alt); }
.aliases { list-style: none; padding: 0; margin: 0; }
.aliases li { display: flex; justify-content: space-between; padding: .35rem 0; border-bottom: 1px solid var(--border); }

.login-box { max-width: 380px; margin: 12vh auto 0; padding: 1.75rem; }
.login-box p { margin: .25rem 0 1.25rem; }

@media (max-width: 640px) {
  .form-grid, .form-grid.tight { grid-template-columns: 1fr; }
  .kpi strong { font-size: 1.2rem; }
  .topbar { gap: .5rem; }
  .logout { margin-left: 0; }
  .choice { flex-direction: column; align-items: stretch; }
}
