:root {
    --coffee-dark: #3b2317;
    --coffee: #6f4e37;
    --coffee-light: #a97c50;
    --cream: #f7f1e8;
    --frost: #7fb3d5;
    --used: #999;
    --danger: #b3452c;

    /* Semantische Tokens -- der Rest des Stylesheets referenziert nur noch diese,
       damit der Dark-Mode-Block unten an einer Stelle alles umschalten kann. */
    --bg: var(--cream);
    --surface: #ffffff;
    --text: var(--coffee-dark);
    --text-muted: #555;
    --text-faint: #808080;
    --border: #eee;
    --border-strong: #ccc;
    --shadow: rgba(0,0,0,0.08);

    --header-bg: var(--coffee-dark);
    --header-text: var(--cream);
    --button-bg: var(--coffee);
    --button-text: #ffffff;
    --button-hover: var(--coffee-dark);
    --link-color: var(--coffee);
    --link-hover: var(--coffee-dark);

    --cancel-bg: #ccc;
    --cancel-text: #333;
    --modal-overlay: rgba(0,0,0,0.4);

    --badge-bg: #eee;
    --status-frisch-bg: #dff0d8;
    --status-frisch-text: #3c763d;
    --status-eingefroren-bg: #d9edf7;
    --status-eingefroren-text: #31708f;

    --reminder-bg: #fdf3d7;
    --reminder-border: #e0c46a;
    --reminder-text: #7a5c00;
    --error-bg: #fbe4e0;

    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1b1512;
        --surface: #29211b;
        --text: #f1e6da;
        --text-muted: #c2b3a3;
        --text-faint: #93816f;
        --border: #443626;
        --border-strong: #5c4c3a;
        --shadow: rgba(0,0,0,0.45);

        --header-bg: #120d0a;
        --header-text: #f1e6da;
        --button-bg: #8a6349;
        --button-text: #fff2e6;
        --button-hover: #a97c50;
        --link-color: #c99b6c;
        --link-hover: #e0b382;

        --cancel-bg: #443626;
        --cancel-text: #f1e6da;
        --modal-overlay: rgba(0,0,0,0.65);

        --badge-bg: #3a2f24;
        --status-frisch-bg: #234a2c;
        --status-frisch-text: #93e0a5;
        --status-eingefroren-bg: #1e3c52;
        --status-eingefroren-text: #92cdf0;

        --reminder-bg: #4a3a12;
        --reminder-border: #8a6d1f;
        --reminder-text: #ecd48f;
        --error-bg: #4a241c;
        --danger: #e2725a;
        --used: #a99c8f;
    }
}

* { box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    margin: 0;
    padding: 0 1rem 3rem;
    /* Safe-Area-Insets: nur relevant, wenn als PWA vom Home-Bildschirm gestartet
       (viewport-fit=cover + apple-mobile-web-app-status-bar-style=black-translucent),
       im normalen Browser-Tab ist env(...) einfach 0. */
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
    padding-bottom: max(3rem, env(safe-area-inset-bottom));
}

header {
    background: var(--header-bg);
    color: var(--header-text);
    padding: max(1rem, env(safe-area-inset-top)) 1rem 1rem;
    margin: 0 -1rem 1.5rem;
}
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
header h1 { margin: 0; font-size: 1.3rem; }
header a { color: var(--header-text); text-decoration: none; }
.logout-form { margin: 0; }
.logout-form button { background: var(--button-bg); font-size: 0.8rem; padding: 0.35rem 0.8rem; }
.logout-form button:hover { background: var(--coffee-light); }

.login-wrapper {
    background: var(--surface);
    max-width: 360px;
    margin: 3rem auto;
    padding: 1.8rem 2rem;
    border-radius: 10px;
    box-shadow: 0 1px 4px var(--shadow);
}
.login-wrapper h2 { margin-top: 0; }
.login-form label { display: block; font-size: 0.9rem; margin-bottom: 1rem; }
.login-form input {
    display: block;
    width: 100%;
    margin-top: 0.3rem;
    padding: 0.5rem;
    border: 1px solid var(--border-strong);
    border-radius: 4px;
    background: var(--surface);
    color: var(--text);
}

.toolbar { margin-bottom: 1rem; display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; }
.search-input {
    padding: 0.45rem 0.7rem;
    border: 1px solid var(--border-strong);
    border-radius: 6px;
    font-size: 0.9rem;
    flex: 1 1 200px;
    max-width: 320px;
    background: var(--surface);
    color: var(--text);
}

.reminder-banner {
    background: var(--reminder-bg);
    border: 1px solid var(--reminder-border);
    color: var(--reminder-text);
    padding: 0.8rem 1.2rem;
    border-radius: 8px;
    margin-bottom: 1.2rem;
    font-size: 0.9rem;
}
.reminder-banner ul { margin: 0.4rem 0 0; padding-left: 1.2rem; }

.history-table { background: var(--surface); border-radius: 8px; }

button {
    background: var(--button-bg);
    color: var(--button-text);
    border: none;
    border-radius: 6px;
    padding: 0.45rem 0.9rem;
    cursor: pointer;
    font-size: 0.9rem;
}
button:hover { background: var(--button-hover); }
button.link-button { background: none; color: var(--link-color); padding: 0.3rem 0; text-decoration: underline; }
button.link-button:hover { color: var(--link-hover); background: none; }
button.tiny { padding: 0.1rem 0.4rem; font-size: 0.75rem; background: var(--danger); }

.bean-card {
    background: var(--surface);
    border-radius: 10px;
    padding: 1.2rem 1.5rem;
    margin-bottom: 1.2rem;
    box-shadow: 0 1px 4px var(--shadow);
}

.bean-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.bean-header h2 { margin: 0 0 0.2rem; }
.roastery { color: var(--coffee-light); font-size: 0.9rem; }

.bean-actions { display: flex; align-items: center; gap: 0.5rem; }
.total-amount { font-weight: 600; margin-right: 0.5rem; }

.notes { color: var(--text-muted); font-style: italic; margin: 0.5rem 0; }
.brew-recipe {
    color: var(--text);
    background: var(--badge-bg);
    display: inline-block;
    padding: 0.3rem 0.7rem;
    border-radius: 6px;
    font-size: 0.85rem;
    margin: 0.3rem 0 0.5rem;
}

.batch-table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.5rem 0;
    font-size: 0.9rem;
}
.batch-table th, .batch-table td {
    text-align: left;
    padding: 0.4rem 0.5rem;
    border-bottom: 1px solid var(--border);
}
/* flex+nowrap statt reinem Inline-Fluss: bei wrap (oder default inline) lässt der
   Browser die Buttons lieber einzeln untereinander in einer schmalen Spalte
   umbrechen, statt die Tabelle (via .table-scroll) horizontal scrollen zu lassen --
   das sah auf schmalen Screens wie abgerissene, frei schwebende Buttons aus.
   Mit nowrap bleibt die Actions-Zelle immer eine Zeile breit, wodurch die Tabelle
   insgesamt breiter als der Viewport wird und stattdessen seitlich scrollt. */
.batch-actions {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.3rem;
}
.batch-actions button, .batch-actions .label-link { padding: 0.3rem 0.6rem; font-size: 0.8rem; margin-right: 0; flex: none; }
.label-link {
    display: inline-block;
    margin-right: 0.3rem;
    padding: 0.3rem 0.6rem;
    font-size: 0.8rem;
    background: var(--coffee-light);
    color: #ffffff;
    border-radius: 6px;
    text-decoration: none;
}
.label-link:hover { background: var(--button-hover); }

.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 12px;
    font-size: 0.75rem;
    background: var(--badge-bg);
}
tr.status-frisch .badge { background: var(--status-frisch-bg); color: var(--status-frisch-text); }
tr.status-eingefroren .badge { background: var(--status-eingefroren-bg); color: var(--status-eingefroren-text); }
tr.status-aufgebraucht .badge { background: var(--badge-bg); color: var(--used); }
tr.status-aufgebraucht td { color: var(--used); }

.grinder-list { list-style: none; padding: 0; margin: 0.3rem 0; }
.grinder-list li { padding: 0.2rem 0; font-size: 0.9rem; }
.grinder-notes { color: var(--text-faint); font-size: 0.85rem; }
.grinder-name {
    display: inline-block;
    background: var(--badge-bg);
    color: var(--coffee-light);
    padding: 0.1rem 0.5rem;
    border-radius: 10px;
    font-size: 0.78rem;
    font-weight: 600;
}

.empty { color: var(--text-faint); font-style: italic; }
.muted { color: var(--text-faint); font-size: 0.85rem; }

.error-box {
    background: var(--error-bg);
    border: 1px solid var(--danger);
    color: var(--danger);
    padding: 0.7rem 1rem;
    border-radius: 6px;
    margin: 0.8rem 0;
}

.upload-form {
    background: var(--surface);
    padding: 1.2rem;
    border-radius: 10px;
    display: flex;
    gap: 1rem;
    align-items: center;
    margin: 1rem 0;
}

.invoice-meta { color: var(--text-muted); margin-bottom: 1rem; }

.invoice-preview-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border-radius: 8px;
    overflow: hidden;
}
.invoice-preview-table th, .invoice-preview-table td {
    text-align: left;
    padding: 0.6rem 0.7rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}
.invoice-preview-table select, .invoice-preview-table input {
    padding: 0.3rem;
    border: 1px solid var(--border-strong);
    border-radius: 4px;
    background: var(--surface);
    color: var(--text);
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: var(--modal-overlay);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.modal-box {
    background: var(--surface);
    border-radius: 10px;
    padding: 1.5rem 2rem;
    width: min(420px, 90vw);
}
.modal-box h2 { margin-top: 0; }
.modal-box label {
    display: block;
    margin-bottom: 0.8rem;
    font-size: 0.9rem;
}
.modal-box input, .modal-box textarea, .modal-box select {
    display: block;
    width: 100%;
    margin-top: 0.2rem;
    padding: 0.4rem;
    border: 1px solid var(--border-strong);
    border-radius: 4px;
    font-size: 0.95rem;
    background: var(--surface);
    color: var(--text);
}
.modal-buttons { display: flex; gap: 0.5rem; margin-top: 1rem; }
.modal-buttons button[type="button"] { background: var(--cancel-bg); color: var(--cancel-text); }

/* Tabellen mit vielen Spalten auf schmalen Screens seitlich scrollen lassen,
   statt das Layout zu sprengen. */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ---------- Chargen-Detailseite (Ziel des QR-Codes) ---------- */

.detail-page { max-width: 480px; margin: 0 auto; }

.detail-card {
    background: var(--surface);
    border-radius: 10px;
    padding: 1.2rem 1.3rem;
    margin-bottom: 1rem;
    box-shadow: 0 1px 4px var(--shadow);
}
.detail-card h2 { margin: 0 0 0.2rem; }
.detail-card h3 { margin-top: 0; }

.detail-facts {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.9rem 1rem;
    margin: 1.1rem 0 0.2rem;
    padding: 0;
}
.detail-facts dt { font-size: 0.75rem; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.03em; margin: 0; }
.detail-facts dd { margin: 0.15rem 0 0; font-size: 1.1rem; font-weight: 600; }

.detail-actions { display: flex; flex-direction: column; gap: 0.7rem; margin-top: 1.3rem; }
.detail-actions form { margin: 0; }
.big-button {
    display: block;
    width: 100%;
    text-align: center;
    padding: 0.85rem;
    font-size: 1rem;
    border-radius: 8px;
    border: none;
}
a.big-button.label-link { background: var(--coffee-light); }

.inline-form { display: inline; margin-left: 0.3rem; }
.inline-form button { vertical-align: middle; }

.grinder-add { margin-top: 0.8rem; }
.grinder-add summary { cursor: pointer; color: var(--link-color); font-weight: 600; padding: 0.3rem 0; }
.grinder-add form { margin-top: 0.8rem; }
.grinder-add label {
    display: block;
    margin-bottom: 0.8rem;
    font-size: 0.9rem;
}
.grinder-add input, .grinder-add textarea, .grinder-add select {
    display: block;
    width: 100%;
    margin-top: 0.2rem;
    padding: 0.5rem;
    border: 1px solid var(--border-strong);
    border-radius: 4px;
    font-size: 1rem;
    background: var(--surface);
    color: var(--text);
}

/* ---------- Mobile (iPhone & Co.) ---------- */

@media (max-width: 480px) {
    body {
        padding: 0 0.7rem 3rem;
        padding-left: max(0.7rem, env(safe-area-inset-left));
        padding-right: max(0.7rem, env(safe-area-inset-right));
        padding-bottom: max(3rem, env(safe-area-inset-bottom));
        font-size: 16px;
    }
    header {
        margin: 0 -0.7rem 1.2rem;
        padding: max(0.8rem, env(safe-area-inset-top)) 1rem 0.8rem;
    }
    header h1 { font-size: 1.1rem; }

    .toolbar { flex-direction: column; align-items: stretch; }
    .toolbar button { width: 100%; }
    .search-input { max-width: none; }

    .bean-card { padding: 1rem 1.1rem; }
    .bean-header { flex-direction: column; align-items: stretch; }
    .bean-actions { justify-content: flex-end; }

    .modal-box { padding: 1.3rem 1.2rem; width: min(420px, 92vw); }

    .upload-form { flex-direction: column; align-items: stretch; }

    /* Touch-Ziele nach Apple-HIG-Empfehlung (min. ~44pt) komfortabel halten */
    button, .label-link, .big-button { min-height: 2.6rem; }
}
