/* ───────────────────────────────────────────
   Book Notes — Mobile-first CSS
   다크 테마 기준, 768px 이상에서 2단 레이아웃
─────────────────────────────────────────── */

/* ── 리셋 & 기본 ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0f0f1a;
  --surface:   #1e1e2e;
  --surface2:  #2a2a3e;
  --border:    #3a3a5c;
  --primary:   #a78bfa;
  --primary-d: #7c5cda;
  --text:      #e0e0e0;
  --text-muted:#9ca3af;
  --danger:    #f87171;
  --code-bg:   #161625;
  --sidebar-w: 260px;
  --header-h:  52px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  /* 모바일 최적화 */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: pan-y pinch-zoom;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── 로그인 화면 ── */
#login-screen {
  display: none;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 1.5rem;
  padding: 2rem;
  text-align: center;
}
#login-screen.active { display: flex; }

#login-screen h1 { font-size: 2rem; color: var(--primary); }
#login-screen p   { color: var(--text-muted); font-size: 0.95rem; }

.btn-google {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  background: #fff;
  color: #111;
  border: none;
  border-radius: 8px;
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.2s;
}
.btn-google:hover { opacity: 0.9; text-decoration: none; }
.btn-google svg { width: 20px; height: 20px; flex-shrink: 0; }

/* ── 앱 레이아웃 ── */
#app-screen { 
  display: none; 
  height: 100vh; 
  height: 100dvh; /* 동적 viewport height (모바일 주소창 대응) */
  flex-direction: column; 
}
#app-screen.active { display: flex; }

/* ── 헤더 ── */
#app-header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 1rem;
  flex-shrink: 0;
  z-index: 100;
}

#btn-menu {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 0.4rem;
  border-radius: 6px;
  display: flex;
  align-items: center;
}
#btn-menu:hover { background: var(--surface2); }
#btn-menu svg { width: 20px; height: 20px; }

#app-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--primary);
  flex: 1;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-sync {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
}
.btn-sync:hover { color: var(--primary); border-color: var(--primary); }
.btn-sync.syncing { opacity: 0.6; pointer-events: none; }

.btn-logout {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-logout:hover { background: #f8717120; }

/* ── 본문 영역 ── */
#app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ── 사이드바 ── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  flex-shrink: 0;
  /* 모바일: 슬라이드 오버레이 */
  position: fixed;
  top: var(--header-h);
  left: 0;
  bottom: 0;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  z-index: 50;
  will-change: transform; /* 애니메이션 최적화 */
}
#sidebar.open { transform: translateX(0); }

/* 사이드바 열렸을 때 배경 오버레이 */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  top: var(--header-h);
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
}
#sidebar-overlay.active { display: block; }

/* 사이드바 트리 */
.tree-root { padding: 0.5rem 0; }

.tree-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 1rem; /* 터치 영역 확대 */
  cursor: pointer;
  font-size: 0.88rem;
  color: var(--text-muted);
  user-select: none;
  border-radius: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 44px; /* iOS 권장 터치 크기 */
}
.tree-item:hover { background: var(--surface2); color: var(--text); }
.tree-item.active { background: var(--surface2); color: var(--primary); }

.tree-item .icon { font-size: 0.85rem; flex-shrink: 0; }
.tree-item .label { overflow: hidden; text-overflow: ellipsis; }

/* 폴더 토글 화살표 */
details > summary.tree-item {
  list-style: none;
}
details > summary.tree-item::-webkit-details-marker { display: none; }
details > summary.tree-item::before {
  content: "▶";
  font-size: 0.6rem;
  display: inline-block;
  transition: transform 0.15s;
  color: var(--text-muted);
  flex-shrink: 0;
  width: 0.75rem;
  text-align: center;
}
details[open] > summary.tree-item::before {
  transform: rotate(90deg);
}

.tree-children { /* 하위 항목 들여쓰기 */ }
.tree-children .tree-item { padding-left: calc(1rem + var(--depth, 0) * 1rem); }

.btn-clear-cache {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s, border-color 0.15s;
}
.btn-clear-cache:hover  { color: var(--danger); border-color: var(--danger); }
.btn-clear-cache:active { opacity: 0.7; }

/* ── 메인 영역 (탭 바 + 콘텐츠) ── */
#main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── 콘텐츠 영역 ── */
#content-area {
  flex: 1;
  min-height: 0; /* flex 컨테이너 안에서 overflow-y 스크롤이 동작하도록 */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* iOS 모멘텀 스크롤 */
  overscroll-behavior-y: contain; /* 과도한 스크롤 방지 */
  padding: 1.5rem 1rem;
}

/* 환영 메시지 */
#welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 1rem;
  color: var(--text-muted);
  text-align: center;
}
#welcome h2 { color: var(--primary); font-size: 1.3rem; }

/* ── 마크다운 렌더링 ── */
#doc-view { max-width: 800px; margin: 0 auto; }

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  margin-top: 1.6em;
  margin-bottom: 0.5em;
  color: var(--text);
  line-height: 1.3;
}
.markdown-body h1 { font-size: 1.7rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
.markdown-body h2 { font-size: 1.35rem; border-bottom: 1px solid var(--border); padding-bottom: 0.2em; }
.markdown-body h3 { font-size: 1.1rem; }

.markdown-body p { margin: 0.75em 0; }

.markdown-body ul,
.markdown-body ol { margin: 0.5em 0 0.5em 1.5em; }
.markdown-body li { margin: 0.25em 0; }

.markdown-body blockquote {
  border-left: 3px solid var(--primary);
  padding: 0.4em 1em;
  margin: 0.75em 0;
  color: var(--text-muted);
  background: var(--surface);
  border-radius: 0 6px 6px 0;
}

.markdown-body code {
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  font-size: 0.88em;
  font-family: "JetBrains Mono", "Fira Code", Consolas, monospace;
  color: #e879f9;
}

.markdown-body pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1em;
  overflow-x: auto;
  margin: 1em 0;
}
.markdown-body pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: 0.9em;
}

.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 1em 0;
  font-size: 0.9em;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 0.5em 0.8em;
  text-align: left;
}
.markdown-body th { background: var(--surface2); color: var(--text); }

.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5em 0;
}

.markdown-body a {
  color: var(--primary);
  text-decoration: none;
  transition: opacity 0.15s;
}
.markdown-body a:hover {
  opacity: 0.75;
  text-decoration: underline;
}

.markdown-body img { max-width: 100%; border-radius: 6px; }

/* ── 로딩 스피너 ── */
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── 토스트 알림 ── */
#toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 0.6rem 1.2rem;
  font-size: 0.88rem;
  z-index: 200;
  transition: transform 0.3s ease;
  pointer-events: none;
}
#toast.show { transform: translateX(-50%) translateY(0); }

/* ── 사이드바 탭 ── */
.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-tab {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  padding: 0.75rem 0; /* 터치 영역 확대 */
  font-size: 0.88rem;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s, border-color 0.15s;
  min-height: 44px; /* iOS 권장 터치 크기 */
}
.sidebar-tab:hover { color: var(--text); }
.sidebar-tab.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ── 열린 문서 탭 바 ── */
.doc-tabs {
  display: flex;
  overflow-x: auto;
  flex-shrink: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  scrollbar-width: none;
}
.doc-tabs::-webkit-scrollbar { display: none; }

.doc-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  background: var(--surface);
  border: none;
  border-right: 1px solid var(--border);
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  padding: 0.45rem 0.5rem 0.45rem 0.9rem;
  font-size: 0.82rem;
  cursor: pointer;
  white-space: nowrap;
  max-width: 160px;
  min-width: 80px;
  flex-shrink: 0;
  touch-action: manipulation; /* 모바일 300ms 딜레이 제거 */
  transition: color 0.15s, background 0.15s, border-bottom-color 0.15s;
}
.doc-tab:hover { background: var(--surface2); color: var(--text); }
.doc-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: var(--surface2);
}

.doc-tab-label {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
.doc-tab-close {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  font-size: 1rem;
  line-height: 1;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}
.doc-tab-close:hover {
  background: var(--border);
  color: var(--text);
}

/* ── 검색 UI ── */
.search-box {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}
.search-box input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.88rem;
  padding: 0.45rem 0.7rem;
  outline: none;
  transition: border-color 0.15s;
}
.search-box input::placeholder { color: var(--text-muted); }
.search-box input:focus { border-color: var(--primary); }
.search-box button {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0.45rem 0.8rem;
  font-size: 0.88rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}
.search-box button:hover { background: var(--primary-d); }

.btn-reindex {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 0.3rem 0.6rem;
  font-size: 0.78rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  width: 100%;
  margin-bottom: 0.25rem;
}
.btn-reindex:hover { color: var(--primary); border-color: var(--primary); }
.btn-reindex:disabled { opacity: 0.5; pointer-events: none; }

.search-answer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.9rem 1rem;
  font-size: 0.88rem;
  line-height: 1.65;
  margin-bottom: 0.75rem;
}
/* 검색 답변 내 마크다운 폰트 크기 조정 */
.search-answer h1 { font-size: 1.1rem; }
.search-answer h2 { font-size: 1rem; }
.search-answer h3 { font-size: 0.95rem; }

.search-sources {
  font-size: 0.82rem;
}

/* ── Frontmatter collapsible ── */
.frontmatter-block {
  margin-bottom: 0;
}

.frontmatter-summary {
  font-size: 0.78rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  padding: 0.5rem 0; /* 터치 영역 확대 */
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-height: 44px; /* iOS 권장 터치 크기 */
}
.frontmatter-summary::-webkit-details-marker {
  display: none; /* 브라우저 기본 마커 숨김 */
}
.frontmatter-summary::marker {
  display: none; /* 표준 마커 숨김 */
}
.frontmatter-summary::before {
  content: "▶";
  font-size: 0.7rem;
  transition: transform 0.15s;
  display: inline-block; /* transform 적용을 위해 필요 */
}
details[open] .frontmatter-summary::before {
  transform: rotate(90deg);
}
.frontmatter-summary:hover { color: var(--primary); }
/* 모바일 터치 피드백 */
.frontmatter-summary:active { 
  color: var(--primary); 
  opacity: 0.8;
}

.frontmatter-pre {
  margin: 0.4rem 0 0;
  padding: 0.6rem 0.8rem;
  background: var(--surface2);
  border-radius: 6px;
  font-size: 0.78rem;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid var(--border);
}

.frontmatter-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.75rem 0 1.25rem;
}

.sources-section {
  margin-top: 0.6rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  font-size: 0.82rem;
  line-height: 1.6;
  color: var(--text-muted);
  word-break: break-word;
}

.sources-label {
  font-weight: 600;
  color: var(--text-muted);
}

.source-link {
  color: var(--primary);
  text-decoration: none;
  transition: opacity 0.15s;
}
.source-link:hover {
  opacity: 0.75;
  text-decoration: underline;
}

/* ── 데스크톱 (768px+) ── */
@media (min-width: 768px) {
  #btn-menu { display: none; }
  #sidebar-overlay { display: none !important; }

  #sidebar {
    position: static;
    transform: none !important;
    display: block;
  }

  #content-area { padding: 2rem 2.5rem; }
}
