:root {
  --paper: #fdfdfb;
  --bar: rgba(255, 255, 255, 0.96);
  --panel: #ffffff;
  --fg: #1b1b1f;
  --muted: #6b6f7a;
  --line: rgba(20, 25, 40, 0.12);
  --hover: rgba(20, 25, 40, 0.07);
  --active: rgba(31, 95, 209, 0.14);
  --accent: #1f5fd1;
  --danger: #c62828;
  --shadow: 0 6px 24px rgba(20, 25, 40, 0.14), 0 1px 3px rgba(20, 25, 40, 0.1);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bar: rgba(40, 42, 48, 0.97);
    --panel: #25272c;
    --fg: #f1f2f4;
    --muted: #a3a7b3;
    --line: rgba(255, 255, 255, 0.14);
    --hover: rgba(255, 255, 255, 0.1);
    --active: rgba(122, 167, 255, 0.22);
    --accent: #7aa7ff;
    --danger: #ff7b72;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  position: fixed;
  inset: 0;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--paper);
  color: var(--fg);
  font: 15px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#base, #live, #tip {
  position: fixed;
  left: 0;
  top: 0;
  display: block;
}
#ink { position: fixed; inset: 0; touch-action: none; cursor: crosshair; }
#ink.select { cursor: default; }
#ink.pan { cursor: grab; }

/* Frame around the selected photo. The frame itself lets touches through; only the corner handles catch them. */
#sel { position: fixed; left: 0; top: 0; z-index: 1; border: 1.5px solid #1f5fd1; pointer-events: none; }
#sel .h { position: absolute; width: 40px; height: 40px; margin: -20px; display: grid; place-items: center; pointer-events: auto; touch-action: none; }
#sel .h::after { content: ""; width: 12px; height: 12px; border-radius: 50%; background: #fff; border: 2px solid #1f5fd1; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); }
#sel .h[data-h="nw"] { left: 0; top: 0; cursor: nwse-resize; }
#sel .h[data-h="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
#sel .h[data-h="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
#sel .h[data-h="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
#sel-del { left: 0; top: 0; color: var(--danger); }

/* The box you type in. Sits exactly where the text will be drawn on the page. */
#editor, #editor:focus-visible {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  outline: 1.5px dashed #1f5fd1;
  outline-offset: 4px;
  background: transparent;
  resize: none;
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  caret-color: #1f5fd1;
  -webkit-appearance: none;
  appearance: none;
  -webkit-user-select: text;
  user-select: text;
}
@media (max-width: 430px) {
  #tools .icon { width: 34px; }
  #tools .sep { margin: 0 2px; }
}
@media (max-width: 392px) {
  #tools .icon { width: 31px; }
}

/* Floating bars */
.bar {
  position: fixed;
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: var(--bar);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
#top-left { top: calc(8px + var(--safe-t)); left: calc(8px + var(--safe-l)); max-width: calc(100vw - 170px - var(--safe-l) - var(--safe-r)); }
#top-right { top: calc(8px + var(--safe-t)); right: calc(8px + var(--safe-r)); }
#tools { bottom: calc(12px + var(--safe-b)); left: 50%; transform: translateX(-50%); }

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:disabled { opacity: 0.35; cursor: default; }

.icon {
  width: 40px;
  height: 40px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 10px;
}
.icon svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon svg circle { fill: currentColor; stroke: none; }
@media (hover: hover) { .icon:not(:disabled):hover, .item:hover, .note-item:hover { background: var(--hover); } }
.icon[aria-pressed="true"] { background: var(--active); color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.sep { width: 1px; height: 24px; margin: 0 4px; background: var(--line); }

#title {
  min-width: 0;
  width: 200px;
  height: 40px;
  padding: 0 10px 0 4px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--fg);
  font: inherit;
  font-weight: 600;
  text-overflow: ellipsis;
  -webkit-user-select: text;
  user-select: text;
}
#title::placeholder { color: var(--muted); font-weight: 500; }
#title:focus { outline: none; background: var(--hover); }

.swatch, .dot { display: block; border-radius: 50%; }
.swatch { width: 22px; height: 22px; box-shadow: inset 0 0 0 1px var(--line); }
.dot { background: currentColor; }

/* Popovers */
.pop {
  position: fixed;
  z-index: 5;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.pop.row { display: flex; gap: 2px; bottom: calc(70px + var(--safe-b)); left: 50%; transform: translateX(-50%); }
#pop-menu { top: calc(62px + var(--safe-t)); right: calc(8px + var(--safe-r)); width: 240px; }

.label { padding: 6px 8px 4px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; padding: 2px; margin: 0 2px 6px; background: var(--hover); border-radius: 10px; }
.seg button { height: 32px; border-radius: 8px; font-size: 13px; }
.seg button[aria-pressed="true"] { background: var(--panel); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18); font-weight: 600; }
.item { display: flex; align-items: center; justify-content: space-between; width: 100%; min-height: 40px; padding: 0 10px; border-radius: 10px; text-align: left; }
.item.danger { color: var(--danger); }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.slider { flex-direction: column; align-items: stretch; gap: 2px; padding-top: 8px; padding-bottom: 6px; }
.slider input { width: 100%; margin: 0; accent-color: var(--accent); }
.diag { padding: 2px 10px 8px; font-size: 11.5px; line-height: 1.45; color: var(--muted); }

/* Notes drawer */
#scrim { position: fixed; inset: 0; z-index: 8; background: rgba(0, 0, 0, 0.28); }
#drawer {
  position: fixed;
  z-index: 9;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(430px, 100vw);
  padding: calc(8px + var(--safe-t)) 8px calc(8px + var(--safe-b)) calc(8px + var(--safe-l));
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.drawer-head { display: flex; align-items: center; gap: 2px; padding: 0 0 6px; }
.drawer-head h2 { margin: 0 0 0 8px; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.grow { flex: 1; min-width: 0; }
#lib-actions { display: flex; flex: none; }
#lib-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 2px 2px 12px; }
.icon.sm { width: 36px; height: 36px; }
.icon.sm svg { width: 18px; height: 18px; }
.icon.danger { color: var(--danger); }
.d { font-size: 12px; color: var(--muted); }
.empty { margin: 6px 10px 10px; font-size: 14px; color: var(--muted); }
.lib-row { display: flex; align-items: center; gap: 2px; }
.lib-row .note-item { flex: 1; min-width: 0; }
div.note-item { cursor: default; }
.lib-link { display: flex; align-items: center; gap: 10px; width: 100%; margin-bottom: 6px; padding: 12px 10px; border-radius: 12px; background: var(--hover); font-weight: 600; text-align: left; }
.lib-ico { display: grid; }
.lib-ico svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.lib-ico.dim { color: var(--muted); }
.badge:not(:empty) { min-width: 22px; padding: 2px 7px; border-radius: 11px; background: #1f5fd1; color: #fff; font-size: 12px; text-align: center; }
.label-row { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; padding-right: 6px; }
.small-btn { height: 32px; padding: 0 12px; border-radius: 9px; background: var(--hover); font-size: 13px; font-weight: 600; }
.primary-btn { height: 36px; padding: 0 14px; border-radius: 10px; background: #1f5fd1; color: #fff; font-weight: 600; }

/* Notebook shelf and covers */
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 16px 14px; padding: 6px 8px 10px; }
.book { display: flex; flex-direction: column; min-width: 0; text-align: left; }
.book-name { max-width: 100%; margin-top: 7px; font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-head { display: flex; align-items: flex-start; gap: 14px; padding: 6px 8px 12px; }
.book-head .cover { flex: none; width: 96px; }
.book-head-side { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; }
.cover {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  container-type: inline-size;
  overflow: hidden;
  display: grid;
  place-items: center;
  border-radius: 3px 9px 9px 3px;
  background-size: cover;
  background-position: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.28), inset 5px 0 6px -3px rgba(0, 0, 0, 0.45);
  color: #f5f2ea;
}
.cover-label { width: 78%; max-height: 80%; overflow: hidden; font-size: 12cqw; font-weight: 600; line-height: 1.2; text-align: center; overflow-wrap: anywhere; }
.cover-hand { display: block; width: 100%; height: auto; }
/* Classic: black hardcover with an elastic band */
.cover-classic { background-image: linear-gradient(140deg, #323236, #151517 70%); }
.cover-classic::after { content: ""; position: absolute; top: 0; bottom: 0; right: 11%; width: 5.5%; background: linear-gradient(90deg, #0a0a0b, #242427); box-shadow: 0 0 3px rgba(0, 0, 0, 0.6); }
.cover-classic .cover-label { width: 62%; margin-right: 14%; font-weight: 500; letter-spacing: 0.02em; color: #d9d4c7; }
/* Marble: composition book with a taped spine and a white label */
.cover-comp { background-color: #111; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='320'%3E%3Cfilter id='m' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.085' numOctaves='2' seed='11'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3CfeComponentTransfer%3E%3CfeFuncR type='discrete' tableValues='0.06 0.06 0.06 0.96 0.96 0.06 0.06'/%3E%3CfeFuncG type='discrete' tableValues='0.06 0.06 0.06 0.96 0.96 0.06 0.06'/%3E%3CfeFuncB type='discrete' tableValues='0.06 0.06 0.06 0.96 0.96 0.06 0.06'/%3E%3CfeFuncA type='linear' slope='0' intercept='1'/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23m)'/%3E%3C/svg%3E"); }
.cover-comp::before { content: ""; position: absolute; top: 0; bottom: 0; left: 0; width: 11%; background: #0c0c0c; }
.cover-comp .cover-label { width: 64%; margin-left: 9%; padding: 7% 5%; border-radius: 3px; background: #fff; color: #111; box-shadow: 0 0 0 1.5px #111, 0 0 0 4px #fff, 0 0 0 5.5px #111; }
.cover-color { background-image: linear-gradient(140deg, rgba(255, 255, 255, 0.16), rgba(0, 0, 0, 0.2)); }
.cover-photo { background-color: #555; }
.cover-photo .cover-label { position: absolute; left: 0; right: 0; bottom: 0; width: auto; padding: 6% 8%; background: rgba(0, 0, 0, 0.5); font-size: 10.5cqw; }
.cover-new { border: 2px dashed var(--line); background: none; box-shadow: none; color: var(--muted); }
.cover-new svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

/* To Do list */
.todo-add { width: calc(100% - 4px); height: 42px; margin: 2px 2px 8px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; background: transparent; color: inherit; font: inherit; font-size: 16px; -webkit-user-select: text; user-select: text; }
.check { flex: none; display: grid; place-items: center; width: 26px; height: 26px; margin: 0 4px 0 8px; border: 2px solid var(--muted); border-radius: 7px; color: transparent; }
.check svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.todo .note-item .t { white-space: normal; font-weight: 500; }
.todo.done .check { border-color: #1f5fd1; background: #1f5fd1; color: #fff; }
.todo.done .note-item .t { color: var(--muted); text-decoration: line-through; }
.done-list { margin-top: 10px; }
.done-list summary { padding: 8px 10px; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; }

.note-item { display: block; width: 100%; padding: 10px; border-radius: 10px; text-align: left; }
.note-item[aria-current="true"] { background: var(--active); }
.note-item .t { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-item .d { display: block; margin-top: 2px; font-size: 12px; color: var(--muted); }

dialog {
  width: min(350px, calc(100vw - 32px));
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--fg);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog h3 { margin: 0 0 6px; font-size: 17px; }
dialog p { margin: 0 0 12px; font-size: 14px; line-height: 1.4; color: var(--muted); }
dialog input {
  display: block;
  width: 100%;
  height: 44px;
  margin-bottom: 8px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 16px;
  -webkit-user-select: text;
  user-select: text;
}
dialog .err { min-height: 1.3em; margin-bottom: 8px; color: var(--danger); }
dialog .row { display: flex; justify-content: flex-end; gap: 8px; }
dialog .row button { height: 40px; padding: 0 16px; border-radius: 10px; font-weight: 600; }
dialog .row .primary { background: #1f5fd1; color: #fff; }
dialog .row { flex-wrap: wrap; }
#choice-text { white-space: pre-line; -webkit-user-select: text; user-select: text; }
dialog .row .danger { background: #c62828; color: #fff; }
dialog .label { padding-left: 2px; }
dialog .seg { margin: 0 0 10px; }
.seg.two { grid-template-columns: 1fr 1fr; }
#book-preview { width: 112px; margin: 0 auto 14px; }
#book-colors { display: flex; justify-content: center; margin-bottom: 8px; }
#book-photo { margin-bottom: 10px; }
#book-pad-wrap { position: relative; margin-bottom: 4px; }
#book-pad { display: block; width: 100%; aspect-ratio: 300 / 90; border: 1px solid var(--line); border-radius: 10px; background: #fdfdfb; touch-action: none; cursor: crosshair; }
#book-pad-clear { position: absolute; top: 6px; right: 6px; background: rgba(20, 25, 40, 0.08); color: #333; }
#pick-list { max-height: 50vh; margin-bottom: 12px; overflow-y: auto; }

#toast {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: calc(78px + var(--safe-b));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 9px 14px;
  border-radius: 10px;
  background: #1b1b1f;
  color: #fff;
  font-size: 14px;
  box-shadow: var(--shadow);
}
