:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #eef2f7;
  --text: #111827;
  --muted: #64748b;
  --border: #dce3ec;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #dbeafe;
  --danger: #b42318;
  --danger-soft: #fee4e2;
  --success: #067647;
  --warning: #b54708;
  --shadow: 0 18px 50px rgba(15, 23, 42, 0.09);
  --topbar-height: 68px;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #172033;
  --surface-3: #202b40;
  --text: #f8fafc;
  --muted: #94a3b8;
  --border: #2b374c;
  --accent: #60a5fa;
  --accent-hover: #93c5fd;
  --accent-soft: #172554;
  --danger: #fda29b;
  --danger-soft: #4a1d1f;
  --success: #6ce9a6;
  --warning: #fec84b;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

/* Always honor the HTML hidden attribute, even on elements with display:grid. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.6; }
code {
  padding: 0.15rem 0.35rem;
  border-radius: 0.35rem;
  background: var(--surface-3);
  font-family: "SFMono-Regular", Consolas, monospace;
}

.app-shell {
  height: 100%;
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  grid-template-rows: var(--topbar-height) minmax(0, 1fr);
}

.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: inherit;
  text-decoration: none;
}
.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, var(--accent), #7c3aed);
  color: white;
  font-weight: 800;
  box-shadow: 0 7px 18px rgba(37, 99, 235, 0.25);
}
.brand strong, .brand small { display: block; }
.brand strong { line-height: 1.1; }
.brand small { margin-top: 3px; color: var(--muted); font-size: 0.75rem; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.icon-button {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 11px;
  color: var(--text);
  background: var(--surface-2);
}
.icon-button:hover { background: var(--surface-3); }
.connection-pill {
  padding: 7px 11px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}
.connection-pill.online { color: var(--success); }
.connection-pill.offline { color: var(--warning); }

.sidebar {
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 18px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 15;
}
.sidebar-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.primary-button, .secondary-button, .danger-button {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 700;
}
.primary-button { background: var(--accent); color: white; }
.primary-button:hover { background: var(--accent-hover); }
.secondary-button { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.secondary-button:hover { background: var(--surface-3); }
.danger-button { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.compact { padding: 7px 10px; font-size: 0.82rem; }
.text-button { border: 0; padding: 2px; background: transparent; color: var(--accent); font-weight: 700; }
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface-2);
}
.search-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-box input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text); }
.search-box button { border: 0; background: transparent; color: var(--muted); font-size: 1.1rem; }
.folder-filter-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.82rem;
}
.folder-filter-row select {
  min-width: 0;
  flex: 1;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--text);
}
.notes-summary { display: flex; justify-content: space-between; margin: 16px 2px 8px; color: var(--muted); font-size: 0.78rem; }
.notes-list { min-height: 0; overflow-y: auto; padding-right: 3px; }
.note-item {
  width: 100%;
  display: block;
  text-align: left;
  padding: 11px;
  margin-bottom: 6px;
  border: 1px solid transparent;
  border-radius: 11px;
  background: transparent;
  color: var(--text);
}
.note-item:hover { background: var(--surface-2); }
.note-item.active { border-color: var(--accent); background: var(--accent-soft); }
.note-item-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 720; }
.note-item-meta, .note-item-snippet { display: block; margin-top: 5px; color: var(--muted); font-size: 0.73rem; }
.note-item-snippet { line-height: 1.35; overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.list-message { padding: 20px 8px; text-align: center; color: var(--muted); font-size: 0.86rem; }

.workspace { min-width: 0; min-height: 0; padding: 24px; overflow: hidden; }
.empty-state {
  height: 100%;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
  padding: 24px;
}
.empty-state h1 { margin: 18px 0 8px; font-size: clamp(1.7rem, 4vw, 2.6rem); }
.empty-state p { max-width: 590px; margin: 0 0 24px; color: var(--muted); line-height: 1.65; }
.empty-icon {
  width: 88px;
  height: 88px;
  display: grid;
  place-items: center;
  border-radius: 26px;
  background: linear-gradient(145deg, var(--accent-soft), var(--surface));
  border: 1px solid var(--border);
  color: var(--accent);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-weight: 900;
  box-shadow: var(--shadow);
}
.editor-panel {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 15px 18px;
  border-bottom: 1px solid var(--border);
}
.editor-title-group { min-width: 0; }
.editor-title-group input {
  width: min(600px, 100%);
  padding: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.15rem;
  font-weight: 780;
}
.note-path { display: block; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; color: var(--muted); font-size: 0.75rem; white-space: nowrap; }
.editor-actions { display: flex; flex-shrink: 0; gap: 7px; }
.editor-statusbar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 18px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.75rem;
}
#saveStatus.saving { color: var(--accent); }
#saveStatus.saved { color: var(--success); }
#saveStatus.error { color: var(--danger); }
#editor {
  width: 100%;
  height: 100%;
  resize: none;
  border: 0;
  outline: 0;
  padding: clamp(18px, 3vw, 34px);
  background: var(--surface);
  color: var(--text);
  font: 16px/1.7 "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  tab-size: 2;
}

.app-dialog {
  width: min(520px, calc(100% - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.app-dialog::backdrop { background: rgba(15, 23, 42, 0.55); backdrop-filter: blur(4px); }
.app-dialog form { padding: 22px; }
.app-dialog h2 { margin: 0 0 8px; }
.app-dialog p { color: var(--muted); line-height: 1.5; }
.app-dialog label { display: grid; gap: 7px; margin-top: 18px; font-size: 0.85rem; font-weight: 700; }
.app-dialog input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  outline: 0;
}
.app-dialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field-error { min-height: 1.25rem; margin: 6px 0 0 !important; color: var(--danger) !important; font-size: 0.78rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 22px; }
.toast-region { position: fixed; right: 20px; bottom: 20px; z-index: 100; display: grid; gap: 8px; }
.toast {
  max-width: 390px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  animation: toast-in 180ms ease-out;
}
.toast.error { border-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

.mobile-only { display: none; }
.sidebar-backdrop { position: fixed; inset: var(--topbar-height) 0 0; background: rgba(15, 23, 42, 0.5); z-index: 12; }

@media (max-width: 820px) {
  .mobile-only { display: inline-grid; place-items: center; }
  .app-shell { grid-template-columns: minmax(0, 1fr); }
  .topbar { padding: 0 12px; }
  .brand { margin-right: auto; margin-left: 9px; }
  .brand small { display: none; }
  .connection-pill { display: none; }
  .sidebar {
    position: fixed;
    top: var(--topbar-height);
    bottom: 0;
    left: 0;
    width: min(88vw, 350px);
    transform: translateX(-105%);
    transition: transform 180ms ease-out;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .workspace { grid-column: 1; padding: 10px; }
  .editor-panel { border-radius: 13px; }
  .editor-header { align-items: flex-start; flex-direction: column; padding: 12px; }
  .editor-actions { width: 100%; }
  .editor-actions button { flex: 1; }
  .editor-statusbar { padding: 7px 12px; }
  #editor { padding: 16px; font-size: 16px; }
}

@media (max-width: 480px) {
  .sidebar-actions { grid-template-columns: 1fr; }
  .editor-statusbar { align-items: flex-start; flex-direction: column; gap: 3px; }
  .stacked-mobile { flex-direction: column; }
  .stacked-mobile button { width: 100%; }
}

.logout-button { font-size: 1.05rem; }
.auth-page {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  overflow: auto;
  background:
    radial-gradient(circle at top left, var(--accent-soft), transparent 38%),
    var(--bg);
}
.auth-card {
  width: min(460px, 100%);
  padding: clamp(24px, 5vw, 38px);
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.auth-brand { width: fit-content; margin-bottom: 28px; }
.auth-card h1 { margin: 0 0 10px; font-size: clamp(1.7rem, 5vw, 2.35rem); }
.auth-card > p { color: var(--muted); line-height: 1.6; }
.auth-form { display: grid; gap: 16px; margin-top: 24px; }
.auth-form label { display: grid; gap: 8px; font-size: 0.86rem; font-weight: 750; }
.auth-form input {
  width: 100%;
  padding: 12px 13px;
  border: 1px solid var(--border);
  border-radius: 11px;
  outline: none;
  background: var(--surface-2);
  color: var(--text);
}
.auth-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.auth-form .primary-button { margin-top: 4px; padding-block: 12px; }
.auth-error {
  margin-top: 18px;
  padding: 11px 13px;
  border-radius: 10px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.86rem;
  line-height: 1.45;
}
.auth-footnote { margin: 22px 0 0 !important; font-size: 0.79rem; }
