* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-height: 100%; }
body { font-family: system-ui, -apple-system, sans-serif; background: #111; color: #ddd; }

/* ============ LAYOUT PRINCIPAL ============ */
#layout { display: grid; grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; min-height: 100vh; }
header { grid-column: 1; padding: 10px 16px; background: #222; border-bottom: 1px solid #333;
         display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
header h1 { font-size: 17px; color: #fff; }
.search { display: flex; gap: 6px; flex: 1; min-width: 200px; max-width: 500px; }
.search input { flex: 1; padding: 8px 10px; background: #333; color: #fff; border: 1px solid #444; border-radius: 4px; font-size: 15px; }
.search button { padding: 8px 14px; background: #4a7; color: #fff; border: 0; border-radius: 4px; cursor: pointer; }
.stat { font-size: 12px; color: #888; }

.tabs { grid-column: 1; background: #1a1a1a; padding: 8px 16px; display: flex; gap: 6px; overflow-x: auto; border-bottom: 1px solid #333; -webkit-overflow-scrolling: touch; }
.tabs a { color: #bbb; text-decoration: none; padding: 8px 12px; border-radius: 20px; font-size: 13px; white-space: nowrap; background: #222; }
.tabs a.active { background: #4a7; color: #fff; }
.tabs a span { color: #888; font-size: 11px; margin-left: 4px; }
.tabs a.active span { color: #cfe; }

aside { background: #1a1a1a; overflow-y: auto; padding: 10px 0; max-height: calc(100vh - 108px); }
aside a { display: block; padding: 7px 14px; color: #bbb; text-decoration: none; font-size: 13px; }
aside a:hover, aside a.active { background: #2a2a2a; color: #fff; }
aside a span { color: #666; float: right; font-size: 11px; }

main { padding: 14px; overflow-y: auto; max-height: calc(100vh - 108px); }

/* ============ GRID DE PORTADAS ============ */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.card { position: relative; background: #1a1a1a; border-radius: 6px; overflow: hidden;
        text-decoration: none; color: #ddd; display: flex; flex-direction: column;
        transition: transform .1s; -webkit-tap-highlight-color: transparent; }
.card:hover, .card:active { transform: translateY(-2px); background: #222; }
.card img { width: 100%; aspect-ratio: 3/4; object-fit: cover; background: #333; display: block; }
.card .t { padding: 5px 7px; font-size: 11px; line-height: 1.25; height: 28px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; color: #bbb; }
.badge { position: absolute; top: 6px; right: 6px; background: rgba(0,0,0,0.75); color: #fff; padding: 2px 6px; border-radius: 3px; font-size: 10px; text-transform: uppercase; font-weight: bold; }
.tipo-pdf .badge { background: rgba(200,60,60,0.85); }
.tipo-epub .badge { background: rgba(110,70,180,0.85); }
.tipo-audio .badge { background: rgba(60,170,100,0.85); }
.tipo-video .badge { background: rgba(200,130,40,0.85); }
.tipo-imagen .badge { background: rgba(60,150,200,0.85); }

.pager { margin: 20px 0; text-align: center; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.pager a { color: #4a7; text-decoration: none; padding: 8px 14px; background: #1a1a1a; border-radius: 4px; }
.pager span { color: #888; padding: 8px; }

/* ============ DETALLE LIBRO ============ */
.book-detail { display: grid; grid-template-columns: 260px 1fr; gap: 24px; padding: 20px; }
.book-detail img { width: 100%; border-radius: 8px; box-shadow: 0 4px 20px #0008; }
.book-detail h1 { color: #fff; margin-bottom: 12px; font-size: 22px; }
.book-detail p { margin: 4px 0; color: #aaa; }
.book-detail code { background: #333; padding: 2px 6px; border-radius: 3px; font-size: 14px; }
.back { color: #4a7; text-decoration: none; padding: 10px 16px; display: inline-block; font-size: 15px; }
.preview-btn { display: inline-block; background: #c73; color: #fff; padding: 12px 22px;
               border-radius: 6px; text-decoration: none; margin: 14px 0; font-weight: bold; font-size: 15px; }
.preview-btn:hover { background: #e85; }

#frm-solicitar { margin-top: 24px; padding: 16px; background: #1a1a1a; border-radius: 8px; }
#frm-solicitar h3 { color: #fff; margin-bottom: 8px; }
#frm-solicitar input, #frm-solicitar select {
  display: block; width: 100%; margin: 8px 0; padding: 12px; background: #222; color: #fff;
  border: 1px solid #333; border-radius: 4px; font-size: 15px; }
#frm-solicitar button { padding: 12px 22px; background: #4a7; color: #fff; border: 0; border-radius: 4px; cursor: pointer; font-size: 15px; font-weight: bold; }
#resp { margin-top: 10px; color: #4a7; font-size: 14px; }

/* ============ PREVIEW ============ */
.preview-wrap { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.preview-bar { background: #222; padding: 10px 14px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #333; gap: 10px; flex-shrink: 0; }
.preview-bar h2 { color: #fff; font-size: 14px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.timer { background: #4a7; color: #fff; padding: 6px 12px; border-radius: 4px; font-weight: bold; font-family: monospace; font-size: 15px; flex-shrink: 0; }
.timer.warn { background: #c73; }
.timer.done { background: #c33; }
.close-btn { color: #fff; background: #333; border: 0; padding: 8px 12px; border-radius: 4px; text-decoration: none; font-size: 13px; flex-shrink: 0; }

.viewer { flex: 1; background: #111; display: flex; flex-direction: column; align-items: center; overflow: auto; padding: 8px; -webkit-overflow-scrolling: touch; }
.viewer > video, .viewer > audio, .viewer > img:not(.pdf-page) { max-width: 100%; background: #111; }
.viewer audio { width: 90%; margin-top: 40px; }

.pdf-nav { position: sticky; top: 0; z-index: 5; background: #1a1a1acc; backdrop-filter: blur(6px); padding: 8px; display: flex; gap: 8px; align-items: center; border-radius: 0 0 6px 6px; width: 100%; max-width: 900px; justify-content: center; }
.pdf-nav button { background: #4a7; color: #fff; border: 0; padding: 10px 16px; border-radius: 4px; cursor: pointer; font-weight: bold; font-size: 14px; touch-action: manipulation; }
.pdf-nav button:disabled { background: #333; cursor: not-allowed; opacity: 0.5; }
.pdf-nav span { color: #ddd; font-family: monospace; font-size: 13px; }
.pdf-page { width: 100%; max-width: 900px; margin-top: 8px; box-shadow: 0 4px 20px #0008; background: #fff; height: auto; }

.expired { text-align: center; padding: 30px 20px; color: #ddd; max-width: 500px; margin: 0 auto; }
.expired h2 { color: #4a7; margin-bottom: 12px; font-size: 20px; }
.expired .cod { background: #222; padding: 8px 14px; border-radius: 4px; font-family: monospace; color: #fff; display: inline-block; margin: 10px 0; font-size: 13px; word-break: break-all; }
.wa-btn { display: inline-block; background: #25D366; color: #fff; text-decoration: none; padding: 14px 26px; border-radius: 6px; font-weight: bold; margin-top: 18px; font-size: 16px; }
.spinner { border: 4px solid #333; border-top: 4px solid #4a7; border-radius: 50%; width: 40px; height: 40px; animation: spin 1s linear infinite; margin: 20px auto; }
@keyframes spin { 0%{transform:rotate(0)} 100%{transform:rotate(360deg)} }

/* ============ RESPONSIVE ============ */
@media (max-width: 900px) {
  #layout { grid-template-columns: 1fr; grid-template-rows: auto auto auto 1fr; }
  header { padding: 10px 14px; }
  header h1 { font-size: 18px; flex: 1 1 100%; }
  .search { min-width: 100%; order: 3; margin-top: 6px; }
  .search input { font-size: 16px; padding: 10px 12px; }
  .search button { padding: 10px 18px; font-size: 15px; }
  .stat { order: 2; width: 100%; text-align: left; font-size: 13px; }
  .tabs { padding: 8px 14px; gap: 8px; }
  .tabs a { padding: 10px 14px; font-size: 14px; }
  aside { max-height: none; padding: 10px 14px; display: flex; flex-wrap: wrap; gap: 6px; border-bottom: 1px solid #333; overflow-y: visible; }
  aside a { padding: 7px 12px; border-radius: 4px; background: #222; font-size: 13px; }
  aside a span { display: none; }
  main { max-height: none; padding: 12px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
  
  .book-detail { grid-template-columns: 1fr; padding: 14px; gap: 16px; }
  .book-detail img { max-width: 220px; margin: 0 auto; display: block; }
  .preview-bar h2 { font-size: 13px; }
  .timer { font-size: 14px; padding: 6px 12px; }
  .close-btn { padding: 8px 12px; font-size: 13px; }
  .pdf-nav button { padding: 14px 16px; font-size: 14px; }
}
@media (max-width: 520px) {
  .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  
  .badge { font-size: 10px; padding: 2px 5px; }
  header h1 { font-size: 17px; }
}
@media (max-width: 360px) {
  .grid { grid-template-columns: repeat(2, 1fr); gap: 6px; }
  
}

details#cats-details { padding: 8px 14px; }
details#cats-details summary { cursor: pointer; padding: 10px 14px; background: #2a2a2a; color: #fff; border-radius: 6px; font-size: 14px; list-style: none; user-select: none; }
details#cats-details summary::-webkit-details-marker { display: none; }
details#cats-details summary::before { content: "BE  "; margin-right: 6px; }
details#cats-details[open] summary::before { content: "B4  "; }
.cats-list { max-height: 45vh; overflow-y: auto; margin-top: 8px; padding: 4px; background: #151515; border-radius: 6px; }
.cats-list a { display: block; padding: 8px 12px; color: #bbb; text-decoration: none; font-size: 13px; border-radius: 4px; }
.cats-list a:hover, .cats-list a.active { background: #2a2a2a; color: #fff; }
.cats-list a span { color: #666; font-size: 11px; float: right; }
.refresh-btn { background: #4a7; color: #fff; border: 0; width: 38px; height: 38px; border-radius: 50%; cursor: pointer; font-size: 18px; font-weight: bold; }
.refresh-btn:hover { background: #5b8; }
.refresh-btn:disabled { background: #666; cursor: wait; }
.refresh-btn.spin { animation: spin 1.5s linear infinite; }
#refresh-msg { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #222; color: #fff; padding: 12px 20px; border-radius: 6px; box-shadow: 0 4px 20px #000a; z-index: 100; display: none; }
