:root {
  --paper: #f7f8f5;
  --surface: #ffffff;
  --ink: #1b1e1a;
  --muted: #646b62;
  --line: #e1e5de;
  --line-strong: #c9cfc4;
  --accent: #38761d;          /* verde de la plantilla de facturas */
  --accent-ink: #ffffff;
  --accent-soft: #e5f0df;
  --accent-strong: #2c5e17;
  --red: #c4271f;             /* rojo del logo */
  --red-soft: #fbe7e5;
  --warn: #8a5a00;
  --warn-soft: #fff1d1;
  --info: #1f6fae;
  --info-soft: #e2eef8;
  --shadow: 0 1px 2px rgba(20, 30, 20, .06), 0 6px 20px rgba(20, 30, 20, .05);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #131612;
    --surface: #1b1f1a;
    --ink: #e9ece6;
    --muted: #9ba396;
    --line: #2b3129;
    --line-strong: #3a4237;
    --accent: #7cc25a;
    --accent-ink: #0f1a0a;
    --accent-soft: #22341b;
    --accent-strong: #9bd67e;
    --red: #f07a70;
    --red-soft: #3d1c19;
    --warn: #f0c060;
    --warn-soft: #3a2e12;
    --info: #86bdea;
    --info-soft: #1a2c3c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--paper);
  color: var(--ink);
  min-height: 100vh;
  display: flex; flex-direction: column;
}
h1, h2, h3 { line-height: 1.2; margin: 0; letter-spacing: -.01em; }
p { margin: 0; }
a { color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }

/* ---------- barra superior ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.topbar::before {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: 4px; background: var(--accent);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; min-width: 0; }
.brand img { height: 34px; width: auto; border-radius: 4px; background: #fff; }
.brand strong { font-size: 16px; display: block; }
.brand small { color: var(--muted); font-size: 12px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user { display: flex; align-items: center; gap: 12px; font-size: 14px; color: var(--muted); min-width: 0; }
#user-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill { background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; font-size: 12px; padding: 2px 8px; border-radius: 99px; white-space: nowrap; }
.link { color: var(--ink); font-weight: 500; }

main { padding: clamp(16px, 4vw, 36px); max-width: 1240px; width: 100%; margin: 0 auto; flex: 1; }
.footer { text-align: center; color: var(--muted); font-size: 13px; padding: 24px 16px 32px; }

/* ---------- botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 550; font-size: 14px;
  padding: 8px 14px; border-radius: 9px;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn-danger { color: var(--red); border-color: color-mix(in srgb, var(--red) 45%, transparent); }
.btn-danger:hover { background: var(--red-soft); border-color: var(--red); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--accent-soft); border-color: transparent; }
.btn-sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn-lg { padding: 12px 22px; font-size: 16px; }
.icon-btn { border: 0; background: transparent; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--accent-soft); color: var(--ink); }
.btn svg, .tab svg { flex: none; }

/* ---------- formularios ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.form-card { padding: clamp(18px, 3vw, 28px); }
.form-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 14px 16px; }
.form-grid > * { grid-column: span 3; min-width: 0; }
.form-grid > .full { grid-column: 1 / -1; }
.form-grid > .third { grid-column: span 2; }
.form-grid > .sixth { grid-column: span 1; }
@media (max-width: 720px) {
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid > *, .form-grid > .third { grid-column: 1 / -1; }
  .form-grid > .sixth { grid-column: span 1; }
}
.field { display: grid; gap: 5px; align-content: start; }
.field > span, .field > legend, .label { font-size: 13px; font-weight: 600; color: var(--muted); }
.field small { color: var(--muted); font-size: 12px; }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, .tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
input[type="checkbox"], input[type="radio"] { width: auto; accent-color: var(--accent); }
input[aria-invalid="true"] { border-color: var(--red); }
textarea { resize: vertical; }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; cursor: pointer; }
.check input { margin-top: 3px; }
.section-title { font-size: 15px; font-weight: 700; margin: 8px 0 -2px; grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; }
.section-title::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.hint { font-size: 13px; color: var(--muted); }
.form-error { color: var(--red); background: var(--red-soft); padding: 10px 12px; border-radius: 8px; font-size: 14px; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 18px; }
.honeypot { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }

/* ---------- página pública ---------- */
.public-wrap { max-width: 760px; margin: 0 auto; display: grid; gap: 20px; }
.public-intro h1 { font-size: clamp(24px, 4vw, 32px); }
.public-intro p { color: var(--muted); margin-top: 8px; }
.done { text-align: center; padding: 40px 24px; display: grid; gap: 14px; justify-items: center; }
.done .ref { font: 600 22px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--accent-soft); color: var(--accent-strong); padding: 8px 16px; border-radius: 10px; }
.done svg { color: var(--accent); }

/* ---------- vistas centradas ---------- */
.center-view { min-height: 60vh; display: grid; place-items: center; }
.hero { max-width: 460px; text-align: center; display: grid; gap: 14px; justify-items: center; }
.hero img { width: 150px; height: auto; background: #fff; border-radius: 12px; padding: 8px; }
.hero h1 { font-size: clamp(22px, 4vw, 30px); }
.hero p { color: var(--muted); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- pestañas del apartado interno ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 22px; overflow-x: auto; scrollbar-width: thin; }
.tab {
  font: inherit; font-weight: 600; font-size: 14px; color: var(--muted);
  background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 12px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; margin-bottom: -1px;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--accent-strong); border-bottom-color: var(--accent); }
.tab .count { background: var(--red); color: #fff; font-size: 11px; font-weight: 700; border-radius: 99px; padding: 0 6px; min-width: 18px; text-align: center; }

.view-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.view-head h1 { font-size: 24px; }
.view-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
.toolbar select, .toolbar input[type="search"] { width: auto; }
.toolbar input[type="search"] { flex: 1 1 220px; }
.segmented { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 9px; overflow: hidden; background: var(--surface); }
.segmented button { font: inherit; font-size: 13px; font-weight: 600; border: 0; background: transparent; color: var(--muted); padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.segmented button + button { border-left: 1px solid var(--line); }
.segmented button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-strong); }

/* ---------- totales ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { padding: 12px 14px; }
.stat .label { font-size: 12px; }
.stat .value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }

/* ---------- tablas ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th { text-align: left; font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
table.data tr.anulada td { color: var(--muted); }
table.data tr.anulada .tercero { text-decoration: line-through; }
.doc-title { font-weight: 600; }
.doc-sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 99px; background: var(--line); color: var(--ink); white-space: nowrap; }
.badge-emitida { background: var(--accent-soft); color: var(--accent-strong); }
.badge-recibida { background: var(--info-soft); color: var(--info); }
.badge-hoja { background: var(--warn-soft); color: var(--warn); }
.badge-subv { background: transparent; border: 1px solid var(--line-strong); color: var(--muted); }
.badge-ok { background: var(--accent-soft); color: var(--accent-strong); }
.badge-no { background: var(--red-soft); color: var(--red); }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

/* ---------- solicitudes ---------- */
.req-list { display: grid; gap: 12px; }
.req { padding: 16px 18px; display: grid; gap: 10px; }
.req-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; align-items: baseline; }
.req-head h3 { font-size: 16px; }
.req-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 6px 18px; font-size: 14px; }
.req-grid dt { font-size: 12px; color: var(--muted); font-weight: 600; }
.req-grid dd { margin: 0; overflow-wrap: anywhere; }
.req-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- líneas de factura y viajes ---------- */
.lines { display: grid; gap: 8px; grid-column: 1 / -1; }
.line { display: grid; grid-template-columns: minmax(0, 1fr) 90px 120px 110px 36px; gap: 8px; align-items: center; }
.line-head { font-size: 12px; font-weight: 700; color: var(--muted); }
.line .importe { font-weight: 600; }
@media (max-width: 720px) {
  .line { grid-template-columns: 1fr 1fr; padding: 10px; border: 1px solid var(--line); border-radius: 10px; }
  .line > :first-child { grid-column: 1 / -1; }
  .line-head { display: none; }
}
.trips { display: grid; gap: 10px; grid-column: 1 / -1; }
.trip { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: grid; gap: 10px; grid-template-columns: 130px minmax(0, 1fr) minmax(0, 1fr) 90px auto; align-items: end; background: var(--surface); }
.trip .motivo { grid-column: 1 / 4; }
.trip .costs { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)) auto; gap: 10px; align-items: end; }
.trip .trip-total { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; padding-bottom: 8px; white-space: nowrap; }
@media (max-width: 820px) {
  .trip, .trip .costs { grid-template-columns: 1fr 1fr; }
  .trip .motivo { grid-column: 1 / -1; }
}
.totals { grid-column: 1 / -1; justify-self: end; min-width: min(100%, 360px); display: grid; gap: 4px; font-size: 14px; }
.totals div { display: flex; justify-content: space-between; gap: 18px; }
.totals .grand { font-size: 17px; font-weight: 700; border-top: 1px solid var(--line); padding-top: 6px; margin-top: 4px; }
.warn-box { background: var(--warn-soft); color: var(--warn); padding: 10px 12px; border-radius: 8px; font-size: 13.5px; grid-column: 1 / -1; }
.info-box { background: var(--info-soft); color: var(--info); padding: 10px 12px; border-radius: 8px; font-size: 13.5px; grid-column: 1 / -1; }

.subv-checks { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.subv-checks .check { background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
.subv-checks .check:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }

/* ---------- subvenciones ---------- */
.subv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }
.subv-card { padding: 16px; display: grid; gap: 10px; }
.subv-card h3 { font-size: 16px; }
.meter { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.meter > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.meter.over > i { background: var(--warn); }
.subv-rows { display: grid; gap: 3px; font-size: 13.5px; }
.subv-rows div { display: flex; justify-content: space-between; gap: 10px; }
.subv-edit { display: grid; gap: 10px; grid-column: 1 / -1; }
.subv-edit-row { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1.4fr) 90px 130px auto auto; gap: 8px; align-items: center; }
@media (max-width: 820px) { .subv-edit-row { grid-template-columns: 1fr 1fr; } }

/* ---------- diálogos ---------- */
.dialog {
  border: 0; padding: 0; border-radius: 16px; width: min(760px, calc(100vw - 24px)); max-height: calc(100vh - 32px);
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(15, 20, 15, .45); }
.dialog-sm { width: min(460px, calc(100vw - 24px)); }
.dialog-pdf { width: min(980px, calc(100vw - 24px)); height: calc(100vh - 32px); }
.dialog-pdf[open] { display: flex; flex-direction: column; }
.dialog form { display: contents; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.dialog-head h2 { font-size: 18px; }
.dialog-body { padding: 18px 20px; overflow: auto; }
.dialog-pdf .dialog-body { flex: 1; padding: 0; display: flex; }
.dialog-pdf iframe { border: 0; width: 100%; flex: 1; background: #525659; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--line); flex-wrap: wrap; }

.dropzone { position: relative; border: 2px dashed var(--line-strong); border-radius: 12px; padding: 22px; text-align: center; color: var(--muted); background: var(--paper); cursor: pointer; }
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.dropzone strong { color: var(--ink); }

/* ---------- avisos ---------- */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 100; width: min(460px, calc(100vw - 32px)); }
.toast { background: var(--ink); color: var(--paper); padding: 10px 14px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); transition: opacity .3s, transform .3s; }
.toast-error { background: var(--red); color: #fff; }
.toast.out { opacity: 0; transform: translateY(8px); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- ajustes finos y móvil ---------- */
.view-head .actions select { width: auto; }
.req-actions { align-items: center; }
table.data td.c-doc { min-width: 260px; }
table.data td.c-acc { width: 1%; }
table.data td.c-acc .row-actions { flex-wrap: nowrap; }
@media (max-width: 720px) {
  #user-email { display: none; }
  .view-head .actions { width: 100%; }
  .toolbar select { flex: 1 1 45%; }
  .segmented { max-width: 100%; overflow-x: auto; }
  table.data thead { display: none; }
  table.data tbody tr {
    display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 12px 14px;
    grid-template-areas: "fecha imp" "doc doc" "subv subv" "estado acc";
    border-bottom: 1px solid var(--line);
  }
  table.data tbody tr:last-child { border-bottom: 0; }
  table.data td { border: 0; padding: 0; min-width: 0; }
  table.data td.c-doc { min-width: 0; grid-area: doc; }
  table.data td.c-fecha { grid-area: fecha; color: var(--muted); font-size: 13px; }
  table.data td.c-imp { grid-area: imp; font-weight: 700; }
  table.data td.c-subv { grid-area: subv; }
  table.data td.c-subv:has(.muted) { display: none; }
  table.data td.c-estado { grid-area: estado; align-self: center; }
  table.data td.c-acc { grid-area: acc; width: auto; }
}
