:root {
  --bg: #f6f3ec;
  --panel: #ffffff;
  --ink: #2b2622;
  --muted: #6f675f;
  --line: #e2dccf;
  --green: #3f5a3a;
  --green-2: #56784f;
  --green-soft: #e6ede1;
  --amber: #a8641a;
  --amber-soft: #fbefd9;
  --red: #a33a2c;
  --red-soft: #f8e1dc;
  --blue-soft: #e3ebf2;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(43, 38, 34, .06), 0 2px 8px rgba(43, 38, 34, .05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { font-size: 16px; line-height: 1.45; -webkit-text-size-adjust: 100%; }
a { color: var(--green); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1rem 0 .5rem; }
.loading { padding: 2rem; color: var(--muted); }

/* Layout */
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--green); color: #f6f3ec; padding: 1rem .75rem; position: sticky; top: 0; height: 100vh; overflow: auto; }
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.1rem; padding: .25rem .5rem 1rem; }
.brand img { width: 30px; height: 30px; border-radius: 7px; }
.brand small { display: block; font-weight: 400; font-size: .75rem; opacity: .8; }
.nav a { display: flex; align-items: center; gap: .6rem; padding: .55rem .7rem; border-radius: 8px; color: #f6f3ec; text-decoration: none; font-size: .95rem; }
.nav a:hover { background: rgba(255, 255, 255, .08); }
.nav a.active { background: rgba(255, 255, 255, .16); font-weight: 600; }
.nav .ico { width: 1.3em; text-align: center; }
.nav .sep { height: 1px; background: rgba(255, 255, 255, .15); margin: .6rem .5rem; }
.main { padding: 1.5rem 2rem 4rem; max-width: 1200px; width: 100%; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.page-head p { margin: 0; color: var(--muted); }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* Mobile */
.topbar, .tabbar, .drawer { display: none; }
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .topbar { display: flex; align-items: center; justify-content: space-between; background: var(--green); color: #f6f3ec; padding: .6rem 1rem; position: sticky; top: 0; z-index: 20; }
  .topbar .brand { padding: 0; font-size: 1rem; }
  .topbar button { background: none; border: 0; color: inherit; font-size: 1.4rem; padding: .25rem .5rem; }
  .tabbar { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; bottom: 0; left: 0; right: 0; background: #fff; border-top: 1px solid var(--line); z-index: 20; padding-bottom: env(safe-area-inset-bottom); }
  .tabbar a { display: flex; flex-direction: column; align-items: center; padding: .45rem 0 .5rem; font-size: .7rem; color: var(--muted); text-decoration: none; }
  .tabbar a .ico { font-size: 1.25rem; }
  .tabbar a.active { color: var(--green); font-weight: 600; }
  .main { padding: 1rem 1rem 6rem; }
  .drawer { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 30; display: none; }
  .drawer.open { display: block; }
  .drawer .sidebar { display: block; width: 260px; height: 100%; }
  .hide-sm { display: none !important; }
}

/* Panels */
.grid { display: grid; gap: 1rem; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.card { min-width: 0; }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.card.flush { padding: 0; overflow: hidden; }
.card > h2 { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.stat .num { font-size: 2rem; font-weight: 700; line-height: 1.1; }
.stat .lbl { color: var(--muted); font-size: .85rem; }
.stat.warn { background: var(--amber-soft); border-color: #efd5a8; }
.stat.alert { background: var(--red-soft); border-color: #ecc0b7; }
.empty { color: var(--muted); font-size: .9rem; padding: .5rem 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* Lists & tables */
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: .55rem .7rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: #faf8f3; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #faf8f3; }
tr.cancelled td { color: var(--muted); text-decoration: line-through; }
tr.cancelled td.keep { text-decoration: none; }
.table-wrap { overflow-x: auto; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; align-items: center; gap: .75rem; padding: .55rem 0; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.tag { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .9em; }

.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 600; background: var(--green-soft); color: var(--green); white-space: nowrap; }
.badge.warn { background: var(--amber-soft); color: var(--amber); }
.badge.alert { background: var(--red-soft); color: var(--red); }
.badge.grey { background: #ece8df; color: var(--muted); }
.badge.blue { background: var(--blue-soft); color: #2f5674; }

/* Forms */
form .row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .75rem 1rem; margin-bottom: .75rem; }
label { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); }
label .req { color: var(--red); }
input, select, textarea { width: 100%; margin-top: .25rem; font: inherit; color: var(--ink); padding: .6rem .7rem; border: 1px solid #cfc7b8; border-radius: 8px; background: #fff; min-height: 44px; }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--green-2); outline-offset: 0; border-color: var(--green-2); }
input[type=checkbox] { width: 1.15rem; height: 1.15rem; min-height: 0; margin: 0; accent-color: var(--green); }
.hint { font-weight: 400; font-size: .8rem; color: var(--muted); margin-top: .2rem; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .75rem 1rem 1rem; margin: 0 0 1rem; }
legend { font-weight: 700; padding: 0 .4rem; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; font: inherit; font-weight: 600; font-size: .92rem; padding: .55rem 1rem; min-height: 42px; border-radius: 8px; border: 1px solid var(--green); background: var(--green); color: #fff; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--green-2); }
.btn.secondary { background: #fff; color: var(--green); }
.btn.secondary:hover { background: var(--green-soft); }
.btn.danger { background: #fff; color: var(--red); border-color: #d9a49b; }
.btn.danger:hover { background: var(--red-soft); }
.btn.small { padding: .3rem .65rem; min-height: 32px; font-size: .82rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: 1rem; }

.alert-box { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: .92rem; }
.alert-box.warn { background: var(--amber-soft); color: #6d420f; }
.alert-box.alert { background: var(--red-soft); color: #6f2419; }
.alert-box.info { background: var(--blue-soft); color: #23435c; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1rem; }
.filters input, .filters select { width: auto; flex: 1 1 160px; margin: 0; }
.filters input[type=search] { flex: 3 1 240px; }

/* Tier-Auswahl */
.picker { border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
.picker .picker-head { display: flex; gap: .5rem; flex-wrap: wrap; padding: .6rem; border-bottom: 1px solid var(--line); background: #faf8f3; border-radius: var(--radius) var(--radius) 0 0; }
.picker .picker-head input { margin: 0; flex: 2 1 180px; }
.picker .picker-head select { margin: 0; flex: 1 1 140px; width: auto; }
.picker .picker-list { max-height: 320px; overflow: auto; }
.picker label.pick { display: flex; align-items: center; gap: .7rem; padding: .5rem .8rem; border-bottom: 1px solid #f0ece3; font-weight: 400; color: var(--ink); cursor: pointer; font-size: .92rem; }
.picker label.pick:hover { background: #faf8f3; }
.picker .picker-foot { padding: .5rem .8rem; font-size: .85rem; color: var(--muted); border-top: 1px solid var(--line); }

/* Detailseite */
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .6rem 1.25rem; margin: 0; }
.kv div dt { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.kv div dd { margin: 0; font-weight: 500; }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); margin: 1.25rem 0 1rem; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: .6rem .9rem; font: inherit; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; }
.tabs button.active { color: var(--green); border-bottom-color: var(--green); }

.lamb-row { display: grid; grid-template-columns: 110px 1fr 1fr 110px 40px; gap: .5rem; align-items: end; margin-bottom: .5rem; }
.entry-row { display: grid; grid-template-columns: 1.4fr 1fr 110px 140px 1fr 40px; gap: .5rem; align-items: end; margin-bottom: .5rem; }
@media (max-width: 820px) { .lamb-row, .entry-row { grid-template-columns: 1fr 1fr; padding: .5rem; border: 1px solid var(--line); border-radius: 8px; } }

/* Login */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth .card { width: 100%; max-width: 400px; padding: 1.75rem; }
.auth .brand { color: var(--green); padding: 0 0 1rem; }

#toast { visibility: hidden; opacity: 0; position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%) translateY(150%); background: var(--ink); color: #fff; padding: .7rem 1.1rem; border-radius: 8px; font-size: .92rem; transition: transform .2s, opacity .2s; z-index: 50; max-width: 90vw; }
#toast.show { transform: translateX(-50%) translateY(0); visibility: visible; opacity: 1; }
#toast.error { background: var(--red); }
@media (max-width: 820px) { #toast { bottom: 5rem; } }

dialog { border: 0; border-radius: 12px; padding: 1.25rem 1.4rem; max-width: 520px; width: calc(100% - 2rem); box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
dialog::backdrop { background: rgba(0, 0, 0, .35); }

/* Druck */
.print-only { display: none; }
.print-doc { background: #fff; padding: 1.5rem; border: 1px solid var(--line); border-radius: var(--radius); }
.print-doc table { font-size: .8rem; }
.print-doc th, .print-doc td { padding: .3rem .4rem; border: 1px solid #bbb; }
.print-doc .doc-head { display: flex; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
@media print {
  @page { size: A4 landscape; margin: 12mm; }
  body { background: #fff; font-size: 11px; }
  .sidebar, .topbar, .tabbar, .no-print, #toast { display: none !important; }
  .layout { display: block; }
  .main { padding: 0; max-width: none; }
  .print-doc { border: 0; padding: 0; }
  .print-only { display: block; }
  tr { break-inside: avoid; }
  .table-wrap { overflow: visible; }
}

/* Zwei-Faktor */
.steps { padding-left: 1.2rem; margin: 1rem 0 0; }
.steps > li { margin-bottom: 1rem; }
.qr { width: 200px; max-width: 70vw; margin: .75rem 0; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.qr svg { display: block; width: 100%; height: auto; }
.recovery ol { columns: 2; font-size: 1.05rem; background: #faf8f3; border: 1px dashed #cfc7b8; border-radius: 8px; padding: .75rem .75rem .75rem 2.2rem; margin: .75rem 0 0; }
.recovery li { padding: .15rem 0; }
@media print { .auth { min-height: 0; display: block; } .auth .card { box-shadow: none; border: 0; } .alert-box, #saved, #goOn, label:has(#saved) { display: none !important; } }

/* Betriebe */
.tenant-switch { margin: 0 0 .75rem; background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.25); min-height: 38px; padding: .35rem .5rem; font-size: .9rem; }
.tenant-switch option { color: var(--ink); }
.who { font-size: .85rem; padding: .25rem .7rem .5rem; opacity: .9; }
.who span { font-size: .75rem; opacity: .75; }
.linkbox { display: flex; gap: .5rem; margin: .5rem 0; }
.linkbox input { margin: 0; font-size: .8rem; font-family: ui-monospace, monospace; }
td.actions { white-space: nowrap; }

/* Nur-Lesen: Eingabe-Schaltflächen ausblenden (der Server sperrt Eingaben ohnehin) */
.readonly .page-head .actions, .readonly #bWeight, .readonly #bTag, .readonly #bUndo, .readonly [data-cancel], .readonly [data-del], .readonly [data-delw],
.readonly [data-rep], .readonly [data-unrep], .readonly [data-report], .readonly .nav a[href="#/import"], .readonly .tabbar a[href="#/behandlung/neu"],
.readonly .btn-row:has(a[href^="#/abgang"]) { display: none !important; }
