/* Reset & Base Styles */
* { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
* { -webkit-tap-highlight-color: transparent; }
body, html { width: 100%; height: 100%; overflow: hidden; display: flex; flex-direction: column; }
body { background: var(--bg); color: var(--fg); }

/* Themes */
body.theme-light  { --bg: #fffaf0; --bar: #f1e9d6; --fg: #1f2937; --line: #ddd2b8; }
body.theme-sepia  { --bg: #f7eedb; --bar: #ece3cc; --fg: #433422; --line: #d4c5a9; }
body.theme-purple { --bg: #241633; --bar: #1a0f26; --fg: #f3e8ff; --line: #3b2352; }
body.theme-navy   { --bg: #121c2e; --bar: #0b1320; --fg: #e0e6ed; --line: #1e293b; }
body.theme-dark   { --bg: #1e1e1e; --bar: #121212; --fg: #e0e0e0; --line: #2a2a2a; }

/* Force viewer to inherit backgrounds and make iframe transparent to fix flashing and themes */
.viewer-wrapper, #viewer, .epub-viewer { background: transparent; }
.epub-viewer iframe { background: transparent !important; border: none !important; color: inherit !important; }

/* Top App Header with Slide-Away Hide */
#app-header { display: flex; justify-content: space-between; align-items: center; padding: 0 16px; background: inherit; border-bottom: 1px solid rgba(128,128,128,0.2); height: 60px; color: inherit; flex-shrink: 0; transition: margin-top 0.3s ease; overflow: hidden; }
#app-header.hidden { margin-top: -60px; }

.nav-tabs { display: flex; gap: 8px; flex-shrink: 0; }
.tab-btn, .action-btn, .control-btn { min-height: 44px; padding: 8px 12px; border: none; border-radius: 6px; background: rgba(128, 128, 128, 0.15); color: inherit; font-size: 13px; cursor: pointer; font-weight: 500; white-space: nowrap; flex-shrink: 0; }
.tab-btn.active { background: #4f46e5; color: white; }
.header-actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }
.upload-btn { display: inline-flex; align-items: center; background: #10b981 !important; color: white !important; }
.ocean-btn { background: #0284c7 !important; color: white !important; text-decoration: none; display: inline-flex; align-items: center; }

/* Containers */
.view-container { flex: 1; position: relative; width: 100%; height: 100%; display: flex; flex-direction: column; overflow: hidden; }
.filter-bar { display: flex; gap: 8px; padding: 12px 16px; background: rgba(0,0,0,0.02); border-bottom: 1px solid rgba(128,128,128,0.1); overflow-x: auto; align-items: center; flex-shrink: 0; }
.filter-btn { min-height: 44px; background: transparent; border: 1px solid currentColor; opacity: 0.7; border-radius: 20px; padding: 6px 14px; font-size: 13px; cursor: pointer; color: inherit; white-space: nowrap; }
.filter-btn.active { opacity: 1; font-weight: bold; background: rgba(128,128,128,0.25); }
.add-cat-btn { border-style: dashed; font-weight: bold; }
.shelf-grid { flex: 1; overflow-y: auto; padding: 20px; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 20px; align-content: start; }

/* Book Cards */
.book-card { background: rgba(128, 128, 128, 0.08); border: 1px solid rgba(128, 128, 128, 0.2); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; cursor: pointer; position: relative; transition: transform 0.2s; color: inherit; }
.book-card:hover { transform: translateY(-3px); }
.cover-box { width: 100%; height: 170px; background: rgba(128,128,128,0.2); border-radius: 6px; display: flex; align-items: center; justify-content: center; overflow: hidden; position: relative; margin-bottom: 8px; }
.cover-box img { width: 100%; height: 100%; object-fit: cover; }
.status-badge { position: absolute; bottom: 6px; right: 6px; background: rgba(0,0,0,0.7); color: white; font-size: 10px; padding: 2px 6px; border-radius: 4px; }
.status-badge.finished { background: #059669; }
.category-badge { position: absolute; top: 6px; left: 6px; background: rgba(79, 70, 229, 0.85); color: white; font-size: 10px; padding: 2px 6px; border-radius: 4px; max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-title { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 2px; color: inherit; }
.book-author { font-size: 11px; opacity: 0.7; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 6px; color: inherit; }
.card-actions { display: flex; gap: 4px; justify-content: space-between; align-items: center; margin-top: auto; }
.shelf-select { font-size: 11px; padding: 2px 4px; border-radius: 4px; background: rgba(128,128,128,0.2); color: inherit; border: 1px solid rgba(128,128,128,0.3); max-width: 90px; }
.delete-btn { position: absolute; top: 6px; right: 6px; background: #ef4444; color: white; border: none; width: 22px; height: 22px; border-radius: 50%; font-size: 11px; cursor: pointer; display: none; }
.book-card:hover .delete-btn { display: flex; align-items: center; justify-content: center; }

/* Viewer & Overlay Controls */
.viewer-wrapper { flex: 1; width: 100%; height: 100%; position: relative; }
.epub-viewer { width: 100%; height: 100%; }

/* Overlay Reader Headers/Footers */
.reader-header, .reader-footer { position: absolute; left: 0; width: 100%; z-index: 10; background: inherit; display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; transition: transform 0.3s ease, opacity 0.3s ease; }
.reader-header { top: 0; height: 60px; border-bottom: 1px solid rgba(128,128,128,0.2); }
.reader-footer { bottom: 0; height: 60px; border-top: 1px solid rgba(128,128,128,0.2); }

.reader-header.hidden { transform: translateY(-100%); opacity: 0; pointer-events: none; }
.reader-footer.hidden { transform: translateY(100%); opacity: 0; pointer-events: none; }

.font-controls { display: flex; gap: 4px; flex-shrink: 0; }
.theme-picker { display: flex; gap: 6px; align-items: center; flex-shrink: 0; }
.theme-dot { width: 44px; height: 44px; border-radius: 50%; cursor: pointer; display: grid; place-items: center; }
.theme-dot::before { content: ""; width: 28px; height: 28px; border-radius: 50%; }
.theme-dot.light::before { background-color: #fffaf0; border: 1px solid #ccc; }
.theme-dot.sepia::before { background-color: #f7eedb; border: 1px solid #d4c5a9; }
.theme-dot.purple::before { background-color: #241633; border: 2px solid #9333ea; }
.theme-dot.navy::before { background-color: #121c2e; border: 1px solid #2a3b5e; }
.theme-dot.dark::before { background-color: #1e1e1e; border: 1px solid #444; }

.progress-info { flex: 1; margin: 0 16px; display: flex; flex-direction: column; gap: 4px; }
#page-info { font-size: 12px; text-align: center; opacity: 0.8; }
.progress-track { width: 100%; height: 4px; background: rgba(128,128,128,0.2); border-radius: 2px; overflow: hidden; }
#progress-bar { width: 0%; height: 100%; background: #4f46e5; transition: width 0.2s; }

.tts-settings summary { min-height: 44px; display: flex; align-items: center; padding: 0 10px; border-radius: 6px; background: rgba(128,128,128,0.15); cursor: pointer; list-style: none; white-space: nowrap; }
.tts-settings summary::-webkit-details-marker { display: none; }
.tts-settings-panel { position: fixed; top: 126px; right: 12px; z-index: 120; width: min(320px, 85vw); display: grid; grid-template-columns: auto 1fr; gap: 10px; align-items: center; padding: 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--bar); color: var(--fg); box-shadow: 0 8px 24px rgba(0,0,0,0.3); }
.tts-settings-panel select { min-width: 0; min-height: 44px; padding: 6px 8px; border-radius: 6px; background: var(--bg); color: var(--fg); border: 1px solid var(--line); }

/* Drawers */
.toc-drawer { position: fixed; top: 0; left: -320px; width: 320px; height: 100%; background: inherit; box-shadow: 2px 0 10px rgba(0,0,0,0.3); transition: left 0.3s ease; z-index: 100; display: flex; flex-direction: column; }
.toc-drawer.open { left: 0; }
.toc-header { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-bottom: 1px solid rgba(128,128,128,0.2); }
#toc-list, #bookmark-list, #highlight-list { flex: 1; overflow-y: auto; list-style: none; padding: 10px; }
.toc-item, .bookmark-item, .highlight-item { min-height: 44px; padding: 10px 12px; font-size: 14px; cursor: pointer; border-radius: 6px; margin-bottom: 6px; display: flex; justify-content: space-between; align-items: center; background: rgba(128,128,128,0.1); }
.toc-item:hover, .bookmark-item:hover, .highlight-item:hover { background: rgba(128,128,128,0.2); }
.bookmark-del, .highlight-del { min-width: 44px; min-height: 44px; background: #ef4444; color: white; border: none; border-radius: 4px; padding: 2px 6px; font-size: 11px; cursor: pointer; }

/* Theme-specific overrides */
#app-header, body .reader-header, body .reader-footer, body .toc-drawer { background: var(--bar); color: var(--fg); border-color: var(--line); }

/* Reading mode: menus float OVER the page so the page never resizes or jumps */
body.reading #app-header { position: fixed; top: 0; left: 0; right: 0; z-index: 30; margin-top: 0; transition: transform 0.3s ease; }
body.reading #app-header.hidden { transform: translateY(-100%); margin-top: 0; }
body.reading #reader-view { position: fixed; top: 0; left: 0; right: 0; bottom: 0; width: 100%; height: 100%; }
body.reading .reader-header { top: 60px; z-index: 20; }
body.reading .reader-header.hidden { transform: translateY(-200%); }

/* Reading Log Styles */
#reading-log-modal.open { right: 0 !important; left: auto !important; }
#safe-reading-list li { padding: 8px 10px; font-size: 14px; border-bottom: 1px solid rgba(128, 128, 128, 0.15); display: flex; justify-content: space-between; align-items: center; }
.safe-delete-btn { min-width: 44px; min-height: 44px; background: #ef4444; color: white; border: none; border-radius: 4px; padding: 2px 6px; font-size: 11px; cursor: pointer; }
#app-version { position: fixed; right: 8px; bottom: 4px; font-size: 10px; opacity: 0.4; z-index: 1; pointer-events: none; }
body.reading #app-version { display: none; }

@media (max-width: 760px) {
  #app-header { height: auto; min-height: 60px; flex-wrap: wrap; gap: 6px; justify-content: center; padding: 6px 10px; }
  .reader-header, .reader-footer { padding: 8px; gap: 4px; }
  .reader-footer .control-btn, .reader-header .control-btn { padding: 6px 8px; font-size: 12px; }
  .progress-info { min-width: 72px; margin: 0 4px; }
  .toc-drawer { width: min(340px, 88vw); }
  #reading-log-modal { width: min(380px, 100vw) !important; }
}

@media (pointer: coarse) {
  .delete-btn { display: flex; align-items: center; justify-content: center; width: 44px; height: 44px; font-size: 14px; }
}