:root {
  --bg: #1c1c1e;
  --bar: #2c2c2e;
  --fg: #f2f2f7;
  --muted: #8e8e93;
  --accent: #0a84ff;
  --danger: #ff453a;
  --page-shadow: 0 8px 30px rgba(0,0,0,.6);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.topbar, .bottombar, .inspector {
  background: var(--bar);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  flex: 0 0 auto;
}
.topbar { padding-top: calc(8px + var(--sat)); }
.bottombar { padding-bottom: calc(10px + var(--sab)); }
.brand { font-weight: 700; font-size: 17px; margin-right: auto; }

select, input[type=number] {
  background: #3a3a3c;
  color: var(--fg);
  border: 0;
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
}
select { -webkit-appearance: none; appearance: none; padding-right: 22px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 13px) 55%, calc(100% - 8px) 55%;
  background-size: 5px 5px; background-repeat: no-repeat; }
input[type=number] { width: 4.6em; text-align: right; }

.btn {
  border: 0; border-radius: 10px;
  padding: 9px 14px;
  font: inherit; font-weight: 600;
  color: var(--fg); background: #3a3a3c;
  cursor: pointer; touch-action: manipulation;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.subtle { background: transparent; color: var(--muted); }
.btn.wide { flex: 1; padding: 13px; font-size: 16px; }
.btn:disabled { opacity: .45; cursor: default; }
.icon {
  border: 0; border-radius: 10px; width: 42px; height: 40px;
  font-size: 20px; line-height: 1; color: var(--fg); background: #3a3a3c;
  cursor: pointer; touch-action: manipulation;
}
.icon.danger { color: var(--danger); }

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  touch-action: none;
  overflow: hidden;
}
#page {
  position: relative;
  transform-origin: 0 0;
  background: #fff;
  box-shadow: var(--page-shadow);
  overflow: hidden;
  touch-action: none;
}
.margin-guide {
  position: absolute; inset: var(--guide, 0);
  border: 1px dashed rgba(0,0,0,.12);
  pointer-events: none;
}
.empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; color: #666; padding: 24px; pointer-events: none;
}
.empty p { margin: 4px 0; }
.empty .hint { margin-top: 18px; font-size: 13px; color: #999; }

.item {
  position: absolute;
  touch-action: none;
  outline: 0 solid transparent;
}
.item img {
  display: block; width: 100%; height: 100%;
  pointer-events: none;
  -webkit-user-drag: none;
}
.item.selected { outline: 2px solid var(--accent); z-index: 9999 !important; }
.item.selected::after {
  content: attr(data-size);
  position: absolute; left: 0; top: 0;
  background: var(--accent); color: #fff;
  font-size: 11px; padding: 2px 6px; border-radius: 0 0 4px 0;
  white-space: nowrap;
}

.inspector { justify-content: space-between; flex-wrap: wrap; border-top: 1px solid #3a3a3c; }
.inspector label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; }
.inspector .actions { display: flex; gap: 6px; }

.a2hs {
  position: fixed; left: 12px; right: 12px; bottom: calc(76px + var(--sab));
  background: #0a84ff; color: #fff;
  border-radius: 14px; padding: 12px 14px;
  display: flex; align-items: center; gap: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
  font-size: 14px; z-index: 50;
}
.a2hs .icon { background: rgba(255,255,255,.2); color: #fff; width: 36px; height: 36px; flex: 0 0 auto; }
.a2hs .btn.primary { background: #fff; color: var(--accent); }
.a2hs .share { vertical-align: -3px; }

.toast {
  position: fixed; left: 50%; top: calc(70px + var(--sat)); transform: translateX(-50%);
  background: rgba(0,0,0,.85); color: #fff; padding: 10px 16px; border-radius: 12px;
  font-size: 14px; z-index: 60; max-width: 90vw; text-align: center;
}

/* ---- v2: Raster, Seiten, Zuschneiden ---- */
.brand { font-size: 16px; }
.icon.toggle[aria-pressed="true"] { background: var(--accent); color: #fff; }
#page.grid {
  background-color: #fff;
  background-image:
    linear-gradient(to right, rgba(10,132,255,.35) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(10,132,255,.35) 1px, transparent 1px),
    linear-gradient(to right, rgba(10,132,255,.12) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(10,132,255,.12) 1px, transparent 1px);
  background-size: var(--grid10) var(--grid10), var(--grid10) var(--grid10), var(--grid5) var(--grid5), var(--grid5) var(--grid5);
}
.item { overflow: hidden; }
.item img { position: absolute; max-width: none; }

.pagebar {
  background: var(--bar);
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px 0;
}
.pagebar .spacer { flex: 1; }
.pagelabel { min-width: 90px; text-align: center; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.pagebar .icon { width: 38px; height: 36px; }
.pagebar .btn { padding: 8px 12px; }
.bottombar { padding-top: 8px; }
.inspector label input { width: 4.2em; }

.cropper {
  position: fixed; inset: 0; z-index: 100;
  background: #000; color: var(--fg);
  display: flex; flex-direction: column;
}
.cropper-top, .cropper-bottom {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 12px; background: var(--bar);
}
.cropper-top { padding-top: calc(10px + var(--sat)); }
.cropper-bottom { padding-bottom: calc(10px + var(--sab)); flex-wrap: wrap; }
.cropper-title { font-weight: 600; }
.crop-area { position: relative; flex: 1; min-height: 0; touch-action: none; overflow: hidden; }
.crop-area img { position: absolute; max-width: none; pointer-events: none; -webkit-user-drag: none; }
.crop-box {
  position: absolute;
  box-shadow: 0 0 0 9999px rgba(0,0,0,.6);
  outline: 1px solid rgba(255,255,255,.9);
  touch-action: none;
}
.crop-box::before, .crop-box::after {
  content: ""; position: absolute; pointer-events: none; border: 0 solid rgba(255,255,255,.35);
}
.crop-box::before { left: 33.33%; right: 33.33%; top: 0; bottom: 0; border-width: 0 1px; }
.crop-box::after { top: 33.33%; bottom: 33.33%; left: 0; right: 0; border-width: 1px 0; }
.crop-box .h {
  position: absolute; width: 44px; height: 44px; touch-action: none;
}
.crop-box .h::after {
  content: ""; position: absolute; width: 22px; height: 22px;
  border: 3px solid #fff; border-radius: 2px;
}
.h[data-h="tl"] { left: -22px; top: -22px; } .h[data-h="tl"]::after { right: 8px; bottom: 8px; border-right: 0; border-bottom: 0; }
.h[data-h="tr"] { right: -22px; top: -22px; } .h[data-h="tr"]::after { left: 8px; bottom: 8px; border-left: 0; border-bottom: 0; }
.h[data-h="bl"] { left: -22px; bottom: -22px; } .h[data-h="bl"]::after { right: 8px; top: 8px; border-right: 0; border-top: 0; }
.h[data-h="br"] { right: -22px; bottom: -22px; } .h[data-h="br"]::after { left: 8px; top: 8px; border-left: 0; border-top: 0; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 0; border-radius: 999px; padding: 7px 12px; font: inherit; font-size: 13px;
  background: #3a3a3c; color: var(--fg); cursor: pointer; touch-action: manipulation;
}
.chip.active { background: var(--accent); color: #fff; }

.zoomchip {
  position: absolute; right: 12px; bottom: 12px;
  border: 0; border-radius: 999px; padding: 7px 12px;
  font: inherit; font-size: 13px; font-weight: 600;
  background: rgba(0,0,0,.7); color: #fff; cursor: pointer; touch-action: manipulation;
}
/* Inspector behält seinen Platz, sonst springt die Seite beim Antippen eines Bildes */
.inspector[hidden] { display: flex !important; visibility: hidden; }
