/* ==========================================================================
   Datalab — Design System (dark raffiné + thème clair, responsive)
   ========================================================================== */

:root {
  --accent: #ffbb00;
  --accent-2: #ff9500;
  --accent-ink: #1a1500;
  --danger: #ef4444;
  --danger-ink: #fff;
  --success: #22c55e;
  --radius: 14px;
  --radius-sm: 10px;
  --radius-pill: 999px;
  --shadow: 0 10px 30px -12px rgba(0, 0, 0, .5);
  --shadow-sm: 0 2px 8px -2px rgba(0, 0, 0, .35);
  --fs: 14px;
  --ff: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Helvetica, Arial, sans-serif;
  --sp: 16px;
  --tr: .18s ease;
}

/* --- Thème sombre (défaut) --- */
:root, :root[data-theme="dark"] {
  --bg: #0d0f14;
  --bg-grad: radial-gradient(1200px 600px at 80% -10%, #1a1f2b 0%, transparent 60%), radial-gradient(900px 500px at -10% 10%, #17130a 0%, transparent 55%);
  --surface: #151922;
  --surface-2: #1b2130;
  --surface-3: #222a3a;
  --border: #262d3b;
  --border-strong: #333d51;
  --text: #e8edf4;
  --muted: #93a0b4;
  --input-bg: #0e1219;
  --ring: rgba(255, 187, 0, .35);
  color-scheme: dark;
}

/* --- Thème clair --- */
:root[data-theme="light"] {
  --bg: #f4f6fb;
  --bg-grad: radial-gradient(1000px 500px at 85% -10%, #e7edfb 0%, transparent 60%), radial-gradient(800px 500px at -5% 5%, #fff4dd 0%, transparent 55%);
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --surface-3: #eef2f8;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #172033;
  --muted: #64748b;
  --input-bg: #ffffff;
  --ring: rgba(255, 149, 0, .3);
  --shadow: 0 10px 30px -14px rgba(30, 41, 59, .25);
  --shadow-sm: 0 1px 4px rgba(30, 41, 59, .1);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--ff); font-size: var(--fs); line-height: 1.45;
  background: var(--bg); background-image: var(--bg-grad); background-attachment: fixed;
  color: var(--text); -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.05rem; }
h4 { font-size: .82rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.muted { color: var(--muted); }
.grow { flex: 1 1 auto; min-width: 0; }
.nowrap { white-space: nowrap; }
.ic { width: 1.15em; height: 1.15em; flex: 0 0 auto; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- Boutons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 16px; border-radius: var(--radius-sm); border: 1px solid transparent;
  background: var(--surface-3); color: var(--text); font: inherit; font-weight: 600;
  cursor: pointer; transition: var(--tr); white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: .55; cursor: not-allowed; transform: none; }
.btn .ic { width: 1.05em; height: 1.05em; }
.btn.primary { background: linear-gradient(180deg, var(--accent), var(--accent-2)); color: var(--accent-ink); box-shadow: 0 6px 16px -8px var(--accent); }
.btn.danger { background: var(--danger); color: var(--danger-ink); }
.btn.ghost { background: transparent; border-color: var(--border-strong); color: var(--text); }
.btn.ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn.block { width: 100%; }
.btn.sm { padding: 7px 12px; font-size: .85rem; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: transparent; color: var(--muted);
  cursor: pointer; transition: var(--tr);
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.ghost { border-color: transparent; }
.icon-btn.danger:hover { color: var(--danger); background: rgba(239, 68, 68, .12); }

.chip-btn {
  padding: 7px 12px; border-radius: var(--radius-pill); border: 1px solid var(--border-strong);
  background: transparent; color: var(--muted); font: inherit; font-size: .82rem; cursor: pointer; transition: var(--tr);
}
.chip-btn:hover { color: var(--text); border-color: var(--accent); }
.chip-btn.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }

/* ---------- Champs ---------- */
.input, input:not([type=checkbox]):not([type=color]):not([type=radio]), select, textarea {
  width: 100%; padding: 10px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--input-bg); color: var(--text);
  font: inherit; transition: var(--tr); outline: none;
}
.input.sm, select.sm { padding: 7px 10px; font-size: .85rem; width: auto; }
.input:focus, input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring); }
.input.num { text-align: right; font-variant-numeric: tabular-nums; }
input[type=color].color { width: 42px; height: 40px; padding: 3px; cursor: pointer; flex: 0 0 auto; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: .82rem; color: var(--muted); font-weight: 500; }
.field.grow { flex: 1; }
label.inline { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: .85rem; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; color: var(--text); }
.check.sm { font-size: .82rem; color: var(--muted); }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.input-affix { position: relative; display: flex; }
.input-affix .reveal { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 34px; height: 34px; }

/* ---------- Cartes ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow-sm);
  margin-bottom: 18px;
}
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.card-head h3 { margin-right: auto; }
.page-head { margin: 4px 0 18px; }
.empty { padding: 32px; text-align: center; color: var(--muted); }
.pill {
  display: inline-flex; align-items: center; padding: 4px 12px; border-radius: var(--radius-pill);
  font-weight: 600; font-size: .78rem; border: 1px solid; white-space: nowrap;
}

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 20px; position: relative; }
.login-card { width: 100%; max-width: 400px; padding: 32px; margin: 0; box-shadow: var(--shadow); }
.login-form { display: flex; flex-direction: column; gap: 16px; }
.login-form h1 { font-size: 1.6rem; margin-top: 8px; }
.login-form .field span { font-weight: 600; color: var(--text); }
.brand-block { display: inline-flex; align-items: center; gap: 10px; }
.logo-dot { width: 14px; height: 14px; border-radius: 5px; background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: 0 0 14px var(--accent); flex: 0 0 auto; }
.brand-name { font-weight: 700; letter-spacing: -.02em; }
.form-error { background: rgba(239, 68, 68, .12); border: 1px solid rgba(239, 68, 68, .35); color: #fca5a5; padding: 10px 12px; border-radius: var(--radius-sm); font-size: .85rem; }
.theme-fab { position: absolute; top: 18px; right: 18px; width: 42px; height: 42px; }

/* ---------- Shell ---------- */
.shell { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px;
  padding: 12px 20px; border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
}
.nav-desktop { display: flex; gap: 6px; margin: 0 auto 0 12px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.nav-btn {
  display: inline-flex; align-items: center; gap: 8px; padding: 9px 14px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: transparent; color: var(--muted); font: inherit; font-weight: 550; cursor: pointer; transition: var(--tr);
}
.nav-btn:hover { color: var(--text); background: var(--surface-2); }
.nav-btn.active { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.user-chip { display: flex; align-items: center; gap: 10px; padding: 4px 8px; }
.user-meta { display: flex; flex-direction: column; line-height: 1.15; }
.user-meta strong { font-size: .85rem; }
.user-meta .muted { font-size: .72rem; }
.avatar { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; font-weight: 700; color: #fff; flex: 0 0 auto; text-shadow: 0 1px 2px rgba(0,0,0,.4); }
.avatar.sm { width: 30px; height: 30px; border-radius: 8px; font-size: .8rem; }
.main { width: 100%; max-width: 1200px; margin: 0 auto; padding: 24px 20px 40px; flex: 1; }
.nav-mobile { display: none; }

/* ---------- Saisie ---------- */
.entry-form .entry-top { margin-bottom: 18px; }
.metric-groups { display: flex; flex-direction: column; gap: 12px; }
.metric-group { display: flex; align-items: center; gap: 16px; }
.metric-group h4 { flex: 0 0 116px; margin: 0; }
.metric-grid { display: flex; flex-wrap: nowrap; gap: 8px; overflow-x: auto; flex: 1; padding-bottom: 4px; }
.metric-field { display: flex; flex-direction: column; gap: 4px; flex: 0 0 auto; width: 78px; }
.metric-field.money { width: 116px; }
.metric-field .input.num { width: 100%; padding: 8px; }
.metric-label { font-size: .72rem; color: var(--muted); font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metric-label em { color: var(--accent); font-style: normal; }
.form-actions { display: flex; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.hist-filters { display: flex; gap: 8px; }
.day-sep { margin: 18px 0 10px; position: sticky; }
.day-sep:first-child { margin-top: 4px; }
.entry-row { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 6px; transition: var(--tr); overflow-x: auto; }
.entry-row:hover { border-color: var(--border-strong); }
.entry-row > * { flex: 0 0 auto; }
.entry-row .entry-user { font-size: .78rem; white-space: nowrap; }
.entry-row .entry-metrics { display: flex; gap: 6px; margin-left: auto; }
.entry-row .entry-actions { display: flex; gap: 2px; padding-left: 8px; }
.entry-row .entry-actions .icon-btn { width: 30px; height: 30px; }
.metric-chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 9px; border-radius: 8px; background: var(--surface-3); font-size: .76rem; }
.metric-chip .mc-label { color: var(--muted); font-weight: 500; }
.metric-chip .mc-val { font-weight: 700; font-variant-numeric: tabular-nums; }
.metric-chip.hot { background: color-mix(in srgb, var(--accent) 16%, var(--surface-3)); }
.metric-chip.hot .mc-val { color: var(--accent); }
.skeleton-list { height: 120px; border-radius: var(--radius-sm); background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: sk 1.3s infinite; }
@keyframes sk { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ---------- Stats ---------- */
.stats-controls { display: flex; flex-direction: column; gap: 14px; }
.ctrl-row { display: flex; gap: 20px; flex-wrap: wrap; align-items: center; }
.ctrl-group { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.ctrl-lbl { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 3px; gap: 2px; }
.seg-btn { padding: 6px 14px; border-radius: 7px; border: none; background: transparent; color: var(--muted); font: inherit; font-size: .85rem; font-weight: 550; cursor: pointer; transition: var(--tr); }
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: var(--surface); color: var(--accent); box-shadow: var(--shadow-sm); }
.sites-toggle { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.site-toggle { display: inline-flex; align-items: center; gap: 8px; padding: 7px 13px; border-radius: var(--radius-pill); border: 1px solid var(--border); background: var(--surface); color: var(--muted); font: inherit; font-size: .82rem; cursor: pointer; transition: var(--tr); opacity: .5; }
.site-toggle .dot { width: 10px; height: 10px; border-radius: 50%; }
.site-toggle.on { opacity: 1; color: var(--text); border-color: var(--border-strong); }
.kpi-row { display: flex; flex-wrap: wrap; gap: 14px; }
.kpi { display: flex; flex-direction: column; gap: 2px; padding: 10px 16px; border-radius: var(--radius-sm); background: var(--surface-2); min-width: 120px; }
.kpi.big { background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 18%, var(--surface-2)), var(--surface-2)); }
.kpi-lbl { font-size: .72rem; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.kpi-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.kpi-val { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.chart-host { position: relative; width: 100%; }
.chart-svg { display: block; overflow: visible; }
.chart-svg .grid { stroke: var(--border); stroke-width: 1; }
.chart-svg .axis-line { stroke: var(--border-strong); stroke-width: 1; }
.chart-svg text.axis { fill: var(--muted); font-size: 11px; }
.chart-svg text.axis.y { text-anchor: end; }
.chart-svg text.axis.x { text-anchor: middle; }
.chart-svg .hover-line { stroke: var(--border-strong); stroke-dasharray: 4 4; }
.chart-tip { position: absolute; pointer-events: none; background: var(--surface-3); border: 1px solid var(--border-strong); border-radius: 10px; padding: 8px 10px; font-size: .78rem; box-shadow: var(--shadow); min-width: 150px; z-index: 5; }
.chart-tip .tt-date { font-weight: 700; margin-bottom: 5px; }
.chart-tip .tt-row { display: flex; align-items: center; gap: 6px; }
.chart-tip .tt-row b { margin-left: auto; font-variant-numeric: tabular-nums; }
.chart-tip .tt-dot { width: 8px; height: 8px; border-radius: 50%; }

/* ---------- Tables ---------- */
.table-scroll { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.data-table th, .data-table td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table th { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.data-table td.num, .data-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table .strong { font-weight: 700; }
.data-table tfoot td { border-top: 2px solid var(--border-strong); border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface-2); }

/* ---------- Admin ---------- */
.admin-tabs { margin-bottom: 18px; }
.add-row .inline-form { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.add-row .inline-form .input:not(.color) { flex: 1; min-width: 160px; }
.admin-grid { display: flex; flex-direction: column; gap: 8px; }
.admin-row { display: flex; align-items: center; gap: 14px; padding: 10px 14px; border-radius: var(--radius-sm); background: var(--surface-2); border: 1px solid var(--border); transition: var(--tr); }
.admin-row:hover { border-color: var(--border-strong); }
.admin-row.off { opacity: .55; }
.site-row .sr-name { flex: 1; min-width: 120px; }
.site-row .sr-counts { font-size: .78rem; }
.site-row .sr-actions { display: flex; gap: 6px; margin-left: auto; }
.user-row .ur-id { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.user-row .ur-id strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-row .ur-id .muted { font-size: .78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-row .count { font-size: .76rem; }
.user-row .ur-actions { display: flex; gap: 4px; }
.role-badge { padding: 3px 10px; border-radius: var(--radius-pill); font-size: .72rem; font-weight: 600; }
.role-badge.admin { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }
.role-badge.operator { background: var(--surface-3); color: var(--muted); }
.audit-tag { padding: 3px 9px; border-radius: 7px; font-size: .74rem; font-weight: 600; background: var(--surface-3); color: var(--muted); }
.audit-tag.login_ok, .audit-tag.entry_add, .audit-tag.site_create, .audit-tag.user_create { background: rgba(34,197,94,.15); color: #4ade80; }
.audit-tag.login_fail, .audit-tag.login_locked, .audit-tag.entry_delete, .audit-tag.site_delete, .audit-tag.user_delete { background: rgba(239,68,68,.15); color: #f87171; }
.pwd-box { display: flex; align-items: center; gap: 12px; background: var(--surface-2); border: 1px dashed var(--border-strong); border-radius: var(--radius-sm); padding: 14px; margin-top: 12px; }
.pwd-box code { font-size: 1.2rem; font-weight: 700; letter-spacing: .05em; color: var(--accent); flex: 1; }
.form-grid { display: flex; flex-direction: column; gap: 14px; min-width: min(400px, 80vw); }

/* Switch */
.switch { position: relative; display: inline-flex; width: 40px; height: 22px; flex: 0 0 auto; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch .slider { position: absolute; inset: 0; background: var(--surface-3); border: 1px solid var(--border-strong); border-radius: 999px; transition: var(--tr); }
.switch .slider::before { content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background: var(--muted); border-radius: 50%; transition: var(--tr); }
.switch input:checked + .slider { background: var(--accent); border-color: transparent; }
.switch input:checked + .slider::before { transform: translateX(18px); background: var(--accent-ink); }

/* ---------- Toasts ---------- */
.toast-stack { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 100; }
.toast { padding: 12px 16px; border-radius: var(--radius-sm); background: var(--surface-3); border: 1px solid var(--border-strong); color: var(--text); box-shadow: var(--shadow); font-size: .85rem; max-width: 340px; opacity: 0; transform: translateY(12px); transition: var(--tr); }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
.toast.info { border-left: 3px solid var(--accent); }

/* ---------- Modal ---------- */
.modal-overlay { position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; padding: 20px; background: rgba(4, 6, 12, .55); backdrop-filter: blur(3px); opacity: 0; transition: var(--tr); }
.modal-overlay.show { opacity: 1; }
.modal { width: 100%; max-width: 480px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); transform: translateY(12px) scale(.98); transition: var(--tr); max-height: 90vh; overflow: auto; }
.modal-overlay.show .modal { transform: none; }
.modal-head { display: flex; align-items: center; gap: 12px; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin-right: auto; }
.modal-body { padding: 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--border); }

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 860px) {
  .ctrl-row { gap: 12px; }
  .user-row .count { display: none; }
}
@media (max-width: 720px) {
  h2 { font-size: 1.2rem; }
  .topbar { padding: 10px 14px; }
  .nav-desktop { display: none; }
  .user-meta { display: none; }
  .main { padding: 18px 14px 92px; }
  .card { padding: 16px; }
  .nav-mobile {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
    background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px);
    border-top: 1px solid var(--border); padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    justify-content: space-around;
  }
  .nav-mobile .nav-btn { flex-direction: column; gap: 3px; font-size: .68rem; padding: 6px 10px; flex: 1; }
  .nav-mobile .nav-btn .ic { width: 1.35em; height: 1.35em; }
  .metric-group { flex-direction: column; align-items: stretch; gap: 6px; }
  .metric-group h4 { flex: none; }
  .kpi-val { font-size: 1.1rem; }
  .admin-row { flex-wrap: wrap; }
  .user-row .ur-actions, .site-row .sr-actions { margin-left: auto; }
  .site-row .sr-counts { width: 100%; order: 5; }
  .toast-stack { left: 14px; right: 14px; bottom: 84px; }
  .toast { max-width: none; }
}
@media (max-width: 420px) {
  .ctrl-group { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
