:root {
  --paper: #fdfcf8;
  --bg: #f1eee6;
  --ink: #1c1c1e;
  --muted: #7a766c;
  --line: #dfdbd0;
  --accent: #2f5fd0;
  --accent-soft: #e4ebfb;
  --card: #fff;
  --hover: rgba(0,0,0,.06);
  --rule: #e3e0d4;
  color-scheme: light;
  --bar-h: 54px;
  --side-w: 280px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  color: var(--ink);
}
html[data-theme="dark"] {
  --paper: #1b1b1e; --bg: #101012; --ink: #f2f2f4; --muted: #9a9aa3; --line: #2c2c31;
  --accent: #5b8cf0; --accent-soft: #223153; --card: #2a2a2f; --hover: rgba(255,255,255,.1); --rule: #2d2d33;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); overscroll-behavior: none; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }

#app { position: fixed; inset: 0; display: flex; padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }

/* sidebar */
#side { width: var(--side-w); flex: none; display: flex; flex-direction: column; background: var(--bg); border-right: 1px solid var(--line); padding-top: env(safe-area-inset-top); transition: transform .22s ease, margin-left .22s ease; z-index: 30; }
#side.closed { margin-left: calc(-1 * var(--side-w)); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 14px 10px 18px; }
.logo { font-size: 22px; font-weight: 700; letter-spacing: -.02em; }
#tree { flex: 1; overflow-y: auto; padding: 4px 8px 16px; -webkit-overflow-scrolling: touch; }
.side-foot { display: flex; align-items: center; gap: 8px; padding: 10px 18px calc(10px + env(safe-area-inset-bottom)); color: var(--muted); font-size: 12px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #3aa76d; flex: none; }
.dot.warn { background: #d9a31b; }
.dot.off { background: #b8b3a6; }

.folder { margin-top: 6px; }
.frow, .nrow { display: flex; align-items: center; min-height: 44px; border-radius: 10px; }
.frow { font-weight: 650; padding-left: 6px; }
.frow .name, .nrow .name { flex: 1; min-width: 0; text-align: left; padding: 10px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.frow .caret { width: 20px; text-align: center; color: var(--muted); transition: transform .15s; }
.folder.open > .frow .caret { transform: rotate(90deg); }
.count { color: var(--muted); font-weight: 500; font-size: 13px; margin-right: 2px; }
.more { width: 36px; height: 36px; border-radius: 8px; color: var(--muted); font-size: 18px; line-height: 1; }
.more:active { background: var(--hover); }
.notes { display: none; padding-left: 14px; }
.folder.open > .notes { display: block; }
.nrow { padding-left: 10px; }
.nrow .name { font-weight: 500; }
.nrow .date { color: var(--muted); font-size: 12px; margin-right: 4px; }
.nrow.cur { background: var(--accent-soft); }
.nrow.cur .name { color: var(--accent); font-weight: 650; }
.addnote { color: var(--accent); font-weight: 600; font-size: 14px; padding: 10px 10px; min-height: 40px; display: block; text-align: left; width: 100%; }
.empty { color: var(--muted); padding: 22px 18px; font-size: 14px; line-height: 1.4; }

/* top bar */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
#bar { height: calc(var(--bar-h) + env(safe-area-inset-top)); padding-top: env(safe-area-inset-top); flex: none; display: flex; align-items: center; gap: 8px; padding-left: 8px; padding-right: 8px; background: var(--bg); border-bottom: 1px solid var(--line); z-index: 20; }
#title { flex: 1 1 120px; min-width: 70px; height: 38px; border: 0; background: transparent; font-size: 17px; font-weight: 650; padding: 0 8px; border-radius: 8px; outline: none; text-overflow: ellipsis; }
#title:focus { background: var(--hover); }
.group { display: flex; align-items: center; gap: 2px; flex: none; }
.icon { width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; color: var(--ink); }
.icon svg, .pill svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon:active { background: var(--hover); }
.icon.on { background: var(--accent-soft); color: var(--accent); }
.icon[disabled] { opacity: .3; }
.pill { height: 34px; padding: 0 14px; border-radius: 17px; background: var(--hover); font-weight: 600; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }
.pill svg { width: 16px; height: 16px; }
.pill.primary { background: var(--accent); color: #fff; }
.tidy { gap: 8px; }
#tidyBtn[aria-pressed="true"] { background: var(--accent); color: #fff; }
#strength { width: 78px; accent-color: var(--accent); }
body.tidy-off #strength { opacity: .3; pointer-events: none; }
.sw { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; position: relative; margin: 0 2px; }
.sw::after { content: ""; position: absolute; inset: 3px; border-radius: 50%; background: var(--c); border: 1px solid rgba(0,0,0,.18); }
.sw.on { border-color: var(--accent); }
body.erasing #swatches { opacity: .25; pointer-events: none; }

#thickDot { display: block; border-radius: 50%; background: var(--ink); width: 8px; height: 8px; }
body.erasing #thick { opacity: .25; pointer-events: none; }
.thickbox { padding: 10px 12px 12px; }
.thickbox input { width: 100%; accent-color: var(--accent); }
.thickbox .prev { height: 44px; display: grid; place-items: center; }
.thickbox .prev span { display: block; width: 80%; border-radius: 99px; }

/* page */
#page { position: relative; flex: 1; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; background: var(--paper); touch-action: none; overscroll-behavior: none; }
#stage { position: sticky; top: 0; left: 0; width: 100%; overflow: hidden; touch-action: none; }
#stage canvas { position: absolute; left: 0; top: 0; width: 100%; height: 100%; display: block; touch-action: none; -webkit-touch-callout: none; user-select: none; -webkit-user-select: none; }
#sizer { width: 1px; }
body.erasing #live { cursor: cell; }

#swap { position: fixed; right: calc(14px + env(safe-area-inset-right)); bottom: calc(18px + env(safe-area-inset-bottom)); width: 54px; height: 54px; border-radius: 27px; background: var(--card); color: var(--ink); box-shadow: 0 4px 16px rgba(0,0,0,.2), 0 0 0 1px var(--hover); z-index: 15; }
#swap svg { width: 24px; height: 24px; }
body.erasing #swap { background: var(--accent); color: #fff; }

/* popover + dialog */
#popover { position: fixed; z-index: 60; min-width: 220px; max-width: 320px; background: var(--card); color: var(--ink); border-radius: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.2), 0 0 0 1px var(--hover); padding: 6px; }
#popover button.item { display: flex; width: 100%; align-items: center; justify-content: space-between; min-height: 44px; padding: 0 12px; border-radius: 9px; text-align: left; font-size: 16px; }
#popover button.item:active { background: var(--hover); }
#popover .danger { color: #c4302b; }
#popover .head { padding: 8px 12px 4px; color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.toggle { width: 46px; height: 28px; border-radius: 14px; background: #cfcbc0; position: relative; flex: none; transition: background .15s; }
.toggle::after { content: ""; position: absolute; left: 2px; top: 2px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.toggle.on { background: var(--accent); }
.toggle.on::after { transform: translateX(18px); }
.seg { display: flex; gap: 6px; padding: 4px 6px 8px; }
.seg button { flex: 1; height: 38px; border-radius: 9px; background: var(--hover); font-weight: 600; font-size: 14px; }
.seg button.on { background: var(--accent); color: #fff; }
#dialog { position: fixed; inset: 0; z-index: 70; display: grid; place-items: center; background: rgba(0,0,0,.28); }
#dialog[hidden], #popover[hidden] { display: none; }
#dialogForm { width: min(420px, 90vw); background: var(--card); color: var(--ink); border-radius: 18px; padding: 22px; box-shadow: 0 14px 50px rgba(0,0,0,.3); }
#dialogForm h2 { margin: 0 0 12px; font-size: 19px; }
#dialogForm p { margin: 0 0 12px; color: var(--muted); line-height: 1.4; }
#dialogInput { width: 100%; background: transparent; color: var(--ink); height: 46px; border-radius: 10px; border: 1px solid var(--line); padding: 0 12px; font-size: 17px; outline: none; }
#dialogInput:focus { border-color: var(--accent); }
#dialogForm .row { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }

#scrim { display: none; }

/* narrow screens: the sidebar slides over the page */
@media (max-width: 900px) {
  #side { position: fixed; left: 0; top: 0; bottom: 0; margin-left: 0; box-shadow: 4px 0 30px rgba(0,0,0,.18); }
  #side.closed { margin-left: 0; transform: translateX(-105%); box-shadow: none; }
  #scrim { position: fixed; inset: 0; z-index: 25; background: rgba(0,0,0,.22); }
  body.side-open #scrim { display: block; }
  #strength { width: 56px; }
}
@media (max-width: 760px) {
  #swatches .sw { margin: 0; }
  .tidy span { display: none; }
  #title { flex-basis: 70px; }
}

/* sign-in screen (only shown when the server has accounts) */
#auth { position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; background: var(--bg); }
#auth[hidden] { display: none; }
#authForm { width: min(380px, 90vw); display: flex; flex-direction: column; gap: 12px; }
#authForm h1 { margin: 0; font-size: 34px; letter-spacing: -.02em; }
#authForm p { margin: 0; color: var(--muted); line-height: 1.4; }
#authForm input { width: 100%; background: var(--card); color: var(--ink); height: 50px; border-radius: 12px; border: 1px solid var(--line); padding: 0 14px; font-size: 17px; outline: none; }
#authForm input:focus { border-color: var(--accent); }
#authErr { color: #c4302b !important; min-height: 1.4em; }
#authGo { height: 50px; font-size: 17px; }
#authForm .link { color: var(--accent); font-size: 15px; height: 40px; }
