/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-primary: #06060b;
  --bg-secondary: #0d0d14;
  --bg-tertiary: #14141e;
  --bg-hover: #1a1a28;
  --bg-active: #222233;
  --border: #1e1e2e;
  --border-light: rgba(255, 255, 255, 0.06);
  --text-primary: #e4e4e8;
  --text-secondary: #8888a0;
  --text-muted: #55556a;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-glow: rgba(59, 130, 246, 0.15);
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #10b981;
  --warning: #f59e0b;
  --radius: 8px;
  --radius-lg: 12px;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  --font-sans: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

:root[data-theme="light"] {
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fb;
  --bg-tertiary: #f0f1f4;
  --bg-hover: #e8eaef;
  --bg-active: #dfe1e8;
  --border: #d8dae0;
  --border-light: rgba(0, 0, 0, 0.06);
  --text-primary: #1a1a2e;
  --text-secondary: #4a4a68;
  --text-muted: #8888a0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-glow: rgba(37, 99, 235, 0.1);
}

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
}

/* ===== Login Page ===== */
.login-page {
  font-family: var(--font-sans);
  background: #050509;
  color: var(--text-primary);
  min-height: 100vh;
  overflow: hidden;
}

/* Split Layout */
.login-split {
  display: flex;
  min-height: 100vh;
}

/* --- Left: Aurora Panel --- */
.login-aurora {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 4rem;
  overflow: hidden;
  background: #050509;
}

.aurora-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
}

.aurora-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(100px);
  will-change: transform;
}

.aurora-orb--1 {
  width: 600px; height: 600px;
  top: -15%; left: -10%;
  background: radial-gradient(circle, rgba(59, 130, 246, 0.18) 0%, transparent 70%);
  animation: auroraShift1 18s ease-in-out infinite;
}

.aurora-orb--2 {
  width: 500px; height: 500px;
  top: 40%; right: -15%;
  background: radial-gradient(circle, rgba(99, 102, 241, 0.14) 0%, transparent 70%);
  animation: auroraShift2 22s ease-in-out infinite;
}

.aurora-orb--3 {
  width: 450px; height: 450px;
  bottom: -10%; left: 20%;
  background: radial-gradient(circle, rgba(14, 165, 233, 0.1) 0%, transparent 70%);
  animation: auroraShift3 20s ease-in-out infinite;
}

.aurora-orb--4 {
  width: 300px; height: 300px;
  top: 20%; left: 40%;
  background: radial-gradient(circle, rgba(139, 92, 246, 0.08) 0%, transparent 70%);
  animation: auroraShift4 24s ease-in-out infinite;
}

@keyframes auroraShift1 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  25% { transform: translate(40px, 30px) scale(1.08); }
  50% { transform: translate(-20px, 60px) scale(0.95); }
  75% { transform: translate(30px, -20px) scale(1.04); }
}

@keyframes auroraShift2 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  33% { transform: translate(-50px, -30px) scale(1.1); }
  66% { transform: translate(20px, 40px) scale(0.92); }
}

@keyframes auroraShift3 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  40% { transform: translate(40px, -40px) scale(1.06); }
  70% { transform: translate(-30px, 20px) scale(0.96); }
}

@keyframes auroraShift4 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(-30px, 30px) scale(1.12); }
}

.aurora-noise {
  position: absolute;
  inset: 0;
  z-index: 1;
  opacity: 0.3;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
}

.aurora-grid {
  position: absolute;
  inset: 0;
  z-index: 1;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.018) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.018) 1px, transparent 1px);
  background-size: 48px 48px;
}

.aurora-content {
  position: relative;
  z-index: 2;
  max-width: 440px;
}

.aurora-content h2 {
  font-size: 3rem;
  font-weight: 800;
  line-height: 1.08;
  letter-spacing: -0.04em;
  margin-bottom: 1.25rem;
  background: linear-gradient(160deg, #ffffff 30%, #64748b 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.aurora-content p {
  font-size: 1rem;
  line-height: 1.75;
  color: var(--text-secondary);
  max-width: 360px;
}

.aurora-features {
  margin-top: 3.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.aurora-feature {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-weight: 400;
}

.aurora-feature-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.6;
  flex-shrink: 0;
}

.aurora-attrib {
  position: absolute;
  bottom: 2.5rem;
  left: 4rem;
  z-index: 2;
  font-size: 0.72rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
  letter-spacing: 0.03em;
}

/* --- Right: Form Panel --- */
.login-form-panel {
  width: 460px;
  min-width: 460px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  position: relative;
  background: #0a0a12;
  border-left: 1px solid var(--border-light);
}

.login-form-panel::before {
  content: '';
  position: absolute;
  top: 25%;
  left: -1px;
  width: 1px;
  height: 50%;
  background: linear-gradient(to bottom, transparent, rgba(59, 130, 246, 0.2), rgba(99, 102, 241, 0.15), transparent);
}

.form-wrapper {
  width: 100%;
  max-width: 340px;
  animation: formEnter 0.7s cubic-bezier(0.16, 1, 0.3, 1) 0.15s both;
}

@keyframes formEnter {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Logo */
.form-logo {
  display: flex;
  justify-content: center;
  margin-bottom: 0.5rem;
}

.form-logo-mark {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 0 40px -8px rgba(59, 130, 246, 0.12);
}

.form-logo-mark img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Form header */
.form-header {
  text-align: center;
  margin-bottom: 2.25rem;
}

.form-header h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text-primary);
  margin-bottom: 0.3rem;
}

.form-header p {
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* Form fields */
.field { margin-bottom: 1.15rem; }

.field label {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.45rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.field-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.field-icon {
  position: absolute;
  left: 0.85rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
  transition: color 0.2s;
}

.field-input-wrap:focus-within .field-icon { color: var(--accent); }

.field input {
  width: 100%;
  padding: 0.75rem 0.9rem 0.75rem 2.7rem;
  background: rgba(255, 255, 255, 0.025);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  color: var(--text-primary);
  font-size: 0.88rem;
  font-family: var(--font-sans);
  outline: none;
  transition: all 0.25s ease;
}

.field input::placeholder { color: var(--text-muted); }

.field input:focus {
  border-color: rgba(59, 130, 246, 0.35);
  background: rgba(255, 255, 255, 0.04);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.06), 0 0 20px -8px rgba(59, 130, 246, 0.1);
}

/* Error & animations */
.error-msg {
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.2);
  color: #f87171;
  padding: 0.6rem 0.85rem;
  border-radius: 10px;
  font-size: 0.8rem;
  margin-bottom: 0.85rem;
  animation: errorIn 0.3s ease-out;
}

@keyframes errorIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 10%,50%,90% { transform: translateX(-4px); } 30%,70% { transform: translateX(4px); } }
.shake { animation: shake 0.5s ease-in-out; }

/* Submit button */
.btn-sign-in {
  width: 100%;
  padding: 0.78rem;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: all 0.25s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  position: relative;
  overflow: hidden;
  margin-top: 0.5rem;
}

.btn-sign-in::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.1), transparent);
  opacity: 0;
  transition: opacity 0.25s;
}

.btn-sign-in:hover:not(:disabled)::before { opacity: 1; }
.btn-sign-in:hover:not(:disabled) { box-shadow: 0 8px 24px -6px rgba(59, 130, 246, 0.35); transform: translateY(-1px); }
.btn-sign-in:active:not(:disabled) { transform: translateY(0); }
.btn-sign-in:disabled { cursor: not-allowed; opacity: 0.8; }
.btn-sign-in.loading { background: linear-gradient(135deg, #2563eb, #1d4ed8); }
.btn-sign-in.success { background: linear-gradient(135deg, #10b981, #059669); }

.btn-spinner { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Footer */
.form-footer {
  display: flex;
  justify-content: center;
  margin-top: 2rem;
  padding-top: 1.25rem;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.form-secure {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.7rem;
  color: var(--text-muted);
}

/* --- Login Responsive --- */
@media (max-width: 900px) {
  .login-page { overflow: auto; }
  .login-split { flex-direction: column; min-height: 100vh; min-height: 100dvh; }

  .login-aurora {
    flex: none;
    padding: 2.5rem 2rem 2rem;
    padding-top: calc(2.5rem + env(safe-area-inset-top, 0px));
    min-height: auto;
  }

  .aurora-content h2 { font-size: 2.1rem; }
  .aurora-content p { font-size: 0.92rem; }
  .aurora-features { display: none; }
  .aurora-attrib { display: none; }

  .login-form-panel {
    width: 100%;
    min-width: unset;
    flex: 1;
    border-left: none;
    border-top: 1px solid var(--border-light);
    padding: 2rem 1.5rem;
    padding-bottom: calc(2rem + env(safe-area-inset-bottom, 0px));
  }

  .login-form-panel::before {
    top: -1px; left: 15%; width: 70%; height: 1px;
    background: linear-gradient(to right, transparent, rgba(59, 130, 246, 0.2), transparent);
  }

  .form-wrapper { max-width: 400px; }
}

@media (max-width: 480px) {
  .login-aurora { padding: 1.75rem 1.25rem 1.25rem; }
  .aurora-content h2 { font-size: 1.6rem; margin-bottom: 0.75rem; }
  .aurora-content p { font-size: 0.85rem; line-height: 1.6; }

  .login-form-panel { padding: 1.5rem 1.25rem; }
  .form-logo-mark { width: 48px; height: 48px; border-radius: 12px; }
  .form-header { margin-bottom: 1.75rem; }
  .form-header h1 { font-size: 1.25rem; }

  /* Prevent iOS zoom on input focus */
  .field input { font-size: 16px; }
  .btn-sign-in { font-size: 16px; padding: 0.85rem; }
}

@media (max-width: 360px) {
  .login-aurora { padding: 1.5rem 1rem 1rem; }
  .aurora-content h2 { font-size: 1.4rem; }
  .login-form-panel { padding: 1.25rem 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  .aurora-orb, .form-wrapper { animation: none; }
  .form-wrapper { opacity: 1; transform: none; }
  .error-msg, .shake { animation: none; }
  .btn-spinner { animation: none; opacity: 0.6; }
}

/* ===== App Layout ===== */
.app-layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* --- Sidebar --- */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-primary);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.25rem 0.4rem;
  margin: -0.25rem -0.4rem;
  border-radius: var(--radius);
  font-family: var(--font-sans);
  transition: background 0.15s;
}

.sidebar-brand:hover { background: var(--bg-hover); }

.sidebar-logo {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  object-fit: cover;
}

.sidebar-brand h2 {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.sidebar-search {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  margin: 0.5rem 0.6rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  transition: border-color 0.2s;
}

.sidebar-search:focus-within {
  border-color: rgba(59, 130, 246, 0.4);
}

.sidebar-search input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.82rem;
  outline: none;
  min-width: 0;
}

.sidebar-search input::placeholder { color: var(--text-muted); }

.note-tree {
  flex: 1;
  overflow-y: auto;
  padding: 0.4rem 0;
}

.folder {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem 0.25rem;
  color: var(--text-muted);
}

.folder-name {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.note-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 1rem 0.45rem 1rem;
  font-size: 0.84rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  border-left: 3px solid transparent;
}

.note-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.note-item.active {
  background: var(--bg-active);
  color: var(--text-primary);
  border-left-color: var(--accent);
}

.note-item svg {
  flex-shrink: 0;
  opacity: 0.4;
}

.note-item.active svg { opacity: 0.7; }

.note-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-footer {
  padding: 0.7rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.user-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.user-label {
  font-size: 0.8rem;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- Main content --- */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  background: var(--bg-primary);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  min-height: 50px;
  gap: 0.75rem;
  flex-shrink: 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  flex: 1;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

.sidebar-toggle { display: none; }

.note-title-display {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.note-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* --- Buttons --- */
.btn-primary {
  padding: 0.78rem;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: all 0.25s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.btn-primary:hover:not(:disabled) { box-shadow: 0 8px 24px -6px rgba(59, 130, 246, 0.35); }
.btn-primary:disabled { cursor: not-allowed; opacity: 0.8; }

.btn-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
}

.btn-icon:hover { background: var(--bg-hover); border-color: var(--border-light); }

.btn-icon--subtle {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}

.btn-icon--subtle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-action {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.65rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.76rem;
  font-family: var(--font-sans);
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn-action:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: rgba(255,255,255,0.08);
}

.btn-action--danger { color: var(--danger); }
.btn-action--danger:hover { background: rgba(239,68,68,0.1); color: var(--danger); }

/* Save status */
.save-status {
  font-size: 0.72rem;
  font-weight: 500;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  white-space: nowrap;
}

.save-status.saved { color: var(--success); }
.save-status.saving { color: var(--accent); }
.save-status.unsaved { color: var(--warning); background: rgba(245,158,11,0.08); }
.save-status.error { color: var(--danger); background: rgba(239,68,68,0.08); }

/* --- Welcome --- */
.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  gap: 0.5rem;
  padding: 2rem;
  overflow-y: auto;
}

.welcome-logo {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  overflow: hidden;
  margin-bottom: 0.25rem;
}

.welcome-logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.welcome h2 { font-size: 1.25rem; color: var(--text-primary); font-weight: 700; letter-spacing: -0.02em; }
.welcome > p { font-size: 0.85rem; color: var(--text-secondary); margin-bottom: 0.5rem; }

.welcome-sections {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-top: 1rem;
  max-width: 720px;
  width: 100%;
}

.welcome-section {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
}

.welcome-section h3 {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-primary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.85rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--border);
}

.welcome-section h3 svg { color: var(--accent); flex-shrink: 0; }

.welcome-hints {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.welcome-hint {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.welcome-hint kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.2rem 0.5rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--text-primary);
  white-space: nowrap;
  min-width: 52px;
  text-align: center;
  line-height: 1.4;
}

.welcome-tip {
  margin-top: 1.5rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.welcome-tip strong { color: var(--text-secondary); }

/* --- Editor --- */
.editor-panel {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.editor-container {
  flex: 1;
  overflow: auto;
}

/* Reading panel */
.reading-panel {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* CodeMirror overrides */
.editor-container .cm-editor {
  height: 100%;
  font-size: 0.88rem;
  background: var(--bg-primary);
}

.editor-container .cm-editor .cm-scroller {
  font-family: var(--font-mono);
  line-height: 1.7;
}

.editor-container .cm-editor .cm-content {
  padding: 1.5rem 2rem;
  max-width: 900px;
}

.editor-container .cm-editor .cm-gutters {
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  color: var(--text-muted);
}

.editor-container .cm-editor .cm-activeLineGutter {
  background: var(--bg-tertiary);
}

.editor-container .cm-editor .cm-activeLine {
  background: rgba(255,255,255,0.02);
}

.editor-container .cm-editor .cm-selectionBackground {
  background: rgba(59,130,246,0.2) !important;
}

.editor-container .cm-editor.cm-focused .cm-cursor {
  border-left-color: var(--accent);
}

/* --- Preview --- */
.preview-container {
  flex: 1;
  padding: 2rem 2.5rem;
  overflow-y: auto;
  scroll-padding-top: 1rem;
}

.preview-container > * {
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}

.preview-container h1, .preview-container h2, .preview-container h3,
.preview-container h4, .preview-container h5, .preview-container h6 {
  margin: 1.5em 0 0.5em;
  color: var(--text-primary);
  font-family: var(--font-sans);
}

.preview-container h1 { font-size: 1.8rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
.preview-container h2 { font-size: 1.4rem; }
.preview-container h3 { font-size: 1.15rem; }

.preview-container p { margin: 0.8em 0; color: var(--text-secondary); line-height: 1.75; }
.preview-container a { color: var(--accent); text-decoration: none; }
.preview-container a:hover { text-decoration: underline; }
.preview-container strong { color: var(--text-primary); }

.preview-container code {
  background: var(--bg-tertiary);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.85em;
  color: #e2b3ff;
}

.preview-container pre {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  overflow-x: auto;
  margin: 1.2em 0;
}

.preview-container pre code {
  background: none;
  padding: 0;
  color: var(--text-primary);
}

.preview-container blockquote {
  border-left: 3px solid var(--accent);
  padding: 0.5rem 1rem;
  margin: 1em 0;
  color: var(--text-secondary);
  background: rgba(59,130,246,0.04);
  border-radius: 0 var(--radius) var(--radius) 0;
}

.preview-container ul, .preview-container ol { padding-left: 1.5rem; margin: 0.8em 0; }
.preview-container li { margin: 0.3em 0; color: var(--text-secondary); }
.preview-container li::marker { color: var(--text-muted); }

.preview-container img { max-width: 100%; border-radius: var(--radius); }

.preview-container mark {
  background: rgba(245, 158, 11, 0.25);
  color: var(--text-primary);
  padding: 0.1em 0.2em;
  border-radius: 3px;
}

.preview-container .wiki-link {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dashed var(--accent);
  padding-bottom: 1px;
}
.preview-container .wiki-link:hover { border-bottom-style: solid; }

.preview-container table { width: 100%; border-collapse: collapse; margin: 1em 0; }
.preview-container th, .preview-container td { border: 1px solid var(--border); padding: 0.5rem 0.8rem; text-align: left; }
.preview-container th { background: var(--bg-tertiary); font-weight: 600; color: var(--text-primary); }
.preview-container td { color: var(--text-secondary); }

.preview-container hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2em 0;
}

/* Callouts (Obsidian-style) */
.callout {
  border-radius: var(--radius);
  padding: 0.8rem 1rem;
  margin: 1em 0;
  border-left: 3px solid var(--accent);
  background: rgba(59,130,246,0.06);
}

.callout-title {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--accent);
  margin-bottom: 0.3rem;
}

.callout-body p { margin: 0.3em 0; }

.callout-warning { border-left-color: var(--warning); background: rgba(245,158,11,0.06); }
.callout-warning .callout-title { color: var(--warning); }
.callout-danger, .callout-error { border-left-color: var(--danger); background: rgba(239,68,68,0.06); }
.callout-danger .callout-title, .callout-error .callout-title { color: var(--danger); }
.callout-tip, .callout-success { border-left-color: var(--success); background: rgba(16,185,129,0.06); }
.callout-tip .callout-title, .callout-success .callout-title { color: var(--success); }

/* --- Changes panel --- */
.changes-panel, .permissions-panel {
  position: absolute;
  top: 50px;
  right: 0;
  bottom: 0;
  width: 340px;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  z-index: 10;
  box-shadow: -4px 0 16px rgba(0,0,0,0.2);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-secondary);
  z-index: 1;
}

.panel-header h3 {
  font-size: 0.85rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.changes-list {
  padding: 0.25rem 0;
}

.changes-empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.changes-date-header {
  padding: 0.5rem 1rem;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 1;
}

.change-item {
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.change-item:hover { background: var(--bg-hover); }

.change-item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.25rem;
}

.change-user {
  font-size: 0.82rem;
  font-weight: 600;
}

.change-time {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.change-stats {
  display: flex;
  gap: 0.5rem;
  font-size: 0.72rem;
  font-family: var(--font-mono);
}

.stat-add { color: var(--success); }
.stat-remove { color: var(--danger); }
.stat-none { color: var(--text-muted); }

/* --- Diff viewer --- */
.diff-viewer {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.diff-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 1rem;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
  font-size: 0.82rem;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.diff-actions {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.diff-content {
  flex: 1;
  overflow: auto;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.6;
}

.diff-line {
  display: flex;
  padding: 1px 0;
  min-height: 1.6em;
}

.diff-line-num {
  width: 48px;
  text-align: right;
  padding-right: 12px;
  color: var(--text-muted);
  user-select: none;
  flex-shrink: 0;
}

.diff-prefix {
  width: 20px;
  text-align: center;
  flex-shrink: 0;
  user-select: none;
  font-weight: 700;
}

.diff-text {
  flex: 1;
  padding-right: 1rem;
  white-space: pre-wrap;
  word-break: break-all;
}

.diff-same { color: var(--text-secondary); }
.diff-same .diff-prefix { color: transparent; }

.diff-add {
  background: rgba(16, 185, 129, 0.08);
  color: #6ee7b7;
}
.diff-add .diff-prefix { color: var(--success); }
.diff-add .diff-line-num { color: var(--success); opacity: 0.6; }

.diff-remove {
  background: rgba(239, 68, 68, 0.08);
  color: #fca5a5;
}
.diff-remove .diff-prefix { color: var(--danger); }
.diff-remove .diff-line-num { color: var(--danger); opacity: 0.6; }

/* --- Permissions panel --- */
.perm-user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  gap: 0.5rem;
}

.perm-name { font-size: 0.85rem; flex: 1; min-width: 0; }

.perm-user-row select, .perm-add select {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  padding: 0.25rem 0.5rem;
  font-size: 0.78rem;
  font-family: var(--font-sans);
}

.btn-remove {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 0.78rem;
  font-family: var(--font-sans);
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  transition: background 0.15s;
}
.btn-remove:hover { background: rgba(239,68,68,0.1); }

.perm-add {
  padding: 0.75rem 1rem;
  display: flex;
  gap: 0.4rem;
  align-items: center;
  flex-wrap: wrap;
}

.perm-add select { flex: 1; min-width: 80px; }

.perm-add button {
  padding: 0.3rem 0.7rem;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: white;
  font-size: 0.78rem;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: background 0.15s;
}
.perm-add button:hover { background: var(--accent-hover); }

/* --- Editable preview mode --- */
.preview-container[contenteditable="true"] {
  outline: none;
  cursor: text;
}

.preview-container[contenteditable="true"] td,
.preview-container[contenteditable="true"] th {
  cursor: text;
}

.preview-container[contenteditable="true"] a {
  cursor: text;
  pointer-events: auto;
}

/* --- Light mode overrides --- */
:root[data-theme="light"] .cm-editor { background: var(--bg-primary); }
:root[data-theme="light"] .cm-editor .cm-gutters { background: var(--bg-secondary); border-color: var(--border); }
:root[data-theme="light"] .preview-container code { color: #7c3aed; }
:root[data-theme="light"] .diff-add { background: rgba(16, 185, 129, 0.1); color: #065f46; }
:root[data-theme="light"] .diff-remove { background: rgba(239, 68, 68, 0.1); color: #991b1b; }
:root[data-theme="light"] .login-page { background: #f0f1f4; }

/* --- Profile styles --- */
.user-info-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  padding: 0.3rem 0.4rem;
  border-radius: var(--radius);
  transition: background 0.15s;
  min-width: 0;
  flex: 1;
  font-family: var(--font-sans);
}

.user-info-btn:hover { background: var(--bg-hover); }

.user-chevron {
  color: var(--text-muted);
  flex-shrink: 0;
  margin-left: auto;
}

.profile-card {
  max-width: 360px;
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.profile-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.profile-info {
  flex: 1;
  min-width: 0;
}

.profile-info h3 {
  font-size: 0.95rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.profile-role {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: var(--accent-glow);
  padding: 0.15rem 0.5rem;
  border-radius: 10px;
  margin-top: 0.2rem;
  line-height: 1.4;
}

.profile-section {
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.profile-section:last-child { border-bottom: none; }

.profile-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0;
}

.profile-label {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.profile-value {
  font-size: 0.82rem;
  color: var(--text-primary);
  font-weight: 500;
}

.profile-action {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.6rem;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.82rem;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: all 0.15s;
}

.profile-action:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.profile-action--danger { color: var(--danger); border-color: rgba(239, 68, 68, 0.2); }
.profile-action--danger:hover { background: rgba(239, 68, 68, 0.08); color: var(--danger); }

.profile-footer { padding-top: 0.5rem; }

.password-section {
  padding: 1rem 1.25rem;
}

.password-section .modal-field {
  margin-bottom: 0.85rem;
}

.password-section .modal-field:last-of-type {
  margin-bottom: 0.5rem;
}

.password-section .modal-field label {
  font-size: 0.72rem;
  margin-bottom: 0.3rem;
}

.password-section .modal-field input {
  padding: 0.55rem 0.7rem;
  font-size: 0.82rem;
}

.password-section .btn-primary {
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.6rem;
  font-size: 0.82rem;
}

.password-section .modal-error,
.password-section .modal-success {
  margin-bottom: 0.5rem;
  font-size: 0.78rem;
}

/* Theme toggle */
.theme-toggle {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0.6rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.78rem;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: all 0.15s;
}

.theme-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* --- Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 50px -12px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 0.95rem;
  font-weight: 600;
}

.modal-body {
  padding: 1.25rem;
}

.modal-field {
  margin-bottom: 0.9rem;
}

.modal-field label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.modal-field input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-family: var(--font-sans);
  outline: none;
  transition: border-color 0.2s;
}

.modal-field input:focus {
  border-color: rgba(59, 130, 246, 0.5);
}

.modal-error {
  padding: 0.5rem 0.75rem;
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: var(--radius);
  color: #f87171;
  font-size: 0.8rem;
}

.modal-success {
  padding: 0.5rem 0.75rem;
  background: rgba(16, 185, 129, 0.08);
  border: 1px solid rgba(16, 185, 129, 0.2);
  border-radius: var(--radius);
  color: #6ee7b7;
  font-size: 0.8rem;
}

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* --- Responsive --- */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
    box-shadow: 4px 0 16px rgba(0,0,0,0.3);
  }

  .sidebar--open { transform: translateX(0); z-index: 101; }

  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 100;
  }

  .sidebar-backdrop.active {
    display: block;
  }

  .sidebar-toggle { display: flex; }

  .topbar { padding: 0 0.6rem; min-height: 44px; }

  .topbar-right { gap: 0.2rem; }

  .btn-action {
    padding: 0.25rem 0.45rem;
    font-size: 0.72rem;
  }

  .btn-action span:not(.btn-preview-label) { display: none; }

  .note-title-display { font-size: 0.82rem; }

  .changes-panel, .permissions-panel { width: 100%; }

  .note-meta { display: none; }

  /* Preview / reading content */
  .preview-container {
    padding: 1.25rem 1rem;
  }

  .preview-container h1 { font-size: 1.4rem; }
  .preview-container h2 { font-size: 1.15rem; }
  .preview-container h3 { font-size: 1rem; }

  .preview-container table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 0.8rem;
  }

  .preview-container th,
  .preview-container td {
    padding: 0.35rem 0.5rem;
    white-space: nowrap;
  }

  /* Editor */
  .editor-container .cm-editor .cm-content {
    padding: 1rem;
  }

  /* Welcome */
  .welcome { padding: 1.5rem 1rem; justify-content: flex-start; padding-top: 2rem; }
  .welcome-sections { grid-template-columns: 1fr; max-width: 400px; }
  .welcome-section { padding: 1rem; }

  /* Diff viewer */
  .diff-header { padding: 0.5rem 0.7rem; font-size: 0.75rem; }
  .diff-line-num { width: 36px; padding-right: 8px; }
  .diff-content { font-size: 0.75rem; }

  /* Modal / Profile */
  .modal-card, .profile-card {
    max-width: calc(100vw - 2rem);
    margin: 0 1rem;
  }

  .profile-header { padding: 1rem; }
  .profile-section { padding: 0.65rem 1rem; }
  .password-section { padding: 0.85rem 1rem; }
}

@media (max-width: 380px) {
  .btn-action .btn-preview-label { display: none; }

  .topbar-right .btn-action svg { width: 14px; height: 14px; }

  .preview-container { padding: 1rem 0.75rem; }
}
