:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #0ea5e9;
  --accent-ink: #ffffff;
  --accent-strong: #0284c7;
  --danger: #ef4444;
  --dark: #0b1220;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #131c2e;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --line: #1f2a40;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
svg { display: block; }

.view { min-height: 100%; display: flex; flex-direction: column; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + var(--safe-t)) 14px 10px;
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.topbar.dark { background: var(--dark); color: #fff; position: static; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; letter-spacing: -.01em; }
.brand img { border-radius: 7px; }
.topbar-actions { display: flex; align-items: center; gap: 4px; }
.icon-btn {
  width: 42px; height: 42px; border: 0; background: transparent; border-radius: 50%;
  display: grid; place-items: center;
}
.icon-btn:active { background: color-mix(in srgb, currentColor 12%, transparent); }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.topbar-actions .icon-btn svg { fill: currentColor; stroke: none; }
.icon-btn.danger { color: #fca5a5; }
.flag-btn {
  width: 34px; height: 24px; padding: 0; border: 1px solid var(--line); border-radius: 4px;
  overflow: hidden; background: none;
}
.flag-btn svg { width: 100%; height: 100%; }
.ed-title { font-weight: 600; flex: 1; text-align: center; }
.icon-btn.invisible { visibility: hidden; }

/* ---------- Dokumentname ---------- */
.docname-row { display: flex; align-items: center; gap: 10px; padding: 4px 16px 10px; }
.docname-row input {
  flex: 1; min-width: 0; font: inherit; font-weight: 600; font-size: 17px;
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text);
}
.docname-row input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.pagecount { color: var(--muted); font-size: 14px; white-space: nowrap; }

/* ---------- Leerzustand ---------- */
.empty { text-align: center; padding: 40px 28px; color: var(--muted); margin: auto 0; }
.empty h2 { color: var(--text); margin: 14px 0 6px; font-size: 20px; }
.empty p { margin: 0 auto 10px; max-width: 360px; }
.empty-ill { width: 96px; height: 96px; margin: 0 auto; color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; }
.hint.center { text-align: center; margin: 4px 16px; }
.hint.light { color: #94a3b8; }

/* ---------- Seitenraster ---------- */
.grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 14px; padding: 6px 16px 16px;
}
.tile {
  position: relative; aspect-ratio: 1 / 1.414;
  background: var(--surface); border-radius: 10px; overflow: hidden;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .08), 0 4px 14px rgba(15, 23, 42, .08);
  touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.tile img { width: 100%; height: 100%; object-fit: contain; background: #fff; pointer-events: none; }
.tile .num {
  position: absolute; left: 6px; bottom: 6px; min-width: 24px; height: 24px; padding: 0 7px;
  border-radius: 12px; background: rgba(15, 23, 42, .78); color: #fff; font-size: 13px; font-weight: 700;
  display: grid; place-items: center;
}
.tile .del {
  position: absolute; right: 4px; top: 4px; width: 30px; height: 30px; border-radius: 50%; border: 0;
  background: rgba(15, 23, 42, .62); color: #fff; display: grid; place-items: center;
}
.tile .del svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; }
.tile.ghost { opacity: .35; }
.tile.chosen { outline: 3px solid var(--accent); transform: scale(1.04); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: 12px; border: 0; font-weight: 600;
}
.btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:active { background: var(--accent-strong); }
.btn.secondary { background: color-mix(in srgb, var(--text) 8%, transparent); }
.btn:disabled { opacity: .4; }
.btn.wide { width: 100%; }

.bottombar {
  position: sticky; bottom: 0; margin-top: auto; z-index: 5;
  display: grid; grid-template-columns: 1fr 1.4fr 1fr; gap: 10px; align-items: center;
  padding: 12px 16px calc(12px + var(--safe-b));
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.bottombar .btn { flex-direction: column; gap: 2px; font-size: 13px; padding: 6px 4px; }
.bottombar .btn.big { min-height: 62px; font-size: 15px; border-radius: 16px; box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 40%, transparent); }
.bottombar .btn.big svg { width: 28px; height: 28px; }

/* ---------- Editor ---------- */
.editor { position: fixed; inset: 0; z-index: 20; background: var(--dark); color: #fff; min-height: 0; }
.ed-step { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.stage { position: relative; flex: 1; min-height: 0; overflow: hidden; display: grid; place-items: center; }
#cropCanvas { position: absolute; }
.overlay { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.overlay .shade { fill: rgba(0, 0, 0, .5); }
.overlay .edge { fill: rgba(14, 165, 233, .10); stroke: #38bdf8; stroke-width: 2.5; stroke-linejoin: round; }
.overlay .handle { fill: rgba(14, 165, 233, .25); stroke: #fff; stroke-width: 3; }
.overlay .mid { fill: #fff; stroke: #38bdf8; stroke-width: 2.5; }
.loupe {
  position: absolute; width: 110px; height: 110px; border-radius: 50%;
  border: 3px solid #fff; box-shadow: 0 6px 20px rgba(0, 0, 0, .5); pointer-events: none;
}
#resultImg { max-width: calc(100% - 32px); max-height: calc(100% - 32px); background: #fff; box-shadow: 0 8px 30px rgba(0, 0, 0, .5); }
.spinner, .mini-spin {
  border: 4px solid rgba(255, 255, 255, .25); border-top-color: #fff; border-radius: 50%;
  animation: spin .8s linear infinite;
}
.spinner { position: absolute; width: 44px; height: 44px; }
.mini-spin { display: inline-block; width: 16px; height: 16px; border-width: 3px; margin-right: 10px; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }

.ed-tools { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 10px 12px 0; }
.filters { display: flex; gap: 6px; overflow-x: auto; max-width: 100%; scrollbar-width: none; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 14px;
  border-radius: 20px; border: 1px solid rgba(255, 255, 255, .18); background: rgba(255, 255, 255, .06);
  color: #fff; font-size: 14px; white-space: nowrap;
}
.chip svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chip.active { background: var(--accent); border-color: var(--accent); }
.ed-actions { display: flex; gap: 10px; padding: 10px 16px calc(14px + var(--safe-b)); }
.ed-actions.two .btn { flex: 1; padding: 0 8px; white-space: nowrap; }
.editor .btn.secondary { background: rgba(255, 255, 255, .1); color: #fff; }

/* ---------- Dialoge ---------- */
.sheet {
  border: 0; padding: 0; width: min(520px, 100%); max-width: 100%;
  margin: auto auto 0; border-radius: 20px 20px 0 0;
  background: var(--surface); color: var(--text);
}
@media (min-width: 640px) { .sheet { margin: auto; border-radius: 20px; } }
.sheet::backdrop { background: rgba(2, 6, 23, .55); }
.sheet form { padding: 20px 20px calc(18px + var(--safe-b)); display: flex; flex-direction: column; gap: 12px; }
.sheet h3 { margin: 0 0 4px; font-size: 19px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
.field input, .field select {
  font: inherit; font-size: 16px; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px;
}
.sheet-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }
.sheet-actions .btn { flex: 1 1 auto; }
.menu-item {
  display: block; color: var(--text); text-decoration: none;
  text-align: left; padding: 14px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg);
}
.legal { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; }
.legal a { color: var(--muted); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(100px + var(--safe-b)); transform: translateX(-50%);
  max-width: calc(100% - 32px); z-index: 50;
  background: rgba(15, 23, 42, .94); color: #fff; padding: 12px 18px; border-radius: 12px;
  font-size: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, .3);
}
.toast .btn { min-height: 40px; }

/* ---------- Login ---------- */
.login { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; padding: 24px 16px; }
.login-card {
  width: min(380px, 100%); background: var(--surface); border-radius: 20px; padding: 28px 22px;
  display: flex; flex-direction: column; gap: 14px; box-shadow: 0 10px 40px rgba(15, 23, 42, .12);
}
.login-card img { align-self: center; border-radius: 14px; }
.login-card h1 { margin: 0 0 6px; text-align: center; font-size: 24px; }
.login-error { margin: 0; color: var(--danger); font-size: 14px; }
