/* 主題：<html data-palette> 是選的主題，data-theme 是它屬於淺色或深色系列，由 js/appearance.js 設定。
   淺色系列的預設是 VS Code，深色系列是 ATOM（跟隨系統就在這兩個之間切換）；
   新增主題＝加一段覆寫，再到 index.html 的外觀面板加一張卡片（深色的也要加進開頭小段程式的 DARK） */
:root {
  /* VS Code Light Modern */
  --accent: #005fb8;       /* 文字、圖示 */
  --accent-fill: #005fb8;  /* 按鈕底色，配 --on-accent */
  --accent-soft: #dcebf9;  /* 目前工具的底色 */
  --on-accent: #fff;
  --sb-bg: #f8f8f8;
  --surface: #ffffff;
  --canvas-auto: #ffffff;  /* 畫布選「自動」時的顏色 */
  --hover: #ececec;
  --selected: #e4e6f1;     /* 側欄目前的頁面 */
  --border: #e5e5e5;
  --text: #3b3b3b;
  --muted: #6e6e6e;
  --chip: #f0f0f0;
  --icon: #424242;
  --press: rgba(0, 0, 0, .08);
  --danger: #c72e2e;
  --shadow: rgba(0, 0, 0, .14);
  --board: #ffffff;   /* 畫布底色、畫布上的文字與格點，由 js/appearance.js 依畫布顏色設定 */
  --ink: #3b3b3b;
  --dot: rgba(0, 0, 0, .16);
  --sb-w: 260px;
}

/* Atom One Light */
:root[data-theme=light][data-palette=one-light] {
  --accent: #2c5fd6;
  --accent-fill: #2c5fd6;
  --accent-soft: #dde6fb;
  --sb-bg: #eaeaeb;
  --surface: #fafafa;
  --canvas-auto: #fafafa;
  --hover: #dfdfe1;
  --selected: #d5d9e3;
  --border: #d8d8da;
  --text: #383a42;
  --muted: #696c77;
  --chip: #e8e8ea;
  --icon: #4f525d;
  --danger: #ca1243;
}

/* ATOM（UARTPro 的配色，主色換成 One Dark 的藍） */
:root[data-theme=dark] {
  color-scheme: dark;
  --accent: #61afef;
  --accent-fill: #4d78cc;
  --accent-soft: #34465e;
  --on-accent: #fff;
  --sb-bg: #21252b;
  --surface: #2c313c;
  --canvas-auto: #282c34;
  --hover: rgba(255, 255, 255, .07);
  --selected: #3a404c;
  --border: #3a404c;
  --text: #dcdfe4;
  --muted: #9aa2b3;
  --chip: #363c48;
  --icon: #c8ccd4;
  --press: rgba(255, 255, 255, .1);
  --danger: #ff7b86;
  --shadow: rgba(0, 0, 0, .4);
}

/* VS Code Dark Modern */
:root[data-theme=dark][data-palette=vscode-dark] {
  --accent: #4daafc;
  --accent-fill: #0078d4;
  --accent-soft: #04395e;
  --sb-bg: #181818;
  --surface: #1f1f1f;
  --canvas-auto: #1f1f1f;
  --hover: rgba(255, 255, 255, .06);
  --selected: #37373d;
  --border: #2b2b2b;
  --text: #cccccc;
  --muted: #9d9d9d;
  --chip: #313131;
  --icon: #c5c5c5;
  --danger: #f48771;
  --shadow: rgba(0, 0, 0, .36);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  font-family: -apple-system, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color: var(--text);
  background: var(--board);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

/* 鍵盤操作時看得到焦點在哪（VS Code 的 focusBorder） */
button:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

svg.i {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* 高度跟著 visualViewport（iPhone 鍵盤彈出時會縮小），由 main.js 設定 */
#app {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--app-top, 0px);
  display: flex;
  height: 100vh;
  height: var(--app-h, 100dvh);
}

/* ---------- Sidebar (Notion 風格頁面樹) ---------- */
#sidebar {
  width: var(--sb-w);
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--sb-bg);
  border-right: 1px solid var(--border);
  transition: margin-left .2s ease, transform .2s ease;
}
body.sb-collapsed #sidebar { margin-left: calc(-1 * var(--sb-w)); }

.sb-head {
  height: 48px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  font-weight: 600;
}
.sb-brand { flex: 1; min-width: 0; display: flex; align-items: center; gap: 4px; height: 36px; padding: 0 8px; margin-left: -8px; border-radius: 6px; font-weight: 600; text-align: left; }
.sb-brand:hover { background: var(--hover); }
#nb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#nb-dirty { color: var(--accent); font-size: 10px; }
.nb-caret { color: var(--muted); font-size: 12px; }

/* 側欄分頁：頁面樹／搜尋 */
.sb-tabs { display: flex; gap: 2px; margin: 0 8px 4px; padding: 2px; border-radius: 8px; background: var(--chip); }
.sb-tabs button { flex: 1; height: 28px; border-radius: 6px; font-size: 13px; color: var(--muted); }
.sb-tabs button:hover { color: var(--text); }
.sb-tabs button[aria-selected=true] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px var(--shadow); }
.sb-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }

#tree {
  flex: 1;
  overflow-y: auto;
  padding: 4px 6px;
}

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 2px;
  height: 32px;
  padding-right: 4px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.row:hover { background: var(--hover); }
/* 目前的頁面：中性底色＋左側主色細線 */
.row.active { background: var(--selected); }
.row.active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 7px;
  bottom: 7px;
  width: 3px;
  border-radius: 2px;
  background: var(--accent);
}

.caret {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  font-size: 10px;
  color: var(--muted);
  transition: transform .15s;
}
.caret.open { transform: rotate(90deg); }
.caret.empty { visibility: hidden; }

.pg-icon { flex: none; width: 20px; text-align: center; font-size: 14px; }
.pg-title {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 14px;
}

.row-btn {
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: 4px;
  font-size: 16px;
  line-height: 1;
  color: var(--muted);
  opacity: 0;
}
.row:hover .row-btn, .row.active .row-btn { opacity: 1; }
.caret:hover, .row-btn:hover { background: var(--press); }

.sb-add {
  margin: 8px;
  padding: 10px;
  border-radius: 6px;
  text-align: left;
  color: var(--muted);
}
.sb-add:hover { background: var(--hover); }

/* ---------- Main ---------- */
#main {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

#topbar {
  flex: none;
  height: 52px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  border-bottom: 1px solid var(--border);
}

.icon-btn {
  flex: none;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 8px;
}
.icon-btn:hover { background: var(--hover); }

.title-wrap { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#crumbs {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#crumbs:empty { display: none; }
#page-title {
  width: 100%;
  padding: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}
#save-state { flex: none; font-size: 12px; color: var(--muted); }

/* ---------- Toolbar ---------- */
#toolbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  overflow-x: auto;
  scrollbar-width: none;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
#toolbar::-webkit-scrollbar { display: none; }

.tb-group { flex: none; display: flex; align-items: center; gap: 2px; }
.tb-sep { flex: none; width: 1px; height: 24px; margin: 0 6px; background: var(--border); }
#toolbar:not([data-tool="pen"]):not([data-ctx~="pen"]) .opts[data-for="pen"],
#toolbar:not([data-tool="hl"]):not([data-ctx~="hl"]) .opts[data-for="hl"],
#toolbar:not([data-tool="eraser"]) .opts[data-for="eraser"],
#toolbar:not([data-ctx~="text"]) .ctx[data-ctx="text"],
#toolbar:not([data-ctx~="image"]) .ctx[data-ctx="image"] { display: none; }
/* 文字格式按鈕 */
.fmt-btn { font-size: 17px; color: var(--icon); }
.fmt-btn[aria-pressed=true] { background: var(--accent-soft); color: var(--accent); }
.fmt-b { font-weight: 800; }
.fmt-i { font-style: italic; font-family: Georgia, serif; }
.font-toggle .font-name { font-size: 13px; }
.font-menu { width: 160px; }
.font-opt { display: block; width: 100%; padding: 7px 8px; border-radius: 6px; font-size: 15px; text-align: left; }
.font-opt:hover { background: var(--hover); }
.font-opt[aria-pressed=true] { background: var(--accent-soft); color: var(--accent); }
/* 字級：− [18] +，點數字框往下開預設字級清單（像 Google 試算表） */
.size-step { width: 30px; font-size: 22px; font-weight: 300; }
.size-input { flex: none; width: 46px; height: 30px; padding: 0 2px; border: 1.5px solid var(--border); border-radius: 6px; font: inherit; font-size: 14px; font-variant-numeric: tabular-nums; text-align: center; color: var(--text); background: var(--surface); }
.size-input:focus, .size-input[aria-expanded=true] { border-color: var(--accent); outline: none; }
.brush-palette.size-menu { width: 64px; max-height: min(440px, 60vh); overflow-y: auto; padding: 4px; }
.size-opt { display: block; width: 100%; height: 34px; border-radius: 6px; font-size: 14px; font-variant-numeric: tabular-nums; }
.size-opt:hover { background: var(--hover); }
.size-opt[aria-pressed=true] { background: var(--accent-soft); color: var(--accent); }
/* 框線與底色 */
.box-preview { width: 20px; height: 20px; border-radius: 4px; }
.box-menu { max-height: 70vh; overflow-y: auto; }
.box-fill-heading { margin-top: 12px; }
.swatch.none::after { background: linear-gradient(135deg, transparent calc(50% - 1px), #dc2626 calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px)), var(--surface); }
.line-opts { display: flex; align-items: center; gap: 2px; margin-bottom: 12px; }
.line-opt { flex: 1; height: 32px; display: grid; place-items: center; border-radius: 6px; }
.line-opt:hover { background: var(--hover); }
.line-opt[aria-pressed=true] { background: var(--accent-soft); }
.line-opt i { width: 22px; border-top: var(--h, 2px) var(--s, solid) var(--icon); }
.line-opt[aria-pressed=true] i { border-top-color: var(--accent); }
.line-sep { flex: none; width: 1px; height: 20px; margin: 0 4px; background: var(--border); }

.tbtn {
  flex: none;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--icon);
}
.tbtn:hover { background: var(--hover); }
.tbtn.active { background: var(--accent-soft); color: var(--accent); }
.tbtn:disabled { opacity: .3; background: none; cursor: default; }

.swatch {
  flex: none;
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 50%;
}
.swatch::after {
  content: "";
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 0 0 1px rgba(128, 128, 128, .55);
}
.swatch.active { box-shadow: inset 0 0 0 2px var(--accent); }

.wbtn {
  flex: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 8px;
}
.wbtn i { width: var(--w); height: var(--w); border-radius: 50%; background: var(--icon); }
.wbtn.active { background: var(--accent-soft); }

.chip {
  flex: none;
  height: 32px;
  padding: 0 10px;
  margin-left: 2px;
  border-radius: 16px;
  font-size: 13px;
  white-space: nowrap;
  background: var(--chip);
}
.chip.on { background: var(--accent-soft); color: var(--accent); }

/* ---------- Board ---------- */
#viewport {
  position: relative;
  flex: 1;
  overflow: hidden;
  touch-action: none;
  background-color: var(--board);
  background-image: radial-gradient(circle, var(--dot) 1px, transparent 1.3px);
  color: var(--ink);
  cursor: crosshair;
  /* 畫布上的選取框、把手跟著畫布深淺，不跟著主題 */
  --accent: #0078d4;
}
#viewport.dark-canvas { --accent: #4daafc; }
#viewport.no-grid { background-image: none; }
#viewport[data-tool="select"] { cursor: grab; }
#viewport[data-input="mouse"][data-tool="select"] { cursor: default; }
#viewport[data-input="mouse"][data-tool="select"] .item { cursor: move; }
#viewport.space-pan, #viewport.space-pan .item { cursor: grab; }
#viewport[data-tool="text"] { cursor: text; }

.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.layer { position: absolute; left: 0; top: 0; }

svg.ink {
  position: absolute;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  overflow: visible;
  pointer-events: none;
}
svg.ink path { fill: none; stroke-linecap: round; stroke-linejoin: round; }
svg.ink path.hl { opacity: .5; mix-blend-mode: multiply; }
.dark-canvas svg.ink path.hl { mix-blend-mode: screen; }
svg.ink path.lasso { fill: color-mix(in srgb, var(--accent) 6%, transparent); stroke: var(--accent); }

.item { position: absolute; }

#viewport[data-tool="pen"] .item,
#viewport[data-tool="hl"] .item,
#viewport[data-tool="eraser"] .item,
#viewport[data-tool="lasso"] .item,
#viewport[data-tool="text"] .img-item { pointer-events: none; }

.img-clip { position: absolute; inset: 0; overflow: hidden; }
.img-item img {
  position: absolute;
  display: block;
  max-width: none;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
}

.text-item {
  width: max-content;
  max-width: 32em;
  border-radius: 4px;
}
.text-item.fixed-w { max-width: none; }
.text-body {
  min-width: 1em;
  padding: 2px 4px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}
/* 有框線或底色時留多一點邊；框線是 outline，編輯框畫在它外面 */
.text-item.boxed .text-body { padding: .2em .45em; }
.text-item:focus-within { box-shadow: 0 0 0 calc(var(--bw, 0px) + 1.5px * var(--inv, 1)) var(--accent); }
.text-body:empty::before { content: "輸入文字…"; color: var(--muted); }
/* 文字框的 markdown 顯示（編輯時改回原始文字） */
.md-h1, .md-h2, .md-h3 { font-weight: 700; }
.md-h1 { font-size: 1.6em; }
.md-h2 { font-size: 1.3em; }
.md-h3 { font-size: 1.1em; }
.md-code { font-family: ui-monospace, Consolas, monospace; font-size: .9em; padding: 0 .2em; border-radius: .2em; background: rgba(0, 0, 0, .06); }
.dark-canvas .md-code { background: rgba(255, 255, 255, .1); }
.md-link { color: var(--accent); text-decoration: underline; }
.md-bullet { display: inline-block; width: 1.1em; }
.md-bullet::before { content: "•"; }
.md-check { position: relative; display: inline-block; width: .85em; height: .85em; margin-right: .1em; vertical-align: -.08em; border: .1em solid currentColor; border-radius: .18em; cursor: pointer; }
.md-check::before { content: ""; position: absolute; inset: -.45em; } /* 手指比較好點 */
.md-check[aria-checked=true] { background: var(--accent); border-color: var(--accent); }
.md-check[aria-checked=true]::after { content: "✓"; position: absolute; inset: 0; color: #fff; font-size: .75em; font-weight: 700; line-height: 1.15; text-align: center; }
.md-done { color: var(--muted); text-decoration: line-through; }
/* ```程式碼區塊、```mermaid 流程圖。文字框照樣在 32 個字寬換行（.md-seg），區塊可以更寬（不折行） */
.text-item.has-block:not(.fixed-w) { max-width: none; }
.md-seg { max-width: 32em; }
.md-pre, .md-mermaid { position: relative; margin: .3em 0; border-radius: 6px; }
.md-pre {
  --tok-k: #a626a4; --tok-s: #50a14f; --tok-n: #986801; --tok-c: #a0a1a7; --tok-f: #4078f2; --tok-m: #e45649;
  width: max-content;
  min-width: 100%;
  box-sizing: border-box;
  padding: .55em .9em;
  background: #f3f4f6;
  color: #383a42;
  font-family: ui-monospace, Consolas, "Cascadia Mono", "Courier New", monospace;
  font-size: .85em;
  font-weight: 400;
  font-style: normal;
  line-height: 1.5;
  white-space: pre;
  tab-size: 4;
}
.md-pre code { display: block; min-height: 1.5em; font: inherit; }
.text-item.fixed-w .md-pre { width: auto; white-space: pre-wrap; }
.dark-canvas .md-pre {
  --tok-k: #c678dd; --tok-s: #98c379; --tok-n: #d19a66; --tok-c: #7f848e; --tok-f: #61afef; --tok-m: #e06c75;
  background: #282c34;
  color: #abb2bf;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .08);
}
.tok-k { color: var(--tok-k); }
.tok-s { color: var(--tok-s); }
.tok-n { color: var(--tok-n); }
.tok-c { color: var(--tok-c); font-style: italic; }
.tok-f { color: var(--tok-f); }
.tok-m { color: var(--tok-m); }
.md-mermaid { width: max-content; min-width: 100%; }
.md-diagram { display: block; height: auto; max-width: none; }
.text-item.fixed-w .md-mermaid { width: auto; }
.text-item.fixed-w .md-diagram { max-width: 100%; }
.md-diagram-wait, .md-diagram-error { padding: .4em .6em; font-size: .85em; color: var(--muted); white-space: pre-wrap; }
.md-diagram-error { color: #e45649; }
/* 右上角的「複製」：滑鼠移過去或選取時才出現（列印、截圖不會有） */
.md-copy {
  position: absolute; top: .35em; right: .35em;
  padding: .1em .5em; border-radius: 4px;
  font: 600 11px/1.6 -apple-system, "Segoe UI", "Microsoft JhengHei", sans-serif;
  color: #fff; background: rgba(40, 44, 52, .75);
  cursor: pointer; user-select: none; -webkit-user-select: none;
  opacity: 0; transition: opacity .15s;
}
.md-copy::before { content: "複製"; }
.md-copy[data-done]::before { content: "已複製"; }
.text-item:hover .md-copy, .text-item.selected .md-copy { opacity: 1; }
.text-item:focus-within .md-copy { display: none; }
#viewport[data-tool="select"] .text-item:not(:focus-within) {
  cursor: grab;
  -webkit-user-select: none;
  user-select: none;
}
#viewport[data-tool="select"] .text-item:not(:focus-within) .text-body {
  -webkit-user-select: none;
  user-select: none;
}
#viewport[data-tool="select"] .text-item.selected .md-link { cursor: pointer; }
/* 編輯中的文字一律是文字游標，不繼承選取框的 move／畫布的 grab */
#viewport .text-item:focus-within .text-body { cursor: text; }

/* 選取範圍框（世界座標，跟著縮放） */
.sel-box {
  position: absolute;
  border: calc(1.5px * var(--inv, 1)) dashed var(--accent);
  border-radius: calc(4px * var(--inv, 1));
  background: color-mix(in srgb, var(--accent) 4%, transparent);
  pointer-events: none;
}

#viewport[data-tool="select"] .item.selected { cursor: move; }

/* 裁切：顯示整張原圖，範圍外變暗；框和把手不會被圖片範圍切掉 */
.img-item.cropping .img-clip { overflow: visible; }
.crop-area { position: absolute; overflow: hidden; pointer-events: none; }
.crop-shade { position: absolute; box-shadow: 0 0 0 100000px rgba(0, 0, 0, .5); }
.crop-frame { position: absolute; outline: calc(1.5px * var(--inv, 1)) solid #fff; box-shadow: 0 0 0 calc(2.5px * var(--inv, 1)) var(--accent); cursor: move; touch-action: none; }

/* 橡皮擦範圍圈（螢幕座標） */
.eraser-cursor {
  position: absolute;
  z-index: 5;
  border: 1.5px solid rgba(0, 0, 0, .45);
  border-radius: 50%;
  background: rgba(255, 255, 255, .35);
  pointer-events: none;
}
.dark-canvas .eraser-cursor { border-color: rgba(255, 255, 255, .6); background: rgba(0, 0, 0, .25); }

/* ---------- 導覽：畫布右下角的縮放、顯示全部、小地圖 ---------- */
#nav {
  position: absolute;
  z-index: 10;
  right: 12px;
  bottom: 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  pointer-events: none;
}
#nav > * { pointer-events: auto; }
.nav-bar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 2px 10px var(--shadow);
}
.nav-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  color: var(--icon);
}
.nav-btn svg.i { width: 18px; height: 18px; }
.nav-btn:hover, #zoom:hover { background: var(--hover); }
.nav-btn[aria-pressed=true] { background: var(--accent-soft); color: var(--accent); }
#zoom {
  min-width: 52px;
  height: 34px;
  border-radius: 7px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.nav-sep { width: 1px; height: 20px; margin: 0 3px; background: var(--border); }
#minimap {
  width: 220px;
  height: 150px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--board);
  box-shadow: 0 4px 16px var(--shadow);
}
#minimap canvas { display: block; width: 100%; height: 100%; cursor: pointer; touch-action: none; }
#minimap canvas.dragging { cursor: grabbing; }

/* 畫面裡看不到任何物件時出現，top 由 main.js 對齊畫布上緣 */
#back-to-content {
  position: absolute;
  z-index: 10;
  left: 50%;
  display: flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 2px 10px var(--shadow);
  transform: translateX(-50%);
}
#back-to-content svg.i { width: 16px; height: 16px; }
#back-to-content:hover { background: var(--accent-soft); }

@media (max-width: 767px) {
  #nav { right: 8px; bottom: 8px; }
  #minimap { width: 168px; height: 116px; }
}

/* ---------- 搜尋：側欄面板、畫布上的搜尋列、結果標記 ---------- */
.sp-box, #find {
  display: flex;
  align-items: center;
  gap: 2px;
}
.sp-box { margin: 4px 8px; padding: 0 2px 0 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
.sp-box:focus-within { border-color: var(--accent); }
#sp-input, #find-input {
  flex: 1;
  min-width: 0;
  height: 30px;
  padding: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font: inherit;
  font-size: 13px;
  color: var(--text);
}
.find-btn {
  flex: none;
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 5px;
  color: var(--icon);
  font-size: 12px;
  font-weight: 600;
}
.find-btn svg.i { width: 16px; height: 16px; }
.find-btn:hover:not(:disabled) { background: var(--hover); }
.find-btn:disabled { opacity: .35; cursor: default; }
.find-btn[aria-pressed=true] { background: var(--accent-soft); color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
#sp-summary { padding: 4px 12px 6px; font-size: 12px; color: var(--muted); }
#sp-results { flex: 1; overflow-y: auto; padding: 0 6px 8px; }
/* 整本筆記本的結果：每頁一組，標題點了收合 */
.sp-page-head {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  height: 28px;
  padding: 0 6px 0 2px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  text-align: left;
}
.sp-page-head:hover { background: var(--hover); }
.sp-caret { flex: none; width: 16px; color: var(--muted); font-size: 11px; text-align: center; transition: transform .15s; }
.sp-page.collapsed .sp-caret { transform: rotate(-90deg); }
.sp-page.collapsed .sp-hit { display: none; }
.sp-title { flex: none; max-width: 70%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sp-page.here .sp-title { color: var(--accent); }
.sp-crumbs { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; font-weight: 400; color: var(--muted); }
.sp-count { flex: none; min-width: 20px; padding: 0 6px; border-radius: 9px; background: var(--chip); font-size: 11px; font-weight: 400; line-height: 18px; text-align: center; color: var(--muted); }
.sp-hit {
  display: block;
  width: 100%;
  padding: 6px 8px 6px 22px;
  border-radius: 6px;
  font-size: 13px;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sp-hit:hover { background: var(--hover); }
.sp-hit.current { background: var(--selected); }
.sp-hit mark { border-radius: 2px; background: rgba(234, 179, 8, .4); color: inherit; }
.sp-hit.current mark { background: rgba(255, 140, 0, .55); }

/* top 由 js/search.js 對齊畫布上緣 */
#find {
  position: absolute;
  z-index: 11;
  right: 12px;
  width: min(420px, calc(100% - 16px));
  padding: 3px 4px 3px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 2px 10px var(--shadow);
}
#find:focus-within { border-color: var(--accent); }
#find.none #find-input { color: var(--danger); }
#find-count { flex: none; min-width: 56px; padding: 0 4px; font-size: 12px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
#find.none #find-count { color: var(--danger); }

/* 畫布上的結果（世界座標，跟著縮放）。縮小到看不清楚時第一塊加 .far，改畫固定大小的圓點 */
.search-layer { pointer-events: none; }
.search-hit {
  position: absolute;
  border-radius: calc(2px * var(--inv, 1));
  background: rgba(250, 204, 21, .45);
  mix-blend-mode: multiply;
}
.search-hit.current {
  background: rgba(255, 140, 0, .5);
  box-shadow: 0 0 0 calc(2px * var(--inv, 1)) #ff8c00;
}
.dark-canvas .search-hit { mix-blend-mode: normal; background: rgba(250, 204, 21, .35); }
.dark-canvas .search-hit.current { background: rgba(255, 140, 0, .45); }
.search-hit.far { mix-blend-mode: normal; } /* 圓點蓋在字上，不要透出底下的字 */
.search-hit.far::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(12px * var(--inv, 1));
  height: calc(12px * var(--inv, 1));
  border-radius: 50%;
  background: #facc15;
  box-shadow: 0 0 0 calc(1.5px * var(--inv, 1)) rgba(0, 0, 0, .55);
  transform: translate(-50%, -50%);
}
.search-hit.far.current::after {
  width: calc(18px * var(--inv, 1));
  height: calc(18px * var(--inv, 1));
  background: #ff8c00;
  box-shadow: 0 0 0 calc(2px * var(--inv, 1)) #fff, 0 0 0 calc(3.5px * var(--inv, 1)) rgba(0, 0, 0, .55);
}
/* 目前那一個外圈一直擴散，縮很小時也一眼看得到 */
.search-hit.far.current::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(18px * var(--inv, 1));
  height: calc(18px * var(--inv, 1));
  border: calc(3px * var(--inv, 1)) solid #ff8c00;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  animation: search-pulse 1.4s ease-out infinite;
}
@keyframes search-pulse {
  from { opacity: .9; transform: translate(-50%, -50%) scale(1); }
  to { opacity: 0; transform: translate(-50%, -50%) scale(3); }
}
@media (prefers-reduced-motion: reduce) {
  .search-hit.far.current::before { animation: none; opacity: 0; }
}

@media (max-width: 767px) {
  #find { right: 8px; }
}

/* ---------- Menu / Toast ---------- */
#menu {
  position: fixed;
  z-index: 50;
  min-width: 180px;
  max-width: min(360px, calc(100vw - 16px));
  max-height: 70vh;
  overflow-y: auto;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 24px var(--shadow);
}
#menu[hidden] { display: none; }
#menu button {
  display: block;
  width: 100%;
  padding: 9px 12px;
  border-radius: 6px;
  text-align: left;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#menu button:hover { background: var(--hover); }
#menu button:disabled { opacity: .35; background: none; cursor: default; }
#menu .danger { color: var(--danger); }
#menu .menu-head { padding: 6px 12px; font-size: 12px; color: var(--muted); }
#menu .menu-sep { margin: 4px 8px; border: 0; border-top: 1px solid var(--border); }
#menu .menu-hint { float: right; margin-left: 16px; font-size: 12px; color: var(--muted); }

#toast {
  position: fixed;
  z-index: 60;
  inset: auto auto 24px 50%;
  margin: 0;
  overflow: visible;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 6px 20px var(--shadow);
  font-size: 14px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#scrim { display: none; }

/* ---------- Responsive ---------- */
@media (hover: none) {
  .row { height: 40px; }
  .row-btn { opacity: .6; }
}

@media (min-width: 768px) {
  .only-mobile { display: none; }
}

@media (max-width: 767px) {
  #sidebar {
    position: fixed;
    z-index: 30;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(82vw, 300px);
    transform: translateX(-100%);
  }
  body.sb-open #sidebar { transform: none; box-shadow: 4px 0 24px var(--shadow); }
  body.sb-open #scrim {
    display: block;
    position: fixed;
    z-index: 20;
    inset: 0;
    background: rgba(0, 0, 0, .3);
  }
  #save-state { display: none; }
}

/* Collaboration controls stay separate from local save status. */
#collab-state, #collab-presence { font-size: 12px; color: var(--muted); }
#collab-state { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#collab-dialog { width: min(520px, calc(100vw - 24px)); max-height: 85dvh; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; padding: 20px; color: var(--text); background: var(--surface); }
#collab-dialog::backdrop { background: rgba(0,0,0,.35); }
.collab-heading { display: flex; align-items: center; justify-content: space-between; }
.collab-heading h2 { margin: 0; font-size: 20px; }
.collab-primary { background: var(--accent-fill); color: var(--on-accent); border-radius: 8px; padding: 10px 14px; margin: 8px 0; }
.collab-primary:disabled { opacity: .4; cursor: default; }
.collab-help { font-size: 13px; color: var(--muted); line-height: 1.6; }
.collab-page { display: flex; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.collab-page button { padding: 8px; border-radius: 6px; text-align: left; }
.collab-page button:first-child { flex: 1; overflow-wrap: anywhere; }
.collab-page button:hover { background: var(--hover); }
#collab-share label { display: block; margin: 12px 0 4px; }
#collab-share input[type=email], #collab-share select { width: 100%; padding: 8px; font: inherit; }
#viewport[data-readonly=true] .text-body { cursor: default; }
svg.ink path.remote-preview { opacity: .55; }
[hidden] { display: none !important; }
@media (max-width: 767px) {
  #topbar { flex-wrap: wrap; gap: 4px; height: auto; min-height: 52px; padding-bottom: 4px; }
  #collab-state { max-width: 160px; font-size: 11px; }
  #collab-presence { display: none; }
}

/* Brush controls and screen-sized preview. */
.palette-toggle { display: flex; align-items: center; justify-content: center; gap: 5px; height: 36px; padding: 0 7px; border-radius: 8px; }
.palette-toggle:hover, .palette-toggle[aria-expanded=true] { background: var(--accent-soft); }
.current-color { width: 23px; height: 23px; border-radius: 50%; background: var(--brush-color); box-shadow: inset 0 0 0 1px rgba(128, 128, 128, .35); }
.palette-arrow { font-size: 11px; color: var(--muted); }
.brush-palette, .popover { position: fixed; z-index: 65; width: 240px; padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); box-shadow: 0 6px 24px var(--shadow); }
.palette-heading { display: block; margin-bottom: 8px; font-size: 12px; color: var(--muted); }
.palette-swatches { display: flex; gap: 6px; margin-bottom: 12px; }
.brush-color { display: flex; align-items: center; justify-content: space-between; border-top: 1px solid var(--border); padding-top: 10px; font-size: 13px; }
.brush-color input { width: 32px; height: 32px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); cursor: pointer; }
.brush-size { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.brush-size input[type=range] { width: 110px; accent-color: var(--accent); }
.width-input { flex: none; width: 46px; height: 26px; padding: 0 2px; border: 1.5px solid var(--border); border-radius: 6px; font: inherit; font-size: 13px; font-variant-numeric: tabular-nums; text-align: center; color: var(--text); background: var(--surface); }
.width-input:focus { border-color: var(--accent); outline: none; }
#brush-preview { position: fixed; z-index: 70; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; width: 132px; height: 140px; border: 1px solid var(--border); border-radius: 12px; background: var(--board); box-shadow: 0 4px 16px var(--shadow); pointer-events: none; }
.brush-dot { display: block; border-radius: 50%; flex: none; }
.brush-caption { font-size: 12px; color: var(--muted); }
/* 選取把手：看得到的點很小，免得擋到內容；可以按的範圍用透明的 ::after 另外撐開。
   尺寸都乘 --inv，螢幕上的大小不受縮放影響 */
.sel-handle, .crop-handle {
  --hit: 26px;
  position: absolute;
  width: calc(10px * var(--inv, 1));
  height: calc(10px * var(--inv, 1));
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: #fff;
  /* 外圈用 box-shadow 畫：小於 1px 的 border 會被進位，放大時把手會變粗 */
  box-shadow: inset 0 0 0 calc(1.5px * var(--inv, 1)) var(--accent), 0 0 0 calc(1px * var(--inv, 1)) rgb(0 0 0 / .15);
  pointer-events: auto;
  transform: translate(-50%, -50%);
  touch-action: none;
}
/* 熱區往框外偏，小框中間還留得下拖曳移動的地方 */
.sel-handle::after, .crop-handle::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--hit) * var(--inv, 1));
  height: calc(var(--hit) * var(--inv, 1));
  transform: translate(calc(-50% + var(--ox, 0) * var(--hit) * var(--inv, 1) / 4), calc(-50% + var(--oy, 0) * var(--hit) * var(--inv, 1) / 4));
}
@media (pointer: coarse) {
  .sel-handle, .crop-handle { --hit: 44px; }
}
/* 邊中把手是貼著虛線的細條 */
.sel-handle:is([data-transform=e], [data-transform=w]), .crop-handle:is([data-crop=e], [data-crop=w]) { width: calc(4px * var(--inv, 1)); height: calc(16px * var(--inv, 1)); border-radius: calc(2px * var(--inv, 1)); }
.sel-handle:is([data-transform=n], [data-transform=s]), .crop-handle:is([data-crop=n], [data-crop=s]) { width: calc(16px * var(--inv, 1)); height: calc(4px * var(--inv, 1)); border-radius: calc(2px * var(--inv, 1)); }
.sel-handle[data-transform=nw], .crop-handle[data-crop=nw] { left: 0; top: 0; cursor: nwse-resize; --ox: -1; --oy: -1; }
.sel-handle[data-transform=n], .crop-handle[data-crop=n] { left: 50%; top: 0; cursor: ns-resize; --oy: -1; }
.sel-handle[data-transform=ne], .crop-handle[data-crop=ne] { left: 100%; top: 0; cursor: nesw-resize; --ox: 1; --oy: -1; }
.sel-handle[data-transform=e], .crop-handle[data-crop=e] { left: 100%; top: 50%; cursor: ew-resize; --ox: 1; }
.sel-handle[data-transform=se], .crop-handle[data-crop=se] { left: 100%; top: 100%; cursor: nwse-resize; --ox: 1; --oy: 1; }
.sel-handle[data-transform=s], .crop-handle[data-crop=s] { left: 50%; top: 100%; cursor: ns-resize; --oy: 1; }
.sel-handle[data-transform=sw], .crop-handle[data-crop=sw] { left: 0; top: 100%; cursor: nesw-resize; --ox: -1; --oy: 1; }
.sel-handle[data-transform=w], .crop-handle[data-crop=w] { left: 0; top: 50%; cursor: ew-resize; --ox: -1; }
.sel-handle[data-transform=rotate] { left: 50%; top: calc(-24px * var(--inv, 1)); cursor: grab; }
/* 框太窄／太矮時藏掉那個方向的邊中把手，只留四角 */
.sel-box[data-narrow] .sel-handle:is([data-transform=e], [data-transform=w]),
.sel-box[data-short] .sel-handle:is([data-transform=n], [data-transform=s]) { display: none; }
/* 拖曳把手時只留正在拉的那一個 */
.sel-box[data-dragging] .sel-handle:not(.dragging) { display: none; }
#viewport:not([data-tool=select]):not([data-tool=lasso]) .sel-handle, #viewport[data-readonly=true] .sel-handle { display: none; }

#drive-dialog { width: min(520px, calc(100vw - 24px)); max-height: 85dvh; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; padding: 20px; color: var(--text); background: var(--surface); }
#drive-dialog::backdrop { background: rgba(0,0,0,.35); }
.drive-muted { color: var(--muted); font-size: 13px; line-height: 1.6; }
.drive-file { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.drive-file strong { overflow-wrap: anywhere; }
.drive-file small { display: block; color: var(--muted); margin-top: 4px; }
.drive-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 16px; }
/* 頂列的儲存按鈕：有變更＝主色底，需要處理（本機同步暫停、衝突）＝警告圖示 */
#sync-button:disabled { opacity: .5; cursor: default; }
#sync-button.on { background: var(--accent-soft); color: var(--accent); }
#sync-button .i-warn, #sync-button.warn .i-cloud { display: none; }
#sync-button.warn .i-warn { display: block; }
#sync-button.warn { color: var(--danger); }
/* 儲存與同步視窗 */
#sync-dialog { width: min(440px, calc(100vw - 24px)); max-height: 85dvh; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; padding: 20px; color: var(--text); background: var(--surface); }
#sync-dialog::backdrop { background: rgba(0,0,0,.35); }
#sync-name { font-size: 14px; font-weight: 400; color: var(--muted); }
.sync-warn { margin: 12px 0 0; padding: 8px 10px; border-radius: 8px; font-size: 13px; color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.sync-warn[hidden], .sync-place[hidden] { display: none; }
.sync-place { padding: 14px 0; border-bottom: 1px solid var(--border); }
.sync-place h3 { margin: 0; font-size: 15px; }
.sync-status { margin: 4px 0 0; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.sync-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.sync-actions:not(:empty) { margin-top: 10px; }
.sync-actions .collab-primary { margin: 0; padding: 7px 12px; }
.sync-links { margin: 12px 0 0; font-size: 13px; color: var(--muted); }
.sync-foot { margin-top: 8px; font-size: 12px; }
.sync-links a, .link-btn { color: var(--accent); }
.link-btn { padding: 0; font: inherit; background: none; }
.link-btn:hover { text-decoration: underline; }
.panel-section { margin-top: 14px; }
.panel-wide { width: 100%; margin-left: 0; }

/* 匯出對話框（js/export.js）：截圖預覽、交給 AI、圖片、PDF */
#export-dialog { width: min(560px, calc(100vw - 24px)); max-height: 85dvh; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; padding: 20px; color: var(--text); background: var(--surface); }
#export-dialog::backdrop { background: rgba(0,0,0,.35); }
#export-dialog textarea { display: block; width: 100%; box-sizing: border-box; padding: 8px; font: inherit; font-size: 14px; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; resize: vertical; }
.export-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 10px 0 0; }
#export-preview { display: block; width: 100%; max-height: min(360px, 50dvh); object-fit: contain; margin: 4px 0 12px; border: 1px solid var(--border); border-radius: 8px; }
#export-preview[hidden] { display: none; }

/* 外觀面板：主題、畫布顏色 */
.popover-note { margin: 10px 0 0; font-size: 12px; line-height: 1.5; color: var(--muted); }
.theme-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 12px; }
.theme-card { display: flex; flex-direction: column; gap: 5px; padding: 5px 5px 6px; border-radius: 8px; font-size: 12px; text-align: left; }
.theme-card:hover { background: var(--hover); }
.theme-card[aria-pressed=true] { box-shadow: inset 0 0 0 2px var(--accent); }
.theme-card > span { display: flex; justify-content: space-between; gap: 4px; padding: 0 2px; }
.theme-card small { font-size: 11px; color: var(--muted); }
/* 縮圖：左邊側欄、右邊內容、底下一條主色 */
.theme-card i {
  display: block;
  height: 34px;
  border-radius: 5px;
  background:
    linear-gradient(var(--p-ac), var(--p-ac)) 40% 70% / 40% 4px no-repeat,
    linear-gradient(90deg, var(--p-sb) 28%, var(--p-bg) 28%);
  box-shadow: inset 0 0 0 1px rgba(128, 128, 128, .35);
}
/* 跟隨系統：橫跨兩欄，縮圖一半淺色一半深色 */
.theme-card.wide { grid-column: 1 / -1; flex-direction: row; align-items: center; gap: 8px; }
.theme-card.wide i { flex: none; width: 48px; height: 26px; background: linear-gradient(135deg, #ffffff 50%, #2c313c 50%); }
.theme-card.wide > span { flex: 1; }

/* 快捷鍵面板 */
#shortcuts-panel { width: 230px; max-height: calc(100dvh - 80px); overflow-y: auto; }
.keys { display: grid; grid-template-columns: 76px 1fr; gap: 6px 12px; align-items: center; margin: 0 0 12px; font-size: 13px; }
.keys:last-child { margin-bottom: 0; }
.keys dt { white-space: nowrap; color: var(--muted); font-size: 12px; }
.keys dd { margin: 0; }
kbd { display: inline-block; min-width: 1.6em; padding: 1px 5px; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; background: var(--hover); color: var(--text); font: 11px/1.5 ui-monospace, Consolas, monospace; text-align: center; }
/* 快捷鍵只在滑鼠模式顯示（手機、平板通常沒有鍵盤） */
html:not([data-input="mouse"]) #btn-shortcuts { display: none; }
.input-modes { display: flex; gap: 6px; }
.input-modes .chip { flex: 1; }
.input-note { margin: 8px 0 12px; }

/* ---------- 匯出 PDF（js/export.js）：列印時只印 #print-root，白紙＋淺色畫布的字色 ---------- */
#print-root { display: none; }
@media print {
  html, body { height: auto; overflow: visible; background: #fff; }
  body > :not(#print-root) { display: none !important; }
  #print-root {
    display: block;
    --ink: #3b3b3b;
    --accent: #0078d4;
    --muted: #6e6e6e;
    color: var(--ink);
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .print-fit { position: relative; overflow: hidden; break-inside: avoid; }
  .print-sheet { position: relative; transform-origin: 0 0; }
}
