/* ==========================================================================
   Atalaya · componentes base · v2.0
   Requiere tokens.css. Solo usa variables: ningún #hex ni px de color sueltos.
   Prefijo de clases: .at-  (evita choques con otros frameworks)
   ========================================================================== */

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--color-background);
  color: var(--color-text);
  font: var(--weight-body) var(--text-body)/var(--leading-body) var(--font-sans);
  font-feature-settings: "tnum" 0;
  -webkit-font-smoothing: antialiased;
}
:where(h1, h2, h3, h4) { margin: 0; font-weight: var(--weight-semibold); color: var(--color-text); text-wrap: balance; }
.at-display { font-size: var(--text-display); line-height: var(--leading-display); letter-spacing: var(--tracking-display); font-weight: 600; }
h1, .at-h1 { font-size: var(--text-h1); line-height: var(--leading-h1); letter-spacing: var(--tracking-h1); }
h2, .at-h2 { font-size: var(--text-h2); line-height: var(--leading-h2); letter-spacing: var(--tracking-h2); }
h3, .at-h3 { font-size: var(--text-h3); line-height: var(--leading-h3); letter-spacing: var(--tracking-h3); }
h4, .at-h4 { font-size: var(--text-h4); line-height: var(--leading-h4); letter-spacing: var(--tracking-h4); }
p { margin: 0; max-width: var(--measure); }
.at-lead { font-size: var(--text-lead); line-height: var(--leading-lead); letter-spacing: var(--tracking-lead); color: var(--color-text-muted); }
.at-small { font-size: var(--text-small); line-height: var(--leading-small); font-weight: var(--weight-small); }
.at-caption { font-size: var(--text-caption); line-height: var(--leading-caption); color: var(--color-text-muted); }
.at-muted { color: var(--color-text-muted); }
.at-label {
  font: var(--weight-label) var(--text-label)/var(--leading-label) var(--font-mono);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--color-text-muted);
}
.at-metric {
  font-size: var(--text-metric); line-height: var(--leading-metric);
  letter-spacing: var(--tracking-metric); font-weight: var(--weight-metric);
  font-variant-numeric: tabular-nums;
}
.at-num, table, time { font-variant-numeric: tabular-nums; }
a { color: var(--color-link); text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: none; box-shadow: var(--focus-ring); }

/* ---------- Botones ---------- */
.at-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--control-height-md);
  padding: 0 var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-control);
  font: var(--weight-small) var(--text-small)/1 var(--font-sans);
  cursor: pointer;
  text-decoration: none;
  transition: background var(--duration-fast) var(--ease-standard),
              border-color var(--duration-fast) var(--ease-standard);
}
.at-btn svg { width: 16px; height: 16px; flex: none; }
/* Una sola acción principal por vista */
.at-btn--primary { background: var(--action-primary-bg); color: var(--action-primary-text); box-shadow: var(--shadow-xs); }
.at-btn--primary:hover { background: var(--action-primary-bg-hover); }
.at-btn--primary:active { background: var(--action-primary-bg-active); }
.at-btn--secondary { background: var(--action-secondary-bg); color: var(--action-secondary-text); border-color: var(--action-secondary-border); box-shadow: var(--shadow-xs); }
.at-btn--secondary:hover { background: var(--action-secondary-bg-hover); border-color: var(--color-border-hover); }
.at-btn--text { background: transparent; color: var(--color-text); }
.at-btn--text:hover { background: var(--color-surface-muted); }
.at-btn--danger { background: var(--action-secondary-bg); color: var(--color-danger); border-color: var(--color-danger-border); }
.at-btn--danger:hover { background: var(--color-danger-bg); }
.at-btn:disabled, .at-btn[aria-disabled="true"] {
  background: var(--action-disabled-bg); color: var(--action-disabled-text);
  border-color: transparent; box-shadow: none; cursor: not-allowed;
}
.at-btn--sm { min-height: var(--control-height-sm); padding: 0 var(--space-3); }
.at-btn--lg { min-height: var(--control-height-lg); padding: 0 var(--space-6); font-size: var(--text-body); }
@media (pointer: coarse) { .at-btn { min-height: var(--touch-target); } }

/* ---------- Insignias de estado (siempre color + texto) ---------- */
.at-badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  font: var(--weight-medium) var(--text-caption)/1.5 var(--font-sans);
  color: var(--color-text-muted);
  white-space: nowrap;
}
.at-badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.at-badge--success { color: var(--color-success); background: var(--color-success-bg); border-color: var(--color-success-border); }
.at-badge--warning { color: var(--color-warning); background: var(--color-warning-bg); border-color: var(--color-warning-border); }
.at-badge--danger  { color: var(--color-danger);  background: var(--color-danger-bg);  border-color: var(--color-danger-border); }
.at-badge--info    { color: var(--color-info);    background: var(--color-info-bg);    border-color: var(--color-info-border); }
.at-badge--alert   { color: var(--color-alert);   background: var(--color-alert-bg);   border-color: var(--color-alert-border); }

/* ---------- Tarjetas y paneles ---------- */
.at-card {
  background: var(--color-surface);
  border: var(--border-default);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: var(--space-6);
}
.at-card--flat { box-shadow: none; }
.at-card__header {
  display: flex; align-items: center; gap: var(--space-3);
  padding-bottom: var(--space-4); margin-bottom: var(--space-4);
  border-bottom: var(--border-default);
}
.at-card__icon {
  display: grid; place-items: center; width: 32px; height: 32px;
  border-radius: var(--radius-md); background: var(--action-selected-bg); color: var(--color-success);
  border: 1px solid var(--color-success-border);
}
.at-panel { background: var(--color-surface); border: var(--border-default); border-radius: var(--radius-panel); }
.at-surface-muted { background: var(--color-surface-muted); }
.at-surface-brand { background: var(--color-surface-inverse); color: var(--color-text-inverse); }

/* ---------- Métrica ---------- */
.at-kpi { display: grid; gap: var(--space-2); }
.at-trend {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px var(--space-2); border-radius: var(--radius-sm);
  font: var(--weight-medium) var(--text-caption)/1.5 var(--font-sans);
  font-variant-numeric: tabular-nums;
}
.at-trend--up   { color: var(--color-success); background: var(--color-success-bg); border: 1px solid var(--color-success-border); }
.at-trend--down { color: var(--color-danger);  background: var(--color-danger-bg);  border: 1px solid var(--color-danger-border); }

/* ---------- Campos ---------- */
.at-field { display: grid; gap: 6px; }
.at-field__label { font: var(--weight-medium) var(--text-small)/var(--leading-small) var(--font-sans); color: var(--color-text); }
.at-field__help  { font-size: var(--text-caption); line-height: var(--leading-caption); color: var(--color-text-muted); }
.at-field__error { font-size: var(--text-caption); line-height: var(--leading-caption); color: var(--color-danger); }
.at-input, .at-select, .at-textarea {
  width: 100%;
  min-height: var(--control-height-md);
  padding: 0 var(--space-3);
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-control);
  font: var(--weight-regular) var(--text-small)/1.4 var(--font-sans);
  box-shadow: var(--shadow-xs);
  transition: border-color var(--duration-fast) var(--ease-standard), box-shadow var(--duration-fast);
}
.at-textarea { padding: var(--space-2) var(--space-3); min-height: 96px; resize: vertical; }
.at-input::placeholder { color: var(--color-text-subtle); }
.at-input:hover, .at-select:hover { border-color: var(--color-border-hover); }
.at-input:focus-visible, .at-select:focus-visible, .at-textarea:focus-visible {
  border-color: var(--action-primary-bg);
  box-shadow: 0 0 0 3px var(--action-selected-bg);
}
.at-input[aria-invalid="true"] { border-color: var(--color-danger); box-shadow: 0 0 0 3px var(--color-danger-bg); }
.at-select {
  appearance: none; padding-right: var(--space-10);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23636965' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right var(--space-3) center;
}
@media (pointer: coarse) { .at-input, .at-select { min-height: var(--touch-target); } }

/* ---------- Casilla e interruptor ---------- */
.at-check { display: inline-flex; align-items: center; gap: var(--space-2); font-size: var(--text-small); cursor: pointer; }
.at-check input { width: 16px; height: 16px; margin: 0; accent-color: var(--action-primary-bg); }
.at-switch { appearance: none; width: 32px; height: 18px; border-radius: var(--radius-pill); background: var(--neutro-300); position: relative; cursor: pointer; transition: background var(--duration-fast); margin: 0; }
.at-switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--neutro-0); box-shadow: var(--shadow-xs); transition: transform var(--duration-fast) var(--ease-standard); }
.at-switch:checked { background: var(--action-primary-bg); }
.at-switch:checked::after { transform: translateX(14px); }

/* ---------- Pestañas segmentadas ---------- */
.at-tabs { display: inline-flex; gap: 2px; padding: 3px; background: var(--color-surface-muted); border: var(--border-default); border-radius: var(--radius-control); }
.at-tab { padding: 6px var(--space-3); border: 0; border-radius: 7px; background: transparent; color: var(--color-text-muted); font: var(--weight-medium) var(--text-small)/1.2 var(--font-sans); cursor: pointer; }
.at-tab[aria-selected="true"] { background: var(--color-surface); color: var(--color-text); box-shadow: var(--shadow-xs); }

/* ---------- Aviso (Almenara: solo lo que requiere acción hoy) ---------- */
.at-notice {
  display: flex; align-items: flex-start; gap: var(--space-3);
  padding: var(--space-4);
  background: var(--color-surface); border: var(--border-default);
  border-radius: var(--radius-card); box-shadow: var(--shadow-pop);
}
.at-notice::before { content: ""; flex: none; margin-top: 6px; width: 8px; height: 8px; border-radius: 50%; background: var(--color-alert-dot); box-shadow: 0 0 0 3px var(--color-alert-bg); }
.at-notice__title { font-weight: var(--weight-semibold); font-size: var(--text-small); }
.at-notice__meta { font-size: var(--text-small); color: var(--color-text-muted); }
.at-notice__action { margin-left: auto; color: var(--color-link); font: var(--weight-semibold) var(--text-small)/1.4 var(--font-sans); background: none; border: 0; cursor: pointer; }

/* ---------- Tablas ---------- */
.at-table { width: 100%; border-collapse: collapse; font-size: var(--text-small); }
.at-table th {
  text-align: left; padding: var(--space-2) var(--space-3);
  font: var(--weight-label) var(--text-label)/var(--leading-label) var(--font-mono);
  letter-spacing: var(--tracking-label); text-transform: uppercase;
  color: var(--color-text-muted); background: var(--color-surface-muted);
}
.at-table td { padding: var(--space-3); border-bottom: var(--border-default); }
.at-table .at-num, .at-table td.num, .at-table th.num { text-align: right; white-space: nowrap; }
.at-table time, .at-nowrap { white-space: nowrap; }
/* El nombre que abre su ficha o su ventana: verde y algo más grueso, sin subrayar; al pasar, subrayado fino.
   No cambia de grosor al pasar, porque la palabra se ensancharía y movería la tabla. */
.at-table td a:not(.at-btn) { font-weight: var(--weight-medium); text-decoration: none; }
.at-table td a:not(.at-btn):hover { text-decoration-line: underline; text-decoration-thickness: 1px; }

/* ---------- Disposición de aplicación ---------- */
.at-app { display: grid; grid-template-columns: 248px 1fr; min-height: 100vh; background: var(--color-surface-muted); }
.at-sidebar { background: var(--color-surface-muted); padding: var(--space-6) var(--space-4); display: flex; flex-direction: column; gap: var(--space-1); }
.at-sidebar__section { margin: var(--space-5) var(--space-2) var(--space-2); font: var(--text-label)/1.3 var(--font-mono); letter-spacing: var(--tracking-label); text-transform: uppercase; color: var(--color-text-muted); }
.at-nav-item { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-2) var(--space-3); border-radius: var(--radius-control); color: var(--color-text); text-decoration: none; font: var(--weight-regular) var(--text-small)/1.4 var(--font-sans); }
.at-nav-item svg { width: 16px; height: 16px; color: var(--color-text-muted); }
.at-nav-item:hover { background: var(--color-surface-sunken); }
.at-nav-item[aria-current="page"] { background: var(--color-surface); border: var(--border-default); box-shadow: var(--shadow-xs); font-weight: var(--weight-medium); }
.at-main { background: var(--color-surface); margin: var(--space-2); border: var(--border-default); border-radius: var(--radius-panel); padding: var(--space-8); }
@media (max-width: 860px) {
  .at-app { grid-template-columns: 1fr; }
  .at-sidebar { display: none; }
  .at-main { margin: 0; border-radius: 0; border: 0; padding: var(--space-4); }
}

/* ---------- Rejilla de datos: cada celda, una noche ---------- */
.at-grid-cells { display: grid; grid-auto-flow: column; grid-template-rows: repeat(var(--rows, 10), 10px); gap: 4px; }
.at-cell { width: 10px; height: 10px; border-radius: 3px; background: var(--chart-empty); }
.at-cell--busy    { background: var(--chart-1); }
.at-cell--pending { background: var(--chart-2); }
.at-cell--today   { background: var(--chart-today); }
.at-cell--alert   { background: var(--chart-alert); }

/* ---------- Estado vacío ---------- */
.at-empty { display: grid; place-items: center; gap: var(--space-2); padding: var(--space-12) var(--space-6); text-align: center; color: var(--color-text-muted); }
.at-empty strong { color: var(--color-text); font-weight: var(--weight-semibold); }

/* ---------- Ventana (diálogo sobre la página) ----------
   <dialog class="at-dialog">: centrada, con el fondo oscurecido y difuminado. La abre y la cierra js/ventana.js.
   Receta y reglas: references/componentes.md de la skill atalaya-design («Ventana»). */
.at-dialog {
  width: min(480px, calc(100% - 2 * var(--space-4)));
  padding: var(--space-6);
  border: var(--border-default);
  border-radius: var(--radius-2xl);
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-modal);
}
/* Para formularios con varios campos. */
.at-dialog--lg { width: min(720px, calc(100% - 2 * var(--space-4))); }
/* Velo tintado con Tinta, igual en tema claro y oscuro. */
.at-dialog::backdrop {
  background: color-mix(in srgb, var(--neutro-900) 35%, transparent);
  backdrop-filter: blur(4px);
}
.at-dialog__header {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-3); margin-bottom: var(--space-5);
}
.at-dialog__actions { display: flex; justify-content: flex-end; gap: var(--space-3); margin-top: var(--space-6); }
/* Si no cabe en alto, se desplaza el cuerpo (el formulario, o .at-dialog__body si no hay formulario) y no la
   ventana entera: así la barra queda por dentro y no tapa las esquinas redondeadas. */
.at-dialog[open] { display: flex; flex-direction: column; }
.at-dialog:has(> form),
.at-dialog:has(> .at-dialog__body) { overflow: hidden; padding-bottom: 0; }
.at-dialog > form,
.at-dialog > .at-dialog__body {
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  /* El cuerpo llega a los bordes de la ventana para que no se recorte el anillo de foco de los campos. */
  margin: calc(-1 * var(--space-1)) calc(-1 * var(--space-6)) 0;
  padding: var(--space-1) var(--space-6) var(--space-6);
  scrollbar-width: thin; scrollbar-color: var(--color-border-hover) transparent;
}

/* ---------- Movimiento ----------
   Ventana: entra con opacidad y 8 px de recorrido (200 ms) y sale en 120 ms; el velo, igual. Sin soporte de
   @starting-style, abre y cierra igual que antes. Con movimiento reducido, tokens.css pone las duraciones a 0. */
.at-dialog {
  opacity: 0;
  transform: translateY(var(--space-2));
  transition: opacity var(--duration-fast) var(--ease-standard),
              transform var(--duration-fast) var(--ease-standard),
              overlay var(--duration-fast) allow-discrete,
              display var(--duration-fast) allow-discrete;
}
.at-dialog[open] { opacity: 1; transform: none; transition-duration: var(--duration-base); }
@starting-style { .at-dialog[open] { opacity: 0; transform: translateY(var(--space-2)); } }
.at-dialog::backdrop {
  opacity: 0;
  transition: opacity var(--duration-fast) var(--ease-standard),
              overlay var(--duration-fast) allow-discrete,
              display var(--duration-fast) allow-discrete;
}
.at-dialog[open]::backdrop { opacity: 1; transition-duration: var(--duration-base); }
@starting-style { .at-dialog[open]::backdrop { opacity: 0; } }

/* Fila que se puede abrir: se tiñe al pasar (120 ms) sin moverse. Menú: el fondo, igual. */
.at-table tbody tr { transition: background-color var(--duration-fast) var(--ease-standard); }
.at-table tbody tr:has(a:not(.at-btn)):hover { background: var(--color-surface-muted); }
.at-nav-item { transition: background-color var(--duration-fast) var(--ease-standard); }

/* Enviando: el botón conserva tamaño y color; solo cambia el texto y el cursor. */
.at-btn[aria-busy="true"] { cursor: progress; }

/* En pantallas táctiles, campos a 16 px: se leen mejor y el móvil no amplía la página al escribir. */
@media (pointer: coarse) { .at-input, .at-select, .at-textarea { font-size: var(--text-body); } }

/* ---------- Iconos ---------- */
.i { width: 16px; height: 16px; flex: none; vertical-align: -3px; }
.i--20 { width: 20px; height: 20px; } .i--24 { width: 24px; height: 24px; } .i--32 { width: 32px; height: 32px; }

/* ---------- Foco visible (dejar al final del archivo) ----------
   Gana a la sombra propia de algunos componentes (botón primario y secundario, pestaña activa, sección
   actual del menú). Sin esto, al navegar con Tab no se ve dónde está el foco. */
.at-btn:focus-visible,
.at-tab:focus-visible,
.at-nav-item:focus-visible { box-shadow: var(--focus-ring); }
