/* ============ FONT FACE ============ */
@font-face {
  font-family: 'Thai Sans Lite';
  src: url('/fonts/thaisanslite_r1.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* ============ VARIABLES ============ */
:root {
  --bg-primary: #0a0a0a;
  --bg-secondary: #0f0f0f;
  --bg-sidebar: #0c0c0c;
  --bg-card: #141414;
  --bg-hover: #1a1a1a;
  --bg-selected: #050505;
  --bg-toolbar: #0f0f0f;
  --bg-modal: #181818;
  --text-primary: #e0e0e0;
  --text-secondary: #aaaaaa;
  --text-dim: #777777;
  --border-color: #1e1e1e;
  --accent: #aaaaaa;
  --danger: #ef4444;
  --font-ui: 'Thai Sans Lite', sans-serif;
  --font-editor: 'Thai Sans Lite', sans-serif;
  --editor-title-size: 18px;
  --editor-content-size: 16px;
  --editor-line-height: 1.8;
  --editor-indent: 2em;
  --sidebar-width: 260px;
  --toolbar-height: 44px;
  --topbar-height: 40px;
}

/* ============ RESET ============ */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-ui);
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ============ SCROLLBAR ============ */
::-webkit-scrollbar {
  width: 6px;
}

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

::-webkit-scrollbar-thumb {
  background: #2a2a3a;
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: #3a3a4a;
}

/* ============ VIEWS ============ */
#app {
  height: 100%;
  width: 100%;
  position: relative;
}

.view {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
}

.view.active {
  display: flex;
}

/* ============ TOP BAR (BOOKS VIEW) ============ */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.app-title {
  font-size: 21px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  flex: 1;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 19px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.2s ease;
  user-select: none;
}

.tab:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.tab.active {
  background: var(--text-primary);
  color: var(--bg-primary);
}

.top-actions {
  display: flex;
  gap: 4px;
  margin-left: 12px;
  flex-shrink: 0;
}

/* ============ ICON BUTTON ============ */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 50%;
  transition: all 0.15s ease;
  position: relative;
  overflow: hidden;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
}

/* ============ BOOKS LIST ============ */
.books-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 2px;
  align-content: start;
}

.book-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  cursor: pointer;
  transition: background 0.15s ease;
  user-select: none;
}

.book-item:hover {
  background: var(--bg-hover);
}

.book-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border-radius: 6px;
  flex-shrink: 0;
}

.book-icon svg {
  color: #aaa;
  width: 20px;
  height: 20px;
}

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

.book-name {
  font-size: 19px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-desc {
  font-size: 17px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

/* ============ FAB ============ */
.fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  z-index: 50;
}

.fab:hover {
  background: var(--bg-hover);
  transform: scale(1.05);
}

/* ============ EDITOR LAYOUT ============ */
.editor-layout {
  display: flex;
  height: 100%;
  width: 100%;
}

/* ============ SIDEBAR ============ */
.sidebar {
  width: var(--sidebar-width);
  min-width: 180px;
  max-width: 400px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 8px 8px 4px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 18px;
  font-weight: 500;
  color: var(--text-secondary);
  flex: 1;
}

.sidebar-actions {
  display: flex;
  gap: 2px;
}

.sidebar-actions .icon-btn {
  width: 28px;
  height: 28px;
}

.sidebar-resizer {
  width: 3px;
  cursor: col-resize;
  background: transparent;
  transition: background 0.15s;
  flex-shrink: 0;
}

.sidebar-resizer:hover,
.sidebar-resizer.active {
  background: var(--accent);
}

/* ============ FILE TREE ============ */
.tree-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 0;
}

.tree-item {
  display: flex;
  align-items: center;
  padding: 3px 8px 3px 0;
  cursor: pointer;
  font-size: 18px;
  color: var(--text-secondary);
  transition: background 0.1s ease;
  user-select: none;
  position: relative;
  white-space: nowrap;
}

.tree-item:hover {
  background: var(--bg-hover);
}

.tree-item.selected {
  background: var(--bg-selected);
  color: var(--text-primary);
}

.tree-indent {
  display: inline-flex;
  flex-shrink: 0;
}

.tree-indent-guide {
  width: 18px;
  height: 100%;
  display: inline-block;
  position: relative;
  flex-shrink: 0;
}

.tree-indent-guide::before {
  content: '';
  position: absolute;
  left: 9px;
  top: 0;
  bottom: 0;
  width: 1px;
  background: var(--border-color);
}

.tree-chevron {
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text-dim);
  transition: transform 0.15s ease;
}

.tree-chevron.expanded {
  transform: rotate(90deg);
}

.tree-chevron.hidden {
  visibility: hidden;
}

.tree-node-icon {
  margin-right: 6px;
  font-size: 14px;
  flex-shrink: 0;
}

.tree-node-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-tags {
  display: flex;
  gap: 3px;
  margin-left: auto;
  padding-left: 8px;
  flex-shrink: 0;
}

.tree-tag-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* Folder inline actions (+ button) */
.tree-folder-actions {
  display: flex;
  margin-left: auto;
  flex-shrink: 0;
}

.tree-folder-actions .icon-btn {
  width: 22px;
  height: 22px;
  opacity: 0.4;
  transition: opacity 0.15s;
}

.tree-folder-actions .icon-btn:hover {
  opacity: 1;
}

/* ============ EDITOR MAIN ============ */
.editor-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-primary);
}

.editor-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border-color);
  min-height: var(--topbar-height);
  gap: 8px;
}

.editor-topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.breadcrumb {
  font-size: 17px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.breadcrumb span {
  color: var(--text-secondary);
}

.word-count {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--text-dim);
  flex-shrink: 0;
  margin-left: 12px;
  white-space: nowrap;
}

.count-divider {
  opacity: 0.4;
}

/* Welcome screen */
.welcome-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.welcome-icon {
  font-size: 48px;
  opacity: 0.3;
}

.welcome-text {
  font-size: 21px;
  color: var(--text-dim);
}

/* Editor content */
.editor-content {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 24px;
}

.doc-title-input {
  display: block;
  width: 100%;
  max-width: 720px;
  margin: 0 auto 24px;
  padding: 8px 0;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font-editor);
  font-size: var(--editor-title-size);
  font-weight: 600;
  text-align: center;
}

.doc-title-input::placeholder {
  color: var(--text-dim);
}

.doc-body {
  flex: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font-editor) !important;
  font-size: var(--editor-content-size);
  line-height: var(--editor-line-height);
  min-height: 300px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.doc-body * {
  font-family: inherit !important;
}

.doc-body:empty::before {
  content: attr(data-placeholder);
  color: var(--text-dim);
}

.doc-body p,
.doc-body div {
  text-indent: var(--editor-indent);
  margin-bottom: 0.5em;
}

/* ============ BOTTOM TOOLBAR ============ */
.bottom-toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 12px;
  background: var(--bg-toolbar);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
  min-height: var(--toolbar-height);
}

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: 50%;
  transition: all 0.15s ease;
  position: relative;
  overflow: hidden;
}

.tool-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
}

.tool-btn.active {
  color: var(--accent);
}

.tool-divider {
  width: 1px;
  height: 20px;
  background: var(--border-color);
  margin: 0 4px;
}

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal-panel {
  background: var(--bg-modal);
  border-radius: 16px;
  width: 90%;
  max-width: 420px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: modalIn 0.2s ease;
}

.modal-panel.small {
  max-width: 360px;
}

@keyframes modalIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(10px);
  }

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

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  font-size: 20px;
  font-weight: 600;
  border-bottom: 1px solid var(--border-color);
}

.modal-body {
  padding: 16px 20px;
}

.modal-footer {
  padding: 12px 20px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  border-top: 1px solid var(--border-color);
}

/* Settings sections */
.setting-section {
  margin-bottom: 20px;
}

.setting-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.color-options {
  display: flex;
  gap: 10px;
}

.color-circle {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.2s ease;
  position: relative;
}

.color-circle:hover {
  border-color: rgba(255, 255, 255, 0.5);
}

.color-circle.active {
  border-color: rgba(255, 255, 255, 0.8);
  border-width: 2px;
}

.color-circle.active::after {
  content: '✓';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--text-primary);
}

.setting-select {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 17px;
  outline: none;
  cursor: pointer;
}

.setting-select option {
  background: var(--bg-primary);
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.setting-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  outline: none;
}

.setting-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s;
}

.setting-slider::-webkit-slider-thumb:hover {
  background: var(--text-primary);
}

.slider-value {
  font-size: 16px;
  color: var(--text-dim);
  min-width: 40px;
  text-align: right;
}

/* ============ CONTEXT MENU ============ */
.context-menu {
  position: fixed;
  background: var(--bg-modal);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 4px;
  min-width: 180px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  z-index: 200;
  animation: modalIn 0.12s ease;
}

.context-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.1s;
}

.context-item svg {
  flex-shrink: 0;
}

.context-item:hover {
  background: var(--bg-hover);
}

.context-item.danger {
  color: var(--danger);
}

.context-divider {
  height: 1px;
  background: var(--border-color);
  margin: 4px 0;
}

/* ============ BUTTONS ============ */
.btn {
  padding: 8px 18px;
  border: none;
  border-radius: 8px;
  font-family: var(--font-ui);
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-primary {
  background: var(--accent);
  color: white;
}

.btn-primary:hover {
  opacity: 0.9;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  background: var(--bg-hover);
}

/* Rename input */
.rename-input {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 18px;
  outline: none;
}

.rename-input:focus {
  border-color: var(--accent);
}

/* ============ SAVE INDICATOR ============ */
.save-indicator {
  position: fixed;
  bottom: 60px;
  right: 20px;
  padding: 6px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-size: 15px;
  color: var(--text-dim);
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 50;
  pointer-events: none;
}

.save-indicator.show {
  opacity: 1;
}

/* ============ MOBILE ============ */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 80;
    width: 280px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

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

  .sidebar-resizer {
    display: none;
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 79;
    display: none;
  }

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

  .books-list {
    grid-template-columns: repeat(2, 1fr);
  }

  .editor-content {
    padding: 16px;
  }

  .doc-title-input {
    font-size: 20px;
  }

  .bottom-toolbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 60;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
  }

  /* When mobile keyboard is visible, toolbar stays above it */
  .keyboard-visible .bottom-toolbar {
    position: fixed;
    bottom: 0;
  }
}

/* ============ TRANSITIONS ============ */
.view {
  transition: opacity 0.2s ease;
}

/* ============ INLINE EDITING ============ */
.tree-item .inline-edit {
  background: var(--bg-primary);
  border: 1px solid var(--accent);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 16px;
  padding: 1px 4px;
  outline: none;
  border-radius: 2px;
  width: 100%;
  min-width: 80px;
}

/* ============ MOVE MODAL ============ */
.move-list {
  max-height: 300px;
  overflow-y: auto;
}

.move-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  cursor: pointer;
  border-radius: 6px;
  font-size: 16px;
  color: var(--text-secondary);
  transition: background 0.1s;
}

.move-item:hover {
  background: var(--bg-hover);
}

.move-item.active {
  background: var(--bg-selected);
  color: var(--text-primary);
}

.move-item-indent {
  width: 16px;
  display: inline-block;
}

/* ============ SORT HANDLE ============ */
.tree-sort-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  cursor: grab;
  color: var(--text-dim);
  margin-right: 2px;
  flex-shrink: 0;
  opacity: 0.5;
  transition: opacity 0.15s;
}

.tree-sort-handle:hover {
  opacity: 1;
}

.tree-sort-handle:active {
  cursor: grabbing;
}

.tree-item.dragging {
  opacity: 0.4;
}

.tree-item.drag-over {
  border-top: 2px solid #888;
}

.btn-primary {
  background: #333;
  color: #e0e0e0;
}

/* ============ RIPPLE EFFECT ============ */
.ripple {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  transform: scale(0);
  animation: ripple-anim 0.5s ease-out;
  pointer-events: none;
}

@keyframes ripple-anim {
  to {
    transform: scale(3);
    opacity: 0;
  }
}

/* ============ SEARCH OVERLAY ============ */
.search-overlay {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: var(--bg-toolbar);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  padding: 6px 10px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 16px;
  outline: none;
}

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

.search-count {
  font-size: 15px;
  color: var(--text-dim);
  min-width: 40px;
  text-align: center;
  white-space: nowrap;
}

.search-highlight {
  background: rgba(200, 200, 200, 0.25);
  border-radius: 2px;
}

.search-highlight.current {
  background: rgba(200, 200, 200, 0.45);
}

/* ============ FORMAT PANEL ============ */
.format-panel {
  position: absolute;
  bottom: calc(var(--toolbar-height) + 4px);
  left: 8px;
  background: var(--bg-modal);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 4px;
  min-width: 220px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  z-index: 200;
  animation: modalIn 0.12s ease;
}

.format-panel-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 6px;
  color: var(--text-secondary);
  transition: background 0.1s;
}

.format-panel-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.format-panel-item svg {
  flex-shrink: 0;
}

.format-panel-divider {
  height: 1px;
  background: var(--border-color);
  margin: 4px 0;
}

.bottom-toolbar {
  position: relative;
}

/* ============ TOGGLE SWITCH ============ */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  cursor: pointer;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--border-color);
  border-radius: 24px;
  transition: all 0.25s ease;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  bottom: 3px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: all 0.25s ease;
}

.toggle-switch input:checked+.toggle-slider {
  background: var(--accent);
}

.toggle-switch input:checked+.toggle-slider::before {
  transform: translateX(20px);
  background: var(--text-primary);
}