/* CSS 변수 - 다크 모드 (기본) */
:root {
  --bg-primary: #1e1e1e;
  --bg-secondary: #252526;
  --bg-topbar: #111827;
  --border: #374151;
  --text-primary: #e5e7eb;
  --text-secondary: #e0e0e0;
  --text-muted: #9ca3af;
  --text-placeholder: #6b7280;
  --link: #93c5fd;
  --btn-bg: #374151;
  --btn-hover: #4b5563;
  --btn-primary: #3b82f6;
  --btn-primary-hover: #2563eb;
  --code-bg: #111827;
  --heading-border: #4b5563;
}

/* 라이트 모드 */
:root.light {
  --bg-primary: #ffffff;
  --bg-secondary: #f3f4f6;
  --bg-topbar: #f9fafb;
  --border: #d1d5db;
  --text-primary: #1f2937;
  --text-secondary: #374151;
  --text-muted: #6b7280;
  --text-placeholder: #9ca3af;
  --link: #2563eb;
  --btn-bg: #e5e7eb;
  --btn-hover: #d1d5db;
  --btn-primary: #3b82f6;
  --btn-primary-hover: #2563eb;
  --code-bg: #f3f4f6;
  --heading-border: #d1d5db;
}

/* 전체 레이아웃 */
body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

/* 상단 바 */
#topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
}

#command {
  flex: 1;
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg-topbar);
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
}

#command::placeholder {
  color: var(--text-placeholder);
}

#command:focus {
  border-color: var(--btn-primary);
}

/* 버튼 스타일 */
.btn {
  padding: 4px 12px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--btn-bg);
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
}

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

.btn-primary {
  background: var(--btn-primary);
  border-color: var(--btn-primary);
  color: #ffffff;
}

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

.hidden {
  display: none !important;
}

/* 메인 영역 */
#main {
  flex: 1;
  display: flex;
  justify-content: center;
  min-height: 0;
}

#main-inner {
  display: flex;
  width: 100%;
  max-width: 1400px;
  height: 100%;
}

/* 사이드바 공통 */
#sidebar-left,
#sidebar-right {
  width: 220px;
  flex-shrink: 0;
  background: var(--bg-secondary);
  border-color: var(--border);
  overflow-y: auto;
}

#sidebar-left {
  border-right: 1px solid var(--border);
}

#sidebar-right {
  border-left: 1px solid var(--border);
}

.sidebar-header {
  padding: 12px;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}

.sidebar-content {
  padding: 12px;
  font-size: 13px;
  color: var(--text-secondary);
}

/* 콘텐츠 영역 */
#content-area {
  flex: 1;
  display: flex;
  min-width: 0;
}

#editor {
  width: 50%;
  height: 100%;
  padding: 12px;
  box-sizing: border-box;
  border: none;
  outline: none;
  resize: none;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 14px;
  border-right: 1px solid var(--border);
}

#preview {
  width: 50%;
  height: 100%;
  padding: 24px 40px;
  box-sizing: border-box;
  overflow: auto;
  background: var(--bg-primary);
  color: var(--text-secondary);
}

/* 콘텐츠 최적 읽기 폭 */
#preview .content-wrapper {
  max-width: 800px;
  margin: 0 auto;
}

/* 읽기 모드: 프리뷰가 전체 */
#preview.fullwidth {
  width: 100%;
  border-left: none;
}

/* 미리보기 기본 마크다운 스타일 */
#preview .page-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 16px 0;
  padding: 12px 0 8px 0;
  border-bottom: 2px solid var(--heading-border);
  position: sticky;
  top: -24px;
  background: var(--bg-primary);
  z-index: 10;
}

#preview .page-title-row .page-title {
  margin: 0;
  padding: 0;
  border: none;
}

.title-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.title-btn {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

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

.title-pin-btn {
  flex-shrink: 0;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  cursor: pointer;
  font-size: 14px;
  opacity: 0.4;
  transition: opacity 0.15s, background 0.15s;
}

.title-pin-btn:hover {
  opacity: 0.8;
  background: var(--btn-bg);
}

.title-pin-btn.pinned {
  opacity: 1;
  background: var(--btn-bg);
}

#preview .page-title {
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 16px 0;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--heading-border);
  color: var(--text-primary);
}

#preview .history-timestamp {
  font-size: 13px;
  color: var(--text-muted);
  margin: -12px 0 16px 0;
}

#preview h1 {
  font-size: 22px;
  margin: 12px 0 8px;
  border-bottom: 1px solid var(--heading-border);
  padding-bottom: 4px;
}

#preview h2 {
  font-size: 18px;
  margin: 10px 0 6px;
}

#preview h3 {
  font-size: 16px;
  margin: 8px 0 4px;
}

#preview p {
  margin: 6px 0;
}

#preview ul,
#preview ol {
  padding-left: 20px;
}

#preview code {
  background: var(--code-bg);
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 13px;
}

#preview pre code {
  display: block;
  padding: 8px;
  background: var(--code-bg);
  border-radius: 4px;
  overflow-x: auto;
}

/* 링크 스타일 */
#preview a {
  color: var(--link);
  text-decoration: none;
}

#preview a:hover {
  text-decoration: underline;
}

#preview ul.doc-list {
  list-style: none;
  padding-left: 0;
}

#preview ul.doc-list li {
  padding: 4px 0;
}

#preview .doc-name {
  font-weight: 500;
}

/* 모바일 대응 */
@media (max-width: 900px) {
  #sidebar-left,
  #sidebar-right {
    display: none;
  }

  #content-area {
    flex-direction: column;
  }

  #editor,
  #preview {
    width: 100%;
    height: 50%;
  }

  #preview.fullwidth {
    height: 100%;
  }
}

/* 목차 스타일 */
.toc-content {
  padding: 8px 12px;
}

.toc-empty {
  color: var(--text-muted);
  font-size: 12px;
  margin: 0;
}

.toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc-item {
  margin: 4px 0;
  line-height: 1.4;
}

.toc-link {
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 12px;
  padding: 2px 0;
  border-radius: 3px;
  transition: background 0.15s;
}

.toc-link:hover {
  color: var(--link);
  background: var(--btn-bg);
}

.toc-number {
  color: var(--text-muted);
  font-weight: 500;
}

.toc-text {
  word-break: break-word;
}

/* 목차 제목 항목 (번호 없음) */
.toc-title-item {
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.toc-title-link {
  font-weight: 600;
  color: var(--text-primary);
}

/* 목차 들여쓰기 */
.toc-depth-0 { padding-left: 0; }
.toc-depth-1 { padding-left: 12px; }
.toc-depth-2 { padding-left: 24px; }
.toc-depth-3 { padding-left: 36px; }
.toc-depth-4 { padding-left: 48px; }
.toc-depth-5 { padding-left: 60px; }

/* 사이드바 탭 시스템 */
.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.sidebar-tab {
  flex: 1;
  padding: 8px 4px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}

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

.sidebar-tab.active {
  color: var(--text-primary);
  background: var(--bg-primary);
  border-bottom: 2px solid var(--btn-primary);
  margin-bottom: -1px;
}

.sidebar-tab-content {
  padding: 8px 12px;
}

.sidebar-empty {
  color: var(--text-muted);
  font-size: 12px;
  margin: 0;
}

/* 백링크 스타일 */
.backlink-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.backlink-item {
  margin: 4px 0;
}

.backlink-link {
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 3px;
  transition: background 0.15s;
}

.backlink-link:hover {
  color: var(--link);
  background: var(--btn-bg);
}

/* 좌측 사이드바 - 문서 목록 */
.sort-toggle-row {
  display: flex;
  gap: 2px;
  margin-bottom: 8px;
}

.sort-btn {
  flex: 1;
  padding: 4px 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.sort-btn:first-child {
  border-radius: 3px 0 0 3px;
}

.sort-btn:last-child {
  border-radius: 0 3px 3px 0;
  border-left: none;
}

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

.sort-btn.active {
  background: var(--btn-primary);
  border-color: var(--btn-primary);
  color: #ffffff;
}

.pages-filter {
  margin-bottom: 8px;
}

#pages-filter-input {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 12px;
  box-sizing: border-box;
  outline: none;
}

#pages-filter-input:focus {
  border-color: var(--btn-primary);
}

#pages-filter-input::placeholder {
  color: var(--text-placeholder);
}

.pages-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.pages-item {
  margin: 2px 0;
  display: flex;
  align-items: center;
}

.pages-link {
  flex: 1;
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 3px;
  transition: background 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pages-link:hover {
  color: var(--link);
  background: var(--btn-bg);
}

.pages-item.active .pages-link {
  color: var(--text-primary);
  background: var(--btn-bg);
  font-weight: 500;
}

.pin-btn {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 10px;
  opacity: 1;
  transition: opacity 0.15s;
}

.pin-btn:hover {
  opacity: 0.5;
}

/* 드래그앤드롭 */
.drag-handle {
  flex-shrink: 0;
  width: 16px;
  color: var(--text-muted);
  font-size: 10px;
  cursor: grab;
  user-select: none;
  opacity: 0.5;
  transition: opacity 0.15s;
}

.pages-item:hover .drag-handle {
  opacity: 1;
}

.pages-item.dragging {
  opacity: 0.5;
  background: var(--btn-bg);
  border-radius: 3px;
}

/* 삭제 버튼 */
.title-btn-delete {
  color: #ff6b6b;
  border-color: #ff6b6b;
}

.title-btn-delete:hover {
  background: #ff6b6b;
  color: #fff;
}

.light .title-btn-delete {
  color: #dc3545;
  border-color: #dc3545;
}

.light .title-btn-delete:hover {
  background: #dc3545;
  color: #fff;
}

/* 외부 링크에 🔗 아이콘 표시 */
#preview .content-wrapper a[href^="http://"]::before,
#preview .content-wrapper a[href^="https://"]::before {
  content: "🔗 ";
  opacity: 0.7;
}
