/* Lexica Ultralegible @font-face declarations */
@font-face {
  font-family: 'Lexica Ultralegible';
  src: url('../fonts/lexica/LexicaUltralegible-Regular.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Lexica Ultralegible';
  src: url('../fonts/lexica/LexicaUltralegible-Bold.woff2') format('woff2');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Lexica Ultralegible';
  src: url('../fonts/lexica/LexicaUltralegible-Italic.woff2') format('woff2');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: 'Lexica Ultralegible';
  src: url('../fonts/lexica/LexicaUltralegible-BoldItalic.woff2') format('woff2');
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

:root {
  --primary: #00BA83;
  --primary-hover: #009E6F;
  --primary-subtle: #E8F8F3;
  --text-main: #0F172A;
  --text-muted: #64748B;
  --bg-body: #FFFFFF;
  --bg-sidebar: #F8FAFC;
  --border-color: #E2E8F0;
  --sidebar-width: 320px;
  --header-height: 48px;
  --bottom-nav-height: 56px;
  --font-reader: 'Lexica Ultralegible', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* Theme Presets */
[data-theme="sepia"] {
  --bg-body: #FBF0D9;
  --bg-sidebar: #F3E5C8;
  --text-main: #45321E;
  --text-muted: #7E6346;
  --border-color: #E3D1B5;
  --primary: #945B1C;
  --primary-subtle: #EFE0C2;
}

[data-theme="dark"] {
  --bg-body: #0F172A;
  --bg-sidebar: #1E293B;
  --text-main: #F1F5F9;
  --text-muted: #94A3B8;
  --border-color: #334155;
  --primary: #10B981;
  --primary-subtle: #1E3A34;
}

[data-theme="oled"] {
  --bg-body: #000000;
  --bg-sidebar: #0A0A0A;
  --text-main: #E2E8F0;
  --text-muted: #8892B0;
  --border-color: #222222;
  --primary: #00E161;
  --primary-subtle: #082915;
}

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

body {
  font-family: var(--font-reader);
  background: var(--bg-body);
  color: var(--text-main);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* Top Reader Header */
.reader-header {
  height: var(--header-height);
  background: var(--bg-body);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 40;
  flex-shrink: 0;
}

.reader-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.btn-icon {
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-main);
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.btn-icon:hover {
  background: var(--primary-subtle);
  border-color: var(--primary);
  color: var(--primary);
}

.course-title-label {
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reader-header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.chapter-badge-header {
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-subtle);
  padding: 3px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

/* Main Container */
.reader-container {
  display: flex;
  flex: 1;
  height: calc(100vh - var(--header-height) - var(--bottom-nav-height));
  position: relative;
  overflow: hidden;
}

/* Sidebar TOC */
.reader-sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100%;
  flex-shrink: 0;
  transition: transform 0.25s ease;
  z-index: 30;
}

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

.sidebar-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.chapter-search-box {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-color);
}

.chapter-search-input {
  width: 100%;
  background: var(--bg-body);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text-main);
  outline: none;
}

.chapter-search-input:focus {
  border-color: var(--primary);
}

.chapter-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 6px;
}

.chapter-item {
  margin-bottom: 2px;
}

.chapter-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: 6px;
  text-decoration: none;
  font-size: 13px;
  color: var(--text-main);
  line-height: 1.35;
  transition: all 0.15s ease;
  gap: 8px;
}

.chapter-link:hover {
  background: rgba(0, 186, 131, 0.08);
  color: var(--primary);
}

.chapter-link.active {
  background: var(--primary);
  color: #FFFFFF !important;
  font-weight: 600;
}

.chapter-badge {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--border-color);
  color: var(--text-muted);
  flex-shrink: 0;
}

.chapter-link.active .chapter-badge {
  background: rgba(255, 255, 255, 0.3);
  color: #FFFFFF;
}

/* Reading View */
.reader-content-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg-body);
  position: relative;
  overflow: hidden;
}

.reading-progress-bar {
  height: 3px;
  background: var(--border-color);
  width: 100%;
  flex-shrink: 0;
}

.reading-progress-fill {
  height: 100%;
  background: var(--primary);
  width: 0%;
  transition: width 0.2s ease;
}

.content-frame-container {
  flex: 1;
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.chapter-iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
  background: transparent;
}

/* Bottom Reader Navigation Bar */
.reader-bottom-nav {
  height: var(--bottom-nav-height);
  background: var(--bg-body);
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 0 8px;
  flex-shrink: 0;
  z-index: 50;
}

.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 600;
  cursor: pointer;
  min-width: 52px;
  height: 48px;
  border-radius: 8px;
  gap: 3px;
  transition: all 0.15s ease;
  user-select: none;
}

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

.nav-btn.active {
  color: var(--primary);
}

.nav-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.nav-btn svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
}

/* Settings Sheet / Popover */
.settings-sheet-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 90;
}

.settings-sheet-backdrop.active {
  display: block;
}

.settings-sheet {
  position: fixed;
  bottom: calc(var(--bottom-nav-height) + 8px);
  right: 16px;
  width: 360px;
  max-width: calc(100vw - 32px);
  background: var(--bg-body);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  z-index: 100;
  padding: 18px;
  display: none;
  flex-direction: column;
  gap: 16px;
  max-height: 80vh;
  overflow-y: auto;
}

.settings-sheet.open {
  display: flex;
  animation: slideUp 0.2s ease;
}

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

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

.settings-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-close-settings {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  border-radius: 6px;
}

.btn-close-settings:hover {
  background: var(--border-color);
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Font Family Selection */
.font-options {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.font-btn {
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--bg-body);
  color: var(--text-main);
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s ease;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.font-btn .sub {
  font-size: 10px;
  color: var(--text-muted);
}

.font-btn.active {
  border-color: var(--primary);
  background: var(--primary-subtle);
  color: var(--primary);
  font-weight: 700;
}

/* Font Size Controls */
.font-size-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 4px 6px;
}

.btn-font-size {
  border: none;
  background: var(--bg-body);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  width: 36px;
  height: 32px;
  font-weight: 700;
  cursor: pointer;
  color: var(--text-main);
}

.font-size-display {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-main);
}

/* Segmented Buttons */
.segmented-control {
  display: flex;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 3px;
  gap: 3px;
}

.seg-btn {
  flex: 1;
  padding: 6px 4px;
  border: none;
  background: transparent;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.seg-btn.active {
  background: var(--bg-body);
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Theme Palette Chips */
.theme-chips {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.theme-chip {
  padding: 8px 4px;
  border-radius: 8px;
  border: 2px solid var(--border-color);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}

.theme-chip.active {
  border-color: var(--primary);
  transform: scale(1.03);
}

.theme-chip[data-t="light"] { background: #FFFFFF; color: #0F172A; }
.theme-chip[data-t="sepia"] { background: #FBF0D9; color: #5F4B32; }
.theme-chip[data-t="dark"] { background: #0F172A; color: #F1F5F9; }
.theme-chip[data-t="oled"] { background: #000000; color: #E2E8F0; }

/* Mobile Adjustments */
@media (max-width: 768px) {
  .reader-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 85%;
    max-width: 320px;
    box-shadow: 10px 0 25px rgba(0, 0, 0, 0.25);
    transform: translateX(-100%);
    z-index: 80;
  }

  .reader-sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 70;
  }

  .sidebar-backdrop.active {
    display: block;
  }

  .settings-sheet {
    left: 8px;
    right: 8px;
    width: auto;
    bottom: calc(var(--bottom-nav-height) + 6px);
  }
}
