:root {
    --ink: #18212f;
    --muted: #627083;
    --line: #d9e0e8;
    --surface: #ffffff;
    --bg: #f4f7f9;
    --primary: #5b0f69;
    --primary-dark: #c4a414;
    --danger: #b42318;
    --green: #60c9a6;
    --red: #ef4438;
    --blue: #5f89c9;
    --gold: #f8c23d;
    --shadow: 0 14px 34px rgba(24, 33, 47, 0.08);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    color: var(--ink);
    background:
        radial-gradient(circle at top left, rgba(248, 194, 61, 0.18), transparent 28rem),
        radial-gradient(circle at top right, rgba(96, 201, 166, 0.16), transparent 24rem),
        var(--bg);
    font-family: Arial, Helvetica, sans-serif;
}

a {
    color: var(--primary-dark);
    text-decoration: none;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    min-height: 72px;
    padding: 0 28px;
    background: rgba(255, 255, 255, 0.94);
    border-bottom: 1px solid var(--line);
    box-shadow: 0 8px 28px rgba(24, 33, 47, 0.06);
}

.brand {
    display: inline-flex;
    align-items: center;
    width: 168px;
    min-width: 132px;
}

.brand img {
    display: block;
    width: 100%;
    height: auto;
}

nav {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 14px;
}

nav a {
    padding: 8px 10px;
    color: var(--ink);
    border-radius: 6px;
}

nav a:hover {
    color: var(--primary);
    background: rgba(91, 15, 105, 0.08);
}

.page {
    width: min(1180px, calc(100% - 32px));
    margin: 28px auto 64px;
}

.dashboard-screen {
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 156px);
}

.header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    margin-bottom: 20px;
}

h1, h2, p {
    margin-top: 0;
}

p {
    color: var(--muted);
}

.panel, table, .card, .stats article, .search {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.auth {
    display: grid;
    place-items: center;
    height: calc(100vh - 72px);
    min-height: 560px;
    padding: 20px 16px;
    background: url("branding/fondo_login_v2.png") center bottom / contain no-repeat;
}

.login-page {
    overflow: hidden;
    background: linear-gradient(135deg, #fbfcfd 0%, #f4f7f9 48%, #eef8f4 100%);
}

.login-page .page {
    width: 100%;
    margin: 0;
}

.auth .panel, form.panel {
    width: min(760px, 100%);
    padding: 24px;
}

.auth .panel {
    width: min(420px, 100%);
    background: rgba(255, 255, 255, 0.94);
    backdrop-filter: blur(4px);
}

label {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

input, select, textarea {
    width: 100%;
    min-height: 42px;
    padding: 9px 11px;
    color: var(--ink);
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 6px;
    font: inherit;
}

textarea {
    resize: vertical;
}

button, .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    padding: 0 16px;
    color: #fff;
    background: linear-gradient(135deg, var(--primary), #7d238c);
    border: 0;
    border-radius: 6px;
    font-weight: 700;
    cursor: pointer;
}

button:hover, .button:hover {
    background: linear-gradient(135deg, #471052, var(--primary));
}

.secondary {
    color: var(--ink);
    background: #e7edf2;
}

.secondary:hover {
    background: #d8e1e8;
}

.danger {
    background: var(--danger);
}

.link-danger {
    min-height: auto;
    padding: 0;
    color: var(--danger);
    background: transparent;
}

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

.followup-patient-field {
    display: grid;
    gap: 7px;
}

.patient-combobox {
    position: relative;
}

.patient-search-results {
    position: absolute;
    z-index: 20;
    top: 68px;
    right: 0;
    left: 0;
    max-height: 280px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 6px;
    box-shadow: var(--shadow);
}

.patient-search-results[hidden],
.patient-search-option[hidden] {
    display: none;
}

.patient-search-option {
    display: grid;
    width: 100%;
    min-height: auto;
    justify-items: start;
    gap: 3px;
    padding: 10px 12px;
    color: var(--ink);
    background: #fff;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    text-align: left;
}

.patient-search-option:last-child {
    border-bottom: 0;
}

.patient-search-option:hover,
.patient-search-option.is-active {
    color: var(--ink);
    background: #f5eef7;
}

.patient-search-option span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 400;
}

.patient-search-status {
    display: block;
    min-height: 18px;
    padding-top: 5px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 400;
}

.patient-search-status[data-state="error"] {
    color: var(--danger);
}

[data-guambra-search][aria-invalid="true"] {
    border-color: var(--danger);
}

.detail-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
    padding: 18px;
    margin-bottom: 18px;
}

.detail-grid div {
    display: grid;
    gap: 5px;
}

.detail-grid b {
    color: var(--muted);
    font-size: 12px;
    text-transform: uppercase;
}

.print-options {
    padding: 18px;
}

.search {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(150px, auto) auto;
    gap: 10px;
    margin-bottom: 16px;
    padding: 12px;
}

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

.actions form {
    margin: 0;
}

.avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    object-fit: cover;
}

.profile-head {
    display: flex;
    align-items: center;
    gap: 14px;
}

.profile-photo {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--line);
}

.span, fieldset {
    grid-column: 1 / -1;
}

fieldset {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
}

legend {
    color: var(--muted);
    font-weight: 700;
}

.check {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    width: auto;
    margin: 6px 12px 6px 0;
}

.check input {
    width: auto;
    min-height: auto;
}

.stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
    margin-bottom: 24px;
}

.stats-single {
    grid-template-columns: minmax(220px, 360px);
}

.stats article {
    position: relative;
    overflow: hidden;
    padding: 18px;
}

.stats article::before,
.card::before {
    content: "";
    width: 44px;
    height: 5px;
    background: linear-gradient(90deg, var(--gold), var(--green), var(--red), var(--blue));
    border-radius: 999px;
}

.stats strong {
    display: block;
    font-size: 30px;
}

.stats span {
    color: var(--muted);
}

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

.card {
    display: grid;
    gap: 8px;
    min-height: 120px;
    padding: 18px;
    color: var(--ink);
}

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

.privacy-note {
    margin-top: auto;
    transform: translateY(32px);
    padding: 16px 18px;
    color: #435064;
    background: #ffffff;
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 13px;
    line-height: 1.55;
}

.privacy-note strong {
    color: var(--ink);
}

table {
    width: 100%;
    border-collapse: collapse;
    overflow: hidden;
    font-size: 13px;
}

th, td {
    padding: 9px 10px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

th {
    color: var(--muted);
    background: #f9fbfc;
    font-size: 12px;
    text-transform: uppercase;
}

tbody tr:hover {
    background: #fbf7fc;
}

.clickable-row {
    cursor: pointer;
}

.flash {
    margin-bottom: 16px;
    padding: 12px 14px;
    border-radius: 8px;
    background: #e8f5ef;
    color: #0f5132;
}

.flash.error {
    background: #fde8e7;
    color: var(--danger);
}

.pdf-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin: 24px auto;
    width: min(900px, calc(100% - 32px));
}

.print-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-bottom: 18px;
}

.ps-print-sheet,
.ps-print-document {
    width: min(900px, 100%);
    margin: 0 auto 40px;
    padding: 28px 34px 34px;
    background: #fff;
    border: 1px solid var(--line);
}

.ps-first-page {
    position: relative;
    width: 297mm;
    height: 210mm;
    padding: 23mm 18mm 32mm 37mm;
    background-image: url("templates/portada_horizontal.png");
    background-size: 297mm 210mm;
    background-repeat: no-repeat;
    background-position: top left;
    border: 0;
    overflow: hidden;
    break-after: page;
    page-break-after: always;
}

.ps-continuation-page {
    width: 297mm;
    min-height: 210mm;
    padding: 18mm 18mm 12mm 37mm;
    background: #fff;
    box-sizing: border-box;
}

.ps-first-page h1 {
    margin: 8mm 0 7mm;
    text-align: center;
    font-size: 18px;
    letter-spacing: 0;
}

.ps-control-table {
    width: 194mm;
    margin: 0 auto;
    border-collapse: collapse;
    background: rgba(255, 255, 255, 0.92);
}

.ps-control-table th,
.ps-control-table td {
    border: 1px solid #111827;
    padding: 2px 6px;
    color: #111827;
    background: transparent;
    text-align: center;
    font-size: 11px;
    line-height: 1.15;
}

.ps-control-table th[rowspan] {
    width: 57%;
    font-size: 11px;
}

.ps-meta,
.ps-followup-table {
    width: 100%;
    border-collapse: collapse;
    background: rgba(255, 255, 255, 0.94);
}

.ps-meta {
    margin-bottom: 8mm;
}

.ps-meta th {
    width: 24%;
    color: #111827;
    background: #d9d9d9;
    text-align: left;
}

.ps-meta th,
.ps-meta td,
.ps-followup-table th,
.ps-followup-table td {
    border: 1px solid #111827;
    padding: 8px 9px;
    font-size: 12px;
}

.ps-followup-table th {
    color: #111827;
    background: #d9d9d9;
    text-align: center;
}

.ps-followup-table td:first-child,
.ps-followup-table td:nth-child(2),
.ps-followup-table td:last-child {
    text-align: center;
}

.ps-followup-table td {
    line-height: 1.35;
}

.sheet {
    width: min(900px, calc(100% - 32px));
    margin: 0 auto 40px;
    padding: 42px;
    background: #fff;
    border: 1px solid var(--line);
}

.sheet header {
    border-bottom: 2px solid var(--ink);
    margin-bottom: 22px;
}

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

.sheet-grid div {
    min-height: 58px;
    padding: 10px;
    border: 1px solid #cfd7df;
}

.sheet-grid b, .sheet-grid span {
    display: block;
}

.sheet-grid b {
    margin-bottom: 6px;
    font-size: 11px;
    text-transform: uppercase;
}

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

@media (max-width: 800px) {
    .topbar, .header-row {
        align-items: flex-start;
        flex-direction: column;
    }
    .form-grid, .stats, .grid, .sheet-grid, .detail-grid {
        grid-template-columns: 1fr;
    }
    .search {
        grid-template-columns: 1fr;
    }
    table {
        display: block;
        overflow-x: auto;
    }
}

@media print {
    @page {
        size: A4 landscape;
        margin: 18mm 0 12mm 0;
    }
    @page ps-cover {
        size: A4 landscape;
        margin: 0;
    }
    @page ps-continuation {
        size: A4 landscape;
        margin: 18mm 0 12mm 0;
    }
    html,
    body {
        width: 297mm;
        min-height: 210mm;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    body {
        background: #fff;
    }
    .pdf-actions {
        display: none;
    }
    .topbar,
    .print-actions,
    .flash {
        display: none;
    }
    .page {
        width: 100%;
        margin: 0;
    }
    .sheet {
        width: 100%;
        margin: 0;
        padding: 0;
        border: 0;
    }
    .ps-print-sheet,
    .ps-print-document {
        width: 297mm;
        margin: 0;
        padding: 0;
        border: 0;
    }
    .ps-first-page {
        page: ps-cover;
        width: 297mm;
        height: 210mm;
        padding: 23mm 18mm 32mm 37mm;
        background-image: url("templates/portada_horizontal.png");
        background-size: 297mm 210mm;
        background-repeat: no-repeat;
        background-position: top left;
        overflow: hidden;
    }
    .ps-continuation-page {
        page: ps-continuation;
        break-before: page;
        page-break-before: always;
        width: 297mm;
        min-height: 180mm;
        padding: 0 18mm 0 37mm;
    }
    .ps-followup-table thead {
        display: table-header-group;
    }
    .ps-followup-table tr {
        break-inside: avoid;
        page-break-inside: avoid;
    }
}

.offline-status { position: sticky; top: .5rem; z-index: 20; margin-bottom: 1rem; padding: .65rem .9rem; border-radius: .5rem; background: #fff3cd; color: #664d03; font-weight: 600; box-shadow: 0 2px 8px #0002; }
.offline-status[data-state="online"] { background: #d1e7dd; color: #0f5132; }
.offline-access-tools { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 1rem; }
.offline-access-tools .offline-status { flex: 1; margin: 0; }
.offline-pin-button { min-height: 36px; white-space: nowrap; }
.offline-controls,
.offline-content,
.offline-message { padding: 24px; }
.offline-controls { margin-bottom: 18px; }
.offline-shell-status,
.offline-form-note { margin: 8px 0 0; font-size: 14px; }
.offline-toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.offline-content > table,
.offline-content > div > table { width: 100%; margin-top: 16px; }
.offline-content > div > input { margin-top: 4px; }
.offline-details { display: grid; grid-template-columns: minmax(180px, .4fr) 1fr; gap: 10px 18px; margin-top: 20px; }
.offline-details dt { font-weight: 700; color: var(--muted); }
.offline-details dd { margin: 0; white-space: pre-wrap; }
.offline-form { width: 100%; margin-top: 16px; }
.offline-form-note { grid-column: 1 / -1; }
.offline-form button { width: fit-content; }
.offline-pin-form { width: min(420px, 100%); margin: 8vh auto; padding: 24px; display: grid; gap: 14px; }
.offline-pin-error { min-height: 20px; margin: 0; color: var(--danger); }

@media (max-width: 720px) {
    .offline-access-tools { align-items: stretch; flex-direction: column; }
    .offline-details { grid-template-columns: 1fr; gap: 4px; }
    .offline-details dd { margin-bottom: 12px; }
}
