:root {
    --bg: #f5f5f4;
    --surface: #ffffff;
    --surface-2: #fafaf9;
    --border: #e7e5e4;
    --text: #1c1917;
    --text-soft: #57534e;
    --text-mute: #a8a29e;
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-soft: #eef2ff;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --ok: #16a34a;
    --ok-soft: #f0fdf4;
    --warn: #d97706;
    --warn-soft: #fffbeb;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(0,0,0,.05), 0 1px 3px rgba(0,0,0,.08);
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
img { max-width: 100%; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
    width: 220px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem .75rem;
    display: flex;
    flex-direction: column;
    gap: .25rem;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}
.brand {
    font-weight: 700;
    font-size: 1.05rem;
    padding: .35rem .6rem 1rem;
    display: flex; align-items: center; gap: .5rem;
}
.brand .dot { width: 12px; height: 12px; border-radius: 3px; background: var(--primary); }
.nav a {
    display: flex; align-items: center; gap: .6rem;
    padding: .5rem .6rem;
    border-radius: 8px;
    color: var(--text-soft);
    font-weight: 500;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--primary-soft); color: var(--primary-dark); }
.nav .sep { height: 1px; background: var(--border); margin: .5rem .3rem; }
.nav .ico { width: 18px; text-align: center; opacity: .8; }
.sidebar .spacer { flex: 1; }
.sidebar .who { font-size: .8rem; color: var(--text-mute); padding: .4rem .6rem; }
.sidebar-collapse-btn {
    margin-left: auto; border: none; background: none; cursor: pointer; color: var(--text-mute);
    width: 22px; height: 22px; flex: 0 0 auto; border-radius: 6px; font-size: .9rem;
}
.sidebar-collapse-btn:hover { background: var(--surface-2); color: var(--text); }

/* Seitenleiste eingeklappt: nur Icons, deutlich mehr Platz fuer den Inhalt (z. B. Rasteransichten) */
html.sidebar-collapsed .sidebar { width: 60px; padding: 1rem .35rem; }
html.sidebar-collapsed .brand { justify-content: center; padding: .35rem 0 1rem; }
html.sidebar-collapsed .brand-label,
html.sidebar-collapsed .nav a span:not(.ico),
html.sidebar-collapsed .current-doc-form,
html.sidebar-collapsed .who,
html.sidebar-collapsed .logout-label { display: none; }
html.sidebar-collapsed .logout-ico { display: inline !important; }
html.sidebar-collapsed .nav a { justify-content: center; padding: .5rem; }
html.sidebar-collapsed .sidebar-collapse-btn { margin: 0 auto; transform: rotate(180deg); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
    display: none;
    align-items: center; justify-content: space-between;
    padding: .6rem .9rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
}
.content { padding: 1.5rem; max-width: 1100px; width: 100%; margin: 0 auto; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* ---------- Components ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.1rem;
}
.grid { display: grid; gap: 1rem; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid.cols-5 { grid-template-columns: repeat(5, 1fr); }
.grid.cols-6 { grid-template-columns: repeat(6, 1fr); }

.stat { display: flex; flex-direction: column; gap: .2rem; }
.stat .n { font-size: 1.8rem; font-weight: 700; }
.stat .l { font-size: .82rem; color: var(--text-mute); text-transform: uppercase; letter-spacing: .04em; }
.stat.warn .n { color: var(--warn); }
.stat.danger .n { color: var(--danger); }

.btn {
    display: inline-flex; align-items: center; gap: .4rem;
    padding: .5rem .85rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit; font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { background: var(--surface); border-color: #fecaca; color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.ok:hover { filter: brightness(.95); }
.btn.sm { padding: .3rem .55rem; font-size: .85rem; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .7rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-mute); font-weight: 600; }
tbody tr:hover { background: var(--surface-2); }
/* Mehrfachauswahl-Tabelle: ganze Zeile anklickbar zum Markieren (nicht nur die kleine Checkbox) */
#batch-form tbody tr { cursor: pointer; }
#batch-form tbody tr a, #batch-form tbody tr button, #batch-form tbody tr input { cursor: auto; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }

.badge {
    display: inline-flex; align-items: center; gap: .3rem;
    padding: .12rem .5rem;
    border-radius: 999px;
    font-size: .78rem; font-weight: 600;
    background: var(--surface-2); color: var(--text-soft);
    border: 1px solid var(--border);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: #bbf7d0; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: #fde68a; }
.badge.danger { background: var(--danger-soft); color: var(--danger); border-color: #fecaca; }
.badge.info { background: var(--primary-soft); color: var(--primary-dark); border-color: #c7d2fe; }
.dot-color { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.field { margin-bottom: 1rem; }
.field label { display: block; font-weight: 600; font-size: .88rem; margin-bottom: .3rem; }
.field .hint { font-size: .8rem; color: var(--text-mute); margin-top: .25rem; }
.qty-avail-hint { font-size: .78rem; color: var(--danger, #dc2626); margin-top: .25rem; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date],
input[type=search], input[type=color], select, textarea {
    width: 100%;
    padding: .55rem .65rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
    background: var(--surface);
    color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
textarea { min-height: 90px; resize: vertical; }
.row { display: flex; gap: 1rem; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 160px; }

.flash { padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-weight: 500; }
.flash.success { background: var(--ok-soft); color: var(--ok); border: 1px solid #bbf7d0; }
.flash.error { background: var(--danger-soft); color: var(--danger); border: 1px solid #fecaca; }
.flash.info { background: var(--primary-soft); color: var(--primary-dark); border: 1px solid #c7d2fe; }

.muted { color: var(--text-mute); }
.small { font-size: .85rem; }
.stack > * + * { margin-top: 1rem; }
.inline { display: inline; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.tag-list { display: flex; flex-wrap: wrap; gap: .3rem; }

/* thumbnails / gallery */
.thumb { width: 56px; height: 56px; min-width: 56px; min-height: 56px; flex-shrink: 0; border-radius: 6px; object-fit: cover; background: var(--surface-2); border: 1px solid var(--border); font-size: 1.2rem; color: var(--text-mute); }
.thumb.lg { width: 72px; height: 72px; min-width: 72px; min-height: 72px; }
.thumb.xl { width: 84px; height: 84px; min-width: 84px; min-height: 84px; }
.thumb.doc-line { width: 84px; height: 52px; min-width: 84px; min-height: 52px; }
/* Spalte darf bei schmalem Viewport nicht unter die Thumb-Breite schrumpfen (sonst wird das Bild
   in Tabellen mit vielen Spalten auf 0 zusammengequetscht statt ueber .table-wrap zu scrollen). */
.print-photo-col, .doc-line-photo { width: 90px; min-width: 90px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: .6rem; }
.gallery figure { margin: 0; position: relative; }
.gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.gallery .pin { position: absolute; top: 4px; left: 4px; }
.gallery .ops { position: absolute; inset: auto 4px 4px auto; display: flex; gap: 3px; }
.gallery a[data-lightbox] { display: block; cursor: zoom-in; }

/* Lightbox: Foto gross anzeigen, mit Vor/Zurueck bei mehreren Fotos */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 1000; display: flex; align-items: center; justify-content: center; }
.lightbox-img { max-width: 92vw; max-height: 88vh; object-fit: contain; }
.lightbox-close, .lightbox-prev, .lightbox-next {
    position: absolute; background: rgba(0,0,0,.5); color: #fff; border: none; border-radius: 50%;
    font-size: 1.5rem; line-height: 1; cursor: pointer; width: 48px; height: 48px;
}
.lightbox-close { top: 1rem; right: 1rem; }
.lightbox-prev { left: 1rem; top: 50%; transform: translateY(-50%); }
.lightbox-next { right: 1rem; top: 50%; transform: translateY(-50%); }
.lightbox-close:hover, .lightbox-prev:hover, .lightbox-next:hover { background: rgba(0,0,0,.75); }
.lightbox-counter { position: absolute; bottom: 1rem; left: 50%; transform: translateX(-50%); color: #fff; background: rgba(0,0,0,.5); padding: .2rem .7rem; border-radius: 12px; font-size: .85rem; }

.item-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: .8rem; }
.item-card { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.item-card .ph { aspect-ratio: 4/3; background: var(--surface-2); display: flex; align-items: center; justify-content: center; color: var(--text-mute); font-size: 1.6rem; }
.item-card .ph img { width: 100%; height: 100%; object-fit: cover; }
.item-card .body { padding: .7rem .8rem; }
.item-card .body .name { font-weight: 600; }
/* Mehrfachauswahl-Checkbox als Overlay auf Raster-Kacheln (Checkbox liegt NICHT im <a>, sonst
   wuerde ein Klick zugleich die Checkbox umschalten und zur Requisite navigieren). */
.item-card-wrap { position: relative; }
.item-card-check {
    position: absolute; top: .4rem; left: .4rem; z-index: 2;
    display: flex; background: rgba(255,255,255,.92); border-radius: 5px; padding: .25rem; line-height: 0; box-shadow: var(--shadow);
}
/* Regal-Pfad unter einer Requisit-Kachel (Raster) - eigener Link ausserhalb der Karte, damit ein
   Klick darauf ins Regal fuehrt statt (wie bei einem verschachtelten <a>) zur Requisite. */
.item-card-location { display: block; margin-top: .3rem; text-decoration: none; }
.item-card-location:hover { text-decoration: underline; }

/* Unterregale als gleichmaessiges Raster (statt frei umbrechender Pillen) - jede Zelle gleich
   breit/hoch, mit kleinem rundem Vorschaubild davor; lange Namen werden abgeschnitten statt die
   Zelle zu verbreitern, damit das Raster ausgerichtet bleibt. */
.folder-pills { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: .5rem; }
.folder-pill {
    display: flex; align-items: center; gap: .6rem; min-width: 0;
    padding: .35rem .7rem .35rem .35rem; border: 1px solid var(--border); border-radius: 999px;
    background: var(--surface); color: var(--text); text-decoration: none; font-size: .9rem;
}
.folder-pill:hover { background: var(--surface-2); text-decoration: none; }
.folder-pill img, .folder-pill-ico {
    width: 50px; height: 50px; border-radius: 50%; object-fit: cover; flex: 0 0 auto;
    background: var(--surface-2); display: flex; align-items: center; justify-content: center;
}
.folder-pill-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

/* Requisit-Suche (Lieferschein-Editor) */
.search-results {
    position: absolute; z-index: 30; left: 0; right: 0; top: 100%; margin-top: .25rem;
    background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
    box-shadow: var(--shadow); max-height: 320px; overflow-y: auto;
}
.search-results .sr-item {
    display: flex; align-items: center; gap: .6rem; padding: .5rem .6rem;
    cursor: pointer; border-bottom: 1px solid var(--border);
}
.search-results .sr-item:last-child { border-bottom: none; }
.search-results .sr-item:hover { background: var(--surface-2); }
.search-results .sr-name { font-weight: 500; flex: 1; min-width: 0; }
.search-results .sr-empty { padding: .6rem; color: var(--text-mute); font-size: .88rem; }

#quick-capture { margin-top: .8rem; padding-top: .8rem; border-top: 1px solid var(--border); }
#qc-photo-strip img.thumb { width: 56px; height: 56px; }

/* Lieferschein: Kachel-Ansicht fuers Ruecksortieren */
[data-view-toggle].is-active { background: var(--primary); border-color: var(--primary); color: #fff; }
.ls-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; }
.ls-tile { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); transition: opacity .15s; }
.ls-tile.is-done { opacity: .55; }
.ls-tile-photo { position: relative; aspect-ratio: 1; background: var(--surface-2); cursor: pointer; }
.ls-tile-photo img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .1s; }
.ls-tile-photo img.active { opacity: 1; }
.ls-tile-noimg { display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; font-size: 2rem; color: var(--text-mute); }
.ls-tile-count { position: absolute; left: 0; right: 0; bottom: 0; padding: .2rem .5rem; font-size: .72rem; background: rgba(0,0,0,.55); color: #fff; text-align: center; }
.ls-tile-body { padding: .7rem .8rem; display: flex; flex-direction: column; gap: .5rem; }
.ls-tile-name { font-weight: 600; }
.ls-tile-qty { font-size: .85rem; color: var(--text-soft); display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.ls-tile-actions { display: flex; flex-direction: column; gap: .35rem; }
.ls-tile-actions .btn.ok { width: 100%; justify-content: center; font-weight: 600; }
.ls-tile-actions .btn.danger { align-self: flex-start; }

/* Foto-Dropzone (Drag & Drop, Klick, Handy-Kamera) */
.dropzone {
    border: 2px dashed var(--border);
    border-radius: 10px;
    padding: 1.1rem;
    text-align: center;
    background: var(--surface-2);
    transition: border-color .15s, background .15s;
}
.dropzone.dragover { border-color: var(--primary); background: var(--primary-soft); }
.dropzone-label { display: flex; flex-direction: column; align-items: center; gap: .25rem; cursor: pointer; }
.dropzone .dz-icon { font-size: 1.6rem; line-height: 1; }

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1.5rem; }
.login-wrap .card { width: 100%; max-width: 360px; }

/* Ausklappbarer Regal-Baum */
.tree-root details.tnode { margin: 0; }
.tree-root summary,
.tree-root .tleaf {
    display: flex; align-items: center; gap: .5rem;
    padding: .3rem .45rem; border-radius: 6px;
}
.tree-root summary { cursor: pointer; list-style: none; }
.tree-root summary::-webkit-details-marker { display: none; }
.tree-root summary::before {
    content: "\25B8"; /* ▸ */
    /* doppelt so gross wie normaler Text - beim Durchklicken vieler Ordner besser zu treffen */
    color: var(--text-mute); font-size: 1.6rem; width: 1em; flex: 0 0 auto; text-align: center; line-height: 1;
}
.tree-root details[open] > summary::before { content: "\25BE"; /* ▾ */ }
.tree-root summary:hover,
.tree-root .tleaf:hover { background: var(--surface-2); }
.tree-root .tleaf { padding-left: calc(.45rem + 1em + .5rem); }
.tchildren { margin-left: .8rem; padding-left: .5rem; border-left: 1px solid var(--border); }
.tname { font-weight: 500; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tname a { color: inherit; text-decoration: none; }
.tname a.active { color: var(--primary); text-decoration: underline; }
.tcount { font-size: .78rem; color: var(--text-mute); flex: 0 0 auto; }
/* Bearbeiten/+ -Icons sind nur im Bearbeiten-Modus sichtbar (sonst nehmen sie Platz weg und die
   Regalnamen werden - vor allem bei laengeren Namen - abgeschnitten). */
.trow-actions { margin-left: auto; display: none; gap: .25rem; flex: 0 0 auto; }
.locations-tree.edit-mode .trow-actions { display: flex; }

/* Regal-Ansicht: Baum links (fix), Inhalt rechts (wie Sortly). Eigene, breitere Seitenbreite nur hier,
   damit in .item-cards 4 Fotos pro Zeile nebeneinander Platz haben (sonst globale max-width:1100px). */
.content:has(.locations-layout) { max-width: 1450px; }
.locations-layout { display: flex; gap: 1rem; align-items: flex-start; }
.locations-tree { flex: 0 0 340px; max-height: 78vh; overflow-y: auto; position: sticky; top: 1rem; }
.locations-content { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1rem; }
@media (max-width: 760px) {
    .locations-layout { flex-direction: column; }
    .locations-tree { position: static; max-height: none; width: 100%; flex: 0 0 auto; }
}
.iconbtn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 26px; height: 26px; border-radius: 6px;
    border: 1px solid var(--border); background: var(--surface);
    color: var(--text-soft); text-decoration: none; font-size: .8rem; line-height: 1;
}
.iconbtn:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
    .sidebar {
        position: fixed; z-index: 40; left: 0; top: 0; bottom: 0;
        transform: translateX(-100%); transition: transform .2s ease;
        box-shadow: 0 0 40px rgba(0,0,0,.15);
    }
    body.nav-open .sidebar { transform: translateX(0); }
    body.nav-open::after { content: ""; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 35; }
    .topbar { display: flex; }
    .content { padding: 1rem; }
    .grid.cols-2, .grid.cols-3, .grid.cols-4, .grid.cols-5, .grid.cols-6 { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 520px) {
    .grid.cols-3, .grid.cols-4, .grid.cols-5, .grid.cols-6 { grid-template-columns: 1fr; }
    .page-head { align-items: flex-start; }
}

@media print {
    .sidebar, .topbar, .page-head .actions, .no-print { display: none !important; }
    .app, .main, .content { display: block; margin: 0; padding: 0; max-width: none; }
    body { background: #fff; font-size: 12px; }
    .card { box-shadow: none; border: none; }
}
