:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --text: #17212b;
  --muted: #5d6b78;
  --border: #d9e0e6;
  --primary: #0e5a86;
  --primary-text: #ffffff;
  --primary-soft: #e1eff7;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --ok: #1d7a46;
  --ok-soft: #e3f4ea;
  --warn: #9a5b00;
  --warn-soft: #fdf0d9;
  --grey-soft: #e9ecef;
  --shadow: 0 1px 2px rgba(16, 24, 32, .06), 0 2px 8px rgba(16, 24, 32, .06);
  --radius: 14px;
  --tabbar-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1418;
    --surface: #182027;
    --surface-2: #212b33;
    --text: #e7edf2;
    --muted: #9aa8b4;
    --border: #2d3943;
    --primary: #5bb3e6;
    --primary-text: #06202f;
    --primary-soft: #16344a;
    --danger: #f2877e;
    --danger-soft: #3a1d1b;
    --ok: #6fd39a;
    --ok-soft: #173325;
    --warn: #f0b95c;
    --warn-soft: #3a2c12;
    --grey-soft: #29333b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h2 { font-size: 1.05rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
.boot-msg { padding: 40px 16px; text-align: center; color: var(--muted); }

/* ---------- Layout */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  background: var(--primary); color: var(--primary-text);
}
.topbar h1 { font-size: 1.1rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .icon-btn { color: inherit; }
main {
  max-width: 760px; margin: 0 auto;
  padding: 12px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  display: grid; gap: 12px;
}
main.full { max-width: none; padding: 0; }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .72rem; position: relative;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--primary); font-weight: 600; }
.tabbar .badge { position: absolute; top: 6px; left: calc(50% + 6px); }

/* ---------- Bausteine */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; display: grid; gap: 10px; min-width: 0;
}
.card.tight { gap: 6px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }
.row.nowrap { flex-wrap: nowrap; }
.spread { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.stack { display: grid; gap: 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.section-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 8px 2px 0; }
.empty { text-align: center; color: var(--muted); padding: 24px 8px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; width: 100%; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--primary); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .9rem; border-radius: 10px; }
.btn.block { width: 100%; }
.btn svg { width: 18px; height: 18px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border: 0; background: transparent; color: var(--muted);
  border-radius: 12px; cursor: pointer; font: inherit;
}
.icon-btn svg { width: 22px; height: 22px; }

.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px;
  font-size: .78rem; font-weight: 600; background: var(--grey-soft); color: var(--muted); white-space: nowrap;
}
.chip.blue { background: var(--primary-soft); color: var(--primary); }
.chip.green { background: var(--ok-soft); color: var(--ok); }
.chip.orange { background: var(--warn-soft); color: var(--warn); }
.chip.red { background: var(--danger-soft); color: var(--danger); }
.badge {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff;
  font-size: .7rem; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
}
.chips-select { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips-select button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px;
  padding: 6px 12px; font: inherit; font-size: .88rem; white-space: nowrap; cursor: pointer;
}
.chips-select button.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }

.pill-checks { display: flex; flex-wrap: wrap; gap: 6px; }
.pill {
  position: relative; display: inline-flex; align-items: center; min-height: 36px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface); color: var(--text);
  font-size: .88rem; cursor: pointer; user-select: none;
}
.pill.focus { border-color: var(--primary); }
.pill input { position: absolute; opacity: 0; width: 1px; height: 1px; min-height: 0; }
.pill:has(input:checked) { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.pill:has(input:focus-visible) { outline: 2px solid var(--primary); outline-offset: 2px; }
div.field { display: grid; gap: 6px; font-size: .85rem; color: var(--muted); }

/* ---------- Formulare */
label.field { display: grid; gap: 4px; font-size: .85rem; color: var(--muted); min-width: 0; }
input, select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit;
}
textarea { min-height: 96px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: var(--primary); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-size: .95rem; color: var(--text); }
.check input { width: 22px; height: 22px; min-height: 0; accent-color: var(--primary); }
.hint { font-size: .82rem; color: var(--muted); }
.error-box { background: var(--danger-soft); color: var(--danger); padding: 10px 12px; border-radius: 10px; font-size: .9rem; }
.warn-box { background: var(--warn-soft); color: var(--warn); padding: 10px 12px; border-radius: 10px; font-size: .9rem; }

/* ---------- Tour */
.progress { height: 10px; border-radius: 5px; background: var(--surface-2); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--primary); border-radius: 5px; }
.progress.done > span { background: var(--ok); }
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.kpis b { display: block; font-size: 1.1rem; }
.kpis span { font-size: .75rem; color: var(--muted); }
.stop { display: grid; grid-template-columns: 40px 1fr; gap: 10px; }
.stop-num {
  width: 36px; height: 36px; border-radius: 50%; background: var(--primary); color: var(--primary-text);
  display: flex; align-items: center; justify-content: center; font-weight: 700;
}
.stop-num.done { background: var(--ok); }
.stop-leg { font-size: .8rem; color: var(--muted); padding-left: 50px; }
.practice-line {
  display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--border); min-width: 0;
}
.practice-line:first-of-type { border-top: 0; }
.practice-line a.name { color: var(--text); text-decoration: none; font-weight: 600; }

/* ---------- Karte */
.map-wrap { position: relative; height: calc(100dvh - 56px - var(--tabbar-h) - env(safe-area-inset-bottom) - env(safe-area-inset-top)); }
.map-el { position: absolute; inset: 0; }
.map-overlay { position: absolute; left: 12px; right: 12px; top: 12px; display: flex; gap: 8px; justify-content: center; pointer-events: none; }
.map-overlay > * { pointer-events: auto; box-shadow: 0 2px 8px rgba(0,0,0,.2); }
.bubble {
  min-width: 30px; height: 30px; padding: 0 7px; border-radius: 15px; border: 2px solid #fff;
  background: #0e5a86; color: #fff; font: 700 13px/26px system-ui, sans-serif; text-align: center;
  box-shadow: 0 1px 4px rgba(0,0,0,.35); cursor: pointer;
}
.bubble.big { min-width: 38px; height: 38px; border-radius: 19px; line-height: 34px; font-size: 15px; }
.bubble.customer { background: #1d7a46; }
.bubble.appointment { background: #c77700; }
.bubble.closed { background: #7b868f; }
.bubble.due { border-color: #e53935; border-width: 3px; }

/* ---------- Sheet (Dialog) */
dialog#sheet {
  width: 100%; max-width: 640px; max-height: 94dvh; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 18px 18px 0 0; background: var(--bg); color: var(--text);
}
dialog#sheet::backdrop { background: rgba(10, 16, 22, .5); }
@media (min-width: 700px) { dialog#sheet { margin: auto; border-radius: 18px; } }
.sheet-head {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 8px;
  padding: 10px 8px 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.sheet-head h2 { flex: 1; min-width: 0; }
.sheet-body { padding: 14px 16px calc(20px + env(safe-area-inset-bottom)); display: grid; gap: 12px; }
.sheet-foot {
  position: sticky; bottom: 0; padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border); display: flex; gap: 8px;
}
.sheet-foot .btn { flex: 1; }

/* ---------- Unterschrift und Nachweis */
.signature-pad {
  width: 100%; height: 200px; background: #fff; border: 2px dashed #9aa8b4; border-radius: 12px;
  touch-action: none; display: block;
}
.consent-text {
  white-space: pre-wrap; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; font-size: .9rem; max-height: 240px; overflow: auto;
}
.signature-img { background: #fff; border: 1px solid var(--border); border-radius: 10px; width: 100%; max-width: 420px; }

.task { display: flex; align-items: flex-start; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); }
.task:first-child { border-top: 0; }
.task input[type=checkbox] { width: 24px; height: 24px; min-height: 0; margin-top: 2px; accent-color: var(--ok); flex: none; }
.task.done .task-title { text-decoration: line-through; color: var(--muted); }
.task-row { display: grid; grid-template-columns: 1fr 150px 44px; gap: 6px; align-items: center; }
@media (max-width: 420px) { .task-row { grid-template-columns: 1fr 44px; } .task-row input[type=date] { grid-column: 1; } .task-row .icon-btn { grid-row: 1 / span 2; grid-column: 2; } }

.result-group .place { display: grid; grid-template-columns: 28px 1fr; gap: 8px; padding: 8px 0; border-top: 1px solid var(--border); }
.result-group .place input[type=checkbox] { width: 22px; height: 22px; min-height: 0; margin-top: 2px; accent-color: var(--primary); }
.place-edit { display: grid; grid-template-columns: 1fr 80px; gap: 6px; margin-top: 6px; }
.place-edit select, .place-edit input { min-height: 36px; padding: 6px 8px; font-size: .88rem; }
.sticky-action {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); z-index: 25;
  padding: 10px 16px; background: var(--surface); border-top: 1px solid var(--border);
}
.sticky-action .btn { width: 100%; max-width: 728px; margin: 0 auto; display: flex; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); transform: translate(-50%, 20px);
  background: #17212b; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: .92rem;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 1000; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: #b3261e; }

.login { max-width: 380px; margin: 12vh auto 0; padding: 0 16px; display: grid; gap: 14px; }
.login h1 { text-align: center; }

@media print {
  .topbar, .tabbar, .sheet-head, .sheet-foot, .no-print { display: none !important; }
  dialog#sheet { position: static; max-height: none; box-shadow: none; }
  body { background: #fff; }
}
