/* Base Styles */
:root { 
    --primary-color: #28a745; 
    --secondary-color: #007bff; 
    --light-gray: #f8f9fa; 
    --danger-color: #dc3545;
    --text-color: #333;
    --border-color: #dee2e6;
    --background-color: #f0f2f5;
}
body { 
    font-family: 'Sarabun', Arial, sans-serif; 
    margin: 0; 
    background-color: var(--background-color); 
    font-size: 16px; 
    color: var(--text-color); 
    line-height: 1.6;
}
.page-container { 
    max-width: 1400px; /* ขยายความกว้างสูงสุดเพื่อรองรับ Sidebar */
    margin: 0 auto; 
    padding: 20px; 
}
h1, h2, h3, h4 { color: var(--primary-color); }

/* User Bar */
.user-bar { 
    padding: 10px 20px;
    background-color: #e9ecef; 
    border-bottom: 1px solid var(--border-color);
    font-size: 0.9em;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 15px;
}
.user-bar a {
    color: var(--secondary-color);
    text-decoration: none;
    padding: 5px 8px;
    border-radius: 4px;
    transition: background-color 0.2s;
}
.user-bar a:hover {
    background-color: rgba(0, 123, 255, 0.1);
}

/* Cart Section */
.cart { 
    background-color: #fff; 
    border-radius: 8px; 
    box-shadow: 0 4px 8px rgba(0,0,0,0.05); 
    padding: 20px; 
    margin-bottom: 20px; 
}
details > summary {
    font-size: 1.2em;
    font-weight: 600;
    cursor: pointer;
    list-style: none;
}
details > summary::-webkit-details-marker {
    display: none;
}
.cart h4 { margin-top: 0; display: flex; align-items: center; gap: 8px; }
.cart table { 
    width: 100%; 
    border-collapse: collapse; 
    margin-top: 15px; 
    font-size: 0.9em;
}
.cart th, .cart td { 
    padding: 10px 8px; 
    border-bottom: 1px solid var(--border-color); 
    text-align: left; 
    vertical-align: middle;
}
.cart th { 
    font-weight: 600; 
    color: #495057; 
    background-color: var(--light-gray); 
}
.cart-item-info { 
    display: flex; 
    align-items: center; 
    gap: 10px; 
}
.cart-item-info .thumb, /* เพิ่ม thumb เข้ามาด้วย */
.cart-item-info img { 
    width: 50px; 
    height: 50px; 
    object-fit: contain; 
    border-radius: 4px; 
    background-color: #f8f8f8; 
}
.cart-item-info .name { font-weight: 600; }
.price-breakdown { font-size: 0.8em; color: #6c757d; line-height: 1.5; }
.cart-quantity-input { 
    width: 60px; 
    padding: 5px; 
    border: 1px solid #ccc; 
    border-radius: 4px; 
    text-align: center;
}
.cart-actions { 
    display: flex; 
    justify-content: flex-end; 
    align-items: center; 
    gap: 10px; 
    padding-top: 15px; 
    flex-wrap: wrap;
}
.update-cart-btn, .clear-cart, .checkout { 
    padding: 8px 16px; 
    margin-left: 8px;
    text-decoration: none; 
    color: white; 
    border-radius: 5px; 
    border: none;
    cursor: pointer;
    font-weight: bold;
    transition: opacity 0.2s;
}
.update-cart-btn:hover, .clear-cart:hover, .checkout:hover { opacity: 0.9; }
.update-cart-btn { background-color: var(--secondary-color); }
.clear-cart { background-color: #6c757d; }
.checkout { background-color: var(--primary-color); }

/* --- Sidebar Layout --- */
.main-layout {
    display: flex;
    flex-wrap: wrap; 
    gap: 20px;
    margin-top: 20px;
}
.sidebar-nav {
    flex: 0 0 240px; 
    background-color: #ffffff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.05);
    height: fit-content;
}
.sidebar-nav h3 {
    margin-top: 0;
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 10px;
    font-size: 1.1em;
}
.sidebar-nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.sidebar-nav ul li a {
    display: block;
    padding: 10px 15px;
    text-decoration: none;
    color: var(--text-color);
    border-radius: 5px;
    transition: background-color 0.3s, color 0.3s;
    font-weight: 500;
    border-bottom: 1px solid #f0f0f0;
}
.sidebar-nav ul li:last-child a { border-bottom: none; }
.sidebar-nav ul li a:hover { background-color: #e9ecef; }
.sidebar-nav ul li a.active {
    background-color: var(--primary-color);
    color: white;
    font-weight: bold;
}
.product-display {
    flex: 1; 
    min-width: 0;
}

/* Section Header & Search */
.section-header { 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    margin-bottom: 15px; 
    flex-wrap: wrap;
    gap: 10px;
}
.section-header h2 { margin: 0; color: var(--text-color); font-size: 1.4rem; }
.search-form { display: flex; }
.search-form input[type="text"] { 
    border: 1px solid #ccc; 
    border-right: none; 
    padding: 8px; 
    border-radius: 20px 0 0 20px; 
    font-size: 0.9em;
}
.search-form button { 
    border: 1px solid #ccc; 
    background: #f0f0f0; 
    padding: 8px 12px; 
    border-radius: 0 20px 20px 0; 
    cursor: pointer; 
    font-size: 1rem; 
    line-height: 1; 
    color: var(--text-color);
}

/* Product Grid */
.product-grid { 
    display: grid; 
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 15px; 
    margin-top: 10px; 
}
.product-item { 
    display: flex; 
    flex-direction: column; 
    background-color: #fff; 
    border-radius: 8px; 
    box-shadow: 0 4px 8px rgba(0,0,0,0.05); 
    overflow: hidden; 
    transition: transform 0.2s, box-shadow 0.2s; 
    position: relative; 
}
.product-item:hover { transform: translateY(-5px); box-shadow: 0 8px 16px rgba(0,0,0,0.1); }
.product-image-container { 
    width: 100%; 
    aspect-ratio: 1 / 1;
    padding: 10px; 
    background-color: #fff; 
    box-sizing: border-box; 
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}
.product-image-container img { 
    max-width: 100%; 
    max-height: 100%;
    object-fit: contain;
    display: block;
}
.product-details { 
    padding: 0 15px 15px 15px; 
    flex-grow: 1; 
}
.product-details h3 {
    margin-top: 10px;
    margin-bottom: 10px;
    font-size: 1em;
    height: 3.2em;      /* แก้ไข: กำหนดความสูงให้พอดีกับ 2 บรรทัด */
    line-height: 1.6em; /* เพิ่ม: ควบคุมระยะห่างระหว่างบรรทัด */
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2; /* สำหรับเบราว์เซอร์รุ่นเก่า */
    line-clamp: 2;          /* สำหรับเบราว์เซอร์มาตรฐานใหม่ */
    -webkit-box-orient: vertical;
}
.price-display {
    font-size: 1.1em;
    font-weight: bold;
    color: var(--primary-color);
    text-align: center;
    margin-bottom: 10px;
}
.price-tiers { 
    font-size: 0.85em; 
    text-align: left; 
    padding: 0 5px; 
    line-height: 1.5;
}
.price-tiers p { margin: 5px 0; }
.product-actions form { 
    display: flex; 
    gap: 10px; 
    padding: 15px; 
    background-color: var(--light-gray); 
}
.product-actions input[type="number"] { 
    width: 60px; 
    padding: 8px; 
    text-align: center; 
    border: 1px solid #ccc; 
    border-radius: 4px; 
    font-size: 0.9em;
}
.product-actions button { 
    flex-grow: 1; 
    padding: 8px; 
    background-color: var(--secondary-color); 
    color: white; 
    border: none; 
    border-radius: 4px; 
    cursor: pointer; 
    font-weight: bold; 
    transition: background-color 0.2s;
}
.product-actions button:hover { background-color: #0069d9; }
.product-actions button:disabled { background-color: #ccc; cursor: not-allowed; }

/* Status Message */
.status-message { 
    padding: 15px; 
    border-radius: 4px; 
    margin: 0 0 20px 0; 
    text-align: center; 
    font-weight: 500;
}
.status-message.error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb;}
.status-message.success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; }

/* Pagination Controls */
.pagination {
    display: flex;
    justify-content: center;
    margin-top: 30px;
    gap: 5px;
    flex-wrap: wrap; 
}
.pagination a, .pagination span {
    padding: 8px 15px;
    border: 1px solid #ccc;
    border-radius: 5px;
    text-decoration: none;
    color: var(--primary-color);
    background-color: #fff;
    transition: background-color 0.3s, color 0.3s;
    font-size: 0.9em;
}
.pagination a:hover {
    background-color: #e9ecef;
}
.pagination .current-page {
    background-color: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
    font-weight: bold;
}
.pagination .disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background-color: #f8f9fa;
}


/* Responsive Adjustments */
@media (max-width: 992px) {
    .main-layout {
        flex-direction: column;
    }
    .sidebar-nav {
        flex: 0 0 auto; 
        width: 100%;
        box-sizing: border-box;
    }
}
@media (max-width: 768px) {
    body { font-size: 15px; }
    .page-container { padding: 10px; }
    .user-bar { 
        padding: 10px; 
        justify-content: center;
        font-size: 0.85em;
    }
    .cart, .product-section { padding: 15px; }
    .cart th, .cart td { padding: 8px 5px; }
    .cart-item-info img { width: 40px; height: 40px; }
    .cart-item-info .name { font-size: 0.9em; }
    .cart-quantity-input { width: 45px; font-size: 0.85em; }
    .cart-actions { justify-content: center; }

    .section-header { 
        flex-direction: column; 
        align-items: stretch; 
        gap: 15px; 
    }
    .section-header h2 { text-align: center; }
    .search-form { width: 100%; }

    .product-grid { 
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    }
    .product-details h3 { font-size: 0.9em; height: 3.2em; }
    .price-tiers { font-size: 0.8em; padding: 0 5px; }
    .product-actions form { flex-direction: column; gap: 8px; }
    .product-actions input[type="number"] { width: 100%; box-sizing: border-box; }
    
    .pagination { gap: 3px; }
    .pagination a, .pagination span { padding: 6px 10px; font-size: 0.8em; }

    .cart-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .cart-table-wrapper table {
        min-width: 600px;
    }
}