/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    overflow-x: hidden;
    width: 100%;
    max-width: 100%;
}

:root {
    --primary-color: #2563eb;
    --primary-dark: #1e40af;
    --secondary-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --bg-tertiary: #f3f4f6;
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --radius: 8px;
    --radius-lg: 12px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    color: var(--text-primary);
    background-color: var(--bg-secondary);
    line-height: 1.6;
    overflow-x: hidden;
    width: 100%;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
}

/* Header */
.header {
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    padding: 1rem 0;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2rem;
}

.logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-color);
    margin: 0;
}

.logo-link {
    text-decoration: none;
}

.search-box {
    display: flex;
    flex: 1;
    max-width: 500px;
    gap: 0.5rem;
}

.search-input {
    flex: 1;
    padding: 0.75rem 1rem;
    border: 2px solid var(--border-color);
    border-radius: var(--radius);
    font-size: 1rem;
    transition: border-color 0.2s;
}

.search-input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.search-btn {
    padding: 0.75rem 1.5rem;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 1rem;
    transition: background 0.2s;
}

.search-btn:hover {
    background: var(--primary-dark);
}

/* Main Layout */
.main-container {
    display: flex;
    gap: 2rem;
    padding: 2rem 20px;
    align-items: flex-start;
    width: 100%;
    box-sizing: border-box;
}

/* Sidebar */
.sidebar {
    width: 280px;
    flex-shrink: 0;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    box-shadow: var(--shadow-md);
    position: sticky;
    top: 100px;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
}

.sidebar-section {
    margin-bottom: 2rem;
}

.sidebar-section:last-child {
    margin-bottom: 0;
}

.sidebar-section h2 {
    font-size: 1.25rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.5rem;
}

.filter-group {
    margin-bottom: 1rem;
}

.filter-group label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.95rem;
}

.filter-group input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.reset-filters-btn {
    padding: 0.5rem 1rem;
    background: var(--text-secondary);
    color: white;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.85rem;
    transition: background 0.2s;
}

.reset-filters-btn:hover {
    background: var(--text-primary);
}

/* Categories Tree */
.categories-tree {
    list-style: none;
}

.category-item {
    margin-bottom: 0.25rem;
}

.category-wrapper {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.category-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 0.7rem;
    transition: transform 0.2s, color 0.2s;
    user-select: none;
    flex-shrink: 0;
}

.category-toggle:hover {
    color: var(--primary-color);
}

.category-spacer {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

.category-link {
    flex: 1;
    display: block;
    padding: 0.5rem 0.75rem;
    color: var(--text-primary);
    text-decoration: none;
    border-radius: var(--radius);
    transition: background 0.2s;
    font-size: 0.9rem;
    min-width: 0;
}

.category-link:hover {
    background: var(--bg-tertiary);
}

.category-link.active {
    background: var(--primary-color);
    color: white;
    font-weight: 600;
}

.category-children {
    margin-left: 0.75rem;
    margin-top: 0.25rem;
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    transition: max-height 0.3s ease-out, opacity 0.2s ease-out;
    border-left: 2px solid var(--border-color);
    padding-left: 0.75rem;
}

.category-children.expanded {
    max-height: 5000px;
    opacity: 1;
    transition: max-height 0.5s ease-in, opacity 0.3s ease-in;
}

/* Indentation for nested categories */
.category-item[data-level="1"] .category-link {
    padding-left: 1rem;
}

.category-item[data-level="2"] .category-link {
    padding-left: 1.5rem;
}

.category-item[data-level="3"] .category-link {
    padding-left: 2rem;
}

.category-item[data-level="4"] .category-link {
    padding-left: 2.5rem;
}

/* Main Content */
.main-content {
    flex: 1;
    min-width: 0;
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    background: var(--bg-primary);
    padding: 1rem 1.5rem;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
}

.products-count {
    font-weight: 600;
    color: var(--text-secondary);
}

.toolbar-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.sort-select {
    padding: 0.5rem 1rem;
    border: 2px solid var(--border-color);
    border-radius: var(--radius);
    font-size: 0.95rem;
    cursor: pointer;
    background: white;
}

.sort-select:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* Products Grid */
.products-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.product-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-md);
    transition: transform 0.2s, box-shadow 0.2s;
    cursor: pointer;
    text-decoration: none;
    color: inherit;
    display: block;
}

.product-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
}

.product-image {
    width: 100%;
    height: 220px;
    object-fit: cover;
    background: var(--bg-tertiary);
}

.product-info {
    padding: 1.25rem;
}

.product-name {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text-primary);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.product-category {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
}

.product-price {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

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

.price-original {
    font-size: 1rem;
    color: var(--text-secondary);
    text-decoration: line-through;
}

.discount-badge {
    background: var(--danger-color);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
}

.price-change-badge {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    margin-left: 0.5rem;
}

.price-change-badge.price-increase {
    background-color: #fee2e2;
    color: #dc2626;
}

.price-change-badge.price-decrease {
    background-color: #dcfce7;
    color: #16a34a;
}

.price-change-badge {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    margin-left: 0.5rem;
}

.price-change-badge.price-increase {
    background-color: #fee2e2;
    color: #dc2626;
}

.price-change-badge.price-decrease {
    background-color: #dcfce7;
    color: #16a34a;
}

.new-badge {
    background: var(--warning-color);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    margin-left: auto;
}

/* Product Detail Page */
.breadcrumbs {
    padding: 1rem 0;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.breadcrumbs a {
    color: var(--primary-color);
    text-decoration: none;
}

.breadcrumbs a:hover {
    text-decoration: underline;
}

.product-detail {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 2rem;
    box-shadow: var(--shadow-md);
    margin-bottom: 2rem;
}

.product-detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3rem;
}

.product-detail-image {
    width: 100%;
    border-radius: var(--radius-lg);
    background: var(--bg-tertiary);
}

.product-detail-info h1 {
    font-size: 2rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.product-detail-category {
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
    font-size: 1rem;
}

.product-detail-price {
    margin-bottom: 1.5rem;
}

.product-detail-description {
    margin-top: 2rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border-color);
}

.product-detail-description h3 {
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.product-detail-description p {
    color: var(--text-secondary);
    line-height: 1.8;
}

.product-details-table {
    margin-top: 2rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border-color);
}

.product-details-table h3 {
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.details-table {
    width: 100%;
    border-collapse: collapse;
}

.details-table td {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.details-table td:first-child {
    font-weight: 600;
    color: var(--text-secondary);
    width: 40%;
}

.details-table td:last-child {
    color: var(--text-primary);
}

.uoms-list {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin-top: 1rem;
}

.uom-badge {
    background: var(--bg-tertiary);
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 600;
}

.barcodes-list {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin-top: 1rem;
}

.barcode-badge {
    background: var(--bg-tertiary);
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: monospace;
}

/* Price History */
.price-history-section {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 2rem;
    box-shadow: var(--shadow-md);
    margin-bottom: 2rem;
}

.price-history-section h2 {
    margin-bottom: 1.5rem;
    color: var(--text-primary);
}

.price-history-item {
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.price-history-item:last-child {
    border-bottom: none;
}

.price-history-date {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.price-history-change {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.price-history-old {
    color: var(--text-secondary);
    text-decoration: line-through;
}

.price-history-new {
    font-weight: 600;
    color: var(--primary-color);
}

.price-history-reason {
    background: var(--bg-tertiary);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
}

/* Pagination */
.pagination {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin-top: 2rem;
}

.pagination-btn {
    padding: 0.5rem 1rem;
    border: 2px solid var(--border-color);
    background: var(--bg-primary);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
    color: var(--text-primary);
    display: inline-block;
}

.pagination-btn:hover:not(:disabled) {
    border-color: var(--primary-color);
    background: var(--primary-color);
    color: white;
}

.pagination-btn.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.pagination-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Loading and Error States */
.loading, .error {
    text-align: center;
    padding: 3rem;
    color: var(--text-secondary);
    font-size: 1.1rem;
}

.error {
    color: var(--danger-color);
}

/* Footer */
.footer {
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    padding: 2rem 0;
    margin-top: 4rem;
    text-align: center;
    color: var(--text-secondary);
}

/* Mobile Menu Toggle */
.mobile-menu-toggle {
    display: none;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-primary);
    padding: 0.5rem;
    position: relative;
    z-index: 1000;
    min-width: 44px;
    min-height: 44px;
    touch-action: manipulation;
}

.mobile-menu-toggle:hover {
    color: var(--primary-color);
}

.mobile-menu-toggle:active {
    color: var(--primary-dark);
}

/* Sidebar visibility on mobile */
@media (max-width: 768px) {
    .sidebar.mobile-hidden {
        display: none !important;
    }
}

/* On desktop, sidebar should always be visible (ignore mobile-hidden) */
@media (min-width: 769px) {
    .sidebar.mobile-hidden {
        display: block !important;
    }
}

/* Responsive Design */
@media (max-width: 1024px) {
    .container {
        padding: 0 15px;
    }

    .main-container {
        flex-direction: column;
        gap: 1.5rem;
        padding: 1.5rem 15px;
    }

    .sidebar {
        width: 100%;
        position: static;
        max-height: none;
        top: auto;
        order: 2;
    }

    .main-content {
        order: 1;
    }

    .product-detail-grid {
        grid-template-columns: 1fr;
        gap: 2rem;
    }

    .products-grid {
        grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    }
}

@media (max-width: 768px) {
    .header {
        padding: 0.75rem 0;
    }

    .header-content {
        flex-wrap: wrap;
        gap: 1rem;
    }

    .mobile-menu-toggle {
        display: block;
        order: -1;
    }

    .logo {
        font-size: 1.25rem;
        flex: 1;
        order: 0;
    }

    .search-box {
        max-width: 100%;
        order: 2;
        width: 100%;
    }

    .header-content > div:last-child {
        order: 3;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        width: 100%;
        justify-content: space-between;
    }

    #languageSelect {
        font-size: 0.85rem;
        padding: 0.4rem 0.5rem;
    }

    #logoutBtn {
        font-size: 0.85rem;
        padding: 0.4rem 0.75rem;
    }

    .main-container {
        padding: 1rem 15px;
        gap: 1rem;
    }

    .sidebar {
        display: block;
        padding: 1rem;
        border-radius: var(--radius);
        position: relative;
    }

    .sidebar.mobile-hidden {
        display: none !important;
    }

    .sidebar-section {
        margin-bottom: 1.5rem;
    }

    .sidebar-section h2 {
        font-size: 1.1rem;
    }

    .products-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 1rem;
    }

    .toolbar {
        flex-direction: column;
        gap: 1rem;
        align-items: stretch;
        padding: 1rem;
    }

    .toolbar-right {
        justify-content: space-between;
        flex-wrap: wrap;
    }

    .toolbar-right label {
        font-size: 0.9rem;
    }

    .sort-select {
        flex: 1;
        min-width: 150px;
    }

    .product-card {
        border-radius: var(--radius);
    }

    .product-image {
        height: 180px;
    }

    .product-info {
        padding: 1rem;
    }

    .product-name {
        font-size: 0.95rem;
    }

    .price-current {
        font-size: 1.1rem;
    }

    .product-detail {
        padding: 1.5rem;
        border-radius: var(--radius);
    }

    .product-detail-grid {
        gap: 1.5rem;
    }

    .product-detail-info h1 {
        font-size: 1.5rem;
    }

    .breadcrumbs {
        padding: 0.75rem 0;
        font-size: 0.85rem;
    }
}

@media (max-width: 480px) {
    .container {
        padding: 0 10px;
    }

    .header {
        padding: 0.5rem 0;
    }

    .header-content {
        gap: 0.75rem;
    }

    .logo {
        font-size: 1.1rem;
    }

    .search-input {
        padding: 0.6rem 0.75rem;
        font-size: 0.9rem;
    }

    .search-btn {
        padding: 0.6rem 1rem;
        font-size: 0.9rem;
    }

    .main-container {
        padding: 0.75rem 10px;
    }

    .sidebar {
        padding: 0.75rem;
    }

    .sidebar-section h2 {
        font-size: 1rem;
        margin-bottom: 0.75rem;
    }

    .filter-group {
        margin-bottom: 0.75rem;
    }

    .filter-group label {
        font-size: 0.9rem;
    }

    .category-link {
        font-size: 0.85rem;
        padding: 0.4rem 0.5rem;
    }

    .products-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .product-image {
        height: 200px;
    }

    .product-info {
        padding: 0.75rem;
    }

    .product-name {
        font-size: 0.9rem;
        margin-bottom: 0.5rem;
    }

    .product-category {
        font-size: 0.8rem;
        margin-bottom: 0.5rem;
    }

    .price-current {
        font-size: 1rem;
    }

    .price-original {
        font-size: 0.85rem;
    }

    .toolbar {
        padding: 0.75rem;
    }

    .products-count {
        font-size: 0.9rem;
    }

    .toolbar-right {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }

    .toolbar-right label {
        font-size: 0.85rem;
    }

    .sort-select {
        width: 100%;
        font-size: 0.9rem;
    }

    .product-detail {
        padding: 1rem;
        border-radius: var(--radius);
    }

    .product-detail-info h1 {
        font-size: 1.25rem;
        margin-bottom: 0.75rem;
    }

    .product-detail-category {
        font-size: 0.9rem;
        margin-bottom: 1rem;
    }

    .product-detail-price {
        margin-bottom: 1rem;
    }

    .price-current {
        font-size: 1.5rem;
    }

    .price-original {
        font-size: 1rem;
    }

    .product-detail-description,
    .product-details-table {
        margin-top: 1.5rem;
        padding-top: 1.5rem;
    }

    .details-table {
        font-size: 0.9rem;
    }

    .details-table td {
        padding: 0.5rem;
        display: block;
        width: 100%;
    }

    .details-table td:first-child {
        font-weight: 600;
        padding-bottom: 0.25rem;
        border-bottom: none;
    }

    .details-table td:last-child {
        padding-top: 0.25rem;
        border-bottom: 1px solid var(--border-color);
        margin-bottom: 0.5rem;
    }

    .breadcrumbs {
        font-size: 0.8rem;
        padding: 0.5rem 0;
    }

    .pagination {
        gap: 0.25rem;
        flex-wrap: wrap;
    }

    .pagination-btn {
        padding: 0.4rem 0.75rem;
        font-size: 0.85rem;
    }

    .footer {
        padding: 1.5rem 0;
        font-size: 0.85rem;
    }

    .reset-filters-btn {
        font-size: 0.8rem;
        padding: 0.4rem 0.75rem;
    }

    #categorySearch {
        font-size: 0.85rem;
        padding: 0.4rem;
    }

    .category-search {
        margin-bottom: 0.75rem;
    }
}

/* Touch-friendly improvements */
@media (hover: none) and (pointer: coarse) {
    .product-card:hover {
        transform: none;
    }

    .category-link {
        padding: 0.6rem 0.75rem;
    }

    .category-toggle {
        width: 28px;
        height: 28px;
        font-size: 0.85rem;
    }

    .pagination-btn {
        min-width: 44px;
        min-height: 44px;
    }

    .search-btn,
    .reset-filters-btn,
    #logoutBtn {
        min-height: 44px;
    }

    input[type="checkbox"],
    input[type="radio"] {
        width: 20px;
        height: 20px;
    }
}
