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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #525659;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
}

/* ========== Floating Toolbar ========== */

#toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 8px 12px;
  background: transparent;
  color: #fff;
  z-index: 100;
  pointer-events: none;
}

#toolbar-left,
#toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  pointer-events: auto;
}

#page-indicator {
  display: inline-block;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.7);
  font-variant-numeric: tabular-nums;
  background: rgba(30, 30, 30, 0.35);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 6px 14px;
  border-radius: 10px;
  opacity: 0.75;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: rgba(30, 30, 30, 0.35);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: none;
  border-radius: 10px;
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  opacity: 0.75;
}

.icon-btn:hover {
  background: rgba(30, 30, 30, 0.55);
  color: #fff;
  opacity: 1;
}

.icon-btn:active {
  background: rgba(30, 30, 30, 0.7);
  opacity: 1;
}

.icon-btn[aria-pressed="true"] {
  background: rgba(30, 30, 30, 0.55);
  color: #fff;
  opacity: 1;
}

.icon-btn svg {
  flex-shrink: 0;
}

/* ========== Settings Panel ========== */

.settings-panel {
  position: fixed;
  top: 52px;
  right: 8px;
  width: 220px;
  background: rgba(30, 30, 30, 0.8);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 8px 0;
  z-index: 99;
  opacity: 0;
  transform: translateY(-8px);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.settings-panel.open {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.settings-section {
  padding: 8px 12px;
}

.settings-section + .settings-section {
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.settings-section-title {
  font-size: 11px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.45);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.settings-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.settings-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}

.settings-action-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

#zoom-level {
  min-width: 40px;
  text-align: center;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: rgba(255, 255, 255, 0.8);
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}

.settings-option-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  background: transparent;
  border: 1px solid transparent;
  color: rgba(255, 255, 255, 0.7);
  font-size: 11px;
  padding: 8px 4px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.settings-option-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.settings-option-btn:active {
  background: rgba(255, 255, 255, 0.14);
}

.settings-option-btn[aria-pressed="true"] {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.settings-option-btn svg {
  flex-shrink: 0;
}

#autoscroll-speed {
  flex: 1;
  height: 4px;
  cursor: pointer;
  accent-color: rgba(255, 255, 255, 0.6);
}

#autoscroll-speed-value {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
  min-width: 24px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Background presets */

.settings-bg-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.bg-preset {
  display: flex;
  align-items: center;
  gap: 5px;
  background: transparent;
  border: 1px solid transparent;
  color: rgba(255, 255, 255, 0.65);
  font-size: 11px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.bg-preset:hover {
  background: rgba(255, 255, 255, 0.08);
}

.bg-preset.active {
  border-color: rgba(255, 255, 255, 0.3);
  color: #fff;
}

.bg-swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 4px;
  flex-shrink: 0;
}

/* ========== Viewport ========== */

#viewport {
  flex: 1;
  overflow: auto;
  position: relative;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

#viewport::-webkit-scrollbar {
  display: none;
}

#page-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
  gap: 12px;
  min-height: 100%;
}

.page-wrapper {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  position: relative;
}

.page-wrapper canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.page-wrapper.loading::after {
  content: 'Loading...';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #999;
  font-size: 14px;
}

/* Horizontal paged mode */

.mode-horizontal #viewport {
  overflow: hidden;
}

.mode-horizontal #page-container {
  height: 100%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-wrapper.is-hidden {
  display: none;
}

/* ========== Outline Panel ========== */

#outline-panel {
  position: fixed;
  top: 0;
  left: 0;
  width: 280px;
  height: 100%;
  background: rgba(30, 30, 30, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-right: 1px solid rgba(255, 255, 255, 0.08);
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  z-index: 200;
  display: flex;
  flex-direction: column;
}

#outline-panel.open {
  transform: translateX(0);
}

#outline-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
}

#close-outline {
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.6);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
}

#close-outline:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

#outline-tree {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.outline-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.7);
  padding: 8px 16px;
  font-size: 13px;
  cursor: pointer;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.outline-item:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.outline-item:focus {
  outline: none;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.outline-item.level-1 { padding-left: 16px; }
.outline-item.level-2 { padding-left: 28px; }
.outline-item.level-3 { padding-left: 40px; }
.outline-item.level-4 { padding-left: 52px; }
.outline-item.level-5 { padding-left: 64px; }

#outline-tree::-webkit-scrollbar {
  width: 6px;
}

#outline-tree::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.2);
}

#outline-tree::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

#outline-tree::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* ========== Text Layer ========== */

.text-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  z-index: 1;
  line-height: 1.0;
  user-select: text !important;
  -webkit-user-select: text !important;
}

.text-layer span {
  position: absolute;
  color: transparent;
  white-space: pre;
  user-select: text !important;
  -webkit-user-select: text !important;
  cursor: text;
}

.text-layer span::selection {
  background: rgba(0, 100, 255, 0.3);
}

/* ========== Text Format (Direct HTML Rendering) ========== */

.page-wrapper.text-format {
  height: auto;
  min-height: 200px;
  background: #fff;
  color: #1a1a1a;
  overflow: hidden;
}

.page-content {
  padding: 2em 2.5em;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 16px;
  line-height: 1.8;
  word-wrap: break-word;
  overflow-wrap: break-word;
  user-select: text !important;
  -webkit-user-select: text !important;
  cursor: text;
}

.page-content h1 { font-size: 1.8em; margin: 0.8em 0 0.4em; }
.page-content h2 { font-size: 1.5em; margin: 0.7em 0 0.35em; }
.page-content h3 { font-size: 1.25em; margin: 0.6em 0 0.3em; }
.page-content h4 { font-size: 1.1em; margin: 0.5em 0 0.25em; }
.page-content p { margin: 0.6em 0; }
.page-content ul, .page-content ol { margin: 0.6em 0; padding-left: 1.5em; }
.page-content blockquote {
  border-left: 3px solid #ccc;
  margin: 0.8em 0;
  padding: 0.2em 1em;
  color: #555;
}
.page-content pre {
  background: #f5f5f5;
  padding: 1em;
  border-radius: 4px;
  overflow-x: auto;
  font-size: 14px;
}
.page-content code {
  font-family: 'SF Mono', Consolas, 'Liberation Mono', Menlo, monospace;
  background: #f5f5f5;
  padding: 0.15em 0.4em;
  border-radius: 3px;
  font-size: 0.9em;
}
.page-content pre code {
  background: none;
  padding: 0;
  border-radius: 0;
}
.page-content img { max-width: 100%; height: auto; }
.page-content table { border-collapse: collapse; width: 100%; margin: 0.8em 0; }
.page-content th, .page-content td { border: 1px solid #ddd; padding: 0.5em 0.8em; text-align: left; }
.page-content th { background: #f5f5f5; font-weight: 600; }
.page-content a { color: #2563eb; text-decoration: underline; }

.page-content .txt-content {
  font-family: 'SF Mono', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
  background: transparent;
  padding: 0;
  margin: 0;
  border-radius: 0;
}

/* ========== Responsive ========== */

@media (max-width: 768px) {
  #toolbar {
    padding: 6px 8px;
  }

  .settings-panel {
    right: 4px;
    width: 200px;
  }
}

@media (max-width: 480px) {
  .settings-panel {
    right: 4px;
    left: 4px;
    width: auto;
  }

  #page-indicator {
    font-size: 12px;
  }
}
