:root,
[data-theme="light"] {
  --bg-primary: #ffffff;
  --bg-secondary: #f5f7fa;
  --bg-card: #ffffff;
  --bg-sidebar: #f9fafb;
  --bg-navbar: #ffffff;
  --bg-hover: #f0f2f5;
  --bg-active: #e8f0fe;
  --bg-overlay: rgba(0, 0, 0, 0.4);
  --bg-toast: #323232;

  --text-primary: #1a1a2e;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --text-inverse: #ffffff;

  --border-color: #e5e7eb;
  --border-light: #f0f0f0;

  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-light: #eef2ff;
  --accent-bg: #e8f0fe;

  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #22c55e;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.05);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --sidebar-width: 220px;
  --navbar-height: 56px;

  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

[data-theme="dark"] {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-card: #1e293b;
  --bg-sidebar: #1e293b;
  --bg-navbar: #0f172a;
  --bg-hover: #334155;
  --bg-active: #1e3a5f;
  --bg-overlay: rgba(0, 0, 0, 0.6);
  --bg-toast: #e0e0e0;

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --text-inverse: #0f172a;

  --border-color: #334155;
  --border-light: #1e293b;

  --accent: #818cf8;
  --accent-hover: #6366f1;
  --accent-light: #1e1b4b;
  --accent-bg: #1e3a5f;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -2px rgba(0, 0, 0, 0.2);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -4px rgba(0, 0, 0, 0.3);
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }

button {
  border: none;
  background: none;
  cursor: pointer;
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

input {
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-family: inherit;
  font-size: 0.875rem;
  background: var(--bg-primary);
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.2s;
}

input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

::selection {
  background: var(--accent);
  color: var(--text-inverse);
}

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

/* Utility */
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: 4px; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.gap-4 { gap: 16px; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hidden { display: none !important; }

/* Toast */
#toast-container {
  position: fixed;
  top: 70px;
  right: 20px;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: var(--bg-toast);
  color: var(--text-inverse);
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.3s ease;
  max-width: 360px;
}

.toast.error { background: var(--danger); color: #fff; }
.toast.success { background: var(--success); color: #fff; }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(40px); }
  to { opacity: 1; transform: translateX(0); }
}

/* ===== Navbar ===== */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--navbar-height);
  background: var(--bg-navbar);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 1000;
  gap: 16px;
}

.navbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.navbar-center {
  flex: 1;
  max-width: 400px;
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-primary);
  font-weight: 700;
  font-size: 1.125rem;
  text-decoration: none;
}

.logo svg { color: var(--accent); }

.icon-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  transition: all 0.15s;
}

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

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0 12px;
  transition: all 0.2s;
}

.search-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.search-box svg { color: var(--text-muted); flex-shrink: 0; }

.search-box input {
  border: none;
  background: none;
  padding: 8px 0;
  width: 100%;
  font-size: 0.875rem;
}

.search-box input:focus {
  box-shadow: none;
  border-color: transparent;
}

.theme-btn .icon-moon { display: none; }
[data-theme="dark"] .theme-btn .icon-sun { display: none; }
[data-theme="dark"] .theme-btn .icon-moon { display: block; }

.menu-btn {
  display: none;
}

/* ===== Sidebar ===== */
.sidebar {
  position: fixed;
  top: var(--navbar-height);
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  z-index: 900;
  transition: transform 0.3s ease;
}

.sidebar-content {
  padding: 16px 0;
}

.sidebar-section {
  margin-bottom: 16px;
}

.sidebar-title {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  padding: 0 20px 8px;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 20px;
  font-size: 0.875rem;
  color: var(--text-secondary);
  text-align: left;
  transition: all 0.15s;
  border: none;
  background: none;
  cursor: pointer;
}

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

.sidebar-item.active {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: 899;
}

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

/* ===== Main Content ===== */
.main-content {
  margin-top: var(--navbar-height);
  margin-left: var(--sidebar-width);
  height: calc(100vh - var(--navbar-height));
  overflow-y: auto;
  transition: margin-left 0.3s ease;
}

/* Reader mode: hide sidebar, full-width content */
body.reader-mode .sidebar {
  display: none;
}

body.reader-mode .main-content {
  margin-left: 0;
}

/* Reader mode: simplify navbar */
body.reader-mode .navbar-center {
  display: none;
}

body.reader-mode .menu-btn {
  display: none !important;
}

/* ===== Responsive ===== */
@media (max-width: 767px) {
  .menu-btn { display: flex; }

  .sidebar {
    transform: translateX(-100%);
    z-index: 901;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .main-content {
    margin-left: 0;
  }

  .navbar-center {
    display: none;
  }

  .navbar {
    padding: 0 12px;
  }

  /* Reader mode on mobile: ensure back btn visible */
  body.reader-mode .menu-btn {
    display: none !important;
  }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .menu-btn { display: flex; }

  .main-content {
    margin-left: 0;
  }
}

/* ===== Login Page ===== */
.login-page, .chpwd-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg-secondary);
  padding: 24px;
}

.login-card, .chpwd-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 40px 32px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.login-logo, .chpwd-icon {
  margin-bottom: 16px;
}

.login-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 4px;
  color: var(--text-primary);
}

.login-subtitle {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 32px;
}

.chpwd-title {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text-primary);
}

.chpwd-notice {
  background: #fef3c7;
  color: #92400e;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  margin-bottom: 20px;
  text-align: left;
}

[data-theme="dark"] .chpwd-notice {
  background: #451a03;
  color: #fcd34d;
}

.login-form, .chpwd-form {
  text-align: left;
}

.login-field, .chpwd-field {
  margin-bottom: 16px;
}

.login-field label, .chpwd-field label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.login-field input, .chpwd-field input {
  width: 100%;
  padding: 10px 14px;
  font-size: 0.9375rem;
  border-radius: var(--radius-sm);
}

.login-error, .chpwd-error {
  color: var(--danger);
  font-size: 0.8125rem;
  min-height: 20px;
  margin-bottom: 8px;
}

.login-submit, .chpwd-submit {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.login-submit:hover, .chpwd-submit:hover {
  background: var(--accent-hover);
}

.login-submit:disabled, .chpwd-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.chpwd-cancel {
  display: block;
  text-align: center;
  margin-top: 16px;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.chpwd-cancel:hover {
  color: var(--accent);
}

/* ===== User menu in navbar ===== */
.user-menu-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

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

.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  min-width: 160px;
  z-index: 1100;
  overflow: hidden;
}

.user-dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 16px;
  font-size: 0.8125rem;
  color: var(--text-primary);
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
}

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

.user-dropdown-item.danger {
  color: var(--danger);
}
