:root {
  --brand: #0b4f7a;
  --brand-dark: #083a5a;
  --brand-soft: #e3eef6;
  --accent: #0e8a6a;
  --accent-soft: #e0f3ec;
  --warn: #b45309;
  --warn-soft: #fdf1e0;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --ink: #16283a;
  --ink-2: #4a5d70;
  --ink-3: #7a8a9a;
  --line: #d6dee6;
  --bg: #f4f7fa;
  --card: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 40, 60, .06), 0 2px 8px rgba(16, 40, 60, .06);
  --tabbar-h: 64px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 var(--font);
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
}
img { max-width: 100%; }
a { color: var(--brand); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75em; }
.muted { color: var(--ink-2); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
[hidden] { display: none !important; }

/* ---- Estructura ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--brand); color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; min-width: 0; }
.brand img { border-radius: 7px; background: #fff; flex: none; }
.brand span { display: flex; flex-direction: column; line-height: 1.1; }
.brand strong { font-size: 1rem; }
.brand small { font-size: .7rem; opacity: .8; letter-spacing: .06em; }
.sync-chip {
  border: 0; border-radius: 999px; padding: 6px 12px; font: 600 .8rem var(--font);
  background: rgba(255,255,255,.16); color: #fff; cursor: pointer;
}
.sync-chip.pending { background: #f59e0b; color: #1f1300; }
.sync-chip.error { background: #fecaca; color: #7f1d1d; }
.offline-banner { background: var(--warn-soft); color: var(--warn); font-size: .85rem; padding: 8px 16px; text-align: center; }

main {
  max-width: 980px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom));
  outline: none;
}
body.no-tabs main { padding-bottom: 32px; }
.loading { padding: 48px 0; text-align: center; color: var(--ink-3); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: center;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; max-width: 160px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--ink-3); text-decoration: none; font-size: .75rem; font-weight: 600;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--brand); }

/* ---- Componentes ---- */
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 14px;
}
.card > :last-child { margin-bottom: 0; }
.card-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.card-title h2 { margin: 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row > * { min-width: 0; }
.grow { flex: 1 1 auto; }
.stack > * + * { margin-top: 10px; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 640px) { .grid-2 { grid-template-columns: 1fr 1fr; } }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head h1 { margin: 0; }

label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-size: .85rem; font-weight: 600; color: var(--ink-2); margin-bottom: 5px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--ink);
  padding: 11px 12px; min-height: 46px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
input[type=checkbox] { width: 20px; height: 20px; min-height: 0; vertical-align: middle; accent-color: var(--brand); }
.check { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.input-suffix { position: relative; }
.input-suffix input { padding-right: 64px; }
.input-suffix em { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--ink-3); font-weight: 600; }
.hint { font-size: .8rem; color: var(--ink-3); margin-top: 4px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 10px 18px;
  font: 600 .95rem var(--font); text-decoration: none; cursor: pointer;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: #f1b8b3; }
.btn.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--brand); }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .85rem; }
.btn svg { width: 20px; height: 20px; flex: none; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.actions.end { justify-content: flex-end; }
.fab-bar { position: sticky; bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom)); z-index: 5; display: grid; gap: 10px; margin-top: 16px; }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 9px; border-radius: 999px; font-size: .75rem; font-weight: 700; letter-spacing: .02em;
  background: var(--brand-soft); color: var(--brand); white-space: nowrap;
}
.badge.borrador { background: #eef1f4; color: var(--ink-2); }
.badge.enviado { background: var(--brand-soft); color: var(--brand); }
.badge.aprobado { background: var(--accent-soft); color: var(--accent); }
.badge.rechazado { background: var(--danger-soft); color: var(--danger); }
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.error { background: var(--danger-soft); color: var(--danger); }
.badge.ok { background: var(--accent-soft); color: var(--accent); }

.notice { padding: 12px 14px; border-radius: 10px; background: var(--brand-soft); color: var(--brand-dark); margin-bottom: 14px; font-size: .92rem; }
.notice.warn { background: var(--warn-soft); color: #7c3b06; }
.notice.danger { background: var(--danger-soft); color: #7a1a12; }
.notice.ok { background: var(--accent-soft); color: #0b5a45; }

.list { list-style: none; margin: 0; padding: 0; }
.list-item {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 0; border-top: 1px solid var(--line);
  color: inherit; text-decoration: none;
}
.list > li:first-child > .list-item { border-top: 0; padding-top: 0; }
a.list-item:hover .li-title { color: var(--brand); }
.li-body { flex: 1; min-width: 0; }
.li-title { font-weight: 600; }
.li-meta { font-size: .82rem; color: var(--ink-2); margin-top: 2px; }
.li-thumb { width: 64px; height: 64px; flex: none; border-radius: 8px; object-fit: cover; background: #e8edf2; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.empty { text-align: center; color: var(--ink-3); padding: 28px 12px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; }
.stat b { display: block; font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: .8rem; color: var(--ink-2); }

.bar { height: 8px; background: #e6ecf1; border-radius: 99px; overflow: hidden; min-width: 60px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.bar.over > i { background: var(--warn); }

/* Fotos */
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.photo-tile { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: #e8edf2; }
.photo-tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-tile .x {
  position: absolute; top: 4px; right: 4px; width: 30px; height: 30px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 18px; line-height: 30px; cursor: pointer;
}
.photo-tile .tag { position: absolute; left: 4px; bottom: 4px; font-size: .68rem; padding: 1px 6px; border-radius: 6px; background: rgba(0,0,0,.6); color: #fff; }
.photo-add { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }

.gps { display: inline-flex; align-items: center; gap: 6px; font-size: .82rem; font-weight: 600; padding: 4px 10px; border-radius: 99px; background: #eef1f4; color: var(--ink-2); }
.gps::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.gps.good { background: var(--accent-soft); color: var(--accent); }
.gps.fair { background: var(--warn-soft); color: var(--warn); }
.gps.bad { background: var(--danger-soft); color: var(--danger); }

.act-info { background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: .88rem; margin: -4px 0 14px; }
.act-info .bar { margin-top: 6px; }

/* Tablas */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -16px; padding: 0 16px; }
table { border-collapse: collapse; width: 100%; font-size: .88rem; }
th, td { padding: 8px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .75rem; text-transform: uppercase; letter-spacing: .03em; color: var(--ink-2); background: #f7f9fb; }
tfoot td { font-weight: 700; background: #f7f9fb; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #f3f7fb; }

.tabs { display: flex; gap: 6px; margin-bottom: 14px; overflow-x: auto; }
.tabs a { padding: 8px 14px; border-radius: 99px; text-decoration: none; font-weight: 600; font-size: .9rem; color: var(--ink-2); background: #e9eef3; white-space: nowrap; }
.tabs a.active { background: var(--brand); color: #fff; }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.filters label.field { margin-bottom: 0; }

/* Diálogo */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 40px); box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(10, 25, 40, .45); }
dialog .dlg-body { padding: 18px; overflow: auto; max-height: calc(100vh - 140px); }
dialog .dlg-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
dialog.lightbox { background: #000; width: 100vw; max-width: none; height: 100vh; height: 100dvh; max-height: none; margin: 0; inset: 0; border-radius: 0; }
dialog.lightbox[open] { display: flex; flex-direction: column; }
dialog.lightbox .dlg-body { flex: 1; min-height: 0; max-height: none; padding: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; }
dialog.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
dialog.lightbox .lb-bar { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); color: #fff; font-size: .85rem; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 20px + env(safe-area-inset-bottom)); z-index: 50;
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: #16283a; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: .9rem;
  max-width: calc(100vw - 32px); transition: all .2s; box-shadow: 0 6px 24px rgba(0,0,0,.2);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); }

/* Login */
.login { max-width: 400px; margin: 6vh auto 0; }
.login .logo { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 20px; text-align: center; }
.login .logo img { width: 72px; height: 72px; }

/* ---- Informe imprimible ---- */
.report-doc .doc-head { display: flex; justify-content: space-between; gap: 16px; border-bottom: 3px solid var(--brand); padding-bottom: 10px; margin-bottom: 14px; }
.report-doc .doc-head h1 { margin: 0 0 4px; font-size: 1.25rem; }
.report-doc .doc-company { font-weight: 800; color: var(--brand); letter-spacing: .04em; font-size: .85rem; }
.meta-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 6px 16px; font-size: .9rem; margin-bottom: 14px; }
.meta-grid div span { display: block; font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-3); }
.entry-block { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 12px; break-inside: avoid; }
.entry-block h3 { display: flex; gap: 8px; align-items: baseline; }
.entry-block h3 .n { color: var(--brand); }
.doc-photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin-top: 10px; }
.doc-photo { margin: 0; break-inside: avoid; }
.doc-photo img { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 8px; background: #e8edf2; cursor: zoom-in; display: block; }
.doc-photo figcaption { font-size: .75rem; color: var(--ink-2); margin-top: 4px; }
.map { height: 320px; border-radius: 10px; margin: 10px 0; background: #e8edf2; z-index: 0; }
.signatures { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; margin-top: 50px; }
.signatures div { border-top: 1px solid var(--ink); padding-top: 6px; font-size: .85rem; }
.print-only { display: none; }

@media print {
  @page { size: letter; margin: 14mm 12mm; }
  body { background: #fff; font-size: 11pt; }
  .no-print, .topbar, .tabbar, .offline-banner, .toast { display: none !important; }
  .print-only { display: block; }
  main { padding: 0; max-width: none; }
  .card { box-shadow: none; padding: 0; margin-bottom: 10px; border-radius: 0; }
  .map { height: 260px; }
  .doc-photos { grid-template-columns: repeat(3, 1fr); }
  .table-wrap { overflow: visible; margin: 0; padding: 0; }
  table { font-size: 9pt; }
  th { background: #eef2f6 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .bar > i, .badge { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  a { color: inherit; text-decoration: none; }
}
