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

:root {
  --sidebar-w: 260px;
  --bg: #f8f9fa;
  --sidebar-bg: #1e1e2e;
  --sidebar-text: #cdd6f4;
  --sidebar-hover: #313244;
  --sidebar-active: #45475a;
  --accent: #89b4fa;
  --border: #e0e0e0;
  --text: #1e1e2e;
  --text-muted: #6c7086;
  --danger: #f38ba8;
  --warn: #f9c74f;
}

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; overflow: hidden;
}
.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-actions { display: flex; align-items: center; gap: 8px; }
#sidebar-toggle { display: none; }
#new-note-btn {
  background: var(--accent); color: var(--sidebar-bg); 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; 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); }

/* Conflicting copies: grouped by a shared accent rail so the pair reads as one
   entry with two rows rather than two notes that drifted apart in the list. */
.note-item.in-group { border-left: 3px solid var(--warn); border-radius: 0 6px 6px 0; margin-bottom: 1px; padding-left: 9px; }
.note-item.group-first { border-top-left-radius: 6px; }
.note-item.group-last { border-bottom-left-radius: 6px; margin-bottom: 4px; }
.copy-badge {
  font-size: 10px; font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase;
  color: var(--sidebar-bg); background: var(--warn); border-radius: 3px; padding: 1px 5px;
}
/* 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 state, pinned under the notes list: the honest answer to "is my
   writing safe yet?", which local-first has to keep visible. */
.sync-status {
  display: flex; align-items: center; gap: 8px; flex-shrink: 0;
  padding: 10px 16px; border-top: 1px solid var(--sidebar-active);
  font-size: 12px; color: var(--text-muted);
}
.sync-dot { width: 7px; height: 7px; border-radius: 50%; background: #59c98b; 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); }

/* 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: #fff;
}
#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: #fff; padding: 5px 12px; font-size: 13px;
  cursor: pointer; color: var(--text-muted); transition: all 0.15s;
}
.view-toggle button.active { background: var(--accent); color: #fff; }
.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: #c0324b; border: 1px solid #c0324b; 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: #fff; color: var(--text);
}
#preview {
  flex: 1; padding: 20px 24px; overflow-y: auto; background: #fff;
  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: #f0f4ff; border-radius: 0 4px 4px 0; }
#preview pre { background: #1e1e2e; color: #cdd6f4; 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: #e8ecf1; padding: 2px 6px; border-radius: 3px; }
#preview a { color: var(--accent); }
#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: #f0f4ff; 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; }
  #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%; }
}


/* Conflict banner — the note on top of both copies */
.banner {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: #fff8e1; border-bottom: 1px solid #f0d98a;
  font-size: 13px; color: #6b5300;
}
.banner-text { flex: 1; min-width: 220px; }
.banner-actions { display: flex; gap: 8px; flex-shrink: 0; }
.banner-btn {
  border: 1px solid #d9be6a; background: #fff; color: #6b5300; border-radius: 6px;
  padding: 4px 10px; font-size: 12px; font-weight: 500; cursor: pointer;
}
.banner-btn:hover { background: #fffdf5; }
.banner-btn.danger { border-color: #c0324b; color: #c0324b; font-weight: 600; }

/* Toast — how a conflict announces itself, since it happens during a sync the
   user did not ask for and may not be looking at. */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 12px; max-width: min(560px, calc(100vw - 32px));
  background: var(--sidebar-bg); color: var(--sidebar-text);
  padding: 12px 16px; border-radius: 8px; font-size: 13px; line-height: 1.4;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25); z-index: 20;
}
.toast-btn {
  border: 1px solid var(--sidebar-active); background: transparent; color: var(--accent);
  border-radius: 6px; padding: 4px 10px; font-size: 12px; font-weight: 600; cursor: pointer;
  flex-shrink: 0;
}
.toast-btn:hover { background: var(--sidebar-hover); }

@media (max-width: 700px) {
  .toast { bottom: 12px; }
  .banner { padding: 8px 12px; }
}
