:root {
  --bg: #ffffff;
  --bg-soft: #f6f6f7;
  --bg-elev: #ffffff;
  --border: #e6e6e8;
  --text: #1a1a1c;
  --text-dim: #8a8a90;
  --text-faint: #b4b4ba;
  --accent: #2f6fed;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --sidebar-w: 280px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* Dark palette. Applied automatically when the system is dark (unless the user
   has forced light), or explicitly when data-theme="dark". */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e0e10;
    --bg-soft: #161618;
    --bg-elev: #1c1c1f;
    --border: #2a2a2e;
    --text: #ececee;
    --text-dim: #8a8a90;
    --text-faint: #5a5a60;
    --accent: #5a8cff;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  --bg: #0e0e10;
  --bg-soft: #161618;
  --bg-elev: #1c1c1f;
  --border: #2a2a2e;
  --text: #ececee;
  --text-dim: #8a8a90;
  --text-faint: #5a5a60;
  --accent: #5a8cff;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  transition: grid-template-columns 0.18s ease;
}
body.sidebar-hidden #app,
body.focus-mode #app { grid-template-columns: 0 1fr; }

/* ---- sidebar ---- */
#sidebar {
  border-right: 1px solid var(--border);
  background: var(--bg-soft);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 8px;
  padding-top: max(14px, env(safe-area-inset-top));
}
.brand {
  font-weight: 600;
  letter-spacing: 0.5px;
  font-size: 14px;
  color: var(--text-dim);
}
#search {
  margin: 4px 12px 8px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elev);
  color: var(--text);
  font-size: 13px;
  outline: none;
}
#search:focus { border-color: var(--accent); }

#note-list {
  list-style: none;
  margin: 0;
  padding: 4px 8px 16px;
  overflow-y: auto;
  flex: 1;
}
.note-item {
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
}
.note-item:hover { background: var(--bg-elev); }
.note-item.active { background: var(--bg-elev); box-shadow: inset 0 0 0 1px var(--border); }
.note-row { display: flex; align-items: baseline; gap: 6px; }
.note-title {
  font-size: 13.5px;
  font-weight: 550;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
.pin-dot { color: var(--accent); font-size: 11px; }
.note-time { font-size: 11px; color: var(--text-faint); flex-shrink: 0; }
.note-preview {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}
.list-empty { color: var(--text-faint); font-size: 13px; padding: 16px 10px; }

.sidebar-foot {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
.sidebar-foot .icon-btn { font-size: 15px; }

/* Backdrop behind the mobile drawer; only ever visible on narrow screens. */
#scrim { display: none; }

/* On narrow windows (e.g. a slim window pinned beside a call) the sidebar
   overlays the editor instead of squeezing it; editor stays full-width. */
@media (max-width: 720px) {
  /* sidebar is position:fixed below, so the editor pane is the only in-flow
     grid child — give it the full single column. Must match the specificity of
     `body.sidebar-hidden #app` to override it. */
  #app,
  body.sidebar-hidden #app,
  body.focus-mode #app { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(86vw, 300px);
    z-index: 40;
    box-shadow: var(--shadow);
    transition: transform 0.18s ease;
  }
  body.sidebar-hidden #sidebar { transform: translateX(-100%); box-shadow: none; }

  /* drawer open → dim the editor behind it; tap the dim area to close */
  body:not(.sidebar-hidden) #scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 39;
  }
}

/* ---- editor pane ---- */
#editor-pane { display: flex; flex-direction: column; min-width: 0; }
#bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 14px;
  /* clear the status bar / notch when installed as a standalone PWA */
  padding-top: max(10px, env(safe-area-inset-top));
  padding-right: max(14px, env(safe-area-inset-right));
  border-bottom: 1px solid transparent;
}
.bar-spacer { flex: 1; }
.icon-btn {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 16px;
  line-height: 1;
  width: 32px;
  height: 32px;
  border-radius: 7px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--bg-soft); color: var(--text); }
.icon-btn.on { color: var(--accent); }
body.focus-mode #bar { opacity: 0; transition: opacity 0.2s; }
body.focus-mode #bar:hover { opacity: 1; }

#editor-scroll { flex: 1; overflow-y: auto; }
#editor { max-width: 720px; margin: 0 auto; padding: 32px 40px 30vh; }

/* ---- prose ---- */
.scrbl-prose { outline: none; font-size: 16px; line-height: 1.7; color: var(--text); }
.scrbl-prose:focus { outline: none; }
.scrbl-prose > * { margin: 0 0 0.7em; }
/* First line reads as the note's title: larger + bold automatically. */
.scrbl-prose > p:first-child {
  font-size: 1.45em;
  font-weight: 650;
  line-height: 1.3;
  margin-bottom: 0.5em;
}
.scrbl-prose h1 { font-size: 1.7em; font-weight: 650; margin: 0.4em 0 0.4em; }
.scrbl-prose h2 { font-size: 1.35em; font-weight: 650; margin: 0.5em 0 0.3em; }
.scrbl-prose h3 { font-size: 1.12em; font-weight: 650; }
.scrbl-prose a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.scrbl-prose code {
  background: var(--bg-soft);
  padding: 0.12em 0.34em;
  border-radius: 5px;
  font-size: 0.88em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.scrbl-prose pre {
  background: var(--bg-soft);
  padding: 12px 14px;
  border-radius: 10px;
  overflow-x: auto;
}
.scrbl-prose pre code { background: none; padding: 0; }
.scrbl-prose blockquote {
  border-left: 3px solid var(--border);
  margin-left: 0;
  padding-left: 14px;
  color: var(--text-dim);
}
.scrbl-prose ul, .scrbl-prose ol { padding-left: 1.4em; }
.scrbl-prose li { margin: 0.15em 0; }
.scrbl-prose hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }

/* placeholder for an empty doc */
.scrbl-prose p.is-editor-empty:first-child::before {
  content: "Start writing…";
  color: var(--text-faint);
  float: left;
  height: 0;
  pointer-events: none;
}

/* task list / checkboxes */
.scrbl-prose ul[data-type="taskList"] { list-style: none; padding-left: 0.2em; }
.scrbl-prose ul[data-type="taskList"] li { display: flex; align-items: flex-start; gap: 0.55em; }
.scrbl-prose ul[data-type="taskList"] li > label { margin-top: 0.32em; }
.scrbl-prose ul[data-type="taskList"] li > div { flex: 1; }
.scrbl-prose ul[data-type="taskList"] input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.scrbl-prose ul[data-type="taskList"] li[data-checked="true"] > div { color: var(--text-dim); text-decoration: line-through; }

/* ---- toast ---- */
#toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  border-radius: 10px;
  padding: 9px 10px 9px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  z-index: 50;
}
#toast[hidden] { display: none; }
#toast button {
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: 7px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
}

/* ---- switcher ---- */
#switcher {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.28);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 14vh;
  z-index: 60;
}
#switcher[hidden] { display: none; }
#switcher-box {
  width: min(560px, 92vw);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
#switcher-input {
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 16px;
  padding: 16px 18px;
  outline: none;
}
#switcher-list { list-style: none; margin: 0; padding: 6px; max-height: 50vh; overflow-y: auto; }
.switcher-item {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  padding: 9px 12px;
  border-radius: 8px;
  cursor: pointer;
}
.switcher-item.active { background: var(--bg-soft); }
.switcher-item .note-title { flex: 1; }

/* ---- sync panel ---- */
#sync-panel {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.28);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 12vh;
  z-index: 60;
}
#sync-panel[hidden] { display: none; }
#sync-box {
  width: min(440px, 92vw);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 22px 22px 20px;
}
#sync-box h2 { margin: 0 0 4px; font-size: 16px; }
.sync-status { margin: 0 0 16px; font-size: 13px; color: var(--text-dim); }
.sync-status.ok { color: #2e9e5b; }
.sync-status.err { color: #d9544f; }
#sync-box label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 12px;
}
#sync-box input {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
}
#sync-box input:focus { border-color: var(--accent); }
.sync-actions { display: flex; gap: 8px; margin: 4px 0 14px; }
.sync-actions button {
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 13px;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
}
.sync-actions button.ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.sync-hint { margin: 0; font-size: 12px; color: var(--text-faint); line-height: 1.5; }
#sync-btn.syncing { opacity: 0.5; }
#sync-btn.err { color: #d9544f; }
