:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --bg: #f1f5f9;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --muted: #64748b;
    --code-bg: #1e1e2e;
    --code-fg: #cdd6f4;
    --danger: #ef4444;
    --success: #10b981;
}
* { margin: 0; padding: 0; box-sizing: border-box; }

/* ── SKIP LINK ── */
.skip-link {
    position: absolute;
    top: -100%;
    left: 8px;
    background: var(--primary);
    color: white;
    padding: 8px 16px;
    border-radius: 0 0 6px 6px;
    font-size: .85rem;
    font-weight: 600;
    text-decoration: none;
    z-index: 9999;
    transition: top .1s;
}
.skip-link:focus { top: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ── HEADER ── */
.header {
    background: var(--primary-dark);
    color: white;
    padding: 10px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}
.header h1 { font-size: 1.05rem; font-weight: 700; }
.header-right { display: flex; gap: 8px; align-items: center; }
.header input[type="text"] {
    padding: 5px 10px;
    border-radius: 6px;
    border: 1px solid rgba(255,255,255,0.35);
    background: rgba(255,255,255,0.15);
    color: white;
    font-size: 0.82rem;
    outline: none;
    width: 170px;
}
.header input::placeholder { color: rgba(255,255,255,0.55); }
.header-right label {
    font-size: 0.62rem;
    font-weight: 600;
    color: white;
    text-transform: uppercase;
    letter-spacing: .07em;
    white-space: nowrap;
}
.btn-dl {
    padding: 6px 14px;
    border-radius: 6px;
    border: 1px solid white;
    background: white;
    color: var(--primary-dark);
    font-size: 0.82rem;
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
    transition: background .15s;
}
.btn-dl:hover { background: rgba(255,255,255,0.88); }
.btn-dl-pdf {
    background: #b91c1c;
    border-color: #b91c1c;
    color: white;
}
.btn-dl-pdf:hover { background: #991b1b; }

/* ── TOOLBAR ── */
.toolbar {
    background: var(--surface);
    border-bottom: 2px solid var(--border);
    padding: 7px 14px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
    flex-shrink: 0;
}
.grp-label {
    font-size: 0.62rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .07em;
    font-weight: 600;
    margin-right: 1px;
}
.tsep { width: 1px; height: 28px; background: var(--border); margin: 0 6px; }
.tbtn {
    padding: 5px 9px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.78rem;
    font-family: inherit;
    font-weight: 500;
    color: var(--text);
    transition: all .12s;
    white-space: nowrap;
    line-height: 1.4;
}
.tbtn:hover {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
    transform: translateY(-1px);
    box-shadow: 0 2px 6px rgba(99,102,241,.3);
}

/* ── MAIN ── */
.main {
    flex: 1;
    display: flex;
    overflow: hidden;
    min-height: 0;
}
.panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}
.panel:first-child { border-right: 2px solid var(--border); }

.panel-bar {
    padding: 7px 14px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .07em;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}
.panel-bar-actions { display: flex; gap: 5px; }
.pbtn {
    padding: 3px 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
    font-family: inherit;
    color: var(--muted);
    transition: all .12s;
}
.pbtn:hover { background: var(--primary); color: white; border-color: var(--primary); }
.pbtn.danger:hover { background: var(--danger); border-color: var(--danger); }

/* ── FOCUS VISIBLE ── */
.tbtn:focus-visible,
.pbtn:focus-visible,
.btn:focus-visible,
.btn-dl:focus-visible,
.tbl-controls button:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
    box-shadow: 0 0 0 4px rgba(99,102,241,.2);
}

#editor {
    flex: 1;
    border: none;
    padding: 16px;
    font-family: 'Consolas', 'JetBrains Mono', monospace;
    font-size: .875rem;
    line-height: 1.65;
    resize: none;
    background: var(--code-bg);
    color: var(--code-fg);
    outline: none;
    tab-size: 2;
    min-height: 0;
}
#editor::placeholder { color: #6c7086; }

.preview-body {
    flex: 1;
    overflow-y: auto;
    padding: 24px 32px;
    background: var(--surface);
    min-height: 0;
    font-size: .92rem;
    line-height: 1.75;
}

/* ── PREVIEW MARKDOWN STYLES ── */
.preview-body h1,.preview-body h2,.preview-body h3,
.preview-body h4,.preview-body h5,.preview-body h6 {
    margin: 1.1rem 0 .4rem; line-height: 1.3; color: var(--text);
}
.preview-body h1 { font-size: 1.75rem; border-bottom: 2px solid var(--border); padding-bottom: .3rem; }
.preview-body h2 { font-size: 1.35rem; border-bottom: 1px solid var(--border); padding-bottom: .2rem; }
.preview-body h3 { font-size: 1.1rem; }
.preview-body h4 { font-size: 1rem; }
.preview-body p { margin: .65rem 0; }
.preview-body a { color: var(--primary-dark); text-decoration: none; }
.preview-body a:hover { text-decoration: underline; }
.preview-body strong { font-weight: 700; }
.preview-body em { font-style: italic; }
.preview-body del { text-decoration: line-through; opacity: .65; }
.preview-body blockquote {
    border-left: 4px solid var(--primary);
    margin: 1rem 0;
    padding: 8px 16px;
    background: #eef2ff;
    border-radius: 0 6px 6px 0;
    color: #4b5563;
}
.preview-body code {
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: monospace;
    font-size: .875em;
    color: #be185d;
}
.preview-body pre {
    background: var(--code-bg);
    color: var(--code-fg);
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 1rem 0;
}
.preview-body pre code { background: none; color: inherit; padding: 0; font-size: .875rem; }
.preview-body ul,.preview-body ol { padding-left: 1.5rem; margin: .65rem 0; }
.preview-body li { margin: .2rem 0; }
.preview-body hr { border: none; border-top: 2px solid var(--border); margin: 1.5rem 0; }
.preview-body table { width: 100%; border-collapse: collapse; margin: 1rem 0; font-size: .88rem; }
.preview-body th { background: var(--bg); font-weight: 600; padding: 8px 12px; border: 1px solid var(--border); text-align: left; }
.preview-body td { padding: 8px 12px; border: 1px solid var(--border); }
.preview-body tr:nth-child(even) td { background: var(--bg); }
.preview-body img { max-width: 100%; border-radius: 6px; margin: .5rem 0; }

/* ── PAGEBREAK MARKER ── */
.preview-body .pagebreak-marker {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 1.2rem 0;
    padding: 6px 0;
    border: 2px dashed #ef4444;
    border-radius: 6px;
    color: #ef4444;
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .06em;
    opacity: .7;
}

.empty-state {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center; height: 100%;
    color: var(--muted); text-align: center; gap: 10px;
}
.empty-state .ei { font-size: 2.5rem; opacity: .4; }
.empty-state p { font-size: .88rem; line-height: 1.6; }

/* ── PREVIEW: CLICK-TO-EDIT ── */
.preview-body [data-editable] {
    cursor: pointer;
    border-radius: 3px;
    transition: outline .1s;
    position: relative;
}
.preview-body [data-editable]:hover {
    outline: 2px dashed rgba(99,102,241,.5);
    outline-offset: 3px;
}
.edit-hint {
    position: fixed;
    bottom: 86px;
    right: 20px;
    background: var(--primary-dark);
    color: white;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: .75rem;
    pointer-events: none;
    opacity: 0;
    transition: opacity .2s;
    z-index: 50;
}
.edit-hint.on { opacity: 1; }

/* ── MODAL ── */
.overlay {
    display: none; position: fixed;
    inset: 0; background: rgba(0,0,0,.48);
    z-index: 100; align-items: center; justify-content: center;
}
.overlay.on { display: flex; }
.modal {
    background: var(--surface);
    border-radius: 12px;
    padding: 22px 24px;
    min-width: 400px;
    max-width: 580px;
    width: 90%;
    box-shadow: 0 24px 64px rgba(0,0,0,.3);
    animation: popIn .16s ease;
}
@keyframes popIn {
    from { transform: scale(.92) translateY(6px); opacity: 0; }
    to   { transform: scale(1) translateY(0);     opacity: 1; }
}
.modal-title {
    font-size: .95rem; font-weight: 700;
    margin-bottom: 16px; color: var(--text);
}
.modal.edit-mode .modal-title::before {
    content: '✎ ';
    color: var(--primary);
}
.fg { margin-bottom: 13px; }
.fg label {
    display: block; font-size: .72rem; font-weight: 700;
    margin-bottom: 4px; color: var(--muted);
    text-transform: uppercase; letter-spacing: .05em;
}
.fg input[type="text"], .fg input[type="url"], .fg select, .fg textarea {
    width: 100%; padding: 7px 10px;
    border: 1px solid var(--border); border-radius: 6px;
    font-size: .85rem; font-family: inherit; color: var(--text);
    outline: none; transition: border-color .15s; background: var(--surface);
}
.fg input:focus, .fg select:focus, .fg textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}
.fg textarea { resize: vertical; min-height: 80px; }
.fg textarea.ta-tall { min-height: 110px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.hint { font-size: .72rem; color: var(--muted); margin-top: 3px; }
.frow { display: flex; gap: 10px; }
.frow .fg { flex: 1; }
.modal-actions {
    display: flex; gap: 8px;
    justify-content: flex-end; margin-top: 18px;
}
.btn { padding: 7px 18px; border-radius: 7px; border: 1px solid var(--border); cursor: pointer; font-size: .85rem; font-family: inherit; font-weight: 500; transition: all .12s; }
.btn-ok { background: var(--primary); color: white; border-color: var(--primary); }
.btn-ok:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-cancel { background: var(--surface); color: var(--muted); }
.btn-cancel:hover { background: var(--bg); }

/* ── TABLE BUILDER ── */
.tbl-builder { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.tbl-builder table { width: 100%; border-collapse: collapse; }
.tbl-builder th, .tbl-builder td { padding: 4px; border: 1px solid var(--border); }
.tbl-builder th { background: var(--bg); }
.tbl-builder input[type="text"] {
    width: 100%; border: none; padding: 4px 6px;
    font-size: .8rem; font-family: inherit; outline: none; background: transparent;
}
.tbl-builder th input { font-weight: 600; }
.tbl-builder select {
    width: 100%; border: none; font-size: .72rem;
    background: var(--bg); padding: 2px 4px; font-family: inherit; outline: none; cursor: pointer;
}
.tbl-controls { display: flex; gap: 6px; padding: 8px; background: var(--bg); border-top: 1px solid var(--border); }
.tbl-controls button {
    padding: 4px 10px; font-size: .75rem;
    border: 1px solid var(--border); border-radius: 4px;
    cursor: pointer; background: var(--surface); font-family: inherit; transition: all .12s;
}
.tbl-controls button:hover { background: var(--primary); color: white; border-color: var(--primary); }
.tbl-controls button.del:hover { background: var(--danger); border-color: var(--danger); }

/* ── FOOTER ── */
.footer {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 5px 20px;
    font-size: 0.7rem;
    color: var(--muted);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-shrink: 0;
}
.footer a, .footer-link {
    color: var(--primary-dark);
    text-decoration: underline;
    background: none;
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-size: inherit;
    padding: 0;
}
.footer a:hover, .footer-link:hover { text-decoration: underline; }
.footer a:focus-visible, .footer-link:focus-visible {
    outline: 2px solid var(--primary-dark);
    outline-offset: 2px;
    border-radius: 2px;
}

/* ── MODAL MENTIONS LÉGALES ── */
.legal-modal { max-width: 600px; }
.legal-body { max-height: 60vh; overflow-y: auto; padding-right: 4px; }
.legal-body h2 {
    font-size: .82rem;
    font-weight: 700;
    color: var(--text);
    text-transform: uppercase;
    letter-spacing: .05em;
    margin: 14px 0 4px;
}
.legal-body h2:first-child { margin-top: 0; }
.legal-body p { font-size: .85rem; line-height: 1.6; color: var(--muted); }
.legal-body a { color: var(--primary-dark); }

/* ── TOAST ── */
.toast {
    position: fixed; bottom: 48px; right: 20px;
    background: var(--success); color: white;
    padding: 9px 16px; border-radius: 8px; font-size: .85rem;
    box-shadow: 0 4px 14px rgba(0,0,0,.2);
    z-index: 200; display: none;
    animation: toastIn .2s ease;
}
@keyframes toastIn {
    from { transform: translateY(8px); opacity: 0; }
    to   { transform: translateY(0);   opacity: 1; }
}
@keyframes shake {
    0%,100% { transform: translateX(0); }
    25%     { transform: translateX(-8px); }
    75%     { transform: translateX(8px); }
}

/* ── PRÉFÉRENCE : RÉDUCTION DES ANIMATIONS ── */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ── RESPONSIVE — tablette ── */
@media (max-width: 900px) {
    .header input[type="text"] { width: 120px; }
}

/* ── ONGLETS MOBILES (cachés sur desktop) ── */
.mobile-tabs { display: none; }

/* ── RESPONSIVE — mobile ── */
@media (max-width: 640px) {
    .header { flex-wrap: wrap; gap: 6px; padding: 8px 14px; }
    .header h1 { font-size: .9rem; width: 100%; }
    .header-right { width: 100%; justify-content: flex-end; }
    .header-right label { display: none; }
    .header input[type="text"] { width: 130px; }

    /* Onglets */
    .mobile-tabs {
        display: flex;
        background: var(--surface);
        border-bottom: 2px solid var(--border);
        flex-shrink: 0;
    }
    .mtab {
        flex: 1;
        padding: 10px;
        border: none;
        background: none;
        font-size: .82rem;
        font-family: inherit;
        font-weight: 500;
        color: var(--muted);
        cursor: pointer;
        border-bottom: 2px solid transparent;
        margin-bottom: -2px;
        transition: all .15s;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
    }
    .mtab.active {
        color: var(--primary);
        border-bottom-color: var(--primary);
        font-weight: 700;
    }
    .mtab:focus-visible {
        outline: 2px solid var(--primary);
        outline-offset: -2px;
    }

    /* Panneaux : un seul visible à la fois, pleine hauteur */
    .main { flex-direction: column; }
    .panel { display: none; flex: 1; }
    .panel.tab-active { display: flex; }
    .panel:first-child { border-right: none; }

    .modal { min-width: unset; width: 95%; padding: 16px; }
    .modal-actions { flex-direction: column-reverse; }
    .modal-actions .btn { width: 100%; text-align: center; }

    .edit-hint { bottom: 48px; right: 12px; }
    .toast { bottom: 48px; right: 12px; }
}
