:root {
  --bg: #eceae6;
  --panel: #ffffff;
  --panel-2: #f6f5f2;
  --line: #d9d6cf;
  --text: #1f1d1a;
  --muted: #6d685f;
  --accent: #c2410c;
  --accent-ink: #ffffff;
  --accent-soft: #fbe7dc;
  --danger: #b42318;
  --shadow: 0 1px 2px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.08);
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1b19; --panel: #262522; --panel-2: #2f2d2a; --line: #3d3a35;
    --text: #ece8e1; --muted: #a39d92; --accent: #f97316; --accent-ink: #1c1b19;
    --accent-soft: #3a2a1f; --danger: #f97066; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1b19; --panel: #262522; --panel-2: #2f2d2a; --line: #3d3a35;
  --text: #ece8e1; --muted: #a39d92; --accent: #f97316; --accent-ink: #1c1b19;
  --accent-soft: #3a2a1f; --danger: #f97066; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  padding: 6px 12px; min-height: 52px;
}
.brand { font-weight: 700; white-space: nowrap; color: var(--accent); }
.filegroup { display: flex; gap: 6px; flex: none; }
.toolbar { display: flex; align-items: center; gap: 8px; flex: 1; overflow-x: auto; scrollbar-width: thin; padding: 2px 0; }
.group { display: flex; align-items: center; gap: 4px; flex: none; }
.toolbar > .group { padding-left: 8px; border-left: 1px solid var(--line); }
.actions { display: flex; gap: 6px; flex: none; padding-left: 8px; border-left: 1px solid var(--line); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 12px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--panel-2); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.07); background: var(--accent); }
.btn.danger-ghost { color: var(--danger); }
.btn.icon { width: 34px; padding: 0; font-size: 18px; }
.btn.small { height: 28px; padding: 0 9px; font-size: 13px; }
.btn.zoomval { min-width: 64px; padding: 0 6px; font-variant-numeric: tabular-nums; }
.tool {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  width: 44px; height: 42px; border-radius: var(--radius); border: 1px solid transparent;
  background: transparent; cursor: pointer; font-size: 11px; color: var(--muted);
}
.tool:hover { background: var(--panel-2); color: var(--text); }
.tool.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.tool span { line-height: 1; white-space: nowrap; }

/* ---------- property bar ---------- */
.propbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  background: var(--panel-2); border-bottom: 1px solid var(--line);
  padding: 6px 12px; min-height: 44px; font-size: 13px;
}
.prop { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.prop[hidden] { display: none; }
.prop select, .prop input[type=number] {
  height: 30px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--text);
  padding: 0 6px; font: inherit;
}
.prop input[type=number] { width: 64px; }
.prop input[type=color] { width: 34px; height: 28px; padding: 0 2px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); cursor: pointer; }
.prop input[type=range] { width: 100px; }
.prop output { min-width: 26px; color: var(--text); font-variant-numeric: tabular-nums; }
.seg { gap: 0; }
.tog {
  width: 32px; height: 30px; border: 1px solid var(--line); background: var(--panel); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; margin-left: -1px;
}
.tog:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.tog:last-child { border-radius: 0 6px 6px 0; }
.tog.on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); position: relative; z-index: 1; }
.swatches { gap: 4px; }
.swatch { width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--panel); outline: 1px solid var(--line); background: var(--c); cursor: pointer; padding: 0; }
.objops { gap: 4px; }
.hint { color: var(--muted); margin-left: auto; }

/* ---------- viewer ---------- */
.viewer { flex: 1; overflow: auto; position: relative; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 22px; padding: 22px 16px 80px; min-width: min-content; }
.page { display: flex; flex-direction: column; gap: 6px; }
.pagebar { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 12px; }
.pagebar .num { margin-right: auto; font-variant-numeric: tabular-nums; }
.pagebar button { border: 0; background: transparent; cursor: pointer; color: var(--muted); padding: 3px; border-radius: 5px; display: inline-flex; }
.pagebar button:hover { background: var(--panel); color: var(--text); }
.pagebar button.del:hover { color: var(--danger); }
.pagebar svg { width: 16px; height: 16px; }
.sheet { position: relative; background: #fff; box-shadow: var(--shadow); }
.page.current .sheet { outline: 2px solid var(--accent); outline-offset: 2px; }
.sheet canvas.bg { position: absolute; left: 0; top: 0; pointer-events: none; }
.sheet .canvas-container { position: absolute !important; left: 0; top: 0; }
.sheet .loading { position: absolute; inset: 0; display: grid; place-items: center; color: #999; font-size: 12px; }

.viewer.tool-text .upper-canvas, .viewer.tool-date .upper-canvas { cursor: text !important; }
.viewer.tool-check .upper-canvas, .viewer.tool-cross .upper-canvas,
.viewer.tool-rect .upper-canvas, .viewer.tool-ellipse .upper-canvas,
.viewer.tool-line .upper-canvas, .viewer.tool-whiteout .upper-canvas { cursor: crosshair !important; }

/* ---------- empty state ---------- */
.empty { max-width: 640px; margin: 7vh auto 0; padding: 0 16px; display: flex; flex-direction: column; gap: 16px; }
.drop {
  border: 2px dashed var(--line); border-radius: 14px; background: var(--panel);
  padding: 40px 24px; text-align: center; transition: border-color .15s, background .15s;
}
.drop svg { width: 48px; height: 48px; color: var(--accent); stroke-width: 1.4; }
.drop h1 { font-size: 20px; margin: 10px 0 4px; }
.drop p { margin: 6px 0; color: var(--muted); }
.drop .formats { font-size: 12.5px; max-width: 460px; margin: 14px auto 0; }
body.dragging .drop, body.dragging .pages { border-color: var(--accent); background: var(--accent-soft); }
.linkbtn { border: 0; background: none; color: var(--accent); cursor: pointer; font-weight: 600; text-decoration: underline; padding: 0; }
.privacy { font-size: 13px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.privacy strong { color: var(--text); }

/* ---------- dialogs ---------- */
.dlg { border: 0; border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3); width: min(620px, calc(100vw - 32px)); max-height: calc(100vh - 32px); }
.dlg.small { width: min(420px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(20,18,15,.45); }
.dlg-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 12px; margin: 0; }
.dlg h2 { margin: 0; font-size: 17px; }
.dlg p { margin: 0; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dlg input:not([type]), .dlg input[type=text], .dlg input[type=password], .dlg select {
  height: 34px; border: 1px solid var(--line); border-radius: 6px; padding: 0 8px; background: var(--panel); color: var(--text); font: inherit;
}
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row label { display: inline-flex; align-items: center; gap: 6px; }
.row .sp { flex: 1; }
.grow { flex: 1; }
.muted { color: var(--muted); font-size: 13px; }
.remember { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: none; padding: 8px 12px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.padwrap { position: relative; border: 1px solid var(--line); border-radius: 10px; background: #fff; height: 220px; touch-action: none; }
#sigPad { width: 100%; height: 100%; display: block; border-radius: 10px; cursor: crosshair; }
.padline { position: absolute; left: 8%; right: 8%; bottom: 52px; border-bottom: 1px dashed #c9c4bb; pointer-events: none; }
.padhint { position: absolute; left: 0; right: 0; bottom: 22px; text-align: center; color: #aaa39a; font-size: 12px; pointer-events: none; }
.dot { width: 24px; height: 24px; border-radius: 50%; background: var(--c); border: 3px solid var(--panel); outline: 1px solid var(--line); cursor: pointer; padding: 0; }
.dot.active { outline: 2px solid var(--accent); }
.uploadbox { border: 2px dashed var(--line); border-radius: 10px; padding: 22px; text-align: center; display: flex; flex-direction: column; gap: 8px; align-items: center; color: var(--muted); }
.sigpreview { max-width: 100%; max-height: 220px; align-self: center; background: repeating-conic-gradient(#eee 0 25%, #fff 0 50%) 0 0 / 16px 16px; border-radius: 6px; }
.savedlist { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.saved { position: relative; border: 1px solid var(--line); border-radius: 8px; background: #fff; height: 80px; display: grid; place-items: center; cursor: pointer; padding: 6px; }
.saved.sel { outline: 2px solid var(--accent); }
.saved img { max-width: 100%; max-height: 100%; }
.saved .x { position: absolute; top: 2px; right: 2px; border: 0; background: rgba(0,0,0,.06); border-radius: 50%; width: 22px; height: 22px; cursor: pointer; color: #555; font-size: 14px; line-height: 1; }
.fmt { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.fmt label { border: 1px solid var(--line); border-radius: 10px; padding: 10px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.fmt label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.fmt input { position: absolute; opacity: 0; pointer-events: none; }
.fmt small { color: var(--muted); font-size: 12px; }

/* ---------- busy + toast ---------- */
.busy { position: fixed; inset: 0; background: rgba(20,18,15,.35); display: grid; place-content: center; justify-items: center; gap: 12px; z-index: 50; color: #fff; font-weight: 600; }
.busy[hidden] { display: none; }
.spinner { width: 36px; height: 36px; border: 4px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: #2a2824; color: #fff; padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 32px); z-index: 60; font-size: 13.5px; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); }

@media (max-width: 1340px) { .brand { display: none; } }
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; row-gap: 4px; }
  .actions { margin-left: auto; border-left: 0; }
  .actions .btn, .filegroup .btn { padding: 0 10px; }
  .toolbar { order: 3; flex-basis: 100%; }
  .toolbar > .group:first-child { padding-left: 0; border-left: 0; }
  .propbar { flex-wrap: nowrap; overflow-x: auto; white-space: nowrap; min-height: 46px; }
  .prop { flex: none; }
  .topbar { padding: 4px 8px; }
  .propbar { gap: 10px; padding: 6px 8px; }
  .hint { display: none; }
  .fmt { grid-template-columns: 1fr; }
  .pages { padding: 14px 8px 80px; }
}
.btn.primary.danger { background: var(--danger); border-color: var(--danger); }

/* ---------- 功能分頁（sheet） ---------- */
:root { --tab-edit: #c2410c; --tab-md: #0f766e; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --tab-edit: #f97316; --tab-md: #2dd4bf; } }
:root[data-theme="dark"] { --tab-edit: #f97316; --tab-md: #2dd4bf; }
body[data-sheet="md"] { --accent: #0f766e; --accent-soft: #d8efeb; --accent-ink: #ffffff; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) body[data-sheet="md"] { --accent: #2dd4bf; --accent-soft: #12302c; --accent-ink: #0b1f1c; }
}
:root[data-theme="dark"] body[data-sheet="md"] { --accent: #2dd4bf; --accent-soft: #12302c; --accent-ink: #0b1f1c; }
body[data-sheet="md"] .edit-only, body:not([data-sheet="md"]) .md-only { display: none !important; }

.sheetbar { display: flex; align-items: flex-end; gap: 4px; padding: 6px 12px 0; background: var(--bg); border-bottom: 1px solid var(--line); flex: none; }
.sheettab {
  display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 16px; margin-bottom: -1px;
  border: 1px solid var(--line); border-bottom-color: transparent; border-radius: 9px 9px 0 0;
  background: transparent; color: var(--muted); cursor: pointer; font-weight: 600; white-space: nowrap;
}
.sheettab svg { color: var(--tabc); }
.sheettab[data-sheet="edit"] { --tabc: var(--tab-edit); }
.sheettab[data-sheet="md"] { --tabc: var(--tab-md); }
.sheettab:hover { color: var(--text); background: var(--panel-2); }
.sheettab[aria-selected="true"] { background: var(--panel); color: var(--text); border-bottom-color: var(--panel); box-shadow: inset 0 3px 0 var(--tabc); }
.sheettab .tag { font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 4px; background: var(--tabc); color: #fff; letter-spacing: .5px; }
.sheetnote { margin-left: auto; align-self: center; font-size: 12px; color: var(--muted); padding-bottom: 4px; }

/* ---------- 轉 Markdown ---------- */
.mdsheet { flex: 1; overflow: auto; background: var(--panel); }
.md-wrap { max-width: 1180px; margin: 0 auto; padding: 22px 20px 40px; display: flex; flex-direction: column; gap: 16px; min-height: 100%; }
.md-head { display: flex; align-items: flex-start; gap: 16px; justify-content: space-between; padding-left: 12px; border-left: 4px solid var(--accent); }
.md-head h1 { margin: 0 0 4px; font-size: 21px; }
.md-head p { margin: 0; color: var(--muted); max-width: 720px; }
.md-drop { border: 2px dashed var(--line); border-radius: 14px; padding: 36px 24px; text-align: center; background: var(--panel-2); transition: border-color .15s, background .15s; }
.md-drop > svg { width: 52px; height: 52px; color: var(--accent); stroke-width: 1.4; }
.md-drop h2 { margin: 8px 0 4px; font-size: 19px; }
.md-drop p { margin: 4px 0; color: var(--muted); }
body.dragging[data-sheet="md"] .md-drop, body.dragging[data-sheet="md"] .md-work { border-color: var(--accent); background: var(--accent-soft); }
.md-formats { list-style: none; padding: 0; margin: 18px auto 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 8px; max-width: 760px; text-align: left; }
.md-formats li { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; color: var(--muted); }
.md-formats b { display: block; color: var(--text); font-size: 14px; }

.md-work { display: grid; grid-template-columns: 290px 1fr; gap: 16px; flex: 1; min-height: 480px; border-radius: 12px; }
.md-side { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.md-list { display: flex; flex-direction: column; gap: 6px; }
.md-item { display: flex; align-items: stretch; border: 1px solid var(--line); border-radius: 9px; background: var(--panel); overflow: hidden; }
.md-item.sel { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.md-pick { flex: 1; min-width: 0; display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 8px; padding: 8px 10px; border: 0; background: none; cursor: pointer; text-align: left; }
.md-pick .kind { grid-row: span 2; align-self: center; font-size: 11px; font-weight: 700; min-width: 44px; text-align: center; padding: 4px 6px; border-radius: 6px; background: var(--panel-2); color: var(--muted); }
.md-pick .kind[data-kind="pdf"] { background: #fde2dd; color: #b42318; }
.md-pick .kind[data-kind="docx"], .md-pick .kind[data-kind="odt"] { background: #dde7fb; color: #1d4ed8; }
.md-pick .kind[data-kind="pptx"], .md-pick .kind[data-kind="odp"] { background: #fde6d4; color: #c2410c; }
.md-pick .kind[data-kind="sheet"], .md-pick .kind[data-kind="csv"] { background: #d9f2df; color: #15803d; }
.md-pick .nm { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.md-pick .st { font-size: 12px; color: var(--muted); }
.md-item.st-error .st { color: var(--danger); }
.md-item.st-working .st { color: var(--accent); }
.md-item .x { border: 0; background: none; color: var(--muted); font-size: 18px; width: 34px; cursor: pointer; }
.md-item .x:hover { color: var(--danger); background: var(--panel-2); }
.md-opts { border: 1px solid var(--line); border-radius: 9px; padding: 8px 12px 10px; margin: 4px 0 0; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.md-opts legend { color: var(--muted); padding: 0 4px; font-size: 12px; }
.md-opts label { display: flex; gap: 6px; align-items: center; }
.md-opts .md-sub { padding-left: 22px; color: var(--muted); }
.md-opts select { height: 28px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--text); font: inherit; padding: 0 4px; }
.md-opts:has(#mdOptOcr:not(:checked)) .md-sub { opacity: .45; }
.md-pick .kind[data-kind="image"] { background: #ede4fb; color: #6d28d9; }

.md-main { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.md-bar { display: flex; align-items: center; gap: 8px; }
.md-title { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px; }
.md-title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.md-title span { color: var(--muted); font-size: 12.5px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.md-warn { border: 1px solid #f5d38a; background: #fff8e6; color: #7a4b00; border-radius: 8px; padding: 8px 12px; font-size: 13px; }
.md-warn.err { border-color: #f3b4ad; background: #fdecea; color: var(--danger); }
.md-warn p { margin: 0; }
.md-warn p + p { margin-top: 4px; }
#mdText {
  flex: 1; min-height: 420px; width: 100%; resize: vertical; border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px;
  font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, "Noto Sans Mono CJK TC", monospace; background: var(--panel-2); color: var(--text); tab-size: 2;
}
#mdText:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.md-foot { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; border-top: 1px solid var(--line); padding-top: 12px; }
.md-foot .sp { flex: 1; }
.md-foot select { height: 34px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--text); padding: 0 6px; font: inherit; }
.mdsheet .privacy { margin: 0; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .md-warn { border-color: #6b5313; background: #2d2510; color: #f5d38a; }
  :root:not([data-theme="light"]) .md-pick .kind[data-kind] { filter: saturate(.8) brightness(.9); }
}

@media (max-width: 720px) {
  .sheetbar { padding: 6px 8px 0; }
  .sheettab { padding: 0 12px; }
  .sheetnote { display: none; }
  .md-wrap { padding: 16px 12px 32px; }
  .md-head { flex-direction: column; }
  .md-work { grid-template-columns: 1fr; }
  #mdText { min-height: 55vh; }
  .md-foot select { flex: 1 1 100%; }
}
