:root {
  --desk:        #333d37;
  --desk-edge:   #29312c;
  --desk-text:   #c3ccc2;
  --desk-dim:    #8b968a;
  --paper:       #fffdf9;
  --rule:        #bacddd;
  --rule-strong: #a8bfd3;
  --margin-line: #d98c86;
  --ink:         #1f2a44;
  --ink-dim:     #6e7688;
  --pencil:      #e3b23c;
  --shadow:      rgba(0,0,0,.42);
  --line:        30px;
  --pad-x:       clamp(20px, 6vw, 64px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --desk: #12161a; --desk-edge: #0d1013; --desk-text: #c6ccd2; --desk-dim: #7d8791;
    --paper: #1c2126; --rule: #2f3b45; --rule-strong: #3a4854; --margin-line: #6f4340;
    --ink: #e3e6e4; --ink-dim: #8d959c; --pencil: #d9aa45; --shadow: rgba(0,0,0,.6);
  }
}
:root[data-theme="dark"] {
  --desk: #12161a; --desk-edge: #0d1013; --desk-text: #c6ccd2; --desk-dim: #7d8791;
  --paper: #1c2126; --rule: #2f3b45; --rule-strong: #3a4854; --margin-line: #6f4340;
  --ink: #e3e6e4; --ink-dim: #8d959c; --pencil: #d9aa45; --shadow: rgba(0,0,0,.6);
}

* { box-sizing: border-box; }
html, body { height: 100%; overscroll-behavior: none; }
body {
  margin: 0;
  background: var(--desk);
  color: var(--desk-text);
  font-family: Karla, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; }
:focus-visible { outline: 2px solid var(--pencil); outline-offset: 2px; border-radius: 2px; }

.app { display: flex; height: 100dvh; overflow: hidden; }

/* ---------- sheets rail ---------- */
.rail {
  width: 236px; flex: none; display: flex; flex-direction: column;
  background: var(--desk-edge);
  border-right: 1px solid rgba(255,255,255,.06);
  padding-left: env(safe-area-inset-left);
}
.rail-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  padding: calc(20px + env(safe-area-inset-top)) 16px 12px;
}
.rail-head h1 {
  margin: 0; font-family: Newsreader, Georgia, serif;
  font-size: 21px; font-weight: 500; color: var(--desk-text);
}
.count { font-size: 12.5px; color: var(--desk-dim); }
.new-btn {
  margin: 0 12px 10px; padding: 10px 12px; text-align: left; font-weight: 500;
  border: 1px dashed rgba(255,255,255,.2); border-radius: 3px;
}
.new-btn:hover { border-color: var(--pencil); color: var(--pencil); }
.list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 8px 16px; }
.item {
  position: relative; display: block; width: 100%; text-align: left;
  padding: 10px 32px 10px 12px; border-radius: 3px; color: var(--desk-text);
}
.item + .item { margin-top: 1px; }
.item[aria-current="true"] { background: rgba(255,255,255,.08); box-shadow: inset 2px 0 0 var(--pencil); }
.item .t { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .d { display: block; font-size: 12px; color: var(--desk-dim); margin-top: 2px; }
.item .d .dot { color: var(--pencil); }
.del {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 28px; height: 28px; color: var(--desk-dim); border-radius: 3px; opacity: .55;
}
.del:hover { opacity: 1; color: var(--margin-line); background: rgba(255,255,255,.07); }
.rail-foot {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid rgba(255,255,255,.06); font-size: 12.5px;
}
.rail-foot button { color: var(--desk-dim); font-size: 12.5px; }
.rail-foot button:hover { color: var(--desk-text); }
#sync-state[data-state="synced"] { color: var(--desk-text); }
#sync-state[data-state="error"] { color: var(--margin-line); }

/* ---------- top bar ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: calc(10px + env(safe-area-inset-top)) clamp(12px, 3vw, 28px) 10px;
}
.spacer { flex: 1; }
.icon-btn { display: none; padding: 6px 8px; color: var(--desk-text); font-size: 17px; }
.title-in {
  flex: 0 1 200px; min-width: 90px; background: none; border: 0; padding: 4px 2px;
  font-family: Newsreader, Georgia, serif; font-size: 18px; color: var(--desk-text);
  border-bottom: 1px solid transparent;
}
.title-in::placeholder { color: var(--desk-dim); }
.title-in:hover { border-bottom-color: rgba(255,255,255,.15); }
.title-in:focus { outline: none; border-bottom-color: var(--pencil); }

.seg { display: flex; gap: 1px; background: rgba(255,255,255,.07); padding: 2px; border-radius: 4px; }
.seg button { padding: 6px 11px; font-size: 13px; color: var(--desk-dim); border-radius: 3px; white-space: nowrap; }
.seg button[aria-pressed="true"] { background: var(--desk); color: var(--desk-text); }
.seg button[aria-pressed="true"].hot { color: var(--pencil); }

.pens { display: flex; align-items: center; gap: 6px; }
.pen { width: 30px; height: 30px; border-radius: 50%; border: 2px solid transparent; display: grid; place-items: center; }
.pen i { width: 14px; height: 14px; border-radius: 50%; display: block; }
.pen[aria-pressed="true"] { border-color: var(--pencil); }
.tool { padding: 6px 10px; font-size: 13px; color: var(--desk-dim); border-radius: 3px; }
.tool:hover { color: var(--desk-text); background: rgba(255,255,255,.06); }
.tool[aria-pressed="true"] { color: var(--pencil); background: rgba(255,255,255,.06); }
.status { font-size: 12.5px; color: var(--desk-dim); min-width: 48px; text-align: right; }

/* ---------- the sheet ---------- */
.desk {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 0 clamp(8px, 3vw, 28px) calc(64px + env(safe-area-inset-bottom));
}
.sheet-wrap { max-width: 860px; margin: 0 auto; }
.sheet {
  position: relative; background: var(--paper);
  box-shadow: 0 1px 2px var(--shadow), 0 14px 34px -14px var(--shadow);
  min-height: 60vh;
}
.surface { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.sheet[data-paper="ruled"] .surface {
  background-image: linear-gradient(var(--margin-line), var(--margin-line));
  background-repeat: no-repeat; background-size: 1px 100%;
  background-position: calc(var(--pad-x) - 16px) 0;
}
.lines { position: absolute; inset: 0; }
.sheet[data-paper="ruled"] .lines {
  background-image: repeating-linear-gradient(to bottom,
    transparent 0, transparent calc(var(--line) - 1px),
    var(--rule) calc(var(--line) - 1px), var(--rule) var(--line));
}
.sheet[data-paper="grid"] .lines {
  background-image:
    repeating-linear-gradient(to bottom, var(--rule) 0 1px, transparent 1px var(--line)),
    repeating-linear-gradient(to right, var(--rule) 0 1px, transparent 1px var(--line));
}
.sheet[data-paper="dot"] .lines {
  background-image: radial-gradient(circle at 1px 1px, var(--rule-strong) 1.3px, transparent 1.4px);
  background-size: var(--line) var(--line);
}
.sheet[data-paper="blank"] .lines { background-image: none; }

/* The canvas is purely a display surface. All pointer input is handled on
   .sheet so that an Apple Pencil can draw over the text field as well. */
.pen-layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.sheet { touch-action: pan-y; }
.writing {
  position: relative; display: block; width: 100%; resize: none; border: 0; background: none;
  padding: 0 var(--pad-x); overflow: hidden;
  font-family: Newsreader, Georgia, serif; font-size: 17.5px; line-height: var(--line);
  color: var(--ink); caret-color: var(--pencil);
}
.sheet[data-paper="grid"] .writing { font-family: "IBM Plex Mono", ui-monospace, monospace; font-size: 14px; }
.writing:focus { outline: none; }
.writing::placeholder { color: var(--ink-dim); }
.sheet[data-mode="draw"] .writing { pointer-events: none; }
.sheet[data-mode="draw"] { cursor: crosshair; }
.sheet[data-mode="draw"][data-fingers="draw"] { touch-action: none; }

.hint { max-width: 860px; margin: 10px auto 0; font-size: 12.5px; color: var(--desk-dim); min-height: 1em; }

/* ---------- setup ---------- */
.setup {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: center;
  background: rgba(0,0,0,.55); padding: 20px;
}
.setup[hidden] { display: none; }
.setup-card {
  width: min(420px, 100%); background: var(--desk-edge); border-radius: 6px;
  padding: 24px; display: flex; flex-direction: column; gap: 8px;
  box-shadow: 0 20px 60px -20px rgba(0,0,0,.8);
}
.setup-card h2 { margin: 0; font-family: Newsreader, Georgia, serif; font-weight: 500; font-size: 22px; }
.setup-card p { margin: 0 0 8px; color: var(--desk-dim); font-size: 13.5px; line-height: 1.5; }
.setup-card label { font-size: 12.5px; color: var(--desk-dim); margin-top: 6px; }
.setup-card input {
  background: rgba(0,0,0,.25); border: 1px solid rgba(255,255,255,.12);
  border-radius: 3px; padding: 10px; color: var(--desk-text);
}
.setup-msg { min-height: 1.2em; color: var(--margin-line); font-size: 13px; }
.setup-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.primary {
  background: var(--pencil); color: #23201a; font-weight: 600;
  padding: 10px 18px; border-radius: 3px;
}

/* ---------- narrow / drawer ---------- */
.backdrop { display: none; }
@media (max-width: 820px) {
  .rail {
    position: fixed; inset: 0 auto 0 0; z-index: 20; width: 270px;
    transform: translateX(-102%); transition: transform .22s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.5);
  }
  body[data-drawer="open"] .rail { transform: none; }
  body[data-drawer="open"] .backdrop { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.45); }
  .icon-btn { display: block; }
  .bar { gap: 8px; padding-left: 10px; padding-right: 10px; }
  .title-in { flex: 1 1 120px; font-size: 16px; }
  #print, #export { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ---------- print ---------- */
@media print {
  .rail, .bar, .hint, .backdrop, .setup { display: none !important; }
  body, .app, .desk { display: block; height: auto; overflow: visible; background: #fff; }
  .desk { padding: 0; }
  .sheet { box-shadow: none; background: #fff; min-height: 0; }
  .writing { color: #000; }
  @page { margin: 12mm; }
}
