:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --text: #1d2a22;
  --muted: #66736b;
  --primary: #2f6f4e;
  --primary-dark: #24573d;
  --accent: #d9772f;
  --border: #dce4de;
  --danger: #b84040;
  --shadow: 0 8px 26px rgba(25, 55, 39, .08);
}
* { box-sizing: border-box; }
body { margin: 0; font-family: Inter, system-ui, -apple-system, Segoe UI, sans-serif; background: var(--bg); color: var(--text); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 20px clamp(16px, 4vw, 44px); color: white; background: linear-gradient(120deg, var(--primary-dark), var(--primary)); }
.topbar h1 { margin: 0; font-size: clamp(1.45rem, 3vw, 2rem); }
.topbar p { margin: 4px 0 0; opacity: .8; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; padding: 10px clamp(12px, 4vw, 44px); background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.tab { border: 0; background: transparent; padding: 10px 14px; border-radius: 10px; color: var(--muted); white-space: nowrap; }
.tab.active { background: #e7f0ea; color: var(--primary-dark); font-weight: 700; }
.container { max-width: 1280px; margin: 0 auto; padding: 24px clamp(14px, 3vw, 30px) 60px; }
.panel { display: none; }
.panel.active { display: block; }
.cards { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.card, .box { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); }
.card { padding: 20px; }
.card span { color: var(--muted); font-size: .92rem; }
.card strong { display: block; margin-top: 7px; font-size: clamp(1.35rem, 2.5vw, 2rem); overflow-wrap: break-word; }
.box { padding: 20px; }
.grid { display: grid; gap: 16px; margin-top: 16px; }
.grid.two { grid-template-columns: 1.1fr .9fr; }
h2, h3 { margin-top: 0; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 14px; }
.section-head h2 { margin-bottom: 4px; }
.section-head p { margin: 0; color: var(--muted); }
.btn { border: 1px solid transparent; border-radius: 10px; padding: 10px 14px; font-weight: 700; }
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.secondary { background: white; color: var(--primary-dark); border-color: var(--border); }
.btn.small { padding: 7px 10px; font-size: .9rem; }
.btn.danger { background: #fff0f0; color: var(--danger); border-color: #f3caca; }
.file-label { display: inline-flex; align-items: center; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; }
label { display: grid; gap: 6px; font-weight: 700; font-size: .92rem; }
input, select, textarea { width: 100%; border: 1px solid var(--border); border-radius: 10px; padding: 10px 11px; background: white; color: var(--text); }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(47,111,78,.18); border-color: var(--primary); }
.span-2 { grid-column: span 2; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.hidden { display: none !important; }
.items-head { display: flex; align-items: center; justify-content: space-between; margin-top: 18px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 11px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { color: var(--muted); font-size: .86rem; text-transform: uppercase; letter-spacing: .02em; }
.right { text-align: right; }
.item-total { white-space: nowrap; font-weight: 700; }
.icon-btn { border: 0; background: transparent; color: var(--danger); font-size: 1.2rem; padding: 4px 8px; }
.filters { display: flex; gap: 10px; margin-bottom: 12px; }
.invoice-card { border: 1px solid var(--border); border-radius: 12px; padding: 14px; margin-bottom: 10px; }
.invoice-card:last-child { margin-bottom: 0; }
.invoice-top { display: flex; justify-content: space-between; gap: 14px; }
.invoice-meta { color: var(--muted); margin-top: 6px; font-size: .92rem; }
.invoice-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 12px; }
.badge { display: inline-flex; align-items: center; border-radius: 999px; padding: 4px 9px; background: #eef3ef; color: var(--primary-dark); font-size: .82rem; font-weight: 700; }
.badge.pending { background: #fff4df; color: #8a5c12; }
.empty { color: var(--muted); padding: 16px 0; }
.summary-row { margin-bottom: 14px; }
.summary-row .line { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 5px; }
.bar { height: 10px; border-radius: 999px; overflow: hidden; background: #e7ece8; }
.bar > span { display: block; height: 100%; background: var(--primary); }
.inline-form { display: grid; grid-template-columns: 2fr 1fr auto auto; align-items: end; gap: 10px; }
.progress { min-width: 150px; }
.progress small { color: var(--muted); }
.report-cards { grid-template-columns: repeat(2, minmax(0,1fr)); }
.report-card p { color: var(--muted); min-height: 40px; }
.toast { position: fixed; right: 18px; bottom: 18px; z-index: 50; background: #1f2a23; color: white; padding: 12px 16px; border-radius: 10px; opacity: 0; transform: translateY(12px); pointer-events: none; transition: .2s; box-shadow: var(--shadow); }
.toast.show { opacity: 1; transform: translateY(0); }
dialog { width: min(900px, 92vw); max-height: 88vh; border: 0; border-radius: 16px; padding: 0; box-shadow: 0 24px 80px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
#attachmentContent { padding: 16px; }
#attachmentContent img, #attachmentContent iframe { max-width: 100%; width: 100%; min-height: 65vh; border: 0; object-fit: contain; }
@media (max-width: 900px) {
  .cards { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .grid.two { grid-template-columns: 1fr; }
  .inline-form { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 620px) {
  .topbar, .section-head { align-items: stretch; flex-direction: column; }
  .cards, .report-cards { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .filters { flex-direction: column; }
  .inline-form { grid-template-columns: 1fr; }
  .invoice-top { flex-direction: column; }
}


/* Cámara, OCR y experiencia móvil */
.install { background: #f4a261; color: #34210e; border-color: transparent; }
.btn.accent { background: var(--accent); color: white; }
.btn.accent:disabled, .btn:disabled { opacity: .52; cursor: not-allowed; }
.field-label { display: block; font-weight: 700; font-size: .92rem; margin-bottom: 7px; }
.attachment-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.camera-button, .attachment-actions .file-label { justify-content: center; min-height: 46px; }
.help-text { display: block; color: var(--muted); margin-top: 8px; line-height: 1.4; }
.attachment-preview { margin-top: 12px; border: 1px dashed var(--border); border-radius: 12px; padding: 10px; background: #f8faf8; }
.attachment-preview img { display: block; width: min(100%, 420px); max-height: 330px; object-fit: contain; margin: 0 auto; border-radius: 8px; }
.attachment-preview .file-preview { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.attachment-preview .preview-meta { display: flex; justify-content: space-between; gap: 10px; margin-top: 8px; color: var(--muted); font-size: .86rem; }
.ocr-panel { margin-top: 14px; padding: 14px; border: 1px solid #e6d4c5; border-radius: 14px; background: #fffaf6; }
.ocr-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.ocr-head small { display: block; color: var(--muted); margin-top: 4px; font-weight: 400; }
.ocr-progress { margin-top: 14px; }
.progress-line { display: flex; justify-content: space-between; gap: 10px; color: var(--muted); font-size: .9rem; }
.ocr-progress progress { width: 100%; height: 13px; margin-top: 7px; accent-color: var(--accent); }
.ocr-result { margin-top: 14px; border-top: 1px solid #ead8ca; padding-top: 14px; }
.ocr-summary { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.ocr-chip { display: inline-flex; gap: 5px; align-items: center; padding: 7px 9px; border-radius: 999px; background: white; border: 1px solid #ead8ca; font-size: .86rem; }
.ocr-result details { margin-top: 12px; }
.ocr-result summary { cursor: pointer; color: var(--primary-dark); font-weight: 700; }
.ocr-result textarea { margin-top: 8px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .82rem; }
.ocr-warning { color: #8a5c12; }

@media (max-width: 620px) {
  body { padding-bottom: env(safe-area-inset-bottom); }
  .topbar { padding-top: calc(16px + env(safe-area-inset-top)); }
  .top-actions .btn, .top-actions .file-label { flex: 1; justify-content: center; }
  .section-head .btn { width: 100%; }
  .attachment-actions { display: grid; grid-template-columns: 1fr; }
  .ocr-head { align-items: stretch; flex-direction: column; }
  .ocr-head .btn { width: 100%; min-height: 48px; }
  .box { padding: 15px; border-radius: 14px; }
  th, td { padding: 10px 8px; }
  #invoiceItems input { min-width: 105px; }
  #invoiceItems .item-name { min-width: 190px; }
  .form-actions { position: sticky; bottom: 0; z-index: 4; margin: 18px -15px -15px; padding: 12px 15px calc(12px + env(safe-area-inset-bottom)); background: rgba(255,255,255,.96); border-top: 1px solid var(--border); backdrop-filter: blur(8px); }
  .form-actions .btn { flex: 1; min-height: 46px; }
  .toast { left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); text-align: center; }
}

.version-badge { display: inline-block; margin-left: 6px; padding: 2px 7px; border-radius: 999px; background: rgba(255,255,255,.18); font-size: .72rem; font-weight: 800; letter-spacing: .02em; }
.ocr-result-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.ocr-items-preview { display: grid; gap: 6px; width: 100%; margin-top: 12px; padding: 10px; background: white; border: 1px solid #ead8ca; border-radius: 12px; }
.ocr-items-preview > div { display: flex; justify-content: space-between; gap: 12px; padding-bottom: 6px; border-bottom: 1px dashed #ead8ca; font-size: .86rem; }
.ocr-items-preview > div:last-of-type { border-bottom: 0; padding-bottom: 0; }
.ocr-items-preview span { min-width: 0; overflow-wrap: anywhere; }
.ocr-items-preview strong { white-space: nowrap; }
@media (max-width: 640px) { .ocr-result-actions .btn { flex: 1 1 100%; } }

.ocr-warning-list {
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #fff7e6;
  border: 1px solid #efd39a;
  font-size: .92rem;
}
.ocr-warning-list ul {
  margin: 6px 0 0 18px;
  padding: 0;
}

/* Versión 5: compras manuales y presupuesto total */
.dashboard-cards { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.dashboard-cards .card strong { font-size: clamp(1.1rem, 1.6vw, 1.5rem); }
.project-budget-strip { margin-top: 16px; }
.budget-strip-line { display: flex; justify-content: space-between; gap: 14px; align-items: center; margin-bottom: 8px; }
.bar.large { height: 14px; }
.bar > span.over-budget { background: var(--danger); }
.negative-amount { color: var(--danger) !important; }
.section-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.purchase-mode-notice { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; margin-bottom: 16px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); }
.purchase-mode-notice span { color: var(--muted); font-size: .9rem; }
.purchase-mode-notice.invoice { background: #f4faf6; border-color: #cfe1d5; }
.purchase-mode-notice.manual { background: #fff8e9; border-color: #ecd59d; }
.badge.manual { background: #fff2cf; color: #7b5513; }
.total-budget-box { margin-bottom: 22px; }
.project-budget-form { display: grid; grid-template-columns: minmax(240px, 1fr) auto; align-items: end; gap: 12px; }
.budget-overview { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-top: 18px; }
.budget-overview > div { padding: 14px; border-radius: 12px; background: #f7faf8; border: 1px solid var(--border); }
.budget-overview span { display: block; color: var(--muted); font-size: .86rem; }
.budget-overview strong { display: block; margin-top: 5px; font-size: 1.25rem; }
.budget-progress-wrap { margin-top: 16px; }
.sub-section-head { margin-top: 24px; }

@media (max-width: 1050px) {
  .dashboard-cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 700px) {
  .dashboard-cards, .budget-overview { grid-template-columns: 1fr; }
  .project-budget-form { grid-template-columns: 1fr; }
  .section-actions { display: grid; grid-template-columns: 1fr; width: 100%; }
  .section-actions .btn { width: 100%; }
  .budget-strip-line { align-items: flex-start; flex-direction: column; gap: 4px; }
}


/* v9: selector de categorías dinámicas */
.category-picker { display: flex; gap: .55rem; align-items: center; }
.category-picker select { flex: 1; min-width: 0; }
.category-picker .btn { white-space: nowrap; }
.field-help { display: block; margin-top: .35rem; color: var(--muted, #66736b); font-weight: 400; line-height: 1.3; }
@media (max-width: 520px) {
  .category-picker { align-items: stretch; }
  .category-picker .btn { padding-inline: .65rem; }
}

/* v10: inicio de sesión y sincronización Supabase */
.auth-pending .topbar,
.auth-pending .tabs,
.auth-pending main { visibility: hidden; }
.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: linear-gradient(145deg, #e8f1eb, #f8f4eb);
}
.auth-overlay.hidden { display: none; }
.auth-card {
  width: min(100%, 430px);
  padding: 28px;
  border-radius: 22px;
  background: white;
  box-shadow: 0 22px 70px rgba(31, 73, 51, .18);
  border: 1px solid var(--border);
}
.auth-brand { display: flex; gap: 14px; align-items: center; margin-bottom: 22px; }
.auth-brand h1 { margin: 0; font-size: 1.45rem; }
.auth-brand p { margin: 4px 0 0; color: var(--muted); }
.auth-icon { display: grid; place-items: center; width: 52px; height: 52px; border-radius: 15px; background: #e8f2ec; font-size: 1.7rem; }
.auth-form { display: grid; gap: 14px; }
.auth-form .btn { width: 100%; min-height: 46px; }
.auth-message { margin: 16px 0 0; color: var(--muted); font-size: .9rem; line-height: 1.45; }
.auth-message.error { color: var(--danger); }
.cloud-user { display: grid; gap: 2px; min-width: 150px; }
.cloud-user small { color: rgba(255,255,255,.78); max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sync-status { display: inline-flex; align-items: center; gap: 6px; font-weight: 800; font-size: .82rem; }
.sync-status::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: #f6c453; box-shadow: 0 0 0 3px rgba(246,196,83,.18); }
.sync-status.online::before { background: #76d89b; box-shadow: 0 0 0 3px rgba(118,216,155,.18); }
.sync-status.offline::before { background: #ff8f8f; box-shadow: 0 0 0 3px rgba(255,143,143,.18); }
.sync-box { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 16px; border-color: #bcd8c7; background: #f3faf5; }
.sync-box h2 { margin-bottom: 4px; }
.sync-box p { margin: 0; color: var(--muted); }
@media (max-width: 720px) {
  .sync-box { align-items: stretch; flex-direction: column; }
  .sync-box .btn { width: 100%; }
  .cloud-user { width: 100%; order: -1; }
  .cloud-user small { max-width: 100%; }
}

/* v11: cuentas por pagar y comprobantes */
.compact-cards { margin-bottom: 1rem; }
.allocation-list { display: grid; gap: .65rem; margin: .8rem 0; }
.allocation-row { display: grid; grid-template-columns: minmax(180px, 1fr) 120px 130px; gap: .75rem; align-items: center; padding: .75rem; border: 1px solid var(--border); border-radius: 12px; background: #fff; }
.allocation-row.disabled { opacity: .58; }
.allocation-row .allocation-invoice { display: flex; align-items: flex-start; gap: .55rem; }
.allocation-row .allocation-invoice input[type="checkbox"] { width: 20px; height: 20px; margin-top: 2px; }
.allocation-row small { display: block; color: var(--muted); margin-top: .2rem; }
.allocation-row input[type="number"] { min-width: 0; }
.allocation-summary { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 1.25rem; margin: .5rem 0 1rem; padding: .7rem .9rem; border-radius: 10px; background: #f4f7f4; }
.status-overdue { color: #a32d2d; font-weight: 700; }
.badge.credit { background: #fff2d8; color: #7a4c00; }
.badge.paid { background: #dff4e7; color: #175c35; }
.badge.partial { background: #e8efff; color: #234a8f; }
.badge.overdue { background: #ffe3e3; color: #8c2424; }
.payment-ai-summary { display: grid; gap: .35rem; padding: .8rem; background: #f7faf7; border-radius: 10px; }
.payment-ai-summary ul { margin: .25rem 0 0 1.2rem; }
.labor-report-box .filters { align-items: end; }
.labor-report-box label { min-width: 155px; }
.attachment-inline { display: inline-flex; align-items: center; gap: .35rem; }

@media (max-width: 760px) {
  .allocation-row { grid-template-columns: 1fr; }
  .allocation-summary { justify-content: flex-start; }
  .labor-report-box .filters > * { width: 100%; }
}


/* v12: mano de obra vinculada al presupuesto por categorías */
#budgetTable td:nth-child(3),
#budgetTable td:nth-child(4),
#budgetTable td:nth-child(5),
#budgetTable td:nth-child(6) { white-space: nowrap; }
#budgetTable .progress { min-width: 135px; }
#laborCategoryFilter, #laborReportCategory { min-width: 190px; }
@media (max-width: 700px) {
  #laborCategoryFilter, #laborReportCategory { width: 100%; min-width: 0; }
}


/* v14: facturas en dólares y tipo de cambio BCCR */
.exchange-rate-box { border: 1px solid var(--border, #d6ddd8); border-radius: 14px; padding: 14px; background: var(--surface-soft, #f7faf8); }
.exchange-rate-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; margin-bottom: 10px; }
.exchange-rate-head div { display: grid; gap: 3px; }
.exchange-rate-head small { color: var(--muted, #66736b); font-weight: 400; }
.exchange-rate-grid { margin-top: 8px; }
.item-converted { display: block; margin-top: 3px; color: var(--muted, #66736b); font-size: .82rem; white-space: nowrap; }
.invoice-currency-note { margin-top: 5px; color: var(--muted, #66736b); font-size: .85rem; }
@media (max-width: 680px) { .exchange-rate-head { flex-direction: column; } .exchange-rate-head .btn { width: 100%; } }

/* v15: desembolsos bancarios */
.disbursement-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 14px;
  margin: 16px 0 24px;
}
.disbursement-card { margin: 0; }
.disbursement-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 14px;
}
.disbursement-card-head h3 { margin: 0 0 4px; }
.disbursement-card-head p { margin: 0; color: var(--muted, #68756d); font-size: .9rem; }
.disbursement-metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 12px;
}
.disbursement-metrics > div {
  border: 1px solid var(--border, #d8e0da);
  border-radius: 10px;
  padding: 10px;
  background: rgba(255,255,255,.5);
}
.disbursement-metrics span { display: block; color: var(--muted, #68756d); font-size: .78rem; margin-bottom: 3px; }
.disbursement-metrics strong { font-size: .98rem; }
@media (max-width: 560px) {
  .disbursement-list { grid-template-columns: 1fr; }
  .disbursement-metrics { grid-template-columns: 1fr; }
}


/* v16: resumen fiscal de facturas */
.invoice-tax-editor{margin-top:16px;padding:16px;border:1px solid var(--border);border-radius:14px;background:var(--surface-soft,#f7faf7)}
.invoice-tax-editor-head{display:flex;justify-content:space-between;gap:12px;margin-bottom:12px}
.invoice-tax-editor-head small{display:block;margin-top:4px;color:var(--muted)}
.invoice-tax-editor .tax-grid{grid-template-columns:repeat(3,minmax(0,1fr))}
#invoiceDifference.warning{color:#9a5a00;font-weight:600}
@media (max-width:720px){.invoice-tax-editor .tax-grid{grid-template-columns:1fr}}
