* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f0ece1; color: #2b2b2b;
}
#app { display: flex; flex-direction: column; height: 100vh; }

#topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; background: #8a6d3b; color: #fff;
  flex-shrink: 0; z-index: 5;
}
#topbar h1 { font-size: 17px; margin: 0; flex: 1; font-weight: 600; }
.topbar-actions { display: flex; gap: 4px; }
.icon-btn {
  background: none; border: none; color: inherit; font-size: 20px;
  cursor: pointer; padding: 6px 8px; border-radius: 8px; line-height: 1;
}
.icon-btn:active { background: rgba(255,255,255,0.2); }

.view { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.upload-box { padding: 16px; text-align: center; }
.upload-label {
  display: inline-block; padding: 12px 20px; background: #8a6d3b; color: #fff;
  border-radius: 10px; cursor: pointer; font-weight: 600;
}
#upload-status { margin-top: 8px; font-size: 14px; color: #555; }

.library-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 16px; padding: 16px;
}
.book-card { cursor: pointer; text-align: center; position: relative; }
.book-cover {
  width: 100%; aspect-ratio: 2/3; object-fit: cover; border-radius: 6px;
  background: #ddd6c4; box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.book-cover.placeholder {
  display: flex; align-items: center; justify-content: center; font-size: 34px;
}
.book-title { font-size: 13px; margin-top: 6px; font-weight: 600; }
.book-author { font-size: 11px; color: #666; }
.book-progress-bar { height: 4px; background: rgba(0,0,0,0.12); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.book-progress-fill { height: 100%; background: #8a6d3b; border-radius: 2px; }
.book-progress-text { font-size: 10px; color: #777; margin-top: 3px; }
.book-delete {
  position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,0.55);
  color: #fff; border: none; border-radius: 50%; width: 24px; height: 24px; cursor: pointer;
}
.book-fetch-cover {
  display: block; margin: 4px auto 0; font-size: 11px; padding: 3px 8px;
  border-radius: 6px; border: 1px solid #ccc; background: #fafafa; color: #333; cursor: pointer;
}

#view-reader { display: flex; flex-direction: column; background: var(--reader-bg, #f7f3e9); }
#chapter-nav {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  padding: 8px 12px; font-size: 13px; background: rgba(0,0,0,0.04); flex-shrink: 0; gap: 4px;
}
.nav-btn { background: none; border: 1px solid rgba(0,0,0,0.2); border-radius: 8px; padding: 6px 10px; cursor: pointer; color: inherit; font: inherit; }
#page-indicator { font-size: 11px; opacity: 0.7; white-space: nowrap; flex-basis: 100%; text-align: center; order: 10; }

#reader-content {
  flex: 1; position: relative; overflow: hidden;
  max-width: 720px; margin: 0 auto; width: 100%;
}

.book-page {
  width: 100%; height: 100%; box-sizing: border-box;
  overflow: hidden; padding: 24px 18px;
  background: var(--reader-bg, #f7f3e9);
  color: var(--reader-fg, #2b2b2b);
  font-family: var(--reader-font, Georgia, serif);
  font-size: var(--reader-size, 20px);
  line-height: 1.65;
}
.book-page p, .book-page li, .book-page h1, .book-page h2,
.book-page h3, .book-page blockquote { margin: 0 0 1em; break-inside: avoid; }
.book-page img { max-width: 100%; border-radius: 6px; }
.book-page [data-para-index].speaking { background: rgba(255, 214, 102, 0.35); border-radius: 4px; }
.book-page mark.highlight { padding: 0 1px; border-radius: 2px; cursor: pointer; }

/* StPageFlip base styles (github.com/Nodlik/StPageFlip) */
.stf__parent { position: relative; display: block; box-sizing: border-box; transform: translateZ(0); touch-action: pan-y; }
.stf__parent canvas { position: absolute; width: 100%; height: 100%; left: 0; top: 0; }
.stf__block { position: absolute; width: 100%; height: 100%; box-sizing: border-box; perspective: 2000px; }
.stf__item { display: none; position: absolute; transform-style: preserve-3d; }
.stf__outerShadow, .stf__innerShadow, .stf__hardShadow, .stf__hardInnerShadow { position: absolute; left: 0; top: 0; }

#selection-toolbar {
  position: fixed; display: flex; gap: 6px; background: #222; padding: 6px 8px;
  border-radius: 10px; box-shadow: 0 4px 14px rgba(0,0,0,0.35); z-index: 20;
}
#selection-toolbar .swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid #fff; cursor: pointer; }
#selection-toolbar button#btn-add-note-inline {
  background: #444; color: #fff; border: none; border-radius: 8px; padding: 0 10px; font-size: 13px; cursor: pointer;
}

#player-bar {
  display: flex; align-items: center; gap: 10px; padding: 8px 14px;
  background: #1e1e1e; color: #fff; position: sticky; bottom: 0; flex-shrink: 0;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}
.player-info { flex: 1; min-width: 0; }
#player-title, #player-sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#player-title { font-size: 13px; font-weight: 600; }
#player-sub { font-size: 11px; color: #bbb; }
#player-rate { width: 80px; }

.panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(340px, 90vw);
  background: #fff; color: #222; box-shadow: -4px 0 16px rgba(0,0,0,0.3);
  z-index: 30; display: flex; flex-direction: column;
}
@media (max-width: 600px) {
  .panel { width: 100vw; left: 0; }
}
.panel-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid #eee; flex-shrink: 0; }
.panel-header .close-panel { font-size: 16px; padding: 8px 14px; border: 1px solid #ccc; border-radius: 8px; }
.panel-header h2 { font-size: 16px; margin: 0; }
.panel-body { flex: 1; overflow-y: auto; padding: 14px; }
.panel-body section { margin-bottom: 22px; }
.panel-body h3 { font-size: 13px; text-transform: uppercase; color: #888; margin: 0 0 8px; }
.panel-body select, .panel-body input[type=range] { width: 100%; margin-bottom: 8px; }
.panel-body label { display: flex; justify-content: space-between; align-items: center; font-size: 13px; margin-bottom: 6px; }
.panel-body input[type=color] { width: 40px; height: 28px; border: none; padding: 0; }

.theme-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 10px; }
.theme-swatch { border-radius: 8px; padding: 10px 4px; font-size: 11px; border: 2px solid transparent; cursor: pointer; }
.theme-swatch[data-theme=claro] { background: #f7f3e9; color: #2b2b2b; }
.theme-swatch[data-theme=sepia] { background: #ead9b9; color: #4a3a25; }
.theme-swatch[data-theme=escuro] { background: #242424; color: #e6e2d8; }
.theme-swatch[data-theme=preto] { background: #000; color: #c9c9c9; border-color: #333; }

.voice-row { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; font-size: 13px; border-bottom: 1px solid #f0f0f0; }
.voice-row button { font-size: 12px; padding: 4px 8px; border-radius: 6px; border: 1px solid #ccc; background: #fafafa; color: #333; cursor: pointer; }
.voice-row .status { font-size: 11px; color: #888; }

#notes-list .note-card { border: 1px solid #eee; border-radius: 8px; padding: 10px; margin-bottom: 10px; }
#notes-list .note-excerpt { font-size: 13px; border-left: 4px solid #ffe066; padding-left: 8px; margin-bottom: 6px; font-style: italic; }
#notes-list .note-text { font-size: 13px; white-space: pre-wrap; }
#notes-list .note-actions { display: flex; gap: 8px; margin-top: 6px; }
#notes-list .note-actions button { font-size: 12px; border: none; background: #f2f2f2; color: #333; border-radius: 6px; padding: 4px 8px; cursor: pointer; }
#notes-list .chapter-label { font-size: 11px; color: #999; margin-bottom: 4px; }

#overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 25; }

@media (prefers-color-scheme: dark) {
  html, body { background: #1a1a1a; color: #eee; }
  .panel { background: #222; color: #eee; }
  .panel-header { border-color: #333; }
  .voice-row { border-color: #333; }
  #notes-list .note-card { border-color: #333; }
  .book-progress-bar { background: rgba(255,255,255,0.15); }
  .book-progress-text { color: #aaa; }
}
