/* =====================================================================
   Finance Planner - Feuille de style
   Thème clair / sombre via variables CSS (attribut data-theme sur <html>)
   ===================================================================== */

:root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e5e7eb;
    --text: #0f172a;
    --muted: #64748b;
    --primary: #4f46e5;
    --primary-soft: #eef2ff;
    --success: #059669;
    --success-soft: #ecfdf5;
    --warning: #d97706;
    --warning-soft: #fffbeb;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --info: #0284c7;
    --info-soft: #f0f9ff;
    --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 4px 16px rgba(15, 23, 42, .06);
    --radius: 14px;
    --sidebar-w: 248px;
    color-scheme: light;
}

[data-theme="dark"] {
    --bg: #0b1020;
    --surface: #131a2e;
    --surface-2: #1a2238;
    --border: #26304a;
    --text: #e5e9f5;
    --muted: #8b95b0;
    --primary: #818cf8;
    --primary-soft: #1e2347;
    --success: #34d399;
    --success-soft: #0f2a24;
    --warning: #fbbf24;
    --warning-soft: #2d2412;
    --danger: #f87171;
    --danger-soft: #2f1517;
    --info: #38bdf8;
    --info-soft: #0f2333;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.25; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
small, .muted { color: var(--muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.text-right { text-align: right; }
.nowrap { white-space: nowrap; }
.mt { margin-top: 1rem; }
.mb { margin-bottom: 1rem; }

/* ---------------------------------------------------------------------
   Mise en page : barre latérale + contenu
   --------------------------------------------------------------------- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    transition: transform .2s ease;
    overflow-y: auto;
}

.brand {
    display: flex; align-items: center; gap: .5rem;
    font-weight: 700; font-size: 1.1rem;
    padding: 1.2rem 1.25rem;
    color: var(--text);
}
.brand:hover { text-decoration: none; }

.sidebar nav { display: flex; flex-direction: column; padding: 0 .75rem; gap: 2px; flex: 1; }
.sidebar nav a {
    display: flex; align-items: center; gap: .65rem;
    padding: .6rem .75rem;
    border-radius: 10px;
    color: var(--text);
    font-weight: 500;
}
.sidebar nav a:hover { background: var(--surface-2); text-decoration: none; }
.sidebar nav a.active { background: var(--primary-soft); color: var(--primary); }
.nav-icon { width: 1.4rem; text-align: center; }

.sidebar-foot { padding: 1rem; border-top: 1px solid var(--border); display: grid; gap: .6rem; }
.user-chip { display: flex; gap: .6rem; align-items: center; min-width: 0; }
.user-chip div { min-width: 0; display: flex; flex-direction: column; }
.user-chip small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar {
    width: 36px; height: 36px; flex: none; border-radius: 50%;
    display: grid; place-items: center;
    background: var(--primary); color: #fff; font-weight: 700;
}

.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; }

.topbar {
    position: sticky; top: 0; z-index: 30;
    display: flex; align-items: center; gap: .75rem;
    padding: .8rem 1.5rem;
    background: color-mix(in srgb, var(--bg) 85%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
}
.page-title { font-size: 1.25rem; flex: 1; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: .5rem; }
.icon-btn.menu-btn { display: none; }

.content { padding: 1.5rem; max-width: 1280px; margin: 0 auto; }

.sidebar-backdrop { display: none; }

/* ---------------------------------------------------------------------
   Composants
   --------------------------------------------------------------------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem;
    min-width: 0;
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem; margin-bottom: 1rem; flex-wrap: wrap; }
.card-head h2 { margin: 0; }

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
.stack { display: grid; gap: 1rem; }

/* Indicateurs clés */
.kpi { display: flex; flex-direction: column; gap: .25rem; }
.kpi .label { color: var(--muted); font-size: .85rem; font-weight: 500; }
.kpi .value { font-size: 1.5rem; font-weight: 700; letter-spacing: -.01em; }
.kpi .sub { font-size: .82rem; color: var(--muted); }
.kpi-icon { font-size: 1.3rem; }

/* Boutons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    padding: .55rem 1rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit; font-weight: 600;
    cursor: pointer;
    text-decoration: none !important;
    transition: filter .15s, background .15s;
}
.btn:hover { filter: brightness(.97); background: var(--surface-2); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger); filter: brightness(1.08); }
.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn-success:hover { background: var(--success); filter: brightness(1.08); }
.btn-ghost { background: transparent; }
.btn-sm { padding: .35rem .7rem; font-size: .85rem; }
.btn-block { width: 100%; }
.btn-link { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; cursor: pointer; }
.btn-link.danger { color: var(--danger); }

.icon-btn {
    position: relative;
    width: 38px; height: 38px;
    display: inline-grid; place-items: center;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer; font-size: 1rem;
    text-decoration: none !important;
}
.badge-dot {
    position: absolute; top: -6px; right: -6px;
    background: var(--danger); color: #fff;
    font-size: .7rem; font-weight: 700;
    min-width: 18px; height: 18px; padding: 0 4px;
    border-radius: 9px; display: grid; place-items: center;
}

.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.inline { display: inline; }

/* Formulaires */
.form-grid { display: grid; gap: 1rem; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: .35rem; min-width: 0; }
.field label { font-weight: 600; font-size: .88rem; }
.field .hint { font-size: .8rem; color: var(--muted); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date],
input[type=search], select, textarea {
    width: 100%;
    padding: .6rem .75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
}
input:focus, select:focus, textarea:focus {
    outline: 2px solid color-mix(in srgb, var(--primary) 35%, transparent);
    border-color: var(--primary);
}
input[type=color] { width: 100%; height: 42px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); padding: 3px; }
textarea { min-height: 80px; resize: vertical; }

/* Sélecteur de type revenu / dépense */
.segmented { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 3px; gap: 3px; flex-wrap: wrap; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label, .segmented a {
    padding: .4rem .9rem; border-radius: 9px; cursor: pointer; font-weight: 600;
    color: var(--muted); text-decoration: none;
}
.segmented input:checked + label, .segmented a.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented input[value=expense]:checked + label { color: var(--danger); }
.segmented input[value=income]:checked + label { color: var(--success); }

/* Sélecteur d'icônes */
.icon-picker { display: flex; flex-wrap: wrap; gap: .3rem; }
.icon-picker input { position: absolute; opacity: 0; pointer-events: none; }
.icon-picker label {
    width: 38px; height: 38px; display: grid; place-items: center;
    border: 1px solid var(--border); border-radius: 10px; cursor: pointer; font-size: 1.15rem;
    background: var(--surface-2);
}
.icon-picker input:checked + label { border-color: var(--primary); background: var(--primary-soft); box-shadow: 0 0 0 2px var(--primary); }

/* Tableaux */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .65rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.table tr:last-child td { border-bottom: 0; }
.table tfoot td { font-weight: 700; border-top: 2px solid var(--border); }
.table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Listes */
.list { display: flex; flex-direction: column; }
.list-item { display: flex; align-items: center; gap: .75rem; padding: .7rem 0; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: 0; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .meta { font-size: .82rem; color: var(--muted); }
.amount { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.amount.income { color: var(--success); }
.amount.expense { color: var(--danger); }

.bubble {
    width: 40px; height: 40px; flex: none;
    border-radius: 12px;
    display: grid; place-items: center;
    font-size: 1.2rem;
    background: var(--surface-2);
}

/* Badges */
.badge { display: inline-block; padding: .15rem .55rem; border-radius: 999px; font-size: .75rem; font-weight: 600; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge-success { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-warning { background: var(--warning-soft); color: var(--warning); border-color: transparent; }
.badge-info { background: var(--info-soft); color: var(--info); border-color: transparent; }

/* Alertes */
.alert { padding: .8rem 1rem; border-radius: 12px; margin-bottom: 1rem; border: 1px solid transparent; font-weight: 500; }
.alert-success { background: var(--success-soft); color: var(--success); }
.alert-danger, .alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-warning { background: var(--warning-soft); color: var(--warning); }
.alert-info { background: var(--info-soft); color: var(--info); }

.tip { display: flex; gap: .7rem; align-items: flex-start; padding: .75rem .9rem; border-radius: 12px; margin-bottom: .6rem; }
.tip:last-child { margin-bottom: 0; }
.tip .tip-icon { font-size: 1.2rem; line-height: 1.3; }
.tip-success { background: var(--success-soft); }
.tip-warning { background: var(--warning-soft); }
.tip-danger { background: var(--danger-soft); }
.tip-info { background: var(--info-soft); }

/* Barres de progression */
.progress { height: 10px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; border-radius: 999px; background: var(--primary); transition: width .4s ease; }
.progress.ok > span { background: var(--success); }
.progress.warning > span { background: var(--warning); }
.progress.danger > span { background: var(--danger); }
.progress-row { display: grid; gap: .35rem; padding: .7rem 0; border-bottom: 1px solid var(--border); }
.progress-row:last-child { border-bottom: 0; }
.progress-row .top { display: flex; justify-content: space-between; gap: .75rem; align-items: baseline; flex-wrap: wrap; }

/* Navigation des périodes */
.period-bar { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: .75rem; margin-bottom: 1rem; }
.period-nav { display: flex; align-items: center; gap: .5rem; }
.period-nav .label { font-weight: 700; font-size: 1.05rem; min-width: 12rem; text-align: center; }

.change { font-size: .8rem; font-weight: 600; }
.change.up { color: var(--danger); }
.change.down { color: var(--success); }
.change.up.good { color: var(--success); }
.change.down.bad { color: var(--danger); }

.chart-box { position: relative; height: 280px; }
.chart-box.sm { height: 220px; }

.color-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: .4rem; vertical-align: middle; }

.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }
.empty .big { font-size: 2.2rem; display: block; margin-bottom: .4rem; }

details.panel { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); margin-bottom: 1rem; }
details.panel > summary { padding: 1rem 1.25rem; cursor: pointer; font-weight: 700; list-style: none; }
details.panel > summary::-webkit-details-marker { display: none; }
details.panel > summary::before { content: "＋ "; color: var(--primary); }
details.panel[open] > summary::before { content: "－ "; }
details.panel > .panel-body { padding: 0 1.25rem 1.25rem; }

.filters { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; }

.pagination { display: flex; gap: .4rem; justify-content: center; margin-top: 1rem; flex-wrap: wrap; }

.item-card { display: flex; flex-direction: column; gap: .75rem; }
.item-card .head { display: flex; gap: .75rem; align-items: center; }
.item-card .head .grow { flex: 1; min-width: 0; }

/* ---------------------------------------------------------------------
   Pages d'authentification
   --------------------------------------------------------------------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; position: relative;
    background: radial-gradient(circle at 20% 10%, var(--primary-soft), transparent 50%), var(--bg); }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border);
    border-radius: 18px; box-shadow: var(--shadow); padding: 2rem; }
.auth-brand { font-size: 1.4rem; font-weight: 800; text-align: center; margin-bottom: 1.25rem; }
.auth-card form { display: grid; gap: 1rem; }
.auth-links { text-align: center; font-size: .9rem; margin-top: 1rem; display: grid; gap: .3rem; }
.theme-float { position: absolute; top: 1rem; right: 1rem; }

/* ---------------------------------------------------------------------
   Responsive
   --------------------------------------------------------------------- */
@media (max-width: 1100px) {
    .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 860px) {
    .sidebar { transform: translateX(-100%); box-shadow: var(--shadow); }
    body.sidebar-open .sidebar { transform: translateX(0); }
    body.sidebar-open .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 35; }
    .main { margin-left: 0; }
    .icon-btn.menu-btn { display: inline-grid; }
    .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
    .span-2 { grid-column: auto; }
    .content { padding: 1rem; }
    .topbar { padding: .7rem 1rem; }
}

@media (max-width: 560px) {
    .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .form-grid { grid-template-columns: minmax(0, 1fr); }
    .kpi .value { font-size: 1.15rem; }
    .hide-sm { display: none; }
    .page-title { font-size: 1.05rem; }
    .period-nav .label { min-width: 0; font-size: .95rem; }
    .table .hide-mobile { display: none; }
    .card { padding: 1rem; }
}

/* ---------------------------------------------------------------------
   Impression des rapports
   --------------------------------------------------------------------- */
@media print {
    :root, [data-theme="dark"] {
        --bg: #fff; --surface: #fff; --surface-2: #f5f5f5; --border: #ddd; --text: #000; --muted: #555;
        --shadow: none; color-scheme: light;
    }
    .sidebar, .topbar, .no-print, .sidebar-backdrop { display: none !important; }
    .main { margin-left: 0; }
    .content { padding: 0; max-width: none; }
    .card { break-inside: avoid; box-shadow: none; }
    .grid-4 { grid-template-columns: repeat(4, 1fr); }
    .grid-2 { grid-template-columns: repeat(2, 1fr); }
    .print-only { display: block !important; }
    a { color: inherit; }
}
.print-only { display: none; }
