.documents-module .document-nav a {
    display: grid;
    width: 100%;
    grid-template-columns: 30px 1fr;
    align-items: center;
    gap: 8px;
    padding: 10px;
    color: #536277;
    border-radius: 5px;
    text-align: left;
    text-decoration: none;
}

.documents-module .document-nav a:hover,
.documents-module .document-nav a.active {
    color: var(--navy);
    background: #eef3f8;
}

.documents-module .document-nav a > span {
    display: grid;
    width: 28px;
    height: 28px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 5px;
}

.documents-list-panel {
    min-width: 0;
    overflow: visible;
}

.documents-toolbar {
    position: relative;
    z-index: 2;
}

.document-row-main {
    min-width: 0;
}

.document-row-main strong,
.document-row-main p {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.document-row[data-document-open-url]:not([data-document-open-url=""]) {
    cursor: pointer;
}

.document-row[data-document-open-url]:not([data-document-open-url=""]):hover {
    background: #f8fafc;
}

.document-primary-link {
    color: inherit;
    text-decoration: none;
}

.document-primary-link:hover,
.document-primary-link:focus-visible {
    color: var(--orange);
    text-decoration: underline;
}

.document-row .file-icon {
    overflow: hidden;
    padding-inline: 3px;
    font-size: calc(9px * var(--panel-font-scale, 1));
}

.document-row .file-icon.doc {
    background: #426fa5;
}

.document-actions {
    min-width: 28px;
}

.document-action-menu .document-archive-action {
    color: #a7333e;
}

.documents-empty {
    display: grid;
    min-height: 250px;
    place-content: center;
    justify-items: center;
    gap: 8px;
    color: var(--muted);
    text-align: center;
}

.documents-empty span {
    display: grid;
    width: 52px;
    height: 52px;
    place-items: center;
    color: var(--navy);
    background: #eef3f8;
    border-radius: 12px;
    font-size: calc(22px * var(--panel-font-scale, 1));
}

.documents-pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 11px 16px;
    color: var(--muted);
    border-top: 1px solid var(--line);
    font-size: calc(10px * var(--panel-font-scale, 1));
}

.documents-pagination > div {
    display: flex;
    gap: 4px;
}

.documents-pagination a {
    display: grid;
    width: 26px;
    height: 26px;
    place-items: center;
    color: var(--navy);
    border: 1px solid var(--line);
    border-radius: 4px;
    text-decoration: none;
    font-size: calc(11px * var(--panel-font-scale, 1));
    font-weight: 800;
}

.documents-pagination a.active {
    color: #fff;
    background: var(--navy);
    border-color: var(--navy);
}

.document-modal {
    width: min(620px, calc(100vw - 28px));
}

.document-form-body {
    display: grid;
    gap: 14px;
    max-height: min(65vh, 620px);
    overflow-y: auto;
    padding: 18px;
}

.document-error-link { display: block; width: fit-content; margin-top: 8px; color: inherit; font-weight: 800; text-decoration: underline; text-underline-offset: 3px; }
.document-error-link:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; border-radius: 2px; }

.document-form-body label,
.document-template-editor form {
    color: #34465e;
    font-size: calc(11px * var(--panel-font-scale, 1));
    font-weight: 800;
}

.document-source-options {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.document-modal .document-source-option {
    position: relative;
    cursor: pointer;
}

.document-modal .document-source-option input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
}

.document-source-option > span {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    height: 100%;
    min-height: 54px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #f8fafc;
    line-height: 1.4;
}

.document-source-option input:checked + span {
    border-color: var(--orange);
    color: #ad4600;
    background: #fff5ed;
    box-shadow: inset 0 0 0 1px var(--orange);
}

.document-source-option input:focus-visible + span {
    outline: 2px solid var(--navy);
    outline-offset: 3px;
}

.document-source-option svg,
.document-generation-help svg {
    width: 22px;
    height: 22px;
    flex: 0 0 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.document-source-panel {
    display: grid;
    min-width: 0;
    gap: 14px;
    margin: 0;
    padding: 0;
    border: 0;
}

.document-source-panel[hidden],
.document-generation-note[hidden] {
    display: none;
}

.document-generation-help {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 14px;
    color: #345273;
    background: #f0f5fa;
    border: 1px solid #dce6ef;
    border-radius: 7px;
}

.document-generation-help p,
.document-generation-note {
    margin: 0;
    font-size: calc(11px * var(--panel-font-scale, 1));
    line-height: 1.65;
}

.document-generation-note {
    padding: 12px 14px;
    color: #845311;
    background: #fff9ed;
    border-left: 3px solid #e8ae49;
    border-radius: 4px;
}

.document-library-link {
    justify-self: start;
    color: var(--navy);
    font-size: calc(11px * var(--panel-font-scale, 1));
    font-weight: 700;
    text-underline-offset: 3px;
}

.document-modal [aria-busy="true"] [type="submit"] {
    cursor: wait;
    opacity: .65;
}

.document-file-field {
    position: relative;
    display: grid;
    min-height: 112px;
    grid-template-columns: 48px minmax(0, 1fr) 28px;
    align-items: center;
    gap: 14px;
    padding: 18px;
    color: var(--navy);
    background: #fbfcfe;
    border: 2px dashed #bdcad8;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color .18s ease, border-color .18s ease, box-shadow .18s ease, transform .18s ease;
}

.document-modal .document-file-field {
    display: grid;
}

.document-file-field:hover,
.document-file-field:focus-within,
.document-file-field.is-dragover {
    background: #fff8f3;
    border-color: var(--orange);
    box-shadow: 0 0 0 3px rgba(252, 109, 4, .08);
}

.document-file-field.is-dragover {
    transform: translateY(-1px);
}

.document-file-field input[type="file"] {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: 0;
    padding: 0;
    overflow: hidden;
    opacity: 0;
    border: 0;
    pointer-events: none;
}

.document-file-icon {
    display: grid;
    width: 48px;
    height: 48px;
    place-items: center;
    color: var(--orange);
    background: #fff1e7;
    border: 1px solid #f5c7a8;
    border-radius: 12px;
    transition: color .18s ease, background-color .18s ease, border-color .18s ease;
}

.document-file-icon svg {
    width: 23px;
    height: 23px;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.8;
    transform: rotate(180deg);
}

.document-file-copy {
    min-width: 0;
}

.document-file-copy strong,
.document-file-copy small {
    display: block;
}

.document-file-copy strong {
    color: var(--navy);
    font-size: calc(12px * var(--panel-font-scale, 1));
    line-height: 1.4;
}

.document-file-field small {
    margin-top: 4px;
    color: var(--muted);
    font-size: calc(10px * var(--panel-font-scale, 1));
    font-weight: 600;
    line-height: 1.45;
}

.document-file-name {
    overflow: hidden;
    color: #176747 !important;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 850 !important;
}

.document-file-name[hidden] {
    display: none;
}

.document-file-state {
    display: grid;
    width: 28px;
    height: 28px;
    place-items: center;
    color: transparent;
    background: #eef2f6;
    border-radius: 50%;
    font-size: calc(13px * var(--panel-font-scale, 1));
    font-weight: 900;
    transition: color .18s ease, background-color .18s ease, transform .18s ease;
}

.document-file-state::before {
    color: #718096;
    content: "+";
    font-size: calc(18px * var(--panel-font-scale, 1));
    font-weight: 500;
}

.document-file-field.has-selection {
    background: #f4fbf7;
    border-color: #72b394;
    border-style: solid;
    box-shadow: 0 0 0 3px rgba(38, 132, 94, .07);
}

.document-file-field.has-selection .document-file-icon {
    color: #176747;
    background: #e4f5eb;
    border-color: #abd8bf;
}

.document-file-field.has-selection .document-file-formats {
    display: none;
}

.document-file-field.has-selection .document-file-state {
    color: #fff;
    background: var(--green);
    transform: scale(1.04);
}

.document-file-field.has-selection .document-file-state::before {
    display: none;
}

.document-template-layout {
    display: grid;
    grid-template-columns: 210px minmax(0, 1fr) 260px;
    align-items: start;
    gap: 14px;
}

.document-template-nav {
    display: grid;
    gap: 3px;
    padding: 8px;
}

.document-template-nav a {
    display: grid;
    gap: 6px;
    min-width: 0;
    padding: 9px 10px;
    color: #526175;
    border-radius: 5px;
    text-decoration: none;
    font-size: calc(12px * var(--panel-font-scale, 1));
    font-weight: 750;
}

.document-template-nav-name { overflow-wrap: anywhere; line-height: 1.4; }
.document-template-nav-meta { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; }
.document-template-nav-language,
.document-template-nav-default {
    display: inline-flex;
    align-items: center;
    padding: 3px 6px;
    border: 1px solid #dbe3ed;
    border-radius: 4px;
    font-size: calc(10px * var(--panel-font-scale, 1));
    font-weight: 700;
    line-height: 1.3;
}
.document-template-nav-language { color: #526175; background: #fff; }
.document-template-nav-default { color: #0b345f; background: #eaf2fa; border-color: #c8d9ea; text-transform: capitalize; }

.document-template-nav a:hover,
.document-template-nav a.active {
    color: var(--navy);
    background: #eef3f8;
}

.document-template-editor {
    min-width: 0;
    overflow: hidden;
}

.document-template-editor > .panel-head { flex-wrap: wrap; gap: 12px; }
.document-template-preview { flex-shrink: 0; text-decoration: none; }
.document-template-preview svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.document-template-editor form {
    display: grid;
    gap: 12px;
    padding: 16px 18px 18px;
}

.document-template-source {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    min-height: 84px;
    padding: 15px 18px;
    background: #fbfcfe;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
}

.document-template-source strong {
    display: block;
    overflow: hidden;
    color: var(--navy);
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: calc(11px * var(--panel-font-scale, 1));
}

.document-template-source p,
.document-template-publish-form p,
.document-template-token-help {
    margin: 3px 0 0;
    color: var(--muted);
    font-size: calc(10px * var(--panel-font-scale, 1));
    font-weight: 500;
    line-height: 1.45;
}

.document-template-legacy {
    grid-column: 1 / -1;
    padding: 12px 14px;
    background: #fff8ed;
    border: 1px solid #f1d7ab;
    border-radius: 6px;
}

.document-template-file-field {
    min-height: 150px;
}

.document-template-publish-form {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    border-top: 1px solid var(--line);
}

.document-template-publish-form p {
    margin: 0;
}

.document-template-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.document-template-meta {
    display: grid;
    gap: 14px;
}

.document-template-meta article {
    padding: 16px;
}

.document-template-meta h2 {
    margin: 0 0 12px;
    color: var(--navy);
    font-size: calc(13px * var(--panel-font-scale, 1));
}

.document-template-token-help {
    margin: -5px 0 12px;
}

.document-token-list {
    display: grid;
    gap: 7px;
}

.document-token-list code {
    display: block;
    width: 100%;
    padding: 7px 9px;
    overflow-wrap: anywhere;
    color: #0b345f;
    background: #f2f7fc;
    border: 1px solid #c8d9ea;
    border-radius: 5px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    font-size: calc(11px * var(--panel-font-scale, 1));
    font-weight: 700;
    line-height: 1.4;
    white-space: normal;
}

.document-template-history {
    display: grid;
    gap: 8px;
}

.document-template-history div {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    align-items: center;
    gap: 7px;
    color: #526175;
    font-size: calc(10px * var(--panel-font-scale, 1));
}

.document-template-history strong {
    color: var(--navy);
}

.document-template-history i {
    color: var(--green);
    font-style: normal;
    font-weight: 800;
}

.document-template-history-actions { grid-column: 4; }

@media (max-width: 1180px) {
    .documents-module .document-layout {
        grid-template-columns: 1fr;
    }

    .documents-module .document-nav {
        display: flex;
        overflow-x: auto;
        scrollbar-width: thin;
    }

    .documents-module .document-nav a {
        min-width: 165px;
    }

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

    .document-template-meta {
        grid-column: 1 / -1;
        grid-template-columns: 1fr 1fr;
    }
}

@media (max-width: 960px) {
    .document-template-layout {
        grid-template-columns: 1fr;
    }

    .document-template-nav {
        display: flex;
        overflow-x: auto;
    }

    .document-template-nav a {
        flex: 0 0 auto;
    }

    .document-template-meta {
        grid-column: auto;
    }
}

@media (max-width: 640px) {
    .documents-module .page-actions {
        width: 100%;
    }

    .documents-module .page-actions > * {
        flex: 1 1 auto;
        justify-content: center;
    }

    .documents-toolbar .toolbar-actions select {
        min-width: 145px;
    }

    .document-listing {
        overflow: visible;
    }

    .document-listing .document-row {
        min-width: 0;
        grid-template-columns: 42px minmax(0, 1fr) auto;
        gap: 7px 10px;
        padding: 13px;
    }

    .document-row-main {
        grid-column: 2;
    }

    .document-date,
    .document-size {
        grid-row: 2;
        color: var(--muted);
    }

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

    .document-size {
        display: none;
    }

    .document-row > .status {
        grid-row: 3;
        grid-column: 2;
        justify-self: start;
    }

    .document-actions {
        grid-row: 1;
        grid-column: 3;
    }

    .documents-pagination {
        align-items: flex-start;
        flex-direction: column;
    }

    .document-form-body .form-grid,
    .document-template-meta {
        grid-template-columns: 1fr;
    }

    .document-template-source {
        grid-template-columns: auto minmax(0, 1fr) auto;
    }

    .document-template-publish-form {
        grid-template-columns: 1fr;
    }

    .document-template-actions {
        align-items: stretch;
        flex-direction: column-reverse;
    }

    .document-file-field {
        grid-template-columns: 42px minmax(0, 1fr) 26px;
        gap: 11px;
        padding: 15px;
    }

    .document-file-icon {
        width: 42px;
        height: 42px;
        border-radius: 10px;
    }
}

/* Named template variants and email drafts share the library layout. */
.template-create-panel { margin-bottom: 20px; padding: 18px; }
.template-create-panel > summary { width: fit-content; cursor: pointer; }
.template-create-form, .template-variant-settings, .template-email-editor form { display: grid; gap: 16px; margin-top: 18px; }
.template-variant-settings { margin: 0 0 24px; padding-bottom: 24px; border-bottom: 1px solid var(--border, #e3e7e7); }
.template-variant-settings label, .template-create-form label, .template-email-editor label, .template-variant-maintenance label { display: grid; gap: 8px; }
.template-variant-settings input[type="text"], .template-create-form input, .template-create-form select, .template-email-editor input, .template-email-editor textarea, .template-variant-maintenance input { width: 100%; min-width: 0; box-sizing: border-box; padding: 11px 12px; border: 1px solid var(--border, #dce3e3); border-radius: 9px; font: inherit; }
.template-variant-settings .template-default-choice { display: flex; align-items: center; gap: 8px; }
.template-email-editor, .template-variant-maintenance { margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--border, #e3e7e7); }
.template-email-editor { padding-top: 0; }
.template-email-editor summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    min-height: 54px;
    padding: 16px 18px;
    color: var(--navy);
    background: #f7f9fc;
    font-size: calc(14px * var(--panel-font-scale, 1));
    font-weight: 800;
    line-height: 1.4;
    list-style: none;
    cursor: pointer;
}
.template-email-editor summary::-webkit-details-marker { display: none; }
.template-email-editor summary::after {
    content: "";
    flex-shrink: 0;
    width: 9px;
    height: 9px;
    margin-right: 3px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(45deg);
}
.template-email-editor details[open] > summary::after,
.template-email-editor[open] > summary::after { transform: rotate(225deg); }
.template-email-editor summary:hover { background: #eef3f8; }
.template-email-editor summary:focus-visible { outline: 2px solid var(--orange); outline-offset: -3px; }
.template-email-editor[open] > summary { border-bottom: 1px solid var(--line); }
.template-email-editor > form { margin-top: 0; }
.template-email-editor > details { margin: 0 18px 18px; border: 1px solid var(--line); border-radius: 7px; }
.template-email-editor > details > summary { min-height: 48px; padding: 12px 14px; border-radius: 7px; font-size: calc(12px * var(--panel-font-scale, 1)); }
.template-email-editor > details[open] > summary { border-bottom: 1px solid var(--line); border-radius: 7px 7px 0 0; }
.template-email-editor > details > .document-token-list { padding: 14px; }
.template-email-editor textarea { resize: vertical; }
.template-email-preview { padding: 16px; margin: 20px 0; background: var(--surface-alt, #f4f7f6); border-radius: 10px; }
.template-email-preview pre { white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; }
.template-variant-maintenance { display: grid; gap: 18px; }
.template-variant-maintenance form { display: grid; gap: 12px; }
@media (max-width: 700px) { .template-create-form .form-grid { grid-template-columns: 1fr; } .document-template-actions { flex-wrap: wrap; } }
