* { box-sizing: border-box; }

:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-light: #eef2ff;
    --accent: #6d5ce8;
    --success: #16a34a;
    --success-light: #dcfce7;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --warning: #d97706;
    --warning-light: #fef3c7;
    --neutral: #64748b;
    --neutral-light: #eef1f5;
    --bg: #f5f6fb;
    --surface: #ffffff;
    --border: #e6e8f0;
    --text: #1e2433;
    --text-muted: #6b7280;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 4px 12px rgba(16, 24, 40, 0.06);
    --th-bg: #fafbfd;
    --row-hover: #fafbff;
    --logo-bg: #ffffff;
    color-scheme: light;
}

html[data-theme="dark"] {
    --primary: #6366f1;
    --primary-dark: #818cf8;
    --primary-light: #1e1b4b;
    --accent: #8b7ff0;
    --success: #22c55e;
    --success-light: #14321f;
    --danger: #f87171;
    --danger-light: #3a1a1a;
    --warning: #fbbf24;
    --warning-light: #3a2c0d;
    --neutral: #94a3b8;
    --neutral-light: #1e2635;
    --bg: #12141c;
    --surface: #1a1d29;
    --border: #2a2e3d;
    --text: #e4e6ec;
    --text-muted: #9aa1b1;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.25), 0 4px 14px rgba(0, 0, 0, 0.35);
    --th-bg: #161923;
    --row-hover: #20242f;
    --logo-bg: #ffffff;
    color-scheme: dark;
}

body {
    margin: 0;
    font-family: 'Segoe UI', system-ui, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    transition: background-color 0.2s ease, color 0.2s ease;
}

a { color: inherit; }

/* ---------- App shell / sidebar ---------- */

.app-shell {
    min-height: 100vh;
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    z-index: 90;
}

.app-shell.sidebar-open .sidebar-backdrop { display: block; }

.sidebar {
    width: 240px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 20px 14px;
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
}

.app-shell.sidebar-open .sidebar { transform: translateX(0); }

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--primary);
    padding: 0 10px 20px;
}

.sidebar-brand i { font-size: 1.3rem; }

.sidebar-brand .brand-logo {
    width: 44px;
    height: 44px;
    object-fit: contain;
    border-radius: 8px;
    background: var(--logo-bg);
    padding: 2px;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 10px;
    text-decoration: none;
    color: var(--text-muted);
    font-size: 0.93rem;
    font-weight: 500;
    transition: background 0.15s, color 0.15s;
}

.nav-item i { font-size: 1.1rem; width: 20px; text-align: center; }

.nav-item:hover {
    background: var(--primary-light);
    color: var(--primary);
}

.nav-item.active {
    background: var(--primary);
    color: #fff;
}

.sidebar-user {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 10px 0;
    border-top: 1px solid var(--border);
    margin-top: 12px;
}

.avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}

.user-info {
    display: flex;
    flex-direction: column;
    line-height: 1.25;
    flex: 1;
    min-width: 0;
}

.user-info strong { font-size: 0.88rem; }
.user-info small { color: var(--text-muted); text-transform: capitalize; }

.logout-link {
    color: var(--danger);
    text-decoration: none;
    font-size: 1.1rem;
    padding: 6px;
}

.main-area {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 18px 28px;
    display: flex;
    align-items: center;
    gap: 16px;
}

.burger-btn {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 8px 11px;
    font-size: 1.1rem;
    cursor: pointer;
    line-height: 1;
}

.burger-btn:hover { background: var(--neutral-light); }

.topbar-title {
    margin: 0;
    font-size: 1.3rem;
    font-weight: 700;
    flex: 1;
}

.theme-toggle {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 8px 11px;
    font-size: 1.05rem;
    cursor: pointer;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.theme-toggle:hover { background: var(--neutral-light); }

.theme-toggle .bi-sun-fill { display: none; }
html[data-theme="dark"] .theme-toggle .bi-moon-stars-fill { display: none; }
html[data-theme="dark"] .theme-toggle .bi-sun-fill { display: inline; }

.content {
    padding: 24px 28px;
    max-width: 1300px;
}

.content-auth {
    max-width: none;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

/* ---------- Login ---------- */

.login-page {
    position: relative;
    min-height: 100vh;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: radial-gradient(ellipse at 50% -10%, #1b2450 0%, #0a0c16 55%, #05060b 100%);
}

#loginCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    z-index: 0;
}

.login-page::before {
    content: "";
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 50% 30%, rgba(79, 70, 229, 0.16), transparent 60%);
    pointer-events: none;
    z-index: 1;
}

.login-box {
    position: relative;
    z-index: 2;
    width: 380px;
    max-width: calc(100vw - 32px);
    background: rgba(20, 23, 38, 0.55);
    backdrop-filter: blur(18px) saturate(140%);
    -webkit-backdrop-filter: blur(18px) saturate(140%);
    padding: 40px 34px;
    border-radius: 20px;
    border: 1px solid rgba(148, 163, 255, 0.18);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45), 0 0 60px rgba(79, 70, 229, 0.12), inset 0 1px 0 rgba(255, 255, 255, 0.06);
    text-align: center;
    color: #e6e8f5;
    animation: loginBoxIn 0.6s cubic-bezier(.2, .8, .2, 1);
}

@keyframes loginBoxIn {
    from { opacity: 0; transform: translateY(18px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.login-box h1 {
    color: #f2f3fb;
}

.login-box .login-icon {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6rem;
    margin: 0 auto 14px;
}

.login-box .login-logo {
    width: 130px;
    height: 130px;
    object-fit: contain;
    border-radius: 20px;
    background: radial-gradient(circle, rgba(79, 70, 229, 0.25), transparent 70%);
    padding: 6px;
    margin: 0 auto 12px;
    display: block;
    filter: drop-shadow(0 0 22px rgba(93, 92, 232, 0.55));
    animation: logoFloat 4.5s ease-in-out infinite;
}

@keyframes logoFloat {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.login-page .flash-error {
    background: rgba(220, 38, 38, 0.15);
    color: #fca5a5;
    border: 1px solid rgba(248, 113, 113, 0.35);
}

.login-page .field label {
    color: #aab0c9;
}

.login-page input {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(148, 163, 255, 0.22);
    color: #f2f3fb;
}

.login-page input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(109, 92, 232, 0.25);
}

.login-page input::placeholder { color: #6b7290; }

.login-page .password-toggle { color: #aab0c9; }
.login-page .password-toggle:hover { color: #fff; }

.login-page button[type="submit"] {
    background: linear-gradient(135deg, var(--primary), var(--accent));
    box-shadow: 0 6px 18px rgba(79, 70, 229, 0.35);
}

.login-page button[type="submit"]:hover {
    background: linear-gradient(135deg, var(--primary-dark), var(--primary));
    box-shadow: 0 8px 22px rgba(79, 70, 229, 0.5);
}

.password-field {
    position: relative;
}

.password-field input {
    padding-right: 40px;
}

.password-toggle {
    position: absolute;
    top: 50%;
    right: 8px;
    transform: translateY(-50%);
    background: none;
    border: none;
    padding: 4px;
    color: var(--text-muted, #888);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.login-box h1 {
    font-size: 1.25rem;
    margin: 0 0 22px;
}

/* ---------- Typography ---------- */

h1, h2, h3 { color: var(--text); }
h2 { font-size: 1.05rem; font-weight: 700; margin: 28px 0 14px; }

.page-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 18px;
}

/* ---------- Flash ---------- */

.flash {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border-radius: 10px;
    margin-bottom: 18px;
    font-size: 0.92rem;
}

.flash-success { background: var(--success-light); color: var(--success); }
.flash-error { background: var(--danger-light); color: var(--danger); }

/* ---------- Cards ---------- */

.card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 22px;
    margin-bottom: 22px;
    box-shadow: var(--shadow);
}

.card h2 { margin-top: 0; }

.cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 18px 20px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 14px;
}

.stat-icon {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    flex-shrink: 0;
    background: var(--primary-light);
    color: var(--primary);
}

.stat-icon.success { background: var(--success-light); color: var(--success); }
.stat-icon.danger { background: var(--danger-light); color: var(--danger); }
.stat-icon.warning { background: var(--warning-light); color: var(--warning); }

.stat-card .label { font-size: 0.82rem; color: var(--text-muted); }
.stat-card .value { font-size: 1.35rem; font-weight: 700; }

.charts-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 16px;
    margin-bottom: 24px;
}

@media (max-width: 900px) {
    .charts-grid { grid-template-columns: 1fr; }
}

.chart-card canvas { max-height: 280px; }

/* ---------- Tables ---------- */

table { width: 100%; border-collapse: collapse; background: var(--surface); }

table.card-table {
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}

th, td {
    padding: 12px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 0.88rem;
}

th {
    background: var(--th-bg);
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.72rem;
    letter-spacing: 0.03em;
}

tbody tr:hover { background: var(--row-hover); }
tfoot th { background: var(--primary-light); color: var(--primary-dark); }

/* ---------- Badges ---------- */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
}

.badge-success { background: var(--success-light); color: var(--success); }
.badge-danger { background: var(--danger-light); color: var(--danger); }
.badge-warning { background: var(--warning-light); color: var(--warning); }
.badge-neutral { background: var(--neutral-light); color: var(--neutral); }

tr.statut-retour td { color: var(--danger); }
tr.statut-retour { background: var(--danger-light); }

/* ---------- Forms ---------- */

form.inline-form {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    align-items: end;
    margin-bottom: 20px;
}

form.inline-form > div { display: flex; flex-direction: column; gap: 4px; }

form.inline-form label, form.stacked-form label {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 600;
}

form.stacked-form .field { margin-bottom: 16px; display: flex; flex-direction: column; gap: 6px; text-align: left; }

input, select {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 9px;
    font-size: 0.9rem;
    background: var(--surface);
    color: var(--text);
}

input:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.status-select {
    font-weight: 600;
    border-width: 1px;
}

.status-select.statut-livre_paye    { background: #d1fae5; color: #065f46; border-color: #6ee7b7; }
.status-select.statut-livre_facture { background: #cffafe; color: #0e7490; border-color: #67e8f9; }
.status-select.statut-retour        { background: var(--danger-light); color: #b91c1c; border-color: #fecaca; }
.status-select.statut-ramassage     { background: var(--warning-light); color: #b45309; border-color: #fde68a; }
.status-select.statut-en_attente    { background: var(--neutral-light); color: var(--neutral); border-color: #dfe3ea; }

/* ---------- Statut totaux ---------- */

.statut-totaux-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
    gap: 10px;
    margin-bottom: 20px;
}

.statut-totaux-card {
    background: var(--card-bg, #f5f6fb);
    border: 1.5px solid color-mix(in srgb, var(--card-color) 25%, transparent);
    border-radius: 10px;
    padding: 12px 14px;
    text-decoration: none;
    color: var(--card-color, #1e2433);
    transition: box-shadow .15s, transform .12s;
    display: block;
}
.statut-totaux-card:hover {
    box-shadow: 0 4px 14px rgba(0,0,0,.10);
    transform: translateY(-2px);
}
.statut-totaux-actif {
    box-shadow: 0 0 0 2.5px var(--card-color);
}

.statut-totaux-header {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 600;
    font-size: .82rem;
    margin-bottom: 8px;
}

.statut-totaux-nb {
    font-size: 1.6rem;
    font-weight: 700;
    line-height: 1.1;
}
.statut-totaux-nb small {
    font-size: .75rem;
    font-weight: 400;
    opacity: .75;
}

.statut-totaux-ca,
.statut-totaux-marge {
    font-size: .78rem;
    margin-top: 3px;
    opacity: .85;
}

/* ---------- Buttons ---------- */

button, .btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 9px;
    padding: 10px 16px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}

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

.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
}

.btn-ghost:hover { background: var(--neutral-light); color: var(--text); }

.btn-small { padding: 6px 10px; font-size: 0.78rem; border-radius: 7px; }

.row-actions { display: flex; gap: 8px; }

/* ---------- Misc layout ---------- */

.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

@media (max-width: 900px) {
    .grid-2, .charts-grid { grid-template-columns: 1fr; }
}
