/* KartenScan - mobile-first, ohne Framework. */

:root {
  --bg: #0b1120;
  --bg-soft: #111a2e;
  --surface: #16203a;
  --surface-2: #1d2947;
  --line: #2b3757;
  --text: #eef2fb;
  --muted: #9aa7c4;
  --accent: #4f8cff;
  --accent-ink: #04132e;
  --danger: #ff6b6b;
  --ok: #34d399;
  --radius: 14px;
  --radius-lg: 20px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --bar-h: calc(56px + var(--safe-t));
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb;
    --bg-soft: #ffffff;
    --surface: #ffffff;
    --surface-2: #eef1f8;
    --line: #d8dfef;
    --text: #101828;
    --muted: #5b6780;
    --accent: #2563eb;
    --accent-ink: #ffffff;
  }
}

* { box-sizing: border-box; }

/*
 * Das hidden-Attribut muss gewinnen. Regeln wie ".sheet { display: flex }"
 * sind spezifischer als die Browser-Vorgabe "[hidden] { display: none }" -
 * ohne diese Zeile blieben alle Overlays dauerhaft sichtbar und fingen Klicks ab.
 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body {
  display: flex;
  flex-direction: column;
  /* dvh verhindert Sprünge, wenn die iOS-Adressleiste ein-/ausblendet */
  min-height: 100dvh;
}

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------------- App-Leiste ---------------- */
.appbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: calc(var(--safe-t) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(150%) blur(12px);
  border-bottom: 1px solid var(--line);
}
.appbar__title { margin: 0; font-size: 1.05rem; font-weight: 650; letter-spacing: -.01em; display: flex; align-items: center; gap: 9px; }
.appbar__mark { width: 22px; height: 15px; border-radius: 4px; background: linear-gradient(135deg, var(--accent), #8b5cf6); box-shadow: 0 2px 8px rgba(79,140,255,.4); }
.appbar__actions { display: flex; gap: 4px; }

/* ---------------- Buttons ---------------- */
.btn {
  -webkit-appearance: none; appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 9px;
  min-height: 48px; padding: 0 18px;
  border: 1px solid transparent; border-radius: var(--radius);
  background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
  transition: transform .12s ease, filter .15s ease, background .15s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn:active { transform: scale(.975); }
.btn--primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 18px -6px color-mix(in srgb, var(--accent) 70%, transparent); }
.btn--ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn--xl { min-height: 58px; font-size: 1.05rem; width: 100%; }
.btn--sm { min-height: 40px; font-size: .9rem; padding: 0 14px; }
.btn--grow { flex: 1; }
.btn--add { min-height: 40px; font-size: .9rem; background: transparent; border: 1px dashed var(--line); color: var(--muted); width: 100%; }
.btn[disabled] { opacity: .5; pointer-events: none; }

.iconbtn {
  -webkit-appearance: none; appearance: none;
  position: relative;
  display: inline-flex; align-items: center; justify-content: center;
  width: 42px; height: 42px; padding: 0;
  border: 0; border-radius: 12px;
  background: transparent; color: var(--text); cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.iconbtn:active { background: var(--surface-2); }
.iconbtn--lg { width: 54px; height: 54px; background: rgba(255,255,255,.12); color: #fff; }
.badge {
  position: absolute; top: 4px; right: 3px;
  min-width: 17px; height: 17px; padding: 0 4px;
  border-radius: 9px; background: var(--accent); color: var(--accent-ink);
  font-size: .68rem; font-weight: 700; line-height: 17px; text-align: center;
}

.chip {
  -webkit-appearance: none; appearance: none;
  padding: 7px 12px; border: 1px solid var(--line); border-radius: 999px;
  background: transparent; color: var(--muted);
  font: inherit; font-size: .82rem; font-weight: 600; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.chip:active { background: var(--surface-2); color: var(--text); }

/* ---------------- Ansichten ---------------- */
main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.view { display: none; flex: 1; flex-direction: column; min-height: 0; }
.view.is-active { display: flex; }

/* Start */
.view--start { padding: 20px 16px calc(24px + var(--safe-b)); justify-content: center; gap: 28px; }
.hero { text-align: center; }
.hero__card {
  width: 190px; height: 120px; margin: 0 auto 24px;
  border-radius: 12px; background: linear-gradient(150deg, var(--surface), var(--surface-2));
  border: 1px solid var(--line);
  box-shadow: 0 18px 40px -18px rgba(0,0,0,.6);
  padding: 20px 18px; display: flex; flex-direction: column; gap: 9px;
  transform: rotate(-3deg);
}
.hero__line { height: 7px; border-radius: 4px; background: var(--line); }
.hero__line--xl { width: 72%; height: 11px; background: linear-gradient(90deg, var(--accent), #8b5cf6); }
.hero__line--m { width: 52%; }
.hero__line--s { width: 86%; height: 5px; }
.hero__title { margin: 0 0 8px; font-size: 1.6rem; letter-spacing: -.02em; }
.hero__text { margin: 0 auto; max-width: 34ch; color: var(--muted); font-size: .95rem; }
.actions { display: flex; flex-direction: column; gap: 10px; }
.hint { margin: 0; text-align: center; color: var(--muted); font-size: .82rem; min-height: 1.2em; }

/* Kamera */
.view--camera { background: #000; }
.camera { position: relative; flex: 1; min-height: 0; overflow: hidden; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.camera__frame {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(88vw, 520px); aspect-ratio: 85 / 55;
  border-radius: 12px; box-shadow: 0 0 0 100vmax rgba(0,0,0,.45);
}
.corner { position: absolute; width: 26px; height: 26px; border: 3px solid #fff; }
.corner--tl { top: -2px; left: -2px; border-right: 0; border-bottom: 0; border-radius: 10px 0 0 0; }
.corner--tr { top: -2px; right: -2px; border-left: 0; border-bottom: 0; border-radius: 0 10px 0 0; }
.corner--bl { bottom: -2px; left: -2px; border-right: 0; border-top: 0; border-radius: 0 0 0 10px; }
.corner--br { bottom: -2px; right: -2px; border-left: 0; border-top: 0; border-radius: 0 0 10px 0; }
.camera__hint {
  position: absolute; left: 0; right: 0; bottom: 22px; margin: 0;
  text-align: center; color: #fff; font-size: .88rem; text-shadow: 0 1px 6px rgba(0,0,0,.8);
}
.camera__bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 26px calc(18px + var(--safe-b)); background: #000;
}
.shutter {
  width: 74px; height: 74px; padding: 5px;
  border: 3px solid #fff; border-radius: 50%; background: transparent; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: #fff; transition: transform .1s ease; }
.shutter:active span { transform: scale(.88); }

/* Verarbeitung */
.view--busy { align-items: center; justify-content: center; padding: 24px 20px calc(24px + var(--safe-b)); gap: 18px; }
.busy { display: flex; flex-direction: column; align-items: center; gap: 16px; width: 100%; max-width: 420px; }
.busy__preview { width: 100%; border-radius: var(--radius-lg); overflow: hidden; border: 1px solid var(--line); background: var(--surface); }
.busy__preview img { display: block; width: 100%; height: auto; }
.progress { width: 100%; height: 7px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.progress__bar { height: 100%; width: 0; border-radius: 4px; background: linear-gradient(90deg, var(--accent), #8b5cf6); transition: width .25s ease; }
.busy__status { margin: 0; color: var(--muted); font-size: .9rem; text-align: center; }

/* Ergebnis */
.result { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 16px 8px; }
.result__head { display: flex; gap: 12px; margin-bottom: 18px; }
.result__thumb { width: 108px; height: 70px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); flex: none; }
.result__meta { flex: 1; min-width: 0; }
.result__engine { margin: 0 0 8px; font-size: .78rem; color: var(--muted); }
.result__tools { display: flex; flex-wrap: wrap; gap: 6px; }

/* Formular */
.form { display: flex; flex-direction: column; gap: 12px; }
.field-row { display: flex; gap: 10px; }
.field-row > .field { flex: 1; min-width: 0; }
.field--sm { max-width: 34%; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: .75rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
input[type="text"], input[type="tel"], input[type="email"], input[type="url"], textarea, select {
  -webkit-appearance: none; appearance: none;
  width: 100%; min-height: 48px; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  /* 16px verhindert den Auto-Zoom von iOS Safari beim Fokus */
  font: inherit; font-size: 16px;
}
textarea { min-height: 60px; resize: vertical; line-height: 1.4; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
select { background-image: none; }

.group { margin: 6px 0 0; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius-lg); background: color-mix(in srgb, var(--surface) 45%, transparent); display: flex; flex-direction: column; gap: 9px; }
.group legend { padding: 0 6px; font-size: .75rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.multi { display: flex; gap: 8px; align-items: center; }
.multi input { flex: 1; min-width: 0; }
.multi select { width: auto; min-width: 96px; flex: none; padding: 12px 8px; font-size: .85rem; }
.multi .iconbtn { flex: none; width: 40px; height: 40px; color: var(--muted); }
#phones, #emails, #urls { display: flex; flex-direction: column; gap: 9px; }

/* Fußleiste */
.stickybar {
  position: sticky; bottom: 0;
  display: flex; gap: 10px;
  padding: 12px 16px calc(12px + var(--safe-b));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}

/* ---------------- Sheets ---------------- */
.scrim { position: fixed; inset: 0; z-index: 40; background: rgba(4,8,18,.6); backdrop-filter: blur(2px); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  display: flex; flex-direction: column;
  max-height: 88dvh;
  background: var(--bg-soft);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  border-top: 1px solid var(--line);
  box-shadow: 0 -18px 50px -20px rgba(0,0,0,.7);
  animation: rise .22s ease;
}
.sheet--full { top: 0; border-radius: 0; max-height: none; padding-top: var(--safe-t); }
@keyframes rise { from { transform: translateY(14px); opacity: 0; } }
.sheet__head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 14px 20px; border-bottom: 1px solid var(--line); }
.sheet__head h2 { margin: 0; font-size: 1.02rem; }
.sheet__body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 20px; }
.sheet__foot { display: flex; gap: 10px; padding: 12px 20px calc(14px + var(--safe-b)); border-top: 1px solid var(--line); }

.setting { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 13px 0; border-bottom: 1px solid var(--line); }
.setting--stack { flex-direction: column; align-items: stretch; gap: 10px; }
.setting:last-of-type { border-bottom: 0; }
.setting__label { display: flex; flex-direction: column; gap: 2px; font-weight: 600; font-size: .93rem; }
.setting__label small { font-weight: 400; color: var(--muted); font-size: .78rem; }
.setting select { width: auto; min-width: 140px; flex: none; }
.setting--switch input { -webkit-appearance: none; appearance: none; flex: none; width: 50px; height: 30px; border-radius: 15px; background: var(--surface-2); border: 1px solid var(--line); position: relative; cursor: pointer; transition: background .18s ease; }
.setting--switch input::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: var(--muted); transition: transform .18s ease, background .18s ease; }
.setting--switch input:checked { background: var(--accent); border-color: transparent; }
.setting--switch input:checked::after { transform: translateX(20px); background: #fff; }

.setting--inner { padding: 4px 0 12px; border-bottom: 0; }
.setting__row { display: flex; gap: 8px; margin-top: 10px; }
.setting__row .btn { flex: 1; }
#sync-fields { display: flex; flex-direction: column; gap: 10px; }

.ways { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.way {
  -webkit-appearance: none; appearance: none;
  display: flex; flex-direction: column; gap: 4px; width: 100%;
  padding: 14px 16px; text-align: left;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.way:active { background: var(--surface-2); }
.way__title { font-weight: 650; }
.way__hint { font-size: .82rem; color: var(--muted); line-height: 1.4; }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.list__item { display: flex; align-items: center; gap: 12px; padding: 11px 13px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.list__item h3 { margin: 0; font-size: .95rem; }
.list__item p { margin: 1px 0 0; font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list__body { flex: 1; min-width: 0; }
.list__empty { padding: 34px 10px; text-align: center; color: var(--muted); font-size: .9rem; }

.raw { margin: 0; padding: 14px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); font-size: .82rem; line-height: 1.5; white-space: pre-wrap; word-break: break-word; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.fineprint { margin: 16px 0 0; font-size: .74rem; color: var(--muted); text-align: center; }
#sync-state { margin: 4px 0 0; text-align: left; }

/* Zuschnitt */
.cropper { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; background: #000; overflow: hidden; touch-action: none; }
#crop-image { max-width: 100%; max-height: 100%; display: block; user-select: none; -webkit-user-drag: none; }
.cropbox { position: absolute; border: 2px solid #fff; box-shadow: 0 0 0 100vmax rgba(0,0,0,.55); cursor: move; }
.handle { position: absolute; width: 40px; height: 40px; }
.handle::after { content: ''; position: absolute; width: 20px; height: 20px; border: 3px solid var(--accent); }
.handle--tl { top: -20px; left: -20px; } .handle--tl::after { top: 18px; left: 18px; border-right: 0; border-bottom: 0; }
.handle--tr { top: -20px; right: -20px; } .handle--tr::after { top: 18px; right: 18px; border-left: 0; border-bottom: 0; }
.handle--bl { bottom: -20px; left: -20px; } .handle--bl::after { bottom: 18px; left: 18px; border-right: 0; border-top: 0; }
.handle--br { bottom: -20px; right: -20px; } .handle--br::after { bottom: 18px; right: 18px; border-left: 0; border-top: 0; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); z-index: 90;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 12px 18px; border-radius: 999px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--line);
  box-shadow: 0 12px 30px -12px rgba(0,0,0,.7);
  font-size: .88rem; font-weight: 600;
  animation: rise .2s ease;
}
.toast--error { background: var(--danger); color: #fff; border-color: transparent; }
.toast--ok { background: var(--ok); color: #04231a; border-color: transparent; }

@media (min-width: 720px) {
  .result, .view--start { max-width: 640px; margin: 0 auto; width: 100%; }
  .stickybar { max-width: 640px; margin: 0 auto; width: 100%; }
  .sheet { left: 50%; transform: translateX(-50%); width: min(640px, 100%); border-radius: var(--radius-lg); bottom: 16px; }
  .sheet--full { bottom: 0; top: 0; border-radius: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
