*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  color-scheme: light dark;
  --sidebar-w: 260px;

  /* Light (the default). Dark mode redefines every colour below. */
  --bg: #f8f9fa;
  --surface: #ffffff;          /* editor, preview, toolbar, cards */
  --surface-alt: #fbfbfd;      /* comments panel */
  --surface-tint: #f0f4ff;     /* hovers, blockquotes, table heads */
  --code-bg: #e8ecf1;
  --pre-bg: #1e1e2e;
  --pre-text: #cdd6f4;
  --text: #1e1e2e;
  --text-muted: #6c7086;
  --border: #e0e0e0;
  --border-soft: #f0f0f0;
  --accent: #89b4fa;
  --on-accent: #1e1e2e;        /* text on an accent background */
  --link: #3b6fd4;
  --danger: #c0324b;           /* error text and the armed delete button */
  --danger-dot: #f38ba8;
  --ok: #59c98b;
  --sidebar-bg: #f1f2f6;
  --sidebar-text: #1e1e2e;
  --sidebar-hover: #e3e5ee;
  --sidebar-active: #d6d9e6;
  --menu-bg: #ffffff;
  --alert-bg: #e3e5ee;
  --alert-error-bg: #fde4e9;
  --alert-error-text: #8a1c33;
  --mark-bg: #fdeeb5;
  --mark-active: #f9d66b;
  --mark-line: #f5c242;
  --mark-ring: rgba(245, 194, 66, 0.35);
  --notice-bg: #fff8e6;
  --notice-border: #f5d27a;
  --fab-bg: #1e1e2e;
  --fab-text: #ffffff;
  --shadow: rgba(0, 0, 0, 0.12);
  --shadow-strong: rgba(0, 0, 0, 0.2);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #181825;
    --surface: #1e1e2e;
    --surface-alt: #1a1a28;
    --surface-tint: #2a2b3d;
    --code-bg: #313244;
    --pre-bg: #11111b;
    --pre-text: #cdd6f4;
    --text: #cdd6f4;
    --text-muted: #9399b2;
    --border: #383a4f;
    --border-soft: #2c2d3f;
    --accent: #89b4fa;
    --on-accent: #1e1e2e;
    --link: #89b4fa;
    --danger: #f38ba8;
    --danger-dot: #f38ba8;
    --ok: #59c98b;
    --sidebar-bg: #181825;
    --sidebar-text: #cdd6f4;
    --sidebar-hover: #313244;
    --sidebar-active: #45475a;
    --menu-bg: #1e1e2e;
    --alert-bg: #313244;
    --alert-error-bg: #5a2333;
    --alert-error-text: #f5c2cf;
    --mark-bg: rgba(245, 194, 66, 0.25);
    --mark-active: rgba(245, 194, 66, 0.5);
    --mark-line: #f5c242;
    --mark-ring: rgba(245, 194, 66, 0.35);
    --notice-bg: #3a3220;
    --notice-border: #7a6326;
    --fab-bg: #cdd6f4;
    --fab-text: #1e1e2e;
    --shadow: rgba(0, 0, 0, 0.4);
    --shadow-strong: rgba(0, 0, 0, 0.5);
  }
}

/* Form controls don't inherit colour on their own. */
input, textarea, button { color: inherit; }

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); }

#app { display: flex; height: 100%; height: 100dvh; }

/* Sidebar */
#sidebar {
  width: var(--sidebar-w); min-width: var(--sidebar-w);
  background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: relative; z-index: 10;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px; border-bottom: 1px solid var(--sidebar-active);
}
.sidebar-header h1 { font-size: 18px; font-weight: 600; }
.sidebar-title { min-width: 0; }
.space-subtitle { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.sidebar-actions { display: flex; align-items: center; gap: 8px; }
#sidebar-toggle { display: none; }
#new-note-btn {
  background: var(--accent); color: var(--on-accent); border: none; border-radius: 6px;
  width: 32px; height: 32px; font-size: 20px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
#new-note-btn:hover { opacity: 0.85; }
#note-list { flex: 1; min-height: 0; overflow-y: auto; padding: 8px; }
.note-item {
  padding: 10px 12px; border-radius: 6px; cursor: pointer; margin-bottom: 4px;
  transition: background 0.15s;
}
.note-item:hover { background: var(--sidebar-hover); }
.note-item.active { background: var(--sidebar-active); }
.note-item-title { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-item-meta { display: flex; align-items: center; gap: 6px; margin-top: 2px; }
.note-item-date { font-size: 11px; color: var(--text-muted); }

/* An unsynced note keeps a dot next to its title until the server has it. */
.pending-dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent); margin-left: 6px; vertical-align: middle;
}

/* Sync is quiet: its state lives in the menu, and only a problem (a failed sync,
   or changes waiting over 10 seconds) shows outside it. */
.sync-status { display: flex; align-items: center; gap: 8px; padding: 4px 8px; font-size: 12px; color: var(--text-muted); }
.sync-alert {
  flex-shrink: 0; margin: 0 8px 8px; padding: 6px 10px; border: none; border-radius: 6px; text-align: left;
  background: var(--alert-bg); color: var(--sidebar-text); font-size: 12px; cursor: pointer;
}
.sync-alert.state-error { background: var(--alert-error-bg); color: var(--alert-error-text); }
.install-help { padding: 0 8px; font-size: 12px; color: var(--text-muted); }
.sync-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); flex-shrink: 0; }
.sync-status.has-pending .sync-dot, .sync-status.state-syncing .sync-dot { background: var(--accent); }
.sync-status.state-offline .sync-dot { background: var(--text-muted); }
.sync-status.state-error .sync-dot { background: var(--danger-dot); }

/* Main */
main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-height: 0; }
#editor-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-height: 0; }
#empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--text-muted); font-size: 16px; }
.hidden { display: none !important; }

/* Toolbar */
.toolbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
#title-input {
  flex: 1; border: none; outline: none; font-size: 16px; font-weight: 600;
  background: transparent; min-width: 0;
}
.toolbar-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.view-toggle { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.view-toggle button {
  border: none; background: var(--surface); padding: 5px 12px; font-size: 13px;
  cursor: pointer; color: var(--text-muted); transition: all 0.15s;
}
.view-toggle button.active { background: var(--accent); color: var(--on-accent); }
.view-toggle button:not(:last-child) { border-right: 1px solid var(--border); }
#delete-btn { background: none; border: none; font-size: 18px; cursor: pointer; padding: 4px; opacity: 0.6; }
#delete-btn:hover { opacity: 1; }
/* Second click confirms — a modal confirm() would block the whole page. */
#delete-btn.armed { opacity: 1; font-size: 13px; font-weight: 600; color: var(--danger); border: 1px solid var(--danger); border-radius: 6px; padding: 4px 8px; }

/* Panes */
#panes { flex: 1; display: flex; overflow: hidden; min-height: 0; }
#editor {
  flex: 1; border: none; outline: none; resize: none; padding: 20px 24px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace; font-size: 14px;
  line-height: 1.7; background: var(--surface); color: var(--text);
}
#preview {
  flex: 1; padding: 20px 24px; overflow-y: auto; background: var(--surface);
  border-left: 1px solid var(--border); line-height: 1.7; font-size: 15px;
}

/* View modes */
.view-edit #preview { display: none; }
.view-edit #editor { flex: 1; }
.view-preview #editor { display: none; }
.view-preview #preview { flex: 1; border-left: none; }
.view-split #editor { flex: 1; }
.view-split #preview { flex: 1; }

/* Preview typography */
#preview h1 { font-size: 1.8em; margin: 0.6em 0 0.4em; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
#preview h2 { font-size: 1.4em; margin: 0.5em 0 0.3em; border-bottom: 1px solid var(--border); padding-bottom: 0.2em; }
#preview h3 { font-size: 1.2em; margin: 0.5em 0 0.3em; }
#preview h4, #preview h5, #preview h6 { font-size: 1em; margin: 0.5em 0 0.3em; }
#preview p { margin: 0.6em 0; }
#preview ul, #preview ol { margin: 0.5em 0; padding-left: 1.8em; }
#preview li { margin: 0.2em 0; }
#preview blockquote { border-left: 3px solid var(--accent); margin: 0.6em 0; padding: 0.3em 1em; color: var(--text-muted); background: var(--surface-tint); border-radius: 0 4px 4px 0; }
#preview pre { background: var(--pre-bg); color: var(--pre-text); padding: 14px 18px; border-radius: 6px; overflow-x: auto; margin: 0.6em 0; font-size: 13px; line-height: 1.5; }
#preview code { font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace; font-size: 0.9em; }
#preview :not(pre) > code { background: var(--code-bg); padding: 2px 6px; border-radius: 3px; }
#preview a { color: var(--link); }
#preview hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }
#preview img { max-width: 100%; border-radius: 4px; }
#preview table { border-collapse: collapse; margin: 0.6em 0; width: 100%; }
#preview th, #preview td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
#preview th { background: var(--surface-tint); font-weight: 600; }

/* Mobile */
@media (max-width: 700px) {
  #app { flex-direction: column; }
  #sidebar { width: 100%; min-width: 100%; max-height: 45vh; border-bottom: 1px solid var(--sidebar-active); transition: max-height 0.25s ease; }
  .settings { padding: 16px; }
  #sidebar.collapsed { max-height: none; }
  #sidebar.collapsed #note-list { display: none; }
  #sidebar-toggle {
    display: flex; align-items: center; justify-content: center;
    background: none; border: 1px solid var(--sidebar-active); border-radius: 6px;
    width: 32px; height: 32px; font-size: 16px; cursor: pointer; color: var(--sidebar-text);
  }
  #sidebar-toggle:hover { background: var(--sidebar-hover); }
  #panes { flex-direction: column; }
  .view-split #preview { border-left: none; border-top: 1px solid var(--border); }
  .toolbar { flex-wrap: wrap; }
  #title-input { width: 100%; }
}

/* Signed out, or not the person's own copy: one centred message. */
.centered {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 16px; text-align: center; color: var(--text-muted);
}
.centered h1 { font-size: 24px; color: var(--text); }
.centered a { color: var(--link); }
.centered .primary {
  background: var(--accent); color: var(--on-accent); border: none; border-radius: 6px;
  padding: 10px 20px; font-size: 15px; font-weight: 600; cursor: pointer;
}
.centered .primary:hover { opacity: 0.85; }

/* ---- Spaces, sharing and comments ---- */
[hidden] { display: none !important; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.error { color: var(--danger); }
.small-btn {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; color: var(--text);
  font-size: 13px; padding: 4px 10px; cursor: pointer; white-space: nowrap;
}
.small-btn:hover { background: var(--surface-tint); }
.small-btn.active { border-color: var(--accent); color: var(--link); }
.primary-btn { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.link-btn { background: none; border: none; color: var(--link); cursor: pointer; font-size: 13px; padding: 2px 4px; }
#sidebar .link-btn { color: var(--link); }
#sidebar .small-btn { background: none; border-color: var(--sidebar-active); color: var(--sidebar-text); }
#sidebar .small-btn:hover { background: var(--sidebar-hover); }

.menu-wrap { position: relative; }
.menu-btn {
  position: relative; background: none; border: 1px solid var(--sidebar-active); border-radius: 6px;
  width: 32px; height: 32px; font-size: 16px; cursor: pointer; color: var(--sidebar-text);
}
.menu-btn:hover { background: var(--sidebar-hover); }
.menu-alert { position: absolute; top: -3px; right: -3px; width: 9px; height: 9px; border-radius: 50%; background: var(--danger-dot); border: 2px solid var(--sidebar-bg); }
.app-menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 40; width: 250px; max-height: 70vh; overflow-y: auto;
  background: var(--menu-bg); border: 1px solid var(--sidebar-active); border-radius: 8px; box-shadow: 0 8px 28px var(--shadow-strong);
  padding: 8px; display: flex; flex-direction: column; gap: 4px;
}
.menu-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); padding: 2px 8px; }
.menu-sep { border-top: 1px solid var(--sidebar-active); margin: 4px 0; }
.menu-item {
  width: 100%; background: none; border: none; border-radius: 6px; color: var(--sidebar-text);
  padding: 7px 8px; font-size: 13px; cursor: pointer; text-align: left;
}
.menu-item:hover { background: var(--sidebar-hover); }
.space-item {
  display: flex; align-items: center; justify-content: space-between; gap: 6px; width: 100%;
  background: none; border: none; border-radius: 6px; color: var(--sidebar-text);
  padding: 6px 8px; font-size: 13px; cursor: pointer; text-align: left;
}
.space-item span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.space-item:hover { background: var(--sidebar-hover); }
.space-item.current { background: var(--sidebar-active); }
.new-space-form { display: flex; gap: 6px; }
.new-space-form input {
  flex: 1; min-width: 0; background: var(--surface); border: 1px solid var(--sidebar-active);
  border-radius: 6px; color: var(--sidebar-text); padding: 4px 8px; font-size: 13px;
}
.shared-tag {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--link);
  border: 1px solid var(--link); border-radius: 4px; padding: 0 4px; flex-shrink: 0;
}
/* Reviewers read: the title isn't an input to them. */
.role-reviewer #title-input { cursor: default; }

.share-wrap { position: relative; }
.share-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; width: 280px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 24px var(--shadow);
  padding: 12px; display: flex; flex-direction: column; gap: 8px;
}
.share-title { font-weight: 600; font-size: 13px; }
.share-row { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }

#doc-body { flex: 1; display: flex; min-height: 0; overflow: hidden; }
#doc-body #panes { flex: 1; min-width: 0; }
.comments-panel {
  width: 300px; flex-shrink: 0; overflow-y: auto; background: var(--surface-alt); border-left: 1px solid var(--border);
  padding: 12px; display: flex; flex-direction: column; gap: 10px;
}
#doc-body.comments-hidden .comments-panel, #doc-body.no-comments .comments-panel { display: none; }

/* Settings take the place of the document. */
.settings-view { flex: 1; overflow-y: auto; background: var(--surface); }
.settings { max-width: 640px; padding: 24px; display: flex; flex-direction: column; gap: 24px; }
.settings-head { display: flex; align-items: center; justify-content: space-between; }
.settings-head h2 { font-size: 20px; }
.settings section { display: flex; flex-direction: column; gap: 10px; }
.settings h3 { font-size: 15px; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.settings-space { display: flex; align-items: center; gap: 8px; }
.settings-space > form { flex: 1; }
.settings-space-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.rename-form { display: flex; gap: 6px; min-width: 0; }
.rename-form input {
  flex: 1; min-width: 0; border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px; font: inherit; font-size: 14px; background: var(--surface);
}
.settings-field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.settings-label, .settings-field > label { font-weight: 600; font-size: 13px; }
.switch-row { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; cursor: pointer; }
.access-summary { list-style: none; display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.person-name { font-weight: 500; }
.comments-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.comments-head h2 { font-size: 15px; }
.thread {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 10px;
  display: flex; flex-direction: column; gap: 8px; cursor: pointer;
}
.thread.active { border-color: var(--mark-line); box-shadow: 0 0 0 2px var(--mark-ring); }
.thread.resolved { opacity: 0.8; }
.thread.draft { cursor: default; }
.thread-quote { font-size: 12px; color: var(--text-muted); border-left: 3px solid var(--mark-line); padding-left: 8px; overflow-wrap: anywhere; }
.thread-quote.doc { border-left-color: var(--accent); font-style: italic; }
.thread-quote .detached { display: block; font-style: italic; color: var(--danger); }
.comment-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.comment-author { font-weight: 600; font-size: 13px; }
.comment-date { font-size: 11px; color: var(--text-muted); }
.comment-body { font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.comment + .comment { border-top: 1px solid var(--border-soft); padding-top: 8px; }
.thread textarea, .name-form input {
  width: 100%; border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font: inherit; font-size: 14px; resize: vertical; background: var(--surface);
}
.reply-form { display: flex; gap: 6px; align-items: flex-start; }
.thread-actions { display: flex; justify-content: flex-end; gap: 6px; }
.name-form { display: flex; flex-direction: column; gap: 6px; background: var(--notice-bg); border: 1px solid var(--notice-border); border-radius: 8px; padding: 10px; font-size: 13px; }
.resolved-list summary { cursor: pointer; font-size: 13px; color: var(--text-muted); padding: 4px 0; }
.resolved-list .thread { margin-top: 8px; }

#preview mark.comment-mark { background: var(--mark-bg); color: inherit; border-bottom: 2px solid var(--mark-line); cursor: pointer; }
#preview mark.comment-mark.active { background: var(--mark-active); }
.comment-fab {
  position: fixed; z-index: 30; background: var(--fab-bg); color: var(--fab-text); border: none; border-radius: 16px;
  padding: 6px 12px; font-size: 13px; cursor: pointer; box-shadow: 0 4px 14px var(--shadow-strong);
}

/* A guest's first visit: their name before anything else. */
.welcome {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  padding: 16px; background: var(--bg);
}
.welcome-card {
  width: 100%; max-width: 400px; display: flex; flex-direction: column; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 24px;
  box-shadow: 0 8px 28px var(--shadow);
}
.welcome-card h2 { font-size: 20px; }
.welcome-card p { font-size: 14px; line-height: 1.5; }
.welcome-card input {
  border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; font: inherit; font-size: 15px; background: var(--surface);
}
.welcome-btn { border-radius: 6px; padding: 8px 14px; font-size: 15px; cursor: pointer; border: 1px solid var(--accent); }

@media (max-width: 700px) {
  #doc-body { flex-direction: column; overflow-y: auto; }
  #doc-body #panes { flex: none; min-height: 60vh; }
  .comments-panel { width: 100%; border-left: none; border-top: 1px solid var(--border); overflow: visible; }
  .share-panel { right: auto; left: 0; }
}
