:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8f9fd;
  --ink: #202335;
  --muted: #6f758c;
  --line: #e2e5ef;
  --primary: #6549ed;
  --primary-2: #7b65f4;
  --primary-soft: #eeeaff;
  --ok: #15855e;
  --ok-soft: #e9f8f1;
  --warn: #b7791f;
  --warn-soft: #fff7e8;
  --danger: #b54444;
  --shadow: 0 16px 42px rgba(36, 39, 63, .09);
  --radius: 18px;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body { margin: 0; min-height: 100vh; font: 15px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--ink); background: var(--bg); }
button, input, select { font: inherit; }
button, .button-link { border: 1px solid var(--line); border-radius: 11px; background: var(--surface); color: var(--ink); padding: .7rem .9rem; cursor: pointer; transition: .16s ease; text-decoration: none; }
button:hover, .button-link:hover { transform: translateY(-1px); border-color: #c9cde0; box-shadow: 0 7px 18px rgba(38, 41, 66, .08); }
button:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: none; }
button.primary, .button-link.primary { color: #fff; border-color: var(--primary); background: linear-gradient(135deg, var(--primary), var(--primary-2)); }
button.danger-subtle { color: var(--danger); border-color: #ebcaca; background: #fff8f8; }
button.danger-subtle:hover { border-color: #dfa9a9; background: #fff1f1; }
button.large { padding: .9rem 1.2rem; font-weight: 700; }
input, select { min-width: 0; border: 1px solid var(--line); border-radius: 11px; background: var(--surface); color: var(--ink); padding: .72rem .82rem; outline: none; }
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
progress { accent-color: var(--primary); }

.topbar { position: sticky; top: 0; z-index: 20; min-height: 70px; display: flex; align-items: center; gap: 18px; padding: 10px 22px; border-bottom: 1px solid var(--line); background: rgba(255,255,255,.92); backdrop-filter: blur(14px); }
.brand { display: flex; align-items: baseline; gap: 8px; color: var(--ink); text-decoration: none; white-space: nowrap; }
.brand-mark { font-weight: 900; letter-spacing: .03em; font-size: 1.28rem; color: var(--primary); }
.brand-sub { font-weight: 700; }
.connection-pill { display: flex; align-items: center; gap: 8px; min-width: 0; padding: .48rem .7rem; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #a9adbd; flex: 0 0 auto; }
.status-dot.ok { background: var(--ok); box-shadow: 0 0 0 4px var(--ok-soft); }
.status-dot.busy { background: var(--primary); box-shadow: 0 0 0 4px var(--primary-soft); }
.status-dot.warn { background: var(--warn); box-shadow: 0 0 0 4px var(--warn-soft); }
.status-dot.error { background: var(--danger); }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.root-name { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: .88rem; }

.compat-warning, .privacy-strip { margin: 14px auto 0; width: min(1480px, calc(100% - 28px)); border-radius: 14px; padding: 12px 16px; display: flex; gap: 10px; align-items: center; }
.compat-warning { background: var(--warn-soft); border: 1px solid #f0d69d; color: #75520f; }
.privacy-strip { background: var(--primary-soft); border: 1px solid #dad2ff; color: #4f3db6; }
.privacy-strip span, .compat-warning span { color: inherit; opacity: .82; }

.page-shell { width: min(1480px, calc(100% - 28px)); margin: 16px auto 110px; display: grid; grid-template-columns: minmax(0, 1fr) 390px; gap: 18px; align-items: start; }
.library-pane { min-width: 0; }
.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-bottom: 12px; }
.stats-grid article { border: 1px solid var(--line); border-radius: 15px; background: var(--surface); padding: 13px 15px; display: grid; grid-template-columns: 1fr auto; align-items: baseline; box-shadow: 0 8px 24px rgba(36,39,63,.035); }
.stats-grid span { color: var(--muted); font-weight: 650; }
.stats-grid strong { font-size: 1.5rem; }
.stats-grid small { grid-column: 1 / -1; color: #979bad; }
.scan-line { min-height: 20px; display: flex; gap: 10px; align-items: center; color: var(--muted); font-size: .82rem; }
.scan-line progress { width: 180px; }
.filters { display: grid; grid-template-columns: minmax(240px, 1.5fr) repeat(4, minmax(130px, .55fr)) auto auto; gap: 8px; align-items: center; padding: 10px; border: 1px solid var(--line); border-radius: 16px; background: var(--surface); margin-bottom: 14px; box-shadow: 0 8px 24px rgba(36,39,63,.035); }
.check { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); white-space: nowrap; }
.check input { width: auto; }
.result-count { color: var(--muted); font-size: .88rem; white-space: nowrap; text-align: right; }

.story-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(175px, 1fr)); gap: 13px; }
.story-card { overflow: hidden; min-width: 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); cursor: pointer; transition: .16s ease; box-shadow: 0 8px 26px rgba(36,39,63,.045); }
.story-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); border-color: #cbcfe0; }
.story-card.selected { outline: 3px solid rgba(101,73,237,.2); border-color: var(--primary); }
.cover { display: block; width: 100%; aspect-ratio: 1 / 1; object-fit: contain; background: #fff; }
.cover-placeholder { display: grid; place-items: center; background: linear-gradient(135deg, #ece8ff, #f8f7ff); color: var(--primary); font-weight: 900; letter-spacing: .04em; }
.story-card-body { padding: 11px 12px 13px; }
.card-code { display: flex; align-items: center; justify-content: space-between; gap: 6px; color: var(--muted); font-size: .77rem; font-weight: 700; }
.availability { padding: .18rem .42rem; border-radius: 99px; font-size: .68rem; }
.availability.installed { color: var(--ok); background: var(--ok-soft); }
.availability.missing { background: #f1f2f7; color: #888d9f; }
.story-card h3 { margin: 8px 0 4px; font-size: .98rem; line-height: 1.28; min-height: 2.5em; }
.story-card p { margin: 0; min-height: 2.6em; color: var(--muted); font-size: .8rem; }
.card-flags { min-height: 1.2rem; margin-top: 7px; color: var(--primary); font-size: .78rem; font-weight: 700; }
.empty-state { grid-column: 1 / -1; min-height: 240px; display: grid; place-items: center; align-content: center; gap: 5px; color: var(--muted); border: 1px dashed #cfd3e2; border-radius: var(--radius); background: rgba(255,255,255,.5); }
.empty-state strong { color: var(--ink); font-size: 1.1rem; }

.detail-panel { position: static; align-self: start; min-height: 570px; border: 1px solid var(--line); border-radius: 20px; background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }
.detail-empty { min-height: 570px; display: grid; place-items: center; align-content: center; text-align: center; padding: 30px; color: var(--muted); }
.detail-empty span { width: 82px; height: 82px; border-radius: 24px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); font-weight: 900; font-size: 1.25rem; }
.detail-empty h2 { margin: 18px 0 0; color: var(--ink); }
.detail-empty p { max-width: 280px; }
.detail-card { padding-bottom: 18px; }
.detail-cover-wrap { width: 100%; background: #fff; overflow: hidden; }
.detail-cover { display: block; width: 100%; height: auto; object-fit: contain; background: #fff; }
.detail-heading, .detail-badges, .detail-actions, .meta-list, .track-list, .local-note { margin-left: 18px; margin-right: 18px; }
.detail-heading { margin-top: 16px; }
.eyebrow { color: var(--primary); font-size: .73rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; }
.detail-heading h2 { margin: 4px 0 8px; font-size: 1.4rem; line-height: 1.18; }
.detail-heading p { margin: 0; color: var(--muted); font-size: .88rem; }
.detail-badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.detail-badges span { padding: .3rem .55rem; border-radius: 99px; background: var(--surface-2); color: var(--muted); font-size: .75rem; }
.detail-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 14px; }
.detail-actions .active { background: var(--primary-soft); border-color: #d3c9ff; color: var(--primary); }
.detail-actions .download-link { font-weight: 700; }
.meta-list { margin-top: 15px; border-top: 1px solid var(--line); }
.meta-list div { display: grid; grid-template-columns: 85px 1fr; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.meta-list dt { color: var(--muted); }
.meta-list dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
.track-list { margin-top: 16px; }
.track-list h3 { margin: 0 0 8px; font-size: .96rem; }
.track-row { width: 100%; display: flex; justify-content: space-between; gap: 8px; border-radius: 9px; padding: .5rem .6rem; margin-bottom: 5px; text-align: left; }
.track-row small { color: var(--muted); }
.track-row.active { border-color: var(--primary); background: var(--primary-soft); }
.local-note { margin-top: 15px; padding: 12px; border-radius: 12px; background: var(--surface-2); color: var(--muted); }

.player-bar { position: fixed; z-index: 30; left: 50%; bottom: 14px; transform: translateX(-50%); width: min(1000px, calc(100% - 24px)); display: grid; grid-template-columns: minmax(180px, 1fr) 110px minmax(260px, 1.5fr) auto; align-items: center; gap: 12px; padding: 10px 12px; border: 1px solid #d4d7e4; border-radius: 17px; background: rgba(255,255,255,.96); box-shadow: 0 18px 60px rgba(25,28,48,.22); backdrop-filter: blur(18px); }
.player-copy { min-width: 0; display: flex; flex-direction: column; }
.player-copy strong, .player-copy span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-copy span { color: var(--muted); font-size: .8rem; }
.player-bar audio { width: 100%; height: 40px; }

.converter-dialog { width: min(1040px, calc(100% - 30px)); max-width: none; border: 0; border-radius: 22px; padding: 0; background: var(--surface); box-shadow: 0 28px 100px rgba(26,28,48,.28); }
.converter-dialog::backdrop { background: rgba(24,26,43,.55); backdrop-filter: blur(4px); }
.dialog-card { padding: 22px; }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; }
.dialog-head h2 { margin: 4px 0; }
.dialog-head p { margin: 0; color: var(--muted); }
.converter-grid { display: grid; grid-template-columns: 1.15fr .9fr .95fr; gap: 12px; margin-top: 20px; }
.converter-step { position: relative; min-height: 300px; padding: 18px; border: 1px solid var(--line); border-radius: 16px; background: var(--surface-2); display: flex; flex-direction: column; gap: 11px; }
.converter-step h3 { margin: 2px 0 4px; }
.step-number { width: 28px; height: 28px; display: grid; place-items: center; border-radius: 50%; background: var(--primary); color: #fff; font-weight: 800; }
.hint { color: var(--muted); font-size: .84rem; }
.source-tracks { margin: 0; padding: 0; list-style: none; max-height: 170px; overflow: auto; border-top: 1px solid var(--line); }
.source-tracks li { display: flex; justify-content: space-between; gap: 10px; padding: 6px 2px; border-bottom: 1px solid var(--line); font-size: .82rem; }
.source-tracks small { color: var(--muted); white-space: nowrap; }
.converter-step label:not(.check) { display: grid; gap: 6px; color: var(--muted); font-weight: 650; }
.input-button { display: grid; grid-template-columns: auto 1fr auto; gap: 6px; align-items: center; }
.k-prefix { font-weight: 900; color: var(--primary); }
.danger-check { white-space: normal; color: var(--danger); margin-top: 8px; }
.converter-run progress { width: 100%; }
.conversion-status { font-size: .84rem; color: var(--muted); overflow-wrap: anywhere; }
.safety-note { margin-top: 12px; padding: 12px 14px; background: var(--warn-soft); border: 1px solid #f0d69d; border-radius: 12px; color: #745111; }
.safety-note code { background: rgba(255,255,255,.7); padding: .1rem .3rem; border-radius: 5px; }

.toast { position: fixed; z-index: 50; right: 18px; bottom: 20px; max-width: min(420px, calc(100% - 36px)); padding: 12px 15px; border-radius: 12px; background: #2d3042; color: #fff; opacity: 0; transform: translateY(10px); pointer-events: none; transition: .2s ease; box-shadow: var(--shadow); }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.ok { background: #126c4f; }
.toast.warn { background: #8c651b; }
.toast.error { background: #963a3a; }

@media (max-width: 1120px) {
  .topbar { flex-wrap: wrap; }
  .connection-pill { order: 3; width: 100%; }
  .top-actions { flex: 1; justify-content: flex-end; }
  .page-shell { grid-template-columns: 1fr; }
  .detail-panel { position: static; min-height: 0; }
  .filters { grid-template-columns: repeat(3, 1fr); }
  .filters #searchInput { grid-column: span 2; }
  .converter-grid { grid-template-columns: 1fr; }
  .converter-step { min-height: 0; }
}

@media (max-width: 720px) {
  .topbar { padding: 10px 12px; gap: 9px; }
  .brand { flex: 1; }
  .top-actions { width: 100%; display: grid; grid-template-columns: 1fr 1fr; }
  .top-actions .root-name { grid-column: 1 / -1; max-width: none; }
  .privacy-strip, .compat-warning { align-items: flex-start; flex-direction: column; }
  .page-shell { width: min(100% - 18px, 1480px); margin-top: 10px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters #searchInput { grid-column: 1 / -1; }
  .result-count { text-align: left; }
  .story-grid { grid-template-columns: repeat(2, minmax(0,1fr)); gap: 9px; }
  .story-card h3 { font-size: .9rem; }
  .player-bar { grid-template-columns: 1fr auto; }
  .player-bar progress, .player-bar audio { grid-column: 1 / -1; }
  .dialog-card { padding: 15px; }
  .dialog-head { flex-direction: column; }
}

@media (max-width: 420px) {
  .story-grid { grid-template-columns: 1fr; }
  .stats-grid { gap: 7px; }
  .stats-grid article { padding: 10px; }
}

/* Selector de vista y modo lista */
.catalog-toolbar { min-height: 38px; margin: -4px 0 10px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.catalog-toolbar .result-count { text-align: left; }
.view-switch { display: inline-flex; align-items: center; gap: 3px; padding: 3px; border: 1px solid var(--line); border-radius: 11px; background: var(--surface-2); }
.view-button { display: inline-flex; align-items: center; justify-content: center; gap: 5px; min-height: 32px; padding: .38rem .55rem; border: 0; border-radius: 8px; background: transparent; color: var(--muted); box-shadow: none; }
.view-button:hover { background: #fff; color: var(--ink); transform: none; }
.view-button[aria-pressed="true"] { background: #fff; color: var(--primary); box-shadow: 0 1px 4px rgba(36,39,63,.12); }
.view-button span[aria-hidden="true"] { font-size: 1.05rem; line-height: 1; }
.view-label { font-size: .76rem; font-weight: 750; }

.story-grid.list-view { display: flex; flex-direction: column; gap: 8px; }
.story-grid.list-view .story-card { display: grid; grid-template-columns: 86px minmax(0, 1fr); min-height: 86px; border-radius: 14px; }
.story-grid.list-view .story-card:hover { transform: translateX(2px); }
.story-grid.list-view .cover,
.story-grid.list-view .cover-placeholder { width: 86px; height: 86px; aspect-ratio: auto; object-fit: contain; border-right: 1px solid var(--line); background: #fff; }
.story-grid.list-view .story-card-body { min-width: 0; padding: 9px 12px; display: flex; flex-direction: column; justify-content: center; }
.story-grid.list-view .story-card h3 { margin: 4px 0 3px; min-height: 0; font-size: .96rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.story-grid.list-view .story-card p { min-height: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.story-grid.list-view .card-flags { min-height: 0; margin-top: 4px; }
.story-grid.list-view .empty-state { width: 100%; }

@media (max-width: 720px) {
  .catalog-toolbar { margin-top: -2px; }
  .view-switch { justify-self: start; }
  .view-label { display: none; }
  .story-grid.list-view .story-card { grid-template-columns: 72px minmax(0, 1fr); min-height: 72px; }
  .story-grid.list-view .cover,
  .story-grid.list-view .cover-placeholder { width: 72px; height: 72px; }
  .story-grid.list-view .story-card-body { padding: 7px 9px; }
}

/* Ficha modal para móvil */
.detail-dialog {
  width: min(620px, calc(100% - 20px));
  max-width: none;
  max-height: calc(100dvh - 20px);
  padding: 0;
  border: 0;
  border-radius: 22px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 28px 100px rgba(26,28,48,.32);
  overflow: hidden;
}
.detail-dialog::backdrop { background: rgba(24,26,43,.62); backdrop-filter: blur(4px); }
.detail-dialog-shell { display: flex; flex-direction: column; max-height: calc(100dvh - 20px); }
.detail-dialog-head {
  position: sticky;
  top: 0;
  z-index: 2;
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px 10px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.96);
  backdrop-filter: blur(16px);
}
.detail-dialog-head > div { min-width: 0; display: flex; flex-direction: column; }
.detail-dialog-head strong { max-width: min(66vw, 440px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.detail-dialog-close { flex: 0 0 auto; }
.detail-dialog-content { overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.detail-dialog-content .detail-cover { width: 100%; height: auto; object-fit: contain; }
.detail-dialog-content .detail-card { padding-bottom: max(22px, env(safe-area-inset-bottom)); }

@media (min-width: 721px) {
  .detail-dialog { display: none; }
}

@media (max-width: 720px) {
  .detail-panel { display: none; }
  .detail-dialog {
    width: 100%;
    max-height: 94dvh;
    margin: auto 0 0;
    border-radius: 22px 22px 0 0;
  }
  .detail-dialog-shell { max-height: 94dvh; }
  .detail-dialog-head { padding-top: max(10px, env(safe-area-inset-top)); }
  .detail-dialog-content .detail-cover { width: 100%; height: auto; object-fit: contain; background: #fff; }
  .detail-dialog-content .detail-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .detail-dialog-content .detail-actions .button-link { display: inline-flex; align-items: center; justify-content: center; text-align: center; }
  .detail-dialog-content .meta-list div { grid-template-columns: 78px 1fr; }
}

@media (max-width: 420px) {
  .detail-dialog-content .detail-actions { grid-template-columns: 1fr; }
}
