/* === Reset & Variables === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f5f5;
  --bg-panel: #ffffff;
  --bg-toolbar: #2c2c2c;
  --bg-status: #2c2c2c;
  --text: #333;
  --text-secondary: #666;
  --text-invert: #eee;
  --border: #ddd;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --highlight: rgba(255, 255, 0, 0.45);
  --highlight-current: rgba(255, 165, 0, 0.7);
  --shadow: 0 1px 4px rgba(0,0,0,0.1);
  --radius: 6px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a2e;
    --bg-panel: #222244;
    --bg-toolbar: #111122;
    --bg-status: #111122;
    --text: #ddd;
    --text-secondary: #999;
    --text-invert: #eee;
    --border: #333366;
    --accent: #4f8cff;
    --accent-hover: #6aa0ff;
    --highlight: rgba(255, 255, 60, 0.35);
    --highlight-current: rgba(255, 165, 0, 0.55);
  }
}

html, body { height: 100%; font-family: -apple-system, "Segoe UI", "Noto Sans SC", sans-serif; background: var(--bg); color: var(--text); }

body { display: grid; grid-template-rows: 44px 1fr 28px; grid-template-columns: 280px 1fr; overflow: hidden; }

/* === Toolbar === */
#toolbar {
  grid-column: 1 / -1;
  background: var(--bg-toolbar);
  color: var(--text-invert);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  font-size: 14px;
  z-index: 100;
}
#toolbar-title { font-weight: 600; font-size: 15px; }
#toolbar-actions { display: flex; align-items: center; gap: 8px; }
#toolbar-actions button, #toolbar-actions select, #toolbar-actions input {
  background: rgba(255,255,255,0.15);
  color: var(--text-invert);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius);
  padding: 4px 10px;
  font-size: 13px;
  cursor: pointer;
}
#toolbar-actions button:hover { background: rgba(255,255,255,0.25); }
#toolbar-actions input { width: 55px; text-align: center; }
#toolbar-actions select { min-width: 70px; }
#toolbar-actions select option { background: #333; color: #eee; }
#page-info { font-size: 12px; opacity: 0.8; white-space: nowrap; }

/* === Sidebar === */
#sidebar {
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 50;
  position: relative;
}
#sidebar-toggle-btn {
  position: fixed;
  left: 280px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 60;
  width: 18px;
  height: 48px;
  padding: 0;
  cursor: pointer;
  font-size: 10px;
  color: transparent;
  background: var(--border);
  border: none;
  border-radius: 0 10px 10px 0;
  line-height: 48px;
  text-align: center;
  transition: all 0.2s;
  opacity: 0.4;
  outline: none;
}
#sidebar-toggle-btn::after {
  content: "‹";
  font-size: 20px;
  font-weight: 300;
  color: var(--text-secondary);
  position: absolute;
  left: 4px;
  top: 50%;
  transform: translateY(-50%);
  line-height: 1;
}
#sidebar-toggle-btn.hidden-state {
  left: 2px !important;
  border-radius: 10px !important;
}
#sidebar-toggle-btn.hidden-state::after {
  content: "›";
  left: 5px;
}
#sidebar-toggle-btn:hover {
  opacity: 1;
  width: 22px;
  background: var(--accent);
}
#sidebar-toggle-btn:hover::after { color: #fff; }
#sidebar-tabs { display: flex; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.sidebar-tab {
  flex: 1;
  padding: 10px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  border-bottom: 2px solid transparent;
  transition: all .15s;
}
.sidebar-tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.sidebar-tab:hover { background: rgba(0,0,0,0.03); }

#search-bar {
  display: flex;
  padding: 8px;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#search-input {
  flex: 1;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
#search-input:focus { border-color: var(--accent); }
#search-mode-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  cursor: pointer;
  font-size: 16px;
  white-space: nowrap;
}
#search-mode-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.sidebar-panel { display: none; flex: 1; overflow-y: auto; }
.sidebar-panel.active { display: flex; flex-direction: column; }

#library-list, #toc-list { padding: 0; }
#library-list { flex: 1; overflow-y: auto; }

/* Breadcrumb */
#library-breadcrumb {
  padding: 6px 8px;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  flex-shrink: 0;
  background: var(--bg-secondary, #f7f7f7);
}
#library-breadcrumb .crumb {
  cursor: pointer;
  color: var(--accent);
  padding: 1px 4px;
  border-radius: 3px;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#library-breadcrumb .crumb:hover { background: var(--bg-hover, #e0e0e0); }
#library-breadcrumb .crumb-sep { color: var(--fg-muted, #999); cursor: default; }

/* Library tree entries */
.lib-entry {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  transition: background .1s;
}
.lib-entry:hover { background: rgba(0,0,0,0.04); }
.lib-entry .lib-icon { font-size: 16px; flex-shrink: 0; }
.lib-entry .lib-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-entry .lib-size { font-size: 11px; color: var(--text-secondary); flex-shrink: 0; }

.toc-item {
  padding: 5px 12px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background .1s;
}
.toc-item:hover { background: rgba(0,0,0,0.04); }
.toc-item.active { background: var(--accent); color: #fff; }

.hint { color: var(--text-secondary); font-size: 13px; padding: 12px; display: block; text-align: center; }
.hint-open {
  color: var(--accent);
  font-size: 14px;
  padding: 16px;
  display: block;
  text-align: center;
  cursor: pointer;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  margin: 12px;
  transition: all .15s;
}
.hint-open:hover { border-color: var(--accent); background: rgba(37,99,235,0.05); }

/* === Viewer === */
#viewer {
  background: var(--bg);
  overflow: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  position: relative;
}
#page-container {
  position: relative;
  box-shadow: var(--shadow);
  background: #fff;
  display: inline-block;
  margin: 0 auto;
}
#page-image { display: block; }
#highlight-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
}
.highlight-rect {
  position: absolute;
  background: var(--highlight);
  border-radius: 2px;
  mix-blend-mode: multiply;
}
.highlight-rect.current {
  background: var(--highlight-current);
  z-index: 2;
}

/* In-page search bar */
#inpage-search-bar {
  position: fixed;
  top: 52px;
  right: 20px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: var(--shadow);
  z-index: 160;
}
#inpage-search-bar input {
  width: 200px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
#inpage-search-bar input:focus { border-color: var(--accent); }
#inpage-search-bar button {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text);
}
#inpage-search-bar button:hover { background: var(--border); }
#inpage-search-info { font-size: 12px; color: var(--text-secondary); min-width: 40px; text-align: center; }

/* Results panel (global + in-book search) */
#results-panel {
  position: fixed;
  background: var(--bg-panel);
  display: flex;
  flex-direction: column;
  z-index: 210;
  box-shadow: 0 -2px 12px rgba(0,0,0,0.15);
  /* Default: side */
  bottom: 28px;
  top: 44px;
  left: auto;
  right: 0;
  width: 380px;
  height: auto;
}
/* Bottom mode (via toggle) */
#results-panel:not(.side) {
  top: auto;
  bottom: 28px;
  left: 280px;
  right: 0;
  width: auto;
  height: 40%;
}
/* Resize handle: side = left edge (width), bottom = top edge (height) */
#results-panel-resize-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 4px;
  height: auto;
  cursor: col-resize;
  z-index: 10;
}
#results-panel:not(.side) #results-panel-resize-handle {
  top: -4px;
  left: 0;
  right: 0;
  bottom: auto;
  width: auto;
  height: 6px;
  cursor: row-resize;
}
#results-panel-header {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
  cursor: grab;
  user-select: none;
}
#results-panel-header:active { cursor: grabbing; }
#results-panel-header button {
  background: transparent;
  border: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 2px 6px;
}
#results-panel-header button:hover { color: var(--text); }
#results-panel-count { font-size: 12px; color: var(--text-secondary); margin-left: 8px; }
#results-panel-list { flex: 1; overflow-y: auto; padding: 4px 0; }

.result-item {
  padding: 10px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  transition: background .1s;
}
.result-item:hover { background: rgba(0,0,0,0.04); }
.result-item.active { background: rgba(37,99,235,0.1); }
.result-item .ri-header { display: flex; align-items: center; gap: 8px; margin-bottom: 3px; }
.result-item .ri-book { font-weight: 600; }
.result-item .ri-page { color: var(--accent); font-size: 12px; font-weight: 500; }
.result-item .ri-count { color: var(--text-secondary); font-size: 11px; }
.result-item .ri-snippet { color: var(--text-secondary); font-size: 12px; line-height: 1.4; margin-top: 2px; }
.result-item .ri-snippet mark { background: var(--highlight); color: inherit; padding: 0 1px; border-radius: 2px; }

/* === Status bar === */
#status-bar {
  grid-column: 1 / -1;
  background: var(--bg-status);
  color: var(--text-invert);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 12px;
  font-size: 12px;
  z-index: 100;
}
#status-bar span { opacity: 0.8; }

/* === Scrollbar === */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(128,128,128,0.4); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(128,128,128,0.6); }

/* === Responsive: tablets and phones === */
@media (max-width: 768px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: 44px 1fr 28px;
  }

  /* Toolbar: compact */
  #toolbar { padding: 0 8px; }
  #toolbar-title { font-size: 13px; }
  #toolbar-actions { gap: 4px; }
  #toolbar-actions button, #toolbar-actions select, #toolbar-actions input {
    padding: 3px 6px; font-size: 12px;
  }
  #toolbar-actions input { width: 48px; }
  #jump-page { width: 42px !important; }

  /* Sidebar: overlay instead of grid column */
  #sidebar {
    position: fixed;
    left: 0; top: 44px; bottom: 28px;
    width: 280px;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar.hidden { display: flex !important; }

  /* Sidebar backdrop */
  #sidebar-backdrop {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 199;
  }
  #sidebar-backdrop.show { display: block; }

  /* Sidebar toggle: visible hamburger */
  #sidebar-toggle-btn {
    left: 0;
    border-radius: 0 10px 10px 0;
  }

  /* Viewer: full width */
  #viewer { padding: 10px; }
  #page-container { max-width: 100%; }
  #page-image { max-width: 100%; height: auto; }

  /* Results panel: bottom sheet */
  #results-panel {
    left: 0; right: 0; top: auto !important; bottom: 28px;
    width: auto !important; height: 45%;
    border-radius: 12px 12px 0 0;
    z-index: 210;
  }
  #results-panel.side {
    left: 0; right: 0; top: auto !important; bottom: 28px;
    width: auto !important; height: 45%;
  }

  /* In-page search: bottom of toolbar area */
  #inpage-search-bar {
    top: 52px; right: 8px; left: 8px;
    padding: 4px 8px;
  }
  #inpage-search-bar input { flex: 1; width: auto; }

  /* Fullscreen page viewing */
  body.fullscreen-view #toolbar { display: none; }
  body.fullscreen-view #status-bar { display: none; }
  body.fullscreen-view #sidebar { display: none !important; }
  body.fullscreen-view #viewer { grid-row: 1 / -1; padding: 0; }
}

/* Phone-specific */
@media (max-width: 480px) {
  #sidebar { width: 260px; }
  #toolbar-title { font-size: 12px; }
  #toolbar-actions { gap: 2px; }
  #toolbar-actions button { padding: 2px 4px; font-size: 11px; }

  /* Double-tap page edge to navigate */
  #viewer { padding: 4px; }
}
