:root {
    --bg: #f4f2ee;
    --surface: #ffffff;
    --surface-2: #ece8e1;
    --text: #23211d;
    --muted: #6f6a61;
    --border: #ddd7cc;
    --accent: #7a4b2a;
    --accent-text: #ffffff;
    --accent-soft: #f3e6da;
    --ok: #2f6b3a;
    --ok-soft: #e1efe2;
    --warn: #8a5a00;
    --warn-soft: #fbefd2;
    --danger: #a12f2f;
    --danger-soft: #f7e0e0;
    --thumb-bg: radial-gradient(circle at 50% 40%, #ffffff 0%, #e4dfd6 100%);
    --radius: 10px;
    --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .05);
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #17161a;
        --surface: #222126;
        --surface-2: #2c2a31;
        --text: #ece9e4;
        --muted: #a29d95;
        --border: #3a3840;
        --accent: #d39a6a;
        --accent-text: #1b1410;
        --accent-soft: #3a2c22;
        --ok: #8fcf98;
        --ok-soft: #22352a;
        --warn: #f0c060;
        --warn-soft: #3b3020;
        --danger: #f08a8a;
        --danger-soft: #3d2426;
        --thumb-bg: radial-gradient(circle at 50% 40%, #3b3a42 0%, #25242a 100%);
        --shadow: 0 1px 2px rgba(0, 0, 0, .4);
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
body.has-cart { padding-bottom: 80px; }

a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0 0 16px; }
h2 { font-size: 1.15rem; margin: 0 0 8px; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: .9em; word-break: break-all; }

.container { max-width: 1280px; margin: 0 auto; padding: 20px 16px; }
.narrow { max-width: 420px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.text-warn { color: var(--warn); }

/* Top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { max-width: 1280px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); text-decoration: none; margin-right: auto; }
.nav { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.nav a, .link-button {
    color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 6px;
    background: none; border: 0; font: inherit; cursor: pointer;
}
.nav a:hover, .link-button:hover { background: var(--surface-2); color: var(--text); }
.nav a.is-active { color: var(--text); background: var(--surface-2); font-weight: 600; }
.nav-form { margin: 0; }

/* Forms & buttons */
.input {
    font: inherit; color: var(--text); background: var(--surface);
    border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-width: 0;
}
.input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.input:disabled { opacity: .6; }
/* Native option popups do not reliably inherit the theme colours (light text on a light popup in some browsers). */
select.input option { background-color: var(--surface); color: var(--text); }
textarea.input { width: 100%; resize: vertical; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field .input { width: 100%; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; white-space: nowrap; }

.btn {
    font: inherit; font-weight: 500; cursor: pointer; border-radius: 8px; padding: 8px 14px;
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .6; cursor: default; }
.btn-small { padding: 4px 10px; font-size: .85rem; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }
.icon-button { background: none; border: 0; color: var(--muted); font-size: 1.1rem; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-button:hover { background: var(--surface-2); color: var(--text); }

/* Flash messages */
.flashes { padding-bottom: 0; }
.flash { margin: 0 0 8px; padding: 10px 14px; border-radius: 8px; }
.flash-success { background: var(--ok-soft); color: var(--ok); }
.flash-error { background: var(--danger-soft); color: var(--danger); }

/* Panels & stats */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow); }
.panel-accent { border-color: var(--accent); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: .9rem; }
.copy-row { display: flex; gap: 8px; }
.copy-row .input { flex: 1; font-family: ui-monospace, monospace; font-size: .85rem; }
.plain-list { padding-left: 18px; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin: 0 0 12px; }
.inline-form .input { flex: 1 1 220px; }
.dm-row { border-top: 1px solid var(--border); padding: 14px 0 4px; display: flex; flex-direction: column; gap: 8px; }
.dm-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.dm-name-form { margin: 0; flex: 1 1 320px; }
.dm-name { font-weight: 600; }
.nav-user { color: var(--muted); padding: 6px 10px; }
.grant-form { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; margin: 0; padding: 8px 10px; background: var(--surface-2); border-radius: 8px; }
.hidden-list { list-style: none; padding: 0; margin: 0; }
.hidden-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 0; border-top: 1px solid var(--border); }
.hidden-row form { margin: 0; }
.hidden-row .muted { display: block; }

/* Toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar #search { flex: 1 1 220px; }
.toolbar #folder { flex: 0 1 220px; }
.grid-status { color: var(--muted); }

/* Grid & cards */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; }
@media (max-width: 480px) {
    .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
}

.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    overflow: hidden; display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.card.in-cart { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.card.is-short.in-cart { border-color: var(--warn); box-shadow: 0 0 0 1px var(--warn); }
.card.is-saved { animation: saved 1s ease; }
.card.is-error { border-color: var(--danger); }
@keyframes saved { 0% { box-shadow: 0 0 0 2px var(--ok); } 100% { box-shadow: var(--shadow); } }

.card-thumb {
    position: relative; aspect-ratio: 1; padding: 0; border: 0; cursor: zoom-in;
    background: var(--thumb-bg); display: block; width: 100%;
}
.card-thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.card-thumb img:not([src]) { visibility: hidden; }
.card-thumb.is-pending::after, .card-thumb.is-failed::after {
    position: absolute; inset: 0; display: grid; place-items: center;
    color: var(--muted); font-size: .85rem;
}
.card-thumb.is-pending::after { content: "Vorschau wird erzeugt …"; }
.card-thumb.is-failed::after { content: "Keine Vorschau"; }

.card-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.card-title { font-size: .95rem; margin: 0; line-height: 1.3; overflow-wrap: anywhere; }
.card-folder { margin: 0; font-size: .8rem; color: var(--muted); overflow-wrap: anywhere; }
.card-controls { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-top: auto; padding-top: 6px; }

.badge { display: inline-block; align-self: flex-start; font-size: .75rem; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-empty { background: var(--danger-soft); color: var(--danger); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-hidden { background: var(--surface-2); color: var(--muted); border: 1px dashed var(--border); }
.card-badges { display: flex; flex-wrap: wrap; gap: 4px; }
.badge-status-open { background: var(--accent-soft); color: var(--accent); }
.badge-status-packed { background: var(--warn-soft); color: var(--warn); }
.badge-status-done { background: var(--ok-soft); color: var(--ok); }

/* Stepper */
.stepper { display: inline-flex; align-items: stretch; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.stepper-btn { width: 30px; border: 0; background: var(--surface-2); color: var(--text); font-size: 1rem; cursor: pointer; padding: 0; }
.stepper-btn:hover { filter: brightness(.95); }
.stepper-input {
    width: 44px; border: 0; text-align: center; font: inherit; color: var(--text); background: transparent;
    padding: 4px 0; -moz-appearance: textfield; appearance: textfield;
}
.stepper-input::-webkit-outer-spin-button, .stepper-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* Cart bar */
.cart-bar { position: fixed; left: 0; right: 0; bottom: 0; background: var(--surface); border-top: 1px solid var(--border); box-shadow: 0 -2px 10px rgba(0, 0, 0, .08); z-index: 20; }
.cart-bar-inner { max-width: 1280px; margin: 0 auto; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.cart-list { list-style: none; padding: 0; margin: 0 0 16px; max-height: 45vh; overflow: auto; }
.cart-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.cart-info { display: flex; flex-direction: column; min-width: 0; }

/* Dialogs */
.dialog {
    border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text);
    width: min(520px, calc(100vw - 32px)); max-height: calc(100vh - 32px);
}
.dialog-wide { width: min(960px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0, 0, 0, .5); }
.dialog-body { padding: 18px; margin: 0; }
.dialog-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 12px; }
.dialog-head h2 { margin: 0; }
.dialog-head p { margin: 2px 0 0; }
.dialog-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.dialog-actions .muted { margin-right: auto; }
.viewer-stage { height: min(65vh, 620px); border-radius: var(--radius); background: var(--thumb-bg); overflow: hidden; position: relative; touch-action: none; }
.viewer-stage canvas { width: 100%; height: 100%; display: block; }
.viewer-loading { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; color: var(--muted); }

/* Orders */
.order-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.order-head p { margin: 0; }
.order-done { opacity: .75; }
.order-note { background: var(--surface-2); border-radius: 8px; padding: 8px 12px; margin: 10px 0; }
.order-table { width: 100%; border-collapse: collapse; margin: 12px 0; }
.order-table th { text-align: left; font-size: .8rem; color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--border); padding: 6px; }
.order-table td { border-bottom: 1px solid var(--border); padding: 6px; vertical-align: middle; }
.order-table .num { text-align: right; width: 1%; white-space: nowrap; }
.order-table tr.is-short td { background: var(--warn-soft); }
.order-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.order-actions form { margin: 0; }
.item-list { list-style: none; padding: 0; margin: 0; }
.item-row { display: flex; align-items: center; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.item-name { flex: 1; min-width: 0; }
.mini-thumb { width: 44px; height: 44px; object-fit: contain; border-radius: 6px; background: var(--thumb-bg); display: block; }
.mini-thumb-empty { display: inline-block; }

@media (max-width: 560px) {
    .order-table th:first-child, .order-table td:first-child { display: none; }
    .dialog-actions .muted { width: 100%; }
}
