* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text);
    font-size: var(--fs-base);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 var(--sp-3); font-weight: 600; }
h1 { font-size: var(--fs-lg); }
h2 { font-size: var(--fs-md); }
.icon { width: 20px; height: 20px; flex: none; }
.muted { color: var(--text-muted); font-size: var(--fs-sm); }

/* ---------- App shell ---------- */
.shell { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    flex: none;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    transition: width 0.15s ease;
}
.shell.sidebar-collapsed .sidebar { width: var(--sidebar-w-collapsed); }
.shell.sidebar-collapsed .brand-name,
.shell.sidebar-collapsed .nav-link span,
.shell.sidebar-collapsed .soon-badge,
.shell.sidebar-collapsed .nav-group-label { display: none; }
.shell.sidebar-collapsed .sidebar-collapse-btn .icon { transform: rotate(180deg); }

.sidebar-top {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-3);
    min-height: var(--topbar-h);
}
.sidebar-brand { display: flex; align-items: center; gap: var(--sp-2); color: var(--sidebar-text); text-decoration: none; flex: 1; min-width: 0; }
.brand-mark {
    width: 34px; height: 34px; border-radius: var(--radius-sm);
    background: var(--accent);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; flex: none; overflow: hidden;
}
.brand-mark img { width: 100%; height: 100%; object-fit: cover; }
.brand-name { font-weight: 600; font-size: var(--fs-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-collapse-btn { color: var(--sidebar-text-muted); flex: none; }
.sidebar-collapse-btn:hover { color: var(--sidebar-text); }

.sidebar-nav { flex: 1; padding: 0 var(--sp-2); display: flex; flex-direction: column; gap: var(--sp-4); }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-group-label { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: 0.04em; color: var(--sidebar-text-muted); padding: var(--sp-2) var(--sp-2) 4px; }

.nav-link {
    display: flex; align-items: center; gap: var(--sp-2);
    padding: 10px var(--sp-2);
    border-radius: var(--radius-sm);
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: var(--fs-sm);
    white-space: nowrap;
    cursor: pointer;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
}
.nav-link .icon { color: var(--sidebar-text-muted); }
.nav-link:hover { background: var(--sidebar-bg-hover); }
.nav-link.active { background: var(--sidebar-active-bg); color: var(--sidebar-active-text); }
.nav-link.active .icon { color: var(--sidebar-active-text); }
.nav-link-soon { color: var(--sidebar-text-muted); cursor: default; }
.nav-link-soon .icon { color: var(--sidebar-text-muted); opacity: 0.7; }
.nav-link-soon:hover { background: none; }
.soon-badge {
    margin-left: auto; font-size: 0.68rem; padding: 2px 7px; border-radius: var(--radius-pill);
    background: rgba(255,255,255,0.08); color: var(--sidebar-text-muted); flex: none;
}

.sidebar-bottom { padding: var(--sp-2); border-top: 1px solid rgba(255,255,255,0.08); display: flex; flex-direction: column; gap: 2px; }
.theme-toggle .theme-icon-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .theme-icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .theme-icon-moon { display: block; }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .theme-toggle .theme-icon-sun { display: none; }
    :root:not([data-theme="light"]) .theme-toggle .theme-icon-moon { display: block; }
}

.mobile-nav-backdrop { position: fixed; inset: 0; background: var(--overlay); z-index: 40; }

/* ---------- Main column / topbar ---------- */
.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
    height: var(--topbar-h);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: 0 var(--sp-4);
    position: sticky;
    top: 0;
    z-index: 20;
}
.mobile-nav-btn { display: none; }
.topbar-heading { flex: 1; min-width: 0; }
.breadcrumb { font-size: var(--fs-sm); color: var(--text-muted); display: flex; gap: var(--sp-1); align-items: center; }
.breadcrumb a { color: var(--text-muted); text-decoration: none; }
.breadcrumb a:hover { color: var(--accent); }
.breadcrumb-sep { color: var(--text-faint); }
.breadcrumb-current { color: var(--text); font-weight: 600; }

.topbar-actions { display: flex; align-items: center; gap: var(--sp-2); flex: none; }
.search-box {
    display: flex; align-items: center; gap: var(--sp-1);
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 6px 10px; color: var(--text-faint); background: var(--bg);
}
.search-box .icon { width: 16px; height: 16px; }
.search-box input { border: none; background: none; outline: none; font-size: var(--fs-sm); color: var(--text); width: 160px; }
.search-box input::placeholder { color: var(--text-faint); }
.search-box input:disabled { cursor: not-allowed; }

.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; border-radius: var(--radius-sm);
    border: 1px solid transparent; background: none; color: var(--text-muted); cursor: pointer;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }
.icon-btn:disabled { cursor: not-allowed; opacity: 0.55; }

.user-menu { position: relative; }
.user-menu-trigger {
    display: flex; align-items: center; gap: var(--sp-2);
    border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius-md);
    padding: 6px 10px; cursor: pointer; color: var(--text);
}
.user-menu-trigger:hover { border-color: var(--border-strong); }
.avatar {
    width: 30px; height: 30px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-strong);
    display: flex; align-items: center; justify-content: center; font-size: 0.8rem; font-weight: 700; flex: none;
}
.user-meta { display: flex; flex-direction: column; text-align: left; line-height: 1.2; }
.user-name { font-size: var(--fs-sm); font-weight: 600; }
.user-role { font-size: var(--fs-xs); color: var(--text-muted); }
.user-menu-trigger > .icon { width: 16px; height: 16px; color: var(--text-faint); }
.user-menu-dropdown {
    position: absolute; right: 0; top: calc(100% + 6px); min-width: 190px;
    background: var(--surface-raised); border: 1px solid var(--border); border-radius: var(--radius-md);
    box-shadow: var(--shadow-md); padding: var(--sp-1); z-index: 30;
}
.user-menu-dropdown a { display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2); border-radius: var(--radius-sm); text-decoration: none; color: var(--text); font-size: var(--fs-sm); }
.user-menu-dropdown a:hover { background: var(--bg); }
.user-menu-dropdown .icon { width: 17px; height: 17px; color: var(--text-muted); }

.content { flex: 1; padding: var(--sp-4); max-width: 1200px; width: 100%; margin: 0 auto; }
.footer { text-align: center; color: var(--text-faint); font-size: var(--fs-xs); padding: var(--sp-4); }

/* ---------- Auth (login) shell ---------- */
.auth-shell {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: var(--bg); position: relative; padding: var(--sp-4);
}
.theme-toggle-auth { position: absolute; top: var(--sp-4); right: var(--sp-4); border: 1px solid var(--border); border-radius: var(--radius-sm); width: 38px; height: 38px; display: flex; align-items: center; justify-content: center; background: var(--surface); color: var(--text-muted); cursor: pointer; }
.login-wrap { max-width: 400px; width: 100%; }
.login-brand { display: flex; flex-direction: column; align-items: center; gap: var(--sp-2); margin-bottom: var(--sp-4); text-align: center; }
.login-brand .brand-mark { width: 52px; height: 52px; font-size: 1.3rem; border-radius: var(--radius-md); }
.login-wrap .card { text-align: left; }
.login-help { margin-top: var(--sp-3); }

/* ---------- Cards / surfaces ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--sp-4);
    margin-bottom: var(--sp-4);
    box-shadow: var(--shadow-sm);
}
.card h2 { margin-bottom: var(--sp-3); }
.card > .btn:last-child { margin-bottom: 0; }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: var(--sp-3); }
.kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md); padding: var(--sp-4); box-shadow: var(--shadow-sm); }
.kpi-card .kpi-value { font-size: var(--fs-xl); font-weight: 700; margin-bottom: 2px; }
.kpi-card .kpi-label { color: var(--text-muted); font-size: var(--fs-sm); }
@media (max-width: 860px) { .kpi-grid { grid-template-columns: 1fr; } }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 calc(var(--sp-4) * -1); padding: 0 var(--sp-4); }
.card .table-wrap { margin: 0 calc(var(--sp-4) * -1) calc(var(--sp-4) * -1); padding: 0 var(--sp-4) var(--sp-4); }
table { width: 100%; border-collapse: collapse; min-width: 560px; }
th, td { text-align: left; padding: 10px var(--sp-2); border-bottom: 1px solid var(--border); font-size: var(--fs-sm); vertical-align: middle; }
th { color: var(--text-muted); font-weight: 600; font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: 0.03em; }
tbody tr:hover { background: var(--bg); }
td .table-actions { display: flex; gap: var(--sp-2); flex-wrap: wrap; }

/* ---------- Empty state ---------- */
.empty-state { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--sp-2); padding: var(--sp-5) var(--sp-3); color: var(--text-muted); }
.empty-state .icon { width: 40px; height: 40px; color: var(--text-faint); }
.empty-state p { margin: 0; font-size: var(--fs-sm); }

/* ---------- Forms ---------- */
form.stacked label { display: block; margin: var(--sp-3) 0 6px; font-weight: 600; font-size: var(--fs-sm); }
input[type=text], input[type=password], input[type=email], input[type=tel],
input[type=number], input[type=date], input[type=file], select, textarea {
    width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    font-size: var(--fs-base); background: var(--surface); color: var(--text); font-family: var(--font-sans);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
textarea { resize: vertical; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--sp-3); }
@media (max-width: 640px) { .grid-2 { grid-template-columns: 1fr; } }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; gap: 6px;
    background: var(--accent); color: var(--text-on-accent);
    border: 1px solid transparent;
    padding: 10px 16px; border-radius: var(--radius-sm);
    cursor: pointer; font-size: var(--fs-sm); font-weight: 600;
    text-decoration: none; margin-top: var(--sp-3);
    font-family: var(--font-sans);
}
.btn .icon { width: 16px; height: 16px; }
.btn:hover { background: var(--accent-strong); }
.btn:disabled { cursor: not-allowed; opacity: 0.55; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-ghost { background: none; color: var(--text-muted); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-danger { background: var(--status-red-fg); }
.btn-danger:hover { filter: brightness(0.92); }
.btn-sm { padding: 6px 12px; font-size: var(--fs-xs); margin-top: 0; }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: var(--radius-pill); font-size: var(--fs-xs); font-weight: 600; white-space: nowrap; }
.badge-ouverte, .badge-partiel, .badge-en_attente { background: var(--status-orange-bg); color: var(--status-orange-fg); }
.badge-en_cours, .badge-credit { background: var(--status-blue-bg); color: var(--status-blue-fg); }
.badge-resolue, .badge-paye, .badge-confirme { background: var(--status-green-bg); color: var(--status-green-fg); }
.badge-fermee { background: var(--status-gray-bg); color: var(--status-gray-fg); }
.badge-impaye, .badge-rejete { background: var(--status-red-bg); color: var(--status-red-fg); }

/* ---------- Flash / alerts ---------- */
.flash { display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2) var(--sp-3); border-radius: var(--radius-sm); margin-bottom: var(--sp-3); font-size: var(--fs-sm); }
.flash .icon { width: 18px; height: 18px; flex: none; }
.flash-success { background: var(--status-green-bg); color: var(--status-green-fg); }
.flash-error { background: var(--status-red-bg); color: var(--status-red-fg); }

/* ---------- Modal ---------- */
.confirm-modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.confirm-modal[hidden] { display: none; }
.confirm-modal-backdrop { position: absolute; inset: 0; background: var(--overlay); }
.confirm-modal-box {
    position: relative; background: var(--surface-raised); border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg); padding: var(--sp-4); max-width: 380px; width: calc(100% - var(--sp-4) * 2);
}
.confirm-modal-message { margin: 0 0 var(--sp-4); font-size: var(--fs-base); }
.confirm-modal-actions { display: flex; justify-content: flex-end; gap: var(--sp-2); }
.confirm-modal-actions .btn { margin-top: 0; }

/* ---------- Thread (réclamations) ---------- */
.thread { display: flex; flex-direction: column; gap: var(--sp-3); margin: var(--sp-3) 0; }
.msg { padding: 10px var(--sp-3); border-radius: var(--radius-md); background: var(--bg); max-width: 80%; }
.msg-self { background: var(--accent-soft); align-self: flex-end; }
.msg-meta { font-size: var(--fs-xs); color: var(--text-muted); margin-bottom: 2px; }

/* ---------- Small utilities (avoid inline styles) ---------- */
.filter-row { display: flex; gap: var(--sp-3); align-items: flex-end; flex-wrap: wrap; }
.filter-row .btn { margin-top: 0; }
.w-xs { width: 6.5rem; }
.w-sm { width: 7.5rem; }
.w-md { width: 9rem; }
.pass-code { font-size: var(--fs-md); background: var(--surface); padding: 2px 8px; border-radius: var(--radius-sm); border: 1px solid var(--border); }

/* ---------- Responsive: tablet / mobile ---------- */
@media (max-width: 900px) {
    .sidebar { position: fixed; left: 0; top: 0; z-index: 45; transform: translateX(-100%); transition: transform 0.2s ease; }
    .shell.sidebar-open-mobile .sidebar { transform: translateX(0); width: var(--sidebar-w); }
    .shell.sidebar-collapsed .sidebar { width: var(--sidebar-w); }
    .shell.sidebar-collapsed .brand-name, .shell.sidebar-collapsed .nav-link span,
    .shell.sidebar-collapsed .soon-badge, .shell.sidebar-collapsed .nav-group-label { display: initial; }
    .sidebar-collapse-btn { display: none; }
    .mobile-nav-btn { display: inline-flex; }
    .mobile-nav-backdrop[hidden] { display: none; }
    .search-box input { width: 110px; }
    .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 640px) {
    .content { padding: var(--sp-3); }
    .topbar { padding: 0 var(--sp-3); gap: var(--sp-2); }
    .search-box { display: none; }
    .user-meta { display: none; }
    .kpi-grid { grid-template-columns: 1fr; }
    .content h1 { font-size: var(--fs-md); }
}
