:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #eef2ee;
  --text: #17211c;
  --muted: #5d6b63;
  --line: #dde4de;
  --accent: #0e7c5a;
  --accent-ink: #ffffff;
  --accent-soft: #dcf1e7;
  --ok: #138a5e;
  --ok-soft: #dff3e9;
  --warn: #b25e00;
  --warn-soft: #fff0da;
  --bad: #b3261e;
  --bad-soft: #fbe3e1;
  --info: #1f5fbf;
  --info-soft: #e2ecfb;
  --shadow: 0 1px 2px rgb(16 32 24 / .06), 0 6px 20px rgb(16 32 24 / .07);
  --radius: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1512;
    --surface: #16201b;
    --surface-2: #1d2a24;
    --text: #e7efe9;
    --muted: #9aaba1;
    --line: #2a3832;
    --accent: #34c08c;
    --accent-ink: #062016;
    --accent-soft: #173b2d;
    --ok: #4fd19c;
    --ok-soft: #173b2d;
    --warn: #ffb35c;
    --warn-soft: #3a2a14;
    --bad: #ff8a80;
    --bad-soft: #3d1c1a;
    --info: #8ab4ff;
    --info-soft: #1b2a45;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 8px 24px rgb(0 0 0 / .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

/* ---------- Cabecera ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 9px; }
.brand h1 { margin: 0; font-size: 19px; letter-spacing: -.01em; }
.brand p { margin: 0; font-size: 12.5px; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 6px; }

.wrap { max-width: 760px; margin: 0 auto; padding: 4px 16px 0; }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600;
  text-decoration: none; transition: transform .1s, background .15s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: var(--surface); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, var(--line)); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; }
.btn.grow { flex: 1; }
.btn svg { width: 20px; height: 20px; }
.icon-btn {
  display: grid; place-items: center; width: 42px; height: 42px; border-radius: 12px;
  border: 0; background: transparent; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- Resumen ---------- */
.summary { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 10px; margin: 6px 0 12px; }
.stat {
  display: flex; flex-direction: column; gap: 2px; text-align: left;
  padding: 14px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--surface); box-shadow: var(--shadow); min-width: 0;
}
.stat .label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.stat strong { font-size: 24px; line-height: 1.15; letter-spacing: -.02em; overflow-wrap: anywhere; }
.stat .sub { font-size: 12px; color: var(--muted); }
.stat.main { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.stat.main .label, .stat.main .sub { color: color-mix(in srgb, var(--accent-ink) 78%, transparent); }
.stat.warn strong { color: var(--warn); }
.stat.ret strong { color: var(--info); }
@media (max-width: 520px) {
  .summary { grid-template-columns: 1fr 1fr; }
  .stat.main { grid-column: 1 / -1; }
}

/* ---------- Alerta ---------- */
.alert {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  margin: 0 0 12px; padding: 12px 14px; border-radius: 14px;
  border: 1px solid color-mix(in srgb, var(--warn) 30%, transparent);
  background: var(--warn-soft); color: var(--text);
}
.alert b { color: var(--warn); }
.alert .go { margin-left: auto; color: var(--warn); font-weight: 700; white-space: nowrap; }

/* ---------- Búsqueda y filtros ---------- */
.tools { display: flex; gap: 8px; }
.search {
  flex: 1; display: flex; align-items: center; gap: 8px; padding: 0 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; min-width: 0;
}
.search svg { color: var(--muted); width: 18px; height: 18px; }
.search input { flex: 1; min-width: 0; height: 44px; border: 0; outline: 0; background: transparent; }
.tools select {
  height: 46px; padding: 0 10px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); max-width: 46%;
}
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none; display: inline-flex; gap: 6px; align-items: center;
  height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); font-size: 14px; font-weight: 600; color: var(--muted);
}
.chips button span { font-weight: 700; opacity: .7; }
.chips button.on { background: var(--text); color: var(--bg); border-color: transparent; }

/* ---------- Lista ---------- */
.list { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 10px; }
.card {
  width: 100%; display: grid; grid-template-columns: 56px 1fr auto; align-items: center; gap: 12px;
  padding: 12px; text-align: left; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow);
  transition: transform .12s;
}
.card:active { transform: scale(.985); }
.thumb {
  width: 56px; height: 56px; border-radius: 12px; overflow: hidden;
  display: grid; place-items: center; background: var(--surface-2); font-size: 26px;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.info { min-width: 0; }
.name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { height: 6px; border-radius: 99px; background: var(--surface-2); margin-top: 8px; overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: inherit; background: var(--ok); }
.s-soon .bar i { background: var(--warn); }
.s-expired .bar i { background: var(--bad); }
.left { text-align: right; min-width: 64px; }
.left b { display: block; font-size: 20px; line-height: 1.1; color: var(--ok); }
.left small { font-size: 12px; color: var(--muted); }
.s-soon .left b { color: var(--warn); }
.s-expired .left b { color: var(--bad); font-size: 14px; }
.s-expired { opacity: .72; }
.tag {
  display: inline-block; margin-top: 6px; padding: 2px 8px; border-radius: 99px;
  font-size: 11.5px; font-weight: 700; background: var(--info-soft); color: var(--info);
}

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .big { font-size: 46px; }
.empty h2 { color: var(--text); margin: 8px 0 6px; font-size: 20px; }
.empty p { margin: 0 auto 18px; max-width: 360px; }
.empty .btns { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

.backup-hint { font-size: 13px; color: var(--muted); text-align: center; margin: 18px 0 0; }
.backup-hint button { border: 0; background: none; color: var(--accent); font-weight: 700; padding: 0; text-decoration: underline; }

/* ---------- Botón flotante ---------- */
.fab {
  position: fixed; z-index: 6; left: 50%; transform: translateX(-50%);
  bottom: calc(18px + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 8px;
  height: 54px; padding: 0 22px; border-radius: 999px; border: 0;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 16px; white-space: nowrap;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 40%, transparent);
}
.fab:active { transform: translateX(-50%) scale(.96); }

/* ---------- Hojas (dialog) ---------- */
dialog.sheet {
  width: 100%; max-width: 580px; max-height: 94dvh;
  margin: auto auto 0; padding: 0; border: 0;
  border-radius: 22px 22px 0 0; background: var(--surface); color: var(--text);
  box-shadow: 0 -10px 40px rgb(0 0 0 / .2);
}
dialog.sheet[open] { display: flex; flex-direction: column; animation: up .22s ease-out; }
dialog.sheet > form { display: flex; flex-direction: column; min-height: 0; flex: 1; }
dialog::backdrop { background: rgb(8 14 11 / .5); }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@media (min-width: 640px) {
  dialog.sheet { margin: auto; border-radius: 22px; max-height: 88dvh; }
}
.sheet-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 14px 12px 8px 20px; border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-body { padding: 16px 20px 20px; overflow-y: auto; flex: 1; overscroll-behavior: contain; }
.sheet-foot {
  display: flex; gap: 8px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--surface);
}

/* ---------- Formulario ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; min-width: 0; }
.field-label { font-size: 13.5px; font-weight: 700; color: var(--muted); }
.field input, .field select, .field textarea, .suffix {
  width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); outline: none;
}
.field textarea { resize: vertical; line-height: 1.5; }
.field input:focus, .field select:focus, .field textarea:focus, .suffix:focus-within {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.field.invalid input { border-color: var(--bad); }
.error { color: var(--bad); font-size: 12.5px; min-height: 0; }
.error:empty { display: none; }
.help { font-size: 12.5px; color: var(--muted); }
.help:empty { display: none; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 420px) { .row2 { grid-template-columns: 1fr; gap: 0; } }
.suffix { display: flex; align-items: center; gap: 6px; padding: 0 12px; }
.suffix input { border: 0 !important; box-shadow: none !important; padding: 0; min-height: 44px; background: transparent; flex: 1; min-width: 0; }
.suffix span { color: var(--muted); font-size: 14px; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg button {
  height: 40px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); font-weight: 600; font-size: 14px;
}
.seg button.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.seg .suffix.compact { width: 130px; min-height: 40px; }
.seg .suffix.compact input { min-height: 38px; }

.photos { display: flex; gap: 10px; flex-wrap: wrap; }
.photos:empty { display: none; }
.ph {
  position: relative; width: 84px; height: 108px; border-radius: 12px; overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--line);
}
.ph img { width: 100%; height: 100%; object-fit: cover; }
.ph .pdf { display: grid; place-items: center; height: 100%; font-weight: 800; color: var(--bad); }
.ph button {
  position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 50%;
  border: 0; background: rgb(0 0 0 / .6); color: #fff; display: grid; place-items: center; padding: 0;
}
.ph button svg { width: 14px; height: 14px; }
.photo-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.photo-actions .btn { flex: 1; min-width: 150px; }

/* ---------- Ficha ---------- */
.gallery { display: flex; gap: 10px; overflow-x: auto; scroll-snap-type: x mandatory; margin: 0 -20px 16px; padding: 0 20px; }
.gallery button {
  flex: none; scroll-snap-align: start; width: 150px; height: 200px; padding: 0; border-radius: 14px; overflow: hidden;
  border: 1px solid var(--line); background: var(--surface-2);
}
.gallery img { width: 100%; height: 100%; object-fit: cover; }
.gallery .pdf { display: grid; place-items: center; height: 100%; font-weight: 800; color: var(--bad); }
.no-photo {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px; margin-bottom: 16px;
  border-radius: 12px; background: var(--warn-soft); font-size: 14px;
}
.hero { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
.ring { position: relative; width: 92px; height: 92px; flex: none; }
.ring svg { width: 92px; height: 92px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 9; }
.ring .track { stroke: var(--surface-2); }
.ring .val { stroke: var(--ok); stroke-linecap: round; transition: stroke-dashoffset .6s ease; }
.s-soon .ring .val { stroke: var(--warn); }
.s-expired .ring .val { stroke: var(--bad); }
.ring span { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; font-size: 12px; color: var(--muted); line-height: 1.1; }
.ring span b { display: block; font-size: 22px; color: var(--text); }
.hero h3 { margin: 0 0 4px; font-size: 19px; }
.pill { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: 13px; font-weight: 700; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.soon { background: var(--warn-soft); color: var(--warn); }
.pill.expired { background: var(--bad-soft); color: var(--bad); }
.pill.ret { background: var(--info-soft); color: var(--info); margin-top: 6px; }
.rows { margin: 0 0 18px; border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.rows div { display: flex; justify-content: space-between; gap: 16px; padding: 11px 14px; border-top: 1px solid var(--line); }
.rows div:first-child { border-top: 0; }
.rows dt { color: var(--muted); font-size: 14px; }
.rows dd { margin: 0; font-weight: 600; text-align: right; overflow-wrap: anywhere; }
.notes { white-space: pre-wrap; background: var(--surface-2); padding: 12px 14px; border-radius: 12px; margin: 0 0 18px; font-size: 14.5px; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn.wide { grid-column: 1 / -1; }

.claim-status { padding: 12px 14px; border-radius: 12px; margin-bottom: 14px; font-size: 14.5px; }
.claim-status.ok { background: var(--ok-soft); }
.claim-status.expired { background: var(--bad-soft); }

/* ---------- Ajustes ---------- */
.set { padding: 4px 0 18px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.set:last-child { border-bottom: 0; margin-bottom: 0; }
.set h3 { margin: 0 0 4px; font-size: 16px; }
.muted { color: var(--muted); font-size: 14px; margin: 0 0 12px; }
.set-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }

/* ---------- Visor ---------- */
dialog.viewer {
  width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; padding: 0; border: 0;
  background: #000;
}
dialog.viewer[open] { display: block; }
dialog.viewer::backdrop { background: #000; }
.viewer-track { display: flex; height: 100%; overflow-x: auto; scroll-snap-type: x mandatory; }
.viewer-track > * { flex: none; width: 100%; height: 100%; scroll-snap-align: center; display: grid; place-items: center; padding: 16px; }
.viewer-track img { max-width: 100%; max-height: 100%; object-fit: contain; background: #fff; }
.viewer-track a { color: #fff; font-weight: 700; }
.viewer-close { position: fixed; top: calc(12px + env(safe-area-inset-top)); right: 12px; background: rgb(255 255 255 / .15); color: #fff; }

/* ---------- Aviso flotante ---------- */
/* Usa popover para quedar por encima de las hojas abiertas */
.toast {
  position: fixed; z-index: 50; inset: auto auto calc(90px + env(safe-area-inset-bottom)) 50%; margin: 0; border: 0;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  padding: 12px 18px; border-radius: 12px; background: var(--text); color: var(--bg);
  font-weight: 600; font-size: 14.5px; transition: opacity .25s, transform .25s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
