:root {
  /* Paleta de marca Serrallo Capital */
  --color-bg: #F4F7FA;       /* Papel */
  --color-bg-2: #E9EEF3;
  --color-surface: #ffffff;
  --color-border: rgba(11, 34, 57, 0.14);   /* Línea */
  --color-text: #0B2239;                    /* Tinta */
  --color-text-muted: rgba(11, 34, 57, 0.66);
  --color-primary: #1B4B73;                 /* Azul */
  --color-primary-dark: #0B2239;            /* Tinta */
  --color-accent: #4A87B8;                  /* Acero */
  --color-piedra: #DDE4EB;
  --color-danger: #b3261e;
  --color-warning: #b8860b;
  --radius: 8px;
  --shadow: 0 16px 40px rgba(11, 34, 57, 0.08);
  --font-display: "Marcellus", Georgia, "Times New Roman", serif;
  --font-body: "Archivo", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, Arial, sans-serif;
  font-size: 15px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-body);
  background: var(--color-bg);
  color: var(--color-text);
}

#app-shell { min-height: 100vh; display: flex; flex-direction: column; }

/* Header */
header.app-header {
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1.2rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.brand { display: flex; align-items: center; gap: 0.6rem; }
.brand-tile {
  width: 30px; height: 30px; border-radius: 22%;
  background: var(--color-primary-dark);
  display: flex; align-items: center; justify-content: center; flex: none;
}
.brand-tile svg { width: 62%; fill: var(--color-bg); }
header.app-header h1 { font-family: var(--font-display); font-weight: 400; font-size: 1.3rem; margin: 0; letter-spacing: 0.01em; }
.header-user { font-size: 0.85rem; display: flex; align-items: center; gap: 0.6rem; }
.badge-mock {
  background: var(--color-warning);
  color: #fff;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
}

/* User chip (avatar + nombre + email) */
.user-chip {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--color-surface);
  border-radius: 999px;
  padding: 0.3rem 0.9rem 0.3rem 0.3rem;
  box-shadow: var(--shadow);
}
.avatar-img, .avatar-fallback {
  border-radius: 50%;
  flex: none;
  object-fit: cover;
}
.avatar-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 0.75rem;
  font-family: var(--font-body);
}
.user-chip-info { display: flex; flex-direction: column; line-height: 1.25; }
.user-chip-name { font-weight: 700; font-size: 0.85rem; color: var(--color-text); }
.user-chip-email { font-size: 0.72rem; color: var(--color-text-muted); }

/* "Tirar hacia abajo para recargar" (ver setupPullToRefresh en main.js) */
.pull-refresh-indicator {
  position: fixed; top: 0; left: 0; right: 0; height: 0;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; background: var(--color-bg); z-index: 500; pointer-events: none;
  transition: height 0.15s ease;
}
.pull-refresh-spinner {
  display: inline-block; font-size: 1.3rem; color: var(--color-primary);
  transform: rotate(0deg); transition: transform 0.15s ease;
}
.pull-refresh-spinner-ready { transform: rotate(180deg); }
.pull-refresh-spinner-spin { animation: pull-refresh-spin 0.7s linear infinite; }
@keyframes pull-refresh-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* Tabs */
nav.tabs {
  display: flex;
  gap: 0.2rem;
  background: var(--color-primary-dark);
  padding: 0 1rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
nav.tabs button {
  background: transparent;
  border: none;
  color: rgba(244, 247, 250, 0.72);
  padding: 0.7rem 1rem;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.88rem;
  white-space: nowrap;
  border-bottom: 3px solid transparent;
  /* En iPad, tocar un botón dentro de una barra con scroll horizontal a veces se interpreta
     como el inicio de un arrastre en vez de un toque, y el clic no llega a dispararse.
     touch-action: manipulation quita esa ambigüedad (bug ya visto y corregido en el proyecto
     Contract Management, ahí en su lista de inquilinos con scroll). */
  touch-action: manipulation;
}
nav.tabs button:hover { color: #fff; }
nav.tabs button.active {
  color: #fff;
  border-bottom-color: var(--color-accent);
  font-weight: 600;
}

main#view-root { flex: 1; padding: 1.2rem; max-width: 1400px; width: 100%; margin: 0 auto; }

/* Cards / panels */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.2rem;
  margin-bottom: 1rem;
}
.card h2 { margin-top: 0; font-family: var(--font-display); font-weight: 400; font-size: 1.25rem; color: var(--color-primary-dark); }
.card h3 { font-size: 0.8rem; font-weight: 600; color: var(--color-accent); text-transform: uppercase; letter-spacing: 0.1em; }

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* Forms */
label { display: block; font-size: 0.82rem; font-weight: 600; margin-bottom: 0.25rem; color: var(--color-text-muted); }
.field { margin-bottom: 0.9rem; }
input[type="text"], input[type="number"], input[type="date"], input[type="email"],
select, textarea {
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  font-size: 0.9rem;
  background: #fff;
  color: var(--color-text);
}
textarea { resize: vertical; min-height: 4.5rem; }
.checkbox-row { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 0.4rem; }
.checkbox-row input { width: auto; }
.checkbox-group { display: flex; flex-wrap: wrap; gap: 0.8rem; }
.pill-checkbox {
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 0.35rem 0.8rem;
  cursor: pointer;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: #fff;
  touch-action: manipulation;
}
.pill-checkbox.checked { background: var(--color-bg-2); border-color: var(--color-accent); color: var(--color-primary-dark); font-weight: 600; }

.field-error { color: var(--color-danger); font-size: 0.78rem; margin-top: 0.2rem; }
.field.invalid input, .field.invalid select { border-color: var(--color-danger); }

/* Buttons */
button { font-family: inherit; }
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid var(--color-border);
  background: #fff;
  color: var(--color-text);
  padding: 0.5rem 0.9rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  touch-action: manipulation;
}
.btn:hover { background: var(--color-bg-2); border-color: var(--color-accent); }
.btn-primary { background: var(--color-primary); border-color: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-dark); }
.btn-danger { background: var(--color-danger); border-color: var(--color-danger); color: #fff; }
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.78rem; }
.btn-row { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.6rem; }

/* Table */
.bulk-bar {
  background: var(--color-bg-2); border: 1px solid var(--color-border); border-radius: 6px;
  padding: 0.5rem 0.8rem; margin-bottom: 0.8rem; align-items: center; gap: 0.8rem;
}
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--color-border); text-align: left; }
th { background: var(--color-bg-2); font-weight: 600; color: var(--color-text-muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; }
tr:hover td { background: var(--color-bg-2); }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.th-filter-btn:hover { color: var(--color-primary) !important; }

.tag { display: inline-block; padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600; }
.tag-borrador { background: var(--color-piedra); color: var(--color-text-muted); }
.tag-confirmada { background: #dbeafe; color: var(--color-primary); }
.tag-facturada { background: #dcfce7; color: #166534; }
.tag-nofacturable { background: #ede9fe; color: #6d28d9; }

.badge { display: inline-block; padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600; }
.badge-ok { background: #dcfce7; color: #166534; }
.badge-warning { background: #fffbea; color: #92660a; border: 1px solid var(--color-warning); }
.badge-error { background: #fde8e8; color: #a12626; }
.badge-new { background: #dbeafe; color: var(--color-primary); }

.import-preview-table tr.row-error td { background: #fef2f2; }
.import-preview-table tr.row-warning td { background: #fffbea; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 25, 35, 0.5);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 1rem;
}
.modal {
  background: #fff; border-radius: var(--radius); padding: 1.2rem 1.4rem; max-width: 560px; width: 100%;
  max-height: 90vh; overflow-y: auto; -webkit-overflow-scrolling: touch; box-shadow: 0 10px 40px rgba(0,0,0,0.25);
}
.modal h3 { margin-top: 0; }
.modal-close-row { display: flex; justify-content: flex-end; margin: -0.4rem -0.6rem 0 0; }
.modal-close-btn {
  background: none; border: none; font-size: 1.5rem; line-height: 1; cursor: pointer;
  color: var(--color-text-muted); padding: 0.3rem 0.6rem; touch-action: manipulation;
}
.modal-close-btn:hover { color: var(--color-text); }

/* Toasts */
#toast-root { position: fixed; bottom: 1rem; right: 1rem; display: flex; flex-direction: column; gap: 0.5rem; z-index: 200; }
.toast { background: var(--color-primary-dark); color: #fff; padding: 0.6rem 1rem; border-radius: 6px; font-size: 0.85rem; box-shadow: var(--shadow); }
.toast-error { background: var(--color-danger); }
.toast-success { background: var(--color-accent); }

.muted { color: var(--color-text-muted); }
.text-right { text-align: right; }
.hint { font-size: 0.78rem; color: var(--color-text-muted); margin-top: 0.2rem; }

.stat-tile { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius); padding: 0.9rem 1rem; }
.stat-tile .value { font-size: 1.6rem; font-weight: 700; color: var(--color-primary-dark); }
.stat-tile .label { font-size: 0.78rem; color: var(--color-text-muted); text-transform: uppercase; letter-spacing: 0.03em; }

.chart-box { position: relative; height: 280px; }

.incidencia-row { border: 1px solid var(--color-warning); background: #fffbea; border-radius: 6px; padding: 0.6rem; margin-bottom: 0.5rem; }

@media print {
  /* Solo se imprime la proforma (#proforma-printable): todo lo demás (cabecera, pestañas, el
     resumen mensual, los filtros, los botones, y en el caso de reimprimir una factura archivada
     también el resto del modal) se retira del todo con display:none — no basta con
     visibility:hidden, que preserva el hueco en el flujo del documento y puede añadir una página
     en blanco de sobra si el resto de la pantalla (oculta) es más alta que una página A4. Solo se
     dejan visibles los ancestros de #proforma-printable (para no romper la cadena hasta él) y su
     propio contenido. */
  @page { size: A4; margin: 10mm; }
  body *:not(:has(#proforma-printable)):not(#proforma-printable):not(#proforma-printable *) {
    display: none !important;
  }
  #proforma-printable {
    position: static; margin: 0; padding: 0; box-shadow: none; border: none;
  }
  /* Por defecto, los navegadores no imprimen colores de fondo salvo que el usuario marque a mano
     la casilla "Gráficos de fondo" del diálogo de impresión — así, sin este forzado, el PDF sale
     en blanco y negro (cabecera, franjas de categoría y recuadro de totales pierden el color) aun
     con exactamente el mismo CSS que se ve en pantalla. Esto obliga a imprimir los colores tal
     cual, sin depender de esa casilla. */
  #proforma-printable, #proforma-printable * {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    color-adjust: exact;
  }
  .modal-backdrop { position: static !important; background: none !important; padding: 0 !important; }
  .modal { max-height: none !important; overflow: visible !important; box-shadow: none !important; }
  body { background: #fff; }
  /* Todo el diseño de la proforma está en rem/em, así que reducir el tamaño de fuente base la
     escala entera (cabecera, tablas, totales) manteniendo las mismas proporciones, para que quepa
     en una sola página en vez de desbordar a una segunda casi vacía. */
  :root { font-size: 10.5px; }
  th, td { padding: 0.4rem 0.5rem; }
  .invoice-header { page-break-inside: avoid; }
  .invoice-proyecto { page-break-inside: avoid; }
  .invoice-proyecto-title, tr.invoice-categoria-row { page-break-after: avoid; }
  .invoice-totales { page-break-inside: avoid; }
}

/* ---------- Proforma con aspecto de factura ---------- */
#proforma-printable { padding: 1.6rem; }

.invoice-header {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 1.5rem; border-bottom: 3px solid var(--color-primary-dark); padding-bottom: 1rem; margin-bottom: 1.4rem;
}
.invoice-brand { display: flex; align-items: center; gap: 0.7rem; }
.invoice-brand img { width: 2.9em; height: 2.9em; border-radius: 8px; }
.invoice-brand-name { font-family: var(--font-display); font-size: 1.15rem; color: var(--color-primary-dark); line-height: 1.25; }
.invoice-brand-sub { font-size: 0.75rem; color: var(--color-text-muted); margin-top: 0.1rem; }
.invoice-title-block { text-align: right; }
.invoice-title { font-family: var(--font-display); font-size: 1.7rem; color: var(--color-primary-dark); margin: 0; letter-spacing: 0.04em; }
.invoice-meta { font-size: 0.82rem; color: var(--color-text-muted); margin-top: 0.35rem; line-height: 1.5; }
.invoice-meta strong { color: var(--color-text); }

.invoice-proyecto { margin-top: 1.4rem; }
.invoice-proyecto-title {
  font-family: var(--font-display); font-weight: 400; font-size: 1.05rem; color: #fff; background: var(--color-primary-dark);
  padding: 0.45rem 0.8rem; margin: 0; border-radius: 6px 6px 0 0;
}
.invoice-proyecto .table-scroll table { border: 1px solid var(--color-border); border-top: none; }
tr.invoice-categoria-row td {
  background: var(--color-bg-2); font-weight: 600; color: var(--color-primary); font-size: 0.78rem;
  text-transform: uppercase; letter-spacing: 0.03em; padding-top: 0.5rem; padding-bottom: 0.5rem;
}
tr.invoice-categoria-row:hover td { background: var(--color-bg-2); }
.invoice-proyecto td.concepto { padding-left: 1.5rem; }
.invoice-proyecto td.horas, .invoice-proyecto th.horas { text-align: right; white-space: nowrap; }

.invoice-totales { margin-top: 1.5rem; max-width: 340px; margin-left: auto; border: 1px solid var(--color-border); border-radius: 6px; overflow: hidden; }
.invoice-totales .fila { display: flex; justify-content: space-between; gap: 1rem; padding: 0.45rem 0.9rem; font-size: 0.88rem; border-bottom: 1px solid var(--color-border); }
.invoice-totales .fila:last-child { border-bottom: none; }
.invoice-totales .fila.total { background: var(--color-primary-dark); color: #fff; font-size: 1.05rem; font-weight: 700; padding: 0.65rem 0.9rem; }

/* ---------- Calendario estilo Outlook ---------- */
.cal-toolbar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 0.6rem; }
.cal-label { margin: 0 0.3rem; font-size: 0.95rem; text-transform: capitalize; }
.cal-view-switch { display: flex; border: 1px solid var(--color-border); border-radius: 6px; overflow: hidden; }
.cal-view-btn {
  background: #fff; border: none; border-left: 1px solid var(--color-border);
  padding: 0.4rem 0.85rem; font-size: 0.8rem; cursor: pointer; color: var(--color-text);
}
.cal-view-btn:first-child { border-left: none; }
.cal-view-btn:hover { background: var(--color-bg-2); }
.cal-view-btn.active { background: var(--color-primary); color: #fff; font-weight: 600; }

/* Vista mes */
.cal-month-weekdays {
  display: grid; grid-template-columns: repeat(7, 1fr);
  font-size: 0.7rem; font-weight: 600; color: var(--color-text-muted);
  text-transform: uppercase; letter-spacing: 0.04em; text-align: center; padding: 0.3rem 0;
}
.cal-month {
  display: grid; grid-template-columns: repeat(7, 1fr); grid-auto-rows: 100px;
  border: 1px solid var(--color-border); border-radius: 8px; overflow: hidden;
}
.cal-month-cell {
  border-right: 1px solid var(--color-border); border-bottom: 1px solid var(--color-border);
  padding: 0.3rem; overflow: hidden; background: var(--color-surface);
}
.cal-month-cell:nth-child(7n) { border-right: none; }
.cal-month-cell.out-of-month { background: var(--color-bg-2); color: var(--color-text-muted); }
.cal-month-daynum { font-size: 0.75rem; font-weight: 600; margin-bottom: 0.2rem; }
.cal-month-cell.today .cal-month-daynum {
  background: var(--color-primary); color: #fff; border-radius: 50%;
  width: 20px; height: 20px; display: inline-flex; align-items: center; justify-content: center;
}
.cal-month-event {
  font-size: 0.65rem; background: var(--color-primary); color: #fff; border-radius: 4px;
  padding: 1px 4px; margin-bottom: 2px; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; cursor: pointer;
  /* Evita que un toque sobre un evento dentro de la cuadrícula del mes se interprete como el
     inicio de un scroll (mismo bug de iPad ya visto y corregido en Contract Management). */
  touch-action: manipulation;
}
.cal-month-more { font-size: 0.65rem; color: var(--color-text-muted); cursor: pointer; }
.cal-month-event.registrado { background: #166534; }

/* Vista día / semana (rejilla horaria) */
.cal-time-grid { border: 1px solid var(--color-border); border-radius: 8px; overflow: hidden; background: var(--color-surface); }
.cal-time-header { display: flex; border-bottom: 1px solid var(--color-border); }
.cal-time-header-spacer { width: 52px; flex: none; border-right: 1px solid var(--color-border); }
.cal-time-header-day {
  flex: 1; text-align: center; padding: 0.5rem 0.2rem; font-size: 0.75rem;
  border-right: 1px solid var(--color-border); text-transform: capitalize;
}
.cal-time-header-day:last-child { border-right: none; }
.cal-time-header-day.today { color: var(--color-primary); font-weight: 700; }
.cal-allday-row { display: flex; border-bottom: 1px solid var(--color-border); background: var(--color-bg-2); }
.cal-allday-col { flex: 1; border-right: 1px solid var(--color-border); padding: 0.2rem; }
.cal-time-body { display: flex; max-height: 560px; overflow-y: auto; -webkit-overflow-scrolling: touch; position: relative; }
.cal-time-labels { width: 52px; flex: none; position: relative; border-right: 1px solid var(--color-border); }
.cal-time-label { position: absolute; right: 6px; transform: translateY(-50%); font-size: 0.65rem; color: var(--color-text-muted); }
.cal-time-day-col { flex: 1; position: relative; border-right: 1px solid var(--color-border); }
.cal-time-day-col:last-child { border-right: none; }
.cal-hour-line { position: absolute; left: 0; right: 0; height: 1px; background: var(--color-border); }
.cal-event-block {
  position: absolute; left: 2px; right: 2px; border-radius: 4px;
  background: var(--color-primary); color: #fff; font-size: 0.68rem; padding: 2px 4px;
  overflow: hidden; cursor: pointer; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  touch-action: manipulation;
}
.cal-event-time { font-weight: 600; font-size: 0.6rem; opacity: 0.85; }
.cal-event-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-event-block.registrado { background: #166534; }

/* Puntos de categoría de Outlook (uno por categoría asignada a la reunión): un anillo blanco los
   separa del fondo de color del propio evento, para que no se confundan con él. */
.cal-category-dots { display: inline-flex; gap: 2px; margin-right: 3px; vertical-align: middle; }
.cal-category-dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.85);
}

.cal-registrado-box {
  background: #ecfdf5; border: 1px solid #a7f3d0; color: #166534;
  border-radius: 6px; padding: 0.5rem 0.7rem; font-size: 0.82rem; margin: 0.4rem 0;
}
