:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #111827;
  --muted: #5b6575;
  --border: #dfe3ea;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --primary-soft: #e0e9ff;
  --on-primary: #ffffff;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --warn-soft: #fef3c7;
  --warn: #92400e;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --radius: 10px;

  --u-notfall: #b91c1c;
  --u-notfall-bg: #fee2e2;
  --u-hoch: #c2410c;
  --u-hoch-bg: #ffedd5;
  --u-normal: #1d4ed8;
  --u-normal-bg: #dbeafe;
  --u-niedrig: #4b5563;
  --u-niedrig-bg: #eceff3;

  --s-eingegangen: #1d4ed8;
  --s-eingegangen-bg: #dbeafe;
  --s-bearbeitet: #92400e;
  --s-bearbeitet-bg: #fef3c7;
  --s-abgerechnet: #6d28d9;
  --s-abgerechnet-bg: #ede9fe;
  --s-abgeschlossen: #15803d;
  --s-abgeschlossen-bg: #dcfce7;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e131b;
    --surface: #171d27;
    --surface-2: #1c2330;
    --text: #e8ecf2;
    --muted: #9aa5b5;
    --border: #2a3342;
    --primary: #6b9bff;
    --primary-hover: #8ab0ff;
    --primary-soft: #1d2a47;
    --on-primary: #0b1220;
    --danger: #ff8a8a;
    --danger-soft: #3a1a1d;
    --ok: #5fd68a;
    --ok-soft: #14301f;
    --warn: #f5c26b;
    --warn-soft: #36290f;
    --shadow: none;

    --u-notfall: #ff8a8a;
    --u-notfall-bg: #3a1a1d;
    --u-hoch: #ffb27a;
    --u-hoch-bg: #38220f;
    --u-normal: #8ab0ff;
    --u-normal-bg: #1b2a49;
    --u-niedrig: #aab3c2;
    --u-niedrig-bg: #252d3a;

    --s-eingegangen: #8ab0ff;
    --s-eingegangen-bg: #1b2a49;
    --s-bearbeitet: #f5c26b;
    --s-bearbeitet-bg: #36290f;
    --s-abgerechnet: #c4a8ff;
    --s-abgerechnet-bg: #2a2147;
    --s-abgeschlossen: #5fd68a;
    --s-abgeschlossen-bg: #14301f;
  }
}

* {
  box-sizing: border-box;
}
html {
  -webkit-text-size-adjust: 100%;
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}
.boot {
  padding: 40px;
  color: var(--muted);
}
h1,
h2,
h3 {
  line-height: 1.25;
  margin: 0;
}
h1 {
  font-size: 1.5rem;
}
h2 {
  font-size: 1.1rem;
}
h3 {
  font-size: 0.95rem;
}
a {
  color: var(--primary);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
p {
  margin: 0 0 0.6em;
}
.muted {
  color: var(--muted);
}
.small {
  font-size: 0.85rem;
}
.right {
  text-align: right;
}
.nowrap {
  white-space: nowrap;
}
.pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.hidden {
  display: none !important;
}
:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ---------- Layout ---------- */
.shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
}
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 16px;
  font-weight: 700;
  font-size: 1.05rem;
}
.brand img {
  width: 28px;
  height: 28px;
}
.nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
  font-weight: 500;
}
.nav a:hover {
  background: var(--surface-2);
  text-decoration: none;
}
.nav a.active {
  background: var(--primary-soft);
  color: var(--primary);
}
.nav svg,
.icon {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.nav .count {
  margin-left: auto;
  background: var(--danger);
  color: #fff;
  border-radius: 999px;
  font-size: 0.72rem;
  padding: 1px 7px;
  font-weight: 700;
}
.sidebar .spacer {
  flex: 1;
}
.userbox {
  border-top: 1px solid var(--border);
  padding: 12px 10px 4px;
  font-size: 0.85rem;
}
.userbox .name {
  font-weight: 600;
}
.main {
  padding: 24px 28px 48px;
  max-width: 1200px;
  width: 100%;
  min-width: 0;
}
.page-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.page-head h1 {
  flex: 1;
  min-width: 160px;
}
.back {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 8px;
  font-size: 0.9rem;
}

/* ---------- Karten, Raster ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card + .card {
  margin-top: 16px;
}
.card > h2 {
  margin-bottom: 12px;
}
.grid {
  display: grid;
  gap: 16px;
}
.grid.cols-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.grid.cols-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
.grid.cols-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}
.detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
  gap: 16px;
  align-items: start;
}
.detail-layout .card + .card {
  margin-top: 0;
}
.stack {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.row.between {
  justify-content: space-between;
}

.main > .grid {
  margin-bottom: 16px;
}
.main > .notice {
  margin-bottom: 16px;
}
.stat {
  display: block;
  color: inherit;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.stat:hover {
  text-decoration: none;
  border-color: var(--primary);
}
.stat .n {
  font-size: 1.9rem;
  font-weight: 700;
  line-height: 1.1;
}
.stat .l {
  color: var(--muted);
  font-size: 0.85rem;
}

/* ---------- Formulare ---------- */
label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}
input,
select,
textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  min-height: 40px;
}
textarea {
  resize: vertical;
  min-height: 88px;
}
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  border-color: var(--primary);
}
input[type='checkbox'] {
  width: auto;
  min-height: 0;
}
.field {
  min-width: 0;
}
.form-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 12px 14px;
}
.form-grid .s2 {
  grid-column: span 2;
}
.form-grid .s3 {
  grid-column: span 3;
}
.form-grid .s4 {
  grid-column: span 4;
}
.form-grid .s6 {
  grid-column: span 6;
}
.form-grid h3 {
  grid-column: span 6;
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.75rem;
}
.form-grid h3:first-child {
  border-top: 0;
  padding-top: 0;
  margin-top: 0;
}
.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 3px;
}
.actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  padding: 9px 14px;
  min-height: 40px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
}
.btn:hover {
  background: var(--surface-2);
  text-decoration: none;
}
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}
.btn.primary:hover {
  background: var(--primary-hover);
}
.btn.danger {
  color: var(--danger);
  border-color: var(--danger);
}
.btn.small {
  min-height: 32px;
  padding: 4px 10px;
  font-size: 0.85rem;
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.u-notfall {
  color: var(--u-notfall);
  background: var(--u-notfall-bg);
}
.u-hoch {
  color: var(--u-hoch);
  background: var(--u-hoch-bg);
}
.u-normal {
  color: var(--u-normal);
  background: var(--u-normal-bg);
}
.u-niedrig {
  color: var(--u-niedrig);
  background: var(--u-niedrig-bg);
}
.s-eingegangen {
  color: var(--s-eingegangen);
  background: var(--s-eingegangen-bg);
}
.s-bearbeitet {
  color: var(--s-bearbeitet);
  background: var(--s-bearbeitet-bg);
}
.s-abgerechnet {
  color: var(--s-abgerechnet);
  background: var(--s-abgerechnet-bg);
}
.s-abgeschlossen {
  color: var(--s-abgeschlossen);
  background: var(--s-abgeschlossen-bg);
}
.badge.flag {
  color: var(--warn);
  background: var(--warn-soft);
}
.badge.neutral {
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

/* ---------- Tabellen / Listen ---------- */
.filters {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: end;
  margin-bottom: 14px;
}
.filters .field {
  min-width: 140px;
}
.filters .grow {
  flex: 1;
  min-width: 200px;
}
.chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 5px 13px;
  font: inherit;
  font-size: 0.88rem;
  cursor: pointer;
}
.chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}
table {
  width: 100%;
  border-collapse: collapse;
}
th {
  text-align: left;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
tbody tr:last-child td {
  border-bottom: 0;
}
tr.click {
  cursor: pointer;
}
tr.click:hover td {
  background: var(--surface-2);
}
tr.u-row-notfall td:first-child {
  box-shadow: inset 4px 0 0 var(--u-notfall);
}
tr.u-row-hoch td:first-child {
  box-shadow: inset 4px 0 0 var(--u-hoch);
}
.table-card {
  padding: 0;
  overflow: hidden;
}
.table-wrap {
  overflow-x: auto;
}
.cell-title {
  font-weight: 600;
}
.empty {
  padding: 36px 16px;
  text-align: center;
  color: var(--muted);
}
.pager {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

dl.kv {
  display: grid;
  grid-template-columns: 130px 1fr;
  gap: 6px 12px;
  margin: 0;
}
dl.kv dt {
  color: var(--muted);
  font-size: 0.85rem;
}
dl.kv dd {
  margin: 0;
  overflow-wrap: anywhere;
}

/* ---------- Verlauf ---------- */
.timeline {
  list-style: none;
  margin: 0;
  padding: 0;
}
.timeline li {
  display: grid;
  grid-template-columns: 10px 1fr;
  gap: 10px;
  padding-bottom: 14px;
  position: relative;
}
.timeline li::before {
  content: '';
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--border);
  margin-top: 6px;
}
.timeline li::after {
  content: '';
  position: absolute;
  left: 4px;
  top: 18px;
  bottom: 0;
  width: 2px;
  background: var(--border);
}
.timeline li:last-child::after {
  display: none;
}
.timeline li.kommentar::before {
  background: var(--primary);
}
.timeline li.status::before {
  background: var(--s-abgeschlossen);
}
.timeline li.mail::before {
  background: var(--u-hoch);
}
.timeline .meta {
  color: var(--muted);
  font-size: 0.8rem;
}

.mail-body {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  max-height: 340px;
  overflow: auto;
  font-size: 0.9rem;
}
.attach {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.attach a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 0.85rem;
  background: var(--surface);
}
.thumbs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}
.thumbs img {
  width: 100%;
  height: 90px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

/* ---------- Hinweise, Toasts, Dialoge ---------- */
.notice {
  padding: 10px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
  background: var(--warn-soft);
  color: var(--warn);
  border: 1px solid transparent;
}
.notice.error {
  background: var(--danger-soft);
  color: var(--danger);
}
.notice.ok {
  background: var(--ok-soft);
  color: var(--ok);
}
.notice .row {
  gap: 8px;
}
.toast-area {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 8px;
  max-width: 340px;
  box-shadow: 0 6px 20px rgb(0 0 0 / 25%);
}
.toast.error {
  background: var(--danger);
  color: #fff;
}
dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 32px);
}
dialog::backdrop {
  background: rgb(0 0 0 / 45%);
}
.dialog-body {
  padding: 18px;
}
.dialog-body h2 {
  margin-bottom: 12px;
}
.secret {
  font: 600 1.1rem ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  user-select: all;
  overflow-wrap: anywhere;
}

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  width: min(400px, 100%);
}
.login-card .brand {
  justify-content: center;
  padding-bottom: 18px;
  font-size: 1.25rem;
}
.login-card .field + .field {
  margin-top: 12px;
}

/* ---------- Mobil ---------- */
.topbar,
.bottomnav {
  display: none;
}
@media (max-width: 899px) {
  .shell {
    grid-template-columns: minmax(0, 1fr);
  }
  .sidebar {
    display: none;
  }
  .topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
  }
  .topbar .brand {
    padding: 0;
    flex: 1;
  }
  .main {
    padding: 16px 16px 96px;
  }
  .bottomnav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .bottomnav a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px 2px 7px;
    font-size: 0.7rem;
    color: var(--muted);
    position: relative;
  }
  .bottomnav a.active {
    color: var(--primary);
  }
  .bottomnav a:hover {
    text-decoration: none;
  }
  .bottomnav .count {
    position: absolute;
    top: 3px;
    left: 54%;
    background: var(--danger);
    color: #fff;
    border-radius: 999px;
    font-size: 0.65rem;
    padding: 0 5px;
    font-weight: 700;
  }
  .grid.cols-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .grid.cols-3,
  .grid.cols-2,
  .detail-layout {
    grid-template-columns: minmax(0, 1fr);
  }
  /* Auftragsdetail am Handy: Status und Eckdaten vor dem langen Verlauf */
  .detail-layout > .stack {
    display: contents;
  }
  .slot-1 { order: 1; }
  .slot-2 { order: 2; }
  .slot-3 { order: 3; }
  .slot-4 { order: 4; }
  .slot-5 { order: 5; }
  .slot-6 { order: 6; }
  .slot-7 { order: 7; }
  .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .form-grid .s2,
  .form-grid .s3 {
    grid-column: span 1;
  }
  .form-grid .s4,
  .form-grid .s6,
  .form-grid h3 {
    grid-column: span 2;
  }
  .toast-area {
    left: 16px;
    right: 16px;
    bottom: 80px;
  }

  /* Tabellen werden zu Karten */
  table.cards thead {
    display: none;
  }
  table.cards,
  table.cards tbody,
  table.cards tr,
  table.cards td {
    display: block;
    width: 100%;
  }
  table.cards tr {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
  }
  table.cards tr:last-child {
    border-bottom: 0;
  }
  table.cards td {
    border: 0;
    padding: 2px 0;
  }
  table.cards td[data-label]::before {
    content: attr(data-label) ': ';
    color: var(--muted);
    font-size: 0.8rem;
  }
  table.cards td.no-label::before {
    content: none;
  }
  table.cards tr.u-row-notfall,
  table.cards tr.u-row-hoch {
    border-left: 4px solid var(--u-notfall);
  }
  table.cards tr.u-row-hoch {
    border-left-color: var(--u-hoch);
  }
  table.cards tr.u-row-notfall td:first-child,
  table.cards tr.u-row-hoch td:first-child {
    box-shadow: none;
  }
  dl.kv {
    grid-template-columns: 105px 1fr;
  }
}
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
