/* ============================================================================
   AIKeep — Design System
   Fondation visuelle réutilisable : polices, tokens, reset, typographie,
   primitives (boutons, champs, cartes, avatars, badges). Aucune dépendance
   réseau au runtime — polices bundlées en local (voir static/fonts).
   ========================================================================== */

/* ---------- Polices (bundlées, offline) ---------------------------------- */

@font-face {
    font-family: "Inter";
    font-style: normal;
    font-weight: 100 900;
    font-display: swap;
    src: url("/static/fonts/inter-variable.woff2") format("woff2");
}

@font-face {
    font-family: "JetBrains Mono";
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url("/static/fonts/jetbrains-mono.woff2") format("woff2");
}

/* ---------- Tokens : thème sombre (défaut) ------------------------------- */

:root {
    /* Surfaces — profondeur par couches */
    --bg: #0b0d12;
    --bg-elevated: #10131a;
    --surface: #151922;
    --surface-hover: #1c212c;
    --surface-active: #232936;
    --border: #262c38;
    --border-strong: #333b4a;

    /* Texte */
    --text: #eceef2;
    --text-secondary: #b3bac7;
    --text-muted: #7d8696;
    --text-faint: #59616f;

    /* Accent — indigo affiné */
    --accent: #6d8bff;
    --accent-hover: #829bff;
    --accent-strong: #5474ff;
    --accent-contrast: #ffffff;
    --accent-soft: rgba(109, 139, 255, 0.14);
    --accent-ring: rgba(109, 139, 255, 0.35);

    /* Sémantique */
    --success: #3ecf8e;
    --success-soft: rgba(62, 207, 142, 0.14);
    --warning: #f2b34a;
    --warning-soft: rgba(242, 179, 74, 0.14);
    --danger: #f56565;
    --danger-soft: rgba(245, 101, 101, 0.13);

    /* Typographie */
    --font-sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
    --font-mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", "Consolas", monospace;

    --text-xs: 11px;
    --text-sm: 12.5px;
    --text-base: 14px;
    --text-md: 15px;
    --text-lg: 17px;
    --text-xl: 20px;
    --text-2xl: 26px;
    --text-3xl: 33px;

    /* Espacements (base 4px) */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;
    --space-12: 48px;
    --space-16: 64px;

    /* Rayons */
    --radius-sm: 7px;
    --radius: 10px;
    --radius-md: 13px;
    --radius-lg: 18px;
    --radius-full: 999px;

    /* Ombres — subtiles, en couches */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.45);

    /* Transitions */
    --ease: cubic-bezier(0.32, 0.72, 0, 1);
    --dur: 0.16s;

    color-scheme: dark;
}

/* ---------- Tokens : thème clair ---------------------------------------- */

:root[data-theme="light"] {
    --bg: #f7f8fa;
    --bg-elevated: #ffffff;
    --surface: #ffffff;
    --surface-hover: #f1f3f6;
    --surface-active: #e8ebf0;
    --border: #e4e7ec;
    --border-strong: #d3d8e0;

    --text: #141821;
    --text-secondary: #3d4552;
    --text-muted: #6a7383;
    --text-faint: #9aa2b1;

    --accent: #4361e8;
    --accent-hover: #3a56d4;
    --accent-strong: #3450c8;
    --accent-contrast: #ffffff;
    --accent-soft: rgba(67, 97, 232, 0.09);
    --accent-ring: rgba(67, 97, 232, 0.28);

    --success: #16a06a;
    --success-soft: rgba(22, 160, 106, 0.1);
    --warning: #c98410;
    --warning-soft: rgba(201, 132, 16, 0.12);
    --danger: #d93a3a;
    --danger-soft: rgba(217, 58, 58, 0.08);

    --shadow-sm: 0 1px 2px rgba(20, 24, 33, 0.06);
    --shadow: 0 4px 16px rgba(20, 24, 33, 0.08);
    --shadow-lg: 0 12px 40px rgba(20, 24, 33, 0.12);

    color-scheme: light;
}

/* ---------- Reset ------------------------------------------------------- */

*,
*::before,
*::after {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.55;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    font-feature-settings: "cv02", "cv03", "cv04", "cv11";
}

h1, h2, h3, h4 {
    margin: 0;
    font-weight: 650;
    letter-spacing: -0.014em;
    line-height: 1.2;
    color: var(--text);
}

p { margin: 0; }

a {
    color: var(--accent);
    text-decoration: none;
}
a:hover { text-decoration: underline; }

button {
    font-family: inherit;
    cursor: pointer;
}

::selection {
    background: var(--accent-ring);
    color: var(--text);
}

/* ---------- Scrollbar --------------------------------------------------- */

* {
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

*::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

*::-webkit-scrollbar-track {
    background: transparent;
}

*::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: var(--radius-full);
    border: 3px solid transparent;
    background-clip: padding-box;
}

*::-webkit-scrollbar-thumb:hover {
    background: var(--text-faint);
    background-clip: padding-box;
}

/* ---------- Utilitaires typographiques ---------------------------------- */

.text-muted { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }
.mono { font-family: var(--font-mono); }

.eyebrow {
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    color: var(--text-muted);
}

/* ============================================================================
   PRIMITIVES
   ========================================================================== */

/* ---------- Boutons ----------------------------------------------------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    font-size: var(--text-base);
    font-weight: 550;
    line-height: 1;
    padding: 0 var(--space-4);
    height: 38px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text);
    white-space: nowrap;
    transition: background var(--dur) var(--ease),
                border-color var(--dur) var(--ease),
                color var(--dur) var(--ease),
                transform 0.08s var(--ease),
                box-shadow var(--dur) var(--ease);
}

.btn:hover { text-decoration: none; }
.btn:active { transform: translateY(1px); }

.btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--accent-ring);
}

.btn-primary {
    background: var(--accent);
    color: var(--accent-contrast);
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text);
}
.btn-secondary:hover {
    background: var(--surface-hover);
    border-color: var(--border-strong);
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}
.btn-ghost:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.btn-danger {
    background: var(--danger-soft);
    color: var(--danger);
}
.btn-danger:hover { background: var(--danger); color: #fff; }

.btn-sm { height: 30px; padding: 0 var(--space-3); font-size: var(--text-sm); }
.btn-icon { width: 38px; padding: 0; }
.btn-icon.btn-sm { width: 30px; }

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.btn svg { width: 16px; height: 16px; flex-shrink: 0; }

/* ---------- Champs ------------------------------------------------------ */

.field {
    width: 100%;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-family: inherit;
    font-size: var(--text-base);
    padding: 9px 12px;
    transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

.field::placeholder { color: var(--text-faint); }

/* Menu déroulant : réutilise .field + flèche custom (la flèche native est
   incohérente entre navigateurs/thèmes). */
select.field {
    appearance: none;
    -webkit-appearance: none;
    padding-right: 34px;
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238a94a6' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 16px;
}

.field:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-ring);
}

.field-label {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    font-size: var(--text-sm);
    font-weight: 550;
    color: var(--text-secondary);
}

/* ---------- Cartes ------------------------------------------------------ */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-6);
}

.card-hover {
    transition: background var(--dur) var(--ease),
                border-color var(--dur) var(--ease),
                transform var(--dur) var(--ease);
}
.card-hover:hover {
    background: var(--surface-hover);
    border-color: var(--border-strong);
    transform: translateY(-2px);
}

/* ---------- Avatars ----------------------------------------------------- */

.avatar {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.02em;
    user-select: none;
}

.avatar-accent {
    background: linear-gradient(140deg, var(--accent), var(--accent-strong));
    color: var(--accent-contrast);
}

.avatar-neutral {
    background: var(--surface-active);
    border: 1px solid var(--border);
    color: var(--text-secondary);
}

/* ---------- Badges ------------------------------------------------------ */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: var(--text-xs);
    font-weight: 600;
    padding: 3px 9px;
    border-radius: var(--radius-full);
    background: var(--accent-soft);
    color: var(--accent);
}

.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft, rgba(214, 158, 46, 0.12)); color: var(--warning, #d69e2e); }

.dot {
    width: 7px;
    height: 7px;
    border-radius: var(--radius-full);
    background: var(--success);
    box-shadow: 0 0 8px var(--success);
}

/* ---------- Divers ------------------------------------------------------ */

.alert {
    border-radius: var(--radius);
    padding: 12px 15px;
    font-size: var(--text-base);
    border: 1px solid transparent;
}
.alert-error { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.alert-warning { background: var(--warning-soft, rgba(214, 158, 46, 0.12)); border-color: var(--warning, #d69e2e); color: var(--warning, #d69e2e); }
.alert-success { background: var(--success-soft, rgba(56, 161, 105, 0.12)); border-color: var(--success, #38a169); color: var(--success, #38a169); }

hr.divider {
    border: none;
    border-top: 1px solid var(--border);
    margin: var(--space-6) 0;
}

/* ---------- Onglets (primitif réutilisable) ------------------------------
   Conteneur [data-tabs] + panneaux frères [data-pane] (bascule par ui.js).
   Utilisé par le panneau Sources·Mémoire ET la page Paramètres. */
.panel-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: var(--space-4); flex-wrap: wrap; }
.panel-tab {
    background: none;
    border: none;
    color: var(--text-muted);
    font-weight: 600;
    font-size: var(--text-sm);
    cursor: pointer;
    padding: 9px 14px;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    font-family: inherit;
}
.panel-tab:hover { color: var(--text); }
.panel-tab.is-active { color: var(--text); border-bottom-color: var(--accent); }
.tab-n {
    display: inline-block;
    min-width: 18px;
    text-align: center;
    background: var(--surface-active);
    color: var(--text-secondary);
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    padding: 1px 6px;
    margin-left: 5px;
}
.panel-pane { display: none; }
.panel-pane.is-active { display: block; }

/* ---- Interrupteur (toggle switch) — primitive réutilisable ------------------
   Structure : <label class="toggle"><input type="checkbox" class="toggle-input">
   <span class="toggle-track"><span class="toggle-thumb"></span></span> …</label> */
.toggle { display: inline-flex; align-items: center; gap: var(--space-2); cursor: pointer; }
.toggle-input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-track {
    width: 38px; height: 22px; flex-shrink: 0;
    border-radius: 999px; background: var(--border);
    position: relative; transition: background var(--dur, .15s) var(--ease, ease);
}
.toggle-thumb {
    position: absolute; top: 2px; left: 2px; width: 18px; height: 18px;
    border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
    transition: transform var(--dur, .15s) var(--ease, ease);
}
.toggle-input:checked + .toggle-track { background: var(--accent); }
.toggle-input:checked + .toggle-track .toggle-thumb { transform: translateX(16px); }
.toggle-input:focus-visible + .toggle-track { outline: 2px solid var(--accent-ring, var(--accent)); outline-offset: 2px; }

/* ---- Spinner (indicateur de chargement) — primitive réutilisable ------------ */
.spinner {
    display: inline-block; width: 13px; height: 13px; vertical-align: -2px;
    border: 2px solid var(--border); border-top-color: var(--accent);
    border-radius: 50%; animation: aikeep-spinner .6s linear infinite;
}
@keyframes aikeep-spinner { to { transform: rotate(360deg); } }

/* ---- Croix de fermeture des popups (#27) -----------------------------------
   Primitive réutilisable : coin haut-droit d'un popup (panneau Sources·Mémoire,
   modale de confirmation, et tout futur popup). Le conteneur doit être en
   position relative/absolute. La méthode d'origine (re-cliquer le bouton
   d'ouverture) reste toujours disponible en parallèle. */
.popup-close {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: none;
    border-radius: var(--radius);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: background 0.12s ease, color 0.12s ease;
    z-index: 1;
}
.popup-close:hover { background: var(--surface-active); color: var(--text); }
.popup-close svg { width: 16px; height: 16px; }
