/* ------------------------------------------------------------------
   Einkaufsliste – Stylesheet
   Mobile-first, Dark-Mode via prefers-color-scheme.
------------------------------------------------------------------ */

:root {
    --bg: #f7f8f7;
    --bg-elev: #ffffff;
    --bg-sticky: rgba(247, 248, 247, 0.92);
    --fg: #0f172a;
    --fg-muted: #64748b;
    --border: #e2e8f0;
    --accent: #16a34a;
    --accent-fg: #ffffff;
    --accent-soft: #dcfce7;
    --danger: #dc2626;
    --warning: #f59e0b;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.08);
    --radius: 14px;
    --radius-sm: 10px;
    --tap: 48px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0a0f0c;
        --bg-elev: #131a16;
        --bg-sticky: rgba(10, 15, 12, 0.92);
        --fg: #e6efe9;
        --fg-muted: #8a9a91;
        --border: #1f2a23;
        --accent: #22c55e;
        --accent-fg: #06150c;
        --accent-soft: #0b3d2e;
        --danger: #ef4444;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 16px rgba(0, 0, 0, 0.5);
    }
}

* {
    box-sizing: border-box;
}

[hidden] { display: none !important; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.4;
    -webkit-tap-highlight-color: transparent;
    overflow-x: hidden;
}

body {
    min-height: 100vh;
    padding-bottom: calc(120px + var(--safe-bottom));
}

button, input, select {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}

button {
    cursor: pointer;
    border: none;
    background: none;
    padding: 0;
    line-height: 1;
}

/* ------------------------------------------------------------------ Topbar */

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    background: var(--bg-sticky);
    backdrop-filter: saturate(180%) blur(10px);
    -webkit-backdrop-filter: saturate(180%) blur(10px);
    border-bottom: 1px solid var(--border);
    padding-top: var(--safe-top);
}

.topbar-search {
    display: flex;
    gap: 8px;
    padding: 10px 16px 0;
    align-items: center;
}

.topbar-search .autocomplete {
    flex: 1;
    position: relative;
}

.topbar-search input[type="text"] {
    width: 100%;
    min-height: var(--tap);
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-elev);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.topbar-search input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.topbar-title-row {
    display: flex;
    align-items: center;
    min-height: var(--tap);
}

.topbar-search--title {
    justify-content: space-between;
    align-items: center;
    min-height: var(--tap);
}

.topbar-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 8px 16px 10px;
    gap: 16px;
}

.title {
    margin: 0;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.totals {
    text-align: right;
    line-height: 1.1;
}

.total-main {
    display: block;
    font-size: 20px;
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

.total-sub {
    display: block;
    font-size: 11px;
    color: var(--fg-muted);
    font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------------------ Bottom tabs */

.tabs {
    display: flex;
    gap: 0;
    padding: 0;
}

.tab {
    flex: 1;
    padding: 8px 4px calc(8px + var(--safe-bottom));
    border-radius: 0;
    font-weight: 600;
    font-size: 11px;
    color: var(--fg-muted);
    background: transparent;
    transition: color 0.15s;
    min-height: var(--tap);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
}

.tab svg {
    display: block;
    fill: currentColor;
    flex-shrink: 0;
}

.tab.active {
    color: var(--accent);
}

/* ------------------------------------------------------------------ Main */

.content {
    padding: 12px 16px 0;
    max-width: 760px;
    margin: 0 auto;
}

.view[hidden] {
    display: none;
}

/* ------------------------------------------------------------------ Input row */

.input-row {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    position: relative;
}

.autocomplete {
    flex: 1;
    position: relative;
}

.input-row input[type="text"] {
    width: 100%;
    min-height: var(--tap);
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-elev);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.input-row input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn-primary, .btn-secondary, .btn-finish, .btn-danger {
    min-height: var(--tap);
    min-width: var(--tap);
    padding: 0 16px;
    border-radius: var(--radius);
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: transform 0.05s, opacity 0.15s, background 0.15s;
}

/* Icon-Größen (Font-Awesome-SVGs). Nachfahren-Selektor + explizite
   Größe/Display, damit die inline-SVGs immer sichtbar rendern. */
.btn-primary svg,
.btn-secondary svg,
.icon-btn svg,
.qty-stepper button svg,
.edit-pill svg,
.check svg,
.empty-icon svg {
    display: block;
    fill: currentColor;
    flex-shrink: 0;
}

.btn-primary svg,
.btn-secondary svg {
    width: 20px;
    height: 20px;
}

.icon-btn svg {
    width: 16px;
    height: 16px;
}

.qty-stepper button svg {
    width: 14px;
    height: 14px;
}

.edit-pill svg {
    width: 12px;
    height: 12px;
}

.empty-icon svg {
    width: 48px;
    height: 48px;
    margin: 0 auto;
}

.btn-primary:active, .btn-secondary:active, .btn-finish:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--accent);
    color: var(--accent-fg);
    font-size: 22px;
}

.btn-secondary {
    background: var(--bg-elev);
    color: var(--fg);
    border: 1px solid var(--border);
    font-size: 22px;
}

.btn-danger {
    background: var(--danger);
    color: white;
}

.btn-finish:disabled {
    background: var(--border);
    color: var(--fg-muted);
    cursor: not-allowed;
}

/* ------------------------------------------------------------------ Autocomplete */

.suggestions {
    list-style: none;
    margin: 4px 0 0;
    padding: 6px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    position: absolute;
    left: 0;
    right: 0;
    z-index: 20;
    max-height: 280px;
    overflow-y: auto;
}

.suggestions li {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    min-height: var(--tap);
}

.suggestions li:hover, .suggestions li.active {
    background: var(--accent-soft);
}

.suggestions li.new-item {
    color: var(--accent);
    font-weight: 600;
}

.suggestions li .sub {
    font-size: 12px;
    color: var(--fg-muted);
}

/* ------------------------------------------------------------------ Liste */

.category-group {
    margin-bottom: 22px;
}

.category-group h2 {
    font-size: 13px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--fg-muted);
    margin: 4px 0 8px;
    padding: 0 4px;
}

.category-group.done h2 {
    color: var(--fg-muted);
    opacity: 0.7;
}

.item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 6px;
    transition: opacity 0.15s, background 0.15s;
    min-height: 64px;
}

.item.checked {
    opacity: 0.55;
}

.item.checked .item-name {
    text-decoration: line-through;
}

.check {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid var(--border);
    background: transparent;
    display: grid;
    place-items: center;
    color: var(--accent-fg);
    transition: background 0.15s, border-color 0.15s;
}

.check.checked {
    background: var(--accent);
    border-color: var(--accent);
}

.check svg {
    width: 14px;
    height: 14px;
    display: block;
    opacity: 0;
    transition: opacity 0.15s;
}

.check.checked svg {
    opacity: 1;
}

.item-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.item-name {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.item-meta {
    font-size: 13px;
    color: var(--fg-muted);
    font-variant-numeric: tabular-nums;
}

.qty-stepper {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.qty-stepper button {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: var(--bg);
    border: 1px solid var(--border);
    font-size: 18px;
    line-height: 1;
    color: var(--fg);
    display: grid;
    place-items: center;
}

.qty-stepper button:active {
    background: var(--accent-soft);
}

.qty-stepper .qty-val {
    min-width: 32px;
    text-align: center;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------------------ Katalog */

.catalog-container {
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.cat-section h2 {
    font-size: 13px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--fg-muted);
    margin: 0 0 8px;
    padding: 0 4px;
}

.tile-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.tile {
    position: absolute;
    inset: 0;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-end;
    gap: 2px;
    text-align: left;
    overflow: hidden;
    transition: transform 0.05s, background 0.15s, border-color 0.15s;
    color: var(--fg);
}

.tile:active {
    transform: scale(0.97);
    background: var(--accent-soft);
    border-color: var(--accent);
}

.tile-name {
    font-weight: 600;
    font-size: 14px;
    line-height: 1.2;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

.tile-meta {
    font-size: 11px;
    color: var(--fg-muted);
}

.tile-price {
    font-size: 12px;
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

.tile-in-cart {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.tile-edit {
    position: absolute;
    margin-left: auto;
}

.tile-wrap {
    position: relative;
    aspect-ratio: 1 / 1;
}

.tile-wrap .edit-pill {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--fg-muted);
    font-size: 12px;
    display: grid;
    place-items: center;
    opacity: 0;
    transition: opacity 0.15s;
}

.tile-wrap:hover .edit-pill,
.tile-wrap:focus-within .edit-pill {
    opacity: 1;
}

/* ------------------------------------------------------------------ Empty */

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--fg-muted);
}

.empty-icon {
    font-size: 48px;
    margin-bottom: 12px;
}

.empty-state p {
    margin: 4px 0;
}

.empty-state .muted {
    font-size: 14px;
}

/* ------------------------------------------------------------------ Bottombar */

.bottombar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-sticky);
    backdrop-filter: saturate(180%) blur(10px);
    -webkit-backdrop-filter: saturate(180%) blur(10px);
    border-top: 1px solid var(--border);
    z-index: 25;
}

.btn-finish {
    width: calc(100% - 32px);
    margin: 10px 16px 6px;
    background: var(--accent);
    color: var(--accent-fg);
    font-size: 16px;
    font-weight: 700;
    border-radius: var(--radius);
    min-height: var(--tap);
}

/* ------------------------------------------------------------------ Sheet */

.sheet[hidden] {
    display: none;
}

.sheet {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    animation: fade-in 0.2s ease;
}

.sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
}

.sheet-panel {
    position: relative;
    background: var(--bg-elev);
    width: 100%;
    max-width: 540px;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    padding: 16px 16px calc(16px + var(--safe-bottom));
    box-shadow: var(--shadow);
    animation: slide-up 0.25s ease;
    max-height: 90vh;
    overflow-y: auto;
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slide-up {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.sheet-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.sheet-header h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    color: var(--fg-muted);
    font-size: 18px;
    display: grid;
    place-items: center;
}

.icon-btn:active {
    background: var(--border);
}

.sheet-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.sheet-form label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13px;
    color: var(--fg-muted);
    font-weight: 600;
}

.sheet-form input, .sheet-form select {
    width: 100%;
    min-height: var(--tap);
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--fg);
    font-weight: 500;
}

.sheet-form input:focus, .sheet-form select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.row-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.sheet-actions {
    display: flex;
    gap: 8px;
    justify-content: space-between;
    margin-top: 8px;
}

.sheet-actions .btn-primary {
    flex: 1;
}

.sheet-actions .btn-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid currentColor;
}

/* ------------------------------------------------------------------ Toast */

.toast {
    position: fixed;
    bottom: calc(96px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    background: var(--fg);
    color: var(--bg);
    padding: 10px 18px;
    border-radius: 999px;
    font-size: 14px;
    font-weight: 600;
    box-shadow: var(--shadow);
    z-index: 200;
    animation: toast-in 0.2s ease;
}

@keyframes toast-in {
    from { opacity: 0; transform: translate(-50%, 10px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

/* ------------------------------------------------------------------ FAB */

.fab {
    position: fixed;
    bottom: calc(80px + var(--safe-bottom));
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--accent-fg);
    box-shadow: 0 4px 16px rgba(0,0,0,0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 20;
    transition: transform 0.1s, box-shadow 0.1s;
}

.fab svg {
    width: 22px;
    height: 22px;
    display: block;
    fill: currentColor;
}

.fab:active {
    transform: scale(0.93);
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

/* ------------------------------------------------------------------ History */

.history-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-top: 4px;
}

.history-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    width: 100%;
    text-align: left;
    gap: 12px;
    transition: background 0.15s;
}

.history-card:active {
    background: var(--accent-soft);
}

.history-card-left {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.history-date {
    font-weight: 600;
    font-size: 15px;
}

.history-meta {
    font-size: 13px;
    color: var(--fg-muted);
}

.history-total {
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
}

.history-detail-group {
    margin-bottom: 16px;
}

.history-detail-group h3 {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--fg-muted);
    margin: 0 0 8px;
}

.history-detail-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
}

.history-detail-name {
    flex: 1;
    font-weight: 500;
}

.history-detail-qty {
    color: var(--fg-muted);
    font-size: 13px;
    flex-shrink: 0;
}

.history-detail-price {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
    min-width: 60px;
    text-align: right;
}

.history-loading, .history-error {
    color: var(--fg-muted);
    text-align: center;
    padding: 40px 0;
}

/* ------------------------------------------------------------------ Breakpoints */

@media (min-width: 720px) {
    .topbar-row {
        padding: 16px 24px 10px;
    }
    .tabs {
        padding: 4px 24px 12px;
    }
    .content {
        padding: 16px 24px 0;
    }
    .title {
        font-size: 24px;
    }
    .tile-grid {
        grid-template-columns: repeat(6, 1fr);
    }
}
