:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e6ec;
  --text: #1f2733;
  --muted: #6b7280;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --rojo: #dc2626;
  --rojo-bg: #fee2e2;
  --ambar: #d97706;
  --ambar-bg: #fef3c7;
  --verde: #16a34a;
  --verde-bg: #dcfce7;
  --gris-bg: #eef1f5;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

a { color: var(--brand); }

.muted { color: var(--muted); }
.error { color: var(--rojo); font-weight: 600; }

/* ---------- Botones ---------- */
.btn {
  display: inline-block;
  padding: 0.75rem 1.25rem;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
  background: var(--brand);
  color: #fff;
  transition: background 0.15s ease;
}
.btn:hover { background: var(--brand-dark); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-secundario { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-secundario:hover { background: var(--gris-bg); }
.btn-google { background: #fff; color: #1f2733; border: 1px solid var(--border); font-size: 1.1rem; }
.btn-google:hover { background: var(--gris-bg); }

/* ---------- Login ---------- */
.login-page { display: grid; min-height: 100vh; place-items: center; padding: 1rem; }
.login-card {
  background: var(--surface);
  padding: 2.5rem 2rem;
  border-radius: 16px;
  box-shadow: var(--shadow);
  text-align: center;
  max-width: 380px;
  width: 100%;
}
.login-card h1 { margin: 0 0 0.5rem; font-size: 1.4rem; }
.login-card .btn { margin-top: 1.25rem; width: 100%; }

/* ---------- Layout app ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
}
.topbar .brand { font-weight: 700; }
.nav { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.nav a {
  padding: 0.4rem 0.8rem;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  font-weight: 600;
}
.nav a.activo { background: var(--brand); color: #fff; }
.container { max-width: 1080px; margin: 0 auto; padding: 1.25rem; }

/* ---------- Alerta ---------- */
.alerta {
  background: var(--rojo-bg);
  color: var(--rojo);
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  padding: 0.9rem 1.1rem;
  font-weight: 600;
  margin-bottom: 1rem;
}
.alerta.ok { background: var(--verde-bg); color: var(--verde); border-color: #bbf7d0; }

/* ---------- Tarjetas KPI ---------- */
.tarjetas { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.9rem; margin-bottom: 1.25rem; }
.tarjeta {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.tarjeta .etiqueta { font-size: 0.85rem; color: var(--muted); }
.tarjeta .valor { font-size: 1.5rem; font-weight: 700; margin-top: 0.25rem; }
@media (max-width: 720px) { .tarjetas { grid-template-columns: repeat(2, 1fr); } }

/* ---------- Calendario ---------- */
.calendario-cab { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.75rem; }
.calendario-cab h2 { margin: 0; font-size: 1.15rem; }
.calendario { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.6rem; margin-bottom: 1.5rem; }
.mes {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  text-align: left;
  cursor: pointer;
  font: inherit;
}
.mes .nombre { font-weight: 700; }
.mes .detalle { font-size: 0.85rem; color: var(--muted); margin-top: 0.25rem; }
.mes.rojo { background: var(--rojo-bg); border-color: #fecaca; }
.mes.rojo .detalle { color: var(--rojo); font-weight: 600; }
.mes.ambar { background: var(--ambar-bg); border-color: #fde68a; }
.mes.ambar .detalle { color: var(--ambar); font-weight: 600; }
.mes.verde { background: var(--verde-bg); border-color: #bbf7d0; }
.mes.verde .detalle { color: var(--verde); font-weight: 600; }
.mes.gris { background: var(--gris-bg); color: var(--muted); }
.mes.seleccionado { outline: 3px solid var(--brand); outline-offset: 1px; }
@media (max-width: 720px) { .calendario { grid-template-columns: repeat(2, 1fr); } }

/* ---------- Tabla ---------- */
.tabla-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; min-width: 720px; }
th, td { padding: 0.65rem 0.85rem; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: #f8fafc; font-size: 0.85rem; color: var(--muted); }
td.num, th.num { text-align: right; }
tbody tr:last-child td { border-bottom: none; }

/* ---------- Interruptor ---------- */
.switch { position: relative; display: inline-block; width: 52px; height: 30px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; inset: 0;
  background: #cbd5e1; border-radius: 999px; transition: 0.2s;
}
.slider::before {
  content: ""; position: absolute; height: 22px; width: 22px; left: 4px; top: 4px;
  background: #fff; border-radius: 50%; transition: 0.2s;
}
.switch input:checked + .slider { background: var(--verde); }
.switch input:checked + .slider::before { transform: translateX(22px); }

/* ---------- Panel superior: calendario (izq) + resumen (der) ---------- */
.panel-superior {
  display: grid;
  grid-template-columns: 1fr minmax(280px, 360px);
  gap: 1.25rem;
  align-items: start;
  margin-bottom: 1.25rem;
}
.panel-superior .calendario { margin-bottom: 0; }
.panel-superior .calendario-cab { margin-bottom: 0.75rem; }
.resumen-panel h2 { margin: 0 0 0.75rem; font-size: 1.15rem; }
.resumen-panel .tarjetas { grid-template-columns: repeat(2, 1fr); margin-bottom: 0; }
.resumen-iva-titulo { margin: 1.1rem 0 0.6rem; font-size: 1rem; }
@media (max-width: 900px) {
  .panel-superior { grid-template-columns: 1fr; }
}

/* ---------- Distintivo de rol ---------- */
.badge-rol {
  font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em;
  background: var(--ambar-bg); color: var(--ambar);
  padding: 0.15rem 0.5rem; border-radius: 999px; margin-left: 0.35rem;
}

/* ---------- Celda de acción (interruptor + fecha + nota) ---------- */
.accion-cel { text-align: center; white-space: normal; }
.marca-info {
  display: flex; align-items: center; justify-content: center; gap: 0.3rem;
  margin-top: 0.3rem; font-size: 0.78rem; color: var(--muted);
}
.marca-fecha { white-space: nowrap; }
.marca-nota { cursor: help; }
.icono-editar {
  border: none; background: transparent; cursor: pointer;
  font-size: 0.95rem; color: var(--brand); padding: 0 0.15rem; line-height: 1;
}
.icono-editar:hover { color: var(--brand-dark); }

/* ---------- Filas anuladas y ajustes (NC/ND) ---------- */
tr.anulada td { text-decoration: line-through; color: var(--muted); }
tr.anulada .badge-anulada { text-decoration: none; }
tr.ajuste { background: #fafbfc; }
tr.ajuste td { color: var(--muted); }
.badge-anulada, .badge-nc, .badge-nd {
  display: inline-block; font-size: 0.68rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.02em; padding: 0.1rem 0.45rem; border-radius: 999px;
  vertical-align: middle; white-space: nowrap;
}
.badge-anulada { background: var(--rojo-bg); color: var(--rojo); }
.badge-nc { background: var(--rojo-bg); color: var(--rojo); text-transform: none; }
.badge-nd { background: var(--ambar-bg); color: var(--ambar); text-transform: none; }
.abono { font-size: 0.78rem; }

/* ---------- Modal fecha + nota ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(16, 24, 40, 0.5);
  display: grid; place-items: center; padding: 1rem;
}
.modal {
  background: var(--surface); border-radius: 16px; padding: 1.5rem;
  width: 100%; max-width: 420px; box-shadow: var(--shadow);
}
.modal h3 { margin: 0 0 1rem; font-size: 1.15rem; }
.modal .campo { display: block; margin-bottom: 1rem; }
.modal .campo > span { display: block; font-size: 0.9rem; color: var(--muted); margin-bottom: 0.35rem; }
.modal .campo input, .modal .campo textarea {
  width: 100%; padding: 0.6rem 0.7rem; border: 1px solid var(--border);
  border-radius: 8px; font: inherit; color: var(--text); background: var(--surface);
}
.modal .campo textarea { resize: vertical; }
.modal-acciones { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 0.25rem; }

/* ---------- Utilidades ---------- */
.fila-acciones { display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
.spinner { display: inline-block; width: 1em; height: 1em; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.7s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }
.oculto { display: none !important; }

/* ---------- Clientes ---------- */
.clientes-layout {
  display: grid; grid-template-columns: minmax(260px, 340px) 1fr;
  gap: 1.25rem; align-items: start;
}
.input {
  padding: 0.6rem 0.7rem; border: 1px solid var(--border); border-radius: 8px;
  font: inherit; background: var(--surface); color: var(--text);
}
.lista-clientes {
  list-style: none; margin: 0; padding: 0; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.item-cliente { padding: 0.7rem 0.9rem; border-bottom: 1px solid var(--border); cursor: pointer; }
.item-cliente:last-child { border-bottom: none; }
.item-cliente:hover { background: var(--gris-bg); }
.item-cliente.activo { background: var(--ambar-bg); }
.ic-razon { font-weight: 600; }
.ic-sub { font-size: 0.8rem; }

.cliente-detalle {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.25rem; box-shadow: var(--shadow);
}
.detalle-cab { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; }
.cliente-notas { background: var(--gris-bg); border-radius: 8px; padding: 0.6rem 0.8rem; margin: 0.6rem 0; white-space: pre-wrap; }

.contactos { display: grid; gap: 0.6rem; margin-top: 0.6rem; }
.contacto-card { display: flex; justify-content: space-between; gap: 0.8rem; border: 1px solid var(--border); border-radius: var(--radius); padding: 0.8rem; }
.contacto-nombre { font-weight: 600; }
.contacto-sub { font-size: 0.85rem; margin: 0.15rem 0 0.5rem; }
.contacto-acciones { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.contacto-admin { display: flex; gap: 0.2rem; align-items: flex-start; }

.btn-accion { font-size: 0.8rem; font-weight: 600; padding: 0.35rem 0.7rem; border-radius: 8px; text-decoration: none; color: #fff; white-space: nowrap; }
.btn-accion.wa { background: #25d366; }
.btn-accion.tel { background: var(--brand); }
.btn-accion.mail { background: #6b7280; }
.btn-accion:hover { opacity: 0.9; }

.badge-principal { display: inline-block; font-size: 0.66rem; font-weight: 700; text-transform: uppercase; background: var(--verde-bg); color: var(--verde); padding: 0.1rem 0.4rem; border-radius: 999px; vertical-align: middle; }
.hist-titulo { margin: 1rem 0 0.4rem; }

.modal .campo select { width: 100%; padding: 0.6rem 0.7rem; border: 1px solid var(--border); border-radius: 8px; font: inherit; background: var(--surface); color: var(--text); }
.modal .campo-check { flex-direction: row; align-items: center; gap: 0.5rem; }
.modal .campo-check > span { display: inline; margin: 0; }

@media (max-width: 800px) { .clientes-layout { grid-template-columns: 1fr; } }

/* ---------- Trabajos ---------- */
.filtro { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.78rem; color: var(--muted); }
.filtro .input, .filtro select { font-size: 0.95rem; }
.badge-sinfac { display: inline-block; font-size: 0.68rem; font-weight: 700; text-transform: uppercase; background: var(--ambar-bg); color: var(--ambar); padding: 0.1rem 0.45rem; border-radius: 999px; }
.btn-mini { padding: 0.3rem 0.6rem; font-size: 0.85rem; }
.acciones-cel { white-space: nowrap; display: flex; gap: 0.25rem; align-items: center; }
.asoc-lista { max-height: 260px; overflow: auto; }
.asoc-row { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; padding: 0.45rem 0.2rem; border-bottom: 1px solid var(--border); }
.asoc-row:last-child { border-bottom: none; }
.asoc-row.anulada > span { text-decoration: line-through; color: var(--muted); }
.asoc-row.anulada .badge-anulada { text-decoration: none; }

/* ---------- Responsive general (móvil) ---------- */
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; gap: 0.5rem; padding: 0.6rem 0.9rem; }
  .container { padding: 1rem 0.8rem; }
  h1 { font-size: 1.4rem; }
  .modal { padding: 1.2rem; }
}
