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

:root {
    --bg: var(--tg-theme-bg-color, #ffffff);
    --text: var(--tg-theme-text-color, #1a1a2e);
    --hint: var(--tg-theme-hint-color, #888888);
    --link: var(--tg-theme-link-color, #1a73e8);
    --btn-bg: var(--tg-theme-button-color, #1a73e8);
    --btn-text: var(--tg-theme-button-text-color, #ffffff);
    --secondary-bg: var(--tg-theme-secondary-bg-color, #f0f2f5);
    --card-shadow: 0 2px 8px rgba(0,0,0,.08);
    --radius: 12px;
    --gap: 12px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

/* === Header === */
#header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 16px;
    background: var(--btn-bg);
    color: var(--btn-text);
    position: sticky;
    top: 0;
    z-index: 100;
}
#header h1 { font-size: 1.15rem; font-weight: 700; }
.header-right { display: flex; align-items: center; gap: 10px; }

.badge {
    background: rgba(255,255,255,.2);
    padding: 4px 10px;
    border-radius: 20px;
    font-size: .85rem;
    font-weight: 600;
}

.icon-btn {
    background: rgba(255,255,255,.2);
    border: none;
    color: inherit;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: .85rem;
    cursor: pointer;
    font-weight: 600;
}

/* === Tabs === */
#tabs {
    display: flex;
    background: var(--secondary-bg);
    padding: 4px;
    gap: 4px;
    position: sticky;
    top: 52px;
    z-index: 99;
}
.tab {
    flex: 1;
    border: none;
    background: transparent;
    padding: 10px 0;
    font-size: .88rem;
    font-weight: 600;
    cursor: pointer;
    border-radius: 8px;
    color: var(--hint);
    transition: all .2s;
}
.tab.active {
    background: var(--bg);
    color: var(--text);
    box-shadow: var(--card-shadow);
}

/* === Pages === */
.page { display: none; padding: 16px; padding-bottom: 80px; }
.page.active { display: block; }

/* === Category Filter === */
#category-filter {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 12px;
    -webkit-overflow-scrolling: touch;
}
.cat-btn {
    border: 1.5px solid var(--btn-bg);
    background: transparent;
    color: var(--btn-bg);
    padding: 6px 14px;
    border-radius: 20px;
    font-size: .82rem;
    cursor: pointer;
    white-space: nowrap;
    font-weight: 500;
    transition: all .2s;
}
.cat-btn.active {
    background: var(--btn-bg);
    color: var(--btn-text);
}

/* === Product Grid === */
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
    gap: var(--gap);
}
.product-card {
    background: var(--secondary-bg);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--card-shadow);
    transition: transform .15s;
}
.product-card:active { transform: scale(.97); }

.product-card img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    display: block;
}
.product-info {
    padding: 10px;
}
.product-info h3 {
    font-size: .9rem;
    margin-bottom: 2px;
    line-height: 1.3;
}
.product-info .desc {
    font-size: .75rem;
    color: var(--hint);
    margin-bottom: 6px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.product-info .price {
    font-weight: 700;
    color: var(--link);
    font-size: .95rem;
}
.product-info .add-btn {
    width: 100%;
    margin-top: 8px;
    padding: 8px;
    border: none;
    border-radius: 8px;
    background: var(--btn-bg);
    color: var(--btn-text);
    font-weight: 600;
    font-size: .85rem;
    cursor: pointer;
    transition: opacity .2s;
}
.product-info .add-btn:active { opacity: .8; }

/* === Cart === */
.cart-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--secondary-bg);
}
.cart-item img {
    width: 56px; height: 56px;
    border-radius: 8px;
    object-fit: cover;
}
.cart-item-info { flex: 1; }
.cart-item-info h4 { font-size: .9rem; }
.cart-item-info .ci-price { color: var(--link); font-weight: 600; font-size: .85rem; }

.qty-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}
.qty-controls button {
    width: 28px; height: 28px;
    border-radius: 50%;
    border: 1.5px solid var(--btn-bg);
    background: transparent;
    color: var(--btn-bg);
    font-size: 1rem;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.qty-controls span { font-weight: 600; min-width: 20px; text-align: center; }

#cart-summary {
    margin-top: 16px;
    padding: 16px;
    background: var(--secondary-bg);
    border-radius: var(--radius);
}
.summary-row {
    display: flex;
    justify-content: space-between;
    font-size: 1.1rem;
    margin-bottom: 14px;
}
.payment-methods {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* === Buttons === */
.btn {
    display: block;
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 10px;
    font-size: .95rem;
    font-weight: 600;
    cursor: pointer;
    text-align: center;
    transition: opacity .2s;
}
.btn:active { opacity: .8; }
.btn-primary { background: var(--btn-bg); color: var(--btn-text); }
.btn-secondary { background: var(--secondary-bg); color: var(--text); border: 1.5px solid var(--hint); }

/* === History === */
.purchase-card {
    background: var(--secondary-bg);
    border-radius: var(--radius);
    padding: 14px;
    margin-bottom: 10px;
}
.purchase-card .pc-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 6px;
}
.purchase-card .pc-header .method {
    font-size: .78rem;
    padding: 2px 8px;
    border-radius: 10px;
    background: var(--btn-bg);
    color: var(--btn-text);
}
.purchase-card .pc-items {
    font-size: .85rem;
    color: var(--hint);
}
.invoice-card {
    background: var(--secondary-bg);
    border-radius: var(--radius);
    padding: 12px;
    margin-bottom: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.invoice-card .status {
    font-size: .78rem;
    padding: 2px 8px;
    border-radius: 10px;
}
.invoice-card .status.paid { background: #d4edda; color: #155724; }
.invoice-card .status.unpaid { background: #fff3cd; color: #856404; }

/* === Admin === */
.admin-section {
    margin-bottom: 24px;
}
.admin-section h3 {
    margin-bottom: 10px;
    font-size: 1rem;
}
.admin-section form {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.admin-section input {
    padding: 10px 12px;
    border: 1.5px solid var(--hint);
    border-radius: 8px;
    font-size: .9rem;
    background: var(--bg);
    color: var(--text);
}
.admin-section input:focus {
    outline: none;
    border-color: var(--btn-bg);
}
.user-row {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid var(--secondary-bg);
    font-size: .88rem;
}

/* === Toast === */
.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: var(--bg);
    padding: 10px 20px;
    border-radius: 10px;
    font-size: .9rem;
    z-index: 200;
    transition: opacity .3s;
}
.toast.hidden { opacity: 0; pointer-events: none; }

/* === Utility === */
.hidden { display: none !important; }
.empty-msg { text-align: center; color: var(--hint); padding: 40px 0; }
.mt { margin-top: 24px; }

h2 { font-size: 1.1rem; margin-bottom: 12px; }
