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

[hidden] { display: none !important; }

/* ============================================================
   디자인 토큰
   ============================================================ */
:root, :root[data-theme="dark"] {
  --bg: #0c0e1c;         /* indigo-tinted near-black */
  --surface: #16182c;    /* 카드/바 */
  --surface-2: #20223a;  /* 살짝 밝은 표면 */
  --border: #2e3150;
  --text: #e9eaf6;
  --text-dim: #9698c0;
  --accent: #818cf8;     /* indigo-400 */
  --accent-2: #6366f1;   /* indigo-500 */
  --on-accent: #0b0d1f;
  --highlight: rgba(129, 140, 248, 0.16);
  --word: #fbbf24;       /* 발화 단어는 따뜻한 앰버로 대비 */
  --word-bg: rgba(251, 191, 36, 0.20);
  --header-bg: rgba(12, 14, 28, 0.72);
  --shadow: rgba(0, 0, 0, 0.5);
  --scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f1f1fb;
  --surface: #ffffff;
  --surface-2: #f6f6fe;
  --border: #e4e5f5;
  --text: #1b1d33;
  --text-dim: #6a6c8c;
  --accent: #4f46e5;     /* indigo-600 */
  --accent-2: #4338ca;   /* indigo-700 */
  --on-accent: #ffffff;
  --highlight: rgba(79, 70, 229, 0.10);
  --word: #b45309;
  --word-bg: rgba(180, 83, 9, 0.14);
  --header-bg: rgba(255, 255, 255, 0.80);
  --shadow: rgba(30, 27, 75, 0.12);
  --scheme: light;
}

:root[data-theme="sepia"] {
  --bg: #efe6d3;
  --surface: #f7f0e0;
  --surface-2: #f2e9d5;
  --border: #ddceac;
  --text: #453626;
  --text-dim: #8a765a;
  --accent: #6a5acd;     /* 따뜻한 종이 위 뮤트 인디고(페리윙클) */
  --accent-2: #574bb0;
  --on-accent: #fdf6e3;
  --highlight: rgba(106, 90, 205, 0.13);
  --word: #9a3412;
  --word-bg: rgba(154, 52, 18, 0.14);
  --header-bg: rgba(247, 240, 224, 0.82);
  --shadow: rgba(75, 59, 42, 0.14);
  --scheme: light;
}

:root {
  --radius-sm: 10px;
  --radius: 14px;
  --radius-lg: 20px;
  --radius-full: 999px;
  --reader-font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  --reader-line: 1.85;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}

body {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

button { font-family: inherit; -webkit-tap-highlight-color: transparent; }

/* ============================================================
   헤더
   ============================================================ */
#header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: calc(10px + env(safe-area-inset-top)) 10px 10px;
  background: var(--header-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-left, .header-right { display: flex; align-items: center; gap: 2px; flex: 0 0 auto; }
.header-left { min-width: 36px; }

#title {
  flex: 1;
  min-width: 0;
  font-size: 15.5px;
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-full);
  transition: background .18s var(--ease), transform .1s var(--ease);
}
.icon-btn:hover { background: var(--highlight); }
.icon-btn:active { transform: scale(0.9); }
#btnHome { font-size: 28px; }

#main {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ============================================================
   서재(홈)
   ============================================================ */
.home { min-height: 100%; display: flex; flex-direction: column; }

.lib-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-dim);
  padding: 22px 20px 10px;
}

.library-list { display: flex; flex-direction: column; gap: 10px; padding: 0 16px; }

.lib-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px var(--shadow);
  cursor: pointer;
  transition: transform .14s var(--ease), box-shadow .14s var(--ease), border-color .14s var(--ease);
}
.lib-item:hover { transform: translateY(-1px); box-shadow: 0 6px 18px var(--shadow); border-color: var(--accent); }
.lib-item:active { transform: translateY(0) scale(0.995); }
.lib-item .lib-main { flex: 1; min-width: 0; }
.lib-item .lib-name {
  font-size: 15px;
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lib-item .lib-meta { font-size: 12px; color: var(--text-dim); margin-top: 4px; }
.lib-bar { height: 5px; border-radius: var(--radius-full); background: var(--border); margin-top: 9px; overflow: hidden; }
.lib-bar > i { display: block; height: 100%; border-radius: var(--radius-full); background: linear-gradient(90deg, var(--accent-2), var(--accent)); }
.lib-del {
  background: none; border: none; color: var(--text-dim);
  font-size: 17px; width: 34px; height: 34px; border-radius: var(--radius-full);
  cursor: pointer; flex: 0 0 auto; transition: background .18s var(--ease), color .18s var(--ease);
}
.lib-del:hover { background: var(--highlight); color: var(--text); }

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  flex: 1;
  text-align: center;
  color: var(--text-dim);
  font-size: 16px;
  line-height: 1.6;
  padding: 32px 24px;
}
.empty-emoji {
  font-size: 60px;
  filter: drop-shadow(0 8px 20px var(--shadow));
}

.open-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 24px 16px calc(28px + env(safe-area-inset-bottom));
  text-align: center;
}

.primary-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--on-accent);
  border: none;
  border-radius: var(--radius);
  padding: 15px 40px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  cursor: pointer;
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 40%, transparent);
  transition: transform .12s var(--ease), box-shadow .18s var(--ease), filter .18s var(--ease);
}
.primary-btn:hover { filter: brightness(1.05); box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 50%, transparent); }
.primary-btn:active { transform: scale(0.97); }
.primary-btn.wide { width: 100%; padding: 13px; font-size: 15px; }

.enc-label { font-size: 13.5px; color: var(--text-dim); }
.enc-label select { margin-left: 4px; }
.hint { font-size: 12.5px; color: var(--text-dim); max-width: 300px; line-height: 1.55; }
.version {
  font-size: 11px; color: var(--text-dim); opacity: 0.55;
  letter-spacing: 0.05em; font-weight: 600;
}

/* ============================================================
   본문 (문장)
   ============================================================ */
.sentences {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 18px 40px;
  font-size: 18px;
  line-height: var(--reader-line);
  font-family: var(--reader-font);
  letter-spacing: -0.003em;
}

.sentence {
  display: block;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  word-break: break-word;
  transition: background .2s var(--ease), box-shadow .2s var(--ease);
}
.sentence:hover { background: color-mix(in srgb, var(--highlight) 60%, transparent); }

.sentence.current {
  background: var(--highlight);
  box-shadow: inset 3px 0 0 var(--accent);
}

.sentence.para-end { margin-bottom: 16px; }

mark.word {
  background: var(--word-bg);
  color: var(--word);
  border-radius: 5px;
  padding: 0 2px;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

/* ============================================================
   하단 바 (읽기 / 재생)
   ============================================================ */
#readerBar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--surface);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 20px var(--shadow);
}

.speak-btn { padding: 13px 30px; font-size: 16px; border-radius: var(--radius); }

.font-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 15px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  min-width: 50px;
  transition: background .16s var(--ease), transform .1s var(--ease);
}
.font-btn:hover { background: var(--highlight); }
.font-btn:active { transform: scale(0.94); }

#controls {
  background: var(--surface);
  padding: 12px 16px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 20px var(--shadow);
}

.progress-row { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
#progressText {
  font-size: 11.5px; color: var(--text-dim); white-space: nowrap; min-width: 66px;
  font-variant-numeric: tabular-nums; font-weight: 600;
}

/* 커스텀 진행바 */
#progressBar {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 22px;
  background: transparent;
  cursor: pointer;
}
#progressBar::-webkit-slider-runnable-track {
  height: 5px; border-radius: var(--radius-full);
  background: var(--border);
}
#progressBar::-moz-range-track { height: 5px; border-radius: var(--radius-full); background: var(--border); }
#progressBar::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 16px; height: 16px; margin-top: -5.5px;
  border-radius: var(--radius-full);
  background: var(--accent);
  border: 2px solid var(--surface);
  box-shadow: 0 1px 4px var(--shadow);
  transition: transform .12s var(--ease);
}
#progressBar::-webkit-slider-thumb:active { transform: scale(1.25); }
#progressBar::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: var(--radius-full);
  background: var(--accent); border: 2px solid var(--surface);
}

.control-row { display: flex; align-items: center; justify-content: center; gap: 26px; margin-bottom: 10px; }
.ctrl-btn {
  background: none; border: none; color: var(--text);
  font-size: 24px; cursor: pointer;
  width: 52px; height: 52px; border-radius: var(--radius-full);
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .16s var(--ease), transform .1s var(--ease);
}
.ctrl-btn:hover { background: var(--highlight); }
.ctrl-btn:active { transform: scale(0.9); }

/* 재생 버튼 = 원형 accent (중심 요소) */
.ctrl-btn.play {
  font-size: 24px;
  width: 68px; height: 68px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--on-accent);
  box-shadow: 0 8px 22px color-mix(in srgb, var(--accent) 45%, transparent);
}
.ctrl-btn.play:hover { filter: brightness(1.06); background: linear-gradient(135deg, var(--accent), var(--accent-2)); }

.option-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px 18px; font-size: 12.5px; color: var(--text-dim); }
.option-row label { display: inline-flex; align-items: center; gap: 6px; }
.sleep-info {
  font-size: 12px; color: var(--accent); white-space: nowrap; min-width: 58px;
  text-align: right; font-variant-numeric: tabular-nums; font-weight: 700;
}

.option-row select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 13px;
  max-width: 150px;
  transition: border-color .16s var(--ease);
}
.option-row select:focus { outline: none; border-color: var(--accent); }

select { font-size: 16px; } /* iOS 자동 확대 방지 */

/* ============================================================
   오버레이 패널 (설정 · 탐색) — 슬라이드 인
   ============================================================ */
.panel {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}
.panel:not([hidden]) { animation: panelIn .28s var(--ease); }
.panel-card { flex: 1; display: flex; flex-direction: column; min-height: 0; width: 100%; }
@keyframes panelIn {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0); }
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 10px 12px 18px;
  background: var(--header-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
}
.panel-head h2 { font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.panel-close { font-size: 18px; }

.panel-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px 18px;
  padding-bottom: calc(28px + env(safe-area-inset-bottom));
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}

/* 설정 */
.setting-group { margin-bottom: 28px; }
.setting-group h3 {
  font-size: 12px; color: var(--text-dim); margin-bottom: 12px; font-weight: 700;
  letter-spacing: 0.03em; text-transform: uppercase;
}
.setting-note { font-size: 12px; color: var(--text-dim); margin-top: 10px; line-height: 1.55; }

/* 세그먼트 컨트롤 (pill) */
.seg {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px;
}
.seg-btn {
  flex: 1;
  background: none;
  color: var(--text-dim);
  border: none;
  border-radius: var(--radius-sm);
  padding: 11px 8px;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background .18s var(--ease), color .18s var(--ease), box-shadow .18s var(--ease);
}
.seg-btn:active { transform: scale(0.97); }
.seg-btn.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--on-accent);
  box-shadow: 0 3px 10px color-mix(in srgb, var(--accent) 35%, transparent);
}

/* 탐색 탭 */
.tabs { display: flex; gap: 4px; }
.tab {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 15px;
  font-weight: 650;
  padding: 8px 14px;
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: background .18s var(--ease), color .18s var(--ease);
}
.tab.active { color: var(--on-accent); background: linear-gradient(135deg, var(--accent), var(--accent-2)); }

#searchInput {
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 16px;
  font-size: 16px;
  margin-bottom: 14px;
  transition: border-color .16s var(--ease), box-shadow .16s var(--ease);
}
#searchInput:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--highlight); }

.nav-list { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  line-height: 1.5;
  transition: background .16s var(--ease);
}
.nav-item + .nav-item { border-top: 1px solid var(--border); border-radius: 0; }
.nav-item:hover { background: var(--highlight); border-radius: var(--radius-sm); }
.nav-item .nav-text { flex: 1; min-width: 0; }
.nav-item .nav-idx {
  font-size: 11px; color: var(--text-dim); flex: 0 0 auto;
  font-variant-numeric: tabular-nums;
  background: var(--surface); border: 1px solid var(--border);
  padding: 2px 7px; border-radius: var(--radius-full);
}
.nav-item .nav-del { background: none; border: none; color: var(--text-dim); font-size: 15px; padding: 4px 6px; cursor: pointer; flex: 0 0 auto; border-radius: var(--radius-full); }
.nav-item .nav-del:hover { background: var(--highlight); color: var(--text); }
.nav-item mark { background: var(--word-bg); color: var(--word); border-radius: 3px; padding: 0 1px; }
.nav-empty { color: var(--text-dim); font-size: 14px; padding: 28px 12px; text-align: center; line-height: 1.7; }
.nav-item.toc-item { font-weight: 650; }
#btnAddBookmark { margin-bottom: 14px; }

/* ============================================================
   데스크톱 (PC) 레이아웃 — ≥900px
   ============================================================ */
@media (min-width: 900px) {
  /* --- 홈(서재): 카드 그리드 --- */
  .home { max-width: 960px; width: 100%; margin: 0 auto; }
  .lib-title { padding: 28px 8px 12px; }
  .library-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
    padding: 0 8px;
  }
  .empty { padding: 60px 24px; }
  .open-row { padding-top: 32px; }

  /* --- 문서 열림: 사이드바 + 읽기 영역 그리드 --- */
  body.doc-open {
    display: grid;
    grid-template-columns: 340px minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "header  header"
      "sidebar main"
      "sidebar bars";
  }
  body.doc-open > #header { grid-area: header; }
  body.doc-open > #main { grid-area: main; }
  body.doc-open > #readerBar,
  body.doc-open > #controls { grid-area: bars; }

  /* 탐색 패널 → 좌측 상시 사이드바 */
  body.doc-open > #navPanel.docked {
    grid-area: sidebar;
    position: static;
    inset: auto;
    z-index: auto;
    border-right: 1px solid var(--border);
    background: var(--surface);
    animation: none;
    min-height: 0;
  }
  #navPanel.docked .panel-close { display: none; }
  #navPanel.docked .panel-head { padding-left: 20px; }
  #navPanel.docked .panel-body { max-width: none; padding: 14px 16px; }

  /* 읽기 영역: 더 여유로운 폭 */
  body.doc-open > #main .sentences { max-width: 780px; padding: 28px 32px 48px; font-size: 19px; }

  /* 하단 바도 읽기 컬럼 안에서 중앙 정렬 */
  #controls > *, #readerBar { max-width: 780px; margin-left: auto; margin-right: auto; }
  #readerBar { width: 100%; }

  /* 설정 패널 → 가운데 모달 카드 */
  #settingsPanel {
    align-items: center;
    justify-content: center;
    background: color-mix(in srgb, #000 48%, transparent);
  }
  #settingsPanel .panel-card {
    flex: 0 1 auto;
    width: 520px;
    max-height: 82vh;
    margin: auto;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.45);
  }
  #settingsPanel .panel-head { border-top-left-radius: var(--radius-lg); border-top-right-radius: var(--radius-lg); }

  /* 호버 여백이 있는 PC라 문장 커서 힌트 강화 */
  .sentence { transition: background .16s var(--ease); }
}

/* 아주 넓은 화면에서 사이드바 살짝 넓게 */
@media (min-width: 1280px) {
  body.doc-open { grid-template-columns: 380px minmax(0, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
