/* ------------------------------------------------------------ Farben
   Hell ist Standard. Dunkel greift, wenn das System dunkel ist (und der
   Nutzer nicht "hell" gewählt hat) oder wenn "dunkel" gewählt wurde. */
:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #1f2933;
  --muted: #6b7280;
  --label: #374151;
  --line: #e2e6ec;
  --line-soft: #eef1f5;
  --input-bg: #ffffff;
  --input-border: #cdd3dc;
  --btn-bg: #ffffff;
  --btn-border: #c9d0da;
  --hover: #f4f6f9;
  --hover-soft: #f5f8fc;
  --subtle: #eef1f5;
  --thead: #f8fafc;
  --stripe: #f8fafc;
  --readonly: #f3f5f8;
  --badge-bg: #e9edf2;
  --badge-text: #475569;
  --info-bg: #eef4fb;
  --info-text: #475569;
  --item-active: #e8f0f9;
  --actionbar: rgba(243, 245, 248, .94);
  --primary: #1f4e79;
  --primary-hover: #183e61;
  --link: #1f4e79;
  --topbar: #1f4e79;
  --disabled: #94a3b8;
  --focus: rgba(31, 78, 121, .18);
  --ok: #15803d;
  --ok-bg: #e8f6ee;
  --ok-border: #b7e2c6;
  --ok-text: #14532d;
  --err: #b91c1c;
  --err-bg: #fdecec;
  --err-border: #f3b9b9;
  --err-text: #7f1d1d;
  --danger-border: #f1b5b5;
  --warn: #92400e;
  --warn-bg: #fef3c7;
  --overlay: rgba(15, 23, 42, .45);
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 10px 28px rgba(15, 23, 42, .18);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f141a; --card: #171e27; --text: #e5e9ef; --muted: #95a2b3; --label: #c9d1db;
    --line: #2a3441; --line-soft: #222b36; --input-bg: #10161d; --input-border: #3a4656;
    --btn-bg: #1d2632; --btn-border: #3a4656; --hover: #253041; --hover-soft: #1c2531; --subtle: #253041;
    --thead: #1b232e; --stripe: #1a212b; --readonly: #1a222c; --badge-bg: #263140; --badge-text: #b6c2d0;
    --info-bg: #182a3e; --info-text: #b9cbe0; --item-active: #22364d; --actionbar: rgba(15, 20, 26, .92);
    --primary: #2f6aa3; --primary-hover: #3a7bb8; --link: #8cbcf0; --topbar: #14263a; --disabled: #4b5563;
    --focus: rgba(96, 165, 250, .3);
    --ok: #4ade80; --ok-bg: #12301f; --ok-border: #1f5a3a; --ok-text: #bbf7d0;
    --err: #f87171; --err-bg: #3a1717; --err-border: #6b2525; --err-text: #fecaca; --danger-border: #6b2c2c;
    --warn: #fbbf24; --warn-bg: #3a2c0c; --overlay: rgba(0, 0, 0, .6);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4); --shadow-lg: 0 12px 32px rgba(0, 0, 0, .55);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f141a; --card: #171e27; --text: #e5e9ef; --muted: #95a2b3; --label: #c9d1db;
  --line: #2a3441; --line-soft: #222b36; --input-bg: #10161d; --input-border: #3a4656;
  --btn-bg: #1d2632; --btn-border: #3a4656; --hover: #253041; --hover-soft: #1c2531; --subtle: #253041;
  --thead: #1b232e; --stripe: #1a212b; --readonly: #1a222c; --badge-bg: #263140; --badge-text: #b6c2d0;
  --info-bg: #182a3e; --info-text: #b9cbe0; --item-active: #22364d; --actionbar: rgba(15, 20, 26, .92);
  --primary: #2f6aa3; --primary-hover: #3a7bb8; --link: #8cbcf0; --topbar: #14263a; --disabled: #4b5563;
  --focus: rgba(96, 165, 250, .3);
  --ok: #4ade80; --ok-bg: #12301f; --ok-border: #1f5a3a; --ok-text: #bbf7d0;
  --err: #f87171; --err-bg: #3a1717; --err-border: #6b2525; --err-text: #fecaca; --danger-border: #6b2c2c;
  --warn: #fbbf24; --warn-bg: #3a2c0c; --overlay: rgba(0, 0, 0, .6);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4); --shadow-lg: 0 12px 32px rgba(0, 0, 0, .55);
}

/* ------------------------------------------------------------ Grundlagen */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
form, section { scroll-margin-top: 76px; }
html { -webkit-text-size-adjust: 100%; background: var(--bg); }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
a { color: var(--link); }
h1 { font-size: 1.55rem; margin: 0 0 .2rem; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 0 0 .8rem; }
code { background: var(--subtle); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.wrap { max-width: 1180px; margin: 0 auto; padding: 0 16px; }
main.wrap { padding-top: 22px; padding-bottom: 60px; }
main.narrow { max-width: 440px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-line; }
.right { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Topbar */
.topbar { background: var(--topbar); color: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.15); position: sticky; top: 0; z-index: 20; }
.topbar-in { display: flex; align-items: center; gap: 18px; min-height: 56px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; padding: 4px 0; }
.brand img { display: block; height: 34px; width: auto; }
.mainnav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.mainnav a { color: #dbe7f3; text-decoration: none; padding: 7px 12px; border-radius: 6px; font-weight: 500; }
.mainnav a:hover { background: rgba(255,255,255,.1); color: #fff; }
.mainnav a.active { background: rgba(255,255,255,.18); color: #fff; }
.usermenu { display: flex; align-items: center; gap: 12px; }
.usermenu form { margin: 0; }
.username { color: #fff; text-decoration: none; font-weight: 500; }
.topbar .linkbtn { color: #dbe7f3; }
.themebtn { color: #dbe7f3; }
.themebtn:hover { background: rgba(255,255,255,.12); color: #fff; }
.themebtn svg { display: none; }
:root:not([data-theme]) .themebtn .ic-auto,
:root[data-theme="light"] .themebtn .ic-light,
:root[data-theme="dark"] .themebtn .ic-dark { display: block; }
.floating-theme { position: fixed; top: 12px; right: 12px; color: var(--muted); }
.floating-theme:hover { background: var(--subtle); color: var(--text); }

/* Cards & layout */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 18px; }
.card.flush { padding: 0; overflow: hidden; }
.card .pad, .pad { padding: 16px 20px; margin: 0; }
.card.flush h2.pad { padding-bottom: 0; margin-bottom: 10px; }
.cardhead { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: .8rem; }
.cardhead h2 { margin: 0; }
.card.flush .cardhead.pad { margin: 0; }
.pagehead { display: flex; justify-content: space-between; align-items: flex-end; gap: 14px; flex-wrap: wrap; margin-bottom: 18px; }
.pagehead p { margin: .2rem 0 0; }
.back { display: inline-block; font-size: .9rem; margin-bottom: 4px; text-decoration: none; }
.cols { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 18px; align-items: start; }
.cols > .card { margin-bottom: 0; }
.cols + .card, .cols + section { margin-top: 18px; }
.col-main, .col-side { min-width: 0; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty.small { padding: 18px; }

/* Forms */
label { display: flex; flex-direction: column; gap: 5px; font-weight: 600; font-size: .88rem; color: var(--label); }
label small { font-weight: 400; color: var(--muted); }
input, select, textarea {
  font: inherit; font-weight: 400; color: var(--text); background: var(--input-bg);
  border: 1px solid var(--input-border); border-radius: 6px; padding: 8px 10px; width: 100%; min-height: 40px;
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .8; }
textarea { resize: vertical; min-height: 64px; }
input.readonly { background: var(--readonly); color: var(--muted); cursor: default; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus); }
input[type=checkbox] { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); margin: 0; flex: none; }
input[type=file] { padding: 6px; }
label.chk { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.align-end { align-self: end; min-height: 40px; }
.chkgroup { display: flex; gap: 18px; align-items: end; padding-bottom: 9px; }
.grid { display: grid; gap: 14px 16px; }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
.span3 { grid-column: span 3; }
.span4 { grid-column: 1 / -1; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.inline { display: flex; gap: 10px; flex-wrap: wrap; }
.inline input { max-width: 340px; }
.filterbar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.filterbar input[type=search] { flex: 1; min-width: 220px; }
.filterbar select { width: auto; }
.tablefilter { max-width: 240px; min-height: 34px; padding: 5px 9px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: .92rem; text-decoration: none; cursor: pointer;
  background: var(--btn-bg); color: var(--text); border: 1px solid var(--btn-border); border-radius: 6px;
  padding: 8px 14px; min-height: 40px; white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.primary:disabled { background: var(--disabled); border-color: var(--disabled); cursor: not-allowed; }
.btn.danger { color: var(--err); border-color: var(--danger-border); }
.btn.danger:hover { background: var(--err-bg); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: .85rem; }
.btn.block { width: 100%; }
.btnrow { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 14px; }
.pagehead .btnrow { margin-top: 0; }
.btnrow form { margin: 0; }
.linkbtn { background: none; border: 0; padding: 0; font: inherit; color: var(--link); cursor: pointer; text-decoration: underline; }
.iconbtn {
  background: none; border: 1px solid transparent; border-radius: 6px; cursor: pointer; color: var(--muted);
  width: 32px; height: 32px; font-size: 15px; line-height: 1; display: inline-flex; align-items: center; justify-content: center;
}
.iconbtn:hover { background: var(--subtle); color: var(--text); }
.iconbtn.danger:hover { background: var(--err-bg); color: var(--err); }
.danger-text { color: var(--err); font-size: .85rem; }
.rowbtns { white-space: nowrap; text-align: right; }
.rowbtns form { display: inline; margin: 0; }
.actionbar {
  position: sticky; bottom: 0; z-index: 10; display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap;
  background: var(--actionbar); backdrop-filter: blur(4px); padding: 12px 0; border-top: 1px solid var(--line); margin-top: 4px;
}

/* Flash & badges */
.flash { padding: 11px 14px; border-radius: 6px; margin-bottom: 12px; border: 1px solid; }
.flash a { color: inherit; }
.flash-ok { background: var(--ok-bg); border-color: var(--ok-border); color: var(--ok-text); }
.flash-error { background: var(--err-bg); border-color: var(--err-border); color: var(--err-text); }
.badge { display: inline-block; font-size: .75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--badge-bg); color: var(--badge-text); vertical-align: middle; white-space: nowrap; }
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-err { background: var(--err-bg); color: var(--err); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
h1 .badge { font-size: .8rem; margin-left: 6px; }

/* Tables */
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
table.list th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--thead); }
table.list td { padding: 10px 14px; border-bottom: 1px solid var(--line-soft); vertical-align: top; }
table.list th.num { text-align: right; }
table.list tbody tr:last-child td { border-bottom: 0; }
tr.rowlink { cursor: pointer; }
tr.rowlink:hover td { background: var(--hover-soft); }
tr.inactive td { color: var(--muted); }

/* Positions */
.postable { margin-bottom: 12px; }
.postable th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding: 4px 6px 6px; }
.postable td { padding: 4px 6px; vertical-align: middle; }
.postable .c-pos { width: 44px; }
.postable .c-qty { width: 110px; }
.postable .c-unit { width: 120px; }
.postable .c-act { width: 108px; }
.postable .posnr { color: var(--muted); font-weight: 600; text-align: center; }
.postable input.qty { text-align: right; }
.postable tr:first-child .up, .postable tr:last-child .down { visibility: hidden; }

/* Datumsfelder */
.field { display: flex; flex-direction: column; gap: 5px; }
.dateinput { position: relative; }
.dateinput .datetext { padding-right: 42px; }
.dateinput .datetext.invalid { border-color: var(--err); box-shadow: 0 0 0 3px var(--err-bg); }
.dateinput .datepickwrap {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 34px; height: 34px; overflow: hidden; border-radius: 6px;
}
.dateinput .datepick { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.dateinput .datepickwrap:hover .datepick { background: var(--subtle); color: var(--text); }
.dateinput .datehidden {
  position: absolute; inset: 0; width: 100%; height: 100%; min-width: 0; min-height: 0; max-width: none;
  margin: 0; padding: 0; border: 0; opacity: 0; cursor: pointer; z-index: 1;
  -webkit-appearance: none; appearance: none; font-size: 16px; /* iOS: kein Zoom beim Antippen */
}
.dateinput .datehidden::-webkit-calendar-picker-indicator { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; padding: 0; cursor: pointer; }

/* Vorschlagsliste */
.suggest {
  position: absolute; z-index: 40; max-width: min(560px, calc(100vw - 16px)); max-height: 300px; overflow-y: auto;
  background: var(--card); color: var(--text); border: 1px solid var(--btn-border); border-radius: 8px; box-shadow: var(--shadow-lg); padding: 4px;
}
.sg-item { display: flex; flex-direction: column; gap: 1px; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.sg-item.active { background: var(--item-active); }
.sg-name { font-weight: 500; }
.sg-meta { font-size: .8rem; color: var(--muted); }

/* Attachments, info */
.attlist { list-style: none; padding: 0; margin: 0 0 12px; }
.attlist li { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.attlist li:last-child { border-bottom: 0; }
.attlist li::before { content: "📎"; }
dl.info { display: grid; grid-template-columns: 150px 1fr; gap: 8px 14px; margin: 0; }
dl.info dt { font-weight: 600; color: var(--label); }
dl.info dd { margin: 0; }

/* Versandprotokoll */
.batch + .batch { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.batchhead { font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.sendlist { list-style: none; margin: 0; padding: 0; }
.sendlist li { display: flex; gap: 10px; padding: 5px 0; }
.sendlist .dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 6px; flex: none; background: var(--ok); }
.sendlist li.err .dot { background: var(--err); }
.errtext { color: var(--err); font-size: .85rem; }
/* Die Mail-Vorschau zeigt die echte Mail – bleibt bewusst hell */
.mailpreview { width: 100%; height: 640px; border: 1px solid var(--line); border-radius: 6px; background: #f1f3f6; color-scheme: light; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; overflow-x: auto; }
.tabs a { padding: 9px 14px; text-decoration: none; color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a:hover { color: var(--text); }
.tabs a.active { color: var(--link); border-bottom-color: var(--link); }

/* Dialog */
.dialog { border: 1px solid var(--line); border-radius: 12px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 40px); box-shadow: var(--shadow-lg); color: var(--text); background: var(--card); }
.dialog[open] { display: flex; flex-direction: column; }
.dialog::backdrop { background: var(--overlay); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px 6px; }
.dlg-head h2 { margin: 0; }
.dlg-info { margin: 0 20px 12px; font-size: .9rem; color: var(--info-text); background: var(--info-bg); border-radius: 6px; padding: 9px 12px; }
.dialog .flash { margin: 0 20px 12px; }
.dlg-tools { display: flex; gap: 12px; align-items: center; padding: 0 20px 10px; }
.dlg-tools input { min-height: 36px; }
.suplist { overflow-y: auto; padding: 0 12px; flex: 1; min-height: 80px; }
.supitem { flex-direction: row; align-items: center; gap: 12px; padding: 9px 8px; border-radius: 6px; cursor: pointer; font-weight: 400; color: var(--text); border-bottom: 1px solid var(--line-soft); }
.supitem:hover { background: var(--hover-soft); }
.supitem .supmain { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.supitem small { color: var(--muted); font-size: .82rem; overflow: hidden; text-overflow: ellipsis; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--line); }

/* Auth */
.authbox { margin-top: 40px; padding: 28px; }
.authlogo { margin: 4px 0 22px; }
.authlogo img { display: block; width: 100%; max-width: 300px; height: auto; margin: 0 auto; }
.logo-on-dark { display: none !important; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-on-light { display: none !important; }
  :root:not([data-theme="light"]) .logo-on-dark { display: block !important; }
}
:root[data-theme="dark"] .logo-on-light { display: none !important; }
:root[data-theme="dark"] .logo-on-dark { display: block !important; }

@media (max-width: 900px) {
  .cols { grid-template-columns: 1fr; }
  .cols > .card { margin-bottom: 0; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .span3 { grid-column: 1 / -1; }
}
@media (max-width: 600px) {
  body { font-size: 14.5px; }
  .g3, .g4 { grid-template-columns: 1fr; }
  .span2, .span3 { grid-column: auto; }
  .topbar-in { gap: 8px; padding: 6px 0; }
  .mainnav { order: 3; flex-basis: 100%; }
  .usermenu { margin-left: auto; }
  .card { padding: 16px; }
  dl.info { grid-template-columns: 1fr; gap: 2px; }
  dl.info dd { margin-bottom: 8px; }
  .topbar { position: static; }
  .postable thead { display: none; }
  .postable tbody tr { display: grid; grid-template-columns: 1fr 1fr 36px; gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--line); }
  .postable td { padding: 0; }
  .postable td.posnr { display: none; }
  .postable tr td:nth-child(4) { grid-column: 1 / 3; grid-row: 2; }
  .postable td.rowbtns { grid-column: 3; grid-row: 1 / 3; align-self: center; }
  .postable .up, .postable .down { display: none; }
  .actionbar { justify-content: stretch; }
  .actionbar .btn { flex: 1; }
}
