/* ==========================================================================
   Sistema Médico - estilos base (mobile first)
   ========================================================================== */

:root {
    --c-primary: #0f766e;
    --c-primary-dark: #115e59;
    --c-primary-soft: #ccfbf1;
    --c-bg: #f1f5f9;
    --c-surface: #ffffff;
    --c-text: #0f172a;
    --c-muted: #64748b;
    --c-border: #e2e8f0;
    --c-danger: #b91c1c;
    --c-danger-bg: #fee2e2;
    --c-success: #166534;
    --c-success-bg: #dcfce7;
    --c-info: #1e40af;
    --c-info-bg: #dbeafe;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 12px rgba(15, 23, 42, .04);
    --sidebar-w: 240px;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: var(--c-text);
    background: var(--c-bg);
}

h1, h2, h3, p { margin: 0; }
a { color: var(--c-primary); }

/* ----- Botones ----- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .5rem;
    padding: .65rem 1.1rem;
    font: inherit;
    font-weight: 600;
    border: 1px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    text-decoration: none;
    transition: background .15s, border-color .15s, opacity .15s;
}
.btn:focus-visible, .input-addon:focus-visible, .menu-toggle:focus-visible {
    outline: 3px solid var(--c-primary-soft);
    outline-offset: 1px;
}
.btn-primary { background: var(--c-primary); color: #fff; }
.btn-primary:hover { background: var(--c-primary-dark); }
.btn-outline { background: transparent; color: var(--c-text); border-color: var(--c-border); }
.btn-outline:hover { background: var(--c-bg); }
.btn-sm { padding: .4rem .8rem; font-size: .875rem; }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .65; cursor: progress; }

/* ----- Formularios ----- */
.form { display: grid; gap: 1.1rem; }
.field { display: grid; gap: .35rem; }
.field label { font-size: .875rem; font-weight: 600; }

input[type="email"], input[type="password"], input[type="text"] {
    width: 100%;
    padding: .7rem .85rem;
    font: inherit;
    color: inherit;
    background: #fff;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
}
input:focus { outline: 3px solid var(--c-primary-soft); border-color: var(--c-primary); }

.input-group { display: flex; }
.input-group input { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.input-addon {
    padding: 0 .9rem;
    font: inherit;
    font-size: .875rem;
    font-weight: 600;
    color: var(--c-muted);
    background: var(--c-bg);
    border: 1px solid #cbd5e1;
    border-left: 0;
    border-radius: 0 8px 8px 0;
    cursor: pointer;
}
.input-addon:hover { color: var(--c-text); }

/* ----- Alertas ----- */
.alert {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    padding: .75rem 1rem;
    margin-bottom: 1rem;
    border-radius: 8px;
    font-size: .925rem;
}
.alert-error   { background: var(--c-danger-bg);  color: var(--c-danger); }
.alert-success { background: var(--c-success-bg); color: var(--c-success); }
.alert-info    { background: var(--c-info-bg);    color: var(--c-info); }
.alert-close {
    padding: 0 .25rem;
    font-size: 1.25rem;
    line-height: 1;
    color: inherit;
    background: none;
    border: 0;
    cursor: pointer;
    opacity: .7;
}
.alert-close:hover { opacity: 1; }

/* ----- Marca ----- */
.brand-mark {
    display: inline-grid;
    place-items: center;
    width: 32px;
    height: 32px;
    font-size: 1.4rem;
    font-weight: 700;
    line-height: 1;
    color: #fff;
    background: var(--c-primary);
    border-radius: 8px;
}
.brand-mark-lg { width: 52px; height: 52px; font-size: 2rem; border-radius: 12px; }

/* ==========================================================================
   Login (guest)
   ========================================================================== */
body.guest {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 1rem;
    background: linear-gradient(160deg, #ecfdf5 0%, #f1f5f9 55%, #e0f2fe 100%);
}
.auth-wrap { width: 100%; max-width: 420px; }
.auth-card {
    padding: 2rem 1.5rem;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: 14px;
    box-shadow: var(--shadow);
}
.auth-card-center { display: grid; gap: 1rem; justify-items: center; text-align: center; }
.auth-head { display: grid; gap: .5rem; justify-items: center; margin-bottom: 1.5rem; text-align: center; }
.auth-head h1 { font-size: 1.5rem; }
.auth-head p { color: var(--c-muted); font-size: .95rem; }
.auth-foot { margin-top: 1.5rem; font-size: .8rem; text-align: center; color: var(--c-muted); }
.error-code { font-size: 3.5rem; color: var(--c-primary); }

/* ==========================================================================
   App (autenticado)
   ========================================================================== */
.layout { min-height: 100vh; }

.sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 30;
    width: var(--sidebar-w);
    padding: 1.25rem 1rem;
    color: #e2e8f0;
    background: #0f172a;
    transform: translateX(-100%);
    transition: transform .2s ease;
}
.sidebar.open { transform: translateX(0); }
.sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(15, 23, 42, .5);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
}
.sidebar-backdrop.show { opacity: 1; pointer-events: auto; }

.brand { display: flex; align-items: center; gap: .65rem; margin-bottom: 1.75rem; font-weight: 700; color: #fff; }

.nav { display: grid; gap: .25rem; }
.nav-link {
    display: block;
    padding: .6rem .8rem;
    color: #cbd5e1;
    text-decoration: none;
    border-radius: 8px;
}
a.nav-link:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.nav-link.active { background: var(--c-primary); color: #fff; }
.nav-link.disabled { opacity: .45; cursor: not-allowed; }

.main { min-width: 0; }

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .75rem 1rem;
    background: var(--c-surface);
    border-bottom: 1px solid var(--c-border);
}
.topbar-title { flex: 1; font-size: 1.15rem; }

.menu-toggle {
    display: inline-flex;
    flex-direction: column;
    gap: 4px;
    padding: .55rem;
    background: none;
    border: 1px solid var(--c-border);
    border-radius: 8px;
    cursor: pointer;
}
.menu-toggle span { display: block; width: 18px; height: 2px; background: var(--c-text); }

.topbar-user { display: flex; align-items: center; gap: .75rem; }
.avatar {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    font-size: .8rem;
    font-weight: 700;
    color: var(--c-primary-dark);
    background: var(--c-primary-soft);
    border-radius: 50%;
}
.user-meta { display: none; line-height: 1.2; }
.user-meta small { color: var(--c-muted); }

.content { padding: 1rem; max-width: 1200px; }

.welcome { margin-bottom: 1.25rem; }
.welcome h2 { font-size: 1.4rem; }
.welcome p { color: var(--c-muted); }

.stats { display: grid; gap: .85rem; margin-bottom: 1.25rem; }
.stat {
    display: grid;
    gap: .15rem;
    padding: 1.1rem 1.25rem;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-left: 4px solid var(--c-primary);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.stat-label { font-size: .85rem; color: var(--c-muted); }
.stat-value { font-size: 2rem; line-height: 1.1; }

.card {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.card-head { padding: 1rem 1.25rem; border-bottom: 1px solid var(--c-border); }
.card-head h3 { font-size: 1.05rem; }
.empty { padding: 1.5rem 1.25rem; color: var(--c-muted); }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .925rem; }
.table th, .table td { padding: .75rem 1.25rem; text-align: left; white-space: nowrap; }
.table thead th {
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--c-muted);
    background: #f8fafc;
}
.table tbody tr + tr td { border-top: 1px solid var(--c-border); }

/* Tabla en formato tarjeta en móviles */
@media (max-width: 639px) {
    .table thead { display: none; }
    .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
    .table tr { padding: .6rem 1.25rem; }
    .table tr + tr { border-top: 1px solid var(--c-border); }
    .table td { display: flex; justify-content: space-between; gap: 1rem; padding: .25rem 0; white-space: normal; text-align: right; border: 0 !important; }
    .table td::before { content: attr(data-label); font-size: .8rem; color: var(--c-muted); text-align: left; }
}

/* Tablet */
@media (min-width: 640px) {
    .stats { grid-template-columns: repeat(3, 1fr); }
    .user-meta { display: grid; }
    .content { padding: 1.5rem; }
    .auth-card { padding: 2.25rem 2rem; }
}

/* Escritorio: sidebar fija */
@media (min-width: 960px) {
    .sidebar { transform: none; }
    .sidebar-backdrop { display: none; }
    .main { margin-left: var(--sidebar-w); }
    .menu-toggle { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
}

/* ==========================================================================
   Entrega 2 - Pacientes
   ========================================================================== */
select, textarea, input[type="tel"], input[type="date"] {
    width: 100%;
    padding: .7rem .85rem;
    font: inherit;
    color: inherit;
    background: #fff;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
}
textarea { resize: vertical; min-height: 5rem; }
select:focus, textarea:focus, input[type="tel"]:focus, input[type="date"]:focus {
    outline: 3px solid var(--c-primary-soft);
    border-color: var(--c-primary);
}

.sr-only {
    position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.muted { color: var(--c-muted); }
.req { color: var(--c-danger); }
.pre { white-space: pre-line; }

.btn-danger { background: var(--c-danger); color: #fff; }
.btn-danger:hover { background: #991b1b; }

.page-head {
    display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between;
    gap: .75rem; margin-bottom: 1.1rem;
}
.page-head h2 { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; font-size: 1.4rem; }
.head-actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.head-actions form { margin: 0; }

.toolbar { display: grid; gap: .6rem; margin-bottom: 1rem; }
.toolbar .btn { width: 100%; }

.badge {
    display: inline-block; padding: .15rem .6rem; font-size: .75rem; font-weight: 600;
    border-radius: 999px; vertical-align: middle;
}
.badge-ok  { background: var(--c-success-bg); color: var(--c-success); }
.badge-off { background: #e2e8f0; color: #475569; }

.row-link { color: inherit; text-decoration: none; }
.row-link:hover { color: var(--c-primary); text-decoration: underline; }
.actions { display: flex; gap: .4rem; }

.pagination {
    display: flex; flex-wrap: wrap; justify-content: center; gap: .35rem;
    padding: 1rem; border-top: 1px solid var(--c-border);
}
.pagination a, .pagination .current {
    min-width: 2.25rem; padding: .4rem .7rem; text-align: center;
    border: 1px solid var(--c-border); border-radius: 8px; text-decoration: none;
}
.pagination a { color: var(--c-text); }
.pagination a:hover { background: var(--c-bg); }
.pagination .current { background: var(--c-primary); border-color: var(--c-primary); color: #fff; font-weight: 600; }

/* Formulario */
.form-card { padding: 0; }
.form-section { margin: 0; padding: 1.25rem; border: 0; border-bottom: 1px solid var(--c-border); min-width: 0; }
.form-section legend { padding: 0; margin-bottom: 1rem; font-weight: 700; font-size: 1.02rem; float: left; width: 100%; }
.form-section legend + * { clear: both; }
.form-grid { display: grid; gap: 1rem; }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--c-danger); }
.field-error { color: var(--c-danger); font-size: .82rem; }
.form-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: .6rem; padding: 1rem 1.25rem; }

/* Ficha */
.detail-grid { display: grid; gap: 1rem; align-items: start; }
.detail { margin: 0; padding: .5rem 1.25rem 1rem; display: grid; gap: .75rem; }
.detail dt { font-size: .78rem; color: var(--c-muted); text-transform: uppercase; letter-spacing: .04em; }
.detail dd { margin: 0; }

.content .auth-card-center { margin: 2rem auto; max-width: 460px; }

@media (min-width: 640px) {
    .toolbar { grid-template-columns: 1fr 180px auto auto; }
    .toolbar .btn { width: auto; }
    .form-grid { grid-template-columns: repeat(2, 1fr); }
    .field-wide { grid-column: span 2; }
    .field-full { grid-column: 1 / -1; }
    .detail { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1100px) {
    .detail-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 639px) {
    .actions { justify-content: flex-end; }
}

/* ==========================================================================
   Entrega 3 - Usuarios y perfil
   ========================================================================== */
.user-link { display: flex; align-items: center; gap: .75rem; color: inherit; text-decoration: none; border-radius: 8px; }
.user-link:hover strong { color: var(--c-primary); }
.user-link:focus-visible { outline: 3px solid var(--c-primary-soft); outline-offset: 2px; }

.badge-warn { background: #fef3c7; color: #92400e; }
.badge-role-admin     { background: #ede9fe; color: #5b21b6; }
.badge-role-medico    { background: #dbeafe; color: #1e40af; }
.badge-role-recepcion { background: #fce7f3; color: #9d174d; }

.hint { color: var(--c-muted); font-size: .8rem; }
input:disabled { background: #f1f5f9; color: var(--c-muted); cursor: not-allowed; }

.form-card + .form-card, .form-card + .card { margin-top: 1rem; }
.form-section-last { border-bottom: 0; }
.detail-inline { padding: 0 0 1rem; }
.form-section .head-actions { align-items: center; }
.form-section .head-actions p { margin: 0; }

/* Ajustes de alineación en formularios */
.field { align-content: start; }
.field > .js-gen-pass { justify-self: start; }

/* ==========================================================================
   Entrega 4 - Consultas médicas
   ========================================================================== */
.alert-warning { background: #fef3c7; color: #92400e; }

.patient-banner { display: grid; gap: .5rem; padding: .9rem 1.25rem; margin-bottom: 1rem; }
.patient-banner .allergy { margin: 0; }

.chip {
    display: inline-flex; align-items: center; gap: .5rem; margin: 0 0 1rem; padding: .3rem .4rem .3rem .85rem;
    background: var(--c-primary-soft); color: var(--c-primary-dark); border-radius: 999px; font-size: .9rem;
}
.chip a { display: grid; place-items: center; width: 1.4rem; height: 1.4rem; text-decoration: none; color: inherit; border-radius: 50%; }
.chip a:hover { background: rgba(15, 118, 110, .15); }

.filters { display: grid; gap: .75rem; padding: 1rem 1.25rem; margin-bottom: 1rem; }
.filters-actions { display: flex; gap: .5rem; align-items: end; }

.card-head-flex { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; }
.card-wide { grid-column: 1 / -1; }
.section-gap { margin-top: 1.25rem; }
.cell-wrap { white-space: normal !important; min-width: 12rem; max-width: 24rem; }

.stack { display: grid; gap: 1rem; }
.detail-one { grid-template-columns: 1fr !important; }

.vitals { display: grid; grid-template-columns: repeat(2, 1fr); gap: .75rem; padding: 1rem 1.25rem 1.25rem; }
.vital { display: grid; gap: .1rem; padding: .7rem .9rem; background: #f8fafc; border: 1px solid var(--c-border); border-radius: 8px; }
.vital span { font-size: .75rem; color: var(--c-muted); text-transform: uppercase; letter-spacing: .04em; }
.vital strong { font-size: 1.05rem; }

.imc-line { margin: .75rem 0 0; }
.print-only { display: none; }

@media (min-width: 640px) {
    .filters { grid-template-columns: 2fr 1.2fr 1fr 1fr; align-items: end; }
    .filters .field-grow { grid-column: 1 / 2; }
    .filters-actions { grid-column: 1 / -1; }
    .vitals { grid-template-columns: repeat(4, 1fr); }
    .form-grid-4 { grid-template-columns: repeat(2, 1fr); }
    .stats { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
}
@media (min-width: 960px) {
    .form-grid-4 { grid-template-columns: repeat(4, 1fr); }
    .filters { grid-template-columns: 2fr 1.2fr 1fr 1fr auto; }
    .filters-actions { grid-column: auto; }
}

/* Impresión: solo el contenido de la consulta */
@media print {
    body { background: #fff; }
    .sidebar, .sidebar-backdrop, .topbar, .head-actions, .no-print, .alert-close { display: none !important; }
    .main { margin-left: 0 !important; }
    .content { padding: 0; max-width: none; }
    .card, .patient-banner { box-shadow: none; border-color: #999; break-inside: avoid; }
    .print-only { display: block; margin-bottom: .75rem; font-size: 1.1rem; }
    .vitals { grid-template-columns: repeat(4, 1fr); }
}

/* Correcciones Entrega 4 */
input[type="datetime-local"] {
    width: 100%;
    padding: .7rem .85rem;
    font: inherit;
    color: inherit;
    background: #fff;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
}
input[type="datetime-local"]:focus { outline: 3px solid var(--c-primary-soft); border-color: var(--c-primary); }

.cell-wrap { min-width: 9rem !important; max-width: 16rem !important; }
@media (min-width: 640px) {
    .table th, .table td { padding: .75rem .9rem; }
    .table th:first-child, .table td:first-child { padding-left: 1.25rem; }
    .table th:last-child, .table td:last-child { padding-right: 1.25rem; }
}

/* Tablas: contener el texto oculto (sr-only) dentro del scroll y permitir que las celdas largas se ajusten */
.table-wrap { position: relative; }
@media (min-width: 640px) {
    .table th, .table td { white-space: normal; }
    .table td.actions,
    .table td[data-label="Fecha"],
    .table td[data-label="Documento"],
    .table td[data-label="Teléfono"],
    .table td[data-label="Estado"],
    .table td[data-label="Edad"],
    .table td[data-label="Registrado"],
    .table td[data-label="Último acceso"] { white-space: nowrap; }
    .table td.actions { flex-wrap: nowrap; }
}

/* Tablet: se oculta una columna secundaria para que las acciones queden visibles */
@media (min-width: 640px) and (max-width: 959px) {
    .col-opt { display: none; }
}
