:root {
    --brand: #2962ff;
    --brand-strong: #1748d1;
    --surface: #ffffff;
    --surface-secondary: #f5f7fb;
    --surface-muted: #edf1f7;
    --border: #d9e0ea;
    --text: #172033;
    --text-muted: #647087;
    --header: #12203a;
    --shadow: 0 8px 24px rgba(25, 41, 72, 0.09);
}

html[data-theme="dark"] {
    --surface: #1c1f24;
    --surface-secondary: #14171b;
    --surface-muted: #252a31;
    --border: #353b45;
    --text: #f0f3f8;
    --text-muted: #aeb7c5;
    --header: #0d1014;
    --shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
}

body {
    margin: 0;
    background: var(--surface-secondary);
    color: var(--text);
    font-family: Inter, "Segoe UI", Arial, sans-serif;
}

button,
input,
select,
textarea {
    font: inherit;
}

.app-header {
    min-height: 64px;
    padding: 10px clamp(18px, 4vw, 44px);
    display: flex;
    align-items: center;
    background: var(--header);
    border-bottom: 3px solid var(--brand);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    color: #ffffff;
    text-decoration: none;
}

.brand-logo {
    width: 78px;
    height: 42px;
    object-fit: contain;
}

.brand span {
    display: grid;
    line-height: 1.08;
}

.brand strong {
    font-size: 18px;
    letter-spacing: 0.01em;
}

.brand sup {
    font-size: 9px;
}

.brand small {
    margin-top: 4px;
    color: #aebbd0;
    font-size: 12px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.app-body {
    width: min(1600px, 100%);
    margin: 0 auto;
    padding: 24px clamp(16px, 3vw, 36px) 18px;
}

.page-heading {
    margin-bottom: 20px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 20px;
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--brand);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.page-heading h1 {
    margin: 0;
    font-size: clamp(25px, 3vw, 35px);
    font-weight: 650;
}

.page-subtitle {
    margin: 7px 0 0;
    color: var(--text-muted);
}

.workspace {
    min-height: calc(100vh - 235px);
    display: grid;
    grid-template-columns: minmax(270px, 330px) minmax(0, 1fr);
    gap: 18px;
}

.companies-panel,
.mail-panel {
    min-width: 0;
}

.companies-panel {
    padding: 18px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.panel-heading {
    min-height: 48px;
    margin-bottom: 14px;
    padding: 0 4px 12px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
}

.panel-heading h2 {
    margin: 0 0 4px;
    font-size: 18px;
}

.panel-count {
    color: var(--text-muted);
    font-size: 12px;
}

#companiesList {
    height: calc(100vh - 310px);
    min-height: 380px;
}

.company-item {
    min-height: 68px;
    padding: 5px 2px;
    display: grid;
    align-content: center;
    gap: 3px;
}

.company-item strong {
    overflow: hidden;
    color: var(--text);
    font-size: 14px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.company-item span {
    color: var(--brand);
    font-size: 12px;
    font-weight: 700;
}

.company-item small {
    color: var(--text-muted);
    font-size: 11px;
}

.mail-panel {
    display: grid;
    grid-template-rows: auto auto auto minmax(0, 1fr);
    gap: 14px;
}

.selection-summary,
.filters-panel,
.grid-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.selection-summary {
    min-height: 84px;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
}

.selection-summary > div:first-child {
    min-width: 0;
    display: grid;
    gap: 3px;
}

.summary-label,
.summary-total span {
    color: var(--text-muted);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

#selectedCompanyName {
    overflow: hidden;
    font-size: 19px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.summary-meta {
    color: var(--text-muted);
    font-size: 13px;
}

.summary-total {
    min-width: 145px;
    padding-left: 20px;
    display: grid;
    gap: 3px;
    text-align: right;
    border-left: 1px solid var(--border);
}

.summary-total strong {
    color: var(--brand);
    font-size: 28px;
}

.trace-session-panel {
    min-height: 58px;
    padding: 10px 14px;
    display: grid;
    grid-template-columns: 12px minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 4px solid #7b8494;
    box-shadow: var(--shadow);
}

.trace-session-panel.is-ready {
    border-left-color: #2e9d55;
}

.trace-session-panel.is-error {
    border-left-color: #c83a3a;
}

.trace-session-panel.is-loading {
    border-left-color: #d58b16;
}

.trace-session-indicator {
    width: 10px;
    height: 10px;
    background: #7b8494;
}

.trace-session-panel.is-ready .trace-session-indicator {
    background: #2e9d55;
}

.trace-session-panel.is-error .trace-session-indicator {
    background: #c83a3a;
}

.trace-session-panel.is-loading .trace-session-indicator {
    background: #d58b16;
    animation: trace-session-pulse 1s ease-in-out infinite alternate;
}

.trace-session-content {
    min-width: 0;
    display: grid;
    gap: 2px;
}

.trace-session-content strong {
    color: var(--text);
    font-size: 13px;
}

.trace-session-content span {
    overflow: hidden;
    color: var(--text-muted);
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

@keyframes trace-session-pulse {
    from { opacity: 0.35; }
    to { opacity: 1; }
}

.filters-panel {
    padding: 14px 18px;
    display: flex;
    align-items: flex-end;
    gap: 14px;
    flex-wrap: wrap;
}

.filter-field {
    display: grid;
    gap: 6px;
}

.filter-field label {
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
}

.filter-type {
    width: 170px;
}

.filter-date {
    width: 190px;
}

.filter-actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 10px;
}

.is-hidden {
    display: none !important;
}

.grid-panel {
    min-height: 520px;
    padding: 10px;
    overflow: hidden;
}

#mailsGrid {
    height: calc(100vh - 390px);
    min-height: 500px;
}

.grid-hint {
    color: var(--text-muted);
    font-size: 12px;
}

.loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(12, 18, 29, 0.42);
    backdrop-filter: blur(2px);
}

.loading-overlay.is-visible {
    display: flex;
}

.loading-box {
    min-width: 180px;
    padding: 22px 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 13px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    font-weight: 600;
}

.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--surface-muted);
    border-top-color: var(--brand);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.mail-detail {
    padding: 8px 4px 18px;
    display: grid;
}

.detail-row {
    padding: 13px 5px;
    display: grid;
    grid-template-columns: 145px minmax(0, 1fr);
    gap: 16px;
    border-bottom: 1px solid var(--border);
}

.detail-row span {
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
}

.detail-row strong {
    overflow-wrap: anywhere;
    font-weight: 500;
}

.app-footer {
    padding: 14px 20px 22px;
    color: var(--text-muted);
    font-size: 12px;
    text-align: center;
}

.app-footer a {
    color: var(--brand);
    font-weight: 700;
    text-decoration: none;
}

.error-page {
    max-width: 700px;
    margin: 80px auto;
    padding: 30px;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.error-page h1 {
    margin-top: 0;
}

.error-page small {
    display: block;
    margin-top: 20px;
    color: var(--text-muted);
}

.plain-button {
    display: inline-block;
    margin-top: 12px;
    padding: 10px 16px;
    color: #ffffff;
    background: var(--brand);
    text-decoration: none;
}

@media (max-width: 980px) {
    .workspace {
        grid-template-columns: 1fr;
    }

    #companiesList {
        height: 290px;
        min-height: 290px;
    }

    #mailsGrid {
        height: 620px;
    }
}

@media (max-width: 680px) {
    .app-body {
        padding-inline: 10px;
    }

    .page-heading {
        align-items: flex-start;
    }

    .page-subtitle {
        max-width: 240px;
    }

    .selection-summary {
        align-items: flex-start;
    }

    .summary-total {
        min-width: 92px;
    }

    .filters-panel {
        align-items: stretch;
    }

    .filter-type,
    .filter-date,
    .filter-actions {
        width: 100%;
    }

    .filter-actions {
        margin-left: 0;
        flex-wrap: wrap;
    }

    .detail-row {
        grid-template-columns: 1fr;
        gap: 5px;
    }
}

.startup-error {
    position: relative;
    z-index: 10000;
    margin: 16px;
    padding: 14px 16px;
    display: grid;
    gap: 4px;
    background: #fff4f4;
    border: 1px solid #d32f2f;
    color: #8a1c1c;
}

.startup-error strong {
    font-size: 14px;
}

.startup-error span {
    font-size: 13px;
}

/* Trazabilidad y certificado de entrega */
.trace-action {
    display: flex;
    justify-content: center;
}

.trace-action .dx-button {
    min-width: 104px;
}

.trace-certificate {
    min-height: 100%;
    padding: 8px 10px 18px;
    color: var(--text);
}

.trace-certificate-header {
    padding: 4px 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    border-bottom: 3px solid #39abe2;
}

.trace-brand {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 16px;
}

.trace-brand img {
    width: 122px;
    height: 64px;
    object-fit: contain;
    flex: 0 0 auto;
}

.trace-brand > div,
.trace-certificate-meta {
    min-width: 0;
    display: grid;
    gap: 4px;
}

.trace-brand span,
.trace-certificate-meta span,
.trace-certificate-meta small {
    color: var(--text-muted);
    font-size: 11px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.trace-brand strong {
    font-size: 20px;
    font-weight: 650;
}

.trace-certificate-meta {
    max-width: 430px;
    text-align: right;
}

.trace-certificate-meta strong {
    overflow-wrap: anywhere;
    font-size: 13px;
    font-weight: 600;
}

.trace-recipients {
    padding-top: 18px;
    display: grid;
    gap: 18px;
}

.trace-recipient-card {
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: 5px solid #6b7280;
}

.trace-state-delivered {
    border-top-color: #2e9d55;
}

.trace-state-failed {
    border-top-color: #c83a3a;
}

.trace-state-pending {
    border-top-color: #d58b16;
}

.trace-recipient-header {
    padding: 15px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    background: var(--surface-muted);
    border-bottom: 1px solid var(--border);
}

.trace-recipient-title {
    min-width: 0;
    display: grid;
    gap: 3px;
}

.trace-recipient-title span {
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.trace-recipient-title strong {
    overflow-wrap: anywhere;
    font-size: 16px;
}

.trace-status {
    padding: 6px 11px;
    flex: 0 0 auto;
    font-size: 12px;
    font-weight: 700;
    border: 1px solid #6b7280;
}

.trace-status-delivered {
    color: #237b42;
    background: #edf9f0;
    border-color: #2e9d55;
}

.trace-status-failed {
    color: #a52e2e;
    background: #fff0f0;
    border-color: #c83a3a;
}

.trace-status-pending {
    color: #995f09;
    background: #fff8e8;
    border-color: #d58b16;
}

.trace-status-unknown {
    color: var(--text);
    background: var(--surface-secondary);
    border-color: var(--border);
}

html[data-theme="dark"] .trace-status-delivered {
    color: #82d99d;
    background: #173422;
}

html[data-theme="dark"] .trace-status-failed {
    color: #ffaaaa;
    background: #3d1d1d;
}

html[data-theme="dark"] .trace-status-pending {
    color: #ffd180;
    background: #3c2d14;
}

.trace-mail-summary,
.trace-metrics,
.trace-detail-grid {
    display: grid;
    gap: 12px;
}

.trace-mail-summary {
    padding: 14px 18px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    border-bottom: 1px solid var(--border);
}

.trace-metrics {
    padding: 14px 18px;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    background: var(--surface-secondary);
    border-bottom: 1px solid var(--border);
}

.trace-mail-summary > div,
.trace-metrics > div,
.trace-detail-grid > div {
    min-width: 0;
    display: grid;
    gap: 3px;
}

.trace-mail-summary span,
.trace-metrics span,
.trace-detail-grid span,
.trace-response > span {
    color: var(--text-muted);
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.trace-mail-summary strong,
.trace-metrics strong,
.trace-detail-grid strong {
    overflow-wrap: anywhere;
    font-size: 12px;
    font-weight: 600;
}

.trace-details-section {
    padding: 16px 18px;
    border-bottom: 1px solid var(--border);
}

.trace-details-section:last-child {
    border-bottom: 0;
}

.trace-details-heading {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 18px;
}

.trace-details-heading h3 {
    margin: 0;
    font-size: 14px;
}

.trace-details-heading > span {
    color: var(--text-muted);
    font-size: 11px;
    text-align: right;
}

.trace-detail-grid {
    padding-top: 14px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.trace-response {
    margin-top: 14px;
    display: grid;
    gap: 6px;
}

.trace-response pre {
    max-height: 150px;
    margin: 0;
    padding: 11px 13px;
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    color: var(--text);
    background: var(--surface-secondary);
    border-left: 3px solid #39abe2;
    font-family: Consolas, "Courier New", monospace;
    font-size: 11px;
    line-height: 1.5;
}

.trace-certificate-footer {
    margin-top: 18px;
    padding-top: 12px;
    color: var(--text-muted);
    font-size: 10px;
    text-align: center;
    border-top: 1px solid var(--border);
}

@media (max-width: 720px) {
    .brand-logo {
        width: 62px;
        height: 36px;
    }

    .trace-certificate-header,
    .trace-recipient-header,
    .trace-details-heading {
        align-items: flex-start;
        flex-direction: column;
    }

    .trace-certificate-meta {
        max-width: none;
        text-align: left;
    }

    .trace-mail-summary,
    .trace-metrics,
    .trace-detail-grid {
        grid-template-columns: 1fr;
    }

    .trace-details-heading > span {
        text-align: left;
    }
}


@media (max-width: 680px) {
    .trace-session-panel {
        grid-template-columns: 10px minmax(0, 1fr);
    }

    #refreshTraceSessionButton {
        grid-column: 1 / -1;
        justify-self: stretch;
    }

    #refreshTraceSessionButton .dx-button {
        width: 100%;
    }

    .trace-session-content span {
        white-space: normal;
    }
}

/* Sesión y login */
.app-header {
    justify-content: space-between;
    gap: 20px;
}

.header-session {
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 14px;
    color: #ffffff;
}

.header-session-info {
    min-width: 0;
    display: grid;
    gap: 2px;
    text-align: right;
}

.header-session-info strong,
.header-session-info span {
    max-width: 420px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.header-session-info strong {
    font-size: 13px;
}

.header-session-info span {
    color: #aebbd0;
    font-size: 11px;
}

.logout-button,
.login-secondary-button {
    min-height: 34px;
    padding: 7px 13px;
    border: 1px solid currentColor;
    background: transparent;
    cursor: pointer;
    font-weight: 600;
}

.logout-button {
    color: #ffffff;
}

.logout-button:hover,
.logout-button:focus-visible {
    color: #d9ecff;
    background: rgba(255, 255, 255, 0.08);
}

body.anonymous .app-body {
    width: 100%;
    min-height: calc(100vh - 126px);
    padding: 0;
}

.login-page {
    min-height: calc(100vh - 126px);
    padding: 38px 18px;
    display: grid;
    place-items: center;
    background:
        linear-gradient(135deg, rgba(41, 98, 255, 0.08), transparent 45%),
        var(--surface-secondary);
}

.login-card {
    width: min(470px, 100%);
    padding: 30px 32px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: 4px solid var(--brand);
    box-shadow: var(--shadow);
}

.login-brand {
    padding-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    border-bottom: 1px solid var(--border);
}

.login-brand img {
    width: 112px;
    height: 58px;
    object-fit: contain;
}

.login-brand div {
    min-width: 0;
    display: grid;
    gap: 3px;
}

.login-brand span {
    color: var(--text-muted);
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.login-brand strong {
    font-size: 17px;
}

.login-heading {
    padding: 22px 0 18px;
}

.login-heading h1 {
    margin: 0;
    font-size: 28px;
}

.login-heading p:last-child {
    margin: 8px 0 0;
    color: var(--text-muted);
    line-height: 1.5;
}

.login-fields {
    display: grid;
    gap: 15px;
}

.login-field {
    display: grid;
    gap: 6px;
}

.login-field label {
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 700;
}

.login-actions {
    margin-top: 20px;
    display: grid;
    gap: 10px;
}

.login-secondary-button {
    width: 100%;
    color: var(--text);
    border-color: var(--border);
}

.login-secondary-button:hover,
.login-secondary-button:focus-visible {
    background: var(--surface-muted);
}

.login-help {
    margin: 18px 0 0;
    color: var(--text-muted);
    font-size: 11px;
    text-align: center;
}

.login-message {
    display: none;
    margin: 0 0 16px;
    padding: 10px 12px;
    font-size: 12px;
    line-height: 1.45;
    border-left: 4px solid var(--border);
    background: var(--surface-secondary);
}

.login-message.is-visible {
    display: block;
}

.login-message.is-error {
    color: #9f2f2f;
    border-left-color: #c83a3a;
    background: #fff0f0;
}

.login-message.is-warning {
    color: #8a5a0e;
    border-left-color: #d58b16;
    background: #fff8e8;
}

.login-message.is-success {
    color: #237b42;
    border-left-color: #2e9d55;
    background: #edf9f0;
}

html[data-theme="dark"] .login-message.is-error {
    color: #ffb1b1;
    background: #3d1d1d;
}

html[data-theme="dark"] .login-message.is-warning {
    color: #ffd180;
    background: #3c2d14;
}

html[data-theme="dark"] .login-message.is-success {
    color: #9be1ae;
    background: #173422;
}

/* Superficie fuera de pantalla usada exclusivamente por html2pdf. */
.pdf-export-host {
    --surface: #ffffff;
    --surface-secondary: #f5f7fb;
    --surface-muted: #edf1f7;
    --border: #d9e0ea;
    --text: #172033;
    --text-muted: #667085;
    position: fixed;
    top: 0;
    left: -20000px;
    width: 794px;
    padding: 0;
    color: var(--text);
    background: #ffffff;
    font-family: "Segoe UI", Arial, sans-serif;
}

.pdf-export-host .trace-certificate {
    width: 794px;
    min-height: auto;
    padding: 18px;
    color: var(--text);
    background: #ffffff;
}

.pdf-export-host .trace-status-delivered {
    color: #237b42;
    background: #edf9f0;
    border-color: #2e9d55;
}

.pdf-export-host .trace-status-failed {
    color: #a52e2e;
    background: #fff0f0;
    border-color: #c83a3a;
}

.pdf-export-host .trace-status-pending {
    color: #995f09;
    background: #fff8e8;
    border-color: #d58b16;
}

.pdf-export-host .trace-recipient-card,
.pdf-export-host .trace-details-section {
    break-inside: avoid;
    page-break-inside: avoid;
}

.pdf-export-host .trace-response pre {
    max-height: none;
    overflow: visible;
}

@media (max-width: 680px) {
    .app-header {
        align-items: flex-start;
    }

    .header-session {
        align-items: flex-end;
        flex-direction: column;
        gap: 7px;
    }

    .header-session-info strong,
    .header-session-info span {
        max-width: 180px;
    }

    .login-card {
        padding: 24px 20px;
    }

    .login-brand img {
        width: 92px;
    }
}

/* Ajustes de login LG_001 y FPS_Push */
.visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

.notification-id {
    color: #8fc8ff !important;
    font-family: Consolas, "Courier New", monospace;
    font-size: 10px !important;
}

.logout-button:disabled {
    opacity: 0.6;
    cursor: wait;
}

.login-card {
    position: relative;
}

.login-settings-button {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 4;
    width: 34px;
    height: 34px;
    padding: 0;
    display: grid;
    place-items: center;
    color: var(--text-muted);
    background: var(--surface-secondary);
    border: 1px solid var(--border);
    cursor: pointer;
    font-size: 17px;
    line-height: 1;
}

.login-settings-button:hover,
.login-settings-button:focus-visible {
    color: var(--brand);
    border-color: var(--brand);
}

.login-settings-panel {
    position: absolute;
    top: 52px;
    right: 12px;
    z-index: 10;
    width: min(330px, calc(100% - 24px));
    padding: 14px;
    display: grid;
    gap: 12px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.login-settings-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.login-settings-heading strong {
    font-size: 13px;
}

.login-settings-heading button {
    width: 28px;
    height: 28px;
    padding: 0;
    color: var(--text-muted);
    background: transparent;
    border: 1px solid var(--border);
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
}

.login-settings-panel small {
    color: var(--text-muted);
    font-size: 10px;
    line-height: 1.4;
}

@media (max-width: 680px) {
    .notification-id {
        max-width: 190px !important;
    }

    .login-settings-panel {
        right: 8px;
        width: calc(100% - 16px);
    }
}
