:root {
    --blue: #0a347e;
    --blue-2: #092b69;
    --red: #e41622;
    --red-2: #b90f18;
    --red-soft: #fff1f2;
    --ink: #14213d;
    --muted: #68748a;
    --line: #dfe5f0;
    --bg: #f6f8fc;
    --panel: rgba(255,255,255,.92);
    --shadow: 0 22px 70px rgba(10, 52, 126, .13);
    --radius: 22px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
    color: var(--ink);
    background:
        radial-gradient(circle at 10% 0%, rgba(228, 22, 34, .13), transparent 30%),
        radial-gradient(circle at 90% 10%, rgba(10, 52, 126, .14), transparent 35%),
        linear-gradient(135deg, #ffffff 0%, #f3f6fb 55%, #ffffff 100%);
    min-height: 100vh;
}

button, input, select, textarea { font: inherit; }
a { color: inherit; }
code {
    background: #eef2f8;
    padding: 2px 6px;
    border-radius: 8px;
}

.login-body, .install-body {
    display: grid;
    place-items: center;
    padding: 28px;
}
.login-shell {
    width: min(1120px, 100%);
    display: grid;
    grid-template-columns: 1.08fr .92fr;
    gap: 24px;
    align-items: stretch;
}
.login-brand-card, .login-form-card, .install-card, .panel, .summary-card, .chart-card {
    background: var(--panel);
    border: 1px solid rgba(255,255,255,.68);
    box-shadow: var(--shadow);
    backdrop-filter: blur(16px);
    border-radius: var(--radius);
}
.login-brand-card {
    position: relative;
    overflow: hidden;
    padding: 34px;
    min-height: 560px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
}
.brand-glow {
    position: absolute;
    width: 360px;
    height: 360px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(228,22,34,.20), transparent 67%);
    top: -90px;
    right: -80px;
}
.main-logo {
    width: 168px;
    height: 168px;
    object-fit: contain;
    border-radius: 50%;
    filter: drop-shadow(0 18px 28px rgba(10,52,126,.18));
    margin-bottom: 28px;
}
.program-icon {
    width: min(390px, 82%);
    max-height: 245px;
    object-fit: contain;
    filter: drop-shadow(0 12px 22px rgba(228,22,34,.12));
}
.login-brand-card h1 {
    margin: 8px 0 8px;
    font-size: clamp(2.3rem, 5vw, 4.3rem);
    color: var(--blue);
    letter-spacing: -1px;
}
.login-brand-card p {
    max-width: 560px;
    margin: 0 auto 18px;
    color: var(--muted);
    font-size: 1.05rem;
    line-height: 1.55;
}
.pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 14px;
    border-radius: 999px;
    background: var(--red-soft);
    color: var(--red-2);
    font-weight: 700;
    font-size: .92rem;
}
.login-form-card {
    padding: 34px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}
.mini-heading {
    display: flex;
    flex-direction: column;
    gap: 3px;
    margin-bottom: 28px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .08em;
    font-size: .75rem;
}
.mini-heading strong {
    color: var(--blue);
    font-size: 1.1rem;
    letter-spacing: .14em;
}
.login-form-card h2, .install-card h1 {
    color: var(--blue);
    margin: 0 0 20px;
    font-size: 2rem;
}
.login-form, .record-form { display: grid; gap: 16px; }
label {
    display: grid;
    gap: 7px;
    font-size: .86rem;
    font-weight: 700;
    color: #23304a;
}
input, select, textarea {
    width: 100%;
    border: 1px solid var(--line);
    background: rgba(255,255,255,.94);
    border-radius: 14px;
    padding: 12px 13px;
    color: var(--ink);
    outline: none;
    transition: border-color .18s ease, box-shadow .18s ease, transform .18s ease;
}
input:focus, select:focus, textarea:focus {
    border-color: rgba(10,52,126,.55);
    box-shadow: 0 0 0 4px rgba(10,52,126,.10);
}
textarea { resize: vertical; min-height: 88px; }
.btn {
    border: 0;
    border-radius: 14px;
    padding: 11px 16px;
    font-weight: 800;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: transform .16s ease, box-shadow .16s ease, opacity .16s ease;
    white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary {
    color: #fff;
    background: linear-gradient(135deg, var(--red), var(--red-2));
    box-shadow: 0 12px 24px rgba(228,22,34,.23);
}
.btn.ghost {
    color: var(--blue);
    background: #fff;
    border: 1px solid var(--line);
}
.btn.light {
    color: var(--blue);
    background: #eef3fb;
}
.btn.tiny {
    padding: 7px 10px;
    border-radius: 10px;
    font-size: .78rem;
    background: #edf2fb;
    color: var(--blue);
}
.btn.full { width: 100%; }
.alert {
    border-radius: 14px;
    padding: 12px 14px;
    margin: 8px 0 16px;
    font-weight: 700;
    line-height: 1.45;
}
.alert.danger { background: #ffe6e9; color: #9b111b; border: 1px solid #ffc4cb; }
.alert.success { background: #eafff3; color: #14663a; border: 1px solid #b9f0ce; }
.muted { color: var(--muted); }
.small { font-size: .86rem; line-height: 1.45; }
.install-card {
    width: min(720px, 100%);
    padding: 34px;
    text-align: center;
}
.install-logo { width: 140px; height: 140px; object-fit: contain; border-radius: 30px; }
.install-credentials { text-align: left; background: #f7f9fd; border: 1px solid var(--line); border-radius: 18px; padding: 18px; margin: 18px 0; }

.app-body { padding-bottom: 34px; }
.topbar {
    margin: 18px auto 12px;
    width: min(1500px, calc(100% - 28px));
    background: rgba(255,255,255,.88);
    border: 1px solid rgba(255,255,255,.72);
    box-shadow: 0 14px 50px rgba(10,52,126,.10);
    backdrop-filter: blur(16px);
    border-radius: 26px;
    padding: 14px 18px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
}
.topbar-brand { display: flex; align-items: center; gap: 14px; }
.top-logo { width: 68px; height: 68px; object-fit: contain; border-radius: 50%; }
.topbar p { margin: 0; color: var(--muted); font-size: .85rem; }
.topbar h1 { margin: 2px 0 0; color: var(--blue); font-size: clamp(1.5rem, 3vw, 2.3rem); }
.top-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.user-chip {
    background: #f1f5fb;
    color: var(--blue);
    padding: 10px 12px;
    border-radius: 999px;
    font-weight: 800;
    font-size: .86rem;
}
.tabs {
    width: min(1500px, calc(100% - 28px));
    margin: 0 auto 16px;
    display: flex;
    gap: 10px;
    overflow-x: auto;
    padding: 4px;
}
.tab {
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 12px 18px;
    background: rgba(255,255,255,.8);
    color: var(--blue);
    font-weight: 900;
    cursor: pointer;
}
.tab.active {
    background: linear-gradient(135deg, var(--blue), var(--blue-2));
    color: #fff;
    border-color: transparent;
}
.app-shell { width: min(1500px, calc(100% - 28px)); margin: auto; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.summary-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(150px, 1fr));
    gap: 14px;
    margin-bottom: 28px;
}
.summary-card { padding: 18px; position: relative; overflow: hidden; }
.summary-card::after {
    content: '';
    position: absolute;
    width: 88px;
    height: 88px;
    border-radius: 50%;
    background: rgba(10,52,126,.08);
    right: -28px;
    top: -28px;
}
.summary-card.urgent::after { background: rgba(228,22,34,.14); }
.summary-card.paid::after { background: rgba(10,52,126,.12); }
.summary-card span { color: var(--muted); font-weight: 800; font-size: .83rem; }
.summary-card strong { display: block; margin-top: 6px; font-size: clamp(1.35rem, 3vw, 2.2rem); color: var(--blue); }
.summary-card.urgent strong { color: var(--red); }
.workspace-grid {
    display: grid;
    grid-template-columns: minmax(340px, 460px) 1fr;
    gap: 16px;
    align-items: start;
}
.panel { padding: 18px; }
.panel-title {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    align-items: flex-start;
    margin-bottom: 16px;
}
.panel-title h2 { margin: 0; color: var(--blue); font-size: 1.35rem; }
.panel-title p { margin: 5px 0 0; color: var(--muted); line-height: 1.45; font-size: .92rem; }
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 13px;
}
.form-grid.single { grid-template-columns: 1fr; }
.span-2 { grid-column: 1 / -1; }
.form-actions, .button-row { display: flex; gap: 10px; flex-wrap: wrap; }
.table-panel { min-width: 0; }
.table-title { align-items: center; }
.filters-grid {
    display: grid;
    grid-template-columns: minmax(240px, 2fr) repeat(5, minmax(130px, 1fr));
    gap: 11px;
    margin-bottom: 12px;
    align-items: end;
}
.table-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin: 8px 0 12px;
    color: var(--muted);
    font-weight: 800;
}
.table-scroll {
    width: 100%;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: #fff;
}
.data-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    min-width: 1500px;
    font-size: .82rem;
}
.data-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: linear-gradient(135deg, var(--blue), var(--blue-2));
    color: #fff;
    text-align: left;
    padding: 12px 10px;
    white-space: nowrap;
}
.data-table td {
    padding: 11px 10px;
    border-bottom: 1px solid #edf1f7;
    vertical-align: top;
    color: #22304a;
}
.data-table tr:nth-child(even) td { background: #fbfcff; }
.data-table tr:hover td { background: #fff8f8; }
.compact { min-width: 700px; }
.status-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 7px 10px;
    border-radius: 999px;
    font-weight: 900;
    white-space: nowrap;
}
.status-badge.debt { color: var(--red-2); background: #ffe7e9; }
.status-badge.paid { color: var(--blue); background: #eaf0fb; }
.row-actions { display: flex; gap: 7px; flex-wrap: wrap; }
.reports-panel { padding: 20px; }
.report-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}
.report-card {
    background: linear-gradient(180deg, #ffffff, #f7f9fd);
    border: 1px solid var(--line);
    border-radius: 18px;
    padding: 15px;
}
.report-card span { color: var(--muted); font-weight: 800; font-size: .82rem; }
.report-card strong { display: block; color: var(--blue); font-size: 1.7rem; margin-top: 6px; }
.charts-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(280px, 1fr));
    gap: 16px;
}
.chart-card { padding: 18px; box-shadow: none; border: 1px solid var(--line); }
.chart-card.wide { grid-column: 1 / -1; }
.chart-card h3 { margin: 0 0 10px; color: var(--blue); }
.chart-card canvas { width: 100%; max-height: 360px; }
.small-scroll { max-height: 330px; }
.users-grid { grid-template-columns: minmax(300px, 390px) 1fr; }


/* Corrección reforzada: el historial nunca debe mostrarse hasta que se oprima su botón. */
.modal[hidden],
.modal.is-hidden,
#historyModal[hidden],
#historyModal.is-hidden,
.toast[hidden] {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
}

#historyModal:not(.is-hidden):not([hidden]) {
    display: grid !important;
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
}
.modal {
    position: fixed;
    inset: 0;
    background: rgba(8,18,38,.55);
    display: grid;
    place-items: center;
    padding: 20px;
    z-index: 20;
}
.modal-card {
    width: min(860px, 100%);
    max-height: 86vh;
    overflow: auto;
    background: #fff;
    border-radius: 24px;
    box-shadow: 0 28px 80px rgba(0,0,0,.25);
    padding: 20px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.modal-head h2 { margin: 0; color: var(--blue); }
.history-item {
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 13px;
    margin-bottom: 10px;
    background: #f9fbff;
}
.history-item h3 { margin: 0 0 6px; color: var(--blue); font-size: 1rem; }
.history-item p { margin: 4px 0; color: var(--muted); }
.history-changes {
    margin-top: 8px;
    display: grid;
    gap: 6px;
}
.change-line {
    background: #fff;
    border: 1px solid #edf1f7;
    border-radius: 10px;
    padding: 8px;
    line-height: 1.35;
}
.toast {
    position: fixed;
    right: 18px;
    bottom: 18px;
    background: var(--ink);
    color: #fff;
    padding: 13px 16px;
    border-radius: 16px;
    box-shadow: 0 12px 34px rgba(0,0,0,.22);
    z-index: 30;
    font-weight: 800;
    max-width: min(420px, calc(100% - 36px));
}

@media (max-width: 1180px) {
    .workspace-grid, .users-grid { grid-template-columns: 1fr; }
    .filters-grid { grid-template-columns: repeat(3, 1fr); }
    .search-field { grid-column: 1 / -1; }
    .summary-grid { grid-template-columns: repeat(2, 1fr); }
    .form-panel { order: 1; }
}
@media (max-width: 820px) {
    .login-shell { grid-template-columns: 1fr; }
    .login-brand-card { min-height: auto; padding: 26px; }
    .program-icon { width: min(320px, 90%); }
    .topbar { align-items: flex-start; flex-direction: column; }
    .top-actions { width: 100%; justify-content: space-between; }
    .filters-grid { grid-template-columns: 1fr; }
    .form-grid { grid-template-columns: 1fr; }
    .summary-grid, .report-grid, .charts-grid { grid-template-columns: 1fr; }
    .chart-card.wide { grid-column: auto; }
    .table-title, .panel-title { flex-direction: column; }
    .button-row .btn, .form-actions .btn { flex: 1 1 160px; }
}
@media (max-width: 520px) {
    .login-body, .install-body { padding: 14px; }
    .login-form-card, .login-brand-card, .install-card, .panel { padding: 18px; border-radius: 18px; }
    .main-logo { width: 128px; height: 128px; }
    .top-logo { width: 54px; height: 54px; }
    .top-actions { flex-direction: column; align-items: stretch; }
    .top-actions .btn, .user-chip { width: 100%; text-align: center; justify-content: center; }
}

/* Ajuste de acceso: pantalla limpia sin logotipo grande de Triangulaciones */
.login-shell-single {
    width: min(520px, 100%);
    grid-template-columns: 1fr;
}
.login-form-card-single {
    min-height: 560px;
}
.login-help {
    margin-top: -8px;
    margin-bottom: 8px;
}
@media (max-width: 640px) {
    .login-form-card-single {
        min-height: auto;
    }
}
