:root {
  --green: #007a33;
  --green-dark: #005c26;
  --green-soft: #e6f3ea;
  --ink: #1b1f23;
  --muted: #6b7280;
  --line: #e3e6ea;
  --bg: #f5f6f7;
  --card: #ffffff;
  --warn-bg: #fff4d6;
  --warn: #8a5a00;
  --err-bg: #fde8e8;
  --err: #a12020;
  --ok-bg: #e2f5e8;
  --ok: #166534;
  --info-bg: #e5efff;
  --info: #1d4ed8;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
* { box-sizing: border-box; }
html { font-size: 15px; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.45;
}
a { color: var(--green-dark); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 0.25rem; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; margin: 0 0 0.75rem; }
h3 { font-size: 0.95rem; margin: 0 0 0.5rem; }
p { margin: 0 0 0.75rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
.upper { text-transform: uppercase; }
.text-err { color: var(--err); }
.text-ok { color: var(--ok); }
.text-warn { color: var(--warn); }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  background: #fff; border-bottom: 1px solid var(--line);
  padding: 0 1.25rem; height: 54px; position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 0.5rem; font-size: 1.05rem; color: var(--ink); }
.brand:hover { text-decoration: none; }
.brand.big { font-size: 1.3rem; justify-content: center; margin-bottom: 1rem; }
.brand-dot { width: 14px; height: 14px; border-radius: 50%; background: radial-gradient(circle, var(--green) 0 30%, #fff 31% 55%, var(--green) 56%); display: inline-block; }
.topbar nav { display: flex; gap: 0.25rem; flex: 1; }
.topbar nav a { padding: 0.4rem 0.75rem; border-radius: 6px; color: var(--ink); font-weight: 500; }
.topbar nav a:hover { background: var(--bg); text-decoration: none; }
.topbar nav a.active { background: var(--green-soft); color: var(--green-dark); }
.topbar-user { display: flex; align-items: center; gap: 0.5rem; color: var(--muted); font-size: 0.9rem; }

.container { max-width: 1040px; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }
.container.wide { max-width: 1320px; }

.page-header { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.page-header .subtitle { color: var(--muted); margin: 0; }
.page-header .actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.back { font-size: 0.85rem; color: var(--muted); display: inline-block; margin-bottom: 0.25rem; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem; margin-bottom: 1.25rem; }
.card-title { display: flex; justify-content: space-between; align-items: center; padding-bottom: 0.5rem; }
.table-wrap { padding: 0; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { padding: 0.6rem 0.85rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; background: #fafbfc; }
tbody tr:hover { background: #fafbfc; }
tfoot th { background: #fff; color: var(--ink); text-transform: none; font-size: 0.92rem; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions .btn + .btn, .row-actions form + form { margin-left: 0.25rem; }
tr.muted td { color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; gap: 0.35rem;
  padding: 0.5rem 0.9rem; border-radius: 6px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; line-height: 1.2;
  justify-content: center; text-align: center;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-dark); }
.btn-danger { color: var(--err); border-color: #f1c0c0; }
.btn-danger:hover { background: var(--err-bg); }
.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.82rem; }
.btn-link { border: none; background: none; color: var(--green-dark); padding: 0.25rem 0.5rem; }
.btn.disabled { opacity: 0.5; pointer-events: none; }
.btn:disabled { opacity: 0.6; cursor: wait; }

.alert { padding: 0.75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid transparent; }
.alert ul { margin: 0.35rem 0 0; padding-left: 1.2rem; }
.alert-ok { background: var(--ok-bg); color: var(--ok); border-color: #bfe6cc; }
.alert-err { background: var(--err-bg); color: var(--err); border-color: #f1c0c0; }
.alert-warn { background: var(--warn-bg); color: var(--warn); border-color: #f2dfae; }
.alert-info { background: var(--info-bg); color: var(--info); border-color: #c7d9fb; }

.badge { display: inline-block; padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.78rem; font-weight: 600; white-space: nowrap; }
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-muted { background: var(--bg); color: var(--muted); }
.badge-info { background: var(--info-bg); color: var(--info); }

.empty { text-align: center; color: var(--muted); padding: 3rem 1rem; background: #fff; border: 1px dashed var(--line); border-radius: var(--radius); }

/* Forms */
label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.86rem; font-weight: 500; color: #374151; }
label small { font-weight: 400; }
input, select, textarea {
  font: inherit; font-size: 0.95rem; padding: 0.5rem 0.65rem; border: 1px solid #cfd4da; border-radius: 6px; background: #fff; color: var(--ink); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(0, 122, 51, 0.25); border-color: var(--green); }
input[readonly] { background: #f3f4f6; color: #374151; }
input.num { text-align: right; font-variant-numeric: tabular-nums; }
label.check { flex-direction: row; align-items: center; gap: 0.5rem; font-weight: 500; }
label.check input { width: auto; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.9rem 1.25rem; }
.form-grid .span-2 { grid-column: 1 / -1; }
.form-grid h2 { margin-bottom: 0; padding-top: 0.5rem; border-top: 1px solid var(--line); }
.form-grid h2:first-child { border-top: none; padding-top: 0; }
.form-actions { display: flex; gap: 0.5rem; align-items: center; justify-content: flex-start; }
.stack { display: flex; flex-direction: column; gap: 0.9rem; }
.inline { display: inline; }
.inline-form { display: flex; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }
.inline-form input { flex: 1; min-width: 8rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.9rem 1.25rem; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.9rem 1.25rem; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: 0.35rem 0; border-bottom: 1px solid var(--line); }
.danger-zone { display: flex; justify-content: space-between; align-items: center; gap: 1rem; border: 1px dashed #f1c0c0; border-radius: var(--radius); padding: 0.75rem 1rem; margin-top: 1rem; }
.searchbar { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.searchbar input { max-width: 28rem; }
.toolbar { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.sticky-actions { position: sticky; bottom: 0.75rem; display: flex; justify-content: space-between; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08); }
.help ul { padding-left: 1.2rem; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.25rem; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 0.85rem 1rem; }
.stat-label { display: block; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.stat-value { display: block; font-size: 1.35rem; font-weight: 600; font-variant-numeric: tabular-nums; }

/* Auth */
.auth-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; }
.auth-card { width: 100%; max-width: 380px; }
.auth-card h1 { text-align: center; font-size: 1.2rem; margin-bottom: 1rem; }

/* Liquidación */
.liq-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 1.25rem; align-items: start; }
.liq-side { position: sticky; top: 70px; }
.section-title { display: flex; align-items: center; gap: 0.6rem; margin: 0 0 0.9rem; }
.section-title .step { width: 24px; height: 24px; border-radius: 50%; background: var(--green); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 0.8rem; font-weight: 700; }
.dep-search { position: relative; }
.dep-results {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 30; background: #fff; border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  max-height: 320px; overflow-y: auto; margin-top: 0.25rem;
}
.dep-results button { display: block; width: 100%; text-align: left; background: none; border: none; border-bottom: 1px solid var(--line); padding: 0.6rem 0.75rem; font: inherit; cursor: pointer; }
.dep-results button:hover { background: var(--green-soft); }
.dep-results .none { padding: 0.75rem; color: var(--muted); }
.dep-card { display: flex; justify-content: space-between; align-items: center; gap: 1rem; background: var(--green-soft); border: 1px solid #bfe6cc; border-radius: 6px; padding: 0.75rem 1rem; }
.dep-card .name { font-weight: 600; }
.calc-line { display: grid; grid-template-columns: 1.4fr 1fr 1fr 1fr; gap: 0.5rem 0.75rem; align-items: end; padding: 0.4rem 0; }
.calc-line.head { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); padding-bottom: 0; }
.calc-line .lbl { font-size: 0.9rem; padding-bottom: 0.5rem; }
.calc-line input { text-align: right; }
.calc-line input[type='text'].left { text-align: left; }
.total-row { display: flex; justify-content: space-between; padding: 0.45rem 0; border-top: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.total-row.big { font-size: 1.25rem; font-weight: 700; border-top: 2px solid var(--ink); }
.total-row .diff-ok { color: var(--ok); }
.total-row .diff-bad { color: var(--err); }
.concepto-list { display: flex; flex-direction: column; gap: 0.4rem; }
.concepto-list label.check { padding: 0.35rem 0.5rem; border: 1px solid var(--line); border-radius: 6px; justify-content: space-between; }
.concepto-list label.check span:last-child { font-variant-numeric: tabular-nums; color: var(--muted); }
.concepto-list label.check:has(input:checked) { border-color: var(--green); background: var(--green-soft); }
.objetivo { font-size: 1.6rem; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.status { font-size: 0.85rem; color: var(--muted); min-height: 1.2em; }
.calc-msg { margin-top: 0.75rem; }
.hidden { display: none !important; }

@media (max-width: 900px) {
  .liq-layout, .grid-2, .form-grid, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .liq-side { position: static; }
  .stats { grid-template-columns: 1fr 1fr; }
  .topbar { flex-wrap: wrap; height: auto; padding: 0.5rem 1rem; gap: 0.5rem; }
  .topbar nav { flex-wrap: wrap; }
  .calc-line { grid-template-columns: 1fr 1fr; }
}

/* Firma */
.sign-wrap { max-width: 640px; padding-top: 2rem; }
.sign-wrap .brand.big { justify-content: flex-start; margin-bottom: 1.25rem; }
.sign-card { max-width: none; }
.sign-card h1 { text-align: left; }
.sign-summary { border: 1px solid var(--line); border-radius: var(--radius); padding: 0.25rem 1rem; margin: 1rem 0; }
.sign-summary .row { display: flex; justify-content: space-between; gap: 1rem; padding: 0.5rem 0; border-bottom: 1px solid var(--line); font-size: 0.95rem; }
.sign-summary .row span:last-child, .sign-summary .row strong { text-align: right; }
.sign-summary .amount { display: flex; justify-content: space-between; align-items: center; padding: 0.75rem 0 0.5rem; font-size: 1rem; }
.sign-summary .amount strong { font-size: 1.7rem; color: var(--green-dark); font-variant-numeric: tabular-nums; }
.sigpad { position: relative; border: 2px dashed #cfd4da; border-radius: var(--radius); background: #fff; touch-action: none; overflow: hidden; }
.sigpad canvas { display: block; width: 100%; cursor: crosshair; }
.sigpad-hint { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); color: #c4c9d0; font-size: 1.1rem; pointer-events: none; }
.sign-preview, .firma-img { max-height: 90px; max-width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 0.35rem 0.6rem; background: #fff; }
.firma-box { display: flex; flex-direction: column; gap: 0.5rem; }
.firma-box .firma-img { max-height: 60px; align-self: flex-start; }
.badge-sign { background: #ede9fe; color: #5b21b6; }

.brand-logo { width: 24px; height: 24px; object-fit: contain; display: inline-block; }
.brand.big .brand-logo { width: 40px; height: 40px; }

/* Páginas públicas de firma: centrado y móvil */
.sign-wrap .brand.big { align-items: center; }
.sign-wrap .brand.big span { line-height: 1.2; }
.sign-center { text-align: center; }
.sign-center h1 { text-align: center; }
.sign-icon { width: 48px; height: 48px; border-radius: 50%; background: var(--warn-bg); color: var(--warn); font-weight: 700; font-size: 1.4rem; display: inline-flex; align-items: center; justify-content: center; margin-bottom: 0.75rem; }
.sign-actions { justify-content: space-between; }
.sign-actions .btn { min-height: 44px; }
.sign-actions .btn-primary { flex: 1; justify-content: center; font-size: 1.05rem; }
.sigpad-hint { font-size: 1rem; }
@media (max-width: 600px) {
  html { font-size: 16px; }
  .container { padding: 1rem 0.85rem 3rem; }
  .sign-wrap { padding-top: 1rem; }
  .sign-wrap .brand.big { font-size: 1.05rem; margin-bottom: 0.75rem; }
  .sign-wrap .brand.big .brand-logo { width: 34px; height: 34px; }
  .sign-card { padding: 1rem; }
  .sign-card h1 { font-size: 1.25rem; }
  .sign-summary .row { flex-direction: column; gap: 0.1rem; }
  .sign-summary .row span:last-child, .sign-summary .row strong { text-align: left; }
  .sign-summary .amount { flex-direction: column; align-items: flex-start; gap: 0.1rem; }
  .sign-summary .amount strong { font-size: 2rem; }
  .sign-actions { flex-direction: column-reverse; align-items: stretch; }
  .sign-actions .btn { justify-content: center; }
  .page-header { align-items: flex-start; }
  .page-header .actions { width: 100%; }
  .stats { grid-template-columns: 1fr 1fr; gap: 0.6rem; }
  .stat-value { font-size: 1.1rem; }
  .toolbar { gap: 0.4rem; }
  .btn { min-height: 40px; }
  .calc-line { grid-template-columns: 1fr 1fr; }
  .calc-line .lbl { grid-column: 1 / -1; padding-bottom: 0; }
  .topbar { position: static; }
  .topbar nav a { padding: 0.35rem 0.55rem; font-size: 0.9rem; }
  .liq-side .card { position: static; }
  .sticky-actions { flex-direction: column; gap: 0.5rem; align-items: stretch; }
}

/* Navegación y dashboard en móvil */
@media (max-width: 700px) {
  .topbar { display: grid; grid-template-columns: 1fr auto; align-items: center; row-gap: 0.35rem; column-gap: 0.75rem; height: auto; padding: 0.55rem 0.85rem 0; position: static; }
  .topbar .brand { grid-column: 1; grid-row: 1; font-size: 1rem; }
  .topbar-user { grid-column: 2; grid-row: 1; font-size: 0.85rem; }
  .topbar-user span { max-width: 7rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar nav { grid-column: 1 / -1; grid-row: 2; display: flex; flex-wrap: nowrap; overflow-x: auto; gap: 0.15rem; margin: 0 -0.85rem; padding: 0 0.85rem; scrollbar-width: none; border-top: 1px solid var(--line); }
  .topbar nav::-webkit-scrollbar { display: none; }
  .topbar nav a { flex: 0 0 auto; white-space: nowrap; border-radius: 0; padding: 0.6rem 0.7rem; border-bottom: 2px solid transparent; font-size: 0.9rem; }
  .topbar nav a.active { background: none; border-bottom-color: var(--green); color: var(--green-dark); }
  .page-header { flex-direction: column; align-items: stretch; gap: 0.75rem; margin-bottom: 1rem; }
  .page-header h1 { font-size: 1.3rem; }
  .page-header .actions { display: flex; width: 100%; }
  .page-header .actions > * { flex: 1 1 auto; }
  .page-header .actions form { display: contents; }
  .toolbar { display: flex; flex-wrap: wrap; }
  .toolbar > .muted { width: 100%; font-size: 0.85rem; }
  .toolbar form.inline { display: contents; }
  .toolbar .btn { flex: 1 1 45%; }
  .form-actions { flex-wrap: wrap; }
  .form-actions .btn { flex: 1 1 auto; }
  .searchbar { flex-wrap: wrap; }
  .searchbar input { max-width: none; }
  .inline-form input { flex: 1 1 100%; }
  .danger-zone { flex-direction: column; align-items: stretch; }
  .stats { grid-template-columns: 1fr 1fr; }
  .grid-3, .grid-4 { grid-template-columns: 1fr 1fr; }
  .dep-card { flex-direction: column; align-items: stretch; }
  .row-actions { white-space: normal; }
  .liq-layout { display: flex; flex-direction: column; }
  .liq-side { position: static; }
  .liq-side .card .btn { width: 100%; }
}

/* Tarjetas para listados en móvil */
.cards-mobile { display: none; }
.mcard { display: flex; flex-direction: column; gap: 0.35rem; padding: 0.85rem 1rem; margin-bottom: 0; color: var(--ink); }
.mcard:hover { text-decoration: none; }
.mcard-title { font-weight: 600; color: var(--green-dark); }
.mcard-row { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
@media (max-width: 700px) {
  .cards-mobile { display: grid; gap: 0.6rem; margin-bottom: 1.25rem; }
  .hide-mobile { display: none !important; }
  .firma-grid { grid-template-columns: 1fr; }
  .page-header .actions .btn { flex: 1 1 auto; }
}
