/* ============================================================
   讀伴・樹狀閱讀 — 樣式
   視覺語彙:宣紙(偏青的紙灰)、墨青(主幹)、硃砂(硃批/分支)
   ============================================================ */

:root {
  --bg:        #EDEFE9;   /* 宣紙青灰 */
  --paper:     #FCFCFA;   /* 紙面 */
  --paper-2:   #F6F6F1;
  --ink:       #24313B;   /* 墨青 */
  --ink-soft:  #64737D;
  --ink-faint: #9AA6AD;
  --line:      #DBDED3;
  --vermilion: #A63A2B;   /* 硃砂 */
  --verm-wash: #F5E6E1;
  --verm-line: #DCB9B0;
  --jade-wash: #E9F0EC;
  --sand-wash: #F1EEE2;
  --shadow: 0 2px 10px rgba(36,49,59,.07);
  --radius: 12px;
  --serif: "Noto Serif TC", "Songti TC", serif;
  --sans:  "Noto Sans TC", "PingFang TC", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.7;
}
button { font-family: inherit; cursor: pointer; }
::selection { background: rgba(166,58,43,.22); }

/* ---------- 頂欄 ---------- */
.topbar {
  height: 60px;
  display: flex; align-items: center; gap: 18px;
  padding: 0 18px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  position: relative; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; user-select: none; }
.seal {
  width: 38px; height: 38px;
  background: var(--vermilion);
  color: #fff;
  font-family: var(--serif);
  font-size: 13px; font-weight: 700; line-height: 1.15;
  display: flex; align-items: center; justify-content: center;
  text-align: center;
  border-radius: 6px;
  transform: rotate(-3deg);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.35), 0 1px 3px rgba(166,58,43,.35);
  letter-spacing: 1px;
}
.brand h1 {
  font-family: var(--serif);
  font-size: 19px; font-weight: 700; letter-spacing: 3px;
}
.brand h1 em {
  font-style: normal; font-size: 12px; font-weight: 400;
  color: var(--ink-soft); letter-spacing: 4px; margin-left: 8px;
}
.doc-meta {
  flex: 1; min-width: 0;
  display: flex; align-items: baseline; gap: 10px;
  font-family: var(--serif);
}
.doc-meta .doc-title {
  font-size: 15px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar .actions { display: flex; gap: 8px; }

.btn {
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 13px;
  font-size: 13px;
  transition: all .15s;
}
.btn:hover { border-color: var(--ink-soft); background: var(--paper-2); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: #33434f; }
.btn.verm { background: var(--vermilion); border-color: var(--vermilion); color: #fff; }
.btn.verm:hover { background: #8f2f22; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ink-soft); }
.btn.ghost:hover { color: var(--ink); background: var(--paper-2); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { padding: 3px 9px; font-size: 12px; border-radius: 6px; }

/* ---------- 三欄工作區 ---------- */
.workspace {
  height: calc(100vh - 60px);
  display: grid;
  grid-template-columns: minmax(320px, 5fr) minmax(340px, 5fr) minmax(250px, 3fr);
  gap: 12px;
  padding: 12px;
}
.panel {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  min-height: 0; overflow: hidden;
}
.panel-head {
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 8px;
  flex-shrink: 0;
}
.panel-head .eyebrow {
  font-size: 11px; letter-spacing: 3px; color: var(--ink-faint);
  text-transform: uppercase;
}
.panel-head .name { font-family: var(--serif); font-weight: 600; font-size: 14px; }
.panel-head .spacer { flex: 1; }

/* ---------- 讀本 ---------- */
.reader-body {
  flex: 1; overflow-y: auto;
  padding: 26px 30px 60px;
}
.reader-title {
  font-family: var(--serif);
  font-size: 22px; font-weight: 700; letter-spacing: 2px;
  margin-bottom: 4px;
}
.reader-sub { font-size: 12px; color: var(--ink-faint); margin-bottom: 22px; }
.reader-body p.rp {
  font-family: var(--serif);
  font-size: 16.5px; line-height: 2.05;
  text-indent: 2em;
  margin-bottom: 14px;
  color: #2b3944;
}
.reader-body p.rp::before {
  content: attr(data-no);
  float: left; margin-left: -2em;
  font-family: var(--sans);
  font-size: 10px; color: var(--ink-faint);
  transform: translateY(9px);
  text-indent: 0;
}
.reader-hint {
  margin-top: 26px; padding: 10px 14px;
  background: var(--sand-wash);
  border-radius: 8px;
  font-size: 12.5px; color: var(--ink-soft);
}

/* ---------- 對話 ---------- */
.crumbs {
  display: flex; align-items: center; gap: 6px;
  flex-wrap: wrap; min-width: 0;
  font-size: 13px;
}
.crumb {
  border: none; background: none; padding: 2px 4px;
  color: var(--ink-soft); border-radius: 5px;
  max-width: 150px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.crumb:hover { background: var(--paper-2); color: var(--ink); }
.crumb.current { color: var(--ink); font-weight: 600; }
.crumb.branch.current { color: var(--vermilion); }
.crumb-sep { color: var(--ink-faint); }

.branch-banner {
  flex-shrink: 0;
  padding: 9px 16px;
  background: var(--verm-wash);
  border-bottom: 1px solid var(--verm-line);
  display: flex; align-items: center; gap: 10px;
  font-size: 12.5px; color: #7c2c20;
}
.branch-banner .quote {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: var(--serif);
}
.branch-banner.hidden { display: none; }

.chat-body {
  flex: 1; overflow-y: auto;
  padding: 18px 16px 20px;
  display: flex; flex-direction: column; gap: 14px;
  scroll-behavior: smooth;
}
.msg { display: flex; gap: 9px; max-width: 100%; }
.avatar {
  width: 30px; height: 30px; flex-shrink: 0;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-family: var(--serif); font-weight: 700;
  margin-top: 2px;
}
.msg.user .avatar { background: var(--ink); color: #fff; }
.msg.ai .avatar { background: var(--verm-wash); color: var(--vermilion); border: 1px solid var(--verm-line); }
.bubble-wrap { min-width: 0; flex: 1; }
.bubble {
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 13px;
  font-size: 14.5px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.ai .bubble { background: #FDF9F7; border-color: var(--verm-line); }
.bubble strong { font-weight: 700; color: inherit; }
.bubble em { font-style: normal; border-bottom: 1px solid var(--verm-line); }
.bubble code {
  font-family: ui-monospace, "Courier New", monospace;
  font-size: 13px;
  background: var(--sand-wash);
  border-radius: 4px;
  padding: 1px 5px;
}
.bubble .quoteblock {
  border-left: 3px solid var(--vermilion);
  background: var(--verm-wash);
  padding: 6px 10px;
  margin-bottom: 8px;
  border-radius: 0 6px 6px 0;
  font-family: var(--serif);
  font-size: 13.5px; color: #6d2a1f;
}
.bubble .quoteblock .q-label { font-size: 10.5px; color: #a3695e; font-family: var(--sans); display: block; }
.msg-actions {
  display: flex; gap: 6px; margin-top: 4px;
  opacity: 0; transition: opacity .15s;
}
.msg:hover .msg-actions { opacity: 1; }
.msg-actions button {
  border: none; background: none;
  font-size: 11.5px; color: var(--ink-faint);
  padding: 1px 4px; border-radius: 4px;
}
.msg-actions button:hover { color: var(--vermilion); background: var(--verm-wash); }

/* 分支收束卡片 */
.merge-card {
  border: 1px dashed var(--verm-line);
  background: var(--verm-wash);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13.5px;
  color: #6d2a1f;
}
.merge-card .m-head {
  display: flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--vermilion);
  margin-bottom: 4px; font-weight: 600;
}
.merge-card .m-link {
  border: none; background: none; color: var(--vermilion);
  text-decoration: underline; font-size: 11.5px; padding: 0;
}

/* 打字中 */
.typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--verm-line);
  animation: blink 1.2s infinite;
}
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100%{opacity:.25} 40%{opacity:1} }

/* 輸入區 */
.composer {
  flex-shrink: 0;
  border-top: 1px solid var(--line);
  padding: 10px 12px 12px;
  background: var(--paper);
}
.composer .pending-quote {
  display: flex; align-items: center; gap: 8px;
  background: var(--verm-wash);
  border: 1px solid var(--verm-line);
  border-radius: 8px;
  padding: 5px 10px; margin-bottom: 8px;
  font-size: 12.5px; color: #6d2a1f;
  font-family: var(--serif);
}
.composer .pending-quote span { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.composer .pending-quote button { border: none; background: none; color: var(--vermilion); font-size: 14px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper-2);
  padding: 9px 12px;
  font-family: inherit; font-size: 14px;
  line-height: 1.6;
  max-height: 140px;
  outline: none;
}
.composer textarea:focus { border-color: var(--ink-soft); background: #fff; }
.composer .hint {
  font-size: 11px; color: var(--ink-faint);
  margin-top: 6px;
}

/* ---------- 樹狀圖 & 筆記 ---------- */
.side-panel { display: flex; flex-direction: column; gap: 12px; min-height: 0; background: none; border: none; box-shadow: none; overflow: visible; }
.side-panel > .panel { flex: 1 1 55%; min-height: 0; }
.side-panel > .panel.notes-panel { flex: 1 1 45%; }

.tree-scroll { flex: 1; overflow: auto; padding: 12px 10px; }
#treeSvg { display: block; }
.tree-node { cursor: pointer; }
.tree-node rect {
  fill: var(--paper-2);
  stroke: var(--line); stroke-width: 1.2;
  rx: 9;
  transition: all .15s;
}
.tree-node.branch rect { stroke: var(--verm-line); fill: #FDF8F6; }
.tree-node.current rect { fill: var(--ink); stroke: var(--ink); }
.tree-node.branch.current rect { fill: var(--vermilion); stroke: var(--vermilion); }
.tree-node text { font-size: 12px; fill: var(--ink); font-family: var(--sans); }
.tree-node .t-label { font-family: var(--serif); font-weight: 600; }
.tree-node.current text { fill: #fff; }
.tree-node .t-status { font-size: 9.5px; fill: var(--ink-faint); }
.tree-node.current .t-status { fill: rgba(255,255,255,.75); }
.tree-edge { fill: none; stroke: var(--ink-faint); stroke-width: 1.6; }
.tree-edge.branch { stroke: var(--verm-line); stroke-width: 1.6; stroke-dasharray: none; }
.tree-edge.merged { stroke-dasharray: 4 4; }
.tree-legend {
  padding: 6px 14px 10px;
  font-size: 11px; color: var(--ink-faint);
  display: flex; gap: 12px; flex-shrink: 0;
}
.tree-legend i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.tree-legend .li-trunk i { background: var(--ink); }
.tree-legend .li-branch i { background: var(--vermilion); }

.notes-area {
  flex: 1; overflow-y: auto;
  padding: 14px;
  display: flex; flex-wrap: wrap; gap: 10px;
  align-content: flex-start;
}
.note-bubble {
  cursor: pointer;
  border: none;
  border-radius: 18px;
  padding: 8px 14px;
  font-size: 12.5px;
  font-family: var(--serif);
  color: var(--ink);
  max-width: 100%;
  text-align: left;
  box-shadow: 0 1px 4px rgba(36,49,59,.08);
  animation: floaty 5s ease-in-out infinite;
  line-height: 1.55;
}
.note-bubble.c0 { background: var(--jade-wash); }
.note-bubble.c1 { background: var(--verm-wash); }
.note-bubble.c2 { background: var(--sand-wash); }
.note-bubble:nth-child(2n) { animation-delay: -2.2s; }
.note-bubble:nth-child(3n) { animation-delay: -3.6s; }
@keyframes floaty {
  0%,100% { transform: translateY(0); }
  50%     { transform: translateY(-4px); }
}
@media (prefers-reduced-motion: reduce) {
  .note-bubble { animation: none; }
}
.note-bubble .n-full { display: none; }
.note-bubble.open { border-radius: 12px; width: 100%; animation: none; }
.note-bubble.open .n-short { display: none; }
.note-bubble.open .n-full { display: block; white-space: pre-wrap; }
.note-bubble .n-tools { display: none; gap: 8px; margin-top: 6px; }
.note-bubble.open .n-tools { display: flex; }
.note-bubble .n-tools button {
  border: none; background: none; font-size: 11px;
  color: var(--vermilion); padding: 0;
}
.notes-empty { font-size: 12px; color: var(--ink-faint); padding: 6px 2px; }

/* ---------- 選取浮動工具列 ---------- */
.sel-toolbar {
  position: fixed; z-index: 60;
  display: none;
  background: var(--ink);
  border-radius: 9px;
  padding: 5px;
  gap: 2px;
  box-shadow: 0 4px 16px rgba(20,30,38,.3);
}
.sel-toolbar.show { display: flex; }
.sel-toolbar button {
  border: none; background: none;
  color: #ECEFE9; font-size: 12.5px;
  padding: 5px 10px; border-radius: 6px;
  white-space: nowrap;
}
.sel-toolbar button:hover { background: rgba(255,255,255,.12); }
.sel-toolbar button.verm-act { color: #F3B9AC; }

/* ---------- 遮罩層(上傳/彈窗) ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(36,49,59,.35);
  backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.overlay.hidden { display: none; }
.sheet {
  background: var(--paper);
  border-radius: 16px;
  width: min(660px, 100%);
  max-height: 88vh;
  overflow-y: auto;
  padding: 30px 34px;
  box-shadow: 0 12px 40px rgba(20,30,38,.25);
}
.sheet h2 {
  font-family: var(--serif); font-size: 20px; letter-spacing: 2px;
  margin-bottom: 4px;
}
.sheet .sub { font-size: 13px; color: var(--ink-soft); margin-bottom: 20px; }
.sheet label.f-label { display: block; font-size: 12.5px; color: var(--ink-soft); margin: 14px 0 5px; }
.sheet input[type=text], .sheet textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--paper-2);
  padding: 9px 12px;
  font-family: var(--serif); font-size: 14.5px;
  outline: none;
}
.sheet textarea { min-height: 200px; resize: vertical; line-height: 1.9; }
.sheet input:focus, .sheet textarea:focus { border-color: var(--ink-soft); background: #fff; }
.sheet .row { display: flex; gap: 10px; margin-top: 20px; align-items: center; }
.sheet .row .spacer { flex: 1; }
.saved-docs { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.saved-doc {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--line); border-radius: 9px;
  padding: 8px 12px; background: var(--paper-2);
  font-size: 13.5px;
}
.saved-doc .sd-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--serif); font-weight: 600; border: none; background: none; text-align: left; cursor: pointer; font-size: 13.5px; }
.saved-doc .sd-title:hover { color: var(--vermilion); }
.saved-doc .sd-del { border: none; background: none; color: var(--ink-faint); font-size: 13px; }
.saved-doc .sd-del:hover { color: var(--vermilion); }

.check-row { display: flex; align-items: center; gap: 8px; font-size: 14px; margin: 8px 0; }
.check-row input { width: 15px; height: 15px; accent-color: var(--vermilion); }

.summary-body {
  font-size: 14px; white-space: pre-wrap;
  background: var(--paper-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 14px 16px;
  max-height: 50vh; overflow-y: auto;
  font-family: var(--serif); line-height: 1.9;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff;
  padding: 9px 18px; border-radius: 9px;
  font-size: 13px; opacity: 0; pointer-events: none;
  transition: all .25s; z-index: 100;
  box-shadow: 0 6px 20px rgba(20,30,38,.3);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--vermilion); }

/* ---------- 行動裝置 ---------- */
.mobile-tabs { display: none; }
@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
    padding: 8px;
  }
  .panel, .side-panel { display: none; }
  .workspace[data-view="reader"] #panelReader { display: flex; }
  .workspace[data-view="chat"]   #panelChat   { display: flex; }
  .workspace[data-view="tree"]   #panelSide   { display: flex; }
  .mobile-tabs {
    display: flex; gap: 4px;
  }
  .mobile-tabs button {
    border: 1px solid var(--line); background: var(--paper);
    border-radius: 7px; padding: 5px 10px; font-size: 12.5px; color: var(--ink-soft);
  }
  .mobile-tabs button.on { background: var(--ink); color: #fff; border-color: var(--ink); }
  .brand h1 em { display: none; }
  .topbar .actions .btn.hide-m { display: none; }
  .doc-meta { display: none; }
}

/* 捲軸 */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #C9CDC0; border-radius: 5px; border: 2px solid var(--paper); }
::-webkit-scrollbar-track { background: transparent; }
