/* ── Design Tokens ── */
:root {
    --bg-app: #f5f4f2;
    --bg-sidebar: #1a1a1a;
    --bg-sidebar-hover: #2a2a2a;
    --bg-sidebar-active: #ffffff;
    --text-main: #1a1a1a;
    --text-muted: #8a8a8a;
    --text-sidebar: #c8c8c8;
    --text-sidebar-active: #1a1a1a;
    --border-light: #ebebeb;
    --shadow-soft: 0 8px 32px rgba(0,0,0,0.07);
    --radius-xl: 24px;
    --radius-lg: 18px;
    --radius-md: 12px;
    --radius-sm: 8px;

    /* Pastel accents */
    --pastel-green: #d4edda;
    --pastel-green-text: #276740;
    --pastel-blue: #dbeafe;
    --pastel-blue-text: #1d4ed8;
    --pastel-rose: #fce7f3;
    --pastel-rose-text: #9d174d;
    --pastel-amber: #fef3c7;
    --pastel-amber-text: #92400e;
    --pastel-purple: #ede9fe;
    --pastel-purple-text: #5b21b6;

    --accent: #5b8def;
    --accent-light: #dbeafe;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    min-height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg-app);
    color: var(--text-main);
    -webkit-font-smoothing: antialiased;
}

/* ── Desktop Layout ── */
.app-layout {
    display: flex;
    min-height: 100vh;
}

/* ── Sidebar (Desktop) ── */
.sidebar {
    width: 240px;
    background: var(--bg-sidebar);
    color: white;
    padding: 24px 16px;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow: hidden;
    flex-shrink: 0;
}

.sidebar-inner {
    display: flex;
    flex-direction: column;
    gap: 28px;
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.sidebar-brand { padding: 4px 8px; }

.sidebar-title {
    margin: 0;
    font-size: 22px;
    font-weight: 700;
    color: white;
    letter-spacing: -0.3px;
}

.sidebar-subtitle {
    margin: 4px 0 0;
    font-size: 12px;
    color: #666;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: var(--text-sidebar);
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 500;
    transition: background 0.15s, color 0.15s;
}

.sidebar-link svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

.sidebar-link:hover {
    background: var(--bg-sidebar-hover);
    color: white;
}

.sidebar-link.active {
    background: white;
    color: #1a1a1a;
}

.sidebar-footer {
    padding: 12px 8px 8px;
    border-top: 1px solid #2a2a2a;
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
    margin-top: 8px;
}

.sidebar-household {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: var(--text-sidebar);
    padding: 8px 10px;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 500;
    border: 1px solid #333;
    transition: background 0.15s, color 0.15s;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-household:hover {
    background: var(--bg-sidebar-hover);
    color: white;
    border-color: transparent;
}

.sidebar-household.active {
    background: white;
    color: #1a1a1a;
    border-color: transparent;
}

.sidebar-user-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 2px 4px;
}

.sidebar-settings-link {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: #777;
    text-decoration: none;
    transition: color 0.15s;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar-settings-link:hover { color: white; }
.sidebar-settings-link.active { color: white; }

.sidebar-logout {
    background: none;
    border: 1px solid #333;
    color: #777;
    padding: 5px 10px;
    border-radius: 7px;
    font-size: 11px;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.15s, color 0.15s;
}

.sidebar-logout:hover {
    background: #2a2a2a;
    color: white;
}

/* ── Main content ── */
.app-content {
    flex: 1;
    padding: 28px;
    min-width: 0;
}

.content-shell {
    min-height: calc(100vh - 56px);
    background: #ffffff;
    border-radius: 24px;
    box-shadow: var(--shadow-soft);
    padding: 28px;
}

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 16px;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    border: 1.5px solid transparent;
    text-decoration: none;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
    white-space: nowrap;
    line-height: 1.2;
}

.btn-primary {
    background: #1a1a1a;
    color: white;
    border-color: #1a1a1a;
}

.btn-primary:hover { background: #333; border-color: #333; }

.btn-secondary {
    background: white;
    color: var(--text-main);
    border-color: var(--border-light);
}

.btn-secondary:hover { background: var(--bg-app); }

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border-color: transparent;
    padding: 9px 4px;
}

.btn-ghost:hover { color: var(--text-main); }

.btn-danger {
    background: #fef2f2;
    color: #b91c1c;
    border-color: #fca5a5;
}

.btn-danger:hover { background: #fee2e2; }

/* ── Page layout ── */
.page {
    max-width: 860px;
    margin: 0 auto;
}

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

.page-title {
    margin: 0;
    font-size: 26px;
    font-weight: 700;
    color: var(--text-main);
    letter-spacing: -0.3px;
}

/* ── Toasts ── */
.toast-container {
    position: fixed;
    top: 24px;
    right: 24px;
    z-index: 5000;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.toast {
    display: flex;
    align-items: center;
    gap: 10px;
    background: #1a1a1a;
    color: white;
    padding: 12px 16px;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 500;
    box-shadow: 0 8px 24px rgba(0,0,0,0.2);
    opacity: 0;
    transform: translateY(-8px) scale(0.98);
    animation: toast-in 0.2s ease forwards;
    min-width: 200px;
    max-width: 300px;
}

.toast.success { background: #166534; }
.toast.error { background: #b91c1c; }
.toast.warning { background: #92400e; }
.toast.hide { animation: toast-out 0.2s ease forwards; }
.toast-icon { font-size: 15px; flex-shrink: 0; }
.toast-text { flex: 1; }

@keyframes toast-in { to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-8px) scale(0.98); } }

/* ── Bottom Tab Bar (Mobile only) ── */
.bottom-tab-bar {
    display: none;
}

.tab-more-menu {
    display: none;
}

.tab-more-overlay {
    display: none;
}

.tab-item svg,
.tab-more-btn svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
}

/* ── Mobile ── */
@media (max-width: 768px) {
    /* Hide sidebar on mobile */
    .sidebar {
        display: none;
    }

    /* Show bottom tab bar */
    .bottom-tab-bar {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: #1a1a1a;
        border-top: 1px solid #2a2a2a;
        z-index: 100;
        padding: 8px 4px calc(8px + env(safe-area-inset-bottom));
        gap: 0;
    }

    .tab-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        text-decoration: none;
        color: #666;
        padding: 4px 2px;
        border-radius: 10px;
        transition: color 0.15s;
        font-size: 10px;
        font-weight: 500;
    }

    .tab-item.active {
        color: white;
    }

    .tab-item svg {
        width: 22px;
        height: 22px;
    }

    .tab-more-btn {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        background: none;
        border: none;
        color: #666;
        padding: 4px 2px;
        border-radius: 10px;
        font-size: 10px;
        font-weight: 500;
        cursor: pointer;
    }

    .tab-more-btn svg {
        width: 22px;
        height: 22px;
    }

    /* More menu overlay */
    .tab-more-menu {
        display: none;
        position: fixed;
        bottom: calc(64px + env(safe-area-inset-bottom));
        left: 12px;
        right: 12px;
        background: #1a1a1a;
        border-radius: 18px;
        padding: 8px;
        z-index: 200;
        box-shadow: 0 -8px 32px rgba(0,0,0,0.3);
        flex-direction: column;
        gap: 2px;
    }

    .tab-more-menu.open {
        display: flex;
    }

    .tab-more-overlay.open {
        display: block;
    }

    .tab-more-item {
        display: flex;
        align-items: center;
        gap: 12px;
        text-decoration: none;
        color: #c8c8c8;
        padding: 12px 16px;
        border-radius: 12px;
        font-size: 15px;
        font-weight: 500;
        transition: background 0.15s;
    }

    .tab-more-item:hover, .tab-more-item.active {
        background: #2a2a2a;
        color: white;
    }

    .tab-more-divider {
        height: 1px;
        background: #2a2a2a;
        margin: 4px 0;
    }

    .tab-more-item-small {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px 16px;
        border-radius: 12px;
    }

    .tab-more-item-small a,
    .tab-more-item-small button {
        color: #888;
        font-size: 13px;
        text-decoration: none;
        background: none;
        border: none;
        cursor: pointer;
    }

    .tab-more-item-small a:hover,
    .tab-more-item-small button:hover {
        color: white;
    }

    .tab-more-overlay {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 150;
    }

    .tab-more-overlay.open {
        display: block;
    }

    /* Content adjustments */
    .app-content {
        padding: 12px 12px calc(80px + env(safe-area-inset-bottom));
    }

    .content-shell {
        min-height: auto;
        border-radius: 18px;
        padding: 16px;
    }

    .page-title {
        font-size: 22px;
    }

    .toast-container {
        top: auto;
        bottom: calc(80px + env(safe-area-inset-bottom) + 12px);
        right: 12px;
        left: 12px;
    }

    .toast {
        max-width: 100%;
    }
}

/* ── Barcode Scanner ── */
.barcode-modal {
    max-width: 380px;
    padding: 0;
    overflow: hidden;
}

.barcode-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
}

.barcode-modal-title {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
}

.barcode-close-btn {
    background: none;
    border: none;
    font-size: 24px;
    color: #999;
    cursor: pointer;
    line-height: 1;
    padding: 0;
}

.barcode-close-btn:hover { color: #333; }

.barcode-viewfinder-wrap {
    position: relative;
    background: #000;
    aspect-ratio: 4/3;
    overflow: hidden;
}

#barcodeVideo {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.barcode-viewfinder-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.barcode-viewfinder-frame {
    width: 65%;
    height: 35%;
    border: 2.5px solid white;
    border-radius: 10px;
    box-shadow: 0 0 0 2000px rgba(0,0,0,0.4);
    position: relative;
}

.barcode-viewfinder-frame::before,
.barcode-viewfinder-frame::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 80%;
    height: 2px;
    background: rgba(255,255,255,0.6);
    animation: barcode-scan 2s ease-in-out infinite;
}

@keyframes barcode-scan {
    0%, 100% { top: 20%; }
    50% { top: 80%; }
}

.barcode-hint {
    position: absolute;
    bottom: 12px;
    left: 0; right: 0;
    text-align: center;
    color: white;
    font-size: 13px;
    margin: 0;
    text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

.barcode-manual-wrap {
    display: flex;
    gap: 8px;
    padding: 14px 16px;
    border-top: 1px solid #f0f0f0;
}

.barcode-manual-input {
    flex: 1;
    padding: 9px 12px;
    border: 1.5px solid #e5e5e5;
    border-radius: 10px;
    font-size: 14px;
    font-family: inherit;
}

.barcode-manual-input:focus { outline: none; border-color: #111; }

.barcode-result-title { margin: 0 0 12px; font-size: 18px; font-weight: 700; }

.barcode-result-product {
    font-size: 16px;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 4px;
}

.barcode-result-matched {
    font-size: 13px;
    color: #666;
    margin-bottom: 12px;
}

.barcode-result-inventory {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #f0fdf4;
    border: 1.5px solid #bbf7d0;
    border-radius: 10px;
    padding: 10px 14px;
    font-size: 13px;
    color: #166534;
    margin-bottom: 12px;
}

.barcode-qty-row {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.barcode-qty-row input,
.barcode-qty-row select {
    padding: 8px 10px;
    border: 1.5px solid #e5e5e5;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
}

@media (max-width: 768px) {
    .barcode-modal {
        max-width: 100%;
        border-radius: 20px 20px 0 0;
    }

    #barcodeScannerModal {
        align-items: flex-end;
        padding: 0;
    }

    #barcodeResultModal {
        align-items: flex-end;
        padding: 0;
    }

    #barcodeResultModal .modal {
        border-radius: 20px 20px 0 0;
        max-width: 100%;
        width: 100%;
    }
}