:root {
  --bg-color: #121212;
  --surface-color: #1e1e1e;
  --primary-color: #6a8ee7;
  --primary-color-rgb: 106, 142, 231;
  --text-color: #e0e0e0;
  --text-color-muted: #888;
  --font-family-sans-serif: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-family-serif: 'Noto Serif SC', 'Georgia', serif;
  --border-color: #333;
  --danger-color: #e76a6a;
}

body.theme-light {
    --bg-color: #f0f2f5;
    --surface-color: #ffffff;
    --primary-color: #3a70e7;
    --primary-color-rgb: 58, 112, 231;
    --text-color: #212121;
    --text-color-muted: #666;
    --border-color: #e0e0e0;
    --danger-color: #d32f2f;
}

body.theme-sepia {
    --bg-color: #fbf0d9;
    --surface-color: #f4e8c1;
    --primary-color: #8c5e2a;
    --primary-color-rgb: 140, 94, 42;
    --text-color: #5b4636;
    --text-color-muted: #8d7a69;
    --border-color: #dcd1b3;
    --danger-color: #a54a4a;
}

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

body {
  margin: 0;
  font-family: var(--font-family-sans-serif);
  background-color: var(--bg-color);
  color: var(--text-color);
  overflow: hidden;
  transition: background-color 0.3s, color 0.3s;
}

#root {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

button {
    font-family: inherit;
}

/* API Key Modal */
.api-key-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    backdrop-filter: blur(5px);
}

.api-key-modal {
    background-color: var(--surface-color);
    padding: 30px 40px;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    width: 90%;
    max-width: 450px;
    text-align: center;
    border: 1px solid var(--border-color);
}

.api-key-modal h2 {
    margin-top: 0;
    margin-bottom: 15px;
    color: var(--text-color);
}

.api-key-modal p {
    margin-top: 0;
    margin-bottom: 25px;
    color: var(--text-color-muted);
    line-height: 1.6;
}

.api-key-modal input {
    width: 100%;
    padding: 12px 15px;
    font-size: 1rem;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background-color: var(--bg-color);
    color: var(--text-color);
    margin-bottom: 20px;
}

.api-key-modal input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(var(--primary-color-rgb), 0.3);
}

.api-key-modal button {
    width: 100%;
    padding: 12px 20px;
    font-size: 1rem;
    font-weight: bold;
    border-radius: 6px;
    border: none;
    background-color: var(--primary-color);
    color: white;
    cursor: pointer;
    transition: background-color 0.2s;
}

.api-key-modal button:hover {
    filter: brightness(1.1);
}

/* Context Menu */
.context-menu {
    position: absolute;
    z-index: 1000;
    width: 150px;
    background-color: var(--surface-color);
    border-radius: 6px;
    padding: 5px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    border: 1px solid var(--border-color);
}

.context-menu-item {
    padding: 10px 15px;
    font-size: 0.9rem;
    color: var(--text-color);
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
    background: none;
    border: none;
    width: 100%;
    text-align: left;
}

.context-menu-item:hover {
    background-color: var(--primary-color);
    color: white;
}

/* Bookshelf View */
.bookshelf-view {
  padding: 40px;
  text-align: center;
  flex-grow: 1;
  overflow-y: auto;
}

.bookshelf-header {
    max-width: 1200px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 50px;
}

.bookshelf-header h1 {
  font-size: 2.5rem;
  margin: 0 0 10px 0;
  font-weight: 700;
  color: var(--text-color);
}

.bookshelf-header p {
  font-size: 1.1rem;
  color: var(--text-color-muted);
  margin-top: 0;
}

.api-key-prompt {
    margin-top: 20px;
    padding: 15px;
    background-color: rgba(var(--primary-color-rgb), 0.1);
    border: 1px solid rgba(var(--primary-color-rgb), 0.2);
    border-radius: 8px;
    display: inline-block;
}
.api-key-prompt p {
    margin: 0 0 10px 0;
    color: var(--text-color);
    font-size: 1rem;
}
.api-key-prompt button {
    padding: 8px 16px;
    font-size: 0.9rem;
    font-weight: bold;
    border-radius: 6px;
    border: none;
    background-color: var(--primary-color);
    color: white;
    cursor: pointer;
    transition: background-color 0.2s;
}
.api-key-prompt button:hover {
    filter: brightness(1.1);
}

.bookshelf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 30px;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

.book-card, .import-button {
  width: 160px;
  height: 230px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s, background-color 0.2s;
  padding: 15px;
  text-align: center;
  user-select: none; /* Prevent text selection on right-click */
}

.book-card {
  background-color: var(--surface-color);
  border: 1px solid var(--border-color);
  justify-content: space-between;
}

.book-card:hover, .book-card:focus-within {
  transform: translateY(-8px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.2);
  border-color: var(--primary-color);
}

.book-title {
    font-weight: bold;
    font-size: 1.2rem;
    word-break: break-word;
    color: var(--text-color);
}

.book-author {
    font-size: 0.9rem;
    color: var(--text-color-muted);
}

.import-button {
    background-color: transparent;
    border: 2px dashed var(--text-color-muted);
    color: var(--text-color-muted);
    font-size: 1.1rem;
}

.import-button:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.2);
  background-color: var(--surface-color);
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.import-icon {
  font-size: 3rem;
  line-height: 1;
  margin-bottom: 15px;
}

/* Reader View */
.reader-view {
  height: 100%;
  position: relative;
  background-color: var(--bg-color);
  overflow: hidden; /* Contain positioned children */
}

.reader-sidebar {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  z-index: 10;
  background-color: var(--surface-color);
  padding: 20px;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: transform 0.4s ease-in-out, background-color 0.3s, border-color 0.3s;
}

.reader-view.immersive .reader-sidebar,
.reader-view.card-mode-active .reader-sidebar {
  transform: translateX(-100%);
}

.back-to-shelf-btn {
    background: transparent;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    padding: 10px 15px;
    border-radius: 5px;
    cursor: pointer;
    margin-bottom: 20px;
    font-size: 1rem;
    text-align: left;
    width: 100%;
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}
.back-to-shelf-btn:hover {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    color: white;
}

.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 20px;
    flex-shrink: 0;
}

.tab-button {
    flex: 1;
    padding: 10px;
    background: none;
    border: none;
    color: var(--text-color-muted);
    cursor: pointer;
    font-size: 1rem;
    border-bottom: 2px solid transparent;
    transition: color 0.2s, border-color 0.2s;
}

.tab-button:hover {
    color: var(--text-color);
}

.tab-button.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
    font-weight: bold;
}

.sidebar-content {
    overflow-y: auto;
    flex-grow: 1;
}

.chapter-list h2, .bookmark-list h2 {
    margin-top: 0;
    font-size: 1.1rem;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-color);
    transition: border-color 0.3s, color 0.3s;
}
.bookmark-list h2 {
    border-bottom: none;
}


.chapter-list ul, .bookmark-list ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.chapter-list li {
    padding: 12px 10px;
    cursor: pointer;
    border-radius: 4px;
    font-size: 0.95rem;
    transition: background-color 0.2s, color 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-color-muted);
}

.chapter-list li:hover {
    background-color: var(--bg-color);
    color: var(--text-color);
}

.chapter-list li.active {
    background-color: var(--primary-color);
    color: white;
    font-weight: bold;
}

.bookmark-item {
    padding: 12px 10px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.9rem;
}

.bookmark-text {
    cursor: pointer;
    margin-bottom: 8px;
    color: var(--text-color);
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

.bookmark-text:hover {
    color: var(--primary-color);
}

.bookmark-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
    color: var(--text-color-muted);
}

.bookmark-meta span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding-right: 10px;
}

.delete-bookmark-btn {
    background: none;
    border: none;
    color: var(--text-color-muted);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0 5px;
    line-height: 1;
    transition: color 0.2s;
}

.delete-bookmark-btn:hover {
    color: var(--danger-color);
}

.empty-list-placeholder {
    text-align: center;
    color: var(--text-color-muted);
    font-size: 0.9rem;
    padding: 40px 10px;
}

.reader-main {
  height: 100%;
  overflow-y: auto;
  position: relative;
  margin-left: 280px;
  transition: margin-left 0.4s ease-in-out;
}

.reader-view.immersive .reader-main,
.reader-view.card-mode-active .reader-main {
    margin-left: 0;
}

.bookmark-popup {
    position: absolute;
    transform: translateX(-50%);
    z-index: 60;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    padding: 5px;
    border-radius: 20px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
    display: flex;
    gap: 5px;
}

.bookmark-popup button {
    background: transparent;
    color: var(--text-color);
    border: none;
    padding: 8px 15px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background-color 0.2s, color 0.2s;
    white-space: nowrap;
}

.bookmark-popup button:hover {
    background-color: var(--primary-color);
    color: white;
}

.reader-main article {
  max-width: 800px;
  margin: 0 auto;
  padding: 60px 40px;
}

.reader-main h1 {
  font-size: 2.2rem;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 15px;
  margin-bottom: 30px;
  color: var(--text-color);
  transition: border-color 0.3s, color 0.3s;
}

.reader-main p {
  font-size: var(--reader-font-size, 18px);
  line-height: var(--reader-line-height, 1.9);
  font-family: var(--reader-font-family, var(--font-family-sans-serif));
  text-indent: 2em;
  margin-top: 0;
  margin-bottom: 1.5em;
  color: var(--text-color);
  transition: color 0.3s, font-size 0.3s, line-height 0.3s;
}

.bookmark-highlight {
    background-color: rgba(106, 142, 231, 0.4);
    color: inherit;
    border-radius: 2px;
}

/* Reader Top Controls */
.reader-top-controls {
    position: absolute;
    top: 20px;
    right: 30px;
    z-index: 50;
    display: flex;
    gap: 10px;
    transition: opacity 0.3s ease-in-out;
}

.reader-view.immersive .reader-top-controls {
    opacity: 0;
}

.reader-view.immersive:hover .reader-top-controls {
    opacity: 1;
}

.control-button {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0;
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}

.control-button:hover {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.control-button svg {
    width: 20px;
    height: 20px;
}

/* AI Helper */
.ai-helper {
    position: relative;
}
.ai-helper-dropdown {
    position: absolute;
    top: 50px;
    right: 0;
    width: 160px;
    background-color: var(--surface-color);
    border-radius: 8px;
    padding: 5px;
    box-shadow: 0 5px 25px rgba(0,0,0,0.25);
    border: 1px solid var(--border-color);
}
.ai-helper-dropdown button {
    background: none;
    border: none;
    color: var(--text-color);
    padding: 10px 15px;
    width: 100%;
    text-align: left;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background-color 0.2s;
}
.ai-helper-dropdown button:hover {
    background-color: var(--primary-color);
    color: white;
}

/* Display Settings Menu */
.display-settings {
    position: relative;
}

.display-settings-dropdown {
    position: absolute;
    top: 50px;
    right: 0;
    width: 260px;
    background-color: var(--surface-color);
    border-radius: 8px;
    padding: 15px;
    box-shadow: 0 5px 25px rgba(0,0,0,0.25);
    border: 1px solid var(--border-color);
}

.settings-group {
    margin-bottom: 15px;
}
.settings-group:last-child {
    margin-bottom: 0;
}

.settings-label {
    font-size: 0.8rem;
    font-weight: bold;
    color: var(--text-color-muted);
    margin-bottom: 10px;
    display: block;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.theme-options, .font-options {
    display: flex;
    gap: 10px;
}

.theme-button, .font-button {
    flex: 1;
    padding: 8px 10px;
    font-size: 0.9rem;
    color: var(--text-color);
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
    background: transparent;
    border: 1px solid var(--border-color);
    width: 100%;
    text-align: center;
}

.theme-button:hover, .font-button:hover {
    background-color: var(--bg-color);
    border-color: var(--text-color);
}

.theme-button.active, .font-button.active {
    background-color: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
    font-weight: bold;
}

.font-button.serif {
    font-family: var(--font-family-serif);
}

.slider-container {
    display: flex;
    align-items: center;
    gap: 15px;
}

.slider-container .icon {
    font-size: 1.2rem;
    color: var(--text-color-muted);
}
.slider-container .icon.small {
    font-size: 0.9rem;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  outline: none;
  opacity: 0.7;
  transition: opacity .2s;
  cursor: pointer;
}

input[type="range"]:hover {
  opacity: 1;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
  border: none;
}

.encoding-select {
    width: 100%;
    padding: 8px 10px;
    font-size: 0.9rem;
    color: var(--text-color);
    background-color: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s, border-color 0.2s;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%23888' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
    background-position: right 0.5rem center;
    background-repeat: no-repeat;
    background-size: 1.5em 1.5em;
    padding-right: 2.5rem;
}

.encoding-select:focus {
    outline: none;
    border-color: var(--primary-color);
}


/* Scrollbar styles */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--surface-color);
  transition: background-color 0.3s;
}

::-webkit-scrollbar-thumb {
  background: #555;
  border-radius: 4px;
}

body.theme-light ::-webkit-scrollbar-thumb {
    background: #ccc;
}
body.theme-light ::-webkit-scrollbar-thumb:hover {
    background: #bbb;
}

::-webkit-scrollbar-thumb:hover {
  background: #777;
}

/* Card Mode */
.reader-view.card-mode-active .bookmark-popup {
    display: none;
}

.card-view {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40px;
    box-sizing: border-box;
}

.card-content {
    width: 100%;
    max-width: 700px;
    min-height: 250px;
    height: 60vh;
    max-height: 500px;
    background-color: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.12);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: background-color 0.3s, border-color 0.3s;
}

.card-header {
    padding: 15px 25px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    transition: border-color 0.3s;
}

.card-header h4 {
    margin: 0;
    font-size: 1rem;
    font-weight: normal;
    color: var(--text-color-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding-right: 15px;
}

.card-header span {
    font-size: 0.9rem;
    color: var(--text-color-muted);
    flex-shrink: 0;
}

.card-body {
    padding: 30px 40px;
    overflow-y: auto;
    flex-grow: 1;
}

.card-body p {
    font-size: var(--reader-font-size, 18px);
    line-height: var(--reader-line-height, 1.9);
    font-family: var(--reader-font-family, var(--font-family-sans-serif));
    color: var(--text-color);
    margin: 0;
    text-indent: 0; /* Override default paragraph style */
}

.card-footer {
    padding: 15px 25px;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    transition: border-color 0.3s;
}

.card-footer button {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-color);
    padding: 8px 20px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.2s ease;
}

.card-footer button:hover:not(:disabled) {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    color: white;
}

.card-footer button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Loading and Error states */
.reader-loading {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100%;
    color: var(--text-color-muted);
}

.reader-loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

.reader-error {
    padding: 60px 40px;
    text-align: center;
    max-width: 600px;
    margin: auto;
}

.reader-error h2 {
    color: var(--danger-color);
}
.reader-error p {
    color: var(--text-color-muted);
    line-height: 1.8;
}

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

/* AI Inline Results */
.chapter-summary {
    margin: 20px 0 40px 0;
    padding: 20px;
    background-color: rgba(var(--primary-color-rgb), 0.05);
    border-left: 4px solid var(--primary-color);
    border-radius: 4px;
    color: var(--text-color);
    font-size: 0.95em;
    line-height: 1.8;
}
.chapter-summary blockquote {
    margin: 0;
    padding: 0;
    white-space: pre-wrap;
}

.summary-loading, .summary-error {
    color: var(--text-color-muted);
}
.summary-error {
    color: var(--danger-color);
}

.ai-popover {
    position: absolute;
    z-index: 70;
    width: clamp(280px, 90%, 400px);
    background-color: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.25);
    display: flex;
    flex-direction: column;
    animation: fadeIn 0.2s ease-out;
}

.ai-popover-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    border-bottom: 1px solid var(--border-color);
}

.ai-popover-header h3 {
    margin: 0;
    font-size: 0.9rem;
    font-weight: bold;
}

.ai-popover-header .close-btn {
    background: none;
    border: none;
    color: var(--text-color-muted);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    padding: 0 5px;
    transition: color 0.2s;
}

.ai-popover-header .close-btn:hover {
    color: var(--text-color);
}

.ai-popover-body {
    padding: 15px;
    max-height: 200px;
    overflow-y: auto;
    font-size: 0.9rem;
    line-height: 1.7;
}

.ai-popover-body p {
    margin: 0;
    text-indent: 0;
    white-space: pre-wrap;
}


@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}