/* ── CSS Custom Properties (Light — default) ───────────────────────────────── */
:root {
    --color-bg: #f3f4f6;
    --color-text: #1f2937;
    --color-text-strong: #111827;
    --color-muted: #6b7280;
    --color-lighter: #9ca3af;
    --color-border: #e5e7eb;

    --color-card-bg: #fff;
    --color-card-border: #e5e7eb;
    --color-card-shadow: rgba(0, 0, 0, 0.1);
    --color-card-img-bg: #f9fafb;

    --color-header-bg: #fff;
    --color-header-border: #e5e7eb;
    --color-header-title: #1d4ed8;

    --color-sidebar-bg: #fff;
    --color-sidebar-border: #e5e7eb;

    --color-primary: #2563eb;
    --color-primary-dark: #1d4ed8;
    --color-primary-light: #eff6ff;
    --color-primary-text: #1d4ed8;

    --color-danger: #ef4444;

    --color-hover: #f3f4f6;

    --color-sync-bar: linear-gradient(90deg, #3b82f6, #2563eb);

    --color-loading-bg: #fff;
    --color-spinner-track: #e5e7eb;
    --color-spinner-top: #3b82f6;

    --color-scrollbar-thumb: #d1d5db;

    --sidebar-width: 240px;
    --header-height: 57px;
}

/* ── Dark theme via system preference ──────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
    :root {
        --color-bg: #111827;
        --color-text: #f3f4f6;
        --color-text-strong: #f9fafb;
        --color-muted: #9ca3af;
        --color-lighter: #6b7280;
        --color-border: #374151;

        --color-card-bg: #1f2937;
        --color-card-border: #374151;
        --color-card-shadow: rgba(0, 0, 0, 0.4);
        --color-card-img-bg: #374151;

        --color-header-bg: #1f2937;
        --color-header-border: #374151;
        --color-header-title: #60a5fa;

        --color-sidebar-bg: #1f2937;
        --color-sidebar-border: #374151;

        --color-primary: #3b82f6;
        --color-primary-dark: #2563eb;
        --color-primary-light: #1e3a5f;
        --color-primary-text: #60a5fa;

        --color-danger: #f87171;

        --color-hover: #374151;

        --color-loading-bg: #1f2937;
        --color-spinner-track: #374151;
        --color-spinner-top: #60a5fa;

        --color-scrollbar-thumb: #4b5563;
    }
}

/* ── Dark theme via manual class (takes precedence) ───────────────────────── */
html.dark {
    --color-bg: #111827;
    --color-text: #f3f4f6;
    --color-text-strong: #f9fafb;
    --color-muted: #9ca3af;
    --color-lighter: #6b7280;
    --color-border: #374151;

    --color-card-bg: #1f2937;
    --color-card-border: #374151;
    --color-card-shadow: rgba(0, 0, 0, 0.4);
    --color-card-img-bg: #374151;

    --color-header-bg: #1f2937;
    --color-header-border: #374151;
    --color-header-title: #60a5fa;

    --color-sidebar-bg: #1f2937;
    --color-sidebar-border: #374151;

    --color-primary: #3b82f6;
    --color-primary-dark: #2563eb;
    --color-primary-light: #1e3a5f;
    --color-primary-text: #60a5fa;

    --color-danger: #f87171;

    --color-hover: #374151;

    --color-loading-bg: #1f2937;
    --color-spinner-track: #374151;
    --color-spinner-top: #60a5fa;

    --color-scrollbar-thumb: #4b5563;
}

/* ── Reset & base ───────────────────────────────────────────────────────────── */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    background: var(--color-bg);
    color: var(--color-text);
    transition: background 0.2s, color 0.2s;
}

a {
    color: inherit;
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}

ul {
    list-style: none;
}

/* ── Loading screen ─────────────────────────────────────────────────────────── */
#loading-screen {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--color-loading-bg);
    z-index: 100;
    gap: 12px;
    color: var(--color-muted);
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--color-spinner-track);
    border-top-color: var(--color-spinner-top);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}
@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ── Header ─────────────────────────────────────────────────────────────────── */
header {
    background: var(--color-header-bg);
    border-bottom: 1px solid var(--color-header-border);
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    gap: 8px;
    transition: background 0.2s, border-color 0.2s;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.header-center {
    flex: 1;
    min-width: 0;
    text-align: center;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

/* ── Menu toggle (hamburger) ────────────────────────────────────────── */
#menu-toggle {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    color: var(--color-text);
    font-size: 22px;
    line-height: 1;
    border-radius: 4px;
    transition: background 0.15s;
}
#menu-toggle:hover {
    background: var(--color-hover);
}

/* ── Theme toggle ───────────────────────────────────────────────────── */
#theme-toggle {
    background: none;
    border: 1px solid var(--color-border);
    cursor: pointer;
    padding: 4px 8px;
    color: var(--color-text);
    font-size: 16px;
    line-height: 1;
    border-radius: 6px;
    transition: background 0.15s, border-color 0.2s;
}
#theme-toggle:hover {
    background: var(--color-hover);
}

/* ── Sync progress bar (thin animated bar below header) ───────── */
#sync-progress {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
    height: 3px;
    background: transparent;
    pointer-events: none;
}
#sync-progress .sync-bar {
    width: 100%;
    height: 100%;
    background: var(--color-sync-bar);
    transform: scaleX(0);
    transform-origin: left center;
    transition: transform 0.3s ease;
}
#sync-progress.active .sync-bar {
    animation: sync-progress-anim 1.5s ease-in-out infinite;
}
@keyframes sync-progress-anim {
    0% {
        transform: scaleX(0);
        transform-origin: left center;
    }
    50% {
        transform: scaleX(0.7);
        transform-origin: left center;
    }
    100% {
        transform: scaleX(1);
        transform-origin: left center;
    }
}

header h1 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--color-header-title);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.last-update {
    font-size: 12px;
    color: var(--color-lighter);
    white-space: nowrap;
    margin-right: auto;
    padding-left: 16px;
}

#refresh-btn {
    font-size: 13px;
    padding: 6px 14px;
    background: var(--color-primary);
    color: #fff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s;
    white-space: nowrap;
}
#refresh-btn:hover {
    background: var(--color-primary-dark);
}
#refresh-btn:disabled {
    opacity: 0.5;
    cursor: default;
}

/* ── Layout ─────────────────────────────────────────────────────────────────── */
.layout {
    display: flex;
    max-width: 1440px;
    margin: 0 auto;
    padding: 16px;
    gap: 16px;
    align-items: flex-start;
}

/* ── Sidebar backdrop (mobile) ────────────────────────────────────────── */
#sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 25;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────────── */
#sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    position: sticky;
    top: var(--header-height);
    max-height: calc(100vh - 73px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    transition: transform 0.25s ease, background 0.2s, border-color 0.2s;
}

.filter-block {
    background: var(--color-sidebar-bg);
    border: 1px solid var(--color-sidebar-border);
    border-radius: 8px;
    padding: 12px;
    transition: background 0.2s, border-color 0.2s;
}

.filter-block h2 {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--color-muted);
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.clear-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--color-lighter);
    font-size: 12px;
    padding: 0 2px;
}
.clear-btn:hover {
    color: var(--color-danger);
}

/* Category list */
.cat-item {
    padding: 5px 6px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 4px;
    user-select: none;
}
.cat-item:hover {
    background: var(--color-hover);
}
.cat-item.active {
    background: var(--color-primary-light);
    color: var(--color-primary-text);
    font-weight: 600;
}

.cat-toggle {
    font-size: 11px;
    color: var(--color-lighter);
    width: 12px;
    flex-shrink: 0;
}
.cat-count {
    margin-left: auto;
    font-size: 11px;
    color: var(--color-lighter);
    font-style: normal;
}

.cat-children {
    padding-left: 16px;
}
.cat-child {
    font-size: 12px;
}

/* Address list */
.addr-item label {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 4px;
    font-size: 13px;
    cursor: pointer;
    border-radius: 4px;
}
.addr-item label:hover {
    background: var(--color-hover);
}
.addr-item input[type="checkbox"] {
    cursor: pointer;
    accent-color: var(--color-primary);
}
.no-items {
    font-size: 12px;
    color: var(--color-lighter);
    padding: 4px;
}

/* ── Product grid ─────────────────────────────────────────────────────────────── */
#product-grid-wrapper {
    flex: 1;
    min-width: 0;
}

#product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 14px;
}

#empty-state {
    text-align: center;
    padding: 60px 0;
    color: var(--color-lighter);
    font-size: 15px;
}

.error-msg {
    color: var(--color-danger);
    padding: 20px;
}

/* ── Product card ─────────────────────────────────────────────────────────────── */
.product-card {
    background: var(--color-card-bg);
    border: 1px solid var(--color-card-border);
    border-radius: 10px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    cursor: pointer;
    transition:
        box-shadow 0.15s,
        transform 0.15s,
        background 0.2s,
        border-color 0.2s;
}
.product-card:hover {
    box-shadow: 0 4px 16px var(--color-card-shadow);
    transform: translateY(-2px);
}

.product-img-wrap {
    width: 100%;
    aspect-ratio: 3 / 2;
    background: var(--color-card-img-bg);
    overflow: hidden;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.product-thumb {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: opacity 0.1s;
}

.product-img-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 24px 10px 8px;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
}

.product-price {
    font-size: 18px;
    font-weight: 800;
    color: #fff;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    line-height: 1;
}

.product-body {
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
}

.product-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-strong);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    transition: color 0.2s;
}
.product-card:hover .product-title {
    color: var(--color-primary);
}

.product-addr {
    font-size: 12px;
    color: var(--color-muted);
    margin-top: auto;
    padding-top: 2px;
}

/* ── Irrelevant product ──────────────────────────────────────────────── */
.product-irrelevant {
    opacity: 0.55;
}
.product-irrelevant .product-img-wrap {
    filter: grayscale(60%);
}
.irrelevant-badge {
    display: inline-block;
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    background: var(--color-lighter);
    border-radius: 3px;
    padding: 1px 5px;
    margin-left: 6px;
    vertical-align: middle;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* ── Irrelevant toggle ─────────────────────────────────────────────── */
.filter-irrelevant {
    padding: 10px 12px;
}
.irrelevant-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    cursor: pointer;
    color: var(--color-muted);
    transition: color 0.2s;
}
.irrelevant-toggle input[type="checkbox"] {
    cursor: pointer;
    accent-color: var(--color-lighter);
}
.irrelevant-toggle:hover {
    color: var(--color-text);
}

/* ── Scrollbar (sidebar) ──────────────────────────────────────────────────────── */
#sidebar::-webkit-scrollbar {
    width: 4px;
}
#sidebar::-webkit-scrollbar-thumb {
    background: var(--color-scrollbar-thumb);
    border-radius: 2px;
}

/* ── Load More button ──────────────────────────────────────────────────── */
.load-more-btn {
    display: block;
    width: 100%;
    margin-top: 16px;
    padding: 12px;
    background: var(--color-hover);
    border: 2px dashed var(--color-border);
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    color: var(--color-muted);
    transition:
        background 0.15s,
        border-color 0.15s,
        color 0.2s;
}
.load-more-btn:hover {
    background: var(--color-border);
    border-color: var(--color-lighter);
    color: var(--color-text);
}
/* ── Lightbox ─────────────────────────────────────────────────────────────── */
#lightbox {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 200;
    background: rgba(0, 0, 0, 0.88);
    align-items: center;
    justify-content: center;
}
#lightbox.open {
    display: flex;
}

.lb-img-wrap {
    max-width: 90vw;
    max-height: 88vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
.lb-img {
    max-width: 90vw;
    max-height: 88vh;
    object-fit: contain;
    border-radius: 4px;
    user-select: none;
}

.lb-close {
    position: absolute;
    top: 14px;
    right: 18px;
    background: none;
    border: none;
    color: #fff;
    font-size: 26px;
    line-height: 1;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s;
}
.lb-close:hover {
    opacity: 1;
}

.lb-prev,
.lb-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.12);
    border: none;
    color: #fff;
    font-size: 44px;
    line-height: 1;
    cursor: pointer;
    padding: 6px 16px;
    border-radius: 6px;
    transition: background 0.15s;
    user-select: none;
}
.lb-prev {
    left: 10px;
}
.lb-next {
    right: 10px;
}
.lb-prev:hover,
.lb-next:hover {
    background: rgba(255, 255, 255, 0.25);
}

.lb-counter {
    position: absolute;
    bottom: 14px;
    color: rgba(255, 255, 255, 0.65);
    font-size: 13px;
}

/* ── Product detail page ───────────────────────────────────────────────── */
#product-detail {
    background: var(--color-card-bg);
    border: 1px solid var(--color-card-border);
    border-radius: 10px;
    padding: 20px;
    transition: background 0.2s, border-color 0.2s;
}

.pd-back {
    display: inline-block;
    background: none;
    border: 1px solid var(--color-border);
    color: var(--color-text);
    font-size: 14px;
    padding: 6px 14px;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 16px;
    transition: background 0.15s, border-color 0.2s;
}
.pd-back:hover {
    background: var(--color-hover);
}

.pd-layout {
    display: flex;
    gap: 24px;
    align-items: flex-start;
}

.pd-gallery {
    flex: 1;
    min-width: 0;
    max-width: 500px;
}

.pd-main-img-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
    background: var(--color-card-img-bg);
    border-radius: 8px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s;
}
.pd-main-img-wrap img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    user-select: none;
}

.pd-gal-prev,
.pd-gal-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(0, 0, 0, 0.35);
    border: none;
    color: #fff;
    font-size: 28px;
    line-height: 1;
    cursor: pointer;
    padding: 4px 10px;
    border-radius: 4px;
    transition: opacity 0.2s, background 0.15s;
    user-select: none;
    opacity: 0;
}
.pd-main-img-wrap:hover .pd-gal-prev,
.pd-main-img-wrap:hover .pd-gal-next {
    opacity: 1;
}
.pd-gal-prev { left: 8px; }
.pd-gal-next { right: 8px; }
.pd-gal-prev:hover,
.pd-gal-next:hover {
    background: rgba(0, 0, 0, 0.6);
}

.pd-gal-counter {
    position: absolute;
    bottom: 8px;
    right: 10px;
    color: rgba(255, 255, 255, 0.8);
    font-size: 12px;
    background: rgba(0, 0, 0, 0.4);
    padding: 2px 8px;
    border-radius: 4px;
    pointer-events: none;
}

.pd-thumbs {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    overflow-x: auto;
    padding-bottom: 4px;
}
.pd-thumb {
    width: 60px;
    height: 60px;
    flex-shrink: 0;
    border-radius: 6px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.15s;
    background: var(--color-card-img-bg);
}
.pd-thumb:hover,
.pd-thumb.active {
    border-color: var(--color-primary);
}
.pd-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.pd-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.pd-title {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--color-text-strong);
    line-height: 1.3;
    transition: color 0.2s;
}

.pd-price {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--color-primary);
}

.pd-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    font-size: 13px;
    color: var(--color-muted);
}
.pd-meta span {
    background: var(--color-hover);
    padding: 3px 10px;
    border-radius: 4px;
    transition: background 0.2s;
}

.pd-description {
    font-size: 14px;
    color: var(--color-text);
    line-height: 1.6;
    padding: 12px 0;
    border-top: 1px solid var(--color-border);
    border-bottom: 1px solid var(--color-border);
    transition: border-color 0.2s, color 0.2s;
}

.pd-specs {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 6px 12px;
    font-size: 13px;
}
.pd-specs dt {
    color: var(--color-muted);
    white-space: nowrap;
}
.pd-specs dd {
    color: var(--color-text);
}

.pd-link {
    display: inline-block;
    background: var(--color-primary);
    color: #fff;
    padding: 8px 18px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    text-align: center;
    transition: background 0.15s;
    text-decoration: none;
}
.pd-link:hover {
    background: var(--color-primary-dark);
    text-decoration: none;
}

@media (max-width: 1200px) {
    .pd-layout {
        flex-direction: column;
    }
    .pd-gallery {
        max-width: 100%;
    }
}

/* ── Responsive (mobile) ─────────────────────────────────────────────── */
@media (max-width: 768px) {
    #menu-toggle {
        display: block;
    }

    .last-update {
        display: none;
    }

    .header-center {
        text-align: left;
    }

    header h1 {
        font-size: 0.95rem;
    }

    .layout {
        padding: 12px;
        gap: 0;
    }

    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 30;
        width: 280px;
        max-height: 100vh;
        padding: 16px;
        padding-top: calc(var(--header-height) + 8px);
        background: var(--color-sidebar-bg);
        border-right: 1px solid var(--color-sidebar-border);
        transform: translateX(-100%);
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.2);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    #sidebar-backdrop {
        display: block;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
    }

    #sidebar-backdrop.open {
        opacity: 1;
        pointer-events: auto;
    }

    #product-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 10px;
    }

    .product-card:hover {
        transform: none;
        box-shadow: none;
    }
}

@media (max-width: 480px) {
    #product-grid {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    header {
        padding: 10px 12px;
    }

    header h1 {
        font-size: 0.85rem;
    }

    .layout {
        padding: 8px;
    }

    #refresh-btn {
        font-size: 12px;
        padding: 5px 10px;
    }
}
