:root {
  --azul: #13284a;
  --azul-2: #1d3d6b;
  --naranja: #e07a16;
  --fondo: #f4f6f9;
  --panel: #ffffff;
  --texto: #1b2430;
  --suave: #5b6675;
  --borde: #d9dfe7;
  --verde: #1d7a46;
  --rojo: #b42318;
  --radio: 10px;
  --sombra: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--texto); background: var(--fondo);
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--azul-2); }
h1 { font-size: 1.45rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.loading { padding: 2rem; text-align: center; color: var(--suave); }
[hidden] { display: none !important; }

/* barra superior */
#topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 1rem;
  background: var(--azul); color: #fff; padding: .55rem 1rem; padding-top: max(.55rem, env(safe-area-inset-top));
  flex-wrap: wrap;
}
.brand img.logo { width: auto; max-width: 110px; height: 32px; object-fit: contain; background: #fff; border-radius: 6px; padding: 2px 4px; }
.logo-edit { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
.logo-preview { width: 220px; height: 110px; border: 1px dashed var(--borde); border-radius: 8px; display: flex; align-items: center; justify-content: center; background: #fff; padding: 8px; }
.logo-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.brand { display: flex; align-items: center; gap: .5rem; color: #fff; text-decoration: none; font-weight: 700; }
#nav { display: flex; gap: .25rem; flex: 1; overflow-x: auto; }
#nav a {
  color: #dbe5f1; text-decoration: none; padding: .45rem .75rem; border-radius: 8px; white-space: nowrap; font-weight: 500;
}
#nav a.active, #nav a:hover { background: rgba(255, 255, 255, .14); color: #fff; }
#nav .badge { background: var(--naranja); color: #fff; border-radius: 999px; padding: 0 .45rem; font-size: .78rem; margin-left: .3rem; }
.userbox { display: flex; align-items: center; gap: .75rem; font-size: .92rem; }
.userbox a { color: #fff; text-decoration: none; }
.userbox .link { color: #dbe5f1; }
@media (max-width: 700px) {
  #topbar { gap: .4rem .75rem; }
  #nav { order: 3; flex-basis: 100%; }
  .userbox { margin-left: auto; }
}

main { max-width: 1180px; margin: 0 auto; padding: 1rem; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: .25rem 0 1rem; flex-wrap: wrap; }

/* tarjetas y formularios */
.card { background: var(--panel); border: 1px solid var(--borde); border-radius: var(--radio); box-shadow: var(--sombra); padding: 1rem; margin-bottom: 1rem; }
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .3rem; color: #344054; }
input, select, textarea {
  width: 100%; font: inherit; color: inherit; padding: .65rem .7rem; border: 1px solid #c3ccd8; border-radius: 8px; background: #fff;
  min-height: 46px;
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 3px solid rgba(29, 61, 107, .25); border-color: var(--azul-2); }
input[type=checkbox] { width: 22px; min-height: 22px; height: 22px; accent-color: var(--azul-2); }
.field { margin-bottom: .9rem; }
.grid { display: grid; gap: .9rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
.hint { color: var(--suave); font-size: .88rem; margin: .25rem 0 0; }
.error-msg { color: var(--rojo); font-weight: 600; min-height: 1.2em; }

/* botones */
button, .btn {
  font: inherit; font-weight: 600; border: 1px solid transparent; border-radius: 8px; padding: .65rem 1rem; min-height: 46px;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: .4rem; text-decoration: none;
  background: #e8edf3; color: var(--texto);
}
button:hover, .btn:hover { filter: brightness(.96); }
button:disabled { opacity: .55; cursor: not-allowed; }
.primary { background: var(--azul-2); color: #fff; }
.accent { background: var(--naranja); color: #fff; }
.success { background: var(--verde); color: #fff; }
.danger { background: #fff; color: var(--rojo); border-color: #f1b8b3; }
.ghost { background: #fff; border-color: var(--borde); }
.link { background: none; border: 0; padding: 0; min-height: 0; color: var(--azul-2); font-weight: 500; }
.small { min-height: 36px; padding: .35rem .7rem; font-size: .9rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.icon-btn { min-height: 40px; width: 40px; padding: 0; background: #fff; border-color: var(--borde); color: var(--rojo); font-size: 1.2rem; }

/* estados */
.estado { display: inline-block; border-radius: 999px; padding: .15rem .6rem; font-size: .8rem; font-weight: 700; white-space: nowrap; }
.estado-borrador { background: #eef1f5; color: #475467; }
.estado-pendiente { background: #fff1e0; color: #a64d00; }
.estado-firmado { background: #e3f5ea; color: #136237; }
.estado-facturado { background: #e5edfb; color: #1d4ed8; }

/* filtros */
.chips { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: .8rem; }
.chip { min-height: 38px; padding: .35rem .85rem; border-radius: 999px; background: #fff; border: 1px solid var(--borde); font-weight: 600; font-size: .92rem; }
.chip.on { background: var(--azul); color: #fff; border-color: var(--azul); }
.filters { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); align-items: end; }
.filters label { font-size: .82rem; }
.summary { display: flex; gap: 1.5rem; flex-wrap: wrap; color: var(--suave); margin: .2rem 0 .8rem; }
.summary b { color: var(--texto); }
.bulkbar {
  position: sticky; bottom: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between; gap: .75rem; flex-wrap: wrap;
  background: var(--azul); color: #fff; padding: .7rem 1rem; border-radius: var(--radio); margin-top: .5rem;
}

/* tabla / lista */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .55rem; border-bottom: 1px solid #edf0f4; vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--suave); background: #f8fafc; }
td.num, th.num { text-align: right; white-space: nowrap; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: #f6f9fc; }
.list-table { background: #fff; border: 1px solid var(--borde); border-radius: var(--radio); overflow: hidden; box-shadow: var(--sombra); }
.muted { color: var(--suave); font-size: .88rem; }
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--suave); }

@media (max-width: 760px) {
  .list-table { background: none; border: 0; box-shadow: none; }
  .list-table thead { display: none; }
  .list-table tr { display: grid; grid-template-columns: auto 1fr auto; gap: .1rem .6rem; background: #fff; border: 1px solid var(--borde); border-radius: var(--radio); margin-bottom: .6rem; padding: .75rem; box-shadow: var(--sombra); }
  .list-table td { border: 0; padding: 0; }
  .list-table td.c-sel { grid-row: span 3; align-self: center; }
  .list-table td.c-num { font-weight: 700; }
  .list-table td.c-estado { text-align: right; }
  .list-table td.c-obra { grid-column: 2 / 4; }
  .list-table td.c-fecha { grid-column: 2; color: var(--suave); }
  .list-table td.c-horas { text-align: right; color: var(--suave); }
  .list-table td.c-extra { display: none; }
}

/* líneas del formulario */
.lines { display: grid; gap: .6rem; }
.line { display: grid; gap: .5rem; align-items: end; background: #f8fafc; border: 1px solid #e6eaf0; border-radius: 8px; padding: .6rem; }
.line label { font-size: .78rem; margin-bottom: .15rem; }
.line > div { min-width: 0; }
.line input { min-width: 0; padding-left: .5rem; padding-right: .4rem; }
.line-personal { grid-template-columns: minmax(150px, 2fr) 1fr 1fr 90px 40px; }
.line-material { grid-template-columns: minmax(150px, 3fr) 100px 110px 40px; }
.line-maquina { grid-template-columns: minmax(150px, 2fr) minmax(130px, 1.5fr) 100px 1fr 1fr 80px 40px; }
.line-maquina .f-v { display: none; grid-column: span 3; }
.line-maquina.por-viajes .f-h { display: none; }
.line-maquina.por-viajes .f-v { display: block; }
@media (max-width: 640px) {
  .line-personal { grid-template-columns: 1fr 1fr 74px 40px; }
  .section-head .small { flex-shrink: 0; }
  .line-personal .f-nombre { grid-column: 1 / -1; }
  .line-material { grid-template-columns: 1fr 1fr 40px; }
  .line-material .f-desc { grid-column: 1 / -1; }
  .line-maquina { grid-template-columns: 1fr 1fr; }
  .line-maquina .del { justify-self: end; }
  .line-maquina .f-desc, .line-maquina .f-cond { grid-column: 1 / -1; }
  .line-maquina .f-v { grid-column: auto; }
}
.section-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; margin-bottom: .6rem; }
.section-head h2 { margin: 0; }
.total-line { text-align: right; font-weight: 700; margin-top: .5rem; }
.savebar {
  position: sticky; bottom: 0; z-index: 10; display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap;
  background: rgba(244, 246, 249, .95); backdrop-filter: blur(6px); padding: .75rem 0; padding-bottom: max(.75rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--borde);
}
@media (max-width: 640px) { .savebar button { flex: 1; } }
.notice { background: #fff8eb; border: 1px solid #f6d7a7; color: #7a4a05; padding: .7rem .9rem; border-radius: 8px; margin-bottom: 1rem; display: flex; gap: .75rem; align-items: center; justify-content: space-between; flex-wrap: wrap; }

/* detalle */
.info { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .8rem 1.25rem; }
.info dt { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--suave); font-weight: 600; }
.info dd { margin: .1rem 0 0; font-weight: 600; }
.prewrap { white-space: pre-wrap; }
.firma-box { display: flex; gap: 1.25rem; align-items: center; flex-wrap: wrap; }
.firma-box img { max-width: 320px; width: 100%; border: 1px dashed var(--borde); border-radius: 8px; background: #fff; }
.pending-sign { background: #fff8eb; border: 1px dashed #f0b35e; border-radius: 8px; padding: 1rem; text-align: center; color: #7a4a05; }

/* modal de firma */
.modal-back { position: fixed; inset: 0; z-index: 50; background: rgba(15, 23, 42, .55); display: flex; align-items: center; justify-content: center; padding: .75rem; }
.modal { background: #fff; border-radius: 14px; width: min(720px, 100%); max-height: 100%; overflow: auto; padding: 1rem; box-shadow: 0 20px 50px rgba(0, 0, 0, .3); }
.sign-pad { position: relative; border: 2px dashed #9aa8bb; border-radius: 10px; background: #fff; touch-action: none; }
.sign-pad canvas { display: block; width: 100%; height: 240px; cursor: crosshair; }
.sign-pad .baseline { position: absolute; left: 8%; right: 8%; bottom: 52px; border-bottom: 1px solid #c8d0db; pointer-events: none; }
.sign-pad .ph { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #a3adbb; pointer-events: none; font-size: 1.05rem; }
.modal .grid { grid-template-columns: 3fr 2fr; gap: .6rem; }
.modal .field { margin-bottom: .6rem; }
.modal-small { width: min(420px, 100%); }
.modal-small h2 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; flex-wrap: wrap; margin-top: .4rem; }
@media (max-width: 640px) { .sign-pad canvas { height: 200px; } .modal { padding: .8rem; } }
.sign-summary { background: #f4f7fb; border-radius: 8px; padding: .7rem .9rem; margin-bottom: .8rem; font-size: .95rem; }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: 1.25rem; transform: translateX(-50%) translateY(150%); z-index: 60; transition: transform .25s;
  background: #101828; color: #fff; padding: .75rem 1.1rem; border-radius: 10px; max-width: calc(100% - 2rem); box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.err { background: var(--rojo); }

/* login */
.auth-wrap { min-height: 90vh; display: flex; align-items: center; justify-content: center; }
.auth { width: min(420px, 100%); }
.auth .logo { display: flex; align-items: center; gap: .6rem; margin-bottom: 1rem; }
.auth button { width: 100%; }

/* tabs de gestión */
.tabs { display: flex; gap: .3rem; border-bottom: 2px solid var(--borde); margin-bottom: 1rem; overflow-x: auto; }
.tabs a { padding: .6rem 1rem; text-decoration: none; color: var(--suave); font-weight: 600; border-bottom: 3px solid transparent; margin-bottom: -2px; white-space: nowrap; }
.tabs a.active { color: var(--azul); border-color: var(--naranja); }
.two-col { display: grid; gap: 1rem; grid-template-columns: 1fr 360px; align-items: start; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }
tr.inactive td { color: #98a2b3; }

/* vista de impresión / PDF */
.print-toolbar { display: flex; gap: .5rem; justify-content: flex-end; margin-bottom: 1rem; flex-wrap: wrap; }
.doc { background: #fff; max-width: 210mm; margin: 0 auto; padding: 14mm 14mm; box-shadow: var(--sombra); border: 1px solid var(--borde); font-size: 13px; color: #111; }
.doc h1 { font-size: 22px; letter-spacing: .02em; }
.doc-head { display: flex; justify-content: space-between; gap: 1rem; border-bottom: 3px solid var(--azul); padding-bottom: 10px; margin-bottom: 12px; }
.doc-emp { display: flex; gap: 12px; align-items: center; }
.doc-logo { max-height: 70px; max-width: 170px; object-fit: contain; }
.doc-head .emp { font-weight: 700; font-size: 16px; }
.doc-head .right { text-align: right; }
.doc-boxes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.doc-box { border: 1px solid #bbb; border-radius: 4px; padding: 8px 10px; }
.doc-box h3 { margin: 0 0 4px; font-size: 11px; text-transform: uppercase; color: #555; letter-spacing: .04em; }
.doc h4 { margin: 14px 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--azul); }
.doc table th, .doc table td { border: 1px solid #ccc; padding: 4px 6px; font-size: 12px; }
.doc table th { background: #eef2f7; color: #222; text-transform: none; letter-spacing: 0; }
.doc .text-box { border: 1px solid #ccc; padding: 6px 8px; min-height: 40px; white-space: pre-wrap; }
.doc-sign { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 18px; }
.doc-sign .box { border: 1px solid #bbb; border-radius: 4px; padding: 8px 10px; min-height: 120px; }
.doc-sign img { max-height: 90px; max-width: 100%; display: block; }
.doc-foot { margin-top: 14px; font-size: 10.5px; color: #666; }

@media print {
  @page { size: A4; margin: 10mm; }
  body { background: #fff; }
  #topbar, .print-toolbar, #toast { display: none !important; }
  main { padding: 0; max-width: none; }
  .doc { box-shadow: none; border: 0; padding: 0; max-width: none; }
  .doc table, .doc-sign { break-inside: avoid; }
}
