/* ---------- tokens ---------- */
:root {
  --bg: #f5f5f6;
  --dot: #d2d2d6;
  --fg: #1e1e22;
  --fg-muted: #6b6b73;
  --panel: #ffffff;
  --panel-border: rgba(0, 0, 0, 0.08);
  --panel-shadow: 0 8px 28px rgba(20, 20, 30, 0.14), 0 1px 3px rgba(20, 20, 30, 0.08);
  --hover: rgba(0, 0, 0, 0.06);
  --accent: #2f6bff;
  --accent-soft: rgba(47, 107, 255, 0.16);
  --danger: #d6453d;
  --thread: rgba(30, 30, 34, 0.45);
  --title-fg: #1e1e22;
  --text-fg: #1e1e22;
  --note-fg: #2a2a2e;
  --link-bg: #ffffff;
  --radius: 10px;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-sans: var(--font-ui);
  --font-serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --font-hand: "Segoe Print", "Bradley Hand", "Chalkboard SE", "Comic Sans MS", cursive;
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #1f1f22;
  --dot: #36363b;
  --fg: #ececee;
  --fg-muted: #9a9aa3;
  --panel: #2a2a2f;
  --panel-border: rgba(255, 255, 255, 0.09);
  --panel-shadow: 0 10px 30px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.4);
  --hover: rgba(255, 255, 255, 0.08);
  --accent: #5b8cff;
  --accent-soft: rgba(91, 140, 255, 0.22);
  --thread: rgba(236, 236, 238, 0.45);
  --title-fg: #ececee;
  --text-fg: #ececee;
  --link-bg: #2a2a2f;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.45 var(--font-ui);
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }

/* ---------- stage ---------- */
#stage {
  position: fixed; inset: 0;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  -webkit-touch-callout: none;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1.2px, transparent 1.3px);
  background-size: 28px 28px;
  background-position: var(--grid-x, 0px) var(--grid-y, 0px);
}
#stage.panning { cursor: grabbing; }
#stage.mode-draw { cursor: crosshair; }
#stage.mode-erase { cursor: cell; }
#stage.mode-attach { cursor: alias; }
#stage.drop-target::after {
  content: "Drop images to add them";
  position: absolute; inset: 14px;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  display: grid; place-items: center;
  font-size: 18px; color: var(--accent);
  background: var(--accent-soft);
  pointer-events: none;
}
#world {
  position: absolute; left: 0; top: 0;
  transform-origin: 0 0;
  will-change: transform;
}
#world > svg {
  position: absolute; left: 0; top: 0; width: 1px; height: 1px;
  overflow: visible; pointer-events: none;
}
#threads { z-index: 0; }
#ink { z-index: 999999; }
#threads line { stroke: var(--thread); stroke-width: 1.5; stroke-dasharray: 5 5; vector-effect: non-scaling-stroke; }
#threads circle { fill: var(--thread); }
#ink path { fill: none; stroke-linecap: round; stroke-linejoin: round; }
#ink path.highlighter { mix-blend-mode: multiply; }
[data-theme="dark"] #ink path.highlighter { mix-blend-mode: normal; opacity: 0.55; }
#ink path.hit { stroke: var(--accent); opacity: 0.8; }
#elements { position: absolute; left: 0; top: 0; }

/* ---------- elements ---------- */
.el {
  position: absolute;
  left: 0; top: 0;
  touch-action: none;
}
.el.selected::after {
  content: ""; position: absolute; inset: -3px;
  border: 1.5px solid var(--accent);
  border-radius: inherit;
  pointer-events: none;
}
.el.attach-candidate { outline: 3px solid var(--accent); outline-offset: 3px; }
.el .handle {
  position: absolute; right: -7px; bottom: -7px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--panel); border: 2px solid var(--accent);
  cursor: nwse-resize; display: none;
  touch-action: none;
}
.el.selected .handle { display: block; }
.el[data-font="sans"] { font-family: var(--font-sans); }
.el[data-font="serif"] { font-family: var(--font-serif); }
.el[data-font="mono"] { font-family: var(--font-mono); }
.el[data-font="hand"] { font-family: var(--font-hand); }

.el-image {
  border-radius: 4px;
  overflow: hidden;
  background: var(--hover);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18), 0 6px 18px rgba(0, 0, 0, 0.12);
}
.el-image img { display: block; width: 100%; height: 100%; object-fit: cover; pointer-events: none; -webkit-user-drag: none; }

.el-note {
  min-height: 120px;
  padding: 14px 16px;
  border-radius: 3px;
  color: var(--note-fg);
  background: var(--el-color, #ffe38f);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.14), 0 8px 16px rgba(0, 0, 0, 0.1);
  font-size: var(--el-size, 15px);
  line-height: 1.4;
}
.el-text {
  min-height: 1.4em;
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--el-color, var(--text-fg));
  background: var(--el-bg, transparent);
  font-size: var(--el-size, 16px);
  line-height: 1.5;
}
.el-text:not(.editing):empty::before { content: "Text"; color: var(--fg-muted); }
.el-title {
  padding: 4px 6px;
  font-weight: 700;
  font-size: var(--el-size, 30px);
  line-height: 1.15;
  letter-spacing: -0.01em;
  color: var(--el-color, var(--title-fg));
  white-space: nowrap;
}
.el-link {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  border-radius: 999px;
  background: var(--el-color, var(--link-bg));
  color: #1e1e22;
  font-size: var(--el-size, 15px);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.14), 0 6px 14px rgba(0, 0, 0, 0.1);
  white-space: nowrap;
}
.el-link svg { width: 16px; height: 16px; flex: none; opacity: 0.7; }
.el-link .label { overflow: hidden; text-overflow: ellipsis; }
.el-link a { color: inherit; text-decoration: none; }
.el-link a:hover { text-decoration: underline; }

.el .content { white-space: pre-wrap; word-break: break-word; }
.el .content p { margin: 0; }
.el .content .bullet { position: relative; padding-left: 1.1em; }
.el .content .bullet::before { content: "•"; position: absolute; left: 0.2em; }
.el .content a { color: inherit; text-decoration: underline; }
.el textarea.editor {
  display: block; width: 100%; min-height: 1.4em;
  margin: 0; padding: 0; border: 0; outline: none; resize: none;
  background: transparent; color: inherit;
  font: inherit; line-height: inherit; letter-spacing: inherit;
  white-space: pre-wrap; word-break: break-word;
  overflow: hidden;
}
.el.bold { font-weight: 700; }
.el[data-align="center"] { text-align: center; }
.el[data-align="right"] { text-align: right; }
.el.dragging { opacity: 0.92; }
#stage.can-edit .el { cursor: move; }
#stage.can-edit .el a { cursor: pointer; }
#stage.can-edit .el.editing { cursor: text; }
.el textarea.editor { user-select: text; -webkit-user-select: text; touch-action: auto; cursor: text; }
.el.locked { cursor: default; }

/* ---------- chrome ---------- */
.chrome {
  position: fixed; z-index: 20;
  display: flex; align-items: center; gap: 4px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  box-shadow: var(--panel-shadow);
}
#topright { top: max(12px, env(safe-area-inset-top)); right: max(12px, env(safe-area-inset-right)); }
#bottomright { bottom: max(12px, env(safe-area-inset-bottom)); right: max(12px, env(safe-area-inset-right)); font-size: 12px; color: var(--fg-muted); padding: 4px 6px; }
#bottomright .ib { width: 32px; height: 32px; }
.ib {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: transparent; color: var(--fg);
}
.ib:hover { background: var(--hover); }
.ib svg { width: 20px; height: 20px; }
.ib.me { padding: 0 12px 0 6px; width: auto; border-radius: 999px; gap: 6px; display: inline-flex; max-width: 180px; }
.ib.me .nm { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#status { width: 8px; height: 8px; border-radius: 50%; margin: 0 8px 0 6px; background: #41b979; transition: background .3s; }
#status.saving { background: #e9b949; }
#status.offline { background: var(--danger); }
#boardname { font-size: 13px; color: var(--fg-muted); padding: 0 8px 0 10px; max-width: 32vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* draw bar (visible only while drawing) */
#drawbar {
  bottom: max(16px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  padding: 6px 8px; gap: 6px;
}
#drawbar .sep { width: 1px; height: 22px; background: var(--panel-border); margin: 0 2px; }
#drawbar .tool { border-radius: 999px; width: auto; padding: 0 12px; gap: 6px; font-size: 13px; display: inline-flex; }
#drawbar .tool.active { background: var(--accent-soft); color: var(--accent); }
#drawbar input[type="color"] { width: 30px; height: 30px; border: 0; padding: 0; background: none; border-radius: 50%; overflow: hidden; cursor: pointer; }
#drawbar input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
#drawbar input[type="color"]::-webkit-color-swatch { border: 2px solid var(--panel-border); border-radius: 50%; }
#drawbar input[type="range"] { width: 90px; accent-color: var(--accent); }
#drawbar .hint { font-size: 12px; color: var(--fg-muted); padding: 0 6px; }

/* context menu */
.menu {
  position: fixed; z-index: 40;
  min-width: 200px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  box-shadow: var(--panel-shadow);
  animation: pop .12s ease-out;
}
@keyframes pop { from { opacity: 0; transform: scale(.96); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .menu { animation: none; } }
.menu button {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 9px 10px;
  border: 0; border-radius: 8px;
  background: transparent; text-align: left;
  font-size: 14px;
}
.menu button:hover, .menu button:focus-visible { background: var(--hover); outline: none; }
.menu button.danger { color: var(--danger); }
.menu button svg { width: 18px; height: 18px; opacity: .8; flex: none; }
.menu button .kbd { margin-left: auto; font-size: 11px; color: var(--fg-muted); }
.menu hr { border: 0; border-top: 1px solid var(--panel-border); margin: 6px 4px; }
.menu .muted { padding: 6px 10px; font-size: 12px; color: var(--fg-muted); }

/* element toolbar (floats above selection) */
#eltb {
  position: fixed; z-index: 30;
  display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
  max-width: calc(100vw - 16px);
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  box-shadow: var(--panel-shadow);
}
#eltb .ib { width: 34px; height: 34px; border-radius: 8px; }
#eltb .ib svg { width: 18px; height: 18px; }
#eltb .sep { width: 1px; height: 22px; background: var(--panel-border); margin: 0 3px; }
#eltb select { border: 0; background: transparent; padding: 6px 4px; border-radius: 8px; font-size: 13px; max-width: 120px; }
#eltb select:hover { background: var(--hover); }
#eltb .by { font-size: 11px; color: var(--fg-muted); padding: 0 8px; white-space: nowrap; }
#eltb .swatches { display: flex; gap: 4px; padding: 0 4px; }
.swatch {
  width: 20px; height: 20px; border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, .15); padding: 0; cursor: pointer;
  background: var(--sw);
}
.swatch.on { outline: 2px solid var(--accent); outline-offset: 1px; }
.swatch.custom { background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); position: relative; overflow: hidden; }
.swatch.custom input { position: absolute; inset: -10px; opacity: 0; cursor: pointer; }
#eltb input.urlbox { border: 1px solid var(--panel-border); border-radius: 8px; padding: 6px 8px; font-size: 13px; width: 200px; background: transparent; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20, 20, 28, 0.45);
  display: grid; place-items: center;
  padding: 16px;
}
.dialog {
  width: min(400px, 100%);
  padding: 22px 22px 18px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  box-shadow: var(--panel-shadow);
}
.dialog h2 { margin: 0 0 6px; font-size: 18px; }
.dialog p { margin: 0 0 14px; color: var(--fg-muted); font-size: 14px; }
.dialog label { display: block; font-size: 13px; color: var(--fg-muted); margin: 10px 0 4px; }
.dialog input[type="text"], .dialog input[type="password"], .dialog input[type="url"], .dialog input[type="file"] {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--panel-border); border-radius: 10px;
  background: var(--bg); color: var(--fg);
}
.dialog .row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.dialog .err { color: var(--danger); font-size: 13px; min-height: 1.2em; margin-top: 8px; }
.btn {
  padding: 9px 16px;
  border: 1px solid var(--panel-border); border-radius: 10px;
  background: var(--panel); color: var(--fg);
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.link { border: 0; background: none; color: var(--fg-muted); }

#lightbox { background: rgba(10, 10, 14, 0.92); cursor: zoom-out; }
#lightbox img { max-width: 100vw; max-height: 100vh; object-fit: contain; }

#toasts { position: fixed; left: 50%; top: 16px; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast {
  padding: 10px 16px;
  background: var(--fg); color: var(--bg);
  border-radius: 999px; font-size: 14px;
  box-shadow: var(--panel-shadow);
}
.toast.err { background: var(--danger); color: #fff; }

#empty {
  position: fixed; inset: 0; display: grid; place-items: center;
  pointer-events: none; text-align: center; color: var(--fg-muted);
}
#empty p { max-width: 320px; margin: 0; }

/* ---------- admin / landing ---------- */
.page { max-width: 760px; margin: 0 auto; padding: 48px 20px 80px; }
.page header { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin-bottom: 28px; }
.page h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; }
.page h1 small { font-weight: 400; color: var(--fg-muted); font-size: 14px; margin-left: 8px; }
.boards { display: grid; gap: 10px; }
.board-card {
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 12px;
  padding: 14px 16px;
  background: var(--panel); border: 1px solid var(--panel-border); border-radius: 14px;
}
.board-card .name { font-weight: 600; font-size: 16px; }
.board-card .meta { color: var(--fg-muted); font-size: 13px; margin-top: 2px; word-break: break-all; }
.board-card .meta a { color: var(--accent); text-decoration: none; }
.board-card .actions { display: flex; gap: 4px; }
.landing { min-height: 100svh; display: grid; place-items: center; padding: 20px; }
.landing .dialog { width: min(360px, 100%); }
.landing h1 { margin: 0 0 4px; font-size: 22px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin: 0 0 20px; }
.empty-admin { color: var(--fg-muted); text-align: center; padding: 40px 0; }

@media (max-width: 600px) {
  #boardname { display: none; }
  .menu button { padding: 12px 10px; font-size: 15px; }
  #eltb .by { display: none; }
  #eltb select { max-width: 84px; }
}
