/* =========================================================================
   Nutriwoda — Sistema de gestión clínica nutricional
   Estética Apple: tipografía SF, glassmorphism, azul sistema, mucho aire.
   ========================================================================= */

:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #fafafa;
  --text: #1d1d1f;
  --text-2: #5c5c61;       /* oscurecido: pasa WCAG AA */
  --text-3: #6b6b70;       /* oscurecido de #86868b (3.3:1) a ~5.4:1 para AA */
  --border: #d2d2d7;
  --border-light: #e8e8ed;

  --blue: #0071e3;
  --blue-dark: #0058b0;
  --green: #34c759;
  --green-dark: #1d8c3e;
  --teal: #30b0c7;
  --lime: #6aa800;
  --orange: #ff9500;
  --amber: #d68000;
  --red: #ff3b30;
  --red-dark: #c4271d;
  --purple: #af52de;
  --yellow: #ffcc00;

  --radius: 18px;
  --radius-sm: 12px;
  --radius-pill: 980px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  --shadow-hover: 0 8px 32px rgba(0, 0, 0, 0.10);
  --sidebar-w: 248px;

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text",
    "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  letter-spacing: -0.01em;
  font-size: 15px;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: none; opacity: 0.8; }

/* Foco de teclado visible y consistente en TODO lo accionable (accesibilidad) */
:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
  border-radius: 4px;
}
/* Los inputs ya tienen su anillo propio; evitamos doble outline */
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: none; }

/* Íconos SVG monocromos: heredan el color del texto y se alinean al baseline */
svg.icon {
  width: 1em; height: 1em; flex-shrink: 0;
  stroke: currentColor; fill: none;
  stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round;
  vertical-align: -0.14em;
}

/* ---------------------------------------------------------- Layout shell */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  position: fixed;
  top: 0; left: 0; bottom: 0;
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-right: 1px solid var(--border-light);
  padding: 22px 14px;
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
}

/* Botón hamburguesa y overlay: ocultos en desktop, aparecen en mobile */
.menu-toggle {
  display: none;
  background: rgba(0, 0, 0, 0.05); border: none; cursor: pointer;
  width: 38px; height: 38px; border-radius: 10px;
  color: var(--text); align-items: center; justify-content: center;
}
.menu-toggle:hover { background: rgba(0, 0, 0, 0.09); }
.sidebar-overlay {
  display: none; position: fixed; inset: 0; z-index: 99;
  background: rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
}
.sidebar-overlay.show { display: block; }

.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 10px 22px;
}
.brand .logo {
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, var(--green), var(--teal));
  display: grid; place-items: center;
  color: #fff; font-size: 18px; font-weight: 700;
  box-shadow: 0 4px 12px rgba(52, 199, 89, 0.35);
}
.brand .name { font-size: 19px; font-weight: 650; letter-spacing: -0.02em; }
.brand .sub { font-size: 11px; color: var(--text-3); }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.nav .section-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--text-3); padding: 16px 12px 6px; font-weight: 600;
}
.nav a {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 12px; border-radius: 10px;
  color: var(--text); font-size: 14.5px; font-weight: 450;
  transition: background 0.15s ease;
}
.nav a .ico { width: 22px; display: inline-flex; justify-content: center; font-size: 15px; opacity: 0.92; }
.nav a .ico svg.icon { width: 19px; height: 19px; }
.nav a.active .ico { opacity: 1; }
.kpi .kpi-ico svg.icon { width: 19px; height: 19px; }
.nav a:hover { background: rgba(0, 0, 0, 0.04); opacity: 1; }
.nav a.active {
  background: var(--blue); color: #fff; font-weight: 550;
  box-shadow: 0 2px 8px rgba(0, 113, 227, 0.3);
}

.sidebar-foot {
  border-top: 1px solid var(--border-light); padding-top: 12px; margin-top: 8px;
}
.user-chip {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
}
.user-chip .avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: linear-gradient(135deg, var(--blue), var(--purple));
  color: #fff; display: grid; place-items: center; font-weight: 600; font-size: 13px;
}
.user-chip .meta { line-height: 1.2; }
.user-chip .meta b { font-size: 13.5px; font-weight: 600; }
.user-chip .meta span { font-size: 11.5px; color: var(--text-3); text-transform: capitalize; }

/* ---------------------------------------------------------- Main content */
.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; }

.topbar {
  position: sticky; top: 0; z-index: 50;
  background: rgba(245, 245, 247, 0.8);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border-light);
  padding: 16px 36px;
  display: flex; align-items: center; justify-content: space-between;
}
.topbar h1 { font-size: 24px; font-weight: 650; letter-spacing: -0.02em; }
.topbar .subtitle { font-size: 13px; color: var(--text-2); margin-top: 1px; }

.content { padding: 28px 36px 60px; max-width: 1280px; }

/* ---------------------------------------------------------- Cards & grid */
.grid { display: grid; gap: 18px; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
/* Evita que una tarjeta vacía infle a su hermana (dashboard) */
.grid.top { align-items: start; }

/* Fila de KPIs que se acomoda sola (1 o varios sin huecos raros) */
.kpi-row { display: flex; flex-wrap: wrap; gap: 18px; }
.kpi-row .kpi { flex: 1 1 200px; min-width: 200px; }

/* Montos negativos: en caja un negativo TIENE que gritar */
.neg, .kpi-val.neg, td.neg, .money.neg { color: var(--red); }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  border: 1px solid rgba(0, 0, 0, 0.02);
}
.card.tight { padding: 0; overflow: hidden; }
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.card-head h2 { font-size: 17px; font-weight: 620; letter-spacing: -0.01em; }
.card-head .hint { font-size: 12.5px; color: var(--text-3); }

.section-title {
  font-size: 13px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--text-3); margin: 30px 0 14px;
}
.section-title:first-child { margin-top: 0; }

/* ------------------------------------------------------------- KPI tiles */
.kpi {
  background: var(--surface); border-radius: var(--radius);
  padding: 20px 22px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 4px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.kpi:hover { transform: translateY(-2px); box-shadow: var(--shadow-hover); }
.kpi .kpi-ico {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  font-size: 18px; margin-bottom: 6px;
}
.kpi .kpi-val { font-size: 30px; font-weight: 680; letter-spacing: -0.03em; line-height: 1.1; }
.kpi .kpi-label { font-size: 13px; color: var(--text-2); }
.kpi.blue .kpi-ico   { background: rgba(0,113,227,0.12); color: var(--blue); }
.kpi.green .kpi-ico  { background: rgba(52,199,89,0.14); color: var(--green); }
.kpi.orange .kpi-ico { background: rgba(255,149,0,0.14); color: var(--orange); }
.kpi.red .kpi-ico    { background: rgba(255,59,48,0.12); color: var(--red); }
.kpi.purple .kpi-ico { background: rgba(175,82,222,0.12); color: var(--purple); }
.kpi.teal .kpi-ico   { background: rgba(48,176,199,0.14); color: var(--teal); }

/* ---------------------------------------------------------------- Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left; font-size: 11.5px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.03em; color: var(--text-3);
  padding: 12px 16px; border-bottom: 1px solid var(--border-light);
  background: var(--surface-2); white-space: nowrap;
}
tbody td { padding: 13px 16px; border-bottom: 1px solid var(--border-light); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr { transition: background 0.12s ease; }
tbody tr:hover { background: rgba(0, 113, 227, 0.03); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.strong { font-weight: 600; }
/* Headers ordenables */
.th-sort { color: inherit; font: inherit; text-transform: inherit; letter-spacing: inherit; }
.th-sort:hover { color: var(--blue); }
.th-sort.on { color: var(--blue); }
/* Texto largo en celda: una línea con ellipsis (el resto en title/tooltip) */
.cell-trunc { max-width: 230px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* Fila de totales: separada visualmente del cuerpo */
tfoot td { border-top: 2px solid var(--border); background: var(--surface-2); font-weight: 650; }
/* Fechas vencidas (CRM): rojo para que salten */
td .vencido, .vencido { color: var(--red); font-weight: 600; }

/* ---------------------------------------------------------------- Badges */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 11px; border-radius: var(--radius-pill);
  font-size: 12px; font-weight: 550; white-space: nowrap;
  background: var(--border-light); color: var(--text-2);
}
.badge.dot::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.green  { background: rgba(52,199,89,0.15); color: #1d8c3e; }
.badge.teal   { background: rgba(48,176,199,0.16); color: #1d7d8f; }
.badge.lime   { background: rgba(106,168,0,0.16); color: #4e7d00; }
.badge.blue   { background: rgba(0,113,227,0.12); color: var(--blue-dark); }
.badge.amber  { background: rgba(214,128,0,0.16); color: #9a5d00; }
.badge.orange { background: rgba(255,149,0,0.16); color: #b35e00; }
.badge.red    { background: rgba(255,59,48,0.13); color: #c4271d; }
.badge.purple { background: rgba(175,82,222,0.14); color: #8e3bbb; }
.badge.gray   { background: var(--border-light); color: var(--text-2); }
.badge.yellow { background: rgba(255,204,0,0.22); color: #9b7d00; }
/* Badge que ES una acción (ej: WhatsApp) — se distingue de los de estado */
.badge.action { cursor: pointer; text-decoration: none; }
.badge.action:hover { filter: brightness(0.96); text-decoration: underline; opacity: 1; }

/* Indicador de tendencia en mediciones (▲▼) */
.trend { font-size: 12px; font-weight: 650; margin-left: 4px; white-space: nowrap; }
.trend.up { color: var(--red); }
.trend.down { color: var(--green-dark); }
.trend.flat { color: var(--text-3); }
.trend.good { color: var(--green-dark); }
.trend.bad { color: var(--red); }

/* --------------------------------------------------------------- Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 9px 18px; border-radius: var(--radius-pill);
  font-size: 14px; font-weight: 550; font-family: var(--font);
  border: none; cursor: pointer; transition: all 0.15s ease;
  background: var(--blue); color: #fff; line-height: 1;
}
.btn:hover { background: var(--blue-dark); opacity: 1; }
.btn.secondary { background: rgba(0,0,0,0.05); color: var(--text); }
.btn.secondary:hover { background: rgba(0,0,0,0.09); }
.btn.success { background: var(--green); }
.btn.success:hover { background: #28a745; }
.btn.danger { background: var(--red); }
.btn.ghost { background: transparent; color: var(--blue); padding: 9px 12px; }
.btn.ghost:hover { background: rgba(0,113,227,0.08); }
.btn.sm { padding: 6px 13px; font-size: 13px; min-height: 32px; }
.btn.block { width: 100%; justify-content: center; }
.btn.icon-only { padding: 0; width: 34px; height: 34px; justify-content: center; }
/* Selects inline en tablas: tamaño táctil decente para tablet */
select.inline-select {
  width: auto; padding: 7px 28px 7px 10px; font-size: 13px;
  min-height: 34px; border-radius: 9px;
}
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }

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

/* ----------------------------------------------------------------- Forms */
.form-grid { display: grid; gap: 16px 20px; }
.form-grid.cols-2 { grid-template-columns: 1fr 1fr; }
.form-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 550; color: var(--text-2); }
.req { color: var(--red); font-weight: 700; }
input, select, textarea {
  font-family: var(--font); font-size: 14.5px; color: var(--text);
  padding: 10px 13px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface);
  transition: border 0.15s ease, box-shadow 0.15s ease; width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--blue);
  box-shadow: 0 0 0 3.5px rgba(0, 113, 227, 0.15);
}
textarea { resize: vertical; min-height: 80px; line-height: 1.55; }
.checkbox { flex-direction: row; align-items: center; gap: 9px; }
.checkbox input { width: auto; }

/* --------------------------------------------------------------- Filters */
.toolbar {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  margin-bottom: 20px;
}
.toolbar .search { flex: 1; min-width: 220px; max-width: 360px; position: relative; }
.toolbar .search input { padding-left: 36px; }
.toolbar .search::before {
  content: "🔍"; position: absolute; left: 12px; top: 50%;
  transform: translateY(-50%); font-size: 13px; opacity: 0.5;
}
.toolbar select { width: auto; min-width: 190px; padding-right: 30px; }
.spacer { flex: 1; }

/* ----------------------------------------------------------------- Flash */
.flashes { position: fixed; top: 18px; right: 24px; z-index: 1000; display: flex; flex-direction: column; gap: 10px; }
.flash {
  padding: 13px 18px; border-radius: 14px; font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow-hover); background: var(--surface);
  border-left: 4px solid var(--text-3); min-width: 280px; max-width: 380px;
  animation: slidein 0.3s ease;
  display: flex; align-items: flex-start; gap: 12px; justify-content: space-between;
}
.flash-close {
  background: none; border: none; cursor: pointer; font-size: 20px; line-height: 1;
  color: var(--text-3); padding: 0 2px; flex-shrink: 0;
}
.flash-close:hover { color: var(--text); }
.flash.success { border-color: var(--green); }
.flash.danger { border-color: var(--red); }
.flash.warning { border-color: var(--orange); }
@keyframes slidein { from { transform: translateX(30px); opacity: 0; } to { transform: none; opacity: 1; } }

/* --------------------------------------------------------------- Avatars */
.avatar-sm {
  width: 30px; height: 30px; border-radius: 50%; display: inline-grid; place-items: center;
  background: linear-gradient(135deg, var(--blue), var(--teal));
  color: #fff; font-size: 12px; font-weight: 600; flex-shrink: 0;
}
.person { display: flex; align-items: center; gap: 9px; }
.person .name { font-weight: 550; }
.person .meta { font-size: 12px; color: var(--text-3); }

/* ----------------------------------------------------------- Login page */
.login-wrap {
  min-height: 100vh; display: grid; place-items: center;
  background: radial-gradient(1200px 600px at 70% -10%, rgba(48,176,199,0.18), transparent),
              radial-gradient(900px 500px at 10% 110%, rgba(0,113,227,0.14), transparent), var(--bg);
}
.login-card {
  background: rgba(255,255,255,0.8);
  backdrop-filter: saturate(180%) blur(24px);
  -webkit-backdrop-filter: saturate(180%) blur(24px);
  border: 1px solid rgba(255,255,255,0.5);
  border-radius: 26px; padding: 40px 38px; width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.12);
}
.login-card .logo-big {
  width: 60px; height: 60px; border-radius: 17px; margin: 0 auto 18px;
  background: linear-gradient(135deg, var(--green), var(--teal));
  display: grid; place-items: center; color: #fff; font-size: 30px; font-weight: 700;
  box-shadow: 0 10px 26px rgba(52,199,89,0.4);
}
.login-card h1 { text-align: center; font-size: 23px; font-weight: 650; }
.login-card p.tag { text-align: center; color: var(--text-2); font-size: 13.5px; margin: 4px 0 26px; }
.login-card .field { margin-bottom: 14px; }
.login-hint {
  margin-top: 20px; padding: 12px 14px; background: rgba(0,113,227,0.06);
  border-radius: 12px; font-size: 12.5px; color: var(--text-2); line-height: 1.6;
}

/* --------------------------------------------------------------- Helpers */
.muted { color: var(--text-3); }
.small { font-size: 12.5px; }
.mt-0 { margin-top: 0; } .mt-1 { margin-top: 10px; } .mt-2 { margin-top: 20px; } .mt-3 { margin-top: 30px; }
.mb-1 { margin-bottom: 10px; } .mb-2 { margin-bottom: 20px; }
.flex { display: flex; gap: 12px; align-items: center; }
.flex.between { justify-content: space-between; }
.flex.wrap { flex-wrap: wrap; }
.text-right { text-align: right; }
.divider { height: 1px; background: var(--border-light); margin: 20px 0; }
.empty {
  text-align: center; padding: 50px 20px; color: var(--text-3);
}
.empty .big { font-size: 40px; margin-bottom: 10px; }

/* --------------------------------------------------------------- Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border-light); margin-bottom: 22px; }
.tabs a, .tabs button[role="tab"] {
  padding: 10px 16px; font-size: 14.5px; font-weight: 500; color: var(--text-2);
  border: none; border-bottom: 2px solid transparent; margin-bottom: -1px;
  background: none; cursor: pointer; font-family: var(--font);
}
.tabs a:hover, .tabs button[role="tab"]:hover { color: var(--text); }
.tabs a.active, .tabs button[role="tab"].active { color: var(--blue); border-color: var(--blue); font-weight: 600; }

/* --------------------------------------------------- Patient detail head */
.patient-head {
  display: flex; align-items: center; gap: 18px; margin-bottom: 24px;
}
.patient-head .avatar-xl {
  width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--blue), var(--purple));
  color: #fff; font-size: 24px; font-weight: 650;
}
.patient-head h1 { font-size: 26px; font-weight: 680; letter-spacing: -0.02em; }
.patient-head .tags { display: flex; gap: 8px; margin-top: 6px; flex-wrap: wrap; }

/* timeline for evolución */
.timeline { display: flex; flex-direction: column; gap: 14px; }
.timeline .item {
  display: flex; gap: 14px; padding: 14px 0; border-bottom: 1px solid var(--border-light);
}
.timeline .item:last-child { border-bottom: none; }
.timeline .date {
  font-size: 12px; color: var(--text-3); min-width: 86px; font-weight: 600;
}
.measures { display: flex; gap: 18px; flex-wrap: wrap; }
.measures .m { display: flex; flex-direction: column; }
.measures .m .v { font-size: 17px; font-weight: 650; }
.measures .m .l { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.03em; }

/* AI panel */
.ai-panel {
  background: linear-gradient(135deg, rgba(175,82,222,0.06), rgba(0,113,227,0.05));
  border: 1px solid rgba(175,82,222,0.2); border-radius: var(--radius); padding: 20px;
}
.ai-panel .ai-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.ai-panel .ai-head .spark {
  width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--purple), var(--blue)); color: #fff; font-size: 15px;
}
.ai-alert {
  background: rgba(255,149,0,0.1); border-radius: 10px; padding: 10px 14px;
  font-size: 13px; margin-top: 6px; color: #92400e;
}

@media (max-width: 992px) {
  /* Sidebar pasa a off-canvas */
  .sidebar { transform: translateX(-100%); box-shadow: 0 0 40px rgba(0,0,0,0.18); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .menu-toggle { display: inline-flex; }
  .topbar { gap: 12px; padding: 14px 20px; }
  .content { padding: 22px 20px 48px; }
  .topbar h1 { font-size: 21px; }

  .grid.cols-4 { grid-template-columns: repeat(2, 1fr); }
  .grid.cols-3 { grid-template-columns: 1fr; }
  .grid.cols-2 { grid-template-columns: 1fr; }
  .form-grid.cols-2, .form-grid.cols-3 { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
  .grid.cols-4 { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; }
  .patient-head { gap: 12px; }
  .patient-head .avatar-xl { width: 52px; height: 52px; font-size: 20px; }
  .patient-head h1 { font-size: 21px; }
  .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tabs a { white-space: nowrap; }
}
