/* ============================================================
   Todoluces · Consola de gestión
   Dirección de diseño: consola interna, densa pero calma.
   La firma es tipográfica: datos en monoespaciada (códigos, CAE,
   cantidades) + sistema de "pills" que codifican el estado del documento.
   Paleta anclada a la marca: gris Todoluces + oliva/oro.
   ============================================================ */

:root {
  /* Paleta */
  --ink:        #2b2b2b;   /* texto principal */
  --ink-soft:   #6b6b6b;   /* texto secundario */
  --ink-faint:  #9a9a9a;   /* texto terciario / placeholders */
  --line:       #e4e2dc;   /* divisores y bordes */
  --line-soft:  #efeee9;
  --surface:    #ffffff;   /* tarjetas, filas */
  --bg:         #faf9f6;   /* fondo general, cálido (iluminación) */
  --brand:      #4a4a4a;   /* gris Todoluces */
  --accent:     #a89b38;   /* oliva/oro Todoluces */
  --accent-ink: #6f6722;   /* oliva oscuro para texto sobre claro */

  /* Estados (semánticos, codifican el ciclo del documento) */
  --st-draft:   #8a8a8a;   /* borrador */
  --st-ok:      #3f7d4e;   /* autorizado / confirmado */
  --st-wait:    #b07d18;   /* pendiente */
  --st-err:     #b23b3b;   /* error */
  --st-cancel:  #9a9a9a;   /* cancelado */

  /* Tipografía */
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", ui-monospace, "Cascadia Code",
          Menlo, Consolas, monospace;

  --r: 6px;                /* radio base, discreto */
  --gap: 16px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
}

/* Datos tabulares y códigos: monoespaciada, la firma del sistema */
.mono, code, .data { font-family: var(--mono); font-variant-numeric: tabular-nums; }

a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Layout: sidebar + contenido ---------- */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--brand);
  color: #e9e7e0;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
}
.brand {
  padding: 0 20px 18px;
  display: flex; align-items: baseline; gap: 8px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.brand .logo { font-weight: 700; letter-spacing: -.02em; font-size: 18px; color: #fff; }
.brand .logo b { color: var(--accent); }
.brand .env {
  font-family: var(--mono); font-size: 10px; text-transform: uppercase;
  letter-spacing: .08em; color: var(--accent);
}

.nav { padding: 12px 0; flex: 1; }
.nav .group {
  padding: 14px 20px 6px;
  font-size: 10.5px; text-transform: uppercase; letter-spacing: .1em;
  color: rgba(255,255,255,.4);
}
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 20px; color: rgba(255,255,255,.82); font-size: 13.5px;
}
.nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; color: #fff; }
.nav a.active {
  color: #fff; background: rgba(168,155,56,.16);
  box-shadow: inset 3px 0 0 var(--accent);
}

/* Selector de razón social activa: decisión central del sistema multi-empresa */
.company-switch {
  margin: 0 14px 8px; padding: 10px 12px;
  background: rgba(0,0,0,.18); border-radius: var(--r);
}
.company-switch label {
  display: block; font-size: 10px; text-transform: uppercase;
  letter-spacing: .08em; color: rgba(255,255,255,.45); margin-bottom: 4px;
}
.company-switch select {
  width: 100%; background: #3a3a3a; color: #fff; border: 1px solid #555;
  border-radius: 4px; padding: 6px 8px; font-family: var(--sans); font-size: 13px;
}

/* ---------- Contenido ---------- */
.content { padding: 24px 32px; max-width: 1200px; }

.page-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 20px; gap: 16px;
}
.page-head h1 {
  font-size: 20px; font-weight: 650; letter-spacing: -.01em; margin: 0;
}
.page-head .sub { color: var(--ink-soft); font-size: 13px; }

/* ---------- Tarjetas / KPIs ---------- */
.grid { display: grid; gap: var(--gap); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r); padding: 16px;
}
.kpi .label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--ink-soft);
}
.kpi .value {
  font-family: var(--mono); font-size: 26px; font-weight: 600;
  letter-spacing: -.02em; margin-top: 4px;
}
.kpi .value small { font-size: 14px; color: var(--ink-faint); }

/* ---------- Tablas de datos ---------- */
table.data-table { width: 100%; border-collapse: collapse; background: var(--surface); }
.data-table th {
  text-align: left; font-size: 11px; text-transform: uppercase;
  letter-spacing: .05em; color: var(--ink-soft); font-weight: 600;
  padding: 10px 12px; border-bottom: 1px solid var(--line);
}
.data-table td {
  padding: 10px 12px; border-bottom: 1px solid var(--line-soft); font-size: 13.5px;
}
.data-table tr:hover td { background: #fcfbf8; }
.data-table .num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.data-table .code { font-family: var(--mono); color: var(--ink-soft); }

/* ---------- Pills de estado (codifican el ciclo del documento) ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px; font-weight: 600; padding: 2px 9px; border-radius: 100px;
  border: 1px solid currentColor; line-height: 1.6;
}
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 100px; background: currentColor; }
.pill.draft  { color: var(--st-draft); }
.pill.ok     { color: var(--st-ok); }
.pill.wait   { color: var(--st-wait); }
.pill.err    { color: var(--st-err); }
.pill.cancel { color: var(--st-cancel); opacity: .75; }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: 13px; font-weight: 550;
  padding: 7px 14px; border-radius: var(--r); cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
.btn:hover { border-color: var(--ink-faint); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }

/* ---------- Estados vacíos: una invitación a actuar, no decoración ---------- */
.empty {
  text-align: center; padding: 48px 24px; color: var(--ink-soft);
  border: 1px dashed var(--line); border-radius: var(--r); background: var(--surface);
}
.empty h3 { margin: 0 0 6px; color: var(--ink); font-weight: 600; }

/* ---------- Login ---------- */
.login-wrap { display: grid; place-items: center; min-height: 100vh; padding: 24px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: 32px;
}
.login-card .logo { font-size: 22px; font-weight: 700; letter-spacing: -.02em; }
.login-card .logo b { color: var(--accent); }
.field { margin-bottom: 14px; }
.field label {
  display: block; font-size: 12px; color: var(--ink-soft); margin-bottom: 5px;
}
.field input {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line);
  border-radius: var(--r); font: inherit; font-size: 14px;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

/* ---------- Formularios (campos select, textarea, checkbox) ---------- */
.field select, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line);
  border-radius: var(--r); font: inherit; font-size: 14px; background: var(--surface);
}
.field select:focus, .field textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent);
}
.field textarea { resize: vertical; min-height: 72px; }
.field .hint { font-size: 11.5px; color: var(--ink-soft); margin-top: 3px; }
.field.error input, .field.error select, .field.error textarea { border-color: var(--st-err); }
.field.error .msg { font-size: 11.5px; color: var(--st-err); margin-top: 3px; }
.field input[type="checkbox"] { width: auto; }

.form-grid { display: grid; gap: 0 20px; }
.form-grid.cols-2 { grid-template-columns: 1fr 1fr; }
.form-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
.form-section { margin-bottom: 20px; }
.form-section-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--ink-soft); font-weight: 600; padding-bottom: 8px;
  border-bottom: 1px solid var(--line); margin-bottom: 14px;
}
.form-actions {
  display: flex; gap: 8px; align-items: center;
  padding-top: 16px; border-top: 1px solid var(--line); margin-top: 4px;
}
.form-actions .sep { flex: 1; }

/* ---------- Toolbar: barra de búsqueda + acciones ---------- */
.toolbar {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 16px;
}
.toolbar .search-wrap { flex: 1; max-width: 360px; position: relative; }
.toolbar .search-wrap input {
  width: 100%; padding: 8px 11px 8px 32px;
  border: 1px solid var(--line); border-radius: var(--r);
  font: inherit; font-size: 13.5px; background: var(--surface);
}
.toolbar .search-wrap input:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent);
}
.toolbar .search-wrap::before {
  content: "⌕"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--ink-faint); font-size: 16px; pointer-events: none;
}
.toolbar .sep { flex: 1; }

/* ---------- Paginación ---------- */
.pagination {
  display: flex; align-items: center; gap: 4px; justify-content: flex-end;
  margin-top: 16px; font-size: 13px;
}
.pagination a, .pagination span {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 32px; height: 32px; padding: 0 8px;
  border: 1px solid var(--line); border-radius: var(--r);
  font-family: var(--mono); font-size: 12px; color: var(--ink);
}
.pagination a:hover { border-color: var(--accent); color: var(--accent-ink); text-decoration: none; }
.pagination .active { background: var(--accent); border-color: var(--accent); color: #fff; }
.pagination .disabled { opacity: .4; pointer-events: none; }

/* ---------- Toasts HTMX (HX-Trigger: showToast) ---------- */
#toast-area { position: fixed; bottom: 20px; right: 20px; z-index: 999; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: var(--r);
  font-size: 13px; box-shadow: 0 4px 12px rgba(0,0,0,.18);
  animation: toast-in .15s ease;
}
.toast.ok  { background: var(--st-ok); }
.toast.err { background: var(--st-err); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Panel de avance (líneas de orden) ---------- */
.progress-table th, .progress-table td { padding: 8px 10px; font-size: 13px; }
.progress-bar-wrap { background: var(--line); border-radius: 100px; height: 6px; width: 80px; display: inline-block; vertical-align: middle; }
.progress-bar { background: var(--accent); height: 6px; border-radius: 100px; }

/* ---------- Quick form (modal inline HTMX) ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-box {
  background: var(--surface); border-radius: 10px;
  padding: 24px; width: 100%; max-width: 480px;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
}
.modal-box h2 { margin: 0 0 16px; font-size: 17px; }

/* ---------- Danger/destructive ---------- */
.btn.danger { color: var(--st-err); border-color: var(--st-err); }
.btn.danger:hover { background: var(--st-err); color: #fff; }

/* ---------- Fieldset line items (remito / factura) ---------- */
.line-items-table { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.line-items-table th {
  font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--ink-soft); font-weight: 600; padding: 6px 8px;
  border-bottom: 1px solid var(--line); text-align: left;
}
.line-items-table td { padding: 4px 8px; }
.line-items-table td input, .line-items-table td select {
  width: 100%; padding: 6px 8px; border: 1px solid var(--line);
  border-radius: 4px; font: inherit; font-size: 13px; background: var(--surface);
}
.line-items-table .num-cell { text-align: right; font-family: var(--mono); }

/* ---------- Accesibilidad / responsive ---------- */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; padding: 12px; }
  .nav { display: none; }
  .content { padding: 16px; }
  .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
