/* Marginalia — cross-platform handwriting notebook
   Palette: iron-gall teal ink on a cool desk; highlighter yellow for "found" marks. */
:root {
  --desk: #E4E8EC;
  --desk-2: #D9DEE4;
  --surface: #FBFCFD;
  --surface-2: #F1F4F6;
  --ink: #18202B;
  --muted: #5E6B7A;
  --line: #D2D9E0;
  --accent: #176D74;
  --accent-ink: #FFFFFF;
  --accent-soft: #D6ECEC;
  --mark: #F3D34A;
  --danger: #B3372F;
  --ok: #2E7D4F;
  --shadow: 0 1px 2px rgba(20, 34, 48, .08), 0 6px 20px rgba(20, 34, 48, .10);
  --shadow-lg: 0 10px 40px rgba(20, 34, 48, .22);
  --radius: 10px;
  --font: "PingFang TC", "Microsoft JhengHei UI", "Microsoft JhengHei", "Noto Sans TC", "Noto Sans CJK TC", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Bricolage Grotesque", var(--font);
  --font-mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
  --top-h: 52px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --desk: #0F1216; --desk-2: #151A20; --surface: #1A1F26; --surface-2: #222831;
    --ink: #E4E8EE; --muted: #93A0AE; --line: #2C343E;
    --accent: #4FB7BE; --accent-ink: #0B1B1D; --accent-soft: #1D3A3D;
    --mark: #E8C53A; --danger: #E0655C; --ok: #5DBB84;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
    --shadow-lg: 0 10px 40px rgba(0,0,0,.55);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --desk: #0F1216; --desk-2: #151A20; --surface: #1A1F26; --surface-2: #222831;
  --ink: #E4E8EE; --muted: #93A0AE; --line: #2C343E;
  --accent: #4FB7BE; --accent-ink: #0B1B1D; --accent-soft: #1D3A3D;
  --mark: #E8C53A; --danger: #E0655C; --ok: #5DBB84;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
  --shadow-lg: 0 10px 40px rgba(0,0,0,.55);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--desk); color: var(--ink);
  font: 14px/1.45 var(--font);
  overflow: hidden; overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px;
  border: 0; border-radius: 9px; background: transparent; color: var(--ink);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.sm { width: 30px; height: 30px; }
.icon-btn.sm .ic { width: 17px; height: 17px; }
.icon-btn.on { background: var(--accent-soft); color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px;
  border-radius: 9px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  white-space: nowrap; font-weight: 500;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn .ic { width: 18px; height: 18px; }

#app { position: fixed; inset: 0; }
.view { position: absolute; inset: 0; }

/* ---------------- Library ---------------- */
#library { display: grid; grid-template-columns: 248px 1fr; }
.lib-side {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-right: 1px solid var(--line);
  padding: calc(14px + var(--safe-t)) 12px calc(12px + var(--safe-b));
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; }
.brand-mark {
  width: 26px; height: 30px; border-radius: 3px 7px 7px 3px; background: var(--accent); position: relative;
  box-shadow: inset 5px 0 0 rgba(0,0,0,.18);
}
.brand-mark::after { content: ""; position: absolute; right: -5px; top: 6px; bottom: 6px; width: 3px; border-radius: 2px; background: var(--mark); }
.brand-word { font-family: var(--font-display); font-weight: 700; font-size: 20px; letter-spacing: -.01em; }
.side-nav { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 1px; }
.side-group { margin-top: 14px; }
.side-label {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 11px; font-weight: 600; letter-spacing: .08em; color: var(--muted); padding: 4px 8px;
}
.side-item {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 36px; padding: 6px 8px;
  border: 0; background: transparent; border-radius: 8px; text-align: left; color: var(--ink);
}
.side-item:hover { background: var(--surface-2); }
.side-item.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side-item .ic { width: 18px; height: 18px; }
.side-item .count { margin-left: auto; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.side-item.drop { outline: 2px dashed var(--accent); }
.side-item .tag-dot { width: 9px; height: 9px; border-radius: 50%; margin: 0 4px; flex: none; }
.side-foot { display: flex; align-items: center; gap: 6px; padding-top: 10px; border-top: 1px solid var(--line); }
.sync-pill {
  flex: 1; display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 10px;
  border-radius: 9px; border: 1px solid var(--line); background: var(--surface-2); font-size: 13px; text-align: left;
}
.sync-pill.ok { color: var(--ok); }
.sync-pill.busy .ic { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

.lib-main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.lib-head {
  display: flex; align-items: center; gap: 12px;
  padding: calc(12px + var(--safe-t)) 24px 10px;
}
.search-field {
  flex: 1; display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 12px; max-width: 620px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; color: var(--muted);
}
.search-field:focus-within { border-color: var(--accent); }
.search-field input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--ink); }
kbd { font-family: var(--font-mono); font-size: 11px; padding: 2px 6px; border-radius: 5px; border: 1px solid var(--line); color: var(--muted); }
.lib-actions { display: flex; gap: 8px; margin-left: auto; }
.lib-bar { display: flex; align-items: center; gap: 12px; padding: 4px 24px 8px; }
.crumbs { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; min-width: 0; }
.crumbs h1 { font-family: var(--font-display); font-weight: 700; font-size: 26px; letter-spacing: -.015em; margin: 0; text-wrap: balance; }
.crumbs button { border: 0; background: none; color: var(--muted); font-size: 15px; padding: 2px 4px; border-radius: 6px; }
.crumbs button:hover { color: var(--ink); background: var(--surface-2); }
.crumbs .sep { color: var(--muted); }
.lib-bar-tools { margin-left: auto; display: flex; align-items: center; gap: 6px; }
select {
  height: 34px; padding: 0 28px 0 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface);
  appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 55%, calc(100% - 10px) 55%; background-size: 5px 5px; background-repeat: no-repeat;
}
.lib-scroll { flex: 1; overflow: auto; padding: 6px 24px calc(40px + var(--safe-b)); -webkit-overflow-scrolling: touch; }

.folder-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; margin-bottom: 18px; }
.folder-row:empty { display: none; }
.folder-card {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); text-align: left; min-width: 0;
}
.folder-card:hover { border-color: var(--muted); }
.folder-card.drop { outline: 2px dashed var(--accent); }
.folder-card .ic { color: var(--accent); }
.folder-card .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.folder-card .count { color: var(--muted); font-size: 12px; }

.nb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); gap: 26px 20px; }
.nb-card { position: relative; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.nb-cover {
  position: relative; aspect-ratio: 3 / 4; width: 100%; max-width: 100%; border-radius: 4px 10px 10px 4px; overflow: hidden;
  background: #fff; border: 0; padding: 0; box-shadow: var(--shadow);
  transition: transform .15s ease, box-shadow .15s ease;
}
.nb-cover:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.nb-cover img { width: 100%; height: 100%; object-fit: cover; object-position: left top; display: block; }
.nb-cover .spine { position: absolute; left: 0; top: 0; bottom: 0; width: 9px; background: var(--c, var(--accent)); box-shadow: inset -2px 0 0 rgba(0,0,0,.15); }
.nb-cover .kind {
  position: absolute; right: 8px; bottom: 8px; display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 7px; border-radius: 99px; background: rgba(24, 32, 43, .72); color: #fff; font-size: 11px;
}
.nb-cover .kind .ic { width: 13px; height: 13px; }
.nb-cover .ph { position: absolute; inset: 0; display: grid; place-items: center; color: #9AA6B2; background:
  repeating-linear-gradient(#fff 0 23px, #E3E8EE 23px 24px); }
.nb-meta { display: flex; align-items: flex-start; gap: 4px; min-width: 0; }
.nb-meta .txt { flex: 1; min-width: 0; }
.nb-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nb-sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nb-tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 3px; }
.chip { display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 7px; border-radius: 99px; font-size: 11px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.fav-badge { position: absolute; top: 7px; right: 7px; color: var(--mark); filter: drop-shadow(0 1px 1px rgba(0,0,0,.35)); }
.fav-badge .ic { width: 18px; height: 18px; fill: currentColor; }

.nb-grid.searching { display: block; }
.nb-grid.list { grid-template-columns: 1fr; gap: 2px; }
.nb-grid.list .nb-card { flex-direction: row; align-items: center; gap: 14px; padding: 8px; border-radius: 10px; }
.nb-grid.list .nb-card:hover { background: var(--surface); }
.nb-grid.list .nb-cover { width: 44px; flex: none; }
.nb-grid.list .nb-cover .kind, .nb-grid.list .fav-badge { display: none; }
.nb-grid.list .nb-meta { flex: 1; }

.empty { display: grid; place-items: center; text-align: center; padding: 60px 16px; color: var(--muted); gap: 10px; }
.empty h2 { font-family: var(--font-display); color: var(--ink); margin: 0; font-size: 22px; }

.search-results { display: flex; flex-direction: column; gap: 18px; max-width: 900px; }
.sr-group h3 { font-size: 12px; letter-spacing: .08em; color: var(--muted); margin: 0 0 6px; font-weight: 600; }
.sr-item {
  display: flex; gap: 12px; align-items: flex-start; width: 100%; text-align: left; padding: 10px 12px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface); margin-bottom: 6px;
}
.sr-item:hover { border-color: var(--accent); }
.sr-item .ic { color: var(--accent); margin-top: 2px; }
.sr-item .t { font-weight: 600; }
.sr-item .s { color: var(--muted); font-size: 13px; margin-top: 2px; }
.sr-item mark { background: color-mix(in srgb, var(--mark) 70%, transparent); color: inherit; border-radius: 2px; padding: 0 1px; }

/* ---------------- Editor ---------------- */
#editor { display: flex; flex-direction: column; background: var(--desk); }
.ed-top {
  display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: center; gap: 8px;
  padding: calc(6px + var(--safe-t)) 10px 6px; min-height: var(--top-h);
  background: var(--surface); border-bottom: 1px solid var(--line); z-index: 5;
}
.ed-left, .ed-right { display: flex; align-items: center; gap: 2px; min-width: 0; }
.ed-right { justify-content: flex-end; }
.ed-title {
  min-width: 0; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  border: 0; background: transparent; font-weight: 600; font-size: 15px; padding: 6px 8px; border-radius: 8px;
}
.ed-title:hover { background: var(--surface-2); }
.ed-tools { display: flex; align-items: center; gap: 2px; padding: 3px; border-radius: 12px; background: var(--surface-2); overflow-x: auto; scrollbar-width: none; }
.ed-tools::-webkit-scrollbar { display: none; }
.tool {
  position: relative; display: grid; place-items: center; width: 40px; height: 38px; flex: none;
  border: 0; border-radius: 9px; background: transparent; color: var(--ink);
}
.tool:hover { background: var(--surface); }
.tool.on { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }
.tool:disabled { opacity: .35; cursor: default; }
.tool-dot { position: absolute; bottom: 4px; left: 50%; width: 14px; height: 3px; margin-left: -7px; border-radius: 2px; background: var(--dot, transparent); }
.tool-sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; flex: none; }

.ed-sub {
  display: flex; align-items: center; gap: 14px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none;
  padding: 6px 14px; min-height: 46px; background: var(--surface); border-bottom: 1px solid var(--line); z-index: 4;
}
.ed-sub::-webkit-scrollbar { display: none; }
.ed-sub:empty { display: none; }
.sub-group { display: flex; align-items: center; gap: 6px; flex: none; }
.sub-label { font-size: 12px; color: var(--muted); margin-right: 2px; }
.swatch {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line);
  background: var(--c); padding: 0; flex: none;
}
.swatch.on { box-shadow: 0 0 0 2px var(--accent); }
.swatch.custom { background: conic-gradient(#e53935, #fdd835, #43a047, #1e88e5, #8e24aa, #e53935); position: relative; overflow: hidden; }
.swatch.custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.size-btn { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; border: 0; background: transparent; flex: none; }
.size-btn i { display: block; border-radius: 50%; background: var(--ink); }
.size-btn.on { background: var(--accent-soft); }
.size-btn.on i { background: var(--accent); }
.seg { display: inline-flex; padding: 2px; border-radius: 9px; background: var(--surface-2); border: 1px solid var(--line); flex: none; }
.seg button { height: 30px; padding: 0 10px; border: 0; background: transparent; border-radius: 7px; font-size: 13px; white-space: nowrap; }
.seg button.on { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); font-weight: 600; }
.range { display: flex; align-items: center; gap: 6px; flex: none; }
.range input { width: 110px; accent-color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; flex: none; white-space: nowrap; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

.ed-body { position: relative; flex: 1; display: flex; min-height: 0; }
.panes { flex: 1; display: flex; min-width: 0; min-height: 0; gap: 0; }
.panes.stack { flex-direction: column; }
.pane { position: relative; flex: 1; min-width: 0; min-height: 0; overflow: hidden; background: var(--desk); }
.panes > .pane + .pane { border-left: 2px solid var(--line); }
.panes.stack > .pane + .pane { border-left: 0; border-top: 2px solid var(--line); }
.panes.dual .pane.active::after { content: ""; position: absolute; inset: 0; box-shadow: inset 0 0 0 2px var(--accent); pointer-events: none; z-index: 3; }
.pane canvas { position: absolute; left: 0; top: 0; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.pane .overlay { pointer-events: none; }
.pane-head {
  position: absolute; left: 8px; top: 8px; z-index: 4; display: flex; align-items: center; gap: 6px;
  padding: 4px 6px 4px 10px; border-radius: 99px; background: color-mix(in srgb, var(--surface) 92%, transparent);
  box-shadow: var(--shadow); max-width: calc(100% - 16px);
}
.pane-head .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 600; }
.page-ind {
  position: absolute; right: 12px; bottom: calc(12px + var(--safe-b)); z-index: 4; display: flex; align-items: center; gap: 4px;
  padding: 3px 4px 3px 10px; border-radius: 99px; background: color-mix(in srgb, var(--surface) 92%, transparent); box-shadow: var(--shadow);
  font-family: var(--font-mono); font-size: 12px; color: var(--muted);
}
.page-ind button { border: 0; background: transparent; border-radius: 99px; height: 26px; padding: 0 8px; font-family: var(--font-mono); font-size: 12px; color: var(--ink); }
.page-ind button:hover { background: var(--surface-2); }
.page-ind .zoom { min-width: 48px; }

.text-editor {
  position: absolute; z-index: 6; margin: 0; padding: 2px 3px; border: 1.5px dashed var(--accent); border-radius: 3px;
  background: rgba(255,255,255,.35); outline: none; resize: none; overflow: hidden; line-height: 1.3;
  font-family: var(--font); transform-origin: 0 0; white-space: pre-wrap; word-break: break-word;
}

.float-bar {
  position: absolute; z-index: 7; display: flex; align-items: center; gap: 2px; padding: 4px;
  border-radius: 12px; background: var(--ink); color: var(--surface); box-shadow: var(--shadow-lg);
  transform: translate(-50%, -100%); white-space: nowrap;
  max-width: calc(100% - 16px); overflow-x: auto; scrollbar-width: none;
}
.float-bar button { display: inline-flex; align-items: center; gap: 5px; height: 34px; padding: 0 9px; border: 0; border-radius: 8px; background: transparent; color: inherit; font-size: 13px; }
.float-bar button:hover { background: rgba(127,127,127,.25); }
.float-bar .ic { width: 17px; height: 17px; }
.float-bar .sw { width: 18px; height: 18px; border-radius: 50%; padding: 0; border: 2px solid rgba(255,255,255,.6); background: var(--c); }

/* page panel */
.page-panel {
  width: 214px; flex: none; display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-right: 1px solid var(--line); z-index: 3;
}
.page-panel-head { display: flex; align-items: center; gap: 6px; padding: 8px 8px 8px 14px; border-bottom: 1px solid var(--line); }
.page-panel-head strong { font-size: 14px; }
.page-panel-head .muted { font-size: 12px; margin-right: auto; font-family: var(--font-mono); }
.page-list { flex: 1; overflow: auto; padding: 12px 18px; display: flex; flex-direction: column; gap: 14px; }
.pthumb { position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.pthumb .box {
  position: relative; width: 100%; border: 0; padding: 0; background: #fff; border-radius: 3px; box-shadow: 0 0 0 1px var(--line);
  overflow: hidden; display: block;
}
.pthumb .box canvas { display: block; width: 100%; height: auto; }
.pthumb.cur .box { box-shadow: 0 0 0 2.5px var(--accent); }
.pthumb .num { font-family: var(--font-mono); font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 4px; }
.pthumb .bm { color: var(--danger); }
.pthumb .bm .ic { width: 13px; height: 13px; fill: currentColor; }
.pthumb .pmenu { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 7px; border: 0; background: rgba(24,32,43,.72); color: #fff; display: grid; place-items: center; opacity: 0; }
.pthumb:hover .pmenu, .pthumb.cur .pmenu { opacity: 1; }
.pthumb .pmenu .ic { width: 16px; height: 16px; }
.pthumb.dragging { opacity: .4; }
.pthumb.drop-before::before { content: ""; position: absolute; left: -6px; right: -6px; top: -8px; height: 3px; border-radius: 2px; background: var(--accent); }
.page-panel-foot { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); flex-wrap: wrap; }

/* doc search */
.docsearch {
  position: absolute; top: calc(var(--top-h) + 54px + var(--safe-t)); right: 14px; z-index: 8; display: flex; align-items: center; gap: 6px;
  padding: 6px 6px 6px 12px; border-radius: 12px; background: var(--surface); box-shadow: var(--shadow-lg); border: 1px solid var(--line);
  width: min(420px, calc(100% - 28px));
}
.docsearch input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; height: 32px; }
.docsearch .ic { color: var(--muted); }

/* ---------------- Popovers / menus / modals ---------------- */
.popover-layer { position: fixed; inset: 0; pointer-events: none; z-index: 40; }
.menu {
  position: fixed; pointer-events: auto; min-width: 200px; max-width: min(320px, calc(100vw - 24px)); max-height: calc(100vh - 24px); overflow: auto;
  padding: 6px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow-lg);
}
.menu button, .menu .mi {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 36px; padding: 6px 10px;
  border: 0; border-radius: 8px; background: transparent; text-align: left; color: var(--ink);
}
.menu button:hover { background: var(--surface-2); }
.menu button.danger { color: var(--danger); }
.menu button .ic { width: 18px; height: 18px; color: var(--muted); }
.menu button.danger .ic { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 5px 4px; }
.menu .mh { font-size: 11px; letter-spacing: .08em; color: var(--muted); padding: 6px 10px 2px; font-weight: 600; }
.menu .kbd { margin-left: auto; font-family: var(--font-mono); font-size: 11px; color: var(--muted); }

.modal-layer {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px;
  background: rgba(10, 16, 22, .42);
}
.modal {
  width: min(560px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column;
  background: var(--surface); border-radius: 16px; box-shadow: var(--shadow-lg); overflow: hidden;
}
.modal.wide { width: min(820px, 100%); }
.modal-layer > .modal { grid-area: 1 / 1; }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 16px 18px 8px; }
.modal-head h2 { font-family: var(--font-display); font-size: 20px; margin: 0; flex: 1; text-wrap: balance; }
.modal-body { padding: 8px 18px 16px; overflow: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 16px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; letter-spacing: .02em; }
.field input[type="text"], .field input[type="number"], .field input[type="search"], .field textarea, .field select {
  height: 38px; padding: 0 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); width: 100%;
}
.field textarea { height: auto; padding: 8px 10px; }
.field input:focus, .field select:focus { border-color: var(--accent); outline: 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.opt-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.opt {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 8px 6px; border-radius: 10px;
  border: 1.5px solid var(--line); background: var(--surface); font-size: 12px; text-align: center;
}
.opt.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.opt canvas { width: 100%; max-width: 100%; height: auto; border-radius: 3px; box-shadow: 0 0 0 1px var(--line); background: #fff; }
.opt .dim { color: var(--muted); font-size: 11px; font-family: var(--font-mono); }
.color-row { display: flex; gap: 8px; flex-wrap: wrap; }
.tag-input { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 6px; border: 1px solid var(--line); border-radius: 9px; }
.tag-input input { flex: 1; min-width: 100px; border: 0; outline: 0; background: transparent; height: 28px; }
.tag-input .chip button { border: 0; background: none; padding: 0; color: inherit; line-height: 1; }
.note { font-size: 12.5px; color: var(--muted); line-height: 1.55; }
.note b { color: var(--ink); }
.progress { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }
.setting-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.setting-row:last-child { border-bottom: 0; }
.setting-row .lbl { flex: 1; }
.setting-row .lbl small { display: block; color: var(--muted); font-size: 12px; }
.switch { position: relative; width: 42px; height: 24px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch i { position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: background .15s; }
.switch i::after { content: ""; position: absolute; left: 3px; top: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(18px); }

.toast-stack { position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { pointer-events: auto; display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 12px; background: var(--ink); color: var(--surface); box-shadow: var(--shadow-lg); font-size: 13.5px; max-width: min(520px, calc(100vw - 32px)); animation: toastIn .2s ease; }
.toast button { border: 0; background: transparent; color: var(--mark); font-weight: 600; padding: 2px 4px; }
@keyframes toastIn { from { transform: translateY(8px); opacity: 0; } }

/* ---------------- Timer ---------------- */
.timer {
  position: fixed; right: 16px; top: calc(var(--top-h) + 64px + var(--safe-t)); z-index: 30; width: 244px;
  border-radius: 16px; background: var(--surface); box-shadow: var(--shadow-lg); border: 1px solid var(--line);
  padding: 10px 12px 12px; user-select: none; touch-action: none;
}
.timer.mini { width: auto; padding: 6px 8px 6px 12px; border-radius: 99px; }
.timer-head { display: flex; align-items: center; gap: 6px; cursor: grab; }
.timer-head strong { font-size: 13px; flex: 1; }
.timer-face { font-family: var(--font-mono); font-size: 44px; line-height: 1.1; text-align: center; letter-spacing: -.02em; margin: 6px 0 2px; font-variant-numeric: tabular-nums; }
.timer.mini .timer-face { font-size: 18px; margin: 0; }
.timer-ring { height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; margin: 4px 0 10px; }
.timer-ring i { display: block; height: 100%; background: var(--accent); width: 0; }
.timer.done .timer-face { color: var(--danger); animation: pulse .8s ease-in-out infinite; }
.timer-presets { display: flex; gap: 4px; flex-wrap: wrap; justify-content: center; margin-bottom: 8px; }
.timer-presets button { height: 28px; padding: 0 9px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface); font-size: 12px; font-family: var(--font-mono); }
.timer-presets button.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.timer-ctrl { display: flex; gap: 6px; justify-content: center; align-items: center; }
.timer-stats { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }
.timer-stats b { color: var(--ink); font-family: var(--font-mono); font-weight: 500; }

.drop-hint { position: fixed; inset: 12px; z-index: 70; border: 3px dashed var(--accent); border-radius: 20px; display: grid; place-items: center; background: color-mix(in srgb, var(--accent-soft) 70%, transparent); font-size: 20px; font-weight: 600; color: var(--accent); pointer-events: none; }

/* ---------------- Responsive ---------------- */
.only-narrow { display: none; }
@media (max-width: 1100px) {
  .ed-top { grid-template-columns: auto minmax(0, 1fr) auto; }
  .ed-title { max-width: 140px; }
  .tool { width: 36px; }
}
@media (max-width: 860px) {
  #library { grid-template-columns: 1fr; }
  .lib-side { position: fixed; left: 0; top: 0; bottom: 0; width: 272px; z-index: 20; transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow-lg); }
  .lib-side.open { transform: none; }
  .only-narrow { display: inline-grid; }
  .only-wide { display: none; }
  .lib-head { padding-inline: 16px; gap: 8px; flex-wrap: wrap; }
  .lib-bar, .lib-scroll { padding-inline: 16px; }
  .lib-actions .btn span { display: none; }
  .lib-actions .btn { padding: 0 10px; }
  .ed-top { grid-template-columns: 1fr auto; grid-template-areas: "l r" "t t"; row-gap: 4px; padding-inline: 6px; }
  .ed-left { grid-area: l; } .ed-right { grid-area: r; } .ed-tools { grid-area: t; justify-self: stretch; }
  .page-panel { position: absolute; left: 0; top: 0; bottom: 0; box-shadow: var(--shadow-lg); }
  .docsearch { top: calc(var(--top-h) + 100px + var(--safe-t)); }
  .nb-grid { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 20px 14px; }
}
@media (max-width: 480px) {
  .ed-right .icon-btn { width: 34px; }
  .crumbs h1 { font-size: 22px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- compact layout while typing on phones / small tablets ---------- */
#editor.typing .ed-top { display: none; }
#editor.typing .ed-sub { min-height: 40px; padding: 4px 8px; gap: 8px; }
#editor.typing .ed-sub .sub-label { display: none; }
#editor.typing .ed-sub select { height: 30px; font-size: 13px; padding: 0 22px 0 8px; }
#editor.typing .ed-sub .swatch { width: 22px; height: 22px; }
#editor.typing .ed-sub .btn.sm { height: 30px; padding: 0 9px; }
@media (max-width: 560px) {
  .ed-sub { gap: 8px; padding: 5px 8px; min-height: 42px; }
  .ed-sub select { height: 32px; font-size: 13px; }
  .ed-sub .swatch { width: 24px; height: 24px; }
  .ed-sub .sub-label { font-size: 11px; }
}
