:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --text: #17212b;
  --muted: #64707d;
  --border: #e2e7ec;
  --primary: #0f766e;
  --primary-strong: #0b5f58;
  --primary-soft: #ccfbf1;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --success-soft: #dcfce7;
  --success: #15803d;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #182029;
    --surface-2: #1e2731;
    --text: #e6ebf0;
    --muted: #93a1af;
    --border: #2a3541;
    --primary: #2dd4bf;
    --primary-strong: #5eead4;
    --primary-soft: #134e4a;
    --warn: #fbbf24;
    --warn-soft: #422006;
    --danger: #f87171;
    --danger-soft: #450a0a;
    --success-soft: #052e16;
    --success: #4ade80;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--primary); }
h1 { font-size: 1.6rem; margin: 0 0 .25rem; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }
.center { text-align: center; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: .6rem 1.5rem;
  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); text-decoration: none; white-space: nowrap; }
.nav { display: flex; gap: .25rem; flex: 1; flex-wrap: wrap; }
.nav a {
  padding: .4rem .75rem; border-radius: 8px;
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--primary-soft); color: var(--primary-strong); }
.userbox { display: flex; align-items: center; gap: 1rem; }
.userbox a { color: var(--text); text-decoration: none; }
.userbox form { margin: 0; }
.badge {
  font-size: .7rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  background: var(--primary-soft); color: var(--primary-strong);
  padding: .1rem .4rem; border-radius: 6px;
}

/* ---------- Layout ---------- */
.container { max-width: 1240px; margin: 0 auto; padding: 1.5rem; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.page-head p { margin: 0; max-width: 70ch; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}
.card.narrow { max-width: 440px; }
.card.empty { text-align: center; padding: 2.5rem 1rem; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.card-head h2 { margin: 0; }
.grid-2 { display: grid; grid-template-columns: 3fr 2fr; gap: 1.25rem; }
.grid-2 > .card { margin-bottom: 1.25rem; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; gap: 0; } }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.25rem; }
@media (max-width: 900px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem 1.25rem; display: flex; flex-direction: column;
}
.kpi-label { font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.kpi-value { font-size: 1.9rem; font-weight: 700; font-variant-numeric: tabular-nums; margin: .15rem 0; }
.kpi-sub { font-size: .85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kpi-warn { border-color: var(--warn); background: var(--warn-soft); }
.kpi-warn .kpi-value { color: var(--warn); }

/* ---------- Ranking ---------- */
.ranking { list-style: none; margin: 0; padding: 0; }
.ranking li {
  display: grid; grid-template-columns: 1.75rem minmax(5rem, 9rem) 1fr 3rem;
  align-items: center; gap: .6rem; padding: .4rem 0;
}
.rank {
  width: 1.6rem; height: 1.6rem; border-radius: 50%; display: grid; place-items: center;
  background: var(--surface-2); font-size: .8rem; font-weight: 700; color: var(--muted);
}
.ranking li:first-child .rank { background: #fde68a; color: #78350f; }
.rank-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-bar { height: .55rem; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.rank-bar span { display: block; height: 100%; background: var(--primary); border-radius: 99px; }
.rank-val { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Bestand ---------- */
.stock-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: .75rem; }
.stock-item {
  border: 1px solid var(--border); border-radius: 10px; padding: .75rem .9rem;
  display: flex; flex-direction: column; background: var(--surface-2);
}
.stock-name { font-weight: 600; }
.stock-val { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stock-val small { font-size: .8rem; font-weight: 400; color: var(--muted); }
.stock-sub { font-size: .8rem; color: var(--muted); }
.stock-item.low { border-color: var(--warn); background: var(--warn-soft); }
.stock-item.low .stock-val { color: var(--warn); }
.stock-item.empty { border-color: var(--danger); background: var(--danger-soft); }
.stock-item.empty .stock-val { color: var(--danger); }

/* ---------- Tabellen ---------- */
.table-wrap { overflow-x: auto; margin: 0 -1.25rem; padding: 0 1.25rem; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: .55rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; white-space: nowrap; }
th small { text-transform: none; letter-spacing: 0; }
.num { text-align: right; }
tbody tr:hover { background: var(--surface-2); }
tfoot td { font-weight: 700; border-bottom: none; border-top: 2px solid var(--border); }
.matrix td.zero { color: var(--border); }
.matrix th:first-child, .matrix td:first-child { position: sticky; left: 0; background: var(--surface); }
td.actions { white-space: nowrap; text-align: right; }
td.actions form { display: inline; margin-left: .5rem; }
tr.inactive td { opacity: .55; }
.neg { color: var(--danger); }
.pos { color: var(--success); }
.edit-table input:not([type=checkbox]), .edit-table select { width: 100%; min-width: 6rem; }
.edit-table input.qty { min-width: 4.5rem; }

/* ---------- Formulare ---------- */
label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; font-weight: 500; color: var(--muted); margin-bottom: .9rem; }
input, select, button { font: inherit; }
input:not([type=checkbox]), select {
  padding: .5rem .65rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input[type=checkbox] { width: 1.1rem; height: 1.1rem; accent-color: var(--primary); }
input.qty { width: 5rem; text-align: right; }
input.qty:not(:placeholder-shown) { font-weight: 600; }
.form-row { display: flex; gap: 1rem; flex-wrap: wrap; align-items: flex-end; }
.form-row label { margin-bottom: 1rem; }
.form-row .btn { margin-bottom: 1rem; }
.form-row .grow { flex: 1; min-width: 200px; }
.form-actions { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-top: 1rem; flex-wrap: wrap; }
.tally input.qty { width: 4.5rem; }
.tally td.row-sum, td.row-sum { font-weight: 700; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .5rem 1rem; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-strong); }
@media (prefers-color-scheme: dark) { .btn-primary { color: #042f2e; } }
.btn-small { padding: .3rem .7rem; font-size: .85rem; }
.btn-block { width: 100%; }
.btn-link { background: none; border: none; padding: 0; color: var(--muted); cursor: pointer; font-weight: 500; }
.btn-link:hover { color: var(--text); text-decoration: underline; }
.btn-link.danger { color: var(--danger); }

.range-form { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.custom-range { display: inline-flex; gap: .5rem; flex-wrap: wrap; }

.flash { padding: .75rem 1rem; border-radius: 10px; margin-bottom: 1.25rem; font-weight: 500; }
.flash-success { background: var(--success-soft); color: var(--success); }
.flash-error { background: var(--danger-soft); color: var(--danger); }

.chart-box { position: relative; width: 100%; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login { width: 100%; max-width: 380px; padding: 2rem; }
.login h1 { text-align: center; }
.login > p { text-align: center; margin-top: 0; margin-bottom: 1.5rem; }
.login-logo { font-size: 2.5rem; text-align: center; }

@media (max-width: 640px) {
  .container { padding: 1rem; }
  .topbar { padding: .6rem 1rem; gap: .5rem; }
  .userbox { margin-left: auto; }
  .nav { order: 3; flex-basis: 100%; overflow-x: auto; flex-wrap: nowrap; }
}
