/* ==========================================================================
   Essenza · Sistema de diseño compartido
   Estilo sobrio: superficies neutras cálidas, un amarillo mostaza apagado como
   único acento y estados en tonos tranquilos. Sin degradados, brillos ni
   fondos teñidos. Tipografía: Fira Code para datos, Fira Sans para texto.
   Contraste verificado en ambos modos: texto >= 4.5:1, bordes y barras >= 3:1.
   ========================================================================== */

:root {
  color-scheme: light;

  /* Superficies: neutras cálidas, sin tinte amarillo */
  --bg: #faf8f4;
  --surface: #ffffff;
  --surface-2: #f3efe6;
  --border: #e4ddcd;          /* divisores */
  --border-control: #8a7f66;  /* bordes de controles (>= 3:1) */

  /* Texto */
  --fg: #1f1c16;
  --fg-2: #4a453b;
  --fg-muted: #6e6656;

  /* Marca: mostaza. Como relleno lleva texto oscuro encima; para texto e
     iconos sobre fondo claro se usa su versión oscura, que sí se lee. */
  --accent: #e9b949;
  --accent-hover: #f0c562;
  --on-accent: #1f1c16;
  --accent-ink: #7a5a10;
  --accent-soft: rgba(233, 185, 73, 0.16);

  /* Estados (nunca se usan solos: siempre llevan icono o texto) */
  --good: #3f7d52;
  --good-text: #2f6b42;
  --good-soft: transparent;
  --cost: #a8842a;            /* créditos gastados */
  --warn-text: #8a5a12;
  --warn-soft: transparent;
  --danger: #b4493c;
  --danger-text: #a3402f;
  --danger-soft: transparent;
  --info: #7b7465;
  --ring: #1f1c16;

  /* Derivados */
  --btn-hover: #ebe5d8;
  --row-hover: #f7f4ed;
  --good-line: var(--border-control);
  --warn-line: var(--border-control);
  --danger-line: #c9857a;
  --note-line: var(--border);
  --shadow: 0 6px 20px rgba(31, 28, 22, 0.12);

  /* Gráficas */
  --c-cost: #a8842a;
  --c-saved: #3f7d52;
  --c-danger: #b4493c;
  --c-info: #7b7465;
  --c-fg: #1f1c16;
  --c-muted: #6e6656;
  --c-grid: rgba(110, 102, 86, 0.16);
  --c-track: #ece6d8;
  --c-tip-bg: #1f1c16;
  --c-tip-fg: #f3efe6;
  /* Tipografía */
  --font-data: "Fira Code", ui-monospace, "Cascadia Code", Consolas, monospace;
  --font-text: "Fira Sans", "Segoe UI", system-ui, sans-serif;
  --fs-xs: 0.8125rem;   /* 13px, mínimo del sistema */
  --fs-sm: 0.875rem;    /* 14px */
  --fs-md: 1rem;        /* 16px */
  --fs-lg: 1.125rem;
  --fs-xl: 1.75rem;

  /* Espaciado (denso) */
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 24px; --sp-6: 32px;
  --radius: 10px;
  --radius-sm: 8px;

  /* Movimiento */
  --t-fast: 120ms;
  --t: 200ms;
  --ease: cubic-bezier(0.2, 0, 0, 1);

  --tap: 40px;  /* alto mínimo de controles; 44px en pantallas táctiles */
}


/* ---------- Modo oscuro ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;

  /* Carbón neutro cálido (no marrón ni amarillento) y mostaza apagada */
  --bg: #141311;
  --surface: #1c1a17;
  --surface-2: #25231f;
  --border: #33302a;
  --border-control: #7d7564;

  --fg: #ece7dc;
  --fg-2: #c6c0b2;
  --fg-muted: #9b9483;

  --accent: #cfa441;
  --accent-hover: #d9b35a;
  --on-accent: #17150f;
  --accent-ink: #d8b354;
  --accent-soft: rgba(207, 164, 65, 0.12);

  --good: #8cbf98;
  --good-text: #8cbf98;
  --good-soft: transparent;
  --cost: #cfa441;
  --warn-text: #d9ad5c;
  --warn-soft: transparent;
  --danger: #e08a7d;
  --danger-text: #e08a7d;
  --danger-soft: transparent;
  --info: #a39d8f;
  --ring: #ece7dc;

  --btn-hover: #2c2925;
  --row-hover: #211f1b;
  --good-line: var(--border-control);
  --warn-line: var(--border-control);
  --danger-line: #8a5a52;
  --note-line: var(--border);
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.45);

  --c-cost: #cfa441;
  --c-saved: #7fae8a;
  --c-danger: #d98073;
  --c-info: #8f897c;
  --c-fg: #ece7dc;
  --c-muted: #9b9483;
  --c-grid: rgba(155, 148, 131, 0.14);
  --c-track: #2a2722;
  --c-tip-bg: #ece7dc;
  --c-tip-fg: #1c1a17;
}
@media (pointer: coarse) { :root { --tap: 44px; } }

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-text);
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; line-height: 1.2; text-wrap: balance; }
p { margin: 0; }
[hidden] { display: none !important; }

/* ---------- Accesibilidad ---------- */
:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; border-radius: 4px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}
.skip-link {
  position: absolute; left: var(--sp-3); top: -64px; z-index: 100;
  background: var(--accent); color: var(--on-accent); padding: var(--sp-2) var(--sp-4);
  border-radius: var(--radius-sm); font-weight: 600; text-decoration: none;
  transition: top var(--t-fast) var(--ease);
}
.skip-link:focus { top: var(--sp-3); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}

/* ---------- Iconos ---------- */
.ic { width: 1.125em; height: 1.125em; flex: none; vertical-align: -0.2em; }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  min-height: var(--tap); padding: 0 var(--sp-4);
  font: 600 var(--fs-sm) var(--font-text);
  color: var(--fg); background: var(--surface-2);
  border: 1px solid var(--border-control); border-radius: var(--radius-sm);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background-color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), opacity var(--t-fast) var(--ease);
}
.btn:hover { background: var(--btn-hover); }
.btn:active { opacity: 0.85; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger-text); border-color: var(--danger-line); background: transparent; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.sm { min-height: 32px; padding: 0 var(--sp-3); font-size: var(--fs-xs); }
@media (pointer: coarse) { .btn.sm { min-height: 44px; } }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }

/* ---------- Campos de formulario ---------- */
.field { display: grid; gap: var(--sp-1); }
.field > label, .label { font-size: var(--fs-xs); font-weight: 500; color: var(--fg-2); }
.hint { font-size: var(--fs-xs); color: var(--fg-muted); }
.field-error { font-size: var(--fs-xs); color: var(--danger-text); display: flex; gap: 6px; align-items: flex-start; min-height: 1.1em; }
.field-error:empty { display: none; }
input, select, textarea {
  font: inherit; font-size: var(--fs-md); color: var(--fg);
  min-height: var(--tap); padding: var(--sp-2) var(--sp-3);
  background: var(--surface); border: 1px solid var(--border-control); border-radius: var(--radius-sm);
  transition: border-color var(--t-fast) var(--ease);
}
textarea { min-height: 120px; resize: vertical; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--fg-muted); }
input:hover, select:hover, textarea:hover { border-color: var(--fg-2); }
input[aria-invalid="true"], textarea[aria-invalid="true"], select[aria-invalid="true"] { border-color: var(--danger); }
input.mono { font-family: var(--font-data); }

/* ---------- Tarjetas y paneles ---------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--sp-4); min-width: 0; }
.panel .head { display: flex; justify-content: space-between; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
.panel h2 { font: 600 var(--fs-lg) var(--font-text); letter-spacing: -0.01em; }
.panel .sub { color: var(--fg-muted); font-size: var(--fs-sm); margin: 2px 0 var(--sp-3); }

/* ---------- Estados (siempre icono + texto) ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font: 500 var(--fs-xs) var(--font-text); padding: 2px 10px; border-radius: 99px;
  border: 1px solid var(--border-control); color: var(--fg-2); background: transparent; white-space: nowrap;
}
.pill .ic { width: 14px; height: 14px; }
.pill.good { color: var(--good-text); border-color: var(--good-line); background: var(--good-soft); }
.pill.warn { color: var(--warn-text); border-color: var(--warn-line); background: var(--warn-soft); }
.pill.bad { color: var(--danger-text); border-color: var(--danger-line); background: var(--danger-soft); }

/* ---------- Tablas ---------- */
.tbl-wrap { position: relative; overflow: auto; border-radius: var(--radius-sm); } /* position: contiene las etiquetas .sr-only de las celdas, que si no ensanchan la página */
.tbl-wrap.scroll { max-height: 440px; }
.tbl-wrap:focus-visible { outline-offset: -2px; }
table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
th, td { text-align: left; padding: 10px var(--sp-3); border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
thead th { position: sticky; top: 0; z-index: 1; background: var(--surface); color: var(--fg-muted); font-weight: 500; font-size: var(--fs-xs); letter-spacing: 0.03em; }
tbody tr { transition: background-color var(--t-fast) var(--ease); }
tbody tr:hover { background: var(--row-hover); }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-family: var(--font-data); font-variant-numeric: tabular-nums; }
th.num { font-family: var(--font-text); }
td.wrap { white-space: normal; min-width: 180px; }
.who { font-weight: 600; }
.dim { color: var(--fg-muted); }
.mono { font-family: var(--font-data); font-size: var(--fs-sm); }
.th-btn { all: unset; display: inline-flex; align-items: center; gap: 4px; cursor: pointer; border-radius: 4px; padding: 2px 4px; margin: -2px -4px; }
.th-btn:hover { color: var(--fg); }
.th-btn:focus-visible { outline: 2px solid var(--ring); }
.th-btn .ic { width: 12px; height: 12px; opacity: 0.5; transition: transform var(--t-fast) var(--ease); }
th[aria-sort="ascending"] .th-btn, th[aria-sort="descending"] .th-btn { color: var(--fg); }
th[aria-sort] .th-btn .ic { opacity: 0.9; }
th[aria-sort="descending"] .th-btn .ic { transform: rotate(180deg); }

/* ---------- Mensajes ---------- */
.empty { display: flex; gap: var(--sp-3); align-items: center; flex-wrap: wrap; color: var(--fg-muted); padding: var(--sp-3) 2px; }
.empty .ic { color: var(--fg-muted); }
.empty.ok .ic { color: var(--good); }
.note { display: flex; gap: var(--sp-3); align-items: center; flex-wrap: wrap; padding: var(--sp-3) var(--sp-4); border-radius: var(--radius-sm); background: var(--surface); border: 1px solid var(--note-line); border-left: 3px solid var(--accent); color: var(--fg-2); }
.note .ic { color: var(--accent-ink); flex: none; }
.note > span { flex: 1 1 260px; min-width: 0; }
.note a { color: var(--accent-ink); font-weight: 600; }

.toasts { position: fixed; right: var(--sp-4); bottom: var(--sp-4); z-index: 50; display: grid; gap: var(--sp-2); max-width: min(92vw, 420px); }
.toast { display: flex; gap: var(--sp-2); align-items: center; background: var(--surface-2); border: 1px solid var(--border-control); border-radius: var(--radius-sm); padding: var(--sp-3) var(--sp-4); box-shadow: var(--shadow); animation: toast-in var(--t) var(--ease); }
.toast.err { border-color: var(--danger); color: var(--danger-text); }
.toast.ok .ic { color: var(--good); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.skeleton { border-radius: var(--radius); background: var(--surface-2); animation: shimmer 1.4s ease-in-out infinite; }
@keyframes shimmer { 50% { opacity: 0.55; } }

/* ---------- Logo ---------- */
.logo { display: block; height: 30px; width: auto; flex: none; object-fit: contain; }
.brand.big .logo, .login-card .brand .logo { height: 48px; }

/* ---------- Cambio de tema ---------- */
.theme-toggle .ic[hidden] { display: none; }
