:root {
  --bg: #efece5;
  --bg-2: #e6e1d7;
  --panel: #fffdf9;
  --ink: #22201c;
  --muted: #77716a;
  --line: #ddd6ca;
  --accent: #b4552d;
  --accent-ink: #fff8f2;
  --sel: rgba(180, 85, 45, .28);
  --shadow: 0 8px 30px rgba(45, 35, 20, .12);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --serif: "Songti SC", "SimSun", "Noto Serif SC", serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: var(--ink);
  font-family: var(--font);
  background:
    radial-gradient(1200px 600px at 15% -10%, #fff8ea 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #f7efe3 0%, transparent 50%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }
.hidden { display: none !important; }
.small { font-size: 12px; }
.muted { color: var(--muted); }

/* ---------- top bar ---------- */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 18px;
  background: rgba(255, 253, 249, .82);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 190px; }
.logo {
  width: 34px; height: 34px; border-radius: 10px;
  display: grid; place-items: center;
  background: var(--ink); color: #fff8ec;
  font-family: var(--serif); font-size: 19px; font-weight: 700;
  box-shadow: inset 0 -2px 0 rgba(255,255,255,.12);
}
.brandName { font-weight: 700; font-size: 15px; letter-spacing: .2px; }
.brandSub { font-size: 11px; color: var(--muted); margin-top: 1px; }
.docMeta { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.docName { font-size: 13px; color: var(--muted); truncate: ellipsis; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 46vw; }
.badge {
  font-size: 10px; padding: 2px 7px; border-radius: 99px;
  background: #efe6d8; color: #7a5b36; border: 1px solid #e3d5c1;
  white-space: nowrap;
}
.badge.scan { background: #fbeee6; color: #a34a22; border-color: #f0d3c3; }
.topActions { display: flex; gap: 8px; }

.btn {
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 13px;
  transition: .15s ease;
  white-space: nowrap;
}
.btn:hover { border-color: #c9bfae; transform: translateY(-1px); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--accent-ink); }
.btn.primary:hover { background: #35312b; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 12px; border-radius: 9px; }
.iconBtn {
  width: 32px; height: 32px; border-radius: 9px;
  border: 1px solid transparent; background: transparent;
  display: grid; place-items: center; font-size: 18px; line-height: 1;
}
.iconBtn:hover { background: #f0ebe1; }
.iconBtn.subtle { width: auto; padding: 0 8px; font-size: 12px; color: var(--muted); }

/* ---------- main ---------- */
main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  grid-template-rows: minmax(0, 1fr);
}
.viewer { min-width: 0; display: flex; flex-direction: column; }
.viewerBar {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 14px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.45);
}
.navGroup, .zoomGroup { display: flex; align-items: center; gap: 4px; }
.pageInput {
  width: 46px; text-align: center;
  border: 1px solid var(--line); border-radius: 8px;
  padding: 5px 4px; font-size: 13px; background: #fff;
}
.pageTotal { font-size: 12px; color: var(--muted); }
.zoomLabel { font-size: 12px; color: var(--muted); min-width: 66px; text-align: center; }
.barRight { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.pageMeta { font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.pageMeta.scan { color: #a34a22; }

.stage {
  flex: 1; min-height: 0;
  overflow: auto;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 26px;
  scroll-behavior: smooth;
}
.pageShell { margin: 0 auto; }
.page {
  position: relative;
  background: #fff;
  box-shadow: 0 2px 6px rgba(40,30,15,.08), 0 18px 50px rgba(40,30,15,.14);
  border-radius: 2px;
  overflow: hidden;
}
.page canvas { display: block; }

/* ---------- text layer（PDF.js 6 结构） ---------- */
.textLayer {
  position: absolute;
  inset: 0;
  overflow: clip;
  opacity: 1;
  line-height: 1;
  text-align: initial;
  text-size-adjust: none;
  forced-color-adjust: none;
  transform-origin: 0 0;
  caret-color: CanvasText;
  z-index: 2;
  --min-font-size: 1;
  --text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
  --min-font-size-inv: calc(1 / var(--min-font-size));
}
.textLayer > :not(.markedContent),
.textLayer .markedContent span:not(.markedContent) {
  z-index: 1;
  --font-height: 0;
  font-size: calc(var(--text-scale-factor) * var(--font-height));
  --scale-x: 1;
  --rotate: 0deg;
  transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
}
.textLayer span, .textLayer br {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
  user-select: text;
  -webkit-user-select: text;
}
.textLayer .markedContent { display: contents; }
.textLayer ::selection { background: var(--sel); color: transparent; }
.textLayer ::-moz-selection { background: var(--sel); color: transparent; }
.textLayer .endOfContent {
  display: block; position: absolute; inset: 100% 0 0;
  z-index: 0; cursor: default; user-select: none;
}

/* ---------- empty ---------- */
.empty { width: 100%; display: grid; place-items: center; padding: 30px 0; }
.emptyCard {
  max-width: 520px; text-align: center;
  background: rgba(255,253,249,.8);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 34px 34px 26px;
  box-shadow: var(--shadow);
}
.emptyIcon {
  width: 58px; height: 70px; margin: 0 auto 14px;
  border: 1.5px solid #cbbfae; border-radius: 8px;
  display: grid; place-items: center;
  font-family: var(--serif); font-weight: 700; font-size: 15px;
  color: #a3907a; background: #fffaf0;
  box-shadow: 6px 6px 0 #ede4d4;
}
.emptyCard h2 { font-size: 18px; margin: 0 0 8px; }
.emptyCard p { font-size: 13px; color: var(--muted); line-height: 1.7; margin: 0 0 16px; }
.emptyActions { margin-bottom: 16px; }
.samples { display: flex; flex-wrap: wrap; gap: 7px; justify-content: center; align-items: center; font-size: 12px; color: var(--muted); }
.chip {
  border: 1px solid var(--line); background: #fff;
  padding: 5px 10px; border-radius: 99px; font-size: 12px;
}
.chip:hover { border-color: #c8b9a2; background: #fffaf2; }
.hint { margin-top: 14px !important; font-size: 11.5px !important; color: #a39a8c !important; }

/* ---------- page text box ---------- */
.pageTextBox { flex: 0 0 auto; border-top: 1px solid var(--line); background: rgba(255,255,255,.5); }
.pageTextToggle {
  width: 100%; display: flex; justify-content: space-between; align-items: center;
  border: 0; background: transparent; padding: 7px 16px;
  font-size: 12px; color: var(--muted);
}
.pageTextToggle:hover { background: rgba(0,0,0,.03); }
.pageText {
  margin: 0; padding: 0 16px 10px;
  max-height: 120px; overflow: auto;
  font-family: var(--font); font-size: 12px; line-height: 1.65;
  color: #4a453e; white-space: pre-wrap;
}

/* ---------- chat ---------- */
.chat {
  min-width: 0;
  border-left: 1px solid var(--line);
  background: var(--panel);
  display: flex; flex-direction: column;
  min-height: 0;
}
.chatHead {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 14px 11px;
  border-bottom: 1px solid var(--line);
}
.chatTitle { font-weight: 700; font-size: 14px; }
.chatSub { font-size: 11px; color: var(--muted); margin-top: 2px; }
.chatHeadActions { display: flex; align-items: center; gap: 2px; }
.chatBody { flex: 1; min-height: 0; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.chatEmpty { margin: auto; text-align: center; color: var(--muted); font-size: 12.5px; line-height: 1.7; padding: 20px 6px; }
.chatEmpty p { margin: 0 0 12px; }
.quick { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }

.msg { max-width: 92%; border-radius: 13px; padding: 9px 12px; font-size: 13.5px; line-height: 1.75; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: #f0e2d3; border: 1px solid #e7d3bd; border-bottom-right-radius: 5px; }
.msg.ai { align-self: flex-start; background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 5px; box-shadow: 0 1px 2px rgba(0,0,0,.03); }
.msg.sys { align-self: center; background: transparent; color: var(--muted); font-size: 11.5px; border: 1px dashed var(--line); }
.msg.error { align-self: flex-start; background: #fdf1ee; border: 1px solid #f0cfc6; color: #8f3a22; }
.msg .quote {
  display: block; margin: -2px 0 7px; padding: 5px 8px;
  border-left: 2px solid #c9a888; background: rgba(255,255,255,.55);
  color: #6f6252; font-size: 11.5px; line-height: 1.55;
  border-radius: 0 6px 6px 0;
  max-height: 88px; overflow: hidden;
}
.msg .meta { display: block; margin-top: 4px; font-size: 10px; color: #a1988b; }
.msg .cursor { display: inline-block; width: 7px; height: 14px; background: var(--accent); vertical-align: -2px; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.quoteCard {
  margin: 0 14px 8px; padding: 8px 10px;
  background: #fbf4ea; border: 1px solid #ecddc9; border-radius: 10px;
  display: flex; gap: 8px; align-items: flex-start; font-size: 11.5px;
}
.quoteLabel { color: var(--accent); font-weight: 700; white-space: nowrap; }
.quoteText { flex: 1; color: #6b5d4c; max-height: 54px; overflow: hidden; line-height: 1.5; }
.quoteClose { border: 0; background: transparent; color: #a89b8a; padding: 0 2px; }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px 6px; border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px;
  padding: 9px 11px; font: inherit; font-size: 13.5px; line-height: 1.5;
  max-height: 120px; background: #fff; outline: none;
}
.composer textarea:focus { border-color: #c6b49b; box-shadow: 0 0 0 3px rgba(180,85,45,.08); }
.sendBtn {
  width: 38px; height: 38px; border-radius: 11px; border: 0;
  background: var(--ink); color: #fff; font-size: 18px;
  display: grid; place-items: center;
}
.sendBtn:hover { background: #3a352e; }
.sendBtn.stop { background: var(--accent); font-size: 12px; }
.composerHint { padding: 0 14px 9px; font-size: 10.5px; color: #a89f92; }

.chatFab {
  display: none;
  position: fixed; right: 16px; bottom: 18px; z-index: 45;
  border: 0; border-radius: 99px;
  background: var(--ink); color: #fff;
  padding: 12px 18px; font-size: 13px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}

/* ---------- selection bar ---------- */
.selBar {
  position: fixed; z-index: 100;
  display: flex; align-items: center; gap: 2px;
  background: #26221d; color: #fff;
  border-radius: 10px; padding: 4px;
  box-shadow: 0 10px 30px rgba(0,0,0,.28);
  opacity: 0; pointer-events: none; transform: translateY(4px);
  transition: opacity .12s ease, transform .12s ease;
}
.selBar.show { opacity: 1; pointer-events: auto; transform: translateY(0); }
.selBar::after {
  content: ""; position: absolute; left: 50%; bottom: -5px;
  width: 10px; height: 10px; background: #26221d;
  transform: translateX(-50%) rotate(45deg); border-radius: 2px;
}
.selBar button {
  border: 0; background: transparent; color: #fff;
  font-size: 12px; padding: 5px 8px; border-radius: 7px;
}
.selBar button:hover { background: rgba(255,255,255,.14); }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(30, 24, 16, .38);
  backdrop-filter: blur(3px);
  display: grid; place-items: center;
  padding: 18px;
}
.modalCard {
  width: min(520px, 100%);
  max-height: 90dvh;
  background: var(--panel);
  border-radius: 18px;
  box-shadow: 0 30px 80px rgba(0,0,0,.3);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modalHead { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modalHead h3 { margin: 0; font-size: 15px; }
.modalBody { padding: 16px; overflow-y: auto; }
.modalFoot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.field { display: block; margin-bottom: 13px; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field input {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 11px; font: inherit; font-size: 13px; background: #fff; outline: none;
}
.field input:focus { border-color: #c6b49b; box-shadow: 0 0 0 3px rgba(180,85,45,.08); }
.testRow { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
#testResult.ok { color: #2e7d4f; }
#testResult.bad { color: #a53c22; }

.ocrStatus { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.progress { height: 6px; background: #eee7db; border-radius: 99px; overflow: hidden; margin-bottom: 12px; }
.progressBar { height: 100%; width: 0; background: var(--accent); transition: width .2s ease; }
.ocrText {
  width: 100%; min-height: 180px; resize: vertical;
  border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; font: inherit; font-size: 13px; line-height: 1.7;
  background: #fff; outline: none;
}

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #26221d; color: #fff; font-size: 12.5px;
  padding: 9px 15px; border-radius: 10px; z-index: 300;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
  max-width: 80vw;
}

/* ---------- mobile ---------- */
@media (max-width: 880px) {
  .topbar { gap: 10px; padding: 8px 12px; }
  .brand { min-width: 0; }
  .brandSub { display: none; }
  .brandName { font-size: 14px; }
  .docName { max-width: 30vw; }
  .topActions .btn { padding: 7px 10px; font-size: 12px; }
  main { grid-template-columns: 1fr; }
  .viewerBar { padding: 6px 10px; gap: 8px; flex-wrap: wrap; }
  .pageMeta { display: none; }
  .barRight { margin-left: 0; }
  .stage { padding: 12px; }
  .pageTextBox { display: none; }
  .chat {
    position: fixed; left: 0; right: 0; bottom: 0;
    height: min(74dvh, 620px);
    border-left: 0; border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    transform: translateY(103%);
    transition: transform .22s ease;
    z-index: 60;
    box-shadow: 0 -18px 50px rgba(0,0,0,.18);
  }
  .chat.open { transform: translateY(0); }
  .chatFab { display: block; }
  .chatFab.hidden { display: none; }
  .mobileOnly { display: grid; }
  .composerHint { padding-bottom: max(9px, env(safe-area-inset-bottom)); }
  .selBar button { padding: 7px 10px; font-size: 12.5px; }
  .toast { top: 66px; bottom: auto; }
  .chatEmpty { margin: 26px auto auto; }
}
@media (min-width: 881px) {
  .mobileOnly { display: none !important; }
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191714;
    --bg-2: #211e19;
    --panel: #221f1b;
    --ink: #efe8dc;
    --muted: #a49a8b;
    --line: #3a342c;
    --accent: #d77a4c;
    --sel: rgba(215, 122, 76, .35);
  }
  body {
    background:
      radial-gradient(1200px 600px at 15% -10%, #2a241b 0%, transparent 55%),
      radial-gradient(900px 500px at 100% 0%, #262119 0%, transparent 50%),
      var(--bg);
  }
  .topbar, .viewerBar, .pageTextBox { background: rgba(34, 31, 27, .82); }
  .btn { background: #2a2620; }
  .btn.primary { background: #efe8dc; color: #201d18; border-color: #efe8dc; }
  .emptyCard { background: rgba(34,31,27,.75); }
  .emptyIcon { background: #2a2520; color: #b7a58f; box-shadow: 6px 6px 0 #201d18; }
  .chat { background: var(--panel); }
  .msg.ai { background: #2a2621; }
  .msg.user { background: #3a2c22; border-color: #4a372a; }
  .composer textarea, .field input, .ocrText, .pageInput { background: #2a2620; }
  .chip { background: #2a2620; }
  .pageText { color: #c9bfb1; }
}
