:root {
  color-scheme: light dark;
  --bg: #f3f2ee;
  --panel: #fafaf8;
  --surface: #ffffff;
  --well: #e9e8e3;
  --ink: #1c1d1a;
  --muted: #6b6d67;
  --line: rgba(28, 29, 26, .10);
  --line-strong: rgba(28, 29, 26, .18);
  --accent: #17805a;
  --accent-hover: #136d4c;
  --accent-soft: rgba(23, 128, 90, .12);
  --danger: #c8423b;
  --warn: #c98a1b;
  --shadow-sheet: 0 1px 2px rgba(0, 0, 0, .08), 0 10px 28px -10px rgba(0, 0, 0, .22);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151715;
    --panel: #1b1e1b;
    --surface: #262a26;
    --well: #111311;
    --ink: #eceee9;
    --muted: #979c93;
    --line: rgba(255, 255, 255, .08);
    --line-strong: rgba(255, 255, 255, .16);
    --accent: #2a9d70;
    --accent-hover: #33b07f;
    --accent-soft: rgba(42, 157, 112, .16);
    --shadow-sheet: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 30px -10px rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 13px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg);
  display: grid;
  grid-template-rows: auto 1fr auto;
  user-select: none;
  -webkit-font-smoothing: antialiased;
}
.sprite { display: none; }
svg { width: 16px; height: 16px; flex: none; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- Titelleiste */
.titlebar {
  height: 52px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 12px 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.mac .titlebar { padding-left: 86px; } /* Platz für die Ampel */
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 14px; letter-spacing: -.01em; }
.logo {
  display: grid; place-items: center; width: 26px; height: 26px; border-radius: 7px;
  background: var(--accent); color: #fff;
}
.status { display: flex; align-items: center; gap: 6px; min-width: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 7px; min-width: 0; max-width: 300px;
  height: 28px; padding: 0 11px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
  font-size: 12px; font-weight: 500;
}
.chip:hover { border-color: var(--line-strong); }
.chip .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 7px; height: 7px; border-radius: 50%; flex: none; background: var(--muted); }
.chip[data-state=ok] .dot { background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.chip[data-state=err] .dot { background: var(--danger); box-shadow: 0 0 0 3px rgba(200, 66, 59, .15); }
.chip[data-state=pending] .dot { background: var(--warn); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.icon-btn {
  display: grid; place-items: center; width: 30px; height: 30px;
  border: 0; border-radius: 8px; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--well); color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

/* ---------------------------------------------------------------- Hauptbereich */
main { display: grid; grid-template-columns: 236px 1fr; min-height: 0; }
.panel {
  display: flex; flex-direction: column; gap: 18px;
  padding: 18px 16px 16px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}
.panel h3 {
  margin: 0 0 7px; font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
.panel h3 .unit { text-transform: none; letter-spacing: 0; font-weight: 400; opacity: .8; }
.spacer { flex: 1; }

.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px;
  padding: 3px; border-radius: 9px; background: var(--well);
}
.seg button {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  height: 28px; padding: 0 8px; border: 0; border-radius: 7px;
  background: transparent; color: var(--muted); font-weight: 500;
  transition: background .12s, color .12s;
}
.seg button:hover:not(:disabled) { color: var(--ink); }
.seg button[aria-pressed=true] {
  background: var(--surface); color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .12), 0 0 0 .5px var(--line-strong);
}
.seg button:disabled { opacity: .35; }
.seg.vertical { grid-auto-flow: row; }
.seg.vertical button { justify-content: flex-start; height: 34px; padding: 0 10px; }
.seg.vertical button[aria-pressed=true] svg { color: var(--accent); }

.scan-btn {
  position: relative; overflow: hidden;
  display: flex; align-items: center; justify-content: center; gap: 9px;
  height: 46px; border: 0; border-radius: 11px;
  background: var(--accent); color: #fff; font-size: 14px; font-weight: 650;
  box-shadow: 0 1px 0 rgba(255, 255, 255, .15) inset, 0 6px 16px -6px rgba(23, 128, 90, .7);
  transition: background .12s, transform .08s;
}
.scan-btn:hover:not(:disabled) { background: var(--accent-hover); }
.scan-btn:active:not(:disabled) { transform: scale(.985); }
.scan-btn svg { width: 18px; height: 18px; }
.scan-btn kbd { font: inherit; font-size: 11px; font-weight: 500; opacity: .65; }
.scan-btn:disabled { opacity: .55; }
.scan-btn.busy { opacity: 1; }
.scan-btn.busy::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .22), transparent);
  animation: shimmer 1.2s linear infinite;
}
@keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }
.backs-btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  height: 38px; margin-top: -8px; border-radius: 10px;
  border: 1.5px dashed var(--accent); background: var(--accent-soft); color: var(--accent);
  font-weight: 600; animation: appear .25s ease-out;
}
.backs-btn:hover:not(:disabled) { border-style: solid; }
.backs-btn:disabled { opacity: .5; }
.backs-btn[hidden] { display: none; }
.backs-btn .badge { background: var(--accent); color: #fff; }
.hint { margin: -8px 2px 0; font-size: 11.5px; color: var(--muted); text-align: center; }

/* ---------------------------------------------------------------- Seiten */
.stage { display: flex; flex-direction: column; min-height: 0; overflow-y: auto; padding: 18px 22px 22px; }
.stage-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 16px; }
.stage-head h2 { margin: 0; font-size: 18px; font-weight: 650; letter-spacing: -.015em; }
.count { color: var(--muted); font-variant-numeric: tabular-nums; }

.blank-btn {
  margin-left: auto; display: inline-flex; align-items: center; gap: 6px;
  height: 28px; padding: 0 11px; border-radius: 999px;
  border: 1px solid rgba(201, 138, 27, .45); background: rgba(201, 138, 27, .12); color: var(--warn);
  font-size: 12px; font-weight: 600; animation: appear .25s ease-out;
}
.blank-btn:hover:not(:disabled) { background: rgba(201, 138, 27, .2); }
.blank-btn[hidden] { display: none; }
.blank-btn svg { width: 14px; height: 14px; }
.pg.blank .sheet { outline: 2px dashed var(--warn); outline-offset: 3px; }
.pg.blank img { opacity: .55; }
.pg .flag {
  position: absolute; left: 7px; bottom: 7px; display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 4px 2px 8px; border-radius: 6px; background: var(--warn); color: #fff;
  font-size: 11px; font-weight: 600;
}
.pg .flag button {
  border: 0; border-radius: 4px; padding: 1px 6px; background: rgba(255, 255, 255, .25); color: #fff;
  font-size: 10.5px; font-weight: 600;
}
.pg .flag button:hover { background: rgba(255, 255, 255, .4); }
#pages { display: flex; flex-wrap: wrap; gap: 22px 18px; align-content: flex-start; }
.pg { display: flex; flex-direction: column; align-items: center; gap: 7px; animation: appear .25s ease-out; }
@keyframes appear { from { opacity: 0; transform: translateY(6px) scale(.98); } }
.sheet {
  position: relative; border-radius: 3px; overflow: hidden; background: #fff;
  box-shadow: var(--shadow-sheet);
  transition: transform .15s ease, box-shadow .15s ease;
}
.pg:hover .sheet { transform: translateY(-2px); }
.sheet img { display: block; height: 250px; min-width: 150px; }
.pg .num { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tools {
  position: absolute; top: 7px; right: 7px; display: flex; gap: 4px;
  opacity: 0; transform: translateY(-3px); transition: opacity .12s, transform .12s;
}
.pg:hover .tools, .tools:focus-within { opacity: 1; transform: none; }
.tools button {
  display: grid; place-items: center; width: 26px; height: 26px; border: 0; border-radius: 7px;
  background: rgba(20, 22, 20, .72); color: #fff;
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
.tools button:hover { background: rgba(20, 22, 20, .9); }
.tools button.del:hover { background: var(--danger); }
.tools svg { width: 14px; height: 14px; }

/* Platzhalter, während der Scanner arbeitet */
.sheet.scanning { width: 177px; height: 250px; }
.sheet.scanning::before {
  content: ""; position: absolute; inset: 22px 20px;
  background: repeating-linear-gradient(#0000 0 9px, #e8e8e3 9px 13px);
  opacity: .8;
}
.sheet.scanning::after {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: 36px;
  background: linear-gradient(#0000, rgba(23, 128, 90, .28) 85%, #17805a 85%, #17805a 100%);
  animation: sweep 1.8s ease-in-out infinite alternate;
}
@keyframes sweep { from { transform: translateY(-36px); } to { transform: translateY(250px); } }

#empty {
  margin: auto; max-width: 320px; text-align: center; color: var(--muted);
  padding-bottom: 20px;
}
#empty h4 { margin: 10px 0 4px; font-size: 15px; font-weight: 600; color: var(--ink); }
#empty p { margin: 0; }
#empty b { color: var(--ink); font-weight: 600; }
.illu { width: 160px; height: 120px; }
.illu .sheet-b { fill: var(--well); stroke: var(--line-strong); }
.illu .sheet-f { fill: var(--surface); stroke: var(--line-strong); }
.illu .lines { stroke: var(--line-strong); stroke-width: 3; stroke-linecap: round; }
.illu .beam { fill: var(--accent); animation: beam 2.6s ease-in-out infinite alternate; }
@keyframes beam { from { transform: translateY(-48px); } to { transform: translateY(18px); } }

/* ---------------------------------------------------------------- Aktionsleiste */
.actionbar {
  display: flex; align-items: center; gap: 8px;
  padding: 11px 14px; border-top: 1px solid var(--line); background: var(--panel);
}
.title-field {
  flex: 1; min-width: 120px; display: flex; align-items: center; gap: 8px;
  height: 36px; padding: 0 11px; border-radius: 9px;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--muted);
}
.title-field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.title-field input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
  font: inherit; font-size: 13.5px; color: var(--ink); user-select: text;
}
.btn {
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
  height: 36px; padding: 0 14px; border-radius: 9px;
  border: 1px solid var(--line-strong); background: var(--surface); font-weight: 550;
  transition: background .12s, border-color .12s;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: transparent; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover:not(:disabled) { background: var(--well); color: var(--ink); border-color: transparent; }
.btn:disabled { opacity: .45; }
@media (max-width: 760px) {
  #bclear span, #bpdf span { display: none; }
  #bclear, #bpdf { padding: 0 10px; }
}

/* ---------------------------------------------------------------- Meldungen */
#toast {
  position: fixed; left: 50%; bottom: 72px; z-index: 10;
  max-width: min(560px, calc(100vw - 40px));
  padding: 9px 14px; border-radius: 10px;
  background: #1f231f; color: #f2f3ef; font-weight: 500;
  box-shadow: 0 10px 30px -8px rgba(0, 0, 0, .45);
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s; user-select: text;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast.ok { border-left: 3px solid #2fbf85; }
#toast.err { border-left: 3px solid #ef6b63; cursor: pointer; }
@media (prefers-color-scheme: dark) { #toast { background: #eceee9; color: #1c1d1a; } }

/* ---------------------------------------------------------------- Einstellungen */
dialog {
  width: min(520px, calc(100vw - 40px)); max-height: calc(100vh - 60px);
  padding: 0; border: 1px solid var(--line-strong); border-radius: 14px;
  background: var(--panel); color: var(--ink);
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .45);
}
dialog[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
dialog::backdrop { background: rgba(10, 12, 10, .35); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }
dialog form { display: flex; flex-direction: column; }
dialog header { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 4px 22px; }
dialog h2 { margin: 0; font-size: 16px; font-weight: 650; }
dialog section { padding: 12px 22px 6px; display: flex; flex-direction: column; gap: 10px; }
dialog footer {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 14px 18px; margin-top: 8px; border-top: 1px solid var(--line);
}
.sec-head { display: flex; align-items: center; justify-content: space-between; }
.sec-head h3 { margin: 0; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.link-btn {
  display: inline-flex; align-items: center; gap: 5px; border: 0; background: none; padding: 2px 4px;
  color: var(--accent); font-weight: 550; font-size: 12px;
}
.link-btn:disabled { opacity: .5; }
.link-btn.spin svg { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.devices { display: flex; flex-direction: column; gap: 6px; }
.device {
  display: flex; align-items: center; gap: 12px; text-align: left;
  padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line-strong); background: var(--surface);
}
.device:hover { border-color: var(--muted); }
.device[aria-pressed=true] { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.device .ico {
  display: grid; place-items: center; width: 34px; height: 34px; border-radius: 8px;
  background: var(--well); color: var(--muted); flex: none;
}
.device[aria-pressed=true] .ico { background: var(--accent); color: #fff; }
.device .ico svg { width: 19px; height: 19px; }
.device .txt { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.device .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device .meta { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  font-size: 10.5px; font-weight: 600; padding: 2px 7px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); flex: none;
}
.devices .placeholder {
  display: flex; align-items: center; gap: 10px; padding: 12px; border-radius: 10px;
  border: 1px dashed var(--line-strong); color: var(--muted);
}
.devices .placeholder .bar {
  width: 40px; height: 4px; border-radius: 2px; background: var(--well); position: relative; overflow: hidden;
}
.devices .placeholder .bar::after {
  content: ""; position: absolute; inset: 0; width: 40%; border-radius: 2px; background: var(--accent);
  animation: slide 1s ease-in-out infinite alternate;
}
@keyframes slide { to { transform: translateX(150%); } }

.field { display: flex; flex-direction: column; gap: 5px; font-size: 12px; font-weight: 550; }
.field em { font-style: normal; font-weight: 400; color: var(--muted); }
.field input {
  height: 34px; padding: 0 11px; border-radius: 8px;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  font: inherit; font-size: 13px; font-weight: 400; outline: 0; user-select: text;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.settings-msg { margin: 4px 22px 0; font-size: 12px; min-height: 0; user-select: text; }
.settings-msg:empty { display: none; }
.settings-msg.err { color: var(--danger); }
.settings-msg.ok { color: var(--accent); }
.settings-msg.info { color: var(--muted); }

/* ---------------------------------------------------------------- Anmeldung & Server-Hinweise */
[hidden] { display: none !important; }
.managed { font-size: 11px; color: var(--muted); }
.field input:disabled { opacity: .6; }
.login { width: min(360px, calc(100vw - 32px)); }
.login form { align-items: center; text-align: center; padding: 28px 24px 22px; gap: 10px; }
.login h2 { margin: 6px 0 0; font-size: 18px; }
.login p { margin: 0 0 6px; color: var(--muted); }
.login .field { width: 100%; }
.login .settings-msg { margin: 0; }
.logo.big { width: 52px; height: 52px; border-radius: 13px; }
.logo.big svg { width: 30px; height: 30px; }
.btn.wide { width: 100%; justify-content: center; height: 40px; }

/* ---------------------------------------------------------------- Touch: Werkzeuge immer sichtbar */
@media (hover: none) {
  .tools { opacity: 1; transform: none; }
  .tools button { width: 32px; height: 32px; }
  .pg:hover .sheet { transform: none; }
}

/* ---------------------------------------------------------------- Handy */
@media (max-width: 700px) {
  body { font-size: 15px; }
  .titlebar {
    height: auto; min-height: 52px;
    padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
  }
  .brand > span:last-child { display: none; }
  .chip { max-width: 44vw; height: 30px; }

  /* Einstellungen oben, Seiten darunter – alles in einer Scrollfläche */
  main { display: flex; flex-direction: column; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .panel {
    border-right: 0; border-bottom: 1px solid var(--line); overflow: visible;
    display: grid; grid-template-columns: 1fr 1fr; gap: 14px 12px; padding: 14px 14px 16px;
  }
  .panel section:first-child, .panel .spacer, .scan-btn, .backs-btn, .hint { grid-column: 1 / -1; }
  .panel .spacer { display: none; }
  .seg.vertical { grid-auto-flow: column; }
  .seg.vertical button { justify-content: center; gap: 6px; padding: 0 6px; height: 40px; }
  .seg.vertical button span { font-size: 13px; }
  .seg button { height: 36px; }
  .scan-btn { height: 54px; font-size: 16px; border-radius: 13px; }
  .backs-btn { margin-top: 0; height: 46px; }
  .hint { margin: -4px 0 0; }

  .stage { overflow: visible; padding: 16px 14px 20px; flex: 1; }
  #pages { gap: 18px 14px; }
  .pg { width: calc(50% - 7px); }
  .sheet { width: 100%; }
  .sheet img { width: 100%; height: auto; min-width: 0; }
  .sheet.scanning { width: 100%; height: auto; aspect-ratio: 210 / 297; }
  @keyframes sweep { from { transform: translateY(-36px); } to { transform: translateY(calc(50vw * 1.414)); } }
  #empty { margin: 24px auto; }

  .actionbar {
    flex-wrap: wrap; gap: 8px;
    padding: 10px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  .title-field { flex: 1 0 100%; height: 42px; }
  .title-field input, .field input { font-size: 16px; } /* iOS zoomt sonst beim Antippen */
  .btn { height: 42px; }
  #bsend { flex: 1; justify-content: center; }
  #bclear span, #bpdf span { display: none; }
  #toast { bottom: calc(120px + env(safe-area-inset-bottom)); }

  dialog { max-height: calc(100dvh - 40px); }
  dialog section { padding: 10px 16px 4px; }
  dialog header { padding: 14px 12px 2px 16px; }
  .settings-msg { margin: 4px 16px 0; }
  .device { padding: 12px; }
}
