/* ===== Jetons de design ===== */
:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --surface-3: #e6e5e1;
  --border: #dcdbd6;
  --border-strong: #c3c2b7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;
  --accent-strong: #1c5cab;
  --accent-soft: #e3eefc;
  --danger: #c93838;
  --danger-soft: #fbe9e9;
  --ok: #117a3d;
  --ok-soft: #e4f4ea;
  --warn: #9a6300;
  --warn-soft: #fdf1d8;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.05);
  --shadow-lg: 0 10px 40px rgba(0,0,0,.18);
  --radius: 10px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #242422;
    --surface-3: #2e2e2b;
    --border: #383835;
    --border-strong: #4d4d49;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --text-3: #9a9990;
    --accent: #3987e5;
    --accent-strong: #6da7ec;
    --accent-soft: #1b2e47;
    --danger: #e66767;
    --danger-soft: #3a1f1f;
    --ok: #4cc47f;
    --ok-soft: #173024;
    --warn: #e0a53a;
    --warn-soft: #362a12;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    --shadow-lg: 0 10px 40px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 var(--font); background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 17px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
.muted { color: var(--text-2); }
.small { font-size: 12.5px; }
.boot { padding: 40px; color: var(--text-2); }
.hidden { display: none !important; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }

/* ===== En-tête ===== */
.topbar { position: sticky; top: 0; z-index: 50; display: flex; align-items: center; gap: 18px; height: 56px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 15px; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand .logo { width: 26px; height: 26px; }
.nav { display: flex; gap: 2px; flex: 1; overflow-x: auto; }
.nav a { padding: 7px 12px; border-radius: 7px; color: var(--text-2); font-weight: 500; white-space: nowrap; }
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent-strong); }
.usermenu { position: relative; }
.usermenu > button { display: flex; align-items: center; gap: 8px; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 600; font-size: 12px; }

.page { max-width: 1400px; margin: 0 auto; padding: 24px 20px 60px; }
.page.wide { max-width: none; }
.page-head { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .grow { flex: 1; min-width: 220px; }
.page-head p { margin: 4px 0 0; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ===== Boutons & champs ===== */
.btn { display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 13px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 13px; }
.btn.icon { width: 30px; padding: 0; justify-content: center; }
.btn svg, .ico { width: 16px; height: 16px; flex: none; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 7px; height: 34px; padding: 0 10px; width: 100%; min-width: 0;
}
textarea { height: auto; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
label.field, div.field { display: block; margin-bottom: 12px; }
label.field > span, .field-label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 5px; }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; margin: 6px 0; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { min-width: 0; }
.grow { flex: 1; }

/* ===== Cartes & listes ===== */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; min-width: 0; }
a.card { color: inherit; }
a.card:hover, .card.clickable:hover { border-color: var(--accent); text-decoration: none; cursor: pointer; }
.card .card-top { display: flex; align-items: center; gap: 10px; }
.card .card-title { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .card-desc { color: var(--text-2); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 1em; }
.card .card-foot { display: flex; align-items: center; gap: 6px; margin-top: auto; padding-top: 6px; color: var(--text-3); font-size: 12px; flex-wrap: wrap; }
.type-ico { width: 38px; height: 38px; border-radius: 9px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent-strong); flex: none; }
.type-ico svg { width: 22px; height: 22px; }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.blue { background: var(--accent-soft); color: var(--accent-strong); }
.empty { text-align: center; padding: 50px 20px; color: var(--text-2); border: 1px dashed var(--border-strong); border-radius: var(--radius); background: var(--surface); }
.empty h2 { color: var(--text); margin-bottom: 6px; }
.empty .btn { margin-top: 14px; }
.filters-line { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.filters-line input[type=search] { max-width: 320px; }
.filters-line select { width: auto; }

/* ===== Tableaux ===== */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.data { border-collapse: collapse; width: 100%; font-size: 13px; }
table.data th, table.data td { padding: 7px 11px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
table.data th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; color: var(--text-2); z-index: 1; cursor: default; user-select: none; }
table.data th.sortable { cursor: pointer; }
table.data th.sortable:hover { color: var(--text); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:last-child td { border-bottom: none; }
table.data tbody tr:hover td { background: var(--surface-2); }
table.data td.null { color: var(--text-3); font-style: italic; }
.type-tag { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; padding: 1px 6px; border-radius: 4px; background: var(--surface-3); color: var(--text-2); }
.type-tag.int, .type-tag.real { background: var(--accent-soft); color: var(--accent-strong); }
.type-tag.date { background: var(--warn-soft); color: var(--warn); }

/* ===== Modale ===== */
.modal-back { position: fixed; inset: 0; background: rgba(10,10,10,.45); z-index: 200; display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; overflow: auto; }
.modal { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); width: 100%; max-width: 520px; display: flex; flex-direction: column; max-height: 88vh; }
.modal.lg { max-width: 860px; }
.modal.xl { max-width: 1200px; height: 84vh; }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { flex: 1; }
.modal-body { padding: 18px 20px; overflow: auto; flex: 1; min-height: 0; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.modal-foot .left { margin-right: auto; }

/* ===== Notifications ===== */
#toasts { position: fixed; bottom: 18px; right: 18px; z-index: 300; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 9px; box-shadow: var(--shadow-lg); max-width: 380px; animation: tin .2s ease; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes tin { from { transform: translateY(8px); opacity: 0; } }

/* ===== Connexion ===== */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: 100%; max-width: 380px; padding: 28px; gap: 4px; }
.auth .brand { margin-bottom: 14px; font-size: 18px; }
.auth .error { color: var(--danger); min-height: 20px; margin: 4px 0 10px; font-size: 13px; }

/* ===== Éditeur de visualisation ===== */
.editor { display: grid; grid-template-columns: 380px 1fr; gap: 18px; align-items: start; }
.editor .panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; position: sticky; top: 72px; max-height: calc(100vh - 90px); overflow: auto; }
.section { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 14px; }
.section:first-child { border: 0; padding: 0; margin: 0; }
.section > h3 { margin-bottom: 10px; display: flex; align-items: center; gap: 8px; }
.types { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.types button { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 9px 4px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text-2); cursor: pointer; font: inherit; font-size: 11.5px; font-weight: 500; }
.types button svg { width: 22px; height: 22px; }
.types button:hover { border-color: var(--border-strong); color: var(--text); }
.types button.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-strong); }
.listrow { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; }
.listrow > select, .listrow > input { flex: 1; }
.listrow > .narrow { flex: 0 0 110px; }
.addlink { background: none; border: 0; color: var(--accent); font: inherit; font-weight: 500; cursor: pointer; padding: 4px 0; }
.addlink:hover { text-decoration: underline; }
.preview { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); min-height: 520px; display: flex; flex-direction: column; }
.preview-head { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.preview-head h2 { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preview-body { flex: 1; position: relative; min-height: 440px; padding: 12px; }
.preview-foot { padding: 8px 18px; border-top: 1px solid var(--border); color: var(--text-3); font-size: 12px; display: flex; gap: 14px; align-items: center; }
.chipcol { display: flex; flex-wrap: wrap; gap: 4px 12px; max-height: 220px; overflow: auto; border: 1px solid var(--border); border-radius: 7px; padding: 6px 10px; }
.chipcol .check { margin: 3px 0; font-size: 13px; }

/* ===== Visualisations ===== */
.viz { position: absolute; inset: 0; }
.viz-state { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; color: var(--text-2); padding: 16px; font-size: 13px; }
.viz-state.error { color: var(--danger); }
.spinner { width: 22px; height: 22px; border: 2.5px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.kpi { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; padding: 8px 16px; }
.kpi-value { font-size: clamp(26px, 5vw, 46px); font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.1; }
.kpi-label { color: var(--text-2); font-size: 13px; margin-top: 4px; }
.kpi-delta { font-size: 13px; font-weight: 600; margin-top: 6px; }
.kpi-delta.up { color: var(--ok); } .kpi-delta.down { color: var(--danger); }
.kpi-spark { height: 46px; margin-top: 8px; }
.viz-table { position: absolute; inset: 0; overflow: auto; }
.viz-table table.data th { background: var(--surface-2); }
.viz-note { position: absolute; bottom: 2px; right: 8px; font-size: 11px; color: var(--text-3); background: var(--surface); padding: 0 4px; }

/* ===== Tableau de bord ===== */
.dash-head { display: flex; gap: 12px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 12px; }
.dash-head .grow { min-width: 240px; }
.dash-filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; min-height: 0; }
.dfilter { display: flex; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 9px; padding: 4px 6px 4px 10px; }
.dfilter .lbl { font-size: 12.5px; font-weight: 600; color: var(--text-2); white-space: nowrap; }
.dfilter input { height: 28px; width: 130px; }
.dfilter .btn { height: 28px; }
.dfilter.active { border-color: var(--accent); }
.editbar { background: var(--accent-soft); border: 1px solid var(--accent); color: var(--accent-strong); border-radius: 10px; padding: 8px 12px; margin-bottom: 12px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-weight: 500; }
.editbar .grow { min-width: 200px; }
.grid-stack { min-height: 200px; }
.grid-stack.stack-mobile { display: flex; flex-direction: column; gap: 10px; height: auto !important; }
.stack-mobile > .grid-stack-item { position: relative !important; width: 100% !important; left: auto !important; top: auto !important; }
.stack-mobile > .grid-stack-item > .grid-stack-item-content { position: absolute !important; inset: 0 !important; }
.grid-stack.editing { background-image: radial-gradient(var(--border-strong) 1px, transparent 1px); background-size: calc(100% / 12) 88px; border-radius: 10px; }
.grid-stack-item-content { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden !important; inset: 0 !important; }
.w-head { display: flex; align-items: center; gap: 6px; padding: 9px 8px 4px 14px; min-height: 38px; }
.w-title { flex: 1; font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.w-head .btn { opacity: 0; transition: opacity .15s; }
.grid-stack-item:hover .w-head .btn, .w-head .btn:focus, .editing .w-head .btn { opacity: 1; }
.editing .w-head { cursor: move; }
.w-body { flex: 1; position: relative; min-height: 0; margin: 0 6px 6px; }
.w-text { padding: 12px 40px 12px 14px; overflow: auto; flex: 1; display: flex; flex-direction: column; justify-content: center; }
.w-head.text-head { position: absolute; top: 4px; right: 4px; padding: 0; min-height: 0; z-index: 2; }
.editing .w-text { cursor: move; }
.w-text h1 { font-size: 20px; } .w-text h2 { font-size: 16px; } .w-text p { margin: 4px 0; }
.w-filter-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.menu { position: absolute; z-index: 120; background: var(--surface); border: 1px solid var(--border); border-radius: 9px; box-shadow: var(--shadow-lg); padding: 5px; min-width: 200px; }
.menu button { display: flex; width: 100%; align-items: center; gap: 8px; text-align: left; background: none; border: 0; padding: 7px 10px; border-radius: 6px; font: inherit; color: var(--text); cursor: pointer; }
.menu button:hover { background: var(--surface-2); }
.menu button.danger { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }
.popover { position: absolute; z-index: 150; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow-lg); padding: 10px; width: 300px; }
.popover .vals { max-height: 260px; overflow: auto; margin: 8px 0; }
.popover .vals .check { margin: 2px 0; font-size: 13px; justify-content: space-between; }
.popover .vals .check span.n { color: var(--text-3); font-size: 11.5px; margin-left: auto; }
.picklist { max-height: 55vh; overflow: auto; display: flex; flex-direction: column; gap: 6px; }
.pick { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px; cursor: pointer; }
.pick:hover { border-color: var(--border-strong); }
.pick.on { border-color: var(--accent); background: var(--accent-soft); }
.pick .type-ico { width: 32px; height: 32px; }
.pick .type-ico svg { width: 18px; height: 18px; }
.help { background: var(--surface-2); border-radius: 9px; padding: 12px 14px; color: var(--text-2); font-size: 13px; margin-bottom: 16px; }
.dropzone { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 18px; text-align: center; color: var(--text-2); background: var(--surface); margin-bottom: 16px; }
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.progress { height: 6px; border-radius: 4px; background: var(--surface-3); overflow: hidden; margin-top: 8px; }
.progress > div { height: 100%; background: var(--accent); transition: width .2s; }

@media (max-width: 960px) {
  .editor { grid-template-columns: 1fr; }
  .editor .panel { position: static; max-height: none; }
  .topbar { gap: 10px; padding: 0 12px; }
  .brand span, .usermenu .small { display: none; }
  .page { padding: 16px 12px 50px; }
}
@media print {
  .topbar, .dash-head .toolbar, .w-head .btn, .editbar { display: none !important; }
  body { background: #fff; }
}
