/* 便利貼寫作台：左邊自由擺放的桌面，右邊拼貼用的草稿紙。窄於 860px 時用分頁切換。 */
:root {
  --bg: #F3EFE8;
  --surface: #FFFFFF;
  --ink: #2A2620;
  --muted: #6E665A;
  --line: #DDD5C8;
  --accent: #2F6F62;
  --accent-ink: #FFFFFF;
  --accent-soft: #DCEEE9;
  --focus: #3B63C6;
  --desk: #E7DFD1;
  --desk-dot: rgba(90, 70, 40, .16);
  --paper: #FFFDF8;
  --tape: rgba(255, 255, 255, .62);
  --hl: #E2A400;
  --shadow: rgba(70, 50, 20, .30);
  --pin: #D24A3C;
  --p0: #FBE7A1; --p0i: #5A4300;
  --p1: #CBEBD6; --p1i: #1F4D33;
  --p2: #CFE2F6; --p2i: #1C3D5E;
  --p3: #F7D2DD; --p3i: #6A2238;
  --p4: #E1D8F4; --p4i: #3D2D66;
  --p5: #E7DFCF; --p5i: #4A4234;
  --font-ui: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  --font-serif: "Noto Serif TC", "Songti TC", "PMingLiU", serif;
  --font-hand: "LXGW WenKai TC", "Kaiti TC", "DFKai-SB", "BiauKai", var(--font-serif);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171A; --surface: #1F2226; --ink: #ECE7DE; --muted: #A49C90; --line: #343840;
    --accent: #6CC5B2; --accent-ink: #0D2620; --accent-soft: #1D3A34; --focus: #8DA6F0;
    --desk: #1B1D21; --desk-dot: rgba(255, 255, 255, .07); --paper: #24272C; --tape: rgba(255, 255, 255, .16);
    --hl: #F2C14E; --shadow: rgba(0, 0, 0, .55); --pin: #F0715F;
    --p0: #4A4120; --p0i: #F6E7AE; --p1: #22402F; --p1i: #CBEBD5; --p2: #223A52; --p2i: #D2E5F8;
    --p3: #4A2532; --p3i: #F6CFDA; --p4: #352C4E; --p4i: #E2D8F6; --p5: #3B362D; --p5i: #E8E0D0;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171A; --surface: #1F2226; --ink: #ECE7DE; --muted: #A49C90; --line: #343840;
  --accent: #6CC5B2; --accent-ink: #0D2620; --accent-soft: #1D3A34; --focus: #8DA6F0;
  --desk: #1B1D21; --desk-dot: rgba(255, 255, 255, .07); --paper: #24272C; --tape: rgba(255, 255, 255, .16);
  --hl: #F2C14E; --shadow: rgba(0, 0, 0, .55); --pin: #F0715F;
  --p0: #4A4120; --p0i: #F6E7AE; --p1: #22402F; --p1i: #CBEBD5; --p2: #223A52; --p2i: #D2E5F8;
  --p3: #4A2532; --p3i: #F6CFDA; --p4: #352C4E; --p4i: #E2D8F6; --p5: #3B362D; --p5i: #E8E0D0;
  color-scheme: dark;
}
.p0 { --pb: var(--p0); --pi: var(--p0i); } .p1 { --pb: var(--p1); --pi: var(--p1i); }
.p2 { --pb: var(--p2); --pi: var(--p2i); } .p3 { --pb: var(--p3); --pi: var(--p3i); }
.p4 { --pb: var(--p4); --pi: var(--p4i); } .p5 { --pb: var(--p5); --pi: var(--p5i); }

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font-ui); font-size: 15px; line-height: 1.6; }
button, select, input, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
body.dragging, body.dragging * { cursor: grabbing !important; user-select: none !important; -webkit-user-select: none !important; }

.app {
  height: 100%; display: flex; flex-direction: column; gap: 10px;
  max-width: 1500px; margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.top { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.top h1 { margin: 0 6px 0 0; font: 600 19px/1.3 var(--font-serif); letter-spacing: .04em; }
.home { text-decoration: none; color: var(--muted); font-size: 18px; padding: 2px 6px; border-radius: 6px; }
.home:hover { color: var(--ink); background: var(--surface); }
.grow { flex: 1; min-width: 0; }
.field { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; min-height: 36px; min-width: 0; }
.field::placeholder { color: var(--muted); }
#proj { max-width: 200px; }
#pname { width: 180px; }
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 6px 12px; min-height: 36px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 500; }
.btn.danger:hover { border-color: #C0392B; color: #C0392B; }
.btn[disabled] { opacity: .45; cursor: default; }
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu .sheet {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 50; min-width: 210px; display: flex; flex-direction: column; gap: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px; box-shadow: 0 8px 28px var(--shadow);
}
.who { margin: 0 2px 4px; font-size: 12px; color: var(--muted); word-break: break-all; }

.tabs { display: none; gap: 8px; }
.tab { flex: 1; min-height: 42px; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; cursor: pointer; font-weight: 500; }
.tab[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.panes { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); gap: 14px; }
.pane { position: relative; min-width: 0; min-height: 0; display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }

/* ---------- desk ---------- */
.desk-pane { background: var(--desk); }
.desk-head { display: flex; flex-direction: column; gap: 8px; padding: 10px 12px; background: var(--surface); border-bottom: 1px solid var(--line); }
.capture { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.capture textarea {
  flex: 1 1 260px; min-width: 0; resize: none; height: 40px; overflow: hidden; border: 1px dashed var(--line); border-radius: 8px;
  padding: 8px 12px; background: var(--bg); font: 16px/1.5 var(--font-hand);
}
.capture textarea:focus { border-style: solid; outline: 2px solid var(--focus); outline-offset: -1px; }
.catpick { display: flex; gap: 4px; flex-wrap: wrap; }
.catpick button {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 3px 10px 3px 4px; min-height: 30px; font-size: 13px; cursor: pointer; color: var(--muted);
}
.catpick button i { width: 20px; height: 20px; border-radius: 4px; background: var(--pb); transform: rotate(-6deg); box-shadow: 0 1px 2px var(--shadow); }
.catpick button[aria-checked="true"] { border-color: var(--ink); color: var(--ink); font-weight: 500; }
.desk-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.desk-tools .field { width: 180px; }
.qcount { font-size: 13px; color: var(--muted); }

.desk-scroll { flex: 1; min-height: 0; overflow: auto; position: relative; overscroll-behavior: contain; }
.desk {
  position: relative; min-width: 100%; min-height: 100%;
  background-image: radial-gradient(var(--desk-dot) 1.2px, transparent 1.4px);
  background-size: 22px 22px;
}
.desk-empty { position: absolute; left: 40px; top: 40px; max-width: 360px; color: var(--muted); font: 17px/1.8 var(--font-hand); pointer-events: none; }
.desk-empty p { margin: 0 0 4px; }
.hint { margin: 0; padding: 6px 12px; font-size: 12px; color: var(--muted); background: var(--surface); border-top: 1px solid var(--line); }

.note {
  position: absolute; width: 176px; min-height: 86px; padding: 20px 14px 14px;
  background: var(--pb); color: var(--pi);
  font: 17px/1.65 var(--font-hand); letter-spacing: .02em;
  border-radius: 2px 2px 3px 14px / 2px 2px 3px 6px;
  transform: rotate(var(--r));
  box-shadow: 0 1px 1px var(--shadow), 0 8px 14px -8px var(--shadow);
  cursor: grab; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: transform .16s ease, box-shadow .16s ease, opacity .16s ease;
}
.note::before {
  content: ""; position: absolute; top: -9px; left: 50%; width: 62px; height: 20px; margin-left: -31px;
  background: var(--tape); transform: rotate(calc(var(--r) * -1.8));
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .03);
}
.note:hover { transform: rotate(var(--r)) translateY(-2px); box-shadow: 0 2px 2px var(--shadow), 0 14px 22px -10px var(--shadow); }
.note.sel { transform: rotate(0deg); outline: 2px solid var(--focus); outline-offset: 4px; cursor: default; }
.note.editing { cursor: text; }
.note.used { opacity: .5; }
.note.used:hover, .note.used.sel { opacity: .9; }
.note.dim { opacity: .16; }
.note.hit { outline: 3px solid var(--hl); outline-offset: 3px; }
.note.placeholder { opacity: .18; transition: none; }
.note.pop { animation: pop .3s ease-out; }
@keyframes pop { from { transform: rotate(var(--r)) translateY(-14px) scale(1.12); opacity: 0; } }
.ntext { white-space: pre-wrap; word-break: break-word; min-height: 1.6em; }
.ntext .ph { opacity: .5; }
.note textarea { display: block; width: 100%; border: 0; background: transparent; resize: none; overflow: hidden; padding: 0; outline: 0; font: inherit; color: inherit; letter-spacing: inherit; }
.nmeta { margin-top: 8px; }
.stamp {
  display: inline-block; font: 500 11px/1.5 var(--font-ui); padding: 0 6px; border: 1.5px solid currentColor; border-radius: 4px;
  transform: rotate(-5deg); opacity: .85; letter-spacing: .05em;
}
.pin { position: absolute; top: -5px; right: 14px; width: 15px; height: 15px; border-radius: 50%; background: var(--pin); box-shadow: inset -2px -2px 0 rgba(0,0,0,.18), 0 2px 3px var(--shadow); }

.ghost { position: fixed !important; left: 0; top: 0; z-index: 1000; pointer-events: none; margin: 0; transition: none !important; animation: none !important;
  box-shadow: 0 22px 36px -12px var(--shadow), 0 2px 4px var(--shadow) !important; opacity: .96; }
.ghost.strip { display: inline-block; max-width: 340px; padding: 4px 8px; font: 17px/1.9 var(--font-serif); }

.tools {
  position: absolute; z-index: 9000; display: flex; align-items: center; gap: 4px; padding: 4px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 22px var(--shadow);
  white-space: nowrap; font: 13px/1 var(--font-ui); color: var(--ink);
}
.tools button { height: 30px; min-width: 30px; padding: 0 8px; border: 1px solid transparent; background: transparent; border-radius: 6px; cursor: pointer; }
.tools button:hover { border-color: var(--line); background: var(--bg); }
.tools button.go { background: var(--accent); color: var(--accent-ink); font-weight: 500; }
.tools .sw { width: 22px; min-width: 22px; height: 22px; padding: 0; border-radius: 4px; background: var(--pb); border: 2px solid transparent; }
.tools .sw.on { border-color: var(--ink); }
.tools .sep { width: 1px; height: 20px; background: var(--line); margin: 0 2px; }

/* drawer panels over the desk */
.drawer { position: absolute; left: 12px; right: 12px; top: 12px; z-index: 60; max-height: calc(100% - 24px); overflow: auto; }
.panel { padding: 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); box-shadow: 0 10px 30px var(--shadow); }
.panel h3 { margin: 0 0 6px; font-size: 15px; }
.panel p { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.panel textarea { width: 100%; height: 150px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
.panel .row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; align-items: center; }
.lbl { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.catlist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.catlist li { display: flex; align-items: center; gap: 6px; }
.catlist .sw { width: 30px; height: 30px; border-radius: 4px; border: 1px solid var(--line); background: var(--pb); cursor: pointer; transform: rotate(-4deg); }
.catlist input { flex: 1; }
.catlist .n { font-size: 12px; color: var(--muted); min-width: 3em; text-align: right; }
.catlist .x { border: 1px solid transparent; background: transparent; width: 32px; height: 32px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.catlist .x:hover { border-color: var(--line); color: var(--ink); }

/* ---------- draft ---------- */
.draft-pane { background: var(--bg); }
.draft-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; background: var(--surface); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.draft-head .title { flex: 1; min-width: 140px; font: 600 16px var(--font-serif); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 6px 12px; min-height: 34px; cursor: pointer; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--surface); }
.draft-scroll { flex: 1; min-height: 0; overflow: auto; padding: 18px 16px 40px; }
.paper {
  position: relative; max-width: 720px; min-height: 100%; margin: 0 auto; padding: 30px 34px 40px;
  background: var(--paper); border-radius: 4px;
  box-shadow: 0 1px 2px var(--shadow), 0 12px 28px -18px var(--shadow);
  font: 17px/2.05 var(--font-serif); letter-spacing: .03em;
}
.paper-empty { color: var(--muted); font-size: 15px; line-height: 1.9; margin: 0 0 10px; }
.paper-empty b { color: var(--ink); }
.para { margin: 0; }
.hblock { margin: 0; font-weight: 700; font-size: 1.14em; }
.strip {
  background: var(--pb); color: var(--pi); padding: 3px 4px; margin: 0 1px; border-radius: 2px;
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
  cursor: grab; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: background .15s, opacity .15s;
}
.strip.free { background: transparent; color: var(--ink); border-bottom: 1.5px dashed var(--line); padding-inline: 1px; }
.strip.head { background: transparent; color: var(--ink); border-bottom: 2px solid var(--ink); padding-inline: 1px; }
.strip:hover { filter: brightness(.97); }
.strip.sel { outline: 2px solid var(--focus); outline-offset: 1px; }
.strip.editing { cursor: text; user-select: text; -webkit-user-select: text; outline: 2px solid var(--focus); background: var(--surface); color: var(--ink); filter: none; }
.strip.placeholder { opacity: .22; }
.strip.flash { animation: flash 1.1s ease-out; }
@keyframes flash { 0%, 30% { outline: 3px solid var(--hl); outline-offset: 2px; } 100% { outline: 3px solid transparent; outline-offset: 2px; } }
.caret { display: inline-block; width: 0; height: 1em; position: relative; }
.caret::after { content: ""; position: absolute; left: -2px; top: -.82em; width: 3px; height: 1.55em; border-radius: 2px; background: var(--accent); }
.gap { position: relative; height: 22px; margin: 2px -10px; border-radius: 6px; display: flex; align-items: center; justify-content: center; font: 12px var(--font-ui); color: var(--muted); }
.gap.end { height: 44px; margin-top: 8px; border: 1.5px dashed transparent; }
.gap.end::after { content: "拖到這裡，另起一段"; opacity: .7; }
.paper.is-empty .gap.end { height: 120px; border-color: var(--line); }
.paper.is-empty .gap.end::after { content: "把便利貼拖到這裡"; }
.gap.on { background: var(--accent-soft); outline: 2px dashed var(--accent); color: var(--accent); }
.gap.on::after { content: "放開，另起一段" !important; opacity: 1 !important; }
.gap .merge { opacity: 0; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; font-size: 12px; padding: 0 10px; height: 20px; cursor: pointer; color: var(--muted); }
.gap:hover .merge, .gap .merge:focus-visible { opacity: 1; }
body.dragging .gap .merge { display: none; }

.read h2 { font: 700 1.35em/1.6 var(--font-serif); margin: 0 0 1.1em; }
.read h3 { font: 700 1.12em/1.8 var(--font-serif); margin: 1.4em 0 .5em; }
.read p { margin: 0 0 1.1em; }
.read .none { color: var(--muted); }

.foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; padding: 10px 12px; background: var(--surface); border-top: 1px solid var(--line); }
.stats { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stats .save { display: block; font-size: 12px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.fallback { max-width: 720px; margin: 14px auto 0; }

#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(18px + env(safe-area-inset-bottom, 0px)); z-index: 2000;
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 13px; max-width: calc(100% - 32px); opacity: 0; pointer-events: none; transition: opacity .15s; }
#toast.on { opacity: 1; }

@media (max-width: 859px) {
  .tabs { display: flex; }
  .panes { grid-template-columns: minmax(0, 1fr); }
  .pane { display: none; }
  .app[data-view="desk"] #deskPane { display: flex; }
  .app[data-view="draft"] #draftPane { display: flex; }
  #pname { flex: 1 1 140px; width: auto; }
  .paper { padding: 22px 18px 30px; }
  .draft-scroll { padding: 10px 8px 30px; }
  .hint { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .note, .strip, #toast { transition: none; }
  .note.pop, .strip.flash { animation: none; }
}
