:root {
  --bg: #1c1917;
  --card: #262220;
  --line: #3a3431;
  --fg: #ede8de;
  --muted: #9a9188;
  --accent: #d9942e;
  --danger: #c25b4e;
  --ok: #6f9e60;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* .gate 的 display:grid 会盖掉 UA 的 [hidden]{display:none}(同特异性、后来者赢) */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-text-size-adjust: 100%;
}

h1, h2 { font-weight: 600; }
h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); font-size: 13px; }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.err { color: var(--danger); font-size: 13px; }

header {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; flex-direction: column; line-height: 1.2; margin-right: auto; }
.brand .muted { font-size: 12px; }

main { max-width: 720px; margin: 0 auto; padding: 16px; display: grid; gap: 14px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
}
.row-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }

input[type=text], input[type=password], input:not([type]) {
  width: 100%; padding: 9px 11px;
  background: #1a1715; color: var(--fg);
  border: 1px solid var(--line); border-radius: 8px;
  font: inherit;
}
input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button {
  font: inherit; cursor: pointer; border-radius: 8px;
  border: 1px solid var(--line); background: #302b28; color: var(--fg);
  padding: 8px 13px;
}
button:active { transform: translateY(1px); }
button.primary { background: var(--accent); border-color: var(--accent); color: #1c1917; font-weight: 600; }
button.ghost { background: transparent; }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }

.compose { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; }
@media (max-width: 520px) {
  .compose { grid-template-columns: 1fr auto; }
  .compose > input { grid-column: 1 / -1; }
}
.colors { display: flex; gap: 6px; }
.sw { width: 26px; height: 26px; padding: 0; border-radius: 50%; border: 2px solid transparent; }
.sw.yellow { background: #d9942e; }
.sw.blue   { background: #4c7fb5; }
.sw.red    { background: #c25b4e; }
.sw.active { border-color: var(--fg); }

.list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 8px; }
.list li {
  border: 1px solid var(--line); border-left-width: 4px;
  border-radius: 8px; padding: 9px 11px; background: #221e1c;
}
.list li.yellow { border-left-color: #d9942e; }
.list li.blue   { border-left-color: #4c7fb5; }
.list li.red    { border-left-color: #c25b4e; }
.ann-text { margin-bottom: 4px; word-break: break-word; }
.ann-note { font-size: 13px; color: var(--muted); font-style: italic; }
.ann-foot { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 6px; font-size: 12px; color: var(--muted); }
.ann-foot button { padding: 3px 8px; font-size: 12px; }
.badge { border: 1px solid var(--line); border-radius: 999px; padding: 1px 7px; }
.badge.dirty { border-color: var(--accent); color: var(--accent); }

.pill { font-size: 12px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--line); white-space: nowrap; }
.pill.syncing { border-color: var(--accent); color: var(--accent); }
.pill.idle    { border-color: var(--ok); color: var(--ok); }
.pill.offline, .pill.error, .pill.unauthorized { border-color: var(--danger); color: var(--danger); }
.chip { padding: 3px 10px; font-size: 12px; border-radius: 999px; }

.progress { display: flex; align-items: center; gap: 12px; }
.progress input { flex: 1; accent-color: var(--accent); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.empty { margin-top: 12px; }

.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; }
.gate-card { width: min(360px, 100%); display: grid; gap: 10px; }

.banner {
  border: 1px solid var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
  word-break: break-word;
}
.diag {
  margin: 0; padding: 10px;
  background: #1a1715; border: 1px solid var(--line); border-radius: 8px;
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--muted); white-space: pre-wrap; word-break: break-word;
}

/* ============ 阅读器 ============ */

.app-body.reading { overflow: hidden; height: 100dvh; }

#reader { position: fixed; inset: 0; display: flex; flex-direction: column; }
#viewport { flex: 1; min-height: 0; }

/* 顶/底浮层:点中间区域才出现 */
.chrome { position: fixed; inset: 0; pointer-events: none; z-index: 20; }
.chrome-top, .chrome-bottom {
  position: absolute; left: 0; right: 0;
  pointer-events: auto;
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(12px);
  border-color: var(--line);
}
.chrome-top {
  top: 0; display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom: 1px solid var(--line);
}
.book-title {
  flex: 1; text-align: center; font-size: 13px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chrome-bottom {
  bottom: 0; display: grid; gap: 10px;
  padding: 12px 14px calc(env(safe-area-inset-bottom) + 12px);
  border-top: 1px solid var(--line);
}
.setting { display: flex; align-items: center; gap: 12px; font-size: 13px; }
.setting > span:first-child { width: 34px; color: var(--muted); flex: none; }
.setting input[type=range] { flex: 1; accent-color: var(--accent); }
.themes { display: flex; gap: 6px; }
.themes button { padding: 4px 12px; font-size: 13px; }
.themes button.active { border-color: var(--accent); color: var(--accent); }

/* 进度:细条 + 角标,不抢注意力 */
.progress-bar {
  position: fixed; left: 0; right: 0; bottom: 0; height: 2px;
  background: var(--line); z-index: 15; pointer-events: none;
}
.progress-bar > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.progress-chip {
  position: fixed; right: calc(env(safe-area-inset-right) + 10px);
  bottom: calc(env(safe-area-inset-bottom) + 8px);
  font-size: 11px; color: var(--muted); z-index: 15; pointer-events: none;
  font-variant-numeric: tabular-nums;
}

/* 撤销条:抬指后浮在选区下方,3 秒淡出 */
.undo-bar {
  position: fixed; z-index: 40;
  top: 50%; left: 50%;
  display: flex; align-items: center; gap: 6px;
  padding: 6px;
  background: #302b28; border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .45);
  animation: bar-in .12s ease-out;
}
@keyframes bar-in { from { opacity: 0; transform: translateY(-4px); } }
.undo-bar button { padding: 6px 10px; font-size: 13px; }
.undo-bar .sw { width: 22px; height: 22px; padding: 0; }

/* 标注面板 */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 30;
  width: min(380px, 88vw);
  background: var(--card); border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 12px) 12px calc(env(safe-area-inset-bottom) + 12px);
  box-shadow: -12px 0 32px rgba(0, 0, 0, .4);
}
.panel-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.panel-head strong { margin-right: auto; }
.panel .list { overflow-y: auto; margin-top: 0; }
.panel .list li { display: flex; gap: 8px; align-items: flex-start; }
.ann-jump {
  flex: 1; text-align: left; background: none; border: none; padding: 0;
  color: inherit; font: inherit; cursor: pointer;
}

/* 书库 */
.book-row { display: flex; align-items: center; gap: 10px; }
.book-open {
  flex: 1; text-align: left; background: none; border: none; padding: 0;
  color: inherit; font: inherit; cursor: pointer;
}
.book-name { font-weight: 600; margin-bottom: 2px; }
.btnlike {
  display: inline-flex; align-items: center; text-decoration: none;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 13px; color: var(--fg);
}

/* 手势日志:iPad 上没有控制台,只能画到屏幕上 */
.trace-panel {
  position: fixed; z-index: 60;
  left: 8px; right: 8px; bottom: calc(env(safe-area-inset-bottom) + 8px);
  max-height: 42vh; display: flex; flex-direction: column;
  background: rgba(20, 18, 16, .96); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px;
}
.trace-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.trace-head strong { margin-right: auto; font-size: 13px; }
.trace-head button { padding: 3px 10px; font-size: 12px; }
.trace-panel pre {
  margin: 0; overflow: auto; white-space: pre-wrap; word-break: break-word;
  font: 11px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted);
}

/* 日志入口常驻。点击呼菜单本身可能是坏的,入口不能依赖它。 */
.trace-fab {
  position: fixed; z-index: 55;
  left: calc(env(safe-area-inset-left) + 8px);
  bottom: calc(env(safe-area-inset-bottom) + 8px);
  padding: 5px 10px; font-size: 11px;
  opacity: .35; background: var(--card);
}
.trace-fab:active { opacity: 1; }
