/* Reset, shell and shared controls. */

*, *::before, *::after { box-sizing: border-box; }

/* The attribute must beat the component display rules below. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  transition: background-color var(--dur-theme) ease, color var(--dur-theme) ease;
}

button, input, select, a {
  -webkit-tap-highlight-color: transparent;
}

body.view-library .reader,
body.view-reader .library { display: none; }

h1, h2, h3 { margin: 0; font-weight: 600; }

button { font-family: inherit; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* ---- 顶栏 ---- */
.topbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--sp-3);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
  z-index: 50;
}

.topbar-left, .topbar-right { display: flex; align-items: center; gap: var(--sp-1); }
.topbar-left { flex: 1; min-width: 0; }
.topbar-right, .topbar .icon-btn { flex-shrink: 0; }

.topbar-title {
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-left: var(--sp-2);
  min-width: 0;
}

#topbar-book {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 46vw;
}

.topbar-chapter {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 46vw;
}

/* ---- 按钮 ---- */
.icon-btn {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease;
}

.icon-btn:hover { background: var(--accent-soft); color: var(--text); }
.icon-btn[disabled] { color: var(--text-faint); cursor: default; background: none; }

.icon-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn-primary {
  background: var(--accent);
  color: var(--surface);
  width: 38px;
  height: 38px;
}
.icon-btn-primary svg { fill: currentColor; stroke: none; width: 16px; height: 16px; }
.icon-btn-primary:hover { background: var(--accent); color: var(--surface); filter: brightness(1.08); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  height: 34px;
  padding: 0 var(--sp-4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: border-color 120ms ease, background-color 120ms ease;
}

.btn:hover { border-color: var(--accent); color: var(--accent); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { color: #fff; filter: brightness(1.08); }

.text-btn {
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--radius);
  cursor: pointer;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.text-btn:hover { color: var(--text); background: var(--accent-soft); }

/* ---- 舞台 ---- */
.stage {
  position: absolute;
  inset: var(--topbar-h) 0 0 0;
  overflow: hidden;
}

.library { height: 100%; overflow-y: auto; }
.reader { height: 100%; }

/* ---- 抽屉 ---- */
.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  width: var(--drawer-w);
  max-width: 84vw;
  background: var(--surface);
  z-index: 60;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  transition: transform var(--dur-drawer) var(--ease-drawer);
}

.drawer-left {
  left: 0;
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  padding-top: var(--topbar-h);
}
.drawer-right {
  right: 0;
  border-left: 1px solid var(--border);
  transform: translateX(100%);
  padding-top: var(--topbar-h);
}
.drawer.is-open { transform: translateX(0); }

.drawer-head {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-3);
  border-bottom: 1px solid var(--border);
  min-height: 48px;
}
.drawer-head h2 { font-size: 14px; }

.drawer-body { flex: 1; min-height: 0; overflow-y: auto; padding: var(--sp-2) 0 var(--sp-8); }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .28);
  z-index: 55;
  animation: fade-in 160ms ease;
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

/* ---- 目录树 ---- */
.toc-node { display: block; }

.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  border-radius: 0;
  padding: var(--sp-1) var(--sp-4);
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-dim);
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease;
}

.toc-item:hover { background: var(--accent-soft); color: var(--text); }

.toc-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toc-depth-1 { padding-left: var(--sp-6); font-size: 12.5px; }
.toc-depth-2 { padding-left: var(--sp-7); font-size: 12px; }
.toc-depth-3 { padding-left: var(--sp-8); font-size: 12px; }

.toc-children { display: block; }

/* ---- 搜索结果 ---- */
.search-count {
  margin: var(--sp-2) var(--sp-4);
  font-size: 11px;
  color: var(--text-faint);
}

.search-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  padding: var(--sp-2) var(--sp-4);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.search-item:hover { background: var(--accent-soft); }

.search-chapter {
  display: block;
  font-size: 10px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 2px;
}

.search-snippet {
  display: block;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-dim);
}
.search-snippet em {
  font-style: normal;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 2px;
}

/* ---- 搜索框 ---- */
.search-box { flex: 1; }
.search-box input {
  width: 100%;
  height: 32px;
  padding: 0 var(--sp-3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
.search-box input::placeholder { color: var(--text-faint); }

/* ---- 设置 ---- */
.settings-group {
  padding: var(--sp-4);
  border-bottom: 1px solid var(--border);
}
.settings-group h3 {
  font-size: 11px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: var(--sp-3);
}

.field {
  display: block;
  margin-bottom: var(--sp-3);
  font-size: 12px;
  color: var(--text-dim);
}
.field span { display: flex; justify-content: space-between; margin-bottom: var(--sp-1); }
.field b { color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; }

.field-check { display: flex; align-items: center; gap: var(--sp-2); }
.field-check input { accent-color: var(--accent); }
.field-check span { margin: 0; }

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  height: 18px;
}

select {
  width: 100%;
  height: 30px;
  padding: 0 var(--sp-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
}

.seg { display: flex; gap: var(--sp-1); flex-wrap: wrap; }
.seg button {
  flex: 1;
  min-width: 56px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
}
.seg button.is-active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.keys { list-style: none; margin: 0; padding: 0; font-size: 12px; color: var(--text-dim); }
.keys li { margin-bottom: var(--sp-2); }
kbd {
  display: inline-block;
  min-width: 20px;
  padding: 1px 5px;
  margin-right: 3px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 11px;
  text-align: center;
}

/* ---- 提示条 ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--player-h) + var(--sp-5));
  transform: translateX(-50%);
  padding: var(--sp-2) var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
  font-size: 12px;
  z-index: 60;
  animation: toast-in 200ms var(--ease-out);
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---- 沉浸模式 ---- */
body.immersive .topbar,
body.immersive .player { display: none; }
body.immersive .stage { inset: 0; }
body.immersive .drawer { display: none; }

/* ---- 窄屏 ---- */
/* ---- 平板 ---- */
@media (max-width: 1023px) {
  .drawer { box-shadow: var(--shadow); }
  /* 中等宽度：正文列比设置里的窄 4em */
  :root { --measure: calc(var(--measure-user, 36em) - 4em); }
}

/* ---- 手机 ---- */
@media (max-width: 639px) {
  :root {
    --topbar-h: 46px;
    --player-h: 60px;
    --measure: 100%;
    --font-size: 17px;
    --para-spacing: 0.8em;
  }

  /* 触摸目标至少 44px，图标本身可以小一点 */
  .icon-btn { width: 44px; height: 44px; }
  .icon-btn svg { width: 20px; height: 20px; }

  .topbar { padding: 0 var(--sp-1); }
  #topbar-book { font-size: 14px; max-width: 42vw; }
  .topbar-chapter { font-size: 10px; max-width: 42vw; }

  /* 抽屉全屏覆盖，连顶栏一起盖住：抽屉自己有关闭按钮 */
  .drawer {
    width: 100%;
    max-width: 100%;
    border: none;
    padding-top: 0;
    z-index: 60;
  }
  .overlay { z-index: 55; }
  .drawer-head {
    min-height: 54px;
    padding: var(--sp-2) var(--sp-3);
    gap: var(--sp-3);
  }
  .drawer-head h2 { font-size: 15px; }

  /* 输入框用 16px，iOS 才不会自动放大页面 */
  .search-box input { height: 40px; font-size: 16px; }
  .toc-item {
    padding: var(--sp-2) var(--sp-4);
    font-size: 15px;
    line-height: 1.45;
  }
  .toc-depth-1 { padding-left: var(--sp-5); }
  .toc-depth-2 { padding-left: var(--sp-6); }
  .toc-depth-3 { padding-left: var(--sp-7); }
  .search-item { padding: var(--sp-3) var(--sp-4); }
  .search-snippet { font-size: 13px; }

  /* 设置项在手指下够大 */
  .settings-group { padding: var(--sp-4); }
  .field { font-size: 13px; margin-bottom: var(--sp-4); }
  input[type="range"] { height: 32px; }
  select { height: 42px; font-size: 16px; }
  .seg button { height: 40px; font-size: 14px; }
  .btn { height: 42px; font-size: 14px; }

  .keys { font-size: 13px; }
  .keys li { margin-bottom: var(--sp-3); }
}

/* ---- 横屏手机：把正文列收窄一点，别拉太长 ---- */
@media (max-width: 899px) and (orientation: landscape) {
  :root { --measure: 34em; }
  .column { padding-top: var(--sp-5); }
}
