/* ─── Variables ──────────────────────────────────────────── */
:root {
    /* Valeurs par défaut (thème sombre bleu) — surchargées par /theme.css */
    --bg:          #0f1117;
    --bg-card:     #1a1d27;
    --bg-input:    #22263a;
    --border:      #2e3347;
    --accent:      #4f8ef7;
    --accent-dim:  #4f8ef733;
    --green:       #34d399;
    --red:         #f87171;
    --yellow:      #fbbf24;
    --text:        #e2e8f0;
    --text-muted:  #8892a4;
    --table-hover: rgba(79,142,247,0.04);
    --scroll-bg:   #1a1d27;
    --scroll-thumb:#3e4567;
    --font-mono:   'JetBrains Mono', 'Fira Code', 'Courier New', monospace;
    --font-sans:   'Inter', system-ui, sans-serif;
    --radius:      8px;
    --shadow:      0 4px 20px rgba(0,0,0,0.4);
}

/* ─── Reset ──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; }
body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.6;
}

/* ─── Anti-débordement horizontal ────────────────────────────
   Empêche un élément trop large (canvas Chart.js avant son premier
   redimensionnement, tableau de données dense, contenu collé sans
   wrap) de forcer un scroll horizontal sur toute la page — ce qui
   décale le menu hamburger hors écran sur mobile. Ces règles sont
   des filets de sécurité : elles ne remplacent pas une mise en page
   propre mais garantissent qu'aucune page ne peut "casser" le layout
   global, y compris les pages ajoutées ultérieurement. */
html, body { max-width: 100%; overflow-x: hidden; }
canvas { max-width: 100% !important; }
img, svg { max-width: 100%; }
.card { max-width: 100%; }

/* ─── Layout ─────────────────────────────────────────────── */
.wrapper { display: flex; min-height: 100vh; }

/* ─── Sidebar ────────────────────────────────────────────── */
.sidebar {
    width: 220px;
    background: var(--bg-card);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1.5rem 0;
    position: fixed;
    top: 0; left: 0;
    height: 100vh;
    z-index: 100;
}
.sidebar-brand {
    padding: 0 1.5rem 1.5rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 1rem;
}
.sidebar-brand h1 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 0.05em;
    text-transform: uppercase;
}
.sidebar-brand p {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 0.2rem;
}
.nav-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.65rem 1.5rem;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.875rem;
    font-weight: 500;
    transition: all 0.15s;
    border-left: 3px solid transparent;
}
.nav-item:hover { color: var(--text); background: rgba(79,142,247,0.07); }
.nav-item.active {
    color: var(--accent);
    border-left-color: var(--accent);
    background: rgba(79,142,247,0.10);
}
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar-footer {
    margin-top: auto;
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border);
}
.sidebar-footer a {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-decoration: none;
}
.sidebar-footer a:hover { color: var(--red); }

/* ─── Main content ───────────────────────────────────────── */
.main { margin-left: 220px; flex: 1; padding: 2rem; max-width: 100%; overflow-x: hidden; }
.page-header { margin-bottom: 2rem; }
.page-header h2 { font-size: 1.5rem; font-weight: 700; color: var(--text); }
.page-header p  { color: var(--text-muted); font-size: 0.875rem; margin-top: 0.25rem; }

/* ─── Cards ──────────────────────────────────────────────── */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
}
.card-title {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

/* ─── KPI Grid ───────────────────────────────────────────── */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 2rem; }
.kpi-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem 1.5rem; }
.kpi-label { font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); margin-bottom: 0.4rem; }
.kpi-value { font-family: var(--font-mono); font-size: 1.75rem; font-weight: 700; color: var(--text); }
.kpi-value.positive { color: var(--green); }
.kpi-value.negative { color: var(--red); }
.kpi-sub { font-size: 0.75rem; color: var(--text-muted); margin-top: 0.25rem; }

/* ─── Grid 2 cols ────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; margin-bottom: 1.5rem; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

/* ─── Tables ─────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
thead th {
    text-align: left;
    padding: 0.6rem 1rem;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
}
tbody td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    color: var(--text);
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--table-hover); }
.mono { font-family: var(--font-mono); }

/* ─── Badges ─────────────────────────────────────────────── */
.badge {
    display: inline-block;
    padding: 0.2em 0.7em;
    border-radius: 20px;
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}
.badge-green  { background: rgba(52,211,153,0.15); color: var(--green); }
.badge-red    { background: rgba(248,113,113,0.15); color: var(--red); }
.badge-yellow { background: rgba(251,191,36,0.15);  color: var(--yellow); }
.badge-blue   { background: rgba(79,142,247,0.15);  color: var(--accent); }

/* ─── Buttons ────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 1.1rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    text-decoration: none;
    transition: all 0.15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #6ba3ff; }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text-muted); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: rgba(248,113,113,0.15); color: var(--red); border: 1px solid rgba(248,113,113,0.3); }
.btn-danger:hover { background: rgba(248,113,113,0.3); }
.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.75rem; }

/* ─── Forms ──────────────────────────────────────────────── */
.form-group { margin-bottom: 1.25rem; }
label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--text-muted); margin-bottom: 0.4rem; }
input, select, textarea {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.6rem 0.9rem;
    color: var(--text);
    font-size: 0.875rem;
    font-family: var(--font-sans);
    transition: border-color 0.15s;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(79,142,247,0.15);
}
.field-error { font-size: 0.75rem; color: var(--red); margin-top: 0.3rem; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }

/* ─── Alerts ─────────────────────────────────────────────── */
.alert {
    padding: 0.85rem 1.2rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    margin-bottom: 1.5rem;
    border-left: 4px solid;
}
.alert-success { background: rgba(52,211,153,0.08); border-color: var(--green); color: var(--green); }
.alert-error   { background: rgba(248,113,113,0.08); border-color: var(--red);   color: var(--red); }
.alert-warning { background: rgba(251,191,36,0.08);  border-color: var(--yellow); color: var(--yellow); }

/* ─── Donut chart (CSS pur) ──────────────────────────────── */
.chart-container { display: flex; align-items: center; gap: 2rem; flex-wrap: wrap; }
.legend { flex: 1; min-width: 160px; }
.legend-item { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.5rem; font-size: 0.82rem; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

/* ─── Donut SVG ──────────────────────────────────────────── */
.donut-svg { overflow: visible; }

/* ─── Rebalancement bar ──────────────────────────────────── */
.reb-bar-track {
    height: 6px;
    background: var(--bg-input);
    border-radius: 3px;
    overflow: hidden;
    width: 100px;
    display: inline-block;
    vertical-align: middle;
}
.reb-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s;
}

/* ─── Login page ─────────────────────────────────────────── */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
}
.login-box {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 2.5rem 2rem;
    width: 100%;
    max-width: 380px;
    box-shadow: var(--shadow);
}
.login-box h1 {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 0.3rem;
}
.login-box p { font-size: 0.8rem; color: var(--text-muted); margin-bottom: 2rem; }

/* ─── Snapshot table ─────────────────────────────────────── */
.snap-table td { font-family: var(--font-mono); font-size: 0.82rem; }

/* ─── Topbar mobile (hamburger) ──────────────────────────── */
.topbar {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 56px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    z-index: 200;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
}
.topbar-brand {
    font-size: 1rem;
    font-weight: 700;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.hamburger {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.4rem;
    color: var(--text);
    display: flex;
    flex-direction: column;
    gap: 5px;
}
.hamburger span {
    display: block;
    width: 24px;
    height: 2px;
    background: var(--text);
    border-radius: 2px;
    transition: all 0.25s;
}
.hamburger.open span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.hamburger.open span:nth-child(2) { opacity: 0; }
.hamburger.open span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

/* Overlay sombre derrière la sidebar ouverte */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 149;
}
.sidebar-overlay.visible { display: block; }

/* ─── Responsive ─────────────────────────────────────────── */
@media (max-width: 768px) {
    /* Topbar visible, sidebar cachée par défaut */
    .topbar { display: flex; }
    .main {
        margin-left: 0;
        padding: 1rem;
        padding-top: calc(56px + 1rem); /* compenser la topbar fixe */
    }
    .sidebar {
        transform: translateX(-240px);
        transition: transform 0.28s cubic-bezier(0.4,0,0.2,1);
        width: 240px;
        z-index: 150;
        top: 0;
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0,0,0,0.5);
    }

    /* KPIs en 2 colonnes sur mobile */
    .kpi-grid { grid-template-columns: 1fr 1fr; gap: 0.75rem; }
    .kpi-value { font-size: 1.3rem; }

    /* Page header plus compact */
    .page-header { margin-bottom: 1.25rem; }
    .page-header h2 { font-size: 1.2rem; }

    /* Cards moins de padding */
    .card { padding: 1rem; }

    /* form-row passe en 1 colonne */
    .form-row { grid-template-columns: 1fr; }

    /* Tables : scroll horizontal + texte plus petit */
    .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    table { font-size: 0.78rem; }
    thead th { padding: 0.5rem 0.6rem; }
    tbody td  { padding: 0.6rem 0.6rem; }

    /* Masquer colonnes secondaires des tableaux sur très petit écran */
    .hide-mobile { display: none; }

    /* Boutons plus grands pour le touch */
    .btn { min-height: 40px; }
    .btn-sm { min-height: 32px; font-size: 0.75rem; }

    /* Page header flex sur mobile */
    .page-header-flex {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }

    /* Login box plein écran sur mobile */
    .login-box { padding: 2rem 1.25rem; margin: 1rem; max-width: 100%; }

    /* Thème grid en 2 colonnes */
    #themeGrid { justify-content: center; }
    .theme-card { width: 110px; }
}

@media (max-width: 400px) {
    .kpi-grid { grid-template-columns: 1fr; }
    .kpi-value { font-size: 1.5rem; }
}

/* ─── Additions v2 (multi-utilisateurs) ─────────────────── */
.stat-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.65rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.875rem;
}
.stat-row:last-child { border-bottom: none; }
.stat-label { color: var(--text-muted); }
.stat-value { font-family: var(--font-mono); color: var(--accent); font-weight: 700; }

/* ─── Safe area iOS / mobile ─────────────────────────────── */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .sidebar-footer { padding-bottom: calc(1rem + env(safe-area-inset-bottom)); }
    .topbar { padding-top: env(safe-area-inset-top); }
}

/* ─── Amélioration inputs sur mobile (évite le zoom iOS) ─── */
@media (max-width: 768px) {
    input, select, textarea {
        font-size: 16px; /* iOS ne zoom pas si >= 16px */
    }
    /* Page header avec bouton en colonne sur mobile */
    .page-header[style*="display:flex"] {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }
    /* Formulaire valorisation inline plus compact */
    .table-wrap form { flex-wrap: wrap; }
    .table-wrap input[type="number"] { width: 90px !important; }
}

/* ─── Scrollbar ──────────────────────────────────────────── */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--scroll-bg); }
::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }

/* ─── En-têtes de tableau triables ──────────────────────── */
.th-sortable {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    color: inherit;
    text-decoration: none;
    cursor: pointer;
    transition: color 0.15s;
}
.th-sortable:hover { color: var(--accent); }
