/* ============================================================
   Componentes UI
   ============================================================ */

/* ============ MODAL ============ */
.modal {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal, 1000);
  display: grid;
  place-items: center;
  padding: var(--sp-4);
}
.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  animation: modal-fade var(--dur) var(--ease);
}
.modal__panel {
  position: relative;
  background: var(--bg-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-height: calc(100vh - var(--sp-8));
  display: flex;
  flex-direction: column;
  animation: modal-pop var(--dur) var(--ease);
}
.modal__panel--sm { max-width: 420px; }
.modal__panel--md { max-width: 640px; }
.modal__panel--lg { max-width: 960px; }

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border-subtle);
}
.modal__title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text-hi);
}
.modal__body {
  padding: var(--sp-5);
  overflow-y: auto;
}

body.modal-open { overflow: hidden; }

@keyframes modal-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes modal-pop {
  from { opacity: 0; transform: translateY(8px) scale(.98); }
  to { opacity: 1; transform: none; }
}

/* ============ FORM (inputs, selects, textareas en modales/páginas) ============ */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--sp-4);
}
.form-grid .col-span-2 { grid-column: span 2; }

.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  min-width: 0;
}
.field__label {
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-md);
}
.field__label .req {
  color: var(--danger);
  margin-left: 2px;
}
.field__hint {
  font-size: var(--fs-xs);
  color: var(--text-lo);
}
.field__error {
  font-size: var(--fs-xs);
  color: var(--danger);
  min-height: 16px;
}
.field input,
.field select,
.field textarea {
  height: 38px;
  padding: 0 var(--sp-3);
  background: var(--bg-0);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  color: var(--text-hi);
  font-family: inherit;
  font-size: var(--fs-sm);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
  width: 100%;
  box-sizing: border-box;
}
.field textarea {
  height: auto;
  min-height: 80px;
  padding: var(--sp-2) var(--sp-3);
  resize: vertical;
  font-family: inherit;
}
.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.field input[aria-invalid="true"],
.field select[aria-invalid="true"],
.field textarea[aria-invalid="true"] {
  border-color: var(--danger);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--sp-2);
  padding-top: var(--sp-4);
  margin-top: var(--sp-4);
  border-top: 1px solid var(--border-subtle);
}

.form-error {
  padding: var(--sp-2) var(--sp-3);
  background: var(--danger-dim);
  border: 1px solid var(--danger);
  border-radius: var(--radius-md);
  color: var(--danger);
  font-size: var(--fs-sm);
  margin-bottom: var(--sp-3);
}

/* ============ COMBOBOX (select con búsqueda) ============ */
.combobox {
  position: relative;
}
.combobox__input {
  width: 100%;
  height: 38px;
  padding: 0 36px 0 var(--sp-3);
  background: var(--bg-0);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  color: var(--text-hi);
  font-family: inherit;
  font-size: var(--fs-sm);
  box-sizing: border-box;
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.combobox__input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.combobox__chevron {
  position: absolute;
  right: var(--sp-3);
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-dim);
  pointer-events: none;
  transition: transform var(--dur-fast) var(--ease);
}
.combobox[data-state="open"] .combobox__chevron {
  transform: translateY(-50%) rotate(180deg);
}
.combobox__menu {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  max-height: 240px;
  overflow-y: auto;
  background: var(--bg-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  z-index: 50;
}
.combobox[data-state="open"] .combobox__menu {
  display: block;
}
.combobox__option {
  padding: var(--sp-2) var(--sp-3);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  font-size: var(--fs-sm);
  color: var(--text-hi);
}
.combobox__option:hover,
.combobox__option.is-highlighted {
  background: var(--bg-2);
}
.combobox__option-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.combobox__option-sublabel {
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  flex-shrink: 0;
}
.combobox__empty {
  padding: var(--sp-3);
  color: var(--text-dim);
  text-align: center;
  font-size: var(--fs-sm);
}

/* ============ MINI BAR CHART (facturación mensual) ============ */
.months-chart {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}
.months-chart__row {
  display: grid;
  grid-template-columns: 64px 1fr 110px;
  gap: var(--sp-3);
  align-items: center;
  font-size: var(--fs-sm);
}
.months-chart__label {
  color: var(--text-lo);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}
.months-chart__bar-wrap {
  height: 8px;
  background: var(--bg-3);
  border-radius: var(--radius-full);
  overflow: hidden;
}
.months-chart__bar {
  height: 100%;
  background: linear-gradient(to right, var(--accent), #3B82F6);
  border-radius: inherit;
  transition: width var(--dur) var(--ease);
  min-width: 2px;
}
.months-chart__value {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-md);
  font-size: var(--fs-sm);
}

/* ============ BOTONES ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  height: 36px;
  padding: 0 var(--sp-4);
  border-radius: var(--radius-md);
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  transition: all var(--dur-fast) var(--ease);
  border: 1px solid transparent;
  white-space: nowrap;
  user-select: none;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn--primary {
  background: var(--grad-accent);
  color: var(--bg-0);
  box-shadow: 0 1px 0 rgba(255,255,255,0.08) inset, var(--shadow-sm);
}
.btn--primary:hover:not(:disabled) { filter: brightness(1.08); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.btn--primary:active:not(:disabled) { transform: translateY(0); }

.btn--secondary {
  background: var(--bg-2);
  color: var(--text-hi);
  border-color: var(--border);
}
.btn--secondary:hover:not(:disabled) { background: var(--bg-3); border-color: var(--border-strong); }

.btn--ghost { background: transparent; color: var(--text-md); }
.btn--ghost:hover:not(:disabled) { background: var(--bg-2); color: var(--text-hi); }

.btn--danger {
  background: var(--danger-dim);
  color: var(--danger);
  border-color: var(--danger);
}
.btn--danger:hover:not(:disabled) { background: var(--danger); color: white; }

.btn--sm { height: 30px; padding: 0 var(--sp-3); font-size: var(--fs-xs); }
.btn--lg { height: 44px; padding: 0 var(--sp-5); font-size: var(--fs-md); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  color: var(--text-md);
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
  position: relative;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-2); color: var(--text-hi); }

.badge-dot {
  position: absolute;
  top: 8px; right: 8px;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 2px var(--bg-1), 0 0 8px var(--accent);
}

/* Theme toggle — muestra sol/luna según tema */
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }

/* ============ CARDS ============ */
.card {
  background: var(--bg-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--sp-5);
  transition: border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}

.card:hover { border-color: var(--border); }

.card--interactive { cursor: pointer; }
.card--interactive:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }

.card__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: var(--sp-4);
  gap: var(--sp-3);
}

.card__title {
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--text-md);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.card__body { color: var(--text-md); }

/* ============ KPI CARDS ============ */
.kpi {
  background: var(--bg-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--sp-5);
  position: relative;
  overflow: hidden;
  transition: all var(--dur-fast) var(--ease);
}

.kpi::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--kpi-accent, var(--accent));
  opacity: 0.6;
}

.kpi:hover { border-color: var(--border); transform: translateY(-2px); box-shadow: var(--shadow-md); }

.kpi__label {
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--text-lo);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

.kpi__icon {
  width: 28px; height: 28px;
  display: grid; place-items: center;
  border-radius: var(--radius-md);
  background: var(--accent-dim);
  color: var(--accent);
  flex-shrink: 0;
}

.kpi__icon--success { background: var(--success-dim); color: var(--success); }
.kpi__icon--warning { background: var(--warning-dim); color: var(--warning); }
.kpi__icon--danger { background: var(--danger-dim); color: var(--danger); }
.kpi__icon--info { background: var(--info-dim); color: var(--info); }

.kpi__value {
  font-family: var(--font-mono);
  font-size: var(--fs-3xl);
  font-weight: 600;
  color: var(--text-hi);
  margin-top: var(--sp-3);
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.kpi__unit {
  font-size: var(--fs-md);
  color: var(--text-lo);
  margin-left: var(--sp-1);
}

.kpi__footer {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-top: var(--sp-3);
  font-size: var(--fs-xs);
  color: var(--text-lo);
}

.trend {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-family: var(--font-mono);
  font-weight: 600;
}
.trend--up { color: var(--success); }
.trend--down { color: var(--danger); }

/* ============ GRID ============ */
.grid { display: grid; gap: var(--sp-4); }
.grid--cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid--cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid--cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid--auto-fit { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

@media (max-width: 900px) {
  .grid--cols-3, .grid--cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 560px) {
  .grid--cols-2, .grid--cols-3, .grid--cols-4 { grid-template-columns: 1fr; }
}

/* ============ BADGES ============ */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  padding: 3px 10px;
  border-radius: var(--radius-full);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: 1.4;
  background: var(--bg-3);
  color: var(--text-md);
  border: 1px solid var(--border-subtle);
  text-transform: capitalize;
  white-space: nowrap;
}

.badge--success { background: var(--success-dim); color: var(--success); border-color: transparent; }
.badge--warning { background: var(--warning-dim); color: var(--warning); border-color: transparent; }
.badge--danger { background: var(--danger-dim); color: var(--danger); border-color: transparent; }
.badge--info { background: var(--info-dim); color: var(--info); border-color: transparent; }
.badge--accent { background: var(--accent-dim); color: var(--accent); border-color: transparent; }

/* ============ TABLAS ============ */
.table-wrap {
  background: var(--bg-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.table-wrap__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border-subtle);
  gap: var(--sp-3);
  flex-wrap: wrap;
}

.table-wrap__title {
  font-size: var(--fs-md);
  font-weight: 600;
  color: var(--text-hi);
}

.table-wrap__scroll { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}

.table thead th {
  text-align: left;
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--text-lo);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: var(--sp-3) var(--sp-5);
  background: var(--bg-1);
  border-bottom: 1px solid var(--border-subtle);
  white-space: nowrap;
  position: sticky;
  top: 0;
}

.table tbody td {
  padding: var(--sp-3) var(--sp-5);
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-md);
  vertical-align: middle;
}

.table tbody tr { transition: background var(--dur-fast) var(--ease); }
.table tbody tr:hover { background: var(--bg-3); }
.table tbody tr:last-child td { border-bottom: none; }

.table tbody td strong { color: var(--text-hi); font-weight: 600; }

.table .num, .table .mono {
  font-family: var(--font-mono);
  text-align: right;
}

.table__empty {
  padding: var(--sp-12);
  text-align: center;
  color: var(--text-lo);
}

/* ============ FORMS ============ */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.field__label {
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--text-md);
}

.input, .select, .textarea {
  width: 100%;
  height: 36px;
  padding: 0 var(--sp-3);
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-hi);
  font-size: var(--fs-sm);
  font-family: inherit;
  transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}

.textarea { height: auto; padding: var(--sp-3); min-height: 80px; resize: vertical; }

.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.input::placeholder, .textarea::placeholder { color: var(--text-lo); }

/* ============ EMPTY STATES ============ */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--sp-16) var(--sp-6);
  text-align: center;
  color: var(--text-lo);
  gap: var(--sp-3);
}

.empty__icon {
  width: 56px; height: 56px;
  display: grid; place-items: center;
  border-radius: var(--radius-full);
  background: var(--bg-2);
  border: 1px solid var(--border-subtle);
  color: var(--text-dim);
  margin-bottom: var(--sp-2);
}

.empty__title {
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--text-hi);
}

.empty__desc { font-size: var(--fs-sm); max-width: 480px; }

/* ============ SKELETON LOADER ============ */
.skeleton {
  background: linear-gradient(90deg, var(--bg-3) 0%, var(--bg-4) 50%, var(--bg-3) 100%);
  background-size: 200% 100%;
  border-radius: var(--radius-sm);
  animation: shimmer 1.4s infinite linear;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton--text { height: 12px; width: 100%; }
.skeleton--kpi { height: 148px; border-radius: var(--radius-lg); }
.skeleton--row { height: 48px; }

/* ============ TOASTS ============ */
.toast-stack {
  position: fixed;
  bottom: var(--sp-6);
  right: var(--sp-6);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  max-width: 380px;
}

.toast {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-md);
  padding: var(--sp-3) var(--sp-4);
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  font-size: var(--fs-sm);
  color: var(--text-hi);
  animation: slide-in var(--dur) var(--ease);
  min-width: 260px;
}

.toast--success { border-left-color: var(--success); }
.toast--warning { border-left-color: var(--warning); }
.toast--danger { border-left-color: var(--danger); }

@keyframes slide-in {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ============ LOADING DOTS ============ */
.dots::after {
  content: '';
  display: inline-block;
  width: 1ch;
  text-align: left;
  animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
  0% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
}

/* ============ DETAIL LISTS ============ */
.detail-list {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: var(--sp-3) var(--sp-5);
  font-size: var(--fs-sm);
}

.detail-list dt {
  color: var(--text-lo);
  font-weight: 500;
}

.detail-list dd {
  color: var(--text-hi);
  font-weight: 500;
  word-break: break-word;
}

/* ------------------------------------------------------------
   Segmented tabs (filtros tipo "Todos · Core · No-core · ...")
   ------------------------------------------------------------ */
.seg-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-1);
  background: var(--bg-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--sp-1);
  margin-bottom: var(--sp-4);
}

.seg-tabs__item {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  background: transparent;
  border: none;
  border-radius: calc(var(--radius-md) - 4px);
  color: var(--text-md);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 120ms, color 120ms;
}

.seg-tabs__item:hover {
  background: var(--bg-3);
  color: var(--text-hi);
}

.seg-tabs__item.is-active {
  background: var(--bg-1);
  color: var(--text-hi);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.seg-tabs__count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 18px;
  padding: 0 6px;
  background: var(--bg-3);
  border-radius: 9px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-md);
}

.seg-tabs__item.is-active .seg-tabs__count {
  background: var(--accent-dim);
  color: var(--accent);
}
