:root {
    --bg: #f3f4f7;
    --panel: #ffffff;
    --border: #e3e6ec;
    --text: #101828;
    --text-muted: #667085;
    --text-faint: #98a2b3;
    --accent: #3454d1;
    --accent-dark: #26399c;
    --accent-soft: #eaefff;
    --danger: #d0342c;
    --danger-soft: #fdecea;
    --success-soft: #e6f4ea;
    --success-text: #1e7b34;
    --sidebar-bg: #161d2f;
    --sidebar-text: #aab2c5;
    --sidebar-text-active: #ffffff;
    --sidebar-hover: #212a42;
    --radius: 10px;
    --font-ui: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --font-mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

body {
    font-family: var(--font-ui);
    background: var(--bg);
    color: var(--text);
    margin: 0;
    font-size: 14px;
}

a { color: inherit; }

/* ---------- Layout général ---------- */
.app-shell {
    display: grid;
    grid-template-columns: 240px 1fr;
    min-height: 100vh;
}

.content {
    padding: 28px 36px;
    max-width: 1080px;
    width: 100%;
}

.logged-out-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at top, #1c2540 0%, #0f1626 65%);
    padding: 20px;
}

/* ---------- Sidebar ---------- */
.sidebar {
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    position: sticky;
    top: 0;
    height: 100vh;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 6px 10px;
}
.brand-mark {
    background: var(--accent);
    color: #fff;
    font-weight: 700;
    font-size: 12px;
    width: 30px; height: 30px;
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    letter-spacing: 0.5px;
}
.brand-name { color: #fff; font-weight: 600; font-size: 15px; }

.btn-check {
    width: 100%;
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    display: flex; align-items: center; gap: 8px;
    transition: background 0.15s ease;
}
.btn-check:hover { background: var(--accent-dark); }

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar-nav a {
    display: flex; align-items: center; gap: 10px;
    padding: 9px 10px; border-radius: 8px;
    text-decoration: none; color: var(--sidebar-text);
    font-size: 13.5px; font-weight: 500;
}
.sidebar-nav a:hover { background: var(--sidebar-hover); color: #fff; }
.sidebar-nav a.active { background: var(--sidebar-hover); color: #fff; }
.sidebar-nav .icon { width: 18px; text-align: center; opacity: 0.9; }
.sidebar-nav .count {
    margin-left: auto; background: var(--accent);
    color: #fff; font-size: 11px; font-weight: 700;
    padding: 1px 7px; border-radius: 10px;
}

.sidebar-section { margin-top: 4px; flex: 1; overflow-y: auto; }
.sidebar-section-title {
    font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--text-faint); padding: 6px 10px; font-weight: 600;
}
.sidebar-mailbox {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 10px; border-radius: 8px;
    text-decoration: none; color: var(--sidebar-text); font-size: 13px;
}
.sidebar-mailbox:hover { background: var(--sidebar-hover); color: #fff; }
.sidebar-mailbox .dot {
    width: 6px; height: 6px; border-radius: 50%; background: #4c5d82; flex-shrink: 0;
}
.sidebar-mailbox .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.sidebar-mailbox .count {
    background: rgba(255,255,255,0.12); color: #fff; font-size: 11px;
    font-weight: 600; padding: 1px 7px; border-radius: 10px;
}

.sidebar-logout {
    text-decoration: none; color: var(--sidebar-text);
    font-size: 13px; padding: 8px 10px; border-top: 1px solid rgba(255,255,255,0.08);
    padding-top: 14px;
}
.sidebar-logout:hover { color: #fff; }

/* ---------- Header / recherche ---------- */
.content-header {
    display: flex; align-items: center; justify-content: space-between;
    flex-wrap: wrap; gap: 14px; margin-bottom: 20px;
}
.content-header h1 { font-size: 20px; margin: 0; }

.search-bar {
    display: flex; gap: 8px; flex-wrap: wrap; align-items: center;
}
.search-bar input[type="text"] {
    padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px;
    font-size: 13.5px; min-width: 220px; background: var(--panel);
}
.search-bar select {
    padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
    font-size: 13.5px; background: var(--panel); color: var(--text);
}

/* ---------- Boutons ---------- */
.btn, .btn-small {
    display: inline-block; background: var(--accent); color: #fff;
    border: none; padding: 9px 16px; border-radius: 8px; cursor: pointer;
    text-decoration: none; font-size: 13.5px; font-weight: 600;
}
.btn:hover, .btn-small:hover { background: var(--accent-dark); }
.btn-small { padding: 7px 12px; font-size: 13px; }
.btn-ghost { background: transparent; color: var(--accent); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #a82720; }
.inline { display: inline-block; margin: 0; }

/* ---------- Flash messages ---------- */
.flash-stack { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.flash { padding: 10px 14px; border-radius: 8px; font-size: 13.5px; }
.flash-success { background: var(--success-soft); color: var(--success-text); }
.flash-error { background: var(--danger-soft); color: var(--danger); }
.flash-stack-login { position: absolute; top: 24px; width: 320px; }

/* ---------- Etat vide ---------- */
.empty-state {
    background: var(--panel); border: 1px dashed var(--border); border-radius: var(--radius);
    padding: 48px 24px; text-align: center; color: var(--text-muted);
}
.empty-icon { font-size: 28px; margin-bottom: 10px; color: var(--text-faint); }
.empty-state p { margin: 0 0 16px; }
.empty { color: var(--text-faint); padding: 20px 0; }

/* ---------- Liste de mails (inbox) ---------- */
.inbox-list {
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
    overflow: hidden;
}
.inbox-row {
    display: flex; align-items: center; gap: 14px;
    padding: 13px 18px; text-decoration: none; color: var(--text);
    border-bottom: 1px solid var(--border);
}
.inbox-row:last-child { border-bottom: none; }
.inbox-row:hover { background: #f8f9fc; }

.unread-dot {
    width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
    background: transparent;
}
.inbox-row.unread .unread-dot { background: var(--accent); }

.avatar {
    width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
    background: var(--accent-soft); color: var(--accent-dark);
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 13px;
}

.col-sender {
    width: 170px; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis;
    white-space: nowrap; font-size: 13.5px;
}
.inbox-row.unread .col-sender { font-weight: 700; }

.col-subject {
    flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
    font-size: 13.5px; color: var(--text-muted);
}
.subject-text { color: var(--text); }
.inbox-row.unread .subject-text { font-weight: 700; }
.snippet-text { color: var(--text-faint); }

.col-meta {
    display: flex; align-items: center; gap: 8px; flex-shrink: 0;
}
.mailbox-tag {
    background: var(--accent-soft); color: var(--accent-dark);
    font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 10px;
    white-space: nowrap;
}
.keyword-tag {
    background: #fff3e0; color: #b26a00;
    font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 10px;
    white-space: nowrap;
}
.folder-tag {
    background: #f1eefc; color: #6a3fd6;
    font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 10px;
    white-space: nowrap;
}
.date { color: var(--text-faint); font-size: 12px; width: 110px; text-align: right; font-family: var(--font-mono); }

/* ---------- Vue de lecture d'un mail ---------- */
.back-link {
    display: inline-block; margin-bottom: 16px; color: var(--text-muted);
    text-decoration: none; font-size: 13.5px;
}
.back-link:hover { color: var(--accent); }

.mail-actions { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }

.mail-reader {
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 28px 32px;
}
.mail-reader-head h1 { font-size: 19px; margin: 0 0 8px; }
.mail-reader-tags { display: flex; gap: 8px; margin-bottom: 20px; }
.mail-reader-meta {
    display: flex; align-items: center; gap: 12px;
    padding-bottom: 18px; border-bottom: 1px solid var(--border); margin-bottom: 20px;
}
.avatar-lg { width: 42px; height: 42px; font-size: 16px; }
.mail-reader-sender { font-weight: 700; font-size: 14.5px; }
.mail-reader-date { color: var(--text-faint); font-size: 12.5px; font-family: var(--font-mono); }
.mail-reader-body {
    white-space: pre-wrap; word-wrap: break-word;
    line-height: 1.65; font-size: 14px; color: var(--text);
}

.mail-reader-html-wrapper {
    border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
    background: #fff;
}
.mail-reader-html {
    width: 100%; height: 560px; border: none; display: block;
}

.attachments-section {
    margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--border);
}
.attachments-title {
    font-size: 12.5px; font-weight: 700; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 12px;
}
.attachment-images {
    display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 12px;
}
.attachment-image-tile {
    width: 140px; text-decoration: none; color: var(--text-muted);
    border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
    background: var(--bg);
}
.attachment-image-tile img {
    width: 100%; height: 110px; object-fit: cover; display: block;
    background: #eee;
}
.attachment-image-name {
    display: block; font-size: 11.5px; padding: 6px 8px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.attachment-files { display: flex; flex-direction: column; gap: 6px; }
.attachment-file-row {
    display: flex; align-items: center; gap: 10px;
    padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
    text-decoration: none; color: var(--text); font-size: 13.5px;
}
.attachment-file-row:hover { background: var(--accent-soft); }
.attachment-file-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-file-size { color: var(--text-faint); font-size: 12px; font-family: var(--font-mono); }

.attachment-indicator { color: var(--text-faint); font-size: 13px; margin-left: 4px; }

/* ---------- Tableaux (boîtes mail) ---------- */
.table-scroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.data-table { width: 100%; min-width: 640px; border-collapse: collapse; margin-top: 16px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.data-table th, .data-table td {
    text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--border); font-size: 13.5px;
}
.data-table th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; }
.data-table tr:last-child td { border-bottom: none; }
.data-table .actions { display: flex; gap: 6px; }
.ok { color: var(--success-text); font-weight: 600; }
.muted { color: var(--text-faint); }
.error-text { color: var(--danger); font-size: 12px; margin-top: 4px; }

h1 { font-size: 20px; margin: 0 0 16px; color: var(--text); }

/* ---------- Formulaires ---------- */
.form-box {
    max-width: 460px; background: var(--panel); padding: 26px 28px;
    border-radius: var(--radius); border: 1px solid var(--border);
}
.form-box label { display: block; margin: 14px 0 5px; font-size: 12.5px; font-weight: 600; color: var(--text-muted); }
.form-box input {
    width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; font-size: 13.5px;
}
.checkbox-label { display: flex; align-items: center; gap: 8px; font-weight: normal !important; }
.checkbox-label input { width: auto; }
.hint { font-size: 12px; color: var(--text-faint); margin-top: 6px; line-height: 1.5; }

/* ---------- Login ---------- */
.login-box {
    background: var(--panel); padding: 40px 36px; border-radius: 16px;
    text-align: center; width: 100%; max-width: 340px; box-shadow: 0 20px 60px rgba(0,0,0,0.35);
}
.login-mark {
    background: var(--accent); color: #fff; font-weight: 700; font-size: 14px;
    width: 44px; height: 44px; border-radius: 12px; margin: 0 auto 14px;
    display: flex; align-items: center; justify-content: center;
}
.login-box h1 { font-size: 19px; margin: 0 0 4px; }
.login-sub { color: var(--text-muted); font-size: 13px; margin: 0 0 22px; }
.login-box label { display: block; margin: 12px 0 5px; font-size: 12.5px; font-weight: 600; color: var(--text-muted); text-align: left; }
.login-box input {
    width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 13.5px;
}
.login-box .btn { width: 100%; margin-top: 18px; padding: 10px; }

/* ---------- Mots-clés ---------- */
.inline-form { display: flex; gap: 8px; margin-bottom: 18px; max-width: 460px; }
.inline-form input { flex: 1; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; font-size: 13.5px; }

.keyword-list { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.keyword-list li {
    background: var(--panel); border: 1px solid var(--border); border-radius: 20px;
    padding: 6px 8px 6px 14px; display: flex; align-items: center; gap: 8px; font-size: 13.5px;
}

/* ==================== Responsive ==================== */

/* ----- Tablette / petit écran (<= 900px) : sidebar horizontale ----- */
@media (max-width: 900px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: static;
        height: auto;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 10px 14px;
        padding: 12px 16px;
    }
    .sidebar-brand { padding: 0; margin-right: auto; }
    .check-form { order: 2; width: auto; flex: 0 0 auto; }
    .btn-check { width: auto; padding: 9px 14px; }
    .sidebar-nav {
        order: 3;
        flex: 1 1 100%;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 4px;
    }
    .sidebar-nav a { padding: 7px 10px; }
    .sidebar-section {
        order: 4;
        flex: 1 1 100%;
        max-height: 130px;
        margin-top: 0;
        display: flex;
        flex-direction: column;
    }
    .sidebar-logout {
        order: 5;
        margin-left: auto;
        border-top: none;
        padding-top: 8px;
    }

    .content { padding: 20px; }
}

/* ----- Mobile (<= 640px) ----- */
@media (max-width: 640px) {
    body { font-size: 13.5px; }
    .content { padding: 14px; }

    .content-header { flex-direction: column; align-items: stretch; }
    .content-header h1 { margin-bottom: 2px; }

    .search-bar { flex-direction: column; align-items: stretch; }
    .search-bar input[type="text"],
    .search-bar select,
    .search-bar button,
    .search-bar a.btn-small { width: 100%; }

    /* Liste de mails : passe en affichage sur plusieurs lignes,
       comme une vraie appli mail sur mobile */
    .inbox-row {
        flex-wrap: wrap;
        row-gap: 6px;
        padding: 12px 14px;
    }
    .col-sender { width: auto; flex: 1 1 auto; }
    .col-subject {
        order: 3;
        flex: 1 1 100%;
        white-space: normal;
        overflow: visible;
    }
    .subject-text, .snippet-text { display: block; }
    .snippet-text {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }
    .col-meta {
        order: 4;
        flex: 1 1 100%;
        flex-wrap: wrap;
        gap: 6px;
    }
    .date { width: auto; text-align: left; }

    /* Lecteur de mail */
    .mail-reader { padding: 20px 18px; }
    .mail-reader-head h1 { font-size: 17px; }
    .mail-reader-meta { flex-wrap: wrap; }

    /* Formulaires */
    .form-box, .login-box { padding: 22px 18px; }

    /* Pièces jointes : miniatures plus compactes */
    .attachment-image-tile { width: calc(50% - 6px); }

    /* Mots-clés : champ pleine largeur */
    .inline-form { flex-direction: column; }

    /* Sidebar : liste des boîtes plus compacte, nav en pastilles pleine largeur si besoin */
    .sidebar { padding: 10px 12px; }
    .brand-name { font-size: 14px; }
}

/* ----- Très petits écrans (<= 380px) ----- */
@media (max-width: 380px) {
    .avatar { width: 28px; height: 28px; font-size: 12px; }
    .col-sender { font-size: 12.5px; }
    .attachment-image-tile { width: 100%; }
}
