:root {
  --bg: #ffffff;
  --bg-elev: #f7f7f8;
  --bg-elev-2: #eeeef0;
  --text: #1d1d1f;
  --text-muted: #6b6b73;
  --border: #e3e3e6;
  --accent: #3b82f6;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --warn: #d97706;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --radius: 8px;
  --topbar-h: 44px;
  --sidebar-w: 280px;
  --mark: #fff3a3;
}

.theme-dark {
  --bg: #15171b;
  --bg-elev: #1d2025;
  --bg-elev-2: #262a31;
  --text: #e8e8ea;
  --text-muted: #9aa0aa;
  --border: #2e323a;
  --accent: #5b9bff;
  --accent-text: #0b1118;
  --danger: #f87171;
  --warn: #fbbf24;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  --mark: #5a4a00;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
[hidden] { display: none !important; }  /* restore the standard HTML5 hidden attribute */
body {
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: inherit; }
.muted { color: var(--text-muted); }

/* ---------------- Topbar ---------------- */
#topbar {
  position: fixed; inset: 0 0 auto 0;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}
.brand { font-weight: 600; padding: 0 6px; }
.search-wrap { flex: 1; display: flex; }
#searchInput {
  width: 100%; max-width: 720px; margin: 0 auto;
  height: 30px; padding: 0 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px; outline: none;
}
#searchInput:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }

.icon-btn {
  height: 30px; min-width: 30px; padding: 0 8px;
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 1px solid transparent; border-radius: 6px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-elev-2); }
.icon-btn.danger { color: var(--danger); }

.user-menu { position: relative; }
.menu {
  position: absolute; right: 0; top: 36px;
  min-width: 200px; padding: 6px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.menu-header { padding: 8px 10px; font-weight: 600; }
.menu-item {
  display: block; width: 100%; text-align: left;
  padding: 8px 10px; background: transparent; border: 0; border-radius: 6px; cursor: pointer;
  color: inherit; text-decoration: none;
}
.menu-item:hover { background: var(--bg-elev-2); }

/* ---------------- Auth ---------------- */
#auth {
  display: grid; place-items: center;
  min-height: 100vh; padding: 24px;
  background: var(--bg);
}
.auth-card {
  width: 100%; max-width: 360px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0 0 12px; font-size: 20px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
.tab {
  background: transparent; border: 0; padding: 8px 10px; cursor: pointer;
  border-bottom: 2px solid transparent; color: var(--text-muted);
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-panel { display: none; flex-direction: column; gap: 10px; }
.tab-panel.active { display: flex; }
.tab-panel label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-muted); }
.tab-panel input {
  height: 32px; padding: 0 10px; border-radius: 6px;
  background: var(--bg); border: 1px solid var(--border); outline: none;
}
.tab-panel input:focus { border-color: var(--accent); }
.tab-panel button[type="submit"] {
  height: 34px; border-radius: 6px; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
.google-btn,
.oidc-btn {
  height: 34px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--bg); cursor: pointer; margin-top: 6px;
}
.oidc-buttons { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.form-msg { font-size: 12px; min-height: 1em; }
.form-msg.error { color: var(--danger); }
.form-msg.success { color: var(--warn); }

/* ---------------- App shell ---------------- */
#app {
  position: fixed; inset: var(--topbar-h) 0 0 0;
  display: grid; grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 0;
}
#app.no-sidebar { grid-template-columns: 0 1fr; }
#app.no-sidebar #sidebar { display: none; }

#sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  min-height: 0; overflow: hidden;
}
.sidebar-toolbar {
  display: flex; gap: 4px; align-items: center;
  padding: 8px; border-bottom: 1px solid var(--border);
}
.sidebar-toolbar select {
  flex: 1; height: 28px; padding: 0 6px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
}
.tree { flex: 1; overflow: auto; padding: 6px 4px; }
.tag-list {
  border-top: 1px solid var(--border);
  max-height: 40%; overflow: auto; padding: 6px 4px;
}
.tag-list h4 { margin: 4px 8px; font-size: 11px; text-transform: uppercase; color: var(--text-muted); letter-spacing: .04em; }
.tag {
  display: flex; justify-content: space-between;
  padding: 4px 8px; border-radius: 4px; cursor: pointer;
}
.tag:hover, .tag.active { background: var(--bg-elev-2); }
.tag .count { color: var(--text-muted); font-size: 12px; }

/* tree nodes */
.node { user-select: none; }
.node-row {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 6px; border-radius: 4px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.node-row:hover { background: var(--bg-elev-2); }
.node-row.active { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.node-row .twist { width: 14px; text-align: center; color: var(--text-muted); }
.node-row .icon { width: 16px; text-align: center; }
.node-row .name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.node-row .actions { opacity: 0; gap: 2px; display: flex; }
.node-row:hover .actions { opacity: 1; }
.node-row .actions button {
  background: transparent; border: 0; cursor: pointer; color: var(--text-muted); padding: 0 4px;
}
.node-children { padding-left: 14px; }
.node.search-hit { background: color-mix(in srgb, var(--warn) 14%, transparent); }
mark { background: var(--mark); color: inherit; padding: 0 2px; border-radius: 2px; }

/* ---------------- Note view ---------------- */
.note-view {
  display: flex; flex-direction: column;
  min-height: 0; background: var(--bg);
}
.empty { margin: auto; color: var(--text-muted); }
#noteContainer { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.note-head {
  display: flex; flex-direction: column; gap: 6px;
  padding: 14px 18px 0;
}
.note-title {
  font-size: 22px; font-weight: 700; border: 0; outline: 0; background: transparent; width: 100%;
}
.note-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.note-tags {
  flex: 1; min-width: 200px; height: 28px; padding: 0 10px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 6px; outline: none;
}
.note-tags:focus { border-color: var(--accent); }
.toolbar {
  display: flex; gap: 4px; align-items: center;
  padding: 8px 18px; border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  position: sticky; top: 0; z-index: 2;
}
.toolbar .sep { width: 1px; height: 18px; background: var(--border); margin: 0 4px; }
.toolbar button {
  height: 26px; min-width: 28px; padding: 0 6px;
  background: transparent; border: 1px solid transparent; border-radius: 4px; cursor: pointer;
}
.toolbar button:hover { background: var(--bg-elev-2); }

.editor {
  flex: 1; min-height: 0; resize: none; outline: none; border: 0;
  padding: 16px 22px; background: var(--bg); color: var(--text);
  font: 14px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.preview {
  flex: 1; min-height: 0; overflow: auto;
  padding: 18px 22px;
}

/* Markdown rendered styles */
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4,
.markdown-body h5, .markdown-body h6 { margin: 1.2em 0 .5em; line-height: 1.25; }
.markdown-body h1 { font-size: 1.7em; border-bottom: 1px solid var(--border); padding-bottom: .2em; }
.markdown-body h2 { font-size: 1.4em; border-bottom: 1px solid var(--border); padding-bottom: .2em; }
.markdown-body h3 { font-size: 1.2em; }
.markdown-body p { margin: .6em 0; }
.markdown-body code {
  background: var(--bg-elev-2); padding: 1px 5px; border-radius: 4px; font-size: .92em;
}
.markdown-body pre {
  background: var(--bg-elev-2); padding: 12px 14px; border-radius: 6px; overflow: auto;
}
.markdown-body pre code { background: transparent; padding: 0; }
.markdown-body blockquote {
  border-left: 3px solid var(--border); margin: .6em 0; padding: 0 12px; color: var(--text-muted);
}
.markdown-body ul, .markdown-body ol { padding-left: 1.4em; }
.markdown-body table { border-collapse: collapse; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 4px 8px; }
.markdown-body a { color: var(--accent); }
.markdown-body img { max-width: 100%; height: auto; }
.markdown-body hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }

/* ---------------- Modal / toast ---------------- */
#modalRoot:empty { display: none; }
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: grid; place-items: center; z-index: 50;
}
.modal-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(640px, 92vw);
  max-height: 84vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow);
}
.modal-card header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.modal-card header h2 { margin: 0; font-size: 16px; }
.close-x { background: transparent; border: 0; font-size: 20px; cursor: pointer; }
.modal-card .admin-body { padding: 14px 16px; overflow: auto; }

/* Personal access tokens panel */
.tokens-body { padding: 14px 16px; overflow: auto; display: flex; flex-direction: column; gap: 12px; }
.tokens-body h3 { margin: 4px 0 0; font-size: 14px; }
.tokens-body code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.create-token { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.create-token input { flex: 1; min-width: 180px; height: 30px; padding: 0 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); }
.create-token input:focus { border-color: var(--accent); outline: none; }
.create-token input[type="number"] { flex: 0 0 160px; }
.create-token button { height: 30px; padding: 0 14px; border: 0; border-radius: 6px; background: var(--accent); color: var(--accent-text); cursor: pointer; font-weight: 600; }
.new-token-box { display: flex; flex-direction: column; gap: 8px; padding: 10px 12px; background: color-mix(in srgb, var(--warn) 12%, var(--bg)); border: 1px solid color-mix(in srgb, var(--warn) 35%, var(--border)); border-radius: 6px; }
.new-token-box .warn { margin: 0; color: var(--warn); font-weight: 600; font-size: 13px; }
.token-display { display: flex; gap: 8px; align-items: center; }
.token-display code { flex: 1; padding: 8px 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; word-break: break-all; user-select: all; }
.token-display button { height: 30px; padding: 0 14px; border: 0; border-radius: 6px; background: var(--accent); color: var(--accent-text); cursor: pointer; font-weight: 600; }
.user-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.user-list li {
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px;
}
.user-list .info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.user-list .info .meta { color: var(--text-muted); font-size: 12px; }
.user-list .actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.user-list .actions .sep { width: 1px; height: 18px; background: var(--border); margin: 0 2px; }
.user-list button {
  height: 24px; padding: 0 8px; font-size: 12px;
  border-radius: 4px; border: 1px solid var(--border);
  background: var(--bg); cursor: pointer; white-space: nowrap;
}
.user-list button:hover { background: var(--bg-elev-2); }
.user-list button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.user-list button.primary:hover { filter: brightness(1.08); }
.user-list button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, var(--border)); }
.user-list button.danger:hover { background: color-mix(in srgb, var(--danger) 10%, var(--bg)); }

.toast-root {
  position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100;
}
.toast {
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 12px; box-shadow: var(--shadow);
  max-width: 320px;
}
.toast.error { border-color: var(--danger); }
.toast.success { border-color: var(--accent); }

/* ---------------- Responsive ---------------- */
@media (max-width: 720px) {
  #app { grid-template-columns: 0 1fr; }
  #app.sidebar-open { grid-template-columns: 1fr 0; }
  #app.sidebar-open #sidebar { display: flex; position: fixed; inset: var(--topbar-h) 0 0 0; z-index: 20; width: 100%; }
  #app.sidebar-open #noteView { display: none; }
  .brand { display: none; }
  #searchInput { font-size: 14px; }
}
