/* ===== Design-Tokens ===== */
:root {
  color-scheme: light dark;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --raised: #f7f8fb;
  --text: #12161e;
  --muted: #586174;
  --line: #e2e6ed;
  --accent: #2457f5;
  --accent-fg: #ffffff;
  --accent-soft: #e8eeff;
  --ok: #0f7a55;
  --ok-soft: #e1f5ec;
  --warn: #9a5706;
  --warn-soft: #fdf0d9;
  --danger: #c62828;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
  --tap: 48px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1015;
    --surface: #161a21;
    --raised: #1d222b;
    --text: #eef1f6;
    --muted: #9aa4b5;
    --line: #2a313c;
    --accent: #6c93ff;
    --accent-fg: #0b1020;
    --accent-soft: #1c2744;
    --ok: #47d19f;
    --ok-soft: #12302a;
    --warn: #f2b45f;
    --warn-soft: #352a14;
    --danger: #ff8080;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

/* ===== Basis ===== */
*, *::before, *::after { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* Kein Doppeltipp-Zoom, keine Lupe/Kontextmenü beim langen Drücken auf Bilder (iOS/Android) */
html, body, button, .btn, .fab, .thumb, .rv-img { touch-action: manipulation; }
canvas, .thumb, .rv-img { -webkit-touch-callout: none; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
}
h1 { font-size: 22px; line-height: 1.25; margin: 0; letter-spacing: -.02em; }
h2 { margin: 0; }
p { margin: 0; color: var(--muted); line-height: 1.5; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.i {
  width: 22px; height: 22px; flex: none;
  stroke: currentColor; fill: none; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ===== App-Raster (Mobile zuerst) ===== */
.app {
  position: relative;
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);   /* nie breiter als der Bildschirm (sonst wächst die Spalte auf die Mindestbreite der Kopfzeile) */
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-areas: "top" "stage" "strip" "dock";
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}
@supports (height: 100dvh) { .app { height: 100dvh; } }

.app[data-empty="true"] { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "top" "stage"; }
.app[data-empty="true"] .thumbs,
.app[data-empty="true"] .dock,
.app[data-empty="true"] .progress,
.app[data-empty="true"] #exportBtn { display: none; }

/* ===== Kopfzeile ===== */
.top {
  grid-area: top;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: -.01em; }
.brand .i { color: var(--accent); }
.progress { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--muted); }
.progress span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--ok); border-radius: inherit; transition: width .25s; }
.top-actions { display: flex; gap: 8px; margin-left: auto; }
.top .lbl { display: none; }
.app[data-empty="true"] .top-actions { margin-left: auto; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); min-width: var(--tap);
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit; font-size: 15px; font-weight: 650;
  cursor: pointer;
  transition: background .15s, transform .05s, filter .15s;
}
.btn:hover { background: var(--raised); }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .4; pointer-events: none; }
.btn.icon { padding: 0; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.07); background: var(--accent); }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }

/* ===== Bühne ===== */
.stage { grid-area: stage; min-height: 0; display: flex; flex-direction: column; position: relative; }

.empty {
  margin: auto; max-width: 440px; width: 100%;
  padding: 24px;
  display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center;
}
.hero { width: 96px; height: 96px; border-radius: 28px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.hero .i { width: 46px; height: 46px; stroke-width: 1.7; }
.points { list-style: none; margin: 4px 0 10px; padding: 0; display: grid; gap: 10px; text-align: left; }
.points li { display: flex; gap: 10px; align-items: center; font-size: 14.5px; }
.points .i { width: 20px; height: 20px; color: var(--ok); stroke-width: 2.6; }
.empty .btn.primary { width: 100%; min-height: 56px; border-radius: 16px; font-size: 17px; }
.dropnote { display: none; font-size: 13px; }
@media (hover: hover) and (pointer: fine) { .dropnote { display: block; } }

.editor { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.stage-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px 4px; min-width: 0; }
.fname { flex: 1 1 0; font-weight: 650; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.meta { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.spacer { flex: 1; }
.stage-head .spacer { display: none; }
.stage-head .btn.icon { min-height: 40px; min-width: 40px; border-radius: 10px; }

.canvas-wrap {
  flex: 1; min-height: 0; position: relative; overflow: hidden;
  margin: 4px 12px 12px; border-radius: 14px; background: #14161b; box-shadow: var(--shadow);
}
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: crosshair; }
.float { position: absolute; z-index: 2; display: flex; gap: 6px; }
.float.tl { top: 10px; left: 10px; }
.float.tr { top: 10px; right: 10px; }
.float.br { bottom: 10px; right: 10px; }
.fab {
  display: grid; place-items: center; width: 44px; height: 44px; padding: 0;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow); cursor: pointer;
}
.fab:hover { background: var(--raised); }
.fab:active { transform: scale(.95); }
.fab:disabled { opacity: .4; pointer-events: none; }
.hint {
  position: absolute; bottom: 18px; left: 12px; z-index: 2;
  padding: 8px 14px; border-radius: 99px;
  background: rgba(18, 22, 30, .82); color: #fff; font-size: 13px;
  pointer-events: none; white-space: nowrap;
}

/* ===== Status-Badge ===== */
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 99px;
  font-size: 12px; font-weight: 650; white-space: nowrap;
  background: var(--raised); color: var(--muted); border: 1px solid var(--line);
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge[data-status="wip"] { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge[data-status="done"] { background: var(--ok-soft); color: var(--ok); border-color: transparent; }

/* ===== Miniaturen ===== */
.thumbs {
  grid-area: strip;
  display: flex; gap: 8px; padding: 8px 12px;
  overflow-x: auto; overflow-y: hidden;
  background: var(--surface); border-top: 1px solid var(--line);
  scroll-snap-type: x proximity; scrollbar-width: none;
}
.thumbs::-webkit-scrollbar { display: none; }
.thumb, .thumb-add { flex: none; width: 84px; height: 84px; border-radius: 12px; cursor: pointer; scroll-snap-align: center; }
.thumb { position: relative; padding: 0; overflow: hidden; border: 2px solid transparent; background: var(--raised); }
.thumb canvas { display: block; width: 100%; height: 100%; object-fit: cover; }
.thumb.active { border-color: var(--accent); }
.thumb .num {
  position: absolute; top: 4px; left: 4px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 10px; background: rgba(18, 22, 30, .8); color: #fff;
  font-size: 11px; font-weight: 700; display: grid; place-items: center;
}
.thumb .badge {
  position: absolute; left: 4px; right: 4px; bottom: 4px; justify-content: center;
  padding: 2px 6px; font-size: 10.5px; background: rgba(18, 22, 30, .84); color: #fff; border: 0;
}
.thumb .badge::before { background: #9aa4b5; }
.thumb .badge[data-status="wip"]::before { background: #f2b45f; }
.thumb .badge[data-status="done"]::before { background: #47d19f; }
.thumb .exp {
  position: absolute; top: 4px; right: 4px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--ok); color: #fff; display: grid; place-items: center;
}
.thumb .exp .i { width: 13px; height: 13px; stroke-width: 3; }
.thumb-add { display: grid; place-items: center; border: 2px dashed var(--line); background: transparent; color: var(--muted); }
.thumb-add:hover { background: var(--raised); }

/* ===== Werkzeugleiste unten ===== */
.dock { grid-area: dock; display: grid; gap: 10px; padding: 10px 12px 12px; background: var(--surface); border-top: 1px solid var(--line); }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 14px; background: var(--raised); border: 1px solid var(--line); }
.seg button {
  min-height: 44px; border: 0; border-radius: 10px; background: transparent; color: var(--muted);
  font: inherit; font-weight: 650; display: flex; gap: 8px; align-items: center; justify-content: center; cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.modenote { display: flex; gap: 8px; align-items: flex-start; padding: 8px 10px; border-radius: 10px; font-size: 13px; line-height: 1.4; }
.modenote .i { width: 18px; height: 18px; margin-top: 1px; }
.modenote.safe { background: var(--ok-soft); color: var(--ok); }
.modenote.warn { background: var(--warn-soft); color: var(--warn); font-weight: 600; }
.strength { display: flex; align-items: center; gap: 12px; font-size: 13.5px; color: var(--muted); }
.strength input { flex: 1; height: 32px; accent-color: var(--accent); }
.strength output { min-width: 74px; text-align: right; color: var(--text); font-weight: 650; }
.actions { display: flex; gap: 8px; }
.actions .primary { flex: 1; }
.actions .btn:not(.primary) { }

/* Einspaltige Raster: Spalte nie breiter als der Container */
.dock, .rv-opts, .opt-row, .rv-meta, #rvResult, .dlg-body, .points { grid-template-columns: minmax(0, 1fr); }

/* ===== Hinweise, Dialog, Drag&Drop ===== */
.toast {
  display: none; position: fixed; z-index: 30;
  top: calc(var(--safe-t) + 68px); left: 50%; transform: translateX(-50%);
  max-width: min(92vw, 520px); padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: 14px; box-shadow: var(--shadow); text-align: center;
}
.toast.show { display: block; }
.toast.error { background: var(--danger); color: #fff; }

dialog {
  border: 0; padding: 0; border-radius: 20px;
  background: var(--surface); color: var(--text);
  width: min(440px, calc(100vw - 32px)); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(8, 10, 14, .55); backdrop-filter: blur(3px); }
.dlg-body { padding: 22px 22px 6px; display: grid; gap: 8px; }
.dlg-body h2 { font-size: 18px; letter-spacing: -.01em; }
#dlgText { white-space: pre-line; font-size: 14.5px; max-height: 50vh; overflow: auto; -webkit-user-select: text; user-select: text; }
.dlg-actions { display: flex; gap: 8px; flex-wrap: wrap; padding: 16px 22px 22px; }
.dlg-actions .btn { flex: 1 1 120px; }

.app.dragging::after {
  content: "Bilder hier ablegen"; position: absolute; inset: 8px; z-index: 25;
  display: grid; place-items: center; border-radius: 20px;
  border: 3px dashed var(--accent); background: var(--accent-soft); color: var(--accent);
  font-size: 20px; font-weight: 700; pointer-events: none;
}

/* ===== Abschlusskontrolle ===== */
dialog.sheet, dialog.lightbox {
  width: 100%; height: 100%; max-width: 100%; max-height: 100%;
  margin: 0; padding: 0; border-radius: 0; box-shadow: none;
}
dialog.sheet[open] { display: flex; flex-direction: column; }
.sheet-head { display: flex; align-items: center; gap: 12px; padding: calc(12px + var(--safe-t)) 16px 12px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { flex: 1; font-size: 19px; letter-spacing: -.01em; }
.sheet-body { flex: 1; min-height: 0; overflow: auto; padding: 16px; -webkit-user-select: text; user-select: text; overscroll-behavior: contain; }
.sheet-foot { padding: 12px 16px calc(12px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--surface); }
.foot { display: flex; gap: 8px; flex-wrap: wrap; }
.foot .btn { flex: 1 1 130px; }
.foot .btn.primary { flex: 2 1 200px; }

.lead { color: var(--text); font-weight: 600; margin-bottom: 12px; }
.optnote { font-size: 13px; color: var(--muted); line-height: 1.45; margin: 4px 0 12px; }
#rvWarn { margin-bottom: 12px; }
#rvWarnText, #rvResult .modenote { white-space: pre-line; }
#rvResult { display: grid; gap: 10px; }
#rvResult h3 { margin: 0; font-size: 18px; }
#rvResult p { color: var(--text); }
#rvResult p.optnote { color: var(--muted); margin: 0; }
#rvDeliverNote { margin-top: 12px; }

.rv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.rv-tile { display: flex; flex-direction: column; overflow: hidden; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; transition: opacity .15s; }
.rv-tile.off { opacity: .55; }
.rv-img { position: relative; display: block; width: 100%; aspect-ratio: 4 / 3; padding: 0; border: 0; background: #14161b; cursor: pointer; }
.rv-img canvas { display: block; width: 100%; height: 100%; object-fit: contain; }
.rv-img .seen { position: absolute; right: 6px; bottom: 6px; padding: 2px 8px; border-radius: 99px; background: rgba(18, 22, 30, .85); color: #47d19f; font-size: 11px; font-weight: 700; }
.rv-meta { display: grid; gap: 6px; padding: 8px 10px 10px; }
.rv-pick { display: flex; align-items: center; gap: 10px; min-height: 44px; font-size: 14px; font-weight: 650; cursor: pointer; min-width: 0; }
.rv-pick input { width: 22px; height: 22px; flex: none; accent-color: var(--accent); }
.nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.rv-flags { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 2px 8px; border-radius: 99px; background: var(--raised); border: 1px solid var(--line); color: var(--muted); font-size: 11.5px; }
.chip.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; font-weight: 650; }

.rv-hint { margin: 16px 0; padding: 12px 14px; border-radius: 14px; background: var(--warn-soft); color: var(--warn); font-size: 14px; line-height: 1.45; }
.rv-hint strong { display: block; margin-bottom: 4px; }
.rv-hint ul { margin: 0; padding-left: 20px; display: grid; gap: 4px; }

.rv-opts { display: grid; gap: 10px; }
.opt-row { display: grid; gap: 6px; }
.opt-label { font-size: 13px; font-weight: 650; color: var(--muted); }

.rv-progress { display: grid; align-content: center; justify-items: stretch; gap: 16px; min-height: 60%; text-align: center; }
.rv-progress p { white-space: pre-line; color: var(--text); }
.bar.big { height: 10px; }

dialog.lightbox { background: #0b0d11; color: #fff; }
dialog.lightbox[open] { display: flex; flex-direction: column; }
.lb-head, .lb-foot { display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.lb-head { padding-top: calc(10px + var(--safe-t)); }
.lb-foot { padding-bottom: calc(10px + var(--safe-b)); }
.lb-foot .btn { flex: 1; }
.lb-title { font-size: 14px; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.lightbox .btn { background: #1d222b; color: #fff; border-color: #2a313c; }
.lightbox .btn:hover { background: #262c37; }
.lb-stage { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; position: relative; }
.lb-stage canvas { max-width: 100%; max-height: 100%; object-fit: contain; }
.lb-stage.zoom { display: block; overflow: auto; -webkit-overflow-scrolling: touch; }
.lb-stage.zoom canvas { max-width: none; max-height: none; }
.lb-msg { position: absolute; color: #c8cfdb; font-size: 14px; padding: 0 24px; text-align: center; }
.lb-stage canvas:not([hidden]) ~ .lb-msg { display: none; }

/* ===== Größere Bildschirme ===== */
@media (min-width: 480px) { .top .lbl { display: none; } }
@media (max-width: 399px) { .brand-name { display: none; } }
@media (max-width: 359px) { .stage-head .meta { display: none; } }

@media (min-width: 720px) {
  .top .lbl { display: inline; }
  .top { padding: 12px 20px; }
  .progress { max-width: 360px; margin: 0 auto 0 12px; }
}

@media (min-width: 900px) {
  .app {
    grid-template-columns: 300px minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "top top" "strip stage" "strip dock";
  }
  .app[data-empty="true"] { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "top" "stage"; }

  .thumbs {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); align-content: start; gap: 10px;
    padding: 14px; overflow-x: hidden; overflow-y: auto;
    border-top: 0; border-right: 1px solid var(--line); scroll-snap-type: none;
  }
  .thumb, .thumb-add { width: 100%; height: auto; aspect-ratio: 1; }

  .stage-head { padding: 14px 20px 6px; }
  .canvas-wrap { margin: 6px 20px 16px; }

  .dock { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: 12px 20px; }
  .seg { width: 280px; }
  .modenote { flex: 1 1 260px; }
  .strength { flex: 1 1 240px; }
  .actions { margin-left: auto; }
  .actions .primary { flex: none; }

  dialog.sheet { width: min(1000px, 94vw); height: min(90vh, 860px); margin: auto; border-radius: 20px; box-shadow: var(--shadow); }
  dialog.sheet .sheet-head { padding-top: 16px; }
  dialog.sheet .sheet-foot { padding-bottom: 16px; }
  .rv-grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
  .foot { justify-content: flex-end; }
  .foot .btn, .foot .btn.primary { flex: 0 0 auto; min-width: 150px; }
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
