:root {
  --bg: #f4f6f9; --surface: #fff; --surface-2: #f8fafc; --border: #dde3ec; --text: #1c2733; --muted: #66758a;
  --accent: #1f3a5f; --accent-2: #2f6fb3; --accent-contrast: #fff; --danger: #c0392b; --ok: #1e8e5a; --warn: #c27a00;
  --st-B: #1e8e5a; --st-mB: #e0a100; --st-P: #2f6fb3; --st-oB: #c0392b;
  --shadow: 0 1px 2px rgba(20,35,55,.06), 0 4px 14px rgba(20,35,55,.05);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f151d; --surface: #17202b; --surface-2: #1c2734; --border: #2a3646; --text: #e4eaf2; --muted: #92a0b4;
    --accent: #5b9bd5; --accent-2: #7db3e6; --accent-contrast: #0b1118; --danger: #ff7a6b; --ok: #4cc38a; --warn: #f0b429;
    --st-B: #4cc38a; --st-mB: #f0b429; --st-P: #5b9bd5; --st-oB: #ff7a6b;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  --bg: #0f151d; --surface: #17202b; --surface-2: #1c2734; --border: #2a3646; --text: #e4eaf2; --muted: #92a0b4;
  --accent: #5b9bd5; --accent-2: #7db3e6; --accent-contrast: #0b1118; --danger: #ff7a6b; --ok: #4cc38a; --warn: #f0b429;
  --st-B: #4cc38a; --st-mB: #f0b429; --st-P: #5b9bd5; --st-oB: #ff7a6b; --shadow: 0 1px 2px rgba(0,0,0,.4);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 20px; } h2 { font-size: 16px; } h3 { font-size: 14px; }
a { color: var(--accent-2); }
.muted { color: var(--muted); } .right { text-align: right; } .nowrap { white-space: nowrap; }
.pos { color: var(--ok); } .neg { color: var(--danger); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* Shell */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { background: var(--accent); color: #fff; padding: 16px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .side { background: #101a26; border-right: 1px solid var(--border); } }
:root[data-theme="dark"] .side { background: #101a26; border-right: 1px solid var(--border); }
.brand { font-weight: 700; font-size: 16px; padding: 6px 10px 14px; display: flex; align-items: center; gap: 8px; }
.brand svg { flex: none; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: rgba(255,255,255,.82); text-decoration: none; }
.nav a:hover { background: rgba(255,255,255,.1); color: #fff; }
.nav a.active { background: rgba(255,255,255,.18); color: #fff; font-weight: 600; }
.nav .sep { height: 1px; background: rgba(255,255,255,.15); margin: 8px 6px; }
.side .grow { flex: 1; }
.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; flex-wrap: wrap; }
.topbar .title { font-size: 18px; font-weight: 650; margin-right: auto; }
.content { padding: 20px; max-width: 1500px; width: 100%; margin: 0 auto; }
.menu-btn { display: none; }
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; z-index: 40; width: 240px; transform: translateX(-100%); transition: transform .2s; }
  .side.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .content { padding: 12px; }
}

/* Controls */
button, .btn { font: inherit; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 6px 12px; border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; }
button:hover, .btn:hover { background: var(--surface-2); border-color: var(--accent-2); }
button.primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.12); }
button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
button.ghost { border-color: transparent; background: transparent; }
button.sm { padding: 3px 8px; font-size: 12.5px; }
button:disabled { opacity: .5; cursor: not-allowed; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 7px; padding: 6px 9px; min-width: 0; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 1px; }
input[type=number] { text-align: right; }
textarea { width: 100%; min-height: 70px; resize: vertical; }
label.field { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; color: var(--muted); }
label.field > input, label.field > select, label.field > textarea { color: var(--text); font-size: 14px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; border-radius: 0; background: transparent; padding: 6px 12px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent); color: var(--accent-contrast); }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }

/* Cards / Grid */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card h2 { margin-bottom: 10px; display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.grid { display: grid; gap: 14px; }
.g-kpi { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 14px; }
.g-2 { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); margin-bottom: 14px; }
@media (max-width: 520px) { .g-2 { grid-template-columns: 1fr; } }
.kpi .label { color: var(--muted); font-size: 12.5px; }
.kpi .value { font-size: 24px; font-weight: 700; margin: 2px 0; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12.5px; color: var(--muted); }
.chart-box { position: relative; height: 290px; }
.bar { height: 7px; border-radius: 4px; background: var(--border); overflow: hidden; margin-top: 8px; }
.bar > span { display: block; height: 100%; background: var(--accent-2); }

/* Tabellen */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); max-height: calc(100vh - 220px); }
.table-wrap.auto { max-height: none; }
table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13.5px; }
th, td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 2; font-weight: 600; font-size: 12.5px; color: var(--muted); white-space: nowrap; user-select: none; }
th.sortable { cursor: pointer; } th.sortable:hover { color: var(--text); }
tbody tr:hover td { background: color-mix(in srgb, var(--accent-2) 7%, transparent); }
tfoot td { position: sticky; bottom: 0; background: var(--surface-2); font-weight: 650; border-top: 2px solid var(--border); }
tr.row-B td:first-child, tr.row-mB td:first-child, tr.row-P td:first-child, tr.row-oB td:first-child { box-shadow: inset 9px 0 0 var(--c, var(--border)); }
tr.row-B { --c: var(--st-B); } tr.row-mB { --c: var(--st-mB); } tr.row-P { --c: var(--st-P); } tr.row-oB { --c: var(--st-oB); }
tr.clickable { cursor: pointer; }
td.total, th.total { font-weight: 650; }
tr.subtotal td { background: var(--surface-2); font-weight: 650; }
td select { padding: 2px 4px; font-size: 13px; border-color: transparent; background: transparent; }
td select:hover { border-color: var(--border); background: var(--surface); }
.hint { font-size: 12.5px; color: var(--muted); }

/* Chips / Badges */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; color: #fff; background: var(--c, var(--st-oB)); }
.badge.B { --c: var(--st-B); } .badge.mB { --c: var(--st-mB); } .badge.P { --c: var(--st-P); } .badge.mB { color: #20190a; } .badge.oB { --c: var(--st-oB); }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 13px; }
.chip .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); }
.chip.on { border-color: var(--c, var(--accent-2)); background: color-mix(in srgb, var(--c, var(--accent-2)) 14%, var(--surface)); font-weight: 600; }
.chip.B { --c: var(--st-B); } .chip.mB { --c: var(--st-mB); } .chip.P { --c: var(--st-P); } .chip.oB { --c: var(--st-oB); }
.pill { display: inline-block; font-size: 11.5px; padding: 0 7px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }

/* Modal / Toast */
.overlay { position: fixed; inset: 0; background: rgba(10,20,35,.5); display: flex; align-items: flex-start; justify-content: center; padding: 4vh 12px; z-index: 100; overflow: auto; }
.modal { background: var(--surface); border-radius: 12px; width: min(760px, 100%); box-shadow: 0 20px 60px rgba(0,0,0,.35); display: flex; flex-direction: column; max-height: 92vh; }
.modal.wide { width: min(1000px, 100%); }
.modal header { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal .body { padding: 16px 18px; overflow: auto; }
.modal footer { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.form-grid .span2 { grid-column: span 2; } .form-grid .full { grid-column: 1 / -1; }
.calc-box { display: flex; gap: 18px; flex-wrap: wrap; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); margin-top: 12px; }
#toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: var(--accent); color: var(--accent-contrast); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; }
.toast.error { background: var(--danger); color: #fff; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: 14px; }
.login-card .brand { color: var(--accent); padding: 0; font-size: 20px; }
.error-text { color: var(--danger); font-size: 13px; min-height: 18px; }

.issue { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--border); }
.issue:last-child { border-bottom: 0; }
.issue .count { font-weight: 700; min-width: 28px; text-align: right; }
.diff-from { color: var(--danger); text-decoration: line-through; } .diff-to { color: var(--ok); font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
@media print {
  .side, .topbar, .toolbar, #toasts { display: none !important; }
  .shell { display: block; } .table-wrap { max-height: none; overflow: visible; }
  body { background: #fff; color: #000; } .card { box-shadow: none; }
}

/* Status-Auswahl in der Tabelle: Farbe je nach Status */
td select.st { font-weight: 700; border-radius: 6px; padding: 3px 6px; background: var(--c); color: #fff; border-color: var(--c); cursor: pointer; }
td select.st:hover { background: var(--c); filter: brightness(1.1); border-color: var(--c); }
td select.st.mB { color: #20190a; }
td select.st option { background: var(--surface); color: var(--text); font-weight: 400; }
td select.st.B { --c: var(--st-B); } td select.st.mB { --c: var(--st-mB); } td select.st.P { --c: var(--st-P); } td select.st.oB { --c: var(--st-oB); }
tr.row-B td:first-child, tr.row-mB td:first-child, tr.row-P td:first-child, tr.row-oB td:first-child { padding-left: 16px; }

/* Positionen: volle Breite, Gitterlinien, Zoom */
.content.wide { max-width: none; }
.items-box { --zoom: 1; font-size: calc(13.5px * var(--zoom)); }
.items-box table { font-size: inherit; }
.items-box th, .items-box td { padding: .4em .7em; }
.items-box thead th { font-size: .92em; }
.items-box td select, .items-box td select.st { font-size: inherit; }
.items-box .pill, .items-box .badge { font-size: .88em; }
.grid-table th, .grid-table td { border-right: 1px solid color-mix(in srgb, var(--border) 85%, transparent); }
.grid-table th:last-child, .grid-table td:last-child { border-right: 0; }
.grid-table td, .grid-table th { border-bottom: 1px solid color-mix(in srgb, var(--text) 24%, var(--border)); }
.zoom-bar { position: fixed; right: 16px; bottom: 12px; z-index: 30; display: flex; align-items: center; gap: 6px; padding: 4px 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; box-shadow: var(--shadow); font-size: 12.5px; }
.zoom-bar button { padding: 0 8px; border-radius: 50%; line-height: 1.5; }
.zoom-bar input[type=range] { width: 140px; padding: 0; border: 0; background: transparent; accent-color: var(--accent-2); }
.zoom-bar .val { min-width: 3.2em; text-align: right; cursor: pointer; font-variant-numeric: tabular-nums; }
.items-box.table-wrap { max-height: calc(100vh - 200px); }

td.dev { text-decoration: underline dotted var(--st-mB); text-decoration-thickness: 2px; text-underline-offset: 3px; cursor: help; }

/* Kalkulation */
.kalk-grid { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 14px; align-items: start; }
.kalk-result { position: sticky; top: 70px; max-height: calc(100vh - 90px); overflow: auto; }
@media (max-width: 1100px) { .kalk-grid { grid-template-columns: 1fr; } .kalk-result { position: static; max-height: none; } }
tr.key td { background: color-mix(in srgb, var(--accent-2) 14%, var(--surface)); }
@media print { .kalk-grid { display: block; } .kalk-result { position: static; max-height: none; } }

.lines-table { table-layout: fixed; }
.lines-table th, .lines-table td { padding: 5px 6px; }
.lines-table input:not([type=checkbox]) { width: 100%; }
.lines-table th.num-h { padding-right: 16px; }
.lines-table td.num { padding-right: 10px; }
.center { text-align: center; }

.entity-seg button.on { background: var(--accent-2); }
.entity-seg { border-color: var(--accent-2); }
