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

/* ── Themes ── */
html,
html[data-theme="dark"] {
  --bg: #0f172a;
  --card: #111827;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --border: #263244;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #dc2626;
  --viewer-bg: #0f172a;
  --hover-bg: rgba(255, 255, 255, 0.05);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --radius: 14px;
}

html[data-theme="sepia"] {
  --bg: #b5996e;
  --card: #c8a97a;
  --text: #1e0f00;
  --muted: #5c3d1e;
  --border: #9a7d52;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #dc2626;
  --viewer-bg: #c8a97a;
  --hover-bg: rgba(0, 0, 0, 0.08);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  --radius: 14px;
}

html[data-theme="white"] {
  --bg: #efefef;
  --card: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #d1d5db;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #dc2626;
  --viewer-bg: #ffffff;
  --hover-bg: rgba(0, 0, 0, 0.05);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  --radius: 14px;
}

html,
body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  transition: background 0.2s, color 0.2s;
}

body {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ── */
#sidebar {
  width: 280px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width 0.25s ease, border-color 0.25s ease;
  background: var(--card);
}

#sidebar.collapsed {
  width: 0;
  border-right-color: transparent;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 14px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-header h1 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

/* ── Sidebar tabs ── */
.sidebar-tabs {
  display: flex;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
}

.sidebar-tab {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 9px 8px;
  margin-bottom: -1px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.sidebar-tab:hover:not(.active) {
  color: var(--text);
}

.sidebar-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* ── Upload zone ── */
#upload-zone {
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.25s ease;
}

#upload-zone.open {
  max-height: 140px;
}

.drop-area {
  margin: 12px;
  border: 2px dashed var(--border);
  border-radius: 10px;
  padding: 20px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  font-size: 13px;
}

.drop-area:hover,
.drop-area.drag-over {
  border-color: var(--primary);
  background: rgba(59, 130, 246, 0.06);
  color: var(--text);
}

.drop-area span {
  display: block;
  font-size: 22px;
  margin-bottom: 4px;
}

/* ── Tab content panels ── */
.tab-content {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.tab-content::-webkit-scrollbar { width: 5px; }
.tab-content::-webkit-scrollbar-track { background: transparent; }
.tab-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── EPUB list items ── */
.epub-item {
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  margin-bottom: 6px;
  overflow: hidden;
  transition: border-color 0.15s;
}

.epub-item:hover { border-color: var(--primary); opacity: 0.9; }
.epub-item:hover { opacity: 1; border-color: var(--muted); }
.epub-item.active { border-color: var(--primary); }

.epub-item-body {
  padding: 10px 12px 8px;
  cursor: pointer;
}

.epub-item-title {
  font-weight: 500;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}

.epub-item-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}

.progress-badge {
  background: rgba(59, 130, 246, 0.15);
  color: #93c5fd;
  border: 1px solid rgba(59, 130, 246, 0.25);
  border-radius: 5px;
  padding: 0 5px;
  font-size: 11px;
  font-weight: 500;
}

.epub-item-actions {
  display: flex;
  border-top: 1px solid var(--border);
}

.item-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--muted);
  padding: 6px;
  cursor: pointer;
  font-size: 12px;
  transition: background 0.15s, color 0.15s;
}

.item-btn:hover {
  background: var(--hover-bg);
  color: var(--text);
}

.item-btn.del:hover {
  background: rgba(220, 38, 38, 0.1);
  color: #fca5a5;
}

.item-btn + .item-btn { border-left: 1px solid var(--border); }

.rename-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--primary);
  border-radius: 6px;
  color: var(--text);
  padding: 4px 7px;
  font-size: 13px;
  outline: none;
}

.empty-list {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 32px 16px;
}

/* ── TOC ── */
.toc-item {
  padding: 7px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  line-height: 1.4;
  transition: background 0.1s, color 0.1s;
}

.toc-item:hover {
  background: rgba(59, 130, 246, 0.08);
  color: var(--primary);
}

.toc-item.active {
  color: var(--primary);
  background: rgba(59, 130, 246, 0.12);
  font-weight: 500;
}

/* ── Reader ── */
#reader {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  background: var(--bg);
  transition: background 0.2s;
}

#btn-toggle-bar {
  position: absolute;
  top: 7px;
  right: 8px;
  z-index: 20;
  background: var(--card) !important;
}

.reader-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-right: 44px;
  border-bottom: 1px solid var(--border);
  background: var(--card);
  flex-shrink: 0;
  flex-wrap: wrap;
  overflow: hidden;
  max-height: 80px;
  transition: max-height 0.2s ease, padding-top 0.2s ease, padding-bottom 0.2s ease, border-color 0.2s ease, background 0.2s;
}

.reader-bar.collapsed {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-bottom-color: transparent;
}

.reader-bar-group {
  display: flex;
  align-items: center;
  gap: 5px;
}

.reader-info {
  color: var(--muted);
  font-size: 13px;
  min-width: 80px;
  text-align: center;
}

/* ── Theme buttons ── */
.theme-btn {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  transition: border-color 0.15s, transform 0.1s;
}

.theme-btn:hover { transform: scale(1.15); }
.theme-btn.active { border-color: var(--primary); }
.theme-btn[data-theme="dark"]  { background: #0f172a; box-shadow: inset 0 0 0 1px rgba(255,255,255,0.25); }
.theme-btn[data-theme="sepia"] { background: #f5f0e8; box-shadow: inset 0 0 0 1px rgba(0,0,0,0.2); }
.theme-btn[data-theme="white"] { background: #ffffff;  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.2); }

/* ── Bar separator ── */
.bar-sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  flex-shrink: 0;
}

/* ── Viewer ── */
#viewer-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--viewer-bg);
  transition: background 0.2s;
}

#viewer-wrap.scrolled {
  overflow-y: auto;
}

#viewer-wrap.scrolled::-webkit-scrollbar { width: 8px; }
#viewer-wrap.scrolled::-webkit-scrollbar-track { background: var(--viewer-bg); }
#viewer-wrap.scrolled::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

#epub-viewer {
  flex: 1;
  min-height: 0;
}

#viewer-wrap.scrolled #epub-viewer {
  flex: none;
  height: auto;
  min-height: 100%;
}

#epub-viewer iframe {
  border: none;
}

#reader-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 15px;
}

#reader-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

#viewer-loading {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 14px;
  background: var(--viewer-bg);
}

.hidden { display: none !important; }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
}

.btn:disabled { opacity: 0.4; cursor: default; }

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:not(:disabled):hover { background: var(--primary-hover); }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-ghost:not(:disabled):hover { background: var(--hover-bg); }
.btn-icon { padding: 6px 9px; }

/* ── Error banner ── */
#error-banner {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(220, 38, 38, 0.92);
  color: #fff;
  padding: 10px 20px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 999;
  max-width: 480px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  transition: opacity 0.3s;
}

/* ── Bookmarks ── */
.bookmark-item {
  position: relative;
  padding: 9px 32px 9px 12px;
  border-radius: 6px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}

.bookmark-item:hover { background: var(--hover-bg); }

.bookmark-label {
  font-size: 13px;
  color: var(--text);
  line-height: 1.4;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bookmark-meta { font-size: 11px; color: var(--muted); }

.bookmark-del {
  position: absolute;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  padding: 4px;
  border-radius: 4px;
  line-height: 1;
  transition: color 0.15s, background 0.15s;
}

.bookmark-del:hover { color: var(--danger); background: rgba(220, 38, 38, 0.08); }

#btn-bookmark.bookmarked {
  color: #fbbf24;
  border-color: rgba(251, 191, 36, 0.4);
  background: rgba(251, 191, 36, 0.08);
}

/* ── Mobile ── */
#sidebar-backdrop {
  display: none;
}

@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100%;
    width: min(85vw, 300px);
    z-index: 100;
    transform: translateX(0);
    border-right: 1px solid var(--border);
    transition: transform 0.28s ease, border-color 0.25s ease;
  }

  #sidebar.collapsed {
    width: min(85vw, 300px);
    transform: translateX(-100%);
    border-right-color: var(--border);
  }

  #sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 99;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.28s ease;
  }

  #sidebar-backdrop.visible {
    opacity: 1;
    pointer-events: auto;
  }

  .reader-bar {
    max-height: 110px;
    gap: 4px;
    padding: 6px 44px 6px 8px;
  }

  .reader-info {
    font-size: 12px;
    min-width: 60px;
  }

  #font-label { min-width: 34px !important; }

  .btn { min-height: 38px; }
  .btn-icon { padding: 7px 10px; min-height: 38px; }
}
