:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --border: #dfe3e8;
    --text: #1f2933;
    --muted: #6b7885;
    --accent: #1f6feb;
    --accent-dark: #1a5fd0;
    --ok: #1a7f42;
    --ok-bg: #e4f5ea;
    --err: #b42318;
    --err-bg: #fdecea;
    --warn: #8a5a00;
    --warn-bg: #fdf3e2;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 22px; margin: 24px 0 16px; }
h2 { font-size: 17px; margin: 28px 0 12px; }

/* ---------- Barra superior ---------- */
.topbar {
    display: flex;
    align-items: center;
    gap: 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 24px;
    height: 56px;
    position: relative;
}
.brand { font-size: 17px; color: var(--text); font-weight: 500; }
.brand strong { color: var(--accent); }
.main-nav { display: flex; gap: 18px; flex: 1; }
.main-nav a { color: var(--muted); padding: 4px 0; }
.main-nav a:hover { color: var(--text); text-decoration: none; }
.topbar .user { display: flex; align-items: center; gap: 12px; color: var(--muted); }
.logout { color: var(--muted); }

.menu-toggle {
    display: none;
    width: 42px;
    height: 42px;
    padding: 9px 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    cursor: pointer;
}
.menu-toggle span {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--text);
    margin: 5px 0;
    border-radius: 2px;
}

/* ---------- Layout ---------- */
.container { max-width: 1160px; margin: 0 auto; padding: 0 24px 48px; }

.section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin: 24px 0 12px;
}
.section-head h1, .section-head h2 { margin: 0; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 18px;
    margin-bottom: 18px;
}

.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; }
.empty { color: var(--muted); padding: 32px; text-align: center; background: var(--surface); border: 1px dashed var(--border); border-radius: 8px; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; }
.break { word-break: break-all; }
.truncate { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Login ---------- */
.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 24px; }
.login-card { width: 100%; max-width: 360px; }
.login-card h1 { margin: 0; font-size: 24px; font-weight: 500; }
.login-card h1 strong { color: var(--accent); }
.login-card p { margin: 4px 0 20px; }

/* ---------- Formularios ---------- */
label { display: block; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
input, select {
    display: block;
    width: 100%;
    margin-top: 5px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
}
input:focus, select:focus { outline: 2px solid rgba(31, 111, 235, .25); border-color: var(--accent); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
label.check input { width: auto; margin: 0; }

button, .btn {
    display: inline-block;
    padding: 8px 16px;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: var(--accent);
    color: #fff;
    font-size: 14px;
    font-family: inherit;
    cursor: pointer;
}
button:hover, .btn:hover { background: var(--accent-dark); text-decoration: none; }
.btn-link { background: none; border-color: transparent; color: var(--accent); padding-left: 0; }
.btn-link:hover { background: none; text-decoration: underline; }
.btn-danger {
    padding: 5.6px 11.2px;
    font-size: 9.8px;
    background: var(--err);
    border-color: var(--err);
}
.btn-danger:hover { background: #8f1d14; border-color: #8f1d14; }
.btn-secondary { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-secondary:hover { background: #f4f6f8; }

.modal-backdrop[hidden] { display: none; }
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: center;
    padding: 24px;
    background: rgba(15, 23, 42, .48);
}
.confirm-modal {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 4px 14px;
    width: min(100%, 430px);
    padding: 24px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 18px 50px rgba(15, 23, 42, .22);
}
.confirm-icon {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--err-bg);
    color: var(--err);
    font-size: 20px;
    font-weight: 700;
}
.error-icon { background: var(--err-bg); color: var(--err); }
.confirm-copy h2 { margin: 0; font-size: 18px; }
.confirm-copy p { margin: 4px 0 0; color: var(--muted); }
.confirm-actions { grid-column: 2; display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.confirm-actions .confirm-delete { padding: 8px 14px; font-size: 13px; }

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

.filters {
    display: grid;
    grid-template-columns: repeat(5, minmax(150px, 1fr));
    gap: 14px 14px;
    align-items: end;
}

.filters label {
    margin-bottom: 0;
    min-width: 0;
}

.filters label.grow {
    grid-column: span 2;
}

.filter-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 0;
    justify-content: flex-start;
}

.actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-top: 16px; }
.actions form { margin: 0; }
.table td form { margin: 0; }

.client-actions-heading,
.client-actions-cell { text-align: right !important; }
.table tbody tr.client-actions-open { position: relative; z-index: 5; }
.client-actions-menu {
    position: relative;
    display: inline-block;
    text-align: left;
}
.client-actions-menu summary {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 3px;
    width: 34px;
    height: 34px;
    border: 1px solid transparent;
    border-radius: 6px;
    color: var(--muted);
    cursor: pointer;
    list-style: none;
}
.client-actions-menu summary span {
    display: block;
    width: 15px;
    height: 2px;
    border-radius: 2px;
    background: currentColor;
}
.client-actions-menu summary::-webkit-details-marker { display: none; }
.client-actions-menu summary:hover,
.client-actions-menu[open] summary {
    background: #f4f6f8;
    border-color: var(--border);
    color: var(--text);
}
.client-actions-popover {
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    z-index: 10;
    min-width: 190px;
    padding: 6px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 20px rgba(15, 23, 42, .14);
}
.client-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 9px 10px;
    border: 0;
    border-radius: 5px;
    color: var(--text);
    background: transparent;
    text-align: left;
    font-size: 13px;
    cursor: pointer;
}
.client-menu-item:hover { background: #f4f6f8; }
.client-menu-icon {
    display: inline-grid;
    flex: 0 0 24px;
    place-items: center;
    width: 24px;
    height: 24px;
    border-radius: 6px;
    background: #f1f5f9;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}
.client-actions-popover form { margin: 0; }
.client-actions-popover form .client-menu-delete {
    color: var(--err);
}
.client-menu-delete .client-menu-icon {
    background: var(--err-bg);
    color: var(--err);
}
.client-actions-popover form .client-menu-delete:hover {
    background: var(--err-bg);
}
.client-details-dialog {
    width: min(92vw, 620px);
    max-height: 85vh;
    margin: auto;
    padding: 26px;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 24px 70px rgba(15, 23, 42, .24);
}
.client-details-dialog::backdrop { background: rgba(15, 23, 42, .48); }
.client-dialog-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 22px;
}
.client-dialog-head h2 { margin: 0; font-size: 21px; }
.client-dialog-head .eyebrow {
    margin: 0 0 4px;
    color: var(--accent);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
}
.client-dialog-head .muted { margin: 4px 0 0; }
.dialog-close {
    display: grid;
    flex: 0 0 34px;
    place-items: center;
    width: 34px;
    height: 34px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: #f1f5f9;
    color: var(--muted);
    font-size: 22px;
}
.dialog-close:hover { background: #e8edf3; color: var(--text); }
.client-detail-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    margin: 0;
}
.client-detail-grid > div {
    min-width: 0;
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: #fafbfc;
}
.client-detail-grid .client-detail-wide { grid-column: 1 / -1; }
.client-detail-grid dt { color: var(--muted); font-size: 12px; }
.client-detail-grid dd { margin: 5px 0 0; font-weight: 600; overflow-wrap: anywhere; }
/* ---------- Tablas ---------- */
.table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: visible;
}
.table th, .table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { background: #fafbfc; color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr { cursor: pointer; }
.table tbody tr:hover { background: #f7f9fc; }

.detail-grid { display: grid; grid-template-columns: 220px 1fr; gap: 10px 16px; margin: 0; }
.detail-grid dt { color: var(--muted); font-size: 13px; }
.detail-grid dd { margin: 0; }

/* ---------- Estados ---------- */
.badge {
    display: inline-block;
    padding: 2px 9px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 500;
}
.badge-aceptado { background: var(--ok-bg); color: var(--ok); }
.badge-rechazado { background: var(--err-bg); color: var(--err); }
.badge-error { background: var(--err-bg); color: var(--err); }
.badge-enviado { background: var(--warn-bg); color: var(--warn); }

.alert { padding: 11px 14px; border-radius: 6px; margin: 18px 0; }
.alert-error { background: var(--err-bg); color: var(--err); }
.alert-ok { background: var(--ok-bg); color: var(--ok); }

/* ---------- Estadísticas ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 14px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
.stat-value { display: block; font-size: 26px; font-weight: 600; }
.stat-label { display: block; margin-top: 6px; color: var(--muted); font-size: 13px; }

/* ---------- Bloques de texto ---------- */
pre.messages, pre.raw {
    margin: 0;
    padding: 12px;
    background: #f7f9fc;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-size: 12px;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 420px;
    overflow: auto;
}
details:not(.client-actions-menu) summary { cursor: pointer; color: var(--accent); margin-bottom: 10px; }

.pagination { display: flex; gap: 16px; align-items: center; justify-content: center; padding: 18px; color: var(--muted); }

/* ---------- Responsive móvil ---------- */
@media (max-width: 768px) {
    .topbar {
        flex-wrap: wrap;
        height: auto;
        padding: 10px 14px;
        gap: 8px 12px;
        position: sticky;
        top: 0;
        z-index: 20;
        box-shadow: 0 1px 0 rgba(15, 23, 42, 0.06);
    }

    .brand {
        font-size: 16px;
    }

    .menu-toggle {
        display: inline-block;
        margin-left: auto;
        width: 42px;
        height: 42px;
        border-radius: 10px;
        background: #f5f7fa;
        border-color: #dfe3e8;
    }

    .main-nav {
        display: none;
        width: 100%;
        flex-direction: column;
        gap: 2px;
        padding: 6px 0 0;
        border-top: 1px solid var(--border);
    }

    .main-nav a {
        display: block;
        width: 100%;
        padding: 12px 10px;
        border-radius: 8px;
        color: var(--text);
        font-weight: 500;
        background: #f8fafc;
    }

    .topbar.open .main-nav {
        display: flex;
    }

    .topbar .user {
        display: none;
    }

    .container {
        padding: 0 12px 32px;
    }

    .section-head {
        flex-direction: column;
        align-items: flex-start;
    }

    .filters {
        display: grid;
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .filters label.grow {
        grid-column: auto;
    }

    .filter-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        width: 100%;
    }

    .filter-actions button,
    .filter-actions .btn {
        width: 100%;
        text-align: center;
    }

    .table {
        display: block;
        width: 100%;
        border: none;
        background: transparent;
    }

    .table thead {
        display: none;
    }

    .table tbody {
        display: grid;
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .table tbody tr {
        display: block;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: 12px;
        padding: 12px;
        box-shadow: 0 2px 8px rgba(16, 24, 40, 0.04);
    }

    .table tbody tr td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 10px;
        padding: 8px 0;
        border-bottom: 1px solid #eef1f4;
        min-width: 0;
        white-space: normal;
    }

    .table tbody tr td:last-child {
        border-bottom: none;
    }

    .client-actions-cell { position: relative; z-index: 2; }
    .client-actions-popover { top: 38px; }

    .client-details-dialog { padding: 20px; }
    .client-detail-grid { grid-template-columns: 1fr; }
    .client-detail-grid .client-detail-wide { grid-column: auto; }

    .table tbody tr td.client-actions-cell {
        justify-content: space-between;
    }

    .table tbody tr td::before {
        content: attr(data-label);
        color: var(--muted);
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        flex: 0 0 40%;
    }

    .table tbody tr td span,
    .table tbody tr td div,
    .table tbody tr td a,
    .table tbody tr td strong {
        flex: 1;
        text-align: right;
    }

    .pagination {
        flex-wrap: wrap;
        gap: 8px 12px;
        font-size: 12px;
    }

    .detail-grid {
        grid-template-columns: 1fr;
    }
}
