:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #dfe4ea;
  --text: #1d2733;
  --text-dim: #62717f;
  --brand: #1e6fb8;
  --brand-dark: #0b3b63;
  --accent: #e8f1f8;
  --danger: #c0392b;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* Garantiza que el atributo nativo "hidden" siempre gane, sin importar que
   algun otro selector (ej. .btn-nueva-consulta) fije su propio "display".
   Sin esto, ocultar un elemento por rol (el.hidden = true) no lo ocultaba
   visualmente aunque el acceso ya estuviera bloqueado en el servidor. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 240px;
  flex: 0 0 240px;
  background: var(--brand-dark);
  color: #eafffd;
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: 100%;
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 6px;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: #ffffff22;
  padding: 5px;
  box-sizing: border-box;
  object-fit: contain;
  display: block;
}

.brand-clinica { font-size: 11px; font-weight: 600; letter-spacing: 0.3px; color: #29d0d6; line-height: 1.2; margin: 0 0 1px; }
.brand-name { font-weight: 600; font-size: 15px; }
.brand-sub { font-size: 11px; color: #bcece7; }

#nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-group { margin-top: 6px; }

.nav-group-label {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #82c9c3;
  margin: 8px 10px 4px;
  padding: 4px 2px;
  border: none;
  background: none;
  cursor: pointer;
  text-align: left;
}
.nav-group-label:hover { color: #cdf1ed; }

.nav-group-caret {
  display: inline-block;
  font-size: 10px;
  transition: transform 0.12s ease;
}
.nav-group.expanded .nav-group-caret { transform: rotate(90deg); }

/* display:none por defecto (no solo el atributo [hidden]): un selector de
   clase sin condicionar a [hidden] pisaria la regla nativa del navegador
   por tener la misma especificidad y venir despues (mismo problema que
   tuvo el dialog de consulta, ver mas abajo). Se muestra solo cuando el
   grupo esta .expanded. */
.nav-group-items {
  display: none;
  flex-direction: column;
  gap: 2px;
}
.nav-group.expanded .nav-group-items {
  display: flex;
}

.nav-item {
  display: block;
  padding: 9px 12px;
  border-radius: 8px;
  color: #dff5f2;
  text-decoration: none;
  font-size: 13.5px;
  cursor: pointer;
  border: none;
  background: none;
  text-align: left;
  width: 100%;
}

.nav-item:hover { background: #ffffff14; }
.nav-item.active { background: var(--brand); color: #fff; font-weight: 600; }

/* Usuario con sesion iniciada + cerrar sesion, al fondo del menu lateral. */
.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid #ffffff22;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.sidebar-user { font-size: 12px; color: #cdf1ed; line-height: 1.4; }
.sidebar-user b { color: #fff; display: block; font-size: 13px; }
.btn-logout {
  background: none;
  border: 1px solid #ffffff33;
  color: #eafffd;
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 12.5px;
  cursor: pointer;
  text-align: left;
}
.btn-logout:hover { background: #ffffff14; }

.content {
  flex: 1;
  padding: 26px 34px;
  min-width: 0;
  height: 100%;
  overflow-y: auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.topbar h1 { font-size: 20px; margin: 0; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-ghost { background: transparent; color: var(--text-dim); border: 1px solid var(--border); }
.btn-ghost:hover { background: #eef1f4; }
.btn-icon {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 6px;
  color: var(--text-dim);
}
.btn-icon:hover { background: #eef1f4; color: var(--text); }
.btn-icon.danger:hover { color: var(--danger); background: #fbeceb; }

.filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  font-size: 13px;
}

.filter-bar select {
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.table-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
}

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

thead th {
  text-align: left;
  padding: 10px 14px;
  background: var(--accent);
  color: var(--brand-dark);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody td {
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafcfc; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: #eef1f4;
  color: var(--text-dim);
}
.badge.ok { background: #e4f7ec; color: #1e7a45; }
.badge.warn { background: #fdf1de; color: #a9640f; }
.badge.bad { background: #fbeceb; color: var(--danger); }

.empty-state {
  padding: 26px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}

.row-actions { display: flex; gap: 2px; white-space: nowrap; }

dialog#form-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 22px 24px;
  width: min(640px, 92vw);
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
dialog#form-dialog::backdrop { background: rgba(20, 30, 35, 0.45); }

#form-title { margin: 0 0 14px; font-size: 17px; }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 14px;
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 4px;
}

.field { display: flex; flex-direction: column; gap: 4px; }
.field.full { grid-column: 1 / -1; }

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

.field input, .field select, .field textarea {
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
  font-family: inherit;
}

.field textarea { resize: vertical; min-height: 54px; }
.field input[readonly] { background: var(--accent); color: var(--brand-dark); font-weight: 600; }

.form-error {
  background: #fbeceb;
  color: var(--danger);
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 12.5px;
  margin-top: 10px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.autocomplete-results {
  border: 1px solid var(--border);
  border-radius: 7px;
  margin-top: 4px;
  max-height: 140px;
  overflow-y: auto;
  font-size: 12.5px;
}
.autocomplete-results div {
  padding: 6px 9px;
  cursor: pointer;
}
.autocomplete-results div:hover { background: var(--accent); }

/* ------------------------------------------------------------------
   Nueva consulta (flujo unificado de consulta medica)
   ------------------------------------------------------------------ */
.btn-nueva-consulta {
  display: block;
  width: 100%;
  margin: 4px 0 14px;
  padding: 11px 12px;
  border: none;
  border-radius: 8px;
  background: #ffffff;
  color: var(--brand-dark);
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
}
.btn-nueva-consulta:hover { background: #eafaf8; }

dialog#consulta-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  width: min(880px, 94vw);
  max-height: 90vh;
  overflow-y: auto;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
/* display:flex debe aplicarse solo cuando el dialog esta abierto: si se
   pone sin condicionar a [open], anula la regla nativa del navegador
   "dialog:not([open]) { display:none }" y el dialog queda visible en
   pantalla aunque se llame a dialog.close() (el boton "Cancelar" parecia
   no funcionar por esto). */
dialog#consulta-dialog[open] {
  display: flex;
}
dialog#consulta-dialog::backdrop { background: rgba(20, 30, 35, 0.45); }

.consulta-header {
  padding: 20px 26px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.consulta-header h2 { margin: 0 0 4px; font-size: 18px; }
.consulta-sub { margin: 0; font-size: 12.5px; color: var(--text-dim); }

.consulta-body {
  padding: 4px 26px;
  max-height: 62vh;
  overflow-y: auto;
  flex-shrink: 1;
}

.consulta-section {
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
}
.consulta-section:last-child { border-bottom: none; }
.consulta-section h3 {
  margin: 0 0 10px;
  font-size: 13.5px;
  color: var(--brand-dark);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.consulta-section .hint {
  font-size: 11.5px;
  color: var(--text-dim);
  text-transform: none;
  letter-spacing: normal;
  font-weight: 400;
}
p.hint { font-size: 12.5px; color: var(--text-dim); margin: 0 0 8px; }

.antecedentes-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.antecedente-item {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 7px 10px;
  background: var(--accent);
  border-radius: 7px;
  font-size: 12.5px;
}
.antecedente-item .badge { flex-shrink: 0; }

.antecedente-add-row {
  display: grid;
  grid-template-columns: 160px 1fr auto;
  gap: 8px;
}
.antecedente-add-row select,
.antecedente-add-row input {
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
  font-family: inherit;
}

.dyn-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.dyn-row {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
  padding: 12px 40px 12px 12px;
  background: var(--accent);
  border-radius: 8px;
}
.dyn-row .field { grid-column: auto; }
.dyn-row .field.full { grid-column: 1 / -1; }
.dyn-row .dyn-remove {
  position: absolute;
  top: 8px;
  right: 8px;
}
.dyn-row .autocomplete-results { background: var(--panel); position: relative; z-index: 2; }

.nc-receta-obs { margin-top: 4px; }

.diag-check-field { justify-content: flex-start; }
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  font-weight: 400;
  color: var(--text);
  padding: 7px 0;
}
.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--brand);
}

/* ------------------------------------------------------------------
   Historial de consultas
   ------------------------------------------------------------------ */
.btn-historial {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.35);
  color: #ffffff;
}
.btn-historial:hover { background: #ffffff1f; }

dialog#historial-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  width: min(820px, 94vw);
  max-height: 90vh;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
dialog#historial-dialog::backdrop { background: rgba(20, 30, 35, 0.45); }
dialog#historial-dialog .consulta-body { max-height: 68vh; }

dialog#documentos-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  width: min(760px, 94vw);
  max-height: 90vh;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
dialog#documentos-dialog::backdrop { background: rgba(20, 30, 35, 0.45); }
dialog#documentos-dialog .consulta-body { max-height: 72vh; }

dialog#ginecologia-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  width: min(760px, 94vw);
  max-height: 90vh;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
dialog#ginecologia-dialog::backdrop { background: rgba(20, 30, 35, 0.45); }
dialog#ginecologia-dialog .consulta-body { max-height: 72vh; }

dialog#calculadoras-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  width: min(760px, 94vw);
  max-height: 90vh;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
dialog#calculadoras-dialog::backdrop { background: rgba(20, 30, 35, 0.45); }
dialog#calculadoras-dialog .consulta-body { max-height: 72vh; }

.calc-resultado {
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 8px;
  background: var(--bg-alt, #f2f6f5);
  border: 1px solid var(--border);
}
.calc-resultado .calc-headline { font-size: 15px; font-weight: 700; margin: 0 0 4px; color: var(--brand-dark); }
.calc-resultado .calc-sub { font-size: 12.5px; color: var(--text-dim); margin: 0 0 10px; }
.calc-badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  margin-left: 6px;
}
.calc-badge.ok { background: #e3f4ec; color: #1f7a4d; }
.calc-badge.warn { background: #fdf1da; color: #9a6a11; }
.calc-badge.danger { background: #fbe4e4; color: #b3261e; }
.calc-chart-wrap { margin-top: 8px; overflow-x: auto; }
.calc-chart-legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 11.5px; color: var(--text-dim); margin-top: 6px; }
.calc-chart-legend span { display: inline-flex; align-items: center; gap: 5px; }
.calc-chart-legend i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

.hist-subheading {
  margin: 4px 0 10px;
  font-size: 13.5px;
  color: var(--brand-dark);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.hist-item {
  border: 1px solid var(--border);
  border-radius: 9px;
  margin-bottom: 10px;
  overflow: hidden;
}
.hist-item-header {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 11px 14px;
  background: var(--accent);
  border: none;
  cursor: pointer;
  text-align: left;
  font-size: 13px;
  font-family: inherit;
}
.hist-item-header:hover { background: #dff2f0; }
.hist-date { font-weight: 700; color: var(--brand-dark); white-space: nowrap; }
.hist-motivo { flex: 1; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-medico { color: var(--text-dim); font-size: 12px; white-space: nowrap; }
.hist-toggle { color: var(--text-dim); }

.hist-item-body { padding: 14px 16px; display: flex; flex-direction: column; gap: 12px; }
.hist-item-body[hidden] { display: none; }
.hist-block h4 {
  margin: 0 0 4px;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-dim);
}
.hist-block p { margin: 0; font-size: 13px; white-space: pre-wrap; }
.hist-vitals { display: flex; flex-wrap: wrap; gap: 8px 18px; font-size: 12.5px; }
.hist-vitals span b { color: var(--text-dim); font-weight: 600; margin-right: 4px; }

.hist-diag-item, .hist-sub-item {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 6px 9px;
  background: var(--accent);
  border-radius: 7px;
  font-size: 12.5px;
  margin-bottom: 5px;
}
.hist-sub-group { margin-bottom: 4px; }
.hist-sub-group-title { font-size: 12px; font-weight: 600; color: var(--text-dim); margin: 0 0 4px; }
.hist-sub-item a, .hist-sub-item .btn-archivo { color: var(--brand-dark); font-weight: 600; text-decoration: underline; white-space: nowrap; }
.hist-sub-item .btn-archivo { background: none; border: none; cursor: pointer; padding: 0; font-size: inherit; font-family: inherit; }
.hist-sub-item { flex-wrap: wrap; }

/* ------------------------------------------------------------------
   Panel de exito + impresion (Nueva consulta)
   ------------------------------------------------------------------ */
.form-success {
  background: #e4f7ec;
  border: 1px solid #bfe8cf;
  border-radius: 8px;
  padding: 12px 14px;
  margin-top: 12px;
}
.form-success p {
  margin: 0 0 8px;
  font-size: 13px;
  color: #1e7a45;
  font-weight: 600;
}
.form-print-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.hist-sub-group .btn-print-inline {
  margin-top: 4px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 9px;
  font-size: 11.5px;
  cursor: pointer;
  color: var(--brand-dark);
}
.hist-sub-group .btn-print-inline:hover { background: var(--accent); }
