:root, :root[data-theme="light"] {
  --bg: #f2f5f8;
  --surface: #ffffff;
  --surface-2: #f6f8fa;
  --surface-3: #eef2f5;
  --border: #dde4ea;
  --text: #14222d;
  --muted: #607282;
  --primary: #0b6f8c;
  --primary-hover: #095c75;
  --primary-soft: #e2f2f7;
  --on-primary: #ffffff;
  --ok: #18865a; --ok-soft: #e3f5ec;
  --warn: #b26b00; --warn-soft: #fdf2e0;
  --err: #b42339; --err-soft: #fcebee;
  --info: #2a62b0; --info-soft: #e8f0fb;
  --shadow: 0 1px 2px rgba(16, 34, 46, .05), 0 6px 20px rgba(16, 34, 46, .06);
  --sidebar: #0f1d27;
  --sidebar-text: #c4d2dc;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0b1218;
  --surface: #121c24;
  --surface-2: #16222c;
  --surface-3: #1b2a35;
  --border: #22323f;
  --text: #e4edf3;
  --muted: #8ea2b2;
  --primary: #2fb3d6;
  --primary-hover: #55c5e3;
  --primary-soft: rgba(47, 179, 214, .13);
  --on-primary: #04141b;
  --ok: #3ccf8e; --ok-soft: rgba(60, 207, 142, .13);
  --warn: #f2b45a; --warn-soft: rgba(242, 180, 90, .13);
  --err: #ff8a9b; --err-soft: rgba(255, 138, 155, .12);
  --info: #8ab8f5; --info-soft: rgba(138, 184, 245, .12);
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .3);
  --sidebar: #0a1116;
  --sidebar-text: #aebdc8;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14.5px; line-height: 1.45;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-variant-numeric: tabular-nums; letter-spacing: .02em; }
.nowrap { white-space: nowrap; }
.center { text-align: center; }
.right { text-align: right; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex: none; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
  border-right: 1px solid rgba(255, 255, 255, .05); z-index: 40;
}
.sb-brand { display: flex; align-items: center; gap: 12px; padding: 20px 18px; border-bottom: 1px solid rgba(255, 255, 255, .06); }
.sb-brand img { border-radius: 10px; }
.sb-brand strong { display: block; color: #fff; font-size: 17px; letter-spacing: .06em; }
.sb-brand small { font-size: 12px; opacity: .75; }
.sb-nav { padding: 14px 10px; display: grid; gap: 4px; }
.sb-nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px;
  color: var(--sidebar-text); text-decoration: none; font-weight: 500;
}
.sb-nav a:hover { background: rgba(255, 255, 255, .05); color: #fff; }
.sb-nav a.active { background: rgba(47, 179, 214, .16); color: #6fd3ee; }
.sb-foot { margin-top: auto; padding: 14px; border-top: 1px solid rgba(255, 255, 255, .06); }
.me { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; min-width: 0; }
.me-txt { min-width: 0; }
.me-txt strong { display: block; color: #fff; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-txt small { font-size: 12px; opacity: .75; }
.sb-actions { display: flex; gap: 8px; }
.sidebar .icon-btn { border-color: rgba(255, 255, 255, .1); background: rgba(255, 255, 255, .04); color: var(--sidebar-text); }
.sidebar .icon-btn:hover { color: #fff; border-color: rgba(255, 255, 255, .25); }

.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 60px; display: flex; align-items: center; gap: 12px; padding: 0 24px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
.topbar-right { margin-left: auto; }
.menu-btn { display: none; }
.page { padding: 22px 24px 40px; max-width: 1500px; width: 100%; }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: rgba(47, 179, 214, .18); color: #6fd3ee; font-weight: 700; font-size: 14px;
}
.avatar.sm { width: 32px; height: 32px; font-size: 12px; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); display: inline-grid; place-items: center; cursor: pointer; text-decoration: none;
}
.icon-btn:hover { border-color: var(--primary); color: var(--primary); }
.icon-btn .i-sun { display: none; }
:root[data-theme="light"] .icon-btn .i-sun { display: block; }
:root[data-theme="light"] .icon-btn .i-moon { display: none; }

/* ---------- Componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 16px; border-radius: 10px; border: 1px solid transparent;
  font: inherit; font-weight: 600; font-size: 14px; cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: .6; cursor: progress; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--primary); color: var(--primary); }
.btn-block { width: 100%; height: 46px; font-size: 15px; }
.btn-sm { height: 30px; padding: 0 11px; font-size: 12.5px; border-radius: 8px; gap: 6px; }
.btn-sm svg { width: 15px; height: 15px; }
.btn-view { background: var(--primary-soft); color: var(--primary); margin-top: 8px; }
.btn-view:hover { background: var(--primary); color: var(--on-primary); }
.btn-note { background: var(--surface-3); color: var(--text); border-color: var(--border); letter-spacing: .02em; }
.btn-note:hover { border-color: var(--primary); color: var(--primary); }
.btn-upload { background: transparent; border: 1px dashed var(--border); color: var(--muted); }
.btn-upload:hover { border-color: var(--primary); color: var(--primary); }
.btn-upload.busy { opacity: .6; pointer-events: none; }
.badge { min-width: 19px; height: 19px; padding: 0 5px; border-radius: 10px; background: var(--primary); color: var(--on-primary); font-size: 11px; display: inline-grid; place-items: center; }

.tag { display: inline-block; font-size: 10.5px; font-weight: 700; letter-spacing: .05em; padding: 1px 6px; border-radius: 5px; background: var(--surface-3); border: 1px solid var(--border); margin-right: 4px; color: var(--text); }

.alert { padding: 12px 14px; border-radius: 10px; margin-bottom: 16px; font-size: 14px; }
.alert-error { background: var(--err-soft); color: var(--err); }
.alert-warn { background: var(--warn-soft); color: var(--warn); }
.alert-ok { background: var(--ok-soft); color: var(--ok); }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input, select { height: 38px; padding: 0 12px; }
textarea { padding: 10px 12px; resize: vertical; width: 100%; min-height: 96px; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field { display: grid; gap: 6px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.field input, .field select { width: 100%; }

/* ---------- Estadísticas y filtros ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.stat span { display: block; font-size: 12.5px; color: var(--muted); font-weight: 500; }
.stat strong { font-size: 24px; font-weight: 700; letter-spacing: -.02em; }
.c-ok { color: var(--ok); } .c-warn { color: var(--warn); } .c-info { color: var(--info); }

.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; padding: 14px; margin-bottom: 16px; }
.fl { display: grid; gap: 4px; }
.fl span { font-size: 12px; color: var(--muted); font-weight: 600; }
.fl-search { position: relative; flex: 1 1 260px; }
.fl-search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.fl-search input { width: 100%; padding-left: 36px; }
.fl-actions { display: flex; gap: 8px; }

/* ---------- Tabla ---------- */
.table-card { overflow: hidden; }
.grid { width: 100%; border-collapse: collapse; }
.grid th {
  text-align: left; font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); background: var(--surface-2); padding: 11px 14px; border-bottom: 1px solid var(--border);
}
.grid td { padding: 13px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover td { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
.grid tr.is-done .pac-name::after { content: "Revisado"; margin-left: 8px; font-size: 10.5px; font-weight: 700; color: var(--ok); background: var(--ok-soft); padding: 2px 7px; border-radius: 6px; vertical-align: 2px; }
.grid tr.is-off td { opacity: .55; }
.pac-name { font-weight: 650; font-size: 15px; }
.pac-sub { font-size: 12.5px; color: var(--muted); margin-top: 3px; }
.c-pac { min-width: 220px; }
.c-inf { min-width: 170px; }
.sel-asig { min-width: 170px; max-width: 230px; height: 34px; font-size: 13.5px; }

.inf-list { display: grid; gap: 4px; margin-bottom: 6px; }
.inf-list:empty { display: none; }
.inf-item { display: flex; align-items: center; gap: 4px; }
.inf-item a { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; font-size: 13px; font-weight: 500; max-width: 200px; }
.inf-item a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inf-item a:hover span { text-decoration: underline; }
.x-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 17px; line-height: 1; padding: 2px 5px; border-radius: 6px; }
.x-btn:hover { color: var(--err); background: var(--err-soft); }

.check { display: inline-flex; cursor: pointer; }
.check input { position: absolute; opacity: 0; pointer-events: none; }
.check-box {
  width: 26px; height: 26px; border-radius: 8px; border: 1.5px solid var(--border); background: var(--surface-2);
  display: grid; place-items: center; transition: background .15s, border-color .15s;
}
.check-box svg { width: 16px; height: 16px; stroke-width: 2.6; color: #fff; opacity: 0; transform: scale(.6); transition: .15s; }
.check input:checked + .check-box { background: var(--ok); border-color: var(--ok); }
.check input:checked + .check-box svg { opacity: 1; transform: none; }
:root[data-theme="dark"] .check input:checked + .check-box svg { color: #062016; }
.check input:focus-visible + .check-box { box-shadow: 0 0 0 3px var(--primary-soft); }
.check input:disabled + .check-box { cursor: not-allowed; opacity: .55; }
.check input:not(:disabled):hover + .check-box { border-color: var(--ok); }

.empty { padding: 50px 20px; text-align: center; color: var(--muted); }
.empty svg { width: 40px; height: 40px; opacity: .6; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 14px; gap: 10px; flex-wrap: wrap; }
.pager div { display: flex; gap: 8px; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: none; place-items: center; padding: 16px; background: rgba(4, 10, 14, .62); backdrop-filter: blur(3px); }
.modal.open { display: grid; }
.modal-card { width: 100%; max-width: 560px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 20px 60px rgba(0, 0, 0, .45); animation: pop .16s ease-out; }
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: 0; } }
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 18px 20px 12px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-head p { margin: 2px 0 0; font-size: 13.5px; }
.modal-body { padding: 16px 20px 20px; overflow-y: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 14px; }
.notes { display: grid; gap: 10px; margin-bottom: 16px; }
.notes:empty { display: none; }
.note { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.note-meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.note-meta strong { color: var(--text); font-weight: 600; }
.note-txt { white-space: pre-wrap; overflow-wrap: anywhere; }
.notes-empty { color: var(--muted); font-size: 13.5px; margin-bottom: 14px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 500; font-size: 14px;
  transition: .2s; z-index: 200; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--err); color: #fff; }

/* ---------- Usuarios ---------- */
.page-actions { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-actions p { margin: 0; max-width: 720px; font-size: 13.5px; }
.form-card { padding: 20px; margin-bottom: 18px; }
.form-card h2 { margin: 0 0 16px; font-size: 16px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px 18px; align-items: end; }
.user-cell { display: flex; align-items: center; gap: 10px; }
.role { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 20px; background: var(--surface-3); }
.role-admin { background: var(--err-soft); color: var(--err); }
.role-medico { background: var(--primary-soft); color: var(--primary); }
.role-tecnico { background: var(--warn-soft); color: var(--warn); }
.role-secretaria { background: var(--info-soft); color: var(--info); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.dot.on { background: var(--ok); } .dot.off { background: var(--muted); }
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; height: 38px; font-weight: 500; }
.switch input { position: absolute; opacity: 0; }
.sw { width: 40px; height: 22px; border-radius: 11px; background: var(--surface-3); border: 1px solid var(--border); position: relative; transition: .15s; }
.sw::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: .15s; }
.switch input:checked + .sw { background: var(--ok); border-color: var(--ok); }
.switch input:checked + .sw::after { left: 20px; background: #fff; }

/* ---------- Login ---------- */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(900px 500px at 80% -10%, rgba(47, 179, 214, .16), transparent 60%), radial-gradient(700px 400px at 0% 100%, rgba(60, 90, 160, .14), transparent 60%), var(--bg); }
.login { width: 100%; max-width: 400px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 30px 28px; box-shadow: var(--shadow); }
.login-card form { display: grid; gap: 14px; }
.login-card input { height: 44px; }
.login-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 24px; }
.login-brand img { border-radius: 13px; }
.login-brand h1 { margin: 0; font-size: 22px; letter-spacing: .08em; }
.login-brand p { margin: 0; color: var(--muted); font-size: 14px; }
.login-foot { text-align: center; color: var(--muted); font-size: 12.5px; margin: 18px 0 0; }

/* ---------- Responsive ---------- */
.sb-backdrop { display: none; }
@media (max-width: 960px) {
  .sidebar { position: fixed; left: 0; top: 0; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0, 0, 0, .4); }
  .sidebar.open { transform: none; }
  .sb-backdrop.open { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 30; }
  .menu-btn { display: inline-grid; }
  .topbar { padding: 0 16px; }
  .page { padding: 16px 16px 32px; }
}
@media (max-width: 860px) {
  .grid thead { display: none; }
  .grid, .grid tbody, .grid tr, .grid td { display: block; width: 100%; }
  .grid tr { padding: 12px 14px; border-bottom: 1px solid var(--border); }
  .grid tbody tr:last-child { border-bottom: 0; }
  .grid td { border: 0; padding: 6px 0; display: flex; gap: 12px; align-items: flex-start; text-align: left; }
  .grid td::before { content: attr(data-label); flex: 0 0 96px; font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding-top: 3px; }
  .grid td.c-pac { display: block; padding-bottom: 8px; }
  .grid td.c-pac::before { display: none; }
  .grid td.right { justify-content: flex-end; }
  .grid td.right::before { display: none; }
  .grid td > * { min-width: 0; }
  .sel-asig { max-width: none; flex: 1; }
  .filters .fl { flex: 1 1 140px; }
  .filters .fl input, .filters .fl select { width: 100%; }
  .fl-actions { width: 100%; }
  .fl-actions .btn-primary { flex: 1; }
}
