:root {
    --bg: #f4f7fb;
    --panel: #ffffff;
    --text: #182238;
    --muted: #667085;
    --border: #dfe5ee;
    --border-strong: #c9d2df;
    --primary: #075dcc;
    --primary-dark: #064da9;
    --header: #08225a;
    --soft: #eef4ff;
    --ok: #187a37;
    --ok-bg: #eaf8ee;
    --danger: #b42318;
    --danger-bg: #fff0f0;
    --warning: #a15c00;
    --formula: #5b3db5;
    --formula-bg: #f7f3ff;
}

* { box-sizing: border-box; }

html,
body {
    margin: 0;
    min-height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

body { line-height: 1.45; }
a { color: var(--primary); }

code {
    padding: 1px 4px;
    border-radius: 4px;
    background: #eef2f7;
    color: #344054;
}

.container {
    width: min(1120px, calc(100% - 32px));
    margin: 0 auto;
}

.topbar {
    background: var(--panel);
    border-bottom: 1px solid var(--border);
}

.topbar-inner {
    min-height: 104px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
}

.topbar h1 {
    margin: 2px 0 0;
    font-size: clamp(26px, 4vw, 36px);
    color: var(--header);
}

.topbar-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 10px;
}

.version,
.save-status {
    display: inline-flex;
    align-items: center;
    min-height: 32px;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
}

.version {
    background: var(--soft);
    color: var(--primary);
}

.save-status { gap: 7px; }

.save-status .save-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
}

.save-status.saved {
    color: var(--ok);
    background: var(--ok-bg);
}

.save-status.saving {
    color: var(--warning);
    background: #fff7e8;
}

.save-status.error {
    color: var(--danger);
    background: var(--danger-bg);
}

.save-status.saving .save-dot {
    animation: pulse 1s infinite ease-in-out;
}

@keyframes pulse {
    0%, 100% { opacity: .35; }
    50% { opacity: 1; }
}

.back-link {
    font-size: 13px;
    font-weight: 700;
    text-decoration: none;
}

.table-page {
    padding-bottom: 40px;
}

.table-toolbar {
    display: flex;
    justify-content: space-between;
    padding-top: 18px;
    padding-bottom: 10px;
}

.table-toolbar > div {
    display: grid;
    gap: 3px;
}

.table-toolbar span {
    color: var(--muted);
    font-size: 13px;
}

.formula-bar {
    display: grid;
    grid-template-columns: 72px minmax(0, 1fr);
    margin-bottom: 10px;
    overflow: hidden;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    background: #fff;
}

.formula-cell-ref {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    border-right: 1px solid var(--border);
    background: #f1f4f8;
    color: #475467;
    font-weight: 800;
}

.formula-preview {
    display: flex;
    align-items: center;
    min-width: 0;
    min-height: 36px;
    padding: 6px 10px;
    overflow: hidden;
    color: #344054;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 13px;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.sheet-wrap {
    width: calc(100% - 32px);
    max-width: 1600px;
    margin: 0 auto;
    overflow: auto;
    max-height: calc(100vh - 245px);
    background: #fff;
    border: 1px solid var(--border-strong);
    border-radius: 10px;
}

.sheet {
    border-collapse: separate;
    border-spacing: 0;
    width: max-content;
    min-width: 100%;
    font-size: 14px;
}

.sheet th,
.sheet td {
    height: 36px;
    border-right: 1px solid #dfe3e8;
    border-bottom: 1px solid #dfe3e8;
}

.sheet thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    padding: 0 10px;
    background: #f1f4f8;
    text-align: center;
    font-weight: 750;
    color: #475467;
}

.sheet .corner {
    position: sticky;
    left: 0;
    z-index: 4;
    width: 48px;
    min-width: 48px;
    background: #e9eef5;
}

.sheet .row-number {
    position: sticky;
    left: 0;
    z-index: 1;
    width: 48px;
    min-width: 48px;
    background: #f1f4f8;
    text-align: center;
    font-weight: 650;
    color: #667085;
}

.sheet .cell {
    min-width: 160px;
    padding: 0;
    background: #fff;
    position: relative;
}

.cell-input {
    display: block;
    width: 100%;
    height: 35px;
    padding: 6px 8px;
    border: 0;
    border-radius: 0;
    outline: 0;
    background: transparent;
    color: var(--text);
    font: inherit;
}

.cell-input:focus {
    position: relative;
    z-index: 1;
    box-shadow: inset 0 0 0 2px var(--primary);
    background: #fff;
}

.cell-input.formula-cell:not(:focus) {
    background: var(--formula-bg);
    color: var(--formula);
    font-weight: 650;
}

.cell-input.formula-error:not(:focus) {
    background: var(--danger-bg);
    color: var(--danger);
}

.cell-input.is-saving {
    background: #fffaf0;
}

.cell-input.just-saved {
    background: #f1fbf4;
}

.cell-input.save-error {
    background: var(--danger-bg);
    box-shadow: inset 0 0 0 2px var(--danger);
}

.sheet tr:last-child th,
.sheet tr:last-child td {
    border-bottom: 0;
}

.sheet tr th:last-child,
.sheet tr td:last-child {
    border-right: 0;
}

/* Styles de la page d'accueil V1/V2 conservés */
.main-grid {
    display: grid;
    gap: 22px;
    padding: 28px 0 50px;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 22px;
    box-shadow: 0 3px 12px rgba(16, 24, 40, .04);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
}

.panel h1,
.panel h2 { color: var(--header); }

.panel h2 {
    margin: 0 0 5px;
    font-size: 21px;
}

.panel p {
    margin: 0;
    color: var(--muted);
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}

.field {
    display: grid;
    gap: 7px;
}

.field-wide { grid-column: 1 / -1; }

.field span {
    font-weight: 700;
    font-size: 14px;
}

.field input {
    width: 100%;
    min-height: 46px;
    padding: 10px 12px;
    border: 1px solid var(--border-strong);
    border-radius: 9px;
    background: #fff;
    color: var(--text);
    font: inherit;
    outline: none;
}

.field input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(7, 93, 204, .12);
}

.form-actions { padding-top: 4px; }

.button {
    display: inline-flex;
    min-height: 44px;
    align-items: center;
    justify-content: center;
    padding: 9px 16px;
    border: 1px solid var(--border-strong);
    border-radius: 9px;
    background: #fff;
    color: var(--text);
    text-decoration: none;
    font: inherit;
    font-weight: 750;
    cursor: pointer;
}

.button.primary {
    border-color: var(--primary);
    background: var(--primary);
    color: #fff;
}

.empty-state {
    min-height: 110px;
    display: grid;
    place-content: center;
    text-align: center;
    gap: 5px;
    border: 1px dashed var(--border-strong);
    border-radius: 10px;
    color: var(--muted);
}

.ged-list {
    display: grid;
    gap: 10px;
}

.ged-card {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 18px;
    padding: 15px 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    text-decoration: none;
}

.ged-card > div:first-child {
    display: grid;
    gap: 3px;
}

.ged-card span,
.ged-card small { color: var(--muted); }

.ged-card-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.arrow { font-size: 22px; }

@media (max-width: 850px) {
    .topbar-inner {
        align-items: flex-start;
        padding-top: 18px;
        padding-bottom: 18px;
    }

    .topbar-actions {
        align-items: flex-end;
        flex-direction: column-reverse;
    }
}

@media (max-width: 700px) {
    .save-status { display: none; }

    .form-grid { grid-template-columns: 1fr; }
    .field-wide { grid-column: auto; }

    .formula-bar {
        grid-template-columns: 54px minmax(0, 1fr);
    }
}


/* ============================================================
   V4 — Documents PDF associés aux cellules
   ============================================================ */

.formula-bar {
    grid-template-columns: 72px minmax(0, 1fr) auto;
}

.document-add-button {
    min-height: 36px;
    padding: 6px 13px;
    border: 0;
    border-left: 1px solid var(--border);
    background: var(--primary);
    color: #fff;
    font: inherit;
    font-size: 13px;
    font-weight: 750;
    cursor: pointer;
    white-space: nowrap;
}

.document-add-button:hover:not(:disabled) {
    background: var(--primary-dark);
}

.document-add-button:disabled {
    cursor: not-allowed;
    opacity: .55;
}

.documents-panel {
    margin-bottom: 10px;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: #fff;
}

.documents-panel-heading {
    min-height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
    padding: 9px 12px;
    border-bottom: 1px solid var(--border);
    background: #f8fafc;
}

.documents-panel-heading > div {
    display: grid;
    gap: 2px;
}

.documents-panel-heading strong {
    color: var(--header);
    font-size: 13px;
}

.documents-panel-heading span {
    color: var(--muted);
    font-size: 12px;
}

.documents-count {
    flex: 0 0 auto;
    padding: 4px 8px;
    border-radius: 999px;
    background: var(--soft);
    color: var(--primary) !important;
    font-weight: 750;
}

.documents-list {
    max-height: 185px;
    overflow: auto;
}

.documents-empty {
    padding: 13px 12px;
    color: var(--muted);
    font-size: 13px;
}

.document-item {
    display: grid;
    grid-template-columns: 38px minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    border-bottom: 1px solid #edf0f4;
}

.document-item:last-child {
    border-bottom: 0;
}

.document-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 32px;
    border-radius: 6px;
    background: #fff0f0;
    color: var(--danger);
    font-size: 10px;
    font-weight: 900;
}

.document-meta {
    display: grid;
    min-width: 0;
    gap: 2px;
}

.document-name {
    overflow: hidden;
    color: var(--text);
    font-size: 13px;
    font-weight: 700;
    text-decoration: none;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.document-name:hover {
    color: var(--primary);
    text-decoration: underline;
}

.document-meta span {
    color: var(--muted);
    font-size: 11px;
}

.document-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.document-open,
.document-delete {
    min-height: 30px;
    display: inline-flex;
    align-items: center;
    padding: 5px 9px;
    border-radius: 6px;
    font: inherit;
    font-size: 12px;
    font-weight: 700;
    text-decoration: none;
}

.document-open {
    border: 1px solid var(--border-strong);
    background: #fff;
    color: var(--primary);
}

.document-delete {
    border: 1px solid #f0b9b5;
    background: #fff;
    color: var(--danger);
    cursor: pointer;
}

.cell.has-documents {
    padding-right: 31px;
}

.cell-document-badge {
    position: absolute;
    top: 50%;
    right: 4px;
    z-index: 3;
    min-width: 25px;
    height: 25px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 1px;
    padding: 0 4px;
    transform: translateY(-50%);
    border: 1px solid #c7d7f4;
    border-radius: 999px;
    background: #eef4ff;
    color: var(--primary);
    font-size: 11px;
    font-weight: 800;
    cursor: pointer;
}

.cell-document-badge span {
    font-size: 10px;
}

.cell-document-badge.is-hidden {
    display: none;
}

.cell.has-documents .cell-input {
    padding-right: 35px;
}

@media (max-width: 700px) {
    .formula-bar {
        grid-template-columns: 54px minmax(0, 1fr);
    }

    .document-add-button {
        grid-column: 1 / -1;
        border-top: 1px solid var(--border);
        border-left: 0;
    }

    .document-item {
        grid-template-columns: 34px minmax(0, 1fr);
    }

    .document-actions {
        grid-column: 2;
    }
}


/* ============================================================
   V5 — Analyse du texte des PDF
   ============================================================ */

.document-analyze,
.document-open,
.document-delete {
    min-height: 30px;
}

.document-analyze {
    display: inline-flex;
    align-items: center;
    padding: 5px 9px;
    border: 1px solid #b9cbea;
    border-radius: 6px;
    background: #eef4ff;
    color: var(--primary);
    font: inherit;
    font-size: 12px;
    font-weight: 750;
    cursor: pointer;
}

.document-analyze:hover {
    border-color: var(--primary);
    background: #e3edff;
}

.pdf-analysis-panel {
    margin-bottom: 10px;
    overflow: hidden;
    border: 1px solid #c8d7ef;
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 3px 12px rgba(16, 24, 40, .04);
}

.pdf-analysis-panel.is-hidden,
.analysis-warning.is-hidden {
    display: none;
}

.pdf-analysis-heading {
    min-height: 54px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    background: #f6f9ff;
}

.pdf-analysis-heading > div {
    display: grid;
    min-width: 0;
    gap: 2px;
}

.pdf-analysis-heading strong {
    overflow: hidden;
    color: var(--header);
    font-size: 13px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pdf-analysis-heading span {
    color: var(--muted);
    font-size: 12px;
}

.analysis-close {
    width: 32px;
    height: 32px;
    flex: 0 0 auto;
    border: 1px solid var(--border-strong);
    border-radius: 7px;
    background: #fff;
    color: var(--muted);
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
}

.analysis-warning {
    padding: 9px 12px;
    border-bottom: 1px solid #efd7a3;
    background: #fff8e8;
    color: #795600;
    font-size: 12px;
}

.analysis-toolbar {
    display: grid;
    grid-template-columns: minmax(240px, 1fr) auto;
    align-items: end;
    gap: 12px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
}

.analysis-search {
    display: grid;
    gap: 4px;
}

.analysis-search span {
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
}

.analysis-search input {
    width: 100%;
    min-height: 34px;
    padding: 6px 9px;
    border: 1px solid var(--border-strong);
    border-radius: 7px;
    background: #fff;
    color: var(--text);
    font: inherit;
    font-size: 13px;
    outline: none;
}

.analysis-search input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(7, 93, 204, .1);
}

.analysis-target {
    min-height: 34px;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 10px;
    border-radius: 7px;
    background: var(--soft);
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
}

.analysis-target strong {
    color: var(--primary);
}

.analysis-text {
    display: block;
    width: 100%;
    height: 270px;
    resize: vertical;
    padding: 12px;
    border: 0;
    border-bottom: 1px solid var(--border);
    background: #fbfcfe;
    color: #263247;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 12px;
    line-height: 1.55;
    outline: none;
    white-space: pre-wrap;
}

.analysis-text:focus {
    background: #fff;
    box-shadow: inset 0 0 0 2px rgba(7, 93, 204, .15);
}

.analysis-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 12px;
}

.analysis-button {
    min-height: 34px;
    padding: 6px 11px;
    border: 1px solid var(--border-strong);
    border-radius: 7px;
    background: #fff;
    color: var(--text);
    font: inherit;
    font-size: 12px;
    font-weight: 750;
    cursor: pointer;
}

.analysis-button.primary {
    border-color: var(--primary);
    background: var(--primary);
    color: #fff;
}

.analysis-button:disabled {
    cursor: not-allowed;
    opacity: .5;
}

.analysis-note {
    padding: 0 12px 11px;
    color: var(--muted);
    font-size: 11px;
}

@media (max-width: 850px) {
    .document-actions {
        flex-wrap: wrap;
        justify-content: flex-end;
    }
}

@media (max-width: 700px) {
    .analysis-toolbar {
        grid-template-columns: 1fr;
    }

    .analysis-target {
        justify-content: center;
    }

    .analysis-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .analysis-button {
        width: 100%;
    }

    .analysis-text {
        height: 220px;
    }
}
