@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Noto+Serif+TC:wght@400;500;700&display=swap');

:root {
  /* Premium Palette - Light */
  --bg: #faf7f0;
  --surface: #ffffff;
  --surface-trans: rgba(255, 255, 255, 0.75);
  --ink: #2d2318;
  --muted: #8a8175;
  --accent: #b84f2c;
  --accent-glow: rgba(184, 79, 44, 0.15);
  --line: #ebe5da;
  --shadow: 0 4px 20px rgba(45, 35, 24, 0.06);
  --sidebar-bg: rgba(247, 244, 238, 0.85);
  --selection: rgba(184, 79, 44, 0.2);

  /* Metrics */
  --sidebar-width: 340px;
  --content-max-width: 820px;
  --header-height: 60px;
  --border-radius: 12px;

  /* Fonts */
  --font-ui: 'Inter', system-ui, sans-serif;
  --font-body: 'Noto Serif TC', serif;
  --content-font-size: 1.125rem;

  /* Transitions */
  --ease-spring: cubic-bezier(0.175, 0.885, 0.32, 1.275);
  --ease-smooth: cubic-bezier(0.4, 0, 0.2, 1);
}

body[data-theme="dark"] {
  /* Premium Palette - Dark */
  --bg: #0d1117;
  --surface: #161b22;
  --surface-trans: rgba(22, 27, 34, 0.75);
  --ink: #e6edf3;
  --muted: #8b949e;
  --accent: #e8a84c;
  --accent-glow: rgba(232, 168, 76, 0.15);
  --line: #30363d;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  --sidebar-bg: rgba(13, 17, 23, 0.85);
  --selection: rgba(232, 168, 76, 0.25);
}

/* Base Reset & Typography */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background-color: var(--bg);
  font-family: var(--font-ui);
  line-height: 1.6;
  transition: background-color 0.4s var(--ease-smooth), color 0.4s var(--ease-smooth);
  overflow-x: hidden;
}

::selection {
  background: var(--selection);
}

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

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 4px;
  border: 2px solid transparent;
  background-clip: content-box;
  transition: background 0.2s;
}

::-webkit-scrollbar-thumb:hover {
  background-color: var(--accent);
}

/* App Layout */
.app {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Reading Progress Bar */
#reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: var(--accent);
  width: 0%;
  z-index: 100;
  transition: width 0.1s linear;
  box-shadow: 0 0 10px var(--accent);
}

/* Sidebar - Glassmorphism */
.sidebar {
  width: var(--sidebar-width);
  height: 100vh;
  height: 100dvh;
  position: fixed;
  left: 0;
  top: 0;
  background: var(--sidebar-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  z-index: 50;
  transform: translateX(0);
  transition: transform 0.4s var(--ease-spring);
  padding: 24px;
}

.app.sidebar-collapsed .sidebar {
  transform: translateX(-100%);
}

.sidebar-header {
  margin-bottom: 24px;
  animation: fadeIn 0.6s ease-out;
}

.sidebar-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.sidebar-header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--ink) 0%, var(--muted) 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0;
  font-weight: 500;
}

/* Search */
.search-wrap {
  position: relative;
  margin-bottom: 20px;
}

.search-wrap input {
  width: 100%;
  padding: 10px 14px 10px 42px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface-trans);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 0.95rem;
  transition: all 0.2s var(--ease-smooth);
  backdrop-filter: blur(10px);
}

.search-wrap svg {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  width: 18px;
  height: 18px;
  pointer-events: none;
  transition: color 0.2s;
}

.search-wrap input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
  background: var(--surface);
}

.search-wrap input:focus+svg {
  color: var(--accent);
}

/* Chapter List */
.chapter-list {
  flex: 1;
  overflow-y: auto;
  padding-right: 4px;
  mask-image: linear-gradient(to bottom, transparent, black 16px, black calc(100% - 16px), transparent);
  -webkit-mask-image: linear-gradient(to bottom, transparent, black 16px, black calc(100% - 16px), transparent);
  padding-top: 16px;
  padding-bottom: 16px;
}

.folder {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  transition: color 0.2s;
  margin-top: 12px;
}

.folder:hover {
  color: var(--ink);
}

.folder-arrow {
  transition: transform 0.3s var(--ease-spring);
  font-size: 0.75rem;
}

.folder[aria-expanded="true"] .folder-arrow {
  transform: rotate(90deg);
}

.folder-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-left: 14px;
  margin-bottom: 8px;
  overflow: hidden;
  transition: height 0.3s var(--ease-smooth);
}

.folder-group[hidden] {
  display: none;
}

.chapter-btn {
  width: 100%;
  text-align: left;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 0.95rem;
  cursor: pointer;
  transition: all 0.2s var(--ease-smooth);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.chapter-btn:hover {
  background: var(--surface-trans);
  transform: translateX(4px);
  color: var(--accent);
}

.chapter-btn.active {
  background: var(--surface);
  border-color: rgba(184, 79, 44, 0.1);
  color: var(--accent);
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.04);
}

.chapter-btn.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 10%;
  height: 80%;
  width: 3px;
  background: var(--accent);
  border-radius: 0 3px 3px 0;
}

/* Sidebar Footer Stats */
.sidebar-footer {
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}

/* Main Content */
.main-wrapper {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-width: 0;
  background: var(--bg);
  transition: margin-left 0.4s var(--ease-spring);
}

.app.sidebar-collapsed .main-wrapper {
  margin-left: 0;
}

.reader {
  max-width: var(--content-max-width);
  margin: 0 auto;
  padding: 80px 24px 120px;
  transition: max-width 0.4s var(--ease-spring);
  position: relative;
}

.reader.full-width {
  --content-max-width: 1400px;
}

/* Top Navigation Bar */
.top-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 48px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--line);
  animation: slideDown 0.6s ease-out;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chapter-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
  color: var(--ink);
  font-family: var(--font-body);
}

.nav-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s var(--ease-smooth);
}

.icon-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  stroke-width: 2;
  fill: none;
}

/* Article Content */
.content {
  font-family: var(--font-body);
  font-size: var(--content-font-size);
  color: var(--ink);
  line-height: 1.85;
  text-align: justify;
  animation: fadeIn 0.8s ease-out;
}

.content p {
  margin-bottom: 1.5em;
  text-indent: 2em;
}

.content h1,
.content h2,
.content h3 {
  font-family: var(--font-ui);
  margin-top: 2em;
  margin-bottom: 1em;
  font-weight: 700;
  line-height: 1.3;
}

.content blockquote {
  margin: 2em 0;
  padding: 1em 1.5em;
  border-left: 4px solid var(--accent);
  background: var(--surface-trans);
  border-radius: 0 12px 12px 0;
  color: var(--muted);
  font-style: italic;
}

.content img {
  max-width: 100%;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  margin: 2em 0;
}

/* Bottom Navigation */
.bottom-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 80px;
  gap: 20px;
  animation: fadeInUp 0.8s ease-out 0.2s backwards;
}

.nav-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s var(--ease-smooth);
  text-align: center;
  line-height: 1;
}

.nav-btn svg {
  flex-shrink: 0;
  display: block;
}

.nav-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.06);
}

.nav-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: var(--bg);
}

/* Mobile Sidebar Overlay */
.sidebar-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s var(--ease-smooth);
}

.app:not(.sidebar-collapsed) .sidebar-overlay {
  opacity: 1;
  pointer-events: auto;
}

/* Animations */
@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Responsive */
@media (max-width: 1024px) {
  :root {
    --sidebar-width: 280px;
  }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    box-shadow: 20px 0 40px rgba(0, 0, 0, 0.2);
  }

  .app:not(.sidebar-collapsed) .sidebar {
    transform: translateX(0);
  }

  .main-wrapper {
    margin-left: 0;
    width: 100%;
  }

  .reader {
    padding: 20px 20px 100px;
  }

  .top-nav {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }

  .nav-controls {
    display: flex;
    width: 100%;
    justify-content: space-between;
  }

  /* Adjustments for mobile touch */
  .chapter-btn {
    padding: 14px 16px;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
  }
}

/* TTS Pulse Animation */
@keyframes pulse-ring {
  0% {
    box-shadow: 0 0 0 0 rgba(184, 79, 44, 0.4);
  }

  70% {
    box-shadow: 0 0 0 10px rgba(184, 79, 44, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(184, 79, 44, 0);
  }
}

#tts-toggle.speaking {
  color: var(--accent);
  border-color: var(--accent);
  animation: pulse-ring 2s infinite;
}

/* --- Infinite Scroll: Chapter Divider --- */
.chapter-section {
  margin-bottom: 2em;
}

.chapter-divider {
  display: flex;
  align-items: center;
  gap: 20px;
  margin: 60px 0 40px;
  padding: 20px 0;
  user-select: none;
}

.divider-line {
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  opacity: 0.5;
}

.divider-title {
  font-family: var(--font-body);
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  letter-spacing: 0.05em;
  text-align: center;
}

/* --- Infinite Scroll: Loading Sentinel --- */
.load-sentinel {
  display: flex;
  justify-content: center;
  padding: 40px 0 60px;
}

.loading-spinner {
  display: flex;
  gap: 8px;
  align-items: center;
}

.loading-spinner div {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.4;
  animation: bounce-dot 1.4s infinite ease-in-out both;
}

.loading-spinner div:nth-child(1) {
  animation-delay: -0.32s;
}

.loading-spinner div:nth-child(2) {
  animation-delay: -0.16s;
}

.loading-spinner div:nth-child(3) {
  animation-delay: 0s;
}

@keyframes bounce-dot {

  0%,
  80%,
  100% {
    transform: scale(0.6);
    opacity: 0.3;
  }

  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* --- Bottom Settings Panel --- */
.bottom-panel-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  z-index: 90;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s var(--ease-smooth);
}

.bottom-panel-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.bottom-panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface-trans);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-top: 1px solid var(--line);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.2);
  z-index: 100;
  border-radius: 20px 20px 0 0;
  padding: 12px 24px 40px;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  max-height: 85vh;
  overflow-y: auto;
  display: block;
  /* Ensure it's not hidden by some other rule */
}

.bottom-panel.active {
  transform: translateY(0);
}

.bottom-panel-handle {
  width: 40px;
  height: 5px;
  background: var(--muted);
  border-radius: 3px;
  margin: 0 auto 24px;
  opacity: 0.3;
}

.panel-section {
  margin-bottom: 24px;
}

.panel-section h3 {
  font-size: 0.95rem;
  color: var(--muted);
  margin: 0 0 16px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.panel-divider {
  height: 1px;
  background: var(--line);
  margin: 24px 0;
  opacity: 0.5;
}

/* TTS Controls */
.tts-controls {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
}

.panel-btn {
  padding: 12px 20px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.panel-btn:active {
  transform: scale(0.96);
}

.tts-play-btn {
  flex: 1;
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  box-shadow: 0 4px 12px var(--accent-glow);
}

.tts-play-btn.playing {
  background: var(--surface);
  color: var(--accent);
  border-color: var(--accent);
}

.tts-settings {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.tts-rate-row {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.95rem;
  font-weight: 500;
}

.tts-rate-row input {
  flex: 1;
  accent-color: var(--accent);
}

.tts-start-from select {
  width: 100%;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 0.95rem;
  appearance: none;
  background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23b84f2c%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E");
  background-repeat: no-repeat;
  background-position: right 12px top 50%;
  background-size: 10px auto;
}

body[data-theme="dark"] .tts-start-from select {
  background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23e8a84c%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E");
}

/* Setting Rows */
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-size: 1rem;
  font-weight: 500;
}

.font-size-control {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  padding: 4px;
  border-radius: 12px;
  border: 1px solid var(--line);
}

.panel-btn.small {
  padding: 8px 14px;
  font-size: 0.9rem;
  border: none;
  background: transparent;
}

#panel-font-label {
  font-family: monospace;
  width: 40px;
  text-align: center;
}