:root {
  --bg: #030307;
  --surface: #1a1a2e;
  --surface-elevated: #25253e;
  --accent: #a78bfa;
  --accent-dim: rgba(167, 139, 250, 0.15);
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
  --border: #2a2a4a;
  --danger: #f87171;
  --warning: #eab308;
  --tab-height: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 8px);
  --safe-top: env(safe-area-inset-top, 0px);
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-family: system-ui, -apple-system, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  background: var(--bg);
  color: var(--text);
  height: 100%;
}

body {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

input, button, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  touch-action: manipulation;
}

.tab-btn, .manga-card, .chapter-row, .load-more-btn,
#sheet-leer-btn, #sheet-lib-btn, .source-chip, .settings-btn,
.continue-card, .settings-switch {
  touch-action: manipulation;
}

::-webkit-scrollbar { width: 0; height: 0; }
* { scrollbar-width: none; -ms-overflow-style: none; }

h1, h2, h3 {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-weight: 600;
}

.mono {
  font-family: 'JetBrains Mono', 'SF Mono', monospace;
}

#tab-content {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding-top: calc(var(--safe-top) + 8px);
  padding-bottom: calc(var(--tab-height) + var(--safe-bottom) + 8px);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; min-height: 100%; }

#explorar-panel { padding: 16px 16px 0; }
#biblioteca-panel { padding: 16px 16px 0; }
#ajustes-panel { padding: 16px; }

/* ────────────────────── Tab Bar ────────────────────── */

#tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--tab-height);
  padding-bottom: var(--safe-bottom);
  display: flex;
  justify-content: space-around;
  align-items: center;
  background: rgba(3, 3, 7, 0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid rgba(167, 139, 250, 0.1);
  z-index: 100;
}

.tab-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 20px;
  color: var(--text-secondary);
  transition: color 0.2s;
  font-size: 0.7em;
  font-weight: 500;
}

.tab-btn.active { color: var(--accent); }
.tab-btn svg { width: 24px; height: 24px; }

/* ────────────────────── Search ────────────────────── */

.search-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.search-input {
  flex: 1;
  background: var(--surface);
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 0.95em;
  outline: none;
  transition: border-color 0.2s;
}

.search-input:focus { border-color: var(--accent); }
.search-input::placeholder { color: var(--text-secondary); }

.source-toggle {
  display: flex;
  background: var(--surface);
  border-radius: 12px;
  overflow: hidden;
  flex-shrink: 0;
}

.source-chip {
  padding: 8px 14px;
  font-size: 0.75em;
  font-weight: 600;
  color: var(--text-secondary);
  transition: all 0.2s;
}

.source-chip.active {
  background: var(--accent);
  color: var(--bg);
}

.source-chip.local-disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

/* ────────────────────── Grid ────────────────────── */

.manga-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.manga-card {
  position: relative;
  cursor: pointer;
  transition: transform 0.15s;
}

.manga-card:active { transform: scale(0.97); }

.manga-card .poster {
  width: 100%;
  aspect-ratio: 70/98;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  background: var(--surface);
  display: block;
}

.manga-card .poster-placeholder {
  width: 100%;
  aspect-ratio: 70/98;
  border-radius: 8px;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}

.manga-card .title {
  font-size: 0.85em;
  line-height: 1.3;
  margin-top: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.manga-card .badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: var(--accent);
  color: var(--bg);
  font-size: 0.65em;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
}

.manga-card .badge-sub {
  background: var(--surface-elevated);
  color: var(--text);
  font-weight: 500;
}

/* ────────────────────── Continue Reading ────────────────────── */

.continue-section {
  margin-bottom: 16px;
}

.continue-section h3,
.section-title {
  font-size: 0.85em;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 10px;
}

.continue-row {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 140px;
  gap: 10px;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x;
  padding-bottom: 4px;
}

.continue-card {
  scroll-snap-align: start;
  cursor: pointer;
  flex-shrink: 0;
}

.continue-card .poster {
  width: 140px;
  height: 98px;
  object-fit: cover;
  border-radius: 8px;
  background: var(--surface);
}

.continue-card .info {
  margin-top: 4px;
  font-size: 0.75em;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 50px;
}

.continue-card .info .title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.continue-card .info .chapter {
  color: var(--text-secondary);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.7em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.continue-card .info .progress {
  color: var(--accent);
  font-size: 0.7em;
}

/* ────────────────────── Bottom Sheet ────────────────────── */

#sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

#sheet-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

#sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 85vh;
  touch-action: pan-y;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  z-index: 201;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  display: flex;
  flex-direction: column;
}

#sheet.open {
  transform: translateY(0);
}

#sheet-header {
  padding: 12px;
  display: flex;
  justify-content: center;
  flex-shrink: 0;
}

#sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
}

#sheet-body {
  flex: 1;
  min-height: 0;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 0 16px 130px;
}

.sheet-cover-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 16px;
}

.sheet-cover {
  width: 120px;
  height: 168px;
  object-fit: cover;
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  background: var(--surface);
}

.sheet-cover-placeholder {
  width: 120px;
  height: 168px;
  border-radius: 10px;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  opacity: 0.4;
}

.sheet-title {
  font-size: 1.1em;
  font-weight: 700;
  text-align: center;
}

.sheet-title-en {
  font-size: 0.85em;
  color: var(--text-secondary);
  text-align: center;
  margin-top: 2px;
}

.sheet-meta {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin: 10px 0;
  flex-wrap: wrap;
}

.sheet-meta .badge {
  font-size: 0.7em;
  padding: 3px 10px;
  border-radius: 20px;
  background: var(--accent-dim);
  color: var(--accent);
  font-weight: 600;
}

.sheet-meta .badge.finished { color: #34d399; background: rgba(52,211,153,0.15); }
.sheet-meta .badge.hiatus { color: var(--warning); background: rgba(234,179,8,0.15); }

.sheet-rating {
  text-align: center;
  margin: 6px 0;
}

.stars {
  color: var(--warning);
  font-size: 1.1em;
  letter-spacing: 2px;
}

.rating-num {
  font-size: 0.8em;
  color: var(--text-secondary);
}

.sheet-desc {
  font-size: 0.82em;
  color: var(--text-secondary);
  line-height: 1.5;
  max-height: 80px;
  overflow: hidden;
  position: relative;
  margin-bottom: 12px;
}

.sheet-desc::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 30px;
  background: linear-gradient(transparent, var(--surface));
}

.sheet-desc.expanded {
  max-height: none;
}

.sheet-desc.expanded::after { display: none; }

.sheet-desc-more {
  display: block;
  color: var(--accent);
  font-size: 0.75em;
  font-weight: 600;
  cursor: pointer;
  margin: -6px 0 12px;
}

.sheet-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
  justify-content: center;
}

.sheet-tag {
  font-size: 0.7em;
  padding: 3px 10px;
  border-radius: 20px;
  background: var(--accent-dim);
  color: var(--accent);
  font-weight: 500;
}

.sheet-chapter-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
  margin-top: 4px;
}

.chapter-row {
  display: flex;
  align-items: center;
  padding: 10px 8px;
  border-radius: 8px;
  gap: 10px;
  cursor: pointer;
  transition: background 0.15s;
}

.chapter-row:active { background: var(--surface-elevated); }
.chapter-row.read { opacity: 0.5; }

.chapter-row .cap-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85em;
  font-weight: 600;
  min-width: 55px;
}

.chapter-row .cap-group {
  flex: 1;
  font-size: 0.8em;
  color: var(--text-secondary);
}

.chapter-row .cap-date {
  font-size: 0.7em;
  color: var(--text-secondary);
}

.chapter-row .cap-check {
  color: var(--accent);
  font-size: 0.9em;
}

.chapter-row .lang-badge {
  background: var(--surface-elevated);
  color: var(--text-secondary);
  font-size: 0.6em;
  font-weight: 700;
  padding: 2px 5px;
  border-radius: 3px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  flex-shrink: 0;
}

.fallback-notice {
  background: rgba(234, 179, 8, 0.1);
  border: 1px solid rgba(234, 179, 8, 0.2);
  color: var(--warning);
  font-size: 0.78em;
  padding: 8px 12px;
  border-radius: 8px;
  margin-bottom: 6px;
  text-align: center;
}

.chapter-select {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 4px 8px;
  font-size: 0.8em;
  min-width: 140px;
  max-width: 200px;
  outline: none;
}

.chapter-select:focus { border-color: var(--accent); }

#sheet-leer-btn {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 54px);
  left: 16px;
  right: 16px;
  height: 48px;
  background: var(--accent);
  color: var(--bg);
  font-weight: 700;
  font-size: 1em;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin: 16px 0;
  transition: transform 0.15s;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#sheet-leer-btn:active { transform: scale(0.97); }

#sheet-lib-btn {
  position: absolute;
  bottom: var(--safe-bottom);
  left: 16px;
  right: 16px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.9em;
  font-weight: 600;
  margin: 16px 0;
  transition: transform 0.15s, background 0.2s;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#sheet-lib-btn:active { transform: scale(0.97); }

.lib-btn-add {
  background: var(--surface-elevated);
  color: var(--accent);
  border: 1px solid rgba(167,139,250,0.2);
}

.lib-btn-add:hover {
  background: rgba(167,139,250,0.1);
}

.lib-btn-remove {
  background: rgba(248,113,113,0.08);
  color: var(--danger);
  border: 1px solid rgba(248,113,113,0.15);
}

.lib-btn-remove:hover {
  background: rgba(248,113,113,0.15);
}

/* ────────────────── Load More Button ──────────────────── */

.load-more-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: calc(100% - 32px);
  max-width: 432px;
  margin: 16px auto;
  padding: 12px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 0.9em;
  font-weight: 600;
  transition: background 0.2s, transform 0.15s;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.load-more-btn:hover {
  background: rgba(167,139,250,0.08);
}

.load-more-btn:active {
  transform: scale(0.97);
}

/* ────────────────────── Skeleton ────────────────────── */

.skeleton {
  background: linear-gradient(90deg, var(--surface) 25%, #2a2a3e 50%, var(--surface) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 8px;
}

.skeleton-poster {
  width: 100%;
  aspect-ratio: 70/98;
  border-radius: 8px;
}

.skeleton-title {
  height: 16px;
  margin-top: 6px;
  border-radius: 4px;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ────────────────────── Reader ────────────────────── */

#reader-container {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 300;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

#reader-container.active { display: flex; }

#reader-pages {
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

#reader-pages.scroll-mode {
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
  display: block;
  -webkit-overflow-scrolling: touch;
}

#reader-pages.scroll-mode .reader-page {
  scroll-snap-align: start;
  min-height: 100%;
}

.reader-page {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
}

#reader-pages.scroll-mode .reader-page {
  position: relative;
  opacity: 1;
  pointer-events: auto;
}

.reader-page.active,
#reader-pages.scroll-mode .reader-page {
  opacity: 1;
  pointer-events: auto;
}

.reader-page img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transition: transform 0.2s;
}

.reader-page img.zoomed { transform: scale(2); }

.reader-progress-bar {
  position: fixed;
  right: 4px;
  top: 10%;
  height: 80%;
  width: 4px;
  background: var(--border);
  border-radius: 2px;
  z-index: 301;
  overflow: hidden;
}

.reader-progress-fill {
  width: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: height 0.2s;
}

/* Reader menu */
#reader-menu {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(3, 3, 7, 0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 16px 16px 0 0;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  z-index: 302;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

#reader-menu.open { transform: translateY(0); }

#reader-menu .menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

#reader-menu .menu-info {
  font-size: 0.9em;
  font-weight: 600;
}

#reader-menu .menu-counter {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.8em;
  color: var(--text-secondary);
}

#reader-menu select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 12px;
  font-size: 0.85em;
  width: 100%;
  outline: none;
  margin-bottom: 10px;
}

#reader-menu select:focus { border-color: var(--accent); }

.reader-menu-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.reader-menu-btn {
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.8em;
  font-weight: 600;
  transition: background 0.15s;
  color: var(--text);
  background: var(--surface);
}

.reader-menu-btn.active { background: var(--accent); color: var(--bg); }
.reader-menu-btn.close { color: var(--danger); }

/* Reader loading page */
.reader-loading-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  height: 100%;
}

.reader-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.reader-error-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-secondary);
  font-size: 0.9em;
}

.reader-retry-btn {
  padding: 10px 24px;
  background: var(--accent);
  color: var(--bg);
  border-radius: 8px;
  font-weight: 600;
}

/* Reader tap zones */
#reader-tap-left,
#reader-tap-right {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 33%;
  z-index: 2;
}

#reader-tap-left { left: 0; }
#reader-tap-right { right: 0; }
#reader-tap-center {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 33%;
  right: 33%;
  z-index: 2;
}

/* ────────────────────── Toast ────────────────────── */

#toast {
  position: fixed;
  bottom: calc(var(--tab-height) + var(--safe-bottom) + 16px);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-elevated);
  color: var(--text);
  padding: 10px 20px;
  border-radius: 12px;
  font-size: 0.85em;
  font-weight: 500;
  z-index: 400;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ────────────────────── Offline Banner ────────────────────── */

#offline-banner {
  position: fixed;
  top: var(--safe-top);
  left: 0;
  right: 0;
  background: var(--warning);
  color: #000;
  padding: 8px 16px;
  text-align: center;
  font-size: 0.8em;
  font-weight: 600;
  z-index: 500;
  transform: translateY(-100%);
  transition: transform 0.3s;
  pointer-events: none;
}

#offline-banner.show {
  transform: translateY(0);
  pointer-events: auto;
}

/* ────────────────────── Empty States ────────────────────── */

.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-secondary);
}

.empty-state svg {
  width: 64px;
  height: 64px;
  margin-bottom: 12px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 0.9em;
}

.sheet-retry-btn {
  background: var(--accent);
  color: var(--bg);
  padding: 8px 20px;
  border-radius: 8px;
  margin-top: 8px;
  font-weight: 600;
  font-size: 0.85em;
  cursor: pointer;
}

.sheet-retry-btn:active {
  opacity: 0.8;
}

/* ────────────────────── Ajustes ────────────────────── */

.settings-section {
  margin-bottom: 20px;
}

.settings-section h3 {
  font-size: 0.8em;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.settings-row:last-child { border-bottom: none; }

.settings-label {
  font-size: 0.9em;
  font-weight: 500;
}

.settings-switch {
  position: relative;
  width: 48px;
  height: 26px;
  background: var(--border);
  border-radius: 13px;
  cursor: pointer;
  transition: background 0.2s;
}

.settings-switch.on { background: var(--accent); }

.settings-switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}

.settings-switch.on::after { transform: translateX(22px); }

.settings-btn {
  width: 100%;
  padding: 12px;
  background: var(--surface);
  border-radius: 10px;
  color: var(--danger);
  font-weight: 600;
  text-align: center;
  transition: background 0.15s;
}

.settings-btn:active { background: var(--surface-elevated); }

.settings-cache-info {
  font-size: 0.75em;
  color: var(--text-secondary);
  text-align: center;
  margin-top: 4px;
}

/* ────────────────────── Responsive ────────────────────── */

@media (min-width: 768px) {
  .manga-grid {
    grid-template-columns: repeat(3, 1fr);
    max-width: 600px;
    margin: 0 auto;
  }

  #sheet {
    max-width: 480px;
    left: 50%;
    transform: translate(-50%, 100%);
  }

  #sheet.open {
    transform: translate(-50%, 0);
  }

  #sheet-body {
    padding: 0 24px 135px;
  }

  #sheet-leer-btn {
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 48px);
    max-width: 432px;
  }

  #sheet-lib-btn {
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 48px);
    max-width: 432px;
  }
}

@media (min-width: 768px) and (orientation: landscape) {
  #reader-pages .reader-page {
    /* Hint for double-page on landscape */
  }

  .reader-page img {
    max-width: 50%;
  }
}
