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

:root {
  --bg-page:    #F5F1EB;
  --bg-sidebar: #EBE7E0;
  --bg-surface: #FFFFFF;
  --bg-hover:   #E8E4DD;
  --bg-viewer:  #E0DBD3;
  --border:     #D8D3CA;
  --text:       #2D2B28;
  --text-sec:   #807A72;
  --text-dim:   #A8A29A;
  --accent:     #D97757;
  --accent-hover: #C4684A;
  --accent-soft: rgba(217,119,87,0.08);
  --danger:     #D44B4B;
  --radius:     12px;
  --radius-sm:  8px;
  --font:       -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  --font-mono:  'SF Mono', 'Cascadia Code', 'Fira Code', 'JetBrains Mono', monospace;
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg-page);
  color: var(--text);
  overflow: hidden;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ── Titlebar ── */
#titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 42px;
  background: var(--bg-page);
  border-bottom: 1px solid var(--border);
  padding: 0 12px;
  -webkit-app-region: drag;
  position: relative;
  z-index: 100;
  flex-shrink: 0;
}

.titlebar-drag {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
}

.titlebar-icon { color: var(--accent); }
.titlebar-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.2px;
}
.titlebar-file {
  font-size: 13px;
  color: var(--text-sec);
  margin-left: 2px;
}

.titlebar-controls {
  display: flex;
  gap: 2px;
  -webkit-app-region: no-drag;
}

.tb-btn {
  width: 34px;
  height: 30px;
  border: none;
  background: transparent;
  color: var(--text-sec);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: background 0.15s, color 0.15s;
}
.tb-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ── Welcome Screen ── */
#welcome {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: calc(100% - 42px);
  background: var(--bg-page);
  animation: fadeIn 0.4s ease;
}
#welcome.visible { display: flex; }

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

.welcome-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.welcome-logo {
  margin-bottom: 4px;
  animation: float 3s ease-in-out infinite;
}
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.welcome-content h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text);
}

.subtitle {
  color: var(--text-sec);
  font-size: 15px;
  margin-bottom: 8px;
}

.welcome-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 28px;
}

/* ── Buttons ── */
.btn-primary, .btn-secondary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 24px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--font);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover {
  background: var(--bg-hover);
}

/* ── Drop Zone ── */
.drop-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 32px 48px;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  color: var(--text-dim);
  font-size: 14px;
  transition: all 0.2s;
}
.drop-zone.drag-over {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

/* ── Viewer ── */
#viewer {
  display: none;
  flex-direction: column;
  height: calc(100% - 42px);
  animation: fadeIn 0.3s ease;
}
#viewer.visible { display: flex; }

.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 12px;
  background: var(--bg-page);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.toolbar-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-sec);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: all 0.15s;
}
.toolbar-btn:hover { background: var(--bg-hover); color: var(--text); }

.toolbar-label {
  font-size: 13px;
  color: var(--text-sec);
  padding: 0 6px;
  min-width: 42px;
  text-align: center;
  user-select: none;
}

.toolbar-spacer { flex: 1; }

/* Dictionary toggle */
.dict-toggle {
  width: auto !important;
  padding: 0 8px;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.5px;
}
.dict-toggle-label {
  font-family: var(--font);
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.5px;
}

/* ── Main layout ── */
#main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Sidebar ── */
#sidebar {
  width: 180px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  padding: 10px 8px;
}

#sidebar::-webkit-scrollbar { width: 5px; }
#sidebar::-webkit-scrollbar-track { background: transparent; }
#sidebar::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
#sidebar::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

#thumbnail-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.thumb-item {
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  padding: 4px;
  background: var(--bg-surface);
  transition: all 0.15s;
  text-align: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.thumb-item:hover {
  border-color: var(--border);
}

.thumb-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.thumb-item canvas {
  width: 100%;
  display: block;
  border-radius: 4px;
}

.thumb-label {
  font-size: 11px;
  margin-top: 4px;
  color: var(--text-dim);
  font-weight: 500;
}

.thumb-item.active .thumb-label {
  color: var(--accent);
}

/* ── Viewer container ── */
#viewer-container {
  flex: 1;
  overflow-y: auto;
  background: var(--bg-viewer);
  position: relative;
}

#viewer-container::-webkit-scrollbar { width: 8px; }
#viewer-container::-webkit-scrollbar-track { background: transparent; }
#viewer-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
#viewer-container::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

#pdf-viewer {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 0;
  gap: 20px;
}

.page-wrapper {
  position: relative;
  box-shadow: 0 1px 8px rgba(0,0,0,0.1), 0 0 1px rgba(0,0,0,0.08);
  border-radius: 4px;
  overflow: hidden;
  background: white;
}

.page-wrapper canvas {
  display: block;
}

.text-layer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  opacity: 0.25;
  line-height: 1;
}

.text-layer > span {
  color: transparent;
  position: absolute;
  white-space: pre;
  transform-origin: 0% 0%;
  cursor: text;
}

.text-layer ::selection {
  background: rgba(217, 119, 87, 0.25);
}

/* ── Dark mode ── */
body.dark {
  --bg-page:    #2F2B26;
  --bg-sidebar: #292521;
  --bg-surface: #3A3530;
  --bg-hover:   #44403A;
  --bg-viewer:  #1F1C18;
  --border:     #4A453E;
  --text:       #E8E2D9;
  --text-sec:   #A09890;
  --text-dim:   #787068;
  --accent:     #E08A6A;
  --accent-hover: #D07A5A;
  --accent-soft: rgba(224,138,106,0.1);
}

body.dark .page-wrapper canvas {
  filter: invert(0.85) hue-rotate(180deg);
}

/* ── Dictionary popup ── */
#dict-popup {
  position: fixed;
  z-index: 1000;
  max-width: 400px;
  min-width: 280px;
  max-height: 420px;
  overflow-y: auto;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(0,0,0,0.12), 0 0 1px rgba(0,0,0,0.08);
  padding: 18px;
  font-size: 14px;
  animation: fadeIn 0.15s ease;
}

#dict-popup::-webkit-scrollbar { width: 5px; }
#dict-popup::-webkit-scrollbar-track { background: transparent; }
#dict-popup::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

#dict-popup.hidden { display: none; }

#dict-close {
  position: absolute;
  top: 10px;
  right: 12px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: all 0.15s;
}

#dict-close:hover {
  background: var(--bg-hover);
  color: var(--danger);
}

.dict-word {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 2px;
  letter-spacing: -0.3px;
}

.dict-phonetic {
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.dict-audio-btn {
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 12px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-sec);
  margin-left: 8px;
  transition: all 0.15s;
  font-family: var(--font);
}

.dict-audio-btn:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.dict-pos {
  font-weight: 600;
  font-style: italic;
  margin-top: 12px;
  margin-bottom: 4px;
  color: var(--accent);
  font-size: 13px;
  text-transform: capitalize;
}

.dict-def {
  margin-left: 0;
  margin-bottom: 6px;
  line-height: 1.55;
  color: var(--text);
  padding-left: 12px;
  position: relative;
}

.dict-def::before {
  content: '';
  position: absolute;
  left: 0;
  top: 8px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--text-dim);
}

.dict-example {
  margin-left: 12px;
  font-style: italic;
  color: var(--text-sec);
  font-size: 13px;
  padding-left: 10px;
  border-left: 2px solid var(--border);
  margin-bottom: 6px;
  line-height: 1.5;
}

.dict-error {
  color: var(--text-sec);
  font-style: italic;
}

.dict-loading {
  color: var(--text-dim);
}

.dict-more {
  font-size: 12px;
  color: var(--text-dim);
  font-style: italic;
  margin-top: 6px;
  padding-left: 12px;
}

.dict-source {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-dim);
  text-align: right;
  letter-spacing: 0.3px;
}

/* ── Reading History ── */
.history-section {
  width: 100%;
  max-width: 480px;
  margin-top: 32px;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.history-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-sec);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.history-clear {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: all 0.15s;
  font-family: var(--font);
}

.history-clear:hover {
  color: var(--danger);
  background: var(--bg-hover);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
}

.history-item:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.history-item-icon {
  flex-shrink: 0;
  color: var(--accent);
  opacity: 0.7;
}

.history-item-info {
  flex: 1;
  min-width: 0;
}

.history-item-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item-meta {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 1px;
}

.history-item-page {
  flex-shrink: 0;
  font-size: 12px;
  color: var(--text-sec);
  background: var(--bg-hover);
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 500;
}

.history-item-remove {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1;
  transition: all 0.15s;
  opacity: 0;
}

.history-item:hover .history-item-remove {
  opacity: 1;
}

.history-item-remove:hover {
  color: var(--danger);
  background: var(--bg-hover);
}

/* ── Sidebar overlay (mobile) ── */
#sidebar-overlay {
  display: none;
}

/* ── Mobile Responsive ── */
@media (max-width: 768px) {
  /* Titlebar */
  #titlebar { height: 44px; padding: 0 10px; }
  .titlebar-title { font-size: 13px; }
  .titlebar-file { display: none; }

  /* Welcome */
  .welcome-content h1 { font-size: 22px; }
  .subtitle { font-size: 13px; text-align: center; padding: 0 20px; }
  .drop-zone { padding: 24px 32px; font-size: 13px; }
  #welcome { height: calc(100% - 44px); overflow-y: auto; }
  .welcome-content { padding: 24px 16px; }
  .history-section { max-width: none; margin-top: 24px; }
  .history-item-remove { opacity: 1; }

  /* Viewer */
  #viewer { height: calc(100% - 44px); }

  /* Toolbar */
  .viewer-toolbar { padding: 4px 8px; gap: 1px; }
  .toolbar-btn { width: 36px; height: 36px; }
  .toolbar-label { font-size: 12px; min-width: 36px; padding: 0 3px; }

  /* Sidebar: off-canvas drawer */
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 200px;
    z-index: 500;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    padding-top: 50px;
  }

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

  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 499;
    background: rgba(0,0,0,0.4);
  }

  #sidebar-overlay.visible {
    display: block;
  }

  /* PDF viewer full width */
  #pdf-viewer { padding: 12px 0; gap: 12px; }

  .page-wrapper {
    max-width: 100%;
  }

  .page-wrapper canvas {
    max-width: 100%;
    height: auto !important;
  }

  /* Dictionary popup */
  #dict-popup {
    left: 8px !important;
    right: 8px !important;
    max-width: none;
    min-width: 0;
    bottom: 8px !important;
    top: auto !important;
    max-height: 50vh;
    border-radius: 16px;
    box-shadow: 0 -4px 24px rgba(0,0,0,0.15);
  }
}

@media (min-width: 769px) {
  /* Hide sidebar toggle on desktop */
  #btn-sidebar-toggle { display: none; }
}
