:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #64707f;
  --line: #dde3ea;
  --accent: #1f4e79;
  --accent-soft: #e3edf7;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #9a6200;
  --warn-soft: #fff4dc;
  --ok: #1d7a46;
  --ok-soft: #e4f5eb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --card: #1b2129;
    --text: #e6eaf0;
    --muted: #94a0b0;
    --line: #2c3540;
    --accent: #6aa7e0;
    --accent-soft: #1e3247;
    --danger: #f27a6f;
    --danger-soft: #3a1f1d;
    --warn: #f0b54a;
    --warn-soft: #3a2e17;
    --ok: #5cc98b;
    --ok-soft: #173325;
    --shadow: none;
    color-scheme: dark;
  }
}

* { 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, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.center { text-align: center; }
.small { font-size: .85rem; }
[hidden] { display: none !important; }

/* Kopfzeile */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--accent); color: #fff;
}
@media (prefers-color-scheme: dark) { .topbar { background: #16304a; } }
.brand { color: #fff; font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.search { flex: 1; max-width: 520px; margin-left: auto; }
.search input {
  width: 100%; padding: 8px 12px; border-radius: 8px; border: 0;
  background: rgba(255, 255, 255, .15); color: #fff; font: inherit;
}
.search input::placeholder { color: rgba(255, 255, 255, .7); }
.search input:focus { outline: 2px solid rgba(255, 255, 255, .6); background: rgba(255, 255, 255, .22); }
.topbar .btn.ghost { color: #fff; border-color: rgba(255, 255, 255, .4); }
.usernav { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.usernav a { color: #fff; font-size: .9rem; }
@media (max-width: 900px) { .usernav .lbl:not(#userName) { display: none; } }
@media (max-width: 560px) {
  .topbar { flex-wrap: wrap; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .usernav { margin-left: auto; gap: 10px; }
  .usernav .lbl { display: none; }
}

/* Leser: Bearbeiten-Elemente ausblenden (der Server lehnt Änderungen ohnehin ab) */
body.readonly #addVehicle, body.readonly #editVehicle, body.readonly [data-quick], body.readonly #addEntry,
body.readonly [data-edit], body.readonly #drop, body.readonly #addContract, body.readonly [data-upload] { display: none !important; }
body.readonly .vhead .photo { pointer-events: none; }
body.readonly .vhead .photo::after { display: none; }

main { max-width: 1100px; margin: 0 auto; padding: 16px 16px 64px; }

/* Bausteine */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--accent);
  background: var(--accent); color: #fff; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
@media (prefers-color-scheme: dark) { .btn { color: #0d1620; } }
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  padding: 6px; border-radius: 6px; font-size: 1rem; line-height: 1;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }

.chip {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600;
  background: var(--accent-soft); color: var(--accent); white-space: nowrap;
}
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.danger { background: var(--danger-soft); color: var(--danger); }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.grey { background: var(--line); color: var(--muted); }

.plate {
  display: inline-block; padding: 1px 8px 1px 14px; border: 2px solid #222; border-radius: 4px;
  background: #fff linear-gradient(90deg, #003399 0 8px, #fff 8px); color: #111;
  font: 700 .9rem/1.3 "DIN Alternate", "Arial Narrow", sans-serif; letter-spacing: .5px; white-space: nowrap;
}

/* Fahrzeugkarten */
.vcard { display: flex; flex-direction: column; padding: 0; overflow: hidden; color: inherit; }
.vcard:hover { text-decoration: none; border-color: var(--accent); }
.vcard .photo { height: 150px; }
.vcard .body { padding: 12px 16px 16px; }
.vcard.inactive { opacity: .65; }
.photo {
  background: var(--accent-soft) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; font-size: 3rem;
}
.stats { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 8px; font-size: .85rem; color: var(--muted); }
.add-card {
  display: flex; align-items: center; justify-content: center; min-height: 200px;
  border: 2px dashed var(--line); border-radius: var(--radius); color: var(--muted);
  font-weight: 600; cursor: pointer; background: transparent; font: inherit;
}
.add-card:hover { border-color: var(--accent); color: var(--accent); }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi .v { font-size: 1.4rem; font-weight: 700; }
.kpi .l { color: var(--muted); font-size: .85rem; }

/* Listen */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: 10px 0; border-top: 1px solid var(--line); }
.list > li:first-child { border-top: 0; }
.deadline { display: flex; gap: 12px; align-items: center; }
.deadline .when { min-width: 92px; text-align: right; }

/* Fahrzeugkopf */
.vhead { display: grid; grid-template-columns: 220px 1fr; gap: 16px; padding: 0; overflow: hidden; }
.vhead .photo { min-height: 160px; cursor: pointer; position: relative; }
.vhead .photo::after {
  content: "📷 Foto ändern"; position: absolute; bottom: 8px; right: 8px; font-size: .75rem;
  background: rgba(0, 0, 0, .55); color: #fff; padding: 2px 8px; border-radius: 6px; opacity: 0; transition: opacity .2s;
}
.vhead .photo:hover::after { opacity: 1; }
.vhead .info { padding: 16px 16px 16px 0; }
@media (max-width: 640px) {
  .vhead { grid-template-columns: 1fr; }
  .vhead .info { padding: 0 16px 16px; }
}

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs a {
  padding: 10px 14px; color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent; white-space: nowrap;
}
.tabs a:hover { text-decoration: none; color: var(--text); }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }
.tabs .count { font-weight: 400; font-size: .8rem; margin-left: 4px; }

dl.facts { display: grid; grid-template-columns: minmax(130px, max-content) 1fr; gap: 6px 16px; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; word-break: break-word; }

/* Chronik */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline > li { display: grid; grid-template-columns: 96px 1fr auto; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.timeline > li:first-child { border-top: 0; }
.timeline .date { color: var(--muted); font-size: .85rem; padding-top: 2px; }
.timeline .meta { display: flex; gap: 12px; flex-wrap: wrap; font-size: .85rem; color: var(--muted); margin-top: 2px; }
.timeline .notes { white-space: pre-wrap; margin-top: 4px; font-size: .9rem; }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.attachments a { font-size: .82rem; background: var(--accent-soft); padding: 2px 8px; border-radius: 6px; }
@media (max-width: 560px) {
  .timeline > li { grid-template-columns: 1fr auto; }
  .timeline .date { grid-column: 1 / -1; padding: 0; }
}

/* Dokumente */
.folder { padding: 0; overflow: hidden; }
.folder > summary {
  display: flex; align-items: center; gap: 8px; padding: 12px 16px; cursor: pointer; font-weight: 600; list-style: none;
}
.folder > summary::-webkit-details-marker { display: none; }
.folder > summary::before { content: "📁"; }
.folder[open] > summary::before { content: "📂"; }
.folder > summary .count { margin-left: auto; }
.docs { list-style: none; margin: 0; padding: 0 16px 8px; }
.doc { display: grid; grid-template-columns: 48px 1fr auto; gap: 12px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.doc .thumb {
  width: 48px; height: 48px; border-radius: 8px; background: var(--accent-soft) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; font-size: 1.4rem; overflow: hidden;
}
.doc .title { font-weight: 600; word-break: break-word; }
.doc .actions { display: flex; gap: 2px; }
.dropzone {
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 20px; text-align: center; color: var(--muted);
  cursor: pointer; transition: border-color .2s, background .2s;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }

/* Kosten */
table.data { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.data th, table.data td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tfoot td { font-weight: 700; border-bottom: 0; }
.bar { height: 8px; border-radius: 4px; background: var(--accent); min-width: 2px; }

/* Dialog & Formulare */
dialog {
  border: 0; border-radius: var(--radius); padding: 0; width: min(640px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px); background: var(--card); color: var(--text); box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(10, 15, 25, .5); }
dialog form { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); }
dialog header, dialog footer { padding: 14px 18px; display: flex; align-items: center; gap: 8px; }
dialog header { border-bottom: 1px solid var(--line); }
dialog header h2 { margin: 0; flex: 1; }
dialog footer { border-top: 1px solid var(--line); justify-content: flex-end; }
dialog .content { padding: 16px 18px; overflow-y: auto; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.fields .full, .fields h3 { grid-column: 1 / -1; }
.fields h3 { margin: 8px 0 -4px; font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
@media (max-width: 520px) { .fields { grid-template-columns: 1fr; } }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
label.field.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: .95rem; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; width: 100%;
}
input[type=checkbox] { width: auto; }
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.error { color: var(--danger); font-size: .9rem; }
progress { width: 100%; height: 8px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(100px);
  background: #1c2430; color: #fff; padding: 10px 18px; border-radius: 8px; transition: transform .25s;
  z-index: 100; max-width: calc(100vw - 32px);
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast:not(.show) { visibility: hidden; }
#toast.err { background: var(--danger); }

.login { max-width: 400px; margin: 8vh auto; }
.login .links { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-size: .9rem; }
.notice { background: var(--accent-soft); color: var(--text); border-radius: 8px; padding: 12px; }
.notice.ok { background: var(--ok-soft); }
.table-wrap { overflow-x: auto; }
table.data select { width: auto; padding: 4px 6px; }
tr.me td { background: var(--accent-soft); }
.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.empty .big { font-size: 2.5rem; }

/* Import */
.textpreview { white-space: pre-wrap; font-size: .8rem; background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; max-height: 240px; overflow: auto; margin: 6px 0 0; }
.import-item .field .small { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; color: var(--muted); }
