/* =============================================================================
   SP Appli — Design system principal
   Thème clair / sombre via variables CSS + attribut [data-theme] sur <html>
============================================================================= */

:root {
  --sp-blue-dark: #0b1220;
  --sp-blue: #1250e0;
  --sp-blue-light: #2fb6ff;
  --sp-purple: #7b3ff2;
  --sp-gradient: linear-gradient(135deg, var(--sp-blue) 0%, var(--sp-blue-light) 55%, var(--sp-purple) 100%);

  --bg: #f4f6fb;
  --bg-elevated: #ffffff;
  --bg-sidebar: #0b1220;
  --bg-hover: #eef1f8;
  --border: #e2e6f0;
  --text: #10162b;
  --text-muted: #6b7280;
  --text-inverse: #f5f7fc;

  --success: #17a673;
  --warning: #e6a20b;
  --danger: #e34848;
  --info: #2fb6ff;

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(16, 22, 43, 0.06);
  --shadow: 0 4px 16px rgba(16, 22, 43, 0.08);
  --shadow-lg: 0 12px 32px rgba(16, 22, 43, 0.16);

  --sidebar-w: 264px;
  --topbar-h: 64px;
  --transition: 160ms ease;
}

[data-theme="dark"] {
  --bg: #0d1220;
  --bg-elevated: #141b2d;
  --bg-sidebar: #090d17;
  --bg-hover: #1b2438;
  --border: #232c42;
  --text: #eef1fb;
  --text-muted: #9aa4bf;
  --text-inverse: #0d1220;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background var(--transition), color var(--transition);
}
img { max-width: 100%; }
a { color: var(--sp-blue); text-decoration: none; }
h1, h2, h3, h4 { margin: 0 0 8px; }
p { margin: 0 0 8px; }

.hidden { display: none !important; }
.text-muted { color: var(--text-muted); }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.text-warning { color: var(--warning); }

/* ---------- Layout applicatif ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-sidebar);
  color: var(--text-inverse);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; bottom: 0; left: 0;
  z-index: 40;
  transition: transform var(--transition);
}
.sidebar-logo { display: flex; align-items: center; gap: 10px; padding: 18px 20px; border-bottom: 1px solid rgba(255,255,255,.08); }
.sidebar-logo img { width: 36px; height: 36px; border-radius: 8px; }
.sidebar-logo span { font-weight: 700; font-size: 15px; letter-spacing: .3px; }
.sidebar-nav { flex: 1; overflow-y: auto; padding: 10px 10px 20px; }
.sidebar-section-label { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: #8896c2; padding: 14px 12px 6px; }
.sidebar-link {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; margin: 2px 0; border-radius: var(--radius-sm);
  color: #c7d0ea; font-size: 14px; cursor: pointer; user-select: none;
  transition: background var(--transition), color var(--transition);
}
.sidebar-link .ic { width: 18px; text-align: center; }
.sidebar-link:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar-link.active { background: var(--sp-gradient); color: #fff; font-weight: 600; }
.sidebar-footer { padding: 12px; border-top: 1px solid rgba(255,255,255,.08); font-size: 12px; color: #8896c2; }

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

.topbar {
  height: var(--topbar-h); background: var(--bg-elevated); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 14px; padding: 0 20px; position: sticky; top: 0; z-index: 30;
}
.topbar .menu-toggle { display: none; }
.topbar-search { flex: 1; max-width: 480px; position: relative; }
.topbar-search input {
  width: 100%; padding: 9px 14px 9px 36px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 13px;
}
.topbar-search .ic { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--text-muted); }
.search-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; background: var(--bg-elevated);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg);
  max-height: 420px; overflow-y: auto; z-index: 50;
}
.search-results .sr-group { padding: 8px 12px; font-size: 11px; text-transform: uppercase; color: var(--text-muted); }
.search-results .sr-item { padding: 9px 14px; cursor: pointer; font-size: 13.5px; border-top: 1px solid var(--border); }
.search-results .sr-item:hover { background: var(--bg-hover); }
.search-results .sr-empty { padding: 16px; color: var(--text-muted); font-size: 13px; }

.topbar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.mobile-search-btn { display: none; }
.sidebar-backdrop { display: none; position: fixed; inset: 0; background: rgba(8,12,24,.55); z-index: 39; }
.sidebar-backdrop.visible { display: block; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg-elevated);
  display: flex; align-items: center; justify-content: center; cursor: pointer; position: relative; color: var(--text);
  transition: background var(--transition);
}
.icon-btn:hover { background: var(--bg-hover); }
.badge-count {
  position: absolute; top: -5px; right: -5px; background: var(--danger); color: #fff; font-size: 10px;
  min-width: 16px; height: 16px; border-radius: 999px; display: flex; align-items: center; justify-content: center; padding: 0 3px;
}
.user-chip { display: flex; align-items: center; gap: 8px; padding: 5px 10px 5px 5px; border-radius: 999px; border: 1px solid var(--border); cursor: pointer; background: var(--bg-elevated); }
.user-chip img { width: 28px; height: 28px; border-radius: 50%; }
.user-chip .uc-name { font-size: 13px; font-weight: 600; }
.user-chip .uc-role { font-size: 10.5px; color: var(--text-muted); text-transform: capitalize; }

.content { padding: 22px 26px 60px; max-width: 1440px; width: 100%; margin: 0 auto; }
.page-header { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.page-header h1 { font-size: 21px; }
.page-header .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.breadcrumb { font-size: 12.5px; color: var(--text-muted); margin-bottom: 4px; }
.breadcrumb a { color: var(--text-muted); }

/* ---------- Boutons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 8px; padding: 9px 16px; border-radius: var(--radius-sm);
  font-size: 13.5px; font-weight: 600; cursor: pointer; border: 1px solid transparent; transition: all var(--transition);
  white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--sp-gradient); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-secondary { background: var(--bg-elevated); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-outline { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--bg-hover); color: var(--text); }
.btn-sm { padding: 6px 11px; font-size: 12.5px; }
.btn-icon { padding: 8px; }

/* ---------- Cartes / KPI ---------- */
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 1100px) { .grid-4 { grid-template-columns: repeat(2, 1fr); } .grid-3 { grid-template-columns: repeat(2,1fr);} }
@media (max-width: 640px) { .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; } }

.card {
  background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 18px 20px; box-shadow: var(--shadow-sm);
}
.card h3 { font-size: 14px; color: var(--text-muted); font-weight: 600; }
.kpi-card { display: flex; flex-direction: column; gap: 6px; }
.kpi-card .kpi-value { font-size: 26px; font-weight: 800; }
.kpi-card .kpi-label { font-size: 12.5px; color: var(--text-muted); }
.kpi-card .kpi-trend { font-size: 12px; font-weight: 700; }
.kpi-card .kpi-icon { width: 38px; height: 38px; border-radius: 10px; display: flex; align-items: center; justify-content: center; background: var(--bg-hover); margin-bottom: 4px; }

/* ---------- Tableaux ---------- */
.table-wrap { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; }
table.data-table { width: 100%; min-width: 640px; border-collapse: collapse; font-size: 13.5px; }
table.data-table th {
  text-align: left; padding: 12px 14px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-muted); border-bottom: 1px solid var(--border); background: var(--bg-hover); cursor: pointer; white-space: nowrap;
}
table.data-table td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data-table tbody tr:hover { background: var(--bg-hover); }
table.data-table tbody tr:last-child td { border-bottom: none; }
.table-toolbar { display: flex; align-items: center; gap: 10px; padding: 14px; flex-wrap: wrap; }
.table-toolbar input[type="search"], .table-toolbar select { padding: 8px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 13px; }
.table-empty { padding: 40px 20px; text-align: center; color: var(--text-muted); }
.table-pagination { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; font-size: 12.5px; color: var(--text-muted); border-top: 1px solid var(--border); }

/* ---------- Badges / statuts ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 999px; font-size: 11.5px; font-weight: 700; text-transform: capitalize; }
.badge-gray { background: #e5e7eb; color: #374151; }
.badge-blue { background: #dbeafe; color: #1e40af; }
.badge-green { background: #d1fae5; color: #065f46; }
.badge-yellow { background: #fef3c7; color: #92400e; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-purple { background: #ede9fe; color: #5b21b6; }
[data-theme="dark"] .badge-gray { background: #2a3145; color: #cbd5e1; }
[data-theme="dark"] .badge-blue { background: #1e3a63; color: #93c5fd; }
[data-theme="dark"] .badge-green { background: #113d2f; color: #6ee7b7; }
[data-theme="dark"] .badge-yellow { background: #453510; color: #fcd34d; }
[data-theme="dark"] .badge-red { background: #4a1e1e; color: #fca5a5; }
[data-theme="dark"] .badge-purple { background: #34215e; color: #c4b5fd; }

/* ---------- Formulaires ---------- */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.form-grid.cols-1 { grid-template-columns: 1fr; }
.form-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 700px) { .form-grid, .form-grid.cols-3 { grid-template-columns: 1fr; } }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.span-2 { grid-column: span 2; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--text-muted); }
.field .req { color: var(--danger); }
.field input, .field select, .field textarea {
  padding: 9px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg);
  color: var(--text); font-size: 13.5px; font-family: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--sp-blue-light); outline-offset: 1px; }
.field .error-msg { color: var(--danger); font-size: 11.5px; }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin: 0 0 14px; }
legend { padding: 0 6px; font-size: 13px; font-weight: 700; }

/* ---------- Modales ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(8,12,24,.55); display: flex; align-items: flex-start;
  justify-content: center; padding: 40px 16px; z-index: 100; overflow-y: auto;
}
.modal {
  background: var(--bg-elevated); border-radius: var(--radius-lg); width: 100%; max-width: 640px;
  box-shadow: var(--shadow-lg); animation: modal-in 140ms ease;
}
.modal.modal-lg { max-width: 920px; }
.modal.modal-sm { max-width: 420px; }
@keyframes modal-in { from { opacity: 0; transform: translateY(-8px);} to { opacity: 1; transform: translateY(0);} }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h2 { font-size: 16px; }
.modal-body { padding: 20px; max-height: 70vh; overflow-y: auto; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); }
.modal-close { cursor: pointer; color: var(--text-muted); font-size: 18px; background: none; border: none; }

/* ---------- Toasts ---------- */
.toast-stack { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 200; max-width: 340px; }
.toast {
  background: var(--bg-elevated); border: 1px solid var(--border); border-left: 4px solid var(--sp-blue);
  border-radius: var(--radius-sm); padding: 12px 14px; box-shadow: var(--shadow-lg); font-size: 13px;
  animation: toast-in 160ms ease; display: flex; gap: 10px; align-items: flex-start;
}
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warning); }
@keyframes toast-in { from { opacity: 0; transform: translateX(20px);} to { opacity: 1; transform: translateX(0);} }
.toast strong { display: block; margin-bottom: 2px; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tab-btn { padding: 10px 14px; font-size: 13px; font-weight: 600; color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tab-btn.active { color: var(--sp-blue); border-bottom-color: var(--sp-blue); }

/* ---------- Divers ---------- */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .ic { font-size: 34px; margin-bottom: 10px; }
.timeline { border-left: 2px solid var(--border); margin-left: 8px; padding-left: 18px; }
.timeline-item { position: relative; padding-bottom: 18px; }
.timeline-item::before { content: ""; position: absolute; left: -23px; top: 3px; width: 10px; height: 10px; border-radius: 50%; background: var(--sp-blue); }
.timeline-item .ti-date { font-size: 11.5px; color: var(--text-muted); }
.quick-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.diff-block { background: var(--bg-hover); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 12.5px; margin-top: 6px; }
.diff-block .diff-field { font-weight: 700; }
.diff-old { color: var(--danger); text-decoration: line-through; }
.diff-new { color: var(--success); }
.login-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--sp-gradient); padding: 20px; }
.login-card { background: var(--bg-elevated); border-radius: var(--radius-lg); padding: 40px 36px; width: 100%; max-width: 400px; text-align: center; box-shadow: var(--shadow-lg); }
.login-card img { width: 76px; margin-bottom: 12px; }
.login-card h1 { font-size: 18px; }
.login-card p { color: var(--text-muted); font-size: 13px; margin-bottom: 24px; }
.google-btn { width: 100%; justify-content: center; padding: 11px; }
.loader { width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--sp-blue); border-radius: 50%; animation: spin 700ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.mono { font-family: "SFMono-Regular", Consolas, monospace; }

/* ---------- Planning (calendrier) ---------- */
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.cal-head { font-size: 11.5px; font-weight: 700; color: var(--text-muted); text-align: center; padding: 4px 0; }
.cal-cell { min-height: 92px; background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 6px; cursor: pointer; transition: background var(--transition); }
.cal-cell:hover { background: var(--bg-hover); }
.cal-cell.empty { background: transparent; border-color: transparent; cursor: default; }
.cal-cell.today { border-color: var(--sp-blue); box-shadow: 0 0 0 1px var(--sp-blue) inset; }
.cal-day { font-size: 12px; font-weight: 700; margin-bottom: 4px; }
.cal-item { font-size: 10px; padding: 2px 5px; border-radius: 4px; margin-bottom: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 900px) { .cal-grid { grid-template-columns: repeat(7, minmax(70px,1fr)); overflow-x: auto; } }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); z-index: 40; }
  .main-area { margin-left: 0; }
  .topbar .menu-toggle { display: flex; }
  .topbar-search { display: none; }
  .mobile-search-btn { display: flex; }
  .topbar-search.mobile-open {
    display: flex;
    position: absolute;
    top: 100%; left: 0; right: 0;
    max-width: none;
    margin: 0;
    padding: 10px 14px 14px;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    z-index: 35;
  }
}

@media (max-width: 700px) {
  .content { padding: 16px 14px 50px; }
  .topbar { padding: 0 14px; gap: 10px; }
  .page-header h1 { font-size: 18px; }
  .modal-backdrop { padding: 0; align-items: stretch; }
  .modal { max-width: 100%; min-height: 100%; border-radius: 0; }
  .modal.modal-lg, .modal.modal-sm { max-width: 100%; }
  .table-toolbar { padding: 12px; }
  .user-chip div { display: none; }
  .user-chip { padding: 5px; }
}

@media (max-width: 480px) {
  .toast-stack { left: 12px; right: 12px; bottom: 12px; max-width: none; }
  .kpi-card .kpi-value { font-size: 22px; }
}
