/* ============ VARIABLES TEMA ============ */
:root.theme-dark, .theme-dark {
  --bg: #0f1115;
  --bg-soft: #171a21;
  --bg-card: #1c2029;
  --bg-hover: #232833;
  --border: #2a2f3a;
  --text: #e4e6eb;
  --text-muted: #8a94a6;
  --primary: #6366f1;
  --primary-hover: #7c7ff2;
  --danger: #ef4444;
  --success: #10b981;
  --warning: #f59e0b;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 4px 16px rgba(0,0,0,.35);
  --transition: .2s ease;
}

/* ============ BASE ============ */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select, textarea {
  font-family: inherit; font-size: 14px;
  background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 12px; width: 100%;
  transition: border var(--transition);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 6px; margin-top: 10px; }

/* ============ LAYOUT ============ */
.layout { display: flex; flex-direction: column; min-height: 100vh; }
.nav {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 24px;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 50;
}
.nav-logo { font-size: 18px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.nav-links { display: flex; gap: 4px; flex: 1; }
.nav-link {
  padding: 8px 14px; border-radius: var(--radius-sm);
  color: var(--text-muted); transition: all var(--transition);
}
.nav-link:hover { background: var(--bg-hover); color: var(--text); }
.nav-link.active { background: var(--primary); color: #fff; }
.nav-user { display: flex; align-items: center; gap: 10px; color: var(--text-muted); }
.btn-logout { padding: 6px 12px; border-radius: var(--radius-sm); color: var(--text-muted); }
.btn-logout:hover { color: var(--danger); }

.main { flex: 1; padding: 24px; max-width: 1400px; width: 100%; margin: 0 auto; }

.footer {
  padding: 16px 24px; border-top: 1px solid var(--border);
  color: var(--text-muted); font-size: 12px; text-align: center;
}

/* ============ BOTONES ============ */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: var(--radius-sm);
  background: var(--bg-card); color: var(--text);
  border: 1px solid var(--border);
  transition: all var(--transition); font-size: 13px;
}
.btn:hover { background: var(--bg-hover); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-icon { padding: 6px 8px; }

/* ============ LOADER / SPINNER ============ */
#loader {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg); z-index: 9999;
}
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ LOGIN ============ */
.login-body { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; }
.login-card {
  background: var(--bg-card); padding: 36px; border-radius: var(--radius);
  width: 100%; max-width: 360px;
  border: 1px solid var(--border); box-shadow: var(--shadow);
}
.login-logo { font-size: 22px; text-align: center; margin-bottom: 8px; }
.login-card h1 { font-size: 18px; margin-bottom: 20px; text-align: center; font-weight: 500; color: var(--text-muted); }
.login-card button[type=submit] {
  width: 100%; padding: 12px; margin-top: 18px;
  background: var(--primary); color: #fff; border: none; border-radius: var(--radius-sm);
  font-weight: 500; transition: background var(--transition);
}
.login-card button[type=submit]:hover { background: var(--primary-hover); }
.error { color: var(--danger); font-size: 13px; margin-top: 12px; min-height: 18px; text-align: center; }
.login-footer { margin-top: 24px; color: var(--text-muted); font-size: 12px; }

/* ============ TRABAJOS LIST ============ */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.page-header h1 { font-size: 22px; font-weight: 600; }

.filter-bar {
  display: flex; gap: 10px; flex-wrap: wrap;
  padding: 14px; background: var(--bg-card); border-radius: var(--radius);
  border: 1px solid var(--border); margin-bottom: 14px;
}
.filter-bar input[type=text] { flex: 1; min-width: 180px; }
.date-tabs { display: flex; gap: 4px; background: var(--bg-soft); padding: 3px; border-radius: var(--radius-sm); }
.date-tabs button {
  padding: 6px 12px; border-radius: 4px; font-size: 12px;
  color: var(--text-muted); transition: all var(--transition);
}
.date-tabs button.active { background: var(--primary); color: #fff; }
.date-tabs button:hover:not(.active) { background: var(--bg-hover); color: var(--text); }

/* Multi-select tipos */
.tipos-multi {
  position: relative; min-width: 220px;
}
.tipos-multi-display {
  min-height: 38px; padding: 6px 10px;
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  display: flex; flex-wrap: wrap; gap: 4px; align-items: center;
}
.tipos-multi-display .placeholder { color: var(--text-muted); font-size: 13px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 8px; background: var(--primary); color: #fff;
  border-radius: 12px; font-size: 11px;
}
.chip button { color: #fff; font-weight: bold; padding: 0 2px; }
.tipos-multi-dropdown {
  position: absolute; top: 100%; left: 0; right: 0;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-sm); margin-top: 4px;
  max-height: 240px; overflow-y: auto; z-index: 20;
  box-shadow: var(--shadow);
}
.tipos-multi-dropdown div {
  padding: 8px 12px; cursor: pointer; font-size: 13px;
  transition: background var(--transition);
}
.tipos-multi-dropdown div:hover { background: var(--bg-hover); }

/* Lista de trabajos */
.trabajos-list { display: flex; flex-direction: column; gap: 6px; }
.trabajo-item {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
  transition: all var(--transition);
}
.trabajo-item:hover { border-color: var(--primary); }
.trabajo-header {
  display: grid; grid-template-columns: 6px 90px 110px 1fr 1fr auto;
  align-items: center; gap: 14px; padding: 14px 16px;
  cursor: pointer;
}
.trabajo-color-bar { width: 6px; height: 32px; border-radius: 3px; }
.trabajo-codigo { font-weight: 600; font-size: 15px; }
.trabajo-fecha { color: var(--text-muted); font-size: 12px; }
.trabajo-cliente { font-weight: 500; }
.trabajo-tipos { display: flex; gap: 4px; flex-wrap: wrap; }
.tipo-tag {
  font-size: 11px; padding: 2px 8px;
  background: var(--bg-soft); border-radius: 10px;
  color: var(--text-muted);
}
.trabajo-actions { display: flex; gap: 6px; }

.trabajo-details {
  display: none; padding: 0 16px 14px;
  border-top: 1px solid var(--border); animation: fadeIn 0.2s;
}
.trabajo-item.expanded .trabajo-details { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; padding-top: 14px; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }

.detail-box { font-size: 13px; }
.detail-label { color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; margin-bottom: 3px; }
.detail-value { font-weight: 500; }

.estado-badge {
  display: inline-block; padding: 3px 10px; border-radius: 10px;
  font-size: 11px; font-weight: 500; color: #fff;
}

/* Empty state */
.empty { text-align: center; padding: 60px 20px; color: var(--text-muted); }

/* Paginación */
.pagination { display: flex; justify-content: center; gap: 6px; margin-top: 20px; }
.pagination button { padding: 6px 12px; min-width: 36px; }
.pagination button.active { background: var(--primary); color: #fff; }

/* ============ DRAWER (ventana deslizable) ============ */
.drawer { position: fixed; inset: 0; z-index: 100; pointer-events: none; }
.drawer[aria-hidden="false"] { pointer-events: auto; }
.drawer-backdrop {
  position: absolute; inset: 0; background: rgba(0,0,0,0.5);
  opacity: 0; transition: opacity var(--transition);
}
.drawer[aria-hidden="false"] .drawer-backdrop { opacity: 1; }
.drawer-panel {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: 66.66vw; max-width: 900px; min-width: 380px;
  background: var(--bg); border-left: 1px solid var(--border);
  transform: translateX(100%); transition: transform var(--transition);
  overflow-y: auto; box-shadow: -4px 0 20px rgba(0,0,0,0.3);
}
.drawer[aria-hidden="false"] .drawer-panel { transform: translateX(0); }
@media (max-width: 640px) { .drawer-panel { width: 100vw; } }

.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--bg); z-index: 2;
}
.drawer-header h2 { font-size: 18px; font-weight: 600; }
.drawer-close { font-size: 22px; color: var(--text-muted); padding: 4px 10px; border-radius: var(--radius-sm); }
.drawer-close:hover { background: var(--bg-hover); color: var(--text); }
.drawer-body { padding: 20px; }

/* Formularios */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px 16px; }
.form-row { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); }

/* Buscador con avatar (cliente) */
.search-select { position: relative; }
.search-select-input {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-soft); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 4px 10px;
}
.search-select-input input { background: transparent; border: none; padding: 6px 0; }
.search-select-input input:focus { border: none; }
.client-avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--bg-hover); display: flex; align-items: center; justify-content: center;
  font-size: 11px; color: var(--text-muted); flex-shrink: 0; overflow: hidden;
}
.client-avatar img { width: 100%; height: 100%; object-fit: cover; }
.search-results {
  position: absolute; top: 100%; left: 0; right: 0;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-sm); margin-top: 4px;
  max-height: 260px; overflow-y: auto; z-index: 30;
  box-shadow: var(--shadow); display: none;
}
.search-results.show { display: block; }
.search-result-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; cursor: pointer; font-size: 13px;
  transition: background var(--transition);
}
.search-result-item:hover { background: var(--bg-hover); }
.search-result-item.add-new { color: var(--primary); font-weight: 500; border-top: 1px solid var(--border); }

/* Files uploader */
.file-drop {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 20px; text-align: center; color: var(--text-muted);
  cursor: pointer; transition: all var(--transition);
}
.file-drop:hover, .file-drop.drag { border-color: var(--primary); color: var(--text); }
.file-list { margin-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.file-list div {
  display: flex; justify-content: space-between; padding: 6px 10px;
  background: var(--bg-soft); border-radius: var(--radius-sm); font-size: 12px;
}

/* ============ TOASTS ============ */
#toasts { position: fixed; top: 20px; right: 20px; z-index: 1000; display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 12px 18px; background: var(--bg-card);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); min-width: 240px;
  animation: slideIn 0.25s;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ============ MODAL ============ */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 200;
  animation: fadeBg 0.2s;
}
@keyframes fadeBg { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; width: 100%; max-width: 420px;
  box-shadow: var(--shadow);
}
.modal h3 { margin-bottom: 14px; font-size: 16px; }

/* ============ RESPONSIVE AÑADIDO ============ */
/* =====================================================
   ============  RESPONSIVE — solo añadidos  ===========
   =====================================================
   Todo lo de aquí abajo es aditivo. No modifica reglas
   anteriores. Se puede borrar en bloque sin romper nada.
   ===================================================== */

/* --- Botón hamburguesa (oculto por defecto en desktop) --- */
.nav-toggle {
  display: none;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  background: transparent;
  border: 1px solid var(--border);
  font-size: 18px;
  line-height: 1;
}
.nav-toggle:hover { background: var(--bg-hover); }

/* Evita desbordes horizontales */
html, body { overflow-x: hidden; }
.main { width: 100%; }

/* =========================================
   Breakpoint tablet/mobile — hasta 900px
   ========================================= */
@media (max-width: 900px) {
  .nav {
    padding: 10px 14px;
    flex-wrap: wrap;
    gap: 10px;
  }
  .nav-logo { font-size: 16px; flex: 1; }

  /* Mostrar hamburguesa */
  .nav-toggle { display: inline-flex; align-items: center; justify-content: center; }

  /* Menú colapsable */
  .nav-links {
    display: none;
    order: 10;
    flex-basis: 100%;
    flex-direction: column;
    gap: 2px;
    padding: 8px 0 4px;
    border-top: 1px solid var(--border);
    margin-top: 8px;
  }
  .nav-links.open { display: flex; }
  .nav-link { padding: 10px 12px; width: 100%; }

  /* Botón "Nuevo trabajo" y usuario: caben en una segunda línea */
  #btnNuevoTrabajo, .nav-user, .btn-logout { font-size: 12px; }
  .nav-user { font-size: 12px; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .main { padding: 16px; }
  .page-header { flex-wrap: wrap; gap: 10px; }

  /* Filtros: que se apilen correctamente */
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar input[type=text],
  .filter-bar select,
  .filter-bar .tipos-multi { min-width: 0; width: 100%; }
  .date-tabs { overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }
  .date-tabs button { flex-shrink: 0; }

  /* Lista de trabajos: pasar a layout en 2 filas */
  .trabajo-header {
    grid-template-columns: 6px 1fr auto !important;
    grid-template-areas:
      "bar codigo actions"
      "bar cliente cliente"
      "bar tipos tipos"
      "bar estado estado";
    gap: 6px 10px;
    padding: 12px;
  }
  .trabajo-header > .trabajo-color-bar { grid-area: bar; height: 100%; }
  .trabajo-header > div:nth-child(2) { grid-area: codigo; }
  .trabajo-header > div:nth-child(3) { grid-area: estado; }
  .trabajo-header > .trabajo-cliente { grid-area: cliente; }
  .trabajo-header > .trabajo-tipos { grid-area: tipos; }
  .trabajo-header > .trabajo-actions { grid-area: actions; }

  .trabajo-item.expanded .trabajo-details {
    grid-template-columns: 1fr 1fr;
  }

  /* Clientes */
  .cli-header {
    grid-template-columns: 42px 1fr auto !important;
    grid-template-areas:
      "avatar nombre actions"
      "avatar clinica clinica"
      "avatar tel tel"
      "avatar dir dir";
    gap: 4px 12px;
  }
  .cli-header > .cli-avatar-lg { grid-area: avatar; }
  .cli-header > div:nth-child(2) { grid-area: nombre; }
  .cli-header > div:nth-child(3) { grid-area: tel; color: var(--text-muted); }
  .cli-header > div:nth-child(4) { grid-area: dir; color: var(--text-muted); }
  .cli-header > .cli-actions { grid-area: actions; flex-wrap: wrap; justify-content: flex-end; }
  .cli-actions .btn-sm { font-size: 11px; padding: 4px 8px; }

  .cli-item.expanded .cli-details {
    grid-template-columns: 1fr 1fr;
  }

  /* Trabajadores */
  .tra-header {
    grid-template-columns: 42px 1fr auto !important;
    grid-template-areas:
      "avatar nombre actions"
      "avatar rol rol"
      "avatar estado estado";
    gap: 4px 12px;
  }
  .tra-header > .tra-avatar { grid-area: avatar; }
  .tra-header > div:nth-child(2) { grid-area: nombre; }
  .tra-header > div:nth-child(3) { grid-area: rol; }
  .tra-header > div:nth-child(4) { grid-area: estado; }
  .tra-header > .trabajo-actions { grid-area: actions; flex-wrap: wrap; justify-content: flex-end; }

  /* Servicios */
  .srv-header {
    grid-template-columns: 40px 1fr auto !important;
    grid-template-areas:
      "icon nombre actions"
      "icon meta meta";
    gap: 4px 12px;
  }
  .srv-header > .srv-icon { grid-area: icon; }
  .srv-header > div:nth-child(2) { grid-area: nombre; }
  .srv-header > div:nth-child(3),
  .srv-header > div:nth-child(4) { grid-area: meta; display: inline; margin-right: 10px; }
  .srv-header > div:last-child { grid-area: actions; flex-wrap: wrap; justify-content: flex-end; }

  /* Facturas — header del cliente */
  .fac-header-cli { flex-wrap: wrap; }
  .fac-header-cli .cli-meta { gap: 8px; }
  .fac-list-item { flex-direction: column; align-items: stretch; gap: 10px; }
  .fac-list-item .fac-info { flex-wrap: wrap; gap: 10px; }

  /* Drawer: pantalla completa en móvil */
  .drawer-panel { width: 100vw !important; min-width: 0 !important; max-width: 100vw !important; }

  /* Form grid más compacto en drawer */
  .form-grid { grid-template-columns: 1fr; }

  /* Editor de etapas (servicios) en móvil */
  .etapa-row {
    grid-template-columns: 24px 26px 1fr 24px;
    grid-template-areas:
      "drag num nombre rm"
      "drag num desc rm"
      ". . opt opt";
    gap: 4px 6px;
  }
  .etapa-row > .etapa-drag { grid-area: drag; }
  .etapa-row > .etapa-num { grid-area: num; }
  .etapa-row > input[data-f="nombre"] { grid-area: nombre; }
  .etapa-row > input[data-f="descripcion"] { grid-area: desc; }
  .etapa-row > .opt-label { grid-area: opt; justify-content: flex-start; margin-top: 4px; }
  .etapa-row > .etapa-rm { grid-area: rm; }

  /* Carrito */
  .cart-tipo-row {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "nombre rm"
      "cant valor"
      "desc sub";
    gap: 6px;
  }
  .cart-tipo-row .lbl-nombre:first-child { grid-area: nombre; }
  .cart-tipo-row input[data-f="cantidad"] { grid-area: cant; }
  .cart-tipo-row input[data-f="valor"] { grid-area: valor; }
  .cart-tipo-row input[data-f="descuento"] { grid-area: desc; }
  .cart-tipo-row .lbl-nombre:nth-last-child(2) { grid-area: sub; }
  .cart-tipo-row button { grid-area: rm; justify-self: end; }

  /* Ajustes en 1 columna */
  .ajustes-grid { grid-template-columns: 1fr; }
  .rec-grid { grid-template-columns: 1fr; }

  /* Paginación */
  .pagination { flex-wrap: wrap; }

  /* Acciones en lista: botones más pequeños */
  .trabajo-actions .btn-sm,
  .cli-actions .btn-sm { font-size: 11px; padding: 4px 8px; }
}

/* =========================================
   Breakpoint móvil pequeño — hasta 480px
   ========================================= */
@media (max-width: 480px) {
  .trabajo-item.expanded .trabajo-details,
  .cli-item.expanded .cli-details,
  .tra-item.expanded .tra-details { grid-template-columns: 1fr; }

  .filter-bar .tipos-multi-display { min-height: 38px; }
  .page-header h1 { font-size: 18px; }

  /* Reduce padding para ganar espacio */
  .trabajo-header, .cli-header, .tra-header, .srv-header { padding: 10px; }
}