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

:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #818cf8;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --info: #3b82f6;
    --bg: #f8fafc;
    --bg-dark: #0f172a;
    --card: #ffffff;
    --border: #e2e8f0;
    --text: #0f172a;
    --text-muted: #64748b;
    --sidebar-bg: #1e293b;
    --sidebar-hover: #334155;
    --sidebar-active: #6366f1;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.08);
    --transition: 0.2s ease;
}

html, body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; transition: var(--transition); }
a:hover { color: var(--primary-dark); }

code {
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SF Mono', Monaco, Consolas, monospace;
    font-size: 0.85em;
    color: #db2777;
}

/* ===== LAYOUT ===== */
.app { display: flex; min-height: 100vh; }

.sidebar {
    width: 260px;
    background: var(--sidebar-bg);
    color: #cbd5e1;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: transform var(--transition);
}

.sidebar-brand {
    padding: 22px 24px;
    font-size: 1.3rem;
    font-weight: 700;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}
.sidebar-brand i { color: var(--primary-light); font-size: 1.5rem; }

.sidebar-nav { flex: 1; padding: 16px 0; overflow-y: auto; }

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 24px;
    color: #cbd5e1;
    font-weight: 500;
    transition: var(--transition);
    border-left: 3px solid transparent;
}
.nav-item:hover {
    background: var(--sidebar-hover);
    color: #fff;
    border-left-color: var(--primary-light);
}
.nav-item.active {
    background: rgba(99,102,241,0.15);
    color: #fff;
    border-left-color: var(--primary);
}
.nav-item i { width: 20px; text-align: center; font-size: 1rem; }

.nav-divider {
    padding: 18px 24px 8px;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: #64748b;
}

.sidebar-footer {
    padding: 16px;
    border-top: 1px solid rgba(255,255,255,0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}
.user-info { display: flex; align-items: center; gap: 10px; overflow: hidden; }
.avatar {
    width: 38px; height: 38px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--primary-light));
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}
.user-name { color: #fff; font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-balance { color: #94a3b8; font-size: 0.78rem; }
.logout-btn {
    color: #94a3b8;
    padding: 8px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}
.logout-btn:hover { background: var(--danger); color: #fff; }

/* ===== MAIN ===== */
.main {
    flex: 1;
    margin-left: 260px;
    padding: 0 32px 32px;
    min-width: 0;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 20px 0;
    margin-bottom: 12px;
    border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.5rem; font-weight: 700; color: var(--text); }
.topbar-right { margin-left: auto; }
.menu-toggle {
    display: none;
    background: var(--card);
    border: 1px solid var(--border);
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text);
    font-size: 1.1rem;
}

/* ===== CARDS ===== */
.card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    margin-bottom: 20px;
    overflow: hidden;
    transition: var(--transition);
}
.card-header {
    padding: 16px 22px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background: #fafbfc;
}
.card-header h2 {
    font-size: 1.05rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 10px;
}
.card-header h2 i { color: var(--primary); }
.card-body { padding: 22px; }

/* ===== STATS ===== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}
.stat-card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    transition: var(--transition);
}
.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.stat-icon {
    width: 52px; height: 52px;
    border-radius: 14px;
    display: flex; align-items: center; justify-content: center;
    font-size: 1.4rem;
    color: #fff;
    flex-shrink: 0;
}
.bg-blue { background: linear-gradient(135deg, #3b82f6, #2563eb); }
.bg-green { background: linear-gradient(135deg, #10b981, #059669); }
.bg-orange { background: linear-gradient(135deg, #f59e0b, #d97706); }
.bg-purple { background: linear-gradient(135deg, #8b5cf6, #7c3aed); }
.bg-pink { background: linear-gradient(135deg, #ec4899, #db2777); }
.stat-label { color: var(--text-muted); font-size: 0.85rem; }
.stat-value { font-size: 1.5rem; font-weight: 700; color: var(--text); margin-top: 2px; }

/* ===== FORMS ===== */
.form-group { margin-bottom: 16px; }
.form-group label {
    display: block;
    font-weight: 500;
    font-size: 0.88rem;
    margin-bottom: 6px;
    color: #334155;
}
.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    background: #fff;
    color: var(--text);
    transition: 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(99,102,241,0.15);
}
.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}
@media (max-width: 640px) { .form-row { grid-template-columns: 1fr; } }

/* ===== TOGGLE SWITCH ===== */
.switch { position: relative; display: inline-block; width: 46px; height: 24px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: #cbd5e1;
    border-radius: 24px;
    transition: var(--transition);
}
.slider::before {
    content: "";
    position: absolute;
    height: 18px; width: 18px;
    left: 3px; bottom: 3px;
    background: #fff;
    border-radius: 50%;
    transition: var(--transition);
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.switch input:checked + .slider { background: var(--primary); }
.switch input:checked + .slider::before { transform: translateX(22px); }

/* ===== BUTTONS ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: 0.92rem;
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
    font-family: inherit;
    white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); color: #fff; box-shadow: 0 4px 12px rgba(99,102,241,0.35); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #059669; color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #dc2626; color: #fff; }
.btn-outline { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-outline:hover { background: #f1f5f9; border-color: #cbd5e1; }
.btn-sm { padding: 6px 12px; font-size: 0.82rem; }
.btn-lg { padding: 14px 28px; font-size: 1rem; }
.btn-block { width: 100%; }
.action-bar { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

/* ===== TABLES ===== */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}
.table th {
    background: #f8fafc;
    font-weight: 600;
    color: var(--text-muted);
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.table tr:hover td { background: #fafbfc; }
.table td:last-child { text-align: right; }

/* ===== ALERTS ===== */
.alert {
    padding: 12px 18px;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 0.92rem;
    border-left: 4px solid;
}
.alert-success { background: #f0fdf4; color: #166534; border-color: var(--success); }
.alert-danger { background: #fef2f2; color: #991b1b; border-color: var(--danger); }
.alert-warning { background: #fffbeb; color: #92400e; border-color: var(--warning); }
.alert-info { background: #eff6ff; color: #1e40af; border-color: var(--info); }

/* ===== BADGES ===== */
.badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.72rem;
    font-weight: 600;
}
.badge-success { background: #d1fae5; color: #065f46; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-info { background: #dbeafe; color: #1e40af; }

/* ===== AUTH PAGES ===== */
.auth-body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
}
.auth-box {
    background: #fff;
    border-radius: 20px;
    padding: 40px 36px;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 25px 50px rgba(0,0,0,0.25);
}
.auth-logo {
    width: 64px; height: 64px;
    margin: 0 auto 16px;
    border-radius: 16px;
    background: linear-gradient(135deg, var(--primary), var(--primary-light));
    display: flex; align-items: center; justify-content: center;
    color: #fff;
    font-size: 1.8rem;
}
.auth-box h1 { text-align: center; font-size: 1.6rem; margin-bottom: 6px; }
.auth-sub { text-align: center; color: var(--text-muted); margin-bottom: 24px; font-size: 0.9rem; }
.auth-footer { text-align: center; margin-top: 20px; color: var(--text-muted); font-size: 0.9rem; }

/* ===== UTILITIES ===== */
.text-muted { color: var(--text-muted); }
.text-center { text-align: center; }
.mt-2 { margin-top: 16px; }
.mb-2 { margin-bottom: 16px; }

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .main { margin-left: 0; padding: 0 16px 16px; }
    .menu-toggle { display: inline-flex; align-items: center; justify-content: center; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 500px) {
    .stats-grid { grid-template-columns: 1fr; }
    .card-body { padding: 16px; }
    .topbar h1 { font-size: 1.2rem; }
}
/* Keep content visible without overriding the grid layout of dashboard statistics. */
.main > .card, .main > .stats-grid { visibility: visible; opacity: 1; }
.stats-grid { display: grid; }
.page-error { display:block; margin:16px 0; padding:14px 16px; border:1px solid #fca5a5; border-radius:10px; background:#fff1f2; color:#881337; overflow-wrap:anywhere; }
.table-responsive { width:100%; overflow-x:auto; -webkit-overflow-scrolling:touch; }
button, a, input, select, textarea { touch-action: manipulation; }
@media (max-width:900px) { .main { width:100%; } .topbar { min-height:72px; } .menu-toggle { min-width:44px; min-height:44px; } .sidebar.open { box-shadow:0 0 0 9999px rgba(15,23,42,.28); } }

/* Defensive layout rules for narrow screens and Safari */
.app, .main { min-height: 100vh; }
.main > .card, .main > .stats-grid, .main > form, .main > .page-error { position: relative; z-index: 1; }
button, a { -webkit-tap-highlight-color: transparent; }
@media (max-width: 900px) { .sidebar { pointer-events: none; } .sidebar.open { pointer-events: auto; } .main { overflow-wrap: anywhere; } }
