:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #0f766e;
  --primary-600: #0d6b64;
  --primary-50: #e6f4f2;
  --danger: #dc2626;
  --danger-50: #fef2f2;
  --warning: #b45309;
  --warning-50: #fffbeb;
  --info: #1d4ed8;
  --info-50: #eff6ff;
  --success: #15803d;
  --success-50: #f0fdf4;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(15 23 42 / .06), 0 4px 16px rgb(15 23 42 / .06);
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111a2b;
    --surface-2: #0f1726;
    --border: #1f2a3d;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --primary: #2dd4bf;
    --primary-600: #14b8a6;
    --primary-50: #0f2e2c;
    --danger: #f87171;
    --danger-50: #2a1414;
    --warning: #fbbf24;
    --warning-50: #2a2210;
    --info: #60a5fa;
    --info-50: #0f1d33;
    --success: #4ade80;
    --success-50: #0f2a18;
    --shadow: 0 1px 2px rgb(0 0 0 / .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh; background: var(--bg); color: var(--text);
  font: 15px/1.5 var(--font); -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1rem; }
a { color: var(--primary); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.stack > * + * { margin-top: 1rem; }
.row { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1; }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  min-height: 42px; padding: .55rem 1rem; border-radius: 10px; border: 1px solid transparent;
  font: 600 .95rem var(--font); cursor: pointer; text-decoration: none;
  background: var(--primary); color: #fff; transition: background .15s, transform .05s;
}
@media (prefers-color-scheme: dark) { .btn { color: #042f2e; } }
.btn:hover { background: var(--primary-600); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: var(--surface-2); }
.btn.danger { background: var(--danger-50); color: var(--danger); border-color: transparent; }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.sm { min-height: 34px; padding: .3rem .7rem; font-size: .85rem; }
.btn.block { width: 100%; }
.icon-btn { background: none; border: 0; padding: .4rem; border-radius: 8px; cursor: pointer; color: inherit; font-size: 1.2rem; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); }

/* ---------- Formularios ---------- */
.field { display: flex; flex-direction: column; gap: .3rem; }
.field > span { font-weight: 600; font-size: .88rem; }
.field small { color: var(--muted); }
input, select, textarea {
  width: 100%; min-height: 44px; padding: .6rem .75rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit;
}
textarea { min-height: 88px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 35%, transparent); outline-offset: 1px; border-color: var(--primary); }
.form-grid { display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 640px) { .form-grid.cols-2 { grid-template-columns: 1fr 1fr; } .span-2 { grid-column: span 2; } }
.check { display: flex; gap: .5rem; align-items: center; font-weight: 600; }
.check input { width: 20px; min-height: 20px; }

/* ---------- Tarjetas ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: .75rem; }
.card-head h2 { margin: 0; }
.stats { display: grid; gap: .75rem; grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 900px) { .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat { padding: 1rem; }
.stat .label { color: var(--muted); font-size: .82rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.stat .value { font-size: 1.7rem; font-weight: 700; margin-top: .15rem; }
.stat .hint { font-size: .8rem; color: var(--muted); }
.stat.accent { background: linear-gradient(135deg, var(--primary), #0e7490); color: #fff; border: 0; }
.stat.accent .label, .stat.accent .hint { color: rgb(255 255 255 / .85); }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; gap: .3rem; padding: .15rem .55rem; border-radius: 999px; font-size: .78rem; font-weight: 700; white-space: nowrap; }
.badge.pending { background: var(--warning-50); color: var(--warning); }
.badge.confirmed { background: var(--info-50); color: var(--info); }
.badge.assigned { background: var(--primary-50); color: var(--primary); }
.badge.in_progress { background: var(--info-50); color: var(--info); }
.badge.completed { background: var(--success-50); color: var(--success); }
.badge.cancelled { background: var(--danger-50); color: var(--danger); }
.badge.off { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge.on { background: var(--success-50); color: var(--success); }

/* ---------- Tablas (se convierten en tarjetas en móvil) ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.data th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding: .6rem .7rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data td { padding: .7rem; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
@media (max-width: 760px) {
  table.data thead { display: none; }
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
  table.data tr { border: 1px solid var(--border); border-radius: 12px; margin-bottom: .75rem; padding: .35rem .2rem; background: var(--surface); }
  table.data td { border: 0; padding: .35rem .8rem; display: flex; justify-content: space-between; gap: 1rem; text-align: right; }
  table.data td::before { content: attr(data-label); font-weight: 600; color: var(--muted); text-align: left; font-size: .82rem; }
  table.data td.actions { justify-content: flex-end; }
  table.data td.actions::before { content: none; }
}

/* ---------- Lista de reservas (cliente) ---------- */
.booking { display: grid; gap: .35rem; padding: 1rem; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.booking + .booking { margin-top: .75rem; }
.route { display: grid; grid-template-columns: 16px 1fr; gap: .1rem .6rem; align-items: center; }
.route .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--primary); justify-self: center; }
.route .dot.end { background: var(--danger); border-radius: 2px; }
.route .line { width: 2px; height: 14px; background: var(--border); justify-self: center; }
.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }
.empty .big { font-size: 2.2rem; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: .75rem;
  padding: .7rem 1rem; padding-top: max(.7rem, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 800; font-size: 1.05rem; color: var(--text); text-decoration: none; }
.brand img { width: 32px; height: 32px; border-radius: 8px; }
.container { width: 100%; max-width: 1100px; margin: 0 auto; padding: 1rem; padding-bottom: calc(5rem + env(safe-area-inset-bottom)); }
.grid-2 { display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 960px) { .grid-2 { grid-template-columns: 1.1fr .9fr; align-items: start; } }

/* Admin: barra lateral en escritorio, barra inferior en móvil */
.shell { display: grid; grid-template-columns: 1fr; min-height: 100dvh; }
.sidenav { display: none; }
.bottomnav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--surface); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
}
.navlink { display: flex; flex-direction: column; align-items: center; gap: .1rem; padding: .5rem .2rem; color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600; border: 0; background: none; cursor: pointer; }
.navlink .ico { font-size: 1.25rem; line-height: 1; }
.navlink.active { color: var(--primary); }
@media (min-width: 900px) {
  .shell { grid-template-columns: 240px 1fr; }
  .sidenav { display: flex; flex-direction: column; gap: .25rem; position: sticky; top: 0; height: 100dvh; padding: 1rem .75rem; background: var(--surface); border-right: 1px solid var(--border); }
  .sidenav .brand { padding: .25rem .5rem 1rem; }
  .sidenav .navlink { flex-direction: row; gap: .7rem; font-size: .95rem; padding: .65rem .8rem; border-radius: 10px; justify-content: flex-start; }
  .sidenav .navlink.active { background: var(--primary-50); }
  .sidenav .spacer { flex: 1; }
  .bottomnav { display: none; }
  .shell .container { padding-bottom: 2rem; }
  .shell .topbar .brand { display: none; }
}

/* ---------- Login ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 1.25rem; background: radial-gradient(circle at 20% 0%, var(--primary-50), transparent 60%), var(--bg); }
.auth .card { width: 100%; max-width: 400px; padding: 1.75rem; }
.auth .logo { width: 64px; height: 64px; border-radius: 16px; display: block; margin: 0 auto 1rem; }
.auth h1 { text-align: center; }
.auth p.lead { text-align: center; color: var(--muted); margin-top: 0; }

/* ---------- Diálogos ---------- */
dialog { border: 0; padding: 0; border-radius: 16px; width: min(560px, calc(100vw - 1.5rem)); max-height: calc(100dvh - 2rem); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / .3); }
dialog::backdrop { background: rgb(15 23 42 / .5); backdrop-filter: blur(2px); }
dialog form { display: flex; flex-direction: column; max-height: calc(100dvh - 2rem); }
dialog header { padding: 1rem 1.2rem; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
dialog header h2 { margin: 0; }
dialog .body { padding: 1.2rem; overflow-y: auto; }
dialog footer { padding: .9rem 1.2rem; border-top: 1px solid var(--border); display: flex; gap: .6rem; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- Toast ---------- */
#toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(5.5rem + env(safe-area-inset-bottom)); z-index: 100; display: grid; gap: .5rem; width: min(420px, calc(100vw - 2rem)); }
@media (min-width: 900px) { #toasts { bottom: 1.5rem; } }
.toast { padding: .8rem 1rem; border-radius: 12px; background: #0f172a; color: #fff; font-weight: 600; box-shadow: 0 10px 30px rgb(0 0 0 / .25); animation: pop .2s ease-out; }
.toast.error { background: #b91c1c; }
.toast.ok { background: #047857; }
@keyframes pop { from { opacity: 0; transform: translateY(8px); } }

.alert { padding: .75rem 1rem; border-radius: 10px; font-size: .9rem; }
.alert.error { background: var(--danger-50); color: var(--danger); }
.alert.info { background: var(--info-50); color: var(--info); }
.alert.warn { background: var(--warning-50); color: var(--warning); }

.chart { display: flex; align-items: flex-end; gap: 4px; height: 140px; padding-top: .5rem; }
.chart .bar { flex: 1; background: var(--primary); border-radius: 4px 4px 0 0; min-height: 2px; position: relative; opacity: .85; }
.chart .bar:hover { opacity: 1; }
.chart-labels { display: flex; gap: 4px; font-size: .68rem; color: var(--muted); margin-top: .3rem; }
.chart-labels span { flex: 1; text-align: center; }

.install-banner { display: flex; gap: .75rem; align-items: center; }
.spinner { width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; margin: 2rem auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.filters { display: grid; gap: .6rem; grid-template-columns: 1fr 1fr; }
@media (min-width: 760px) { .filters { grid-template-columns: 2fr 1fr 1fr 1fr auto; align-items: end; } }
.filters .field > span { font-size: .78rem; }
.tabs { display: flex; gap: .4rem; overflow-x: auto; padding-bottom: .25rem; }
.tab { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: .35rem .85rem; font: 600 .85rem var(--font); cursor: pointer; white-space: nowrap; }
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.origin-box { border: 1px solid var(--border); border-radius: 12px; padding: .75rem .9rem 1rem; margin: 0; display: grid; gap: .75rem; min-width: 0; }
.origin-box legend { font-weight: 700; font-size: .88rem; padding: 0 .35rem; }
.geo-results { display: grid; gap: .4rem; }
.geo-option { text-align: left; padding: .6rem .75rem; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font: inherit; cursor: pointer; }
.geo-option:hover, .geo-option:focus-visible { border-color: var(--primary); }
.geo-option small { display: block; color: var(--muted); }
.geo-ok { color: var(--success); font-weight: 600; }
.geo-warn { color: var(--warning); font-weight: 600; }
.photo-picker input[type=file] { min-height: 0; padding: .35rem; font-size: .8rem; width: 100%; min-width: 0; }

/* ---------- Página pública de reserva (/validarreserva) ---------- */
.v-page { max-width: 760px; margin: 0 auto; padding: 1rem; padding-bottom: 2.5rem; }
.v-status { display: flex; align-items: center; gap: .6rem; padding: .8rem 1rem; border-radius: 12px; background: var(--success-50); color: var(--success); font-weight: 700; }
.v-status .code { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--text); }
.v-driver { display: grid; grid-template-columns: auto 1fr auto; gap: 1rem; align-items: center; }
.v-photo { width: 116px; height: 116px; object-fit: cover; background: var(--surface-2); border: 1px solid var(--border); display: grid; place-items: center; color: var(--muted); font-size: 2.2rem; font-weight: 800; overflow: hidden; }
.v-photo.person { border-radius: 50%; }
.v-photo.car { width: 150px; border-radius: 14px; }
.v-photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
/* El vehículo se muestra completo (sin recortar) dentro de su recuadro */
.v-photo.car img { object-fit: contain; }
.v-info { min-width: 0; text-align: center; }
.v-info .name { font-size: 1.25rem; font-weight: 800; line-height: 1.2; }
.v-info .plate { display: inline-block; margin: .5rem 0 .35rem; padding: .2rem .7rem; border: 2px solid var(--text); border-radius: 6px; font-weight: 800; letter-spacing: .12em; font-size: 1.05rem; }
.v-info .vehicle { color: var(--muted); }
.v-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; }
.v-fields { display: grid; grid-template-columns: minmax(130px, auto) 1fr; gap: 0; margin: 0; }
.v-fields dt, .v-fields dd { padding: .65rem .2rem; border-bottom: 1px solid var(--border); margin: 0; }
.v-fields dt { color: var(--muted); font-weight: 600; font-size: .9rem; }
.v-fields dd { font-weight: 600; }
.v-fields dt:last-of-type, .v-fields dd:last-of-type { border-bottom: 0; }
.v-payment { font-size: .92rem; }
.v-payment p { margin: .25rem 0; }
@media (max-width: 560px) {
  .v-driver { gap: .6rem; }
  .v-photo { width: 76px; height: 76px; font-size: 1.5rem; }
  .v-photo.car { width: 92px; }
  .v-info .name { font-size: 1.02rem; }
  .v-info .plate { font-size: .9rem; }
  .v-fields { grid-template-columns: 1fr; }
  .v-fields dt { border-bottom: 0; padding-bottom: 0; }
}
/* ---------- Contrato (siempre como hoja de papel, también en modo oscuro) ---------- */
dialog.contract-dialog { width: min(860px, calc(100vw - 1.5rem)); }
.contract { background: #fff; color: #111; font: 13.5px/1.55 "Times New Roman", Georgia, serif; padding: 2.2rem 2.4rem; border-radius: 6px; text-align: justify; }
.contract p { margin: 0 0 .75rem; }
.contract .right { text-align: right; }
.contract-head { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.contract-head h1 { flex: 1; text-align: center; font-size: 1.15rem; text-decoration: underline; margin: 0; letter-spacing: .02em; }
.contract-logo { width: 84px; height: auto; }
.contract ul.plain { list-style: none; padding-left: 1.2rem; margin: 0 0 .75rem; }
.contract .blank { display: inline-block; border-bottom: 1px solid #111; height: 1em; vertical-align: -2px; }
.contract-vehicle { display: grid; grid-template-columns: max-content 1fr; gap: .1rem .6rem; margin: 0 0 .9rem 1.2rem; }
.contract-vehicle dt { font-weight: 700; }
.contract-vehicle dd { margin: 0; }
.contract-signs { display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin-top: 2.5rem; text-align: center; font-size: 12.5px; }
.contract-signs > div { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
.contract-signs .line { width: 85%; border-top: 1px solid #111; margin-bottom: .3rem; }
/* Logo SR con la firma del representante legal superpuesta (como un timbre firmado) */
.contract-seal-wrap { position: relative; display: grid; place-items: center; width: 100%; min-height: 90px; margin-bottom: .2rem; }
.contract-seal { width: 90px; opacity: .9; grid-area: 1 / 1; }
/* multiply: el fondo blanco de una firma escaneada (JPG) se vuelve transparente y deja ver el logo */
.contract-rep-signature { grid-area: 1 / 1; position: relative; z-index: 1; max-width: 85%; max-height: 90px; object-fit: contain; mix-blend-mode: multiply; }
.contract-signature { mix-blend-mode: multiply; }
.contract-signature { max-width: 80%; max-height: 90px; margin-bottom: -10px; position: relative; }
.contract .signed-note { font-size: 11px; color: #334155; margin-top: .3rem; }
.contract .signed-note.warn { color: #b45309; font-weight: 700; }

.doc-row { display: flex; align-items: center; gap: .75rem; padding: .6rem 0; border-bottom: 1px solid var(--border); }
.doc-row:last-child { border-bottom: 0; }
@media (max-width: 480px) { .doc-row { flex-wrap: wrap; } }
.v-docs { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: center; margin-top: .45rem; }

.sig-preview { width: 240px; height: 100px; background: #fff; border: 1px solid var(--border); border-radius: 10px; display: grid; place-items: center; padding: .4rem; flex: none; }
.sig-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* Recuadro para firmar (siempre blanco, como papel) */
dialog.sig-dialog { width: min(720px, calc(100vw - 1rem)); }
.sig-pad { position: relative; background: #fff; border: 2px dashed #94a3b8; border-radius: 12px; height: clamp(180px, 45dvh, 300px); }
.sig-pad canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; border-radius: 12px; }
.sig-baseline { position: absolute; left: 8%; right: 8%; bottom: 28%; border-bottom: 1px solid #cbd5e1; pointer-events: none; }
.sig-name { position: absolute; left: 0; right: 0; bottom: calc(28% - 1.6em); text-align: center; color: #94a3b8; font-size: .8rem; pointer-events: none; text-transform: uppercase; }
@media (max-width: 560px) {
  .contract { padding: 1.2rem 1rem; font-size: 12.5px; text-align: left; }
  .contract-logo { width: 56px; }
  .contract-signs { grid-template-columns: 1fr; }
}

@media print {
  .no-print { display: none !important; }
  body { background: #fff; }
  /* Al imprimir un contrato solo sale el documento */
  body.print-contract > *:not(dialog), body.print-contract dialog header, body.print-contract dialog footer { display: none !important; }
  body.print-contract dialog { position: static; box-shadow: none; width: auto; max-height: none; border: 0; }
  body.print-contract dialog .body { overflow: visible; padding: 0; }
  body.print-contract dialog::backdrop { background: none; }
  body.print-contract .contract { padding: 0; border-radius: 0; }
}

.kv { display: grid; grid-template-columns: auto 1fr; gap: .35rem 1rem; font-size: .92rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; font-weight: 600; }
