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

:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-light: #e0e7ff;
    --success: #16a34a;
    --success-light: #dcfce7;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --warning: #f59e0b;
    --warning-light: #fef3c7;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;
    --white: #ffffff;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.1), 0 2px 4px rgba(0,0,0,0.06);
    --transition: 0.2s ease;
}

html { font-size: 16px; scroll-behavior: smooth; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
    background: var(--gray-50);
    color: var(--gray-800);
    line-height: 1.6;
    min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; transition: color var(--transition); }
a:hover { color: var(--primary-dark); }
img { max-width: 100%; height: auto; }

/* ========== Container ========== */
.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 1rem;
}

/* ========== Navbar ========== */
.navbar {
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow);
}
.nav-container {
    max-width: 960px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    height: 56px;
}
.nav-brand {
    font-size: 1.125rem;
    font-weight: 700;
    color: var(--primary);
    white-space: nowrap;
}
.nav-toggle {
    display: none;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.25rem;
    color: var(--gray-600);
    line-height: 1;
}
.nav-menu {
    display: flex;
    list-style: none;
    gap: 0.25rem;
    align-items: center;
    flex-wrap: wrap;
}
.nav-menu a {
    display: block;
    padding: 0.375rem 0.75rem;
    border-radius: var(--radius);
    color: var(--gray-600);
    font-size: 0.875rem;
    white-space: nowrap;
    transition: all var(--transition);
}
.nav-menu a:hover,
.nav-menu a.active {
    background: var(--primary-light);
    color: var(--primary);
}

/* ========== Cards ========== */
.card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem;
    margin-bottom: 1rem;
}
.card-title {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--gray-100);
    color: var(--gray-800);
}

/* ========== Forms ========== */
.form-group { margin-bottom: 1rem; }
.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--gray-700);
    margin-bottom: 0.375rem;
}
.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 0.625rem 0.75rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 0.9375rem;
    color: var(--gray-800);
    background: var(--white);
    transition: border-color var(--transition), box-shadow var(--transition);
    font-family: inherit;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79,70,229,0.1);
}

/* ========== Buttons ========== */
.btn {
    display: inline-block;
    padding: 0.625rem 1.25rem;
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-size: 0.9375rem;
    font-weight: 500;
    cursor: pointer;
    text-align: center;
    transition: all var(--transition);
    line-height: 1.5;
    font-family: inherit;
}
.btn-primary {
    background: var(--primary);
    color: var(--white);
}
.btn-primary:hover { background: var(--primary-dark); color: var(--white); }
.btn-outline {
    background: transparent;
    border-color: var(--gray-300);
    color: var(--gray-700);
}
.btn-outline:hover { background: var(--gray-50); border-color: var(--gray-400); }
.btn-danger { background: var(--danger); color: var(--white); }
.btn-danger:hover { background: #b91c1c; }
.btn-sm { padding: 0.375rem 0.75rem; font-size: 0.8125rem; }
.btn-block { display: block; width: 100%; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

/* ========== Alerts ========== */
.alert {
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    font-size: 0.875rem;
    border: 1px solid transparent;
}
.alert-success { background: var(--success-light); border-color: #bbf7d0; color: #166534; }
.alert-error { background: var(--danger-light); border-color: #fecaca; color: #991b1b; }
.alert-info { background: var(--primary-light); border-color: #c7d2fe; color: #3730a3; }

/* ========== Auth Pages ========== */
.auth-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: linear-gradient(135deg, #e0e7ff 0%, #f3f4f6 100%);
}
.auth-card {
    background: var(--white);
    padding: 2rem;
    border-radius: 12px;
    box-shadow: var(--shadow-md);
    width: 100%;
    max-width: 400px;
}
.auth-title { font-size: 1.5rem; font-weight: 700; text-align: center; color: var(--gray-800); }
.auth-subtitle { text-align: center; color: var(--gray-500); margin: 0.5rem 0 1.5rem; font-size: 0.875rem; }
.auth-footer { text-align: center; margin-top: 1.5rem; font-size: 0.875rem; color: var(--gray-500); }

/* ========== Page Header ========== */
.page-header { margin-bottom: 1.5rem; }
.page-header h1 { font-size: 1.5rem; font-weight: 700; color: var(--gray-800); }
.page-header p { color: var(--gray-500); font-size: 0.875rem; margin-top: 0.25rem; }

/* ========== Stats Grid ========== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}
.stat-card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1rem;
    text-align: center;
}
.stat-number {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}
.stat-label { font-size: 0.8125rem; color: var(--gray-500); margin-top: 0.25rem; }

/* ========== Table ========== */
.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}
.table th {
    text-align: left;
    padding: 0.75rem;
    background: var(--gray-50);
    color: var(--gray-600);
    font-weight: 600;
    border-bottom: 2px solid var(--gray-200);
    white-space: nowrap;
}
.table td {
    padding: 0.75rem;
    border-bottom: 1px solid var(--gray-100);
}
.table tr:hover { background: var(--gray-50); }

/* ========== Badge ========== */
.badge {
    display: inline-block;
    padding: 0.125rem 0.5rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
}
.badge-success { background: transparent; color: var(--success); }
.badge-danger { background: transparent; color: var(--danger); }
.badge-warning { background: transparent; color: #92400e; }

/* ========== Utilities ========== */
.text-center { text-align: center; }
.text-primary { color: var(--primary); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-muted { color: var(--gray-500); }

/* ========== Pagination ========== */
.pagination {
    display: flex;
    gap: 0.25rem;
    justify-content: center;
    margin-top: 1rem;
    flex-wrap: wrap;
}
.page-link {
    display: inline-block;
    padding: 0.375rem 0.75rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 0.875rem;
    color: var(--gray-600);
}
.page-link.active { background: var(--primary); color: var(--white); border-color: var(--primary); }
.page-link:hover:not(.active) { background: var(--gray-50); }

/* ========== Balance Card ========== */
.balance-card { padding: 2rem 1rem; }
.balance-label { font-size: 0.875rem; color: var(--gray-500); margin-bottom: 0.25rem; }
.balance-amount { font-size: 2.25rem; font-weight: 700; color: var(--primary); }

/* ========== Invite Code ========== */
.invite-code-highlight {
    font-family: 'Courier New', monospace;
    font-weight: 700;
    color: var(--primary);
    background: var(--primary-light);
    padding: 0.125rem 0.5rem;
    border-radius: 4px;
    letter-spacing: 1px;
}
.invite-code-display {
    font-size: 2rem;
    font-weight: 700;
    font-family: 'Courier New', monospace;
    color: var(--primary);
    letter-spacing: 4px;
    padding: 1rem;
    background: var(--primary-light);
    border-radius: var(--radius);
    border: 2px dashed var(--primary);
    display: inline-block;
    margin: 0.5rem 0;
}

/* ========== Commission Rules ========== */
.commission-rules { display: flex; flex-direction: column; gap: 0.75rem; }
.rule-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem;
    background: var(--gray-50);
    border-radius: var(--radius);
}
.rule-level {
    font-weight: 700;
    color: var(--primary);
    min-width: 80px;
    font-size: 0.875rem;
}
.rule-desc { font-size: 0.875rem; color: var(--gray-600); }

/* ========== Order Summary ========== */
.order-summary {
    background: var(--primary-light);
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: 1rem;
}
.summary-row {
    display: flex;
    justify-content: space-between;
    padding: 0.25rem 0;
    font-size: 0.9375rem;
    color: var(--gray-600);
}
.summary-total {
    font-weight: 700;
    font-size: 1.125rem;
    color: var(--primary);
    border-top: 1px solid rgba(79,70,229,0.2);
    padding-top: 0.5rem;
    margin-top: 0.25rem;
}

/* ========== Info Grid ========== */
.info-grid { display: flex; flex-direction: column; gap: 0.75rem; }
.info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--gray-100);
}
.info-label { color: var(--gray-500); font-size: 0.875rem; }
.info-value { font-weight: 500; font-size: 0.9375rem; color: var(--gray-800); }

/* ========== Quick Actions ========== */
.quick-actions {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}

/* ========== Admin Sidebar Layout ========== */
.admin-layout { display: flex; min-height: 100vh; }
.admin-sidebar {
    width: 200px;
    flex-shrink: 0;
    background: var(--gray-800);
    color: #fff;
    padding: 1.25rem 0;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    overflow-y: auto;
    z-index: 50;
}
.sidebar-brand {
    font-size: 1.125rem;
    font-weight: 700;
    padding: 0 1.25rem 1rem;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    margin-bottom: 0.75rem;
    color: #fff;
}
.admin-sidebar a {
    display: block;
    padding: 0.625rem 1.25rem;
    color: rgba(255,255,255,0.7);
    font-size: 0.875rem;
    transition: all 0.15s;
    border-left: 3px solid transparent;
}
.admin-sidebar a:hover {
    background: rgba(255,255,255,0.08);
    color: #fff;
}
.admin-sidebar a.active {
    background: rgba(255,255,255,0.12);
    color: #fff;
    border-left-color: var(--primary);
}
.nav-badge {
    display: inline-block;
    min-width: 18px;
    height: 18px;
    line-height: 18px;
    border-radius: 9px;
    background: var(--danger);
    color: #fff;
    font-size: 0.6875rem;
    font-weight: 600;
    text-align: center;
    padding: 0 5px;
    margin-left: 0.375rem;
    vertical-align: middle;
}
.nav-badge:empty { display: none; }

.sidebar-logout {
    margin-top: 1rem;
    padding-top: 0.75rem;
    border-top: 1px solid rgba(255,255,255,0.1);
}
.admin-main {
    flex: 1;
    margin-left: 200px;
    padding: 1rem;
    min-width: 0;
}

/* ========== Modal ========== */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}
.modal-overlay.active { display: flex; }
.modal {
    background: var(--white);
    border-radius: 12px;
    padding: 1.5rem;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 20px 25px rgba(0,0,0,0.15);
}
.modal h3 { margin-bottom: 1rem; }

/* ========== Responsive - Tablet & Up ========== */
@media (min-width: 640px) {
    .container { padding: 1.5rem; }
    .card { padding: 1.5rem; margin-bottom: 1.25rem; }
    .auth-card { padding: 2.5rem; }
    .info-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0;
    }
    .info-item { padding: 0.75rem 0; }
}

/* ========== Responsive - Mobile Nav ========== */
@media (max-width: 639px) {
    .nav-toggle { display: block; }
    .nav-menu {
        display: none;
        position: absolute;
        top: 56px;
        left: 0;
        right: 0;
        background: var(--white);
        border-bottom: 1px solid var(--gray-200);
        flex-direction: column;
        padding: 0.5rem 1rem 1rem;
        box-shadow: var(--shadow-md);
    }
    .nav-menu.active { display: flex; }
    .nav-menu a {
        width: 100%;
        padding: 0.625rem 0.75rem;
    }

    /* admin layout */
    .admin-sidebar {
        width: 100%;
        position: static;
        display: flex;
        flex-wrap: wrap;
        gap: 0.25rem;
        padding: 0.75rem;
        border-radius: var(--radius);
        margin-bottom: 1rem;
    }
    .admin-sidebar a { margin-bottom: 0; font-size: 0.8125rem; border-left: none; border-radius: var(--radius); }
    .sidebar-brand, .sidebar-logout { display: none; }
    .admin-main { margin-left: 0; }

    /* stats */
    .stats-grid { grid-template-columns: 1fr 1fr; }
    .stat-number { font-size: 1.25rem; }

    /* table condense */
    .table th, .table td { padding: 0.5rem; font-size: 0.8125rem; }

    .invite-code-display { font-size: 1.5rem; letter-spacing: 2px; }
    .balance-amount { font-size: 1.75rem; }
}

/* ========== Loading Spinner ========== */
.spinner {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    vertical-align: middle;
    margin-right: 0.375rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ========== Bottom Navigation ========== */
.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--white);
    border-top: 1px solid var(--gray-200);
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 56px;
    z-index: 100;
    box-shadow: 0 -1px 3px rgba(0,0,0,0.06);
    padding-bottom: env(safe-area-inset-bottom, 0);
}
.bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    height: 100%;
    flex: 1;
    color: var(--gray-400);
    text-decoration: none;
    font-size: 0.6875rem;
    font-weight: 500;
    position: relative;
    transition: color var(--transition);
    -webkit-tap-highlight-color: transparent;
}
.bottom-nav-item .nav-icon {
    width: 22px;
    height: 22px;
}
.bottom-nav-item.active {
    color: var(--primary);
}
.bottom-nav-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 24px;
    height: 3px;
    background: var(--primary);
    border-radius: 0 0 3px 3px;
}
body.has-bottom-nav {
    padding-bottom: 66px;
}
body.has-bottom-nav .toast {
    bottom: 76px;
    top: auto;
}
@media (min-width: 640px) {
    .bottom-nav {
        max-width: 480px;
        left: 50%;
        transform: translateX(-50%);
        border-radius: 12px 12px 0 0;
    }
}

/* ========== Toast ========== */
.toast {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 2000;
    padding: 1rem 1.25rem;
    border-radius: var(--radius);
    color: #fff;
    font-size: 0.875rem;
    box-shadow: var(--shadow-md);
    animation: toastIn 0.3s ease;
    max-width: 350px;
}
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-info { background: var(--primary); }
@keyframes toastIn {
    from { opacity: 0; transform: translateY(-1rem); }
    to { opacity: 1; transform: translateY(0); }
}

/* ========== Poll Flash ========== */
@keyframes pollFlash {
    0% { background-color: transparent; }
    30% { background-color: var(--primary-light); }
    100% { background-color: transparent; }
}
.poll-flash {
    animation: pollFlash 0.8s ease;
}

