/* =====================================================
   전경 (典經) 오디오 리더
   ===================================================== */

/* ── 폰트 ── */
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@300;400;500;700&family=Noto+Serif+KR:wght@400;600;700&display=swap');

/* ── 테마 변수 (라이트) ── */
:root {
  --bg:             #F8F3EA;
  --bg2:            #EFE7D8;
  --surface:        #FFFFFF;
  --surface2:       #FAF6EF;
  --border:         #E0D4BF;
  --text:           #1C1508;
  --text2:          #6B5C46;
  --accent:         #9B6F1F;
  --accent-hover:   #C08A2A;
  --gold:           #D4A930;
  --gold-glow:      rgba(212, 169, 48, 0.20);
  --playing-bg:     rgba(212, 169, 48, 0.09);
  --playing-border: #D4A930;
  --shadow-xs:      0 1px 2px rgba(0,0,0,0.06);
  --shadow-sm:      0 2px 8px rgba(0,0,0,0.08);
  --shadow-md:      0 4px 20px rgba(0,0,0,0.10);
  --radius:         14px;
  --radius-sm:      8px;
  --header-h:       56px;
  --nav-h:          46px;
  --player-h:       108px;
  --font-size:      1rem;
  --easing:         cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── 테마 변수 (다크) ── */
[data-theme="dark"] {
  --bg:             #100C06;
  --bg2:            #180E06;
  --surface:        #1C1409;
  --surface2:       #231A0E;
  --border:         #2E2214;
  --text:           #EDE2CA;
  --text2:          #9B8B72;
  --accent:         #D4A930;
  --accent-hover:   #F0C848;
  --gold:           #D4A930;
  --gold-glow:      rgba(212, 169, 48, 0.15);
  --playing-bg:     rgba(212, 169, 48, 0.09);
  --playing-border: #D4A930;
  --shadow-xs:      0 1px 2px rgba(0,0,0,0.3);
  --shadow-sm:      0 2px 8px rgba(0,0,0,0.4);
  --shadow-md:      0 4px 20px rgba(0,0,0,0.5);
}

/* ── 리셋 ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; font-size: var(--font-size); }

body {
  font-family: 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.8;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }
select { font-family: inherit; }

/* ── 앱 레이아웃 ── */
#app {
  max-width: 760px;
  margin: 0 auto;
  min-height: 100dvh;
}

/* ============================================
   헤더
   ============================================ */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-xs);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  overflow: hidden;
  transition: height 0.4s var(--easing), opacity 0.4s var(--easing);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 18px;
}

.header-title {
  display: flex;
  align-items: baseline;
  gap: 9px;
}

.title-kr {
  font-family: 'Noto Serif KR', serif;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: var(--accent);
}

.title-hanja {
  font-family: 'Noto Serif KR', serif;
  font-size: 0.72rem;
  color: var(--text2);
  letter-spacing: 0.12em;
}

.header-actions {
  display: flex;
  gap: 6px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: var(--surface2);
  border-radius: 50%;
  cursor: pointer;
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s var(--easing), color 0.2s var(--easing);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--border); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

[data-theme="light"] .icon-sun  { display: none; }
[data-theme="dark"]  .icon-moon { display: none; }

/* ============================================
   편(篇) 탭 네비게이션
   ============================================ */
.book-nav {
  position: sticky;
  top: var(--header-h);
  z-index: 99;
  height: var(--nav-h);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  transition: height 0.4s var(--easing), opacity 0.4s var(--easing);
}
.book-nav::-webkit-scrollbar { display: none; }

.book-tab {
  flex-shrink: 0;
  padding: 0 18px;
  height: 100%;
  border: none;
  background: transparent;
  color: var(--text2);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.2s, border-color 0.2s;
  white-space: nowrap;
  letter-spacing: 0.04em;
}
.book-tab:hover { color: var(--text); }
.book-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 700;
}

/* ============================================
   장(章) 카드 목록
   ============================================ */
.chapter-list {
  padding: 16px 14px calc(var(--player-h) + 24px);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ── 개별 카드 ── */
.chapter-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-xs);
  overflow: hidden;
  transition: box-shadow 0.25s var(--easing), border-color 0.25s var(--easing);
}
.chapter-card.is-open  { box-shadow: var(--shadow-sm); }
.chapter-card.is-current {
  border-color: var(--playing-border);
  box-shadow: 0 0 0 1px var(--playing-border), var(--shadow-sm);
}

/* ── 카드 헤더 ── */
.chapter-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 15px;
  cursor: pointer;
  user-select: none;
  transition: background 0.18s;
}
.chapter-header:hover { background: var(--surface2); }

.chapter-num-badge {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--bg2);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Noto Serif KR', serif;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text2);
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.chapter-card.is-current .chapter-num-badge {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.chapter-title-wrap { flex: 1; min-width: 0; }

.chapter-title {
  font-family: 'Noto Serif KR', serif;
  font-size: 0.98rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.35;
}
.chapter-card.is-current .chapter-title { color: var(--accent); }

.chapter-meta {
  font-size: 0.72rem;
  color: var(--text2);
  margin-top: 2px;
}

.chapter-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* 장 전체 재생 버튼 */
.btn-chapter-play {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--accent);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.18s, color 0.18s, transform 0.1s;
  flex-shrink: 0;
}
.btn-chapter-play:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-chapter-play:active { transform: scale(0.9); }
.btn-chapter-play.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-chapter-play svg { width: 15px; height: 15px; }

/* 아코디언 화살표 */
.chevron {
  width: 18px;
  height: 18px;
  color: var(--text2);
  transition: transform 0.3s var(--easing);
  flex-shrink: 0;
}
.chapter-card.is-open .chevron { transform: rotate(180deg); }

/* ============================================
   절(節) 목록
   ============================================ */
.verse-list {
  overflow: hidden;
  max-height: 0;
  border-top: 0px solid var(--border);
  transition: border-top-width 0s;
}
.chapter-card.is-open .verse-list {
  border-top-width: 1px;
}

.verse-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 11px 15px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
  position: relative;
}
.verse-item:last-child { border-bottom: none; }
.verse-item:hover { background: var(--surface2); }

/* 현재 재생 중인 절 */
.verse-item.is-playing {
  background: var(--playing-bg);
}
.verse-item.is-playing::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--playing-border);
  border-radius: 0 2px 2px 0;
}

.verse-num {
  flex-shrink: 0;
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text2);
  min-width: 22px;
  padding-top: 3px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}
.verse-item.is-playing .verse-num { color: var(--gold); }

.verse-text {
  flex: 1;
  min-width: 0;
  font-size: var(--font-size);
  line-height: 1.85;
  color: var(--text);
  word-break: keep-all;
  overflow-wrap: break-word;
}

/* 재생 중 절 글자 하이라이트 */
.verse-item.is-playing .verse-text {
  color: var(--text);
}

/* 절 본문 내 그림 (정의도·현무경 등) */
.verse-figure {
  display: block;
  margin: 14px auto;
  text-align: center;
  max-width: 560px;
}
.verse-figure img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  border: 1px solid var(--border);
  cursor: zoom-in;
}
.verse-figure figcaption {
  font-size: 0.82rem;
  color: var(--text-muted, var(--text));
  opacity: 0.75;
  margin-top: 4px;
  font-style: italic;
}
/* 다크 모드: 흰 배경/검은 글씨 손그림을 보기 좋게 색 반전 */
[data-theme="dark"] .verse-figure img {
  filter: invert(0.92) hue-rotate(180deg);
}

/* 절 액션 버튼 공통 — 절대 위치로 레이아웃에서 분리 */
.btn-verse-play,
.btn-verse-bookmark,
.btn-verse-share {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s, color 0.15s;
  box-shadow: var(--shadow-xs);
}

/* 절 미니 재생 버튼 */
.btn-verse-play { right: 10px; }
.verse-item:hover .btn-verse-play,
.verse-item.is-playing .btn-verse-play { opacity: 1; }
.btn-verse-play:hover,
.btn-verse-play.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-verse-play svg { width: 11px; height: 11px; }

/* 재생 중 맥박 애니메이션 */
@keyframes playPulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--gold-glow); }
  50%       { box-shadow: 0 0 0 6px transparent; }
}
.verse-item.is-playing .btn-verse-play.active {
  animation: playPulse 2s ease-in-out infinite;
}

/* ============================================
   하단 플레이어 바
   ============================================ */
.player-bar {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 760px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 24px rgba(0,0,0,0.12);
  z-index: 200;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  transition: transform 0.4s var(--easing), opacity 0.4s var(--easing);
}

/* 진행 바 */
.player-progress {
  padding: 10px 16px 3px;
}

.progress-track {
  position: relative;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  cursor: pointer;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(to right, var(--accent), var(--gold));
  border-radius: 2px;
  width: 0%;
  pointer-events: none;
  transition: width 0.1s linear;
}

.progress-input {
  position: absolute;
  top: -10px;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  opacity: 0;
  cursor: pointer;
  margin: 0;
}

.progress-times {
  display: flex;
  justify-content: space-between;
  font-size: 0.68rem;
  color: var(--text2);
  margin-top: 5px;
  font-variant-numeric: tabular-nums;
}

/* 플레이어 메인 */
.player-main {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 12px 10px;
}

.player-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

#info-location,
#info-text {
  cursor: pointer;
}
#info-location:hover { text-decoration: underline; }

.info-location {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 0.03em;
}

.info-text {
  display: block;
  font-size: 0.78rem;
  color: var(--text2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

/* 컨트롤 버튼 */
.player-controls {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.ctrl-btn {
  width: 38px;
  height: 38px;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.ctrl-btn:hover { background: var(--surface2); }
.ctrl-btn svg { width: 20px; height: 20px; }

.ctrl-play {
  width: 46px;
  height: 46px;
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow-sm);
  transition: background 0.15s, transform 0.1s;
}
.ctrl-play:hover { background: var(--accent-hover); }
.ctrl-play:active { transform: scale(0.93); }

.icon-pause { display: none; }
#app.is-playing .icon-play  { display: none; }
#app.is-playing .icon-pause { display: block; }

/* 속도 선택 */
#speed-select {
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 0.72rem;
  padding: 5px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  outline: none;
  flex-shrink: 0;
  transition: border-color 0.15s;
}
#speed-select:focus { border-color: var(--accent); }

/* ============================================
   수면 타이머
   ============================================ */
.sleep-wrap {
  position: relative;
  flex-shrink: 0;
}

.sleep-btn {
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  white-space: nowrap;
  padding: 0 4px;
  min-width: 34px;
}
.sleep-btn svg { width: 15px; height: 15px; }
.sleep-btn:hover { background: var(--border); }
.sleep-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.sleep-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  min-width: 80px;
  z-index: 400;
}
.sleep-menu button {
  display: block;
  width: 100%;
  padding: 9px 16px;
  border: none;
  background: none;
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s;
}
.sleep-menu button:hover { background: var(--surface2); }

/* ============================================
   로딩 / 에러 상태
   ============================================ */
.state-message {
  text-align: center;
  padding: 64px 24px;
  color: var(--text2);
  font-size: 0.9rem;
}

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================
   스크롤바
   ============================================ */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ============================================
   반응형 — 모바일 (520px 이하)
   ============================================ */
@media (max-width: 520px) {
  :root {
    --header-h: 52px;
    --nav-h:    42px;
    --player-h: 100px;
  }

  /* 모바일 폰트 크기 (데스크탑 대비 약 7% 축소) */
  html[data-fontsize="md"]  { --font-size: 0.93rem; }
  html[data-fontsize="lg"]  { --font-size: 1.03rem; }
  html[data-fontsize="xl"]  { --font-size: 1.14rem; }
  html[data-fontsize="xxl"] { --font-size: 1.28rem; }

  body { line-height: 1.75; }

  .header-inner { padding: 0 14px; }
  .header-actions { gap: 4px; }
  .icon-btn { width: 34px; height: 34px; }
  .title-hanja { display: none; }

  .book-tab { padding: 0 14px; font-size: 0.82rem; }

  .chapter-list { padding: 10px 8px calc(var(--player-h) + 16px); gap: 7px; }
  .chapter-header { padding: 11px 12px; gap: 9px; }
  .chapter-num-badge { width: 30px; height: 30px; font-size: 0.7rem; }
  .chapter-title { font-size: 0.88rem; }
  .chapter-meta { font-size: 0.67rem; }

  .verse-item { padding: 10px 12px; gap: 8px; }
  .verse-text { line-height: 1.75; }

  .player-bar { border-radius: 0; }
  .player-progress { padding: 8px 14px 3px; }
  .player-main { padding: 2px 10px 10px; gap: 6px; }
  .ctrl-btn { width: 36px; height: 36px; }
  .ctrl-play { width: 44px; height: 44px; }
}

/* 터치 디바이스: hover 없으므로 버튼 항상 일부 노출 */
@media (hover: none) and (pointer: coarse) {
  .btn-verse-bookmark { opacity: 0.35; }
  .btn-verse-bookmark.is-bookmarked { opacity: 1; }
  .btn-verse-share { opacity: 0.35; }
  /* 절 클릭 자체가 재생이므로 플레이 버튼 숨김 */
  .btn-verse-play { display: none; }
}

@media (min-width: 521px) {
  .player-bar {
    border-radius: var(--radius) var(--radius) 0 0;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

/* 폰트 크기 단계 */
html[data-fontsize="md"]  { --font-size: 1rem; }
html[data-fontsize="lg"]  { --font-size: 1.1rem; }
html[data-fontsize="xl"]  { --font-size: 1.22rem; }
html[data-fontsize="xxl"] { --font-size: 1.38rem; }

/* ============================================
   검색 오버레이
   ============================================ */
.search-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 60px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s var(--easing);
}
.search-overlay.is-open {
  opacity: 1;
  pointer-events: all;
}

.search-panel {
  width: 100%;
  max-width: 680px;
  margin: 0 14px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  transform: translateY(-12px);
  transition: transform 0.22s var(--easing);
  max-height: calc(100dvh - 100px);
  display: flex;
  flex-direction: column;
}
.search-overlay.is-open .search-panel {
  transform: translateY(0);
}

.search-input-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.search-icon {
  width: 18px;
  height: 18px;
  color: var(--text2);
  flex-shrink: 0;
}

#search-input {
  flex: 1;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 1rem;
  color: var(--text);
  outline: none;
  min-width: 0;
}
#search-input::placeholder { color: var(--text2); }
#search-input::-webkit-search-cancel-button { display: none; }

.search-close-btn {
  border: none;
  background: var(--surface2);
  color: var(--text2);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}
.search-close-btn:hover { background: var(--border); color: var(--text); }

.search-results {
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}

.search-hint {
  padding: 32px 20px;
  text-align: center;
  color: var(--text2);
  font-size: 0.88rem;
}

.search-count {
  padding: 10px 16px 6px;
  font-size: 0.72rem;
  color: var(--text2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.search-result-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--surface2); }

.search-result-location {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.04em;
}

.search-result-text {
  font-size: 0.88rem;
  color: var(--text);
  line-height: 1.65;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ============================================
   북마크 / 공유 버튼 (절 아이템)
   ============================================ */
.btn-verse-bookmark { right: 80px; }
.btn-verse-bookmark svg { width: 11px; height: 11px; }
.verse-item:hover .btn-verse-bookmark { opacity: 1; }
.btn-verse-bookmark.is-bookmarked {
  opacity: 1;
  color: var(--gold);
  border-color: var(--gold);
  background: var(--gold-glow);
}
.btn-verse-bookmark.is-bookmarked svg { fill: var(--gold); stroke: none; }

.btn-verse-share { right: 45px; }
.btn-verse-share svg { width: 11px; height: 11px; }
.verse-item:hover .btn-verse-share { opacity: 1; }
.btn-verse-share:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-verse-share.copied { background: #27ae60; color: #fff; border-color: #27ae60; opacity: 1; }

.bookmark-remove {
  flex-shrink: 0;
  border: none;
  background: none;
  color: var(--text2);
  font-size: 0.75rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background 0.15s, color 0.15s;
}
.bookmark-remove:hover { background: var(--border); color: var(--text); }

/* ============================================
   한자 팝업
   ============================================ */
.hj {
  border-bottom: 1px dotted var(--accent);
  cursor: pointer;
  color: inherit;
}

#hanja-popup {
  position: absolute;
  z-index: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  padding: 8px 14px;
  pointer-events: none;
  white-space: nowrap;
}

#hanja-popup[hidden] { display: none; }

.hj-char {
  font-family: 'Noto Serif KR', serif;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}

.hj-eum {
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text);
}

.hj-hun {
  font-size: 0.82rem;
  color: var(--text2);
}

/* ============================================
   용어 스팬 & 팝업
   ============================================ */
.term {
  background: rgba(155, 111, 31, 0.13);
  border-radius: 3px;
  padding: 0 2px;
  cursor: pointer;
  color: var(--accent);
  font-weight: 500;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
}

#term-popup {
  position: fixed;
  inset: 0;
  z-index: 450;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

#term-popup[hidden] { display: none; }

.term-popup-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
}

.term-popup-sheet {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 22px 20px calc(20px + env(safe-area-inset-bottom));
  max-height: 65vh;
  overflow-y: auto;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.15);
}

#term-popup-close {
  position: absolute;
  top: 14px;
  right: 16px;
  width: 30px;
  height: 30px;
  border: none;
  background: var(--bg2);
  border-radius: 50%;
  cursor: pointer;
  color: var(--text2);
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.term-popup-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 12px;
  padding-right: 36px;
}

.tp-char {
  font-family: 'Noto Serif KR', serif;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}

.tp-reading {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

.tp-category {
  font-size: 0.7rem;
  background: var(--bg2);
  color: var(--text2);
  padding: 2px 9px;
  border-radius: 20px;
  margin-left: auto;
  white-space: nowrap;
}

.tp-brief {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 10px;
  line-height: 1.6;
}

.tp-detail {
  font-size: 0.86rem;
  color: var(--text2);
  line-height: 1.75;
  white-space: pre-wrap;
}

.search-result-text mark {
  background: var(--gold-glow);
  color: var(--accent);
  font-weight: 700;
  border-radius: 2px;
  padding: 0 1px;
}

/* ── 도움말 패널 ── */
.help-panel {
  max-height: 90svh;
}

.help-content {
  overflow-y: auto;
  padding: 4px 0 24px;
  -webkit-overflow-scrolling: touch;
}

.help-section {
  padding: 14px 20px 10px;
  border-bottom: 1px solid var(--border);
}

.help-section:last-child {
  border-bottom: none;
}

.help-section-title {
  font-size: .9rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
  letter-spacing: .01em;
}

.help-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.help-list li {
  font-size: .88rem;
  color: var(--text2);
  line-height: 1.6;
  padding-left: 10px;
  position: relative;
}

.help-list li::before {
  content: '·';
  position: absolute;
  left: 0;
  color: var(--accent);
  font-weight: 700;
}

.help-list b {
  color: var(--text);
  font-weight: 600;
}

.help-install-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 12px;
}

.help-install-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}

.help-install-label {
  font-size: .85rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 7px;
}

.help-install-steps {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.help-install-steps li {
  font-size: .84rem;
  color: var(--text2);
  line-height: 1.55;
}

.help-install-steps b {
  color: var(--text);
  font-weight: 600;
}

/* ============================================
   태블릿 가로 / 데스크탑 — 2단 레이아웃
   (768px 이상 landscape 또는 1024px 이상)
   ============================================ */
@media (min-width: 768px) and (orientation: landscape), (min-width: 1024px) {
  body {
    overflow: hidden;
    height: 100dvh;
  }

  #app {
    max-width: none;
    width: 100%;
    height: 100dvh;
    display: grid;
    grid-template-areas:
      "header  header"
      "sidenav content"
      "player  player";
    grid-template-columns: 200px 1fr;
    grid-template-rows: var(--header-h) minmax(0, 1fr) var(--player-h);
  }

  /* 헤더: 그리드 전체 너비 */
  .app-header {
    grid-area: header;
    position: static;
  }

  /* 편 탭: 수직 사이드바로 전환 */
  .book-nav {
    grid-area: sidenav;
    position: static;
    height: auto;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    border-bottom: none;
    border-right: 1px solid var(--border);
    padding: 10px 0;
    align-items: stretch;
    scrollbar-width: thin;
  }

  .book-tab {
    height: auto;
    padding: 11px 18px;
    text-align: left;
    border-bottom: none;
    border-left: 3px solid transparent;
    border-right: none;
    justify-content: flex-start;
    white-space: normal;
    font-size: 0.88rem;
  }

  .book-tab.active {
    border-bottom-color: transparent;
    border-left-color: var(--accent);
    background: var(--surface2);
    color: var(--accent);
  }

  /* 장 목록: 우측 콘텐츠 영역 */
  .chapter-list {
    grid-area: content;
    overflow-y: auto;
    min-height: 0;
    max-height: calc(100dvh - var(--header-h) - var(--player-h));
    padding: 16px 20px 20px;
  }

  /* flex-shrink 기본값(1)이면 카드들이 max-height에 압축되어 스크롤 불발 — 0으로 고정 */
  .chapter-card {
    flex-shrink: 0;
  }

  /* 플레이어: 고정 해제, 그리드 하단 배치 */
  .player-bar {
    grid-area: player;
    position: static;
    transform: none;
    left: auto;
    bottom: auto;
    max-width: none;
    border-radius: 0;
    border-left: none;
    border-right: none;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  /* 태블릿에서 도움말 설치 안내 가로 배치 */
  .help-install-grid {
    flex-direction: row;
  }

  .help-install-item {
    flex: 1;
  }
}

/* ============================================
   포커스 모드 (재생 중 상/하단 숨김)
   ============================================ */
#app.is-focus {
  --header-h: 0px;
  --nav-h:    0px;
  --player-h: 0px;
}

#app.is-focus .app-header {
  opacity: 0;
  pointer-events: none;
}

#app.is-focus .book-nav {
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}

#app.is-focus .player-bar {
  transform: translateX(-50%) translateY(100%);
  opacity: 0;
  pointer-events: none;
}

/* 데스크탑: 플레이어 바는 그리드에서 overflow로 숨김, 사이드바(편 탭)는 유지 */
@media (min-width: 768px) and (orientation: landscape), (min-width: 1024px) {
  #app.is-focus .player-bar {
    transform: none;
    overflow: hidden;
  }
  #app.is-focus .book-nav {
    overflow-y: auto;
    height: auto;
    opacity: 1;
    pointer-events: all;
  }
}
