:root {
  color-scheme: light dark;

  --bg: #F2F3F9;
  --surface: #FFFFFF;
  --ink: #171A3A;
  --muted: #5B6087;
  --line: #DEE1F0;
  --accent: #3B3FF0;
  --on-accent: #FFFFFF;
  --head: #3B3FF0;
  --on-head: #FFFFFF;
  --hi: #FFD23F;
  --on-hi: #171A3A;
  --star: #E5A400;
  --danger: #C62839;
  --focus: #171A3A;

  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Noto Serif", ui-serif, Georgia, "Times New Roman", serif;
  --max: 720px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0D1020;
    --surface: #161A33;
    --ink: #ECEEFF;
    --muted: #9BA1CB;
    --line: #282D52;
    --accent: #8B90FF;
    --on-accent: #0D1020;
    --head: #23276E;
    --star: #FFD23F;
    --danger: #FF8A96;
    --focus: #FFFFFF;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 var(--sans);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

svg { display: block; }

/* ---------- Cabecera ---------- */
.top {
  background: var(--head);
  color: var(--on-head);
  padding: calc(env(safe-area-inset-top, 0px) + 10px) max(16px, env(safe-area-inset-right, 0px)) 16px max(16px, env(safe-area-inset-left, 0px));
}
.top-row, .search { max-width: var(--max); margin-inline: auto; }
.top-row { display: flex; align-items: center; justify-content: space-between; min-height: 48px; }

h1 {
  margin: 0;
  font: 700 28px/1.1 var(--serif);
  letter-spacing: -0.01em;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
}
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn:active { background: color-mix(in srgb, currentColor 14%, transparent); }
.on-head { margin-right: -8px; }

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  padding: 0 4px 0 14px;
  height: 48px;
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
}
.search > svg { width: 20px; height: 20px; flex: none; color: var(--muted); }
.search input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  background: transparent;
  outline: none;
  font-size: 16px;
}
.search input::placeholder { color: var(--muted); opacity: 1; }
.search input::-webkit-search-cancel-button { display: none; }
.search:focus-within { outline: 3px solid var(--hi); outline-offset: 2px; }
.clear {
  display: grid; place-items: center;
  width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: transparent; color: var(--muted);
}
.clear svg { width: 18px; height: 18px; }

/* ---------- Filtros ---------- */
.chips-bar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.chips {
  display: flex;
  gap: 8px;
  max-width: var(--max);
  margin-inline: auto;
  padding: 10px max(16px, env(safe-area-inset-right, 0px)) 10px max(16px, env(safe-area-inset-left, 0px));
  overflow-x: auto;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--ink);
  white-space: nowrap;
}
.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

/* ---------- Lista ---------- */
main { max-width: var(--max); margin-inline: auto; padding-bottom: calc(110px + env(safe-area-inset-bottom, 0px)); }

.count {
  margin: 0;
  padding: 14px 16px 8px;
  color: var(--muted);
  font-size: 14px;
}

.list {
  margin: 0;
  padding: 0;
  list-style: none;
  background: var(--surface);
  border-block: 1px solid var(--line);
}
.list li { display: flex; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }

.row {
  flex: 1;
  min-width: 0;
  display: block;
  padding: 14px 4px 14px 16px;
  border: 0;
  background: transparent;
  text-align: left;
}
.row:active { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.row .t { display: block; font-weight: 600; font-size: 16px; }
.row .s {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-top: 2px;
  font: 14px/1.5 var(--serif);
  color: var(--muted);
}
.row .m {
  display: flex; align-items: center; gap: 6px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--muted);
}
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }

.star svg { fill: none; stroke: var(--muted); }
.star[aria-pressed="true"] svg { fill: var(--star); stroke: var(--star); }
.list li .star { align-self: flex-start; margin: 6px 6px 0 0; }

.empty {
  max-width: 420px;
  margin: 40px auto;
  padding: 0 24px;
  text-align: center;
  color: var(--muted);
}
.empty strong { display: block; margin-bottom: 6px; color: var(--ink); font: 700 20px/1.3 var(--serif); }
.empty .btn { margin-top: 16px; }

/* ---------- Botón flotante ---------- */
.fab {
  position: fixed;
  right: max(16px, env(safe-area-inset-right, 0px));
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  z-index: 6;
  display: grid; place-items: center;
  width: 58px; height: 58px;
  border: 0; border-radius: 18px;
  background: var(--accent);
  color: var(--on-accent);
  box-shadow: 0 6px 18px rgba(23, 26, 58, .28);
}
.fab svg { width: 28px; height: 28px; }
.fab:active { transform: scale(.96); }

/* ---------- Botones ---------- */
.btn {
  min-height: 48px;
  padding: 0 20px;
  border: 1.5px solid transparent;
  border-radius: 14px;
  font-weight: 600;
}
.btn.primary { flex: 1; background: var(--accent); color: var(--on-accent); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn.danger { background: transparent; border-color: var(--line); color: var(--danger); }
.btn:active { filter: brightness(.94); }

/* ---------- Hojas (diálogos) ---------- */
dialog.sheet {
  width: 100%;
  max-width: var(--max);
  max-height: 92dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 22px 22px 0 0;
  background: var(--surface);
  color: var(--ink);
  overflow: hidden;
}
dialog.sheet::backdrop { background: rgba(10, 12, 30, .58); }
dialog.sheet[open] { display: flex; flex-direction: column; animation: up .18s ease-out; }
dialog.sheet form { display: flex; flex-direction: column; min-height: 0; flex: 1; }

@keyframes up { from { transform: translateY(24px); opacity: .4; } to { transform: none; opacity: 1; } }

.sheet-head {
  display: flex; align-items: center; gap: 4px;
  padding: 14px 8px 10px 20px;
  border-bottom: 1px solid var(--line);
}
.sheet-titles { flex: 1; min-width: 0; }
.sheet-head h2 { margin: 0; font: 700 20px/1.25 var(--serif); overflow-wrap: anywhere; }
.cat { margin: 2px 0 0; font-size: 14px; color: var(--muted); }

.sheet-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 20px;
}
.sheet-foot {
  display: flex; gap: 10px;
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
  background: var(--surface);
}

/* Campos del prompt */
.vars { display: grid; gap: 12px; margin-bottom: 16px; }
.vars:empty { display: none; }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; margin-bottom: 6px; font-size: 14px; font-weight: 600; }
.field input, .field textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  font-size: 16px;
  line-height: 1.45;
}
.field textarea { resize: vertical; min-height: 84px; }
.vars .field { margin: 0; }
.vars textarea { min-height: 52px; max-height: 40dvh; }
.field input:focus, .field textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 28%, transparent); }
#fText { font-family: var(--serif); min-height: 180px; }

.prompt-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  padding: 16px;
  border-radius: 14px;
  background: var(--bg);
  font: 16px/1.65 var(--serif);
}
.prompt-text mark { padding: 1px 4px; border-radius: 5px; background: var(--hi); color: var(--on-hi); }
.prompt-text mark.empty-var { background: transparent; color: var(--accent); box-shadow: inset 0 -2px 0 var(--hi); padding: 0; border-radius: 0; font-weight: 600; }

.hint { margin: 4px 0 0; font-size: 14px; color: var(--muted); }
.error { margin: 10px 0 0; color: var(--danger); font-weight: 600; }

.menu { display: grid; gap: 4px; align-content: start; }
.menu-item {
  display: block; width: 100%;
  padding: 14px 4px;
  border: 0; border-bottom: 1px solid var(--line);
  background: transparent; text-align: left;
}
.menu-item strong { display: block; font-weight: 600; }
.menu-item span { display: block; font-size: 14px; color: var(--muted); }
.menu .hint { margin-top: 14px; }

/* ---------- Aviso ---------- */
.toast {
  position: fixed;
  inset: auto 0 calc(88px + env(safe-area-inset-bottom, 0px)) 0;
  margin: 0 auto;
  width: fit-content;
  max-width: calc(100% - 32px);
  padding: 12px 16px;
  border: 0;
  border-radius: 14px;
  background: var(--ink);
  color: var(--bg);
  font-size: 15px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .3);
  overflow: visible;
}
.toast:not(:popover-open) { display: none; }
.no-popover .toast { display: none; }
.no-popover .toast.on { display: block; z-index: 50; }
.toast button {
  margin-left: 14px; padding: 4px 6px;
  border: 0; background: transparent;
  color: var(--hi); font-weight: 700;
}
@media (prefers-color-scheme: dark) { .toast button { color: #3B3FF0; } }

@media (min-width: 720px) {
  dialog.sheet { margin: auto; border-radius: 22px; max-height: 86dvh; }
}

@media (prefers-reduced-motion: reduce) {
  dialog.sheet[open] { animation: none; }
  .fab:active { transform: none; }
}
