:root {
  --bg: #121110;
  --panel: rgba(26, 24, 22, 0.88);
  --panel-solid: #1a1816;
  --line: #2e2a26;
  --text: #ddd3c1;
  --muted: #8b8478;
  --accent: #c9a46a;
  --danger: #c4593f;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --sidebar-w: 240px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}

kbd {
  font-family: var(--sans);
  font-size: 0.85em;
  padding: 0 0.35em;
  border: 1px solid var(--line);
  border-radius: 4px;
}

/* ---------- Layout ---------- */

body { display: flex; }

#sidebar {
  width: var(--sidebar-w);
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel-solid);
  border-right: 1px solid var(--line);
  transition: margin-left 0.2s ease;
  z-index: 2;
}

body.sidebar-hidden #sidebar { margin-left: calc(-1 * var(--sidebar-w)); }

#main {
  position: relative;
  flex: 1;
  min-width: 0;
}

/* ---------- Sidebar ---------- */

.sidebar-head, .sidebar-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px;
}

.sidebar-foot { border-top: 1px solid var(--line); margin-top: auto; }

.brand {
  font-family: var(--serif);
  font-size: 18px;
  font-style: italic;
  letter-spacing: 0.02em;
  color: var(--accent);
}

.text-btn {
  color: var(--muted);
  padding: 4px 6px;
  border-radius: 6px;
}
.text-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.05); }

#page-list {
  list-style: none;
  margin: 0;
  padding: 4px 8px;
  overflow-y: auto;
  flex: 1;
}

.sidebar-actions { display: flex; align-items: center; gap: 2px; }
#new-page { white-space: nowrap; }
.sidebar-actions .icon-btn { width: 30px; height: 30px; }
.sidebar-actions .icon-btn svg { width: 18px; height: 18px; }

/* Pages and folders, as one indented tree. --depth is set per row. */
.tree-row {
  --indent: calc(var(--depth, 0) * 14px);
  display: flex;
  align-items: center;
  border-radius: 6px;
  cursor: pointer;
}
.tree-row:hover, .tree-row.menu-open { background: rgba(255, 255, 255, 0.04); }
.tree-row.active { background: rgba(201, 164, 106, 0.12); }
.tree-row.dragging { opacity: 0.4; }
.tree-row.drop-target { background: rgba(201, 164, 106, 0.2); box-shadow: inset 0 0 0 1px var(--accent); }
#page-list.drop-root { box-shadow: inset 0 0 0 1px rgba(201, 164, 106, 0.45); border-radius: 8px; }

.page-open {
  flex: 1;
  min-width: 0;
  text-align: left;
  padding: 7px 8px 7px calc(8px + var(--indent));
}
.page-name {
  display: block;
  font-family: var(--serif);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.page-name.untitled { color: var(--muted); font-style: italic; }
.page-date { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }

.folder-toggle {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  text-align: left;
  padding: 7px 8px 7px calc(2px + var(--indent));
}
.folder-toggle .chev {
  width: 12px;
  height: 12px;
  flex: none;
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: transform 0.15s ease;
}
.folder-toggle[aria-expanded="true"] .chev { transform: rotate(90deg); }
.folder-icon {
  width: 17px;
  height: 17px;
  flex: none;
  fill: none;
  stroke: var(--accent);
  stroke-width: 1.3;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.folder-name {
  flex: 1;
  min-width: 0;
  font-family: var(--serif);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.folder-row.library .folder-name { font-style: italic; color: var(--accent); }
.folder-count { flex: none; font-size: 11px; color: var(--muted); }
.folder-rename {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-family: var(--serif);
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 1px 4px;
  outline: none;
}

.row-more {
  flex: none;
  width: 26px;
  height: 26px;
  margin-right: 4px;
  display: grid;
  place-items: center;
  border-radius: 4px;
  color: var(--muted);
  visibility: hidden;
}
.row-more svg { width: 16px; height: 16px; fill: currentColor; }
.tree-row:hover .row-more, .tree-row.menu-open .row-more, .row-more:focus-visible { visibility: visible; }
.row-more:hover { color: var(--text); background: rgba(255, 255, 255, 0.06); }
/* No hover on touch screens, so keep the menu button visible there. */
@media (hover: none) { .row-more { visibility: visible; } }

#row-menu {
  position: fixed;
  z-index: 20;
  min-width: 190px;
  max-width: 260px;
  max-height: min(420px, calc(100vh - 16px));
  overflow-y: auto;
  padding: 4px;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
#row-menu[hidden] { display: none; }
.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 6px 10px 6px calc(10px + var(--depth, 0) * 12px);
  border-radius: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.menu-item:hover, .menu-item:focus-visible { background: rgba(255, 255, 255, 0.06); outline: none; }
.menu-item.current { color: var(--muted); cursor: default; background: none; }
.menu-item.current::after { content: " (here)"; font-size: 12px; }
.menu-item.danger.armed { color: var(--danger); }
.menu-heading { padding: 6px 10px 2px; font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.menu-sep { height: 1px; margin: 4px 6px; background: var(--line); }

/* ---------- Canvas ---------- */

#stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  /* faint paper grain */
  background-color: var(--bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.035'/%3E%3C/svg%3E");
}

#stage.tool-pen { cursor: crosshair; }
#stage.tool-eraser { cursor: cell; }
#stage.tool-text { cursor: text; }
#stage.tool-hand, #stage.panning-ready { cursor: grab; }
#stage.panning { cursor: grabbing; }

#world {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  --zoom: 1;
}

#image-layer, #note-layer { position: absolute; left: 0; top: 0; }

#ink-layer {
  position: absolute;
  left: 0;
  top: 0;
  overflow: visible;
  pointer-events: none;
}

#ink-layer path.ink {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}
#ink-layer path.hit {
  fill: none;
  stroke: transparent;
  /* a comfortable click target on screen, whatever the zoom */
  stroke-width: calc(14px / var(--zoom));
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}
#stage.tool-select #ink-layer path.hit { pointer-events: stroke; cursor: move; }

.item { position: absolute; }

.item.image img {
  display: block;
  width: 100%;
  height: 100%;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

.item.note {
  font-family: var(--serif);
  line-height: 1.35;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  /* absolutely positioned at the world origin, so size to content explicitly */
  width: max-content;
  /* wrap width grows with the text size, so big titles don't break early */
  max-width: 32em;
  min-width: 1ch;
  padding: 2px 4px;
  outline: none;
  user-select: none;
}
.item.note[contenteditable]:not([contenteditable="false"]) {
  user-select: text;
  cursor: text;
  box-shadow: 0 0 0 calc(1px / var(--zoom)) rgba(201, 164, 106, 0.5);
  border-radius: 2px;
}

#stage.tool-select .item { cursor: move; }
#stage.tool-pen .item, #stage.tool-eraser .item { pointer-events: none; }

#selection {
  position: absolute;
  pointer-events: none;
  outline: calc(1px / var(--zoom)) dashed var(--accent);
  outline-offset: calc(3px / var(--zoom));
}
#selection .handle {
  position: absolute;
  right: calc(-8px / var(--zoom));
  bottom: calc(-8px / var(--zoom));
  width: calc(12px / var(--zoom));
  height: calc(12px / var(--zoom));
  background: var(--accent);
  border-radius: 50%;
  pointer-events: auto;
  cursor: nwse-resize;
}

#empty-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  text-align: center;
  line-height: 2;
  color: var(--muted);
  opacity: 0.6;
  font-family: var(--serif);
  font-style: italic;
  font-size: 15px;
  pointer-events: none;
  user-select: none;
}

/* ---------- Floating chrome ---------- */

#topbar {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }

#page-title {
  background: transparent;
  border: 0;
  outline: none;
  color: var(--text);
  font-family: var(--serif);
  font-size: 22px;
  width: min(60vw, 520px);
  padding: 4px 6px;
  border-radius: 6px;
}
#page-title:focus { background: var(--panel); }
#page-title::placeholder { color: var(--muted); font-style: italic; }

/* Tabs for pages that have them (for example one tab per patch). */
#page-tabs {
  /* Its own row under the title, so it never runs into the toast or the save status. */
  position: absolute;
  top: 46px;
  left: 42px;
  max-width: calc(100% - 42px);
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 3px;
  background: var(--panel);
  border-radius: 999px;
}
#page-tabs[hidden] { display: none; }
.page-tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-family: var(--serif);
  font-size: 15px;
  padding: 4px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.page-tab:hover { color: var(--text); }
.page-tab[aria-selected="true"] { background: var(--text); color: var(--bg); }
.page-tab:focus-visible { outline: 2px solid var(--text); outline-offset: 1px; }

#toolbar {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  backdrop-filter: blur(8px);
  max-width: calc(100% - 32px);
  overflow-x: auto;
}

.group { display: flex; align-items: center; gap: 2px; }
.sep { width: 1px; align-self: stretch; background: var(--line); margin: 4px 2px; }

.icon-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--muted);
  flex: none;
}
.icon-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.05); }
.icon-btn.active { color: var(--accent); background: rgba(201, 164, 106, 0.14); }
.icon-btn:disabled { opacity: 0.35; cursor: default; background: none; }
.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#topbar .icon-btn { background: var(--panel); }

.swatch {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  flex: none;
}
.swatch span {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--c);
}
.swatch.active { box-shadow: inset 0 0 0 1.5px var(--text); }

.size-btn {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  flex: none;
}
.size-btn span { border-radius: 50%; background: var(--muted); }
.size-btn.active span { background: var(--text); }
.size-btn:hover { background: rgba(255, 255, 255, 0.05); }

#corner {
  position: absolute;
  /* top right, so it never collides with the toolbar on narrow windows */
  right: 16px;
  top: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}
#zoom-label { font-size: 12px; font-variant-numeric: tabular-nums; }

/* ---------- Storage ring ---------- */

#storage-ring { width: 26px; height: 26px; border-radius: 50%; }
#storage-ring svg { width: 16px; height: 16px; transform: rotate(-90deg); stroke-width: 2; }
#storage-ring .ring-track { stroke: var(--line); }
#storage-ring .ring-fill {
  stroke: var(--accent);
  stroke-linecap: round;
  stroke-dasharray: 0 100;
  transition: stroke-dasharray 0.4s ease;
}
#storage-ring.warn .ring-fill { stroke: var(--danger); }
#storage-ring { position: relative; }
#storage-ring.remind::after {
  content: '';
  position: absolute;
  top: 3px;
  right: 3px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 2px var(--bg);
}

#storage-pop {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 280px;
  padding: 12px 14px;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  z-index: 5;
}
#storage-pop p { margin: 6px 0 0; }
.storage-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.storage-label { color: var(--text); font-family: var(--serif); font-size: 15px; }
#storage-used { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
#storage-bar { height: 4px; margin-top: 8px; border-radius: 2px; background: var(--line); overflow: hidden; }
#storage-bar span { display: block; height: 100%; background: var(--accent); border-radius: 2px; }
#storage-ring.warn ~ #storage-pop #storage-bar span { background: var(--danger); }
#storage-safe { color: var(--danger); }
#storage-safe.ok { color: var(--muted); }
#storage-over, #storage-backup.due { color: var(--danger); }
#storage-quota { opacity: 0.75; }
.storage-actions { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px -6px -4px; }
.storage-actions .text-btn { color: var(--accent); }

#toast {
  position: absolute;
  left: 50%;
  top: 16px;
  transform: translateX(-50%);
  padding: 8px 14px;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 13px;
}

/* ---------- Small screens ---------- */

@media (max-width: 720px) {
  #sidebar { position: absolute; top: 0; bottom: 0; left: 0; box-shadow: 0 0 24px rgba(0, 0, 0, 0.5); }
  body.sidebar-hidden #sidebar { box-shadow: none; }
  /* keep just the storage ring on small screens */
  #save-status, #zoom-label { display: none; }
  #corner { top: 16px; right: 12px; }
  #toolbar { bottom: 10px; gap: 2px; padding: 4px; }
  .sep { margin: 4px 0; }
}

/* ---------- Library ---------- */

#library {
  width: min(440px, calc(100vw - 32px));
  max-height: min(560px, calc(100vh - 64px));
  padding: 16px 18px 18px;
  background: var(--panel-solid);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
}
#library::backdrop { background: rgba(0, 0, 0, 0.5); }

.library-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.library-head h2 {
  margin: 0;
  font-family: var(--serif);
  font-weight: normal;
  font-size: 20px;
}
.library-note { color: var(--muted); margin: 6px 0 12px; }

#library-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}
.library-entry {
  width: 100%;
  text-align: left;
  display: grid;
  gap: 2px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.library-entry:hover, .library-entry:focus-visible { border-color: var(--accent); background: rgba(255, 255, 255, 0.03); }
.library-title { font-family: var(--serif); font-size: 16px; }
.library-desc, .library-msg { color: var(--muted); font-size: 13px; }
.library-have { color: var(--accent); font-size: 12px; }
.library-fresh { margin: 2px 0 0 6px; font-size: 12px; }
