/* ============ Theme Toggle Button ============ */
.theme-toggle {
  position: relative;
  width: 44px;
  height: 24px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  padding: 0;
  transition: background .3s, box-shadow .3s;
  flex-shrink: 0;
  outline: none;
}
.theme-toggle .toggle-track {
  width: 100%;
  height: 100%;
  border-radius: 12px;
  position: relative;
  overflow: hidden;
  transition: background .3s;
}
.theme-toggle .toggle-thumb {
  position: absolute;
  top: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  transition: left .3s, background .3s;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  line-height: 1;
}

/* Dark mode (default) */
[data-theme="dark"] .theme-toggle .toggle-track {
  background: #2a2a4a;
  border: 1px solid rgba(255,255,255,.12);
}
[data-theme="dark"] .theme-toggle .toggle-thumb {
  left: 2px;
  background: #3a3a5a;
}

/* Light mode */
[data-theme="light"] .theme-toggle .toggle-track {
  background: #e0e7ef;
  border: 1px solid rgba(0,0,0,.1);
}
[data-theme="light"] .theme-toggle .toggle-thumb {
  left: 22px;
  background: #fbbf24;
}

/* ============ Light Theme CSS Variables Override ============ */
[data-theme="light"] {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface2: #eef0f6;
  --border: rgba(0,0,0,.06);
  --border2: rgba(0,0,0,.12);
  --text: #1f2937;
  --text2: #6b7280;
  --accent: #3b6fd4;
  --accent2: #2d5ab8;
  --accent-bg: rgba(59,111,212,.1);
  --green: #16a34a;
  --red: #dc2626;
  --yellow: #ca8a04;
}

/* ============ Light Theme - General Overrides ============ */
[data-theme="light"] body {
  background: var(--bg);
  color: var(--text);
}

[data-theme="light"] ::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,.15);
}
[data-theme="light"] ::-webkit-scrollbar-thumb:hover {
  background: rgba(0,0,0,.25);
}

/* Header */
[data-theme="light"] .header {
  background: var(--surface);
  border-bottom-color: var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
[data-theme="light"] .header h1 {
  color: var(--text2);
}
[data-theme="light"] .header h1 span {
  color: var(--accent);
}

/* Search */
[data-theme="light"] .search-box input {
  background: var(--surface2);
  border-color: var(--border2);
  color: var(--text);
}
[data-theme="light"] .search-box input::placeholder {
  color: var(--text2);
}
[data-theme="light"] .search-results {
  background: var(--surface);
  border-color: var(--border2);
  box-shadow: 0 8px 24px rgba(0,0,0,.1);
}
[data-theme="light"] .search-item {
  border-bottom-color: var(--border);
}
[data-theme="light"] .search-item:hover {
  background: var(--surface2);
}

/* Chat */
[data-theme="light"] .left-panel {
  background: var(--bg);
  border-right-color: var(--border);
}
[data-theme="light"] .chat-msg.ai .avatar {
  background: var(--surface2);
  color: var(--text2);
}
[data-theme="light"] .chat-msg.user .avatar {
  background: var(--accent);
  color: #fff;
}
[data-theme="light"] .chat-msg.ai .bubble {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
[data-theme="light"] .chat-msg.user .bubble {
  background: var(--accent);
  color: #fff;
}
[data-theme="light"] .chat-msg .bubble pre {
  background: #1e293b;
  color: #e2e8f0;
}
[data-theme="light"] .chat-msg .bubble code {
  background: var(--surface2);
  color: #be185d;
}
[data-theme="light"] .chat-msg .bubble pre code {
  background: transparent;
  color: inherit;
}
[data-theme="light"] .chat-msg .bubble th {
  background: var(--surface2);
}
[data-theme="light"] .chat-msg .bubble th,
[data-theme="light"] .chat-msg .bubble td {
  border-color: var(--border2);
}
[data-theme="light"] .chat-msg .bubble blockquote {
  border-left-color: var(--text2);
  color: var(--text2);
}
[data-theme="light"] .chat-msg .bubble .msg-copy-btn {
  background: rgba(0,0,0,.06);
  border-color: rgba(0,0,0,.1);
  color: var(--text2);
}
[data-theme="light"] .chat-msg .bubble .msg-copy-btn:hover {
  background: rgba(0,0,0,.1);
  color: var(--text);
}

/* Chat input area */
[data-theme="light"] .chat-input-area {
  background: var(--bg);
  border-top-color: var(--border);
}
[data-theme="light"] .chat-input-area select {
  background: var(--surface);
  border-color: var(--border2);
  color: var(--text);
}
[data-theme="light"] .chat-input-area textarea {
  background: var(--surface);
  border-color: var(--border2);
  color: var(--text);
}
[data-theme="light"] .chat-input-area textarea::placeholder {
  color: var(--text2);
}
[data-theme="light"] .chat-input-area button {
  background: var(--accent);
  color: #fff;
}
[data-theme="light"] .btn-clear {
  border-color: var(--border2);
  color: var(--text2);
}
[data-theme="light"] .btn-clear:hover {
  border-color: var(--red);
  color: var(--red);
}
[data-theme="light"] .btn-stop {
  border-color: var(--red);
  background: rgba(220,38,38,.08);
  color: var(--red);
}

/* Right panel */
[data-theme="light"] .right-panel {
  background: var(--bg);
}
[data-theme="light"] .right-tabs {
  background: var(--surface);
  border-bottom-color: var(--border);
}
[data-theme="light"] .right-tab {
  color: var(--text2);
}
[data-theme="light"] .right-tab:hover {
  color: var(--text);
}
[data-theme="light"] .right-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* Graph */
[data-theme="light"] #graphContainer {
  background: var(--bg);
}
[data-theme="light"] .graph-tooltip {
  background: var(--surface);
  border-color: var(--border2);
  color: var(--text);
  box-shadow: 0 4px 12px rgba(0,0,0,.1);
}
[data-theme="light"] .graph-legend {
  background: var(--surface);
  border-top-color: var(--border);
}
[data-theme="light"] .legend-item {
  color: var(--text2);
}

/* Wiki */
[data-theme="light"] .cat-tab {
  border-color: var(--border2);
  color: var(--text2);
}
[data-theme="light"] .cat-tab:hover {
  border-color: var(--accent);
  color: var(--text);
}
[data-theme="light"] .cat-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
[data-theme="light"] .file-card {
  background: var(--surface);
  border-color: var(--border);
}
[data-theme="light"] .file-card:hover {
  border-color: var(--accent);
  background: var(--surface2);
}
[data-theme="light"] .pagination button {
  border-color: var(--border2);
  color: var(--text);
}
[data-theme="light"] .pagination button:hover {
  border-color: var(--accent);
}
[data-theme="light"] .pagination button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Modal */
[data-theme="light"] .modal-overlay {
  background: rgba(0,0,0,.3);
}
[data-theme="light"] .modal {
  background: var(--surface);
  border-color: var(--border2);
  box-shadow: 0 16px 48px rgba(0,0,0,.15);
}
[data-theme="light"] .modal-header {
  border-bottom-color: var(--border);
}
[data-theme="light"] .modal-close {
  color: var(--text2);
}
[data-theme="light"] .modal-close:hover {
  color: var(--text);
}
[data-theme="light"] .modal-body {
  color: var(--text);
}
[data-theme="light"] .modal-body h2 {
  border-bottom-color: var(--border);
}
[data-theme="light"] .modal-body th {
  background: var(--surface2);
}
[data-theme="light"] .modal-body th,
[data-theme="light"] .modal-body td {
  border-color: var(--border2);
}
[data-theme="light"] .modal-body code {
  background: var(--surface2);
  color: #be185d;
}
[data-theme="light"] .modal-body blockquote {
  border-left-color: var(--text2);
  color: var(--text2);
}
[data-theme="light"] .modal-body hr {
  border-top-color: var(--border);
}

/* Code blocks in light mode - keep dark for readability */
[data-theme="light"] .code-copy-btn {
  background: rgba(255,255,255,.15);
  border-color: rgba(255,255,255,.2);
  color: rgba(255,255,255,.7);
}
[data-theme="light"] .code-copy-btn:hover {
  background: rgba(255,255,255,.25);
  color: #fff;
}

/* Slash menu */
[data-theme="light"] .slash-menu {
  background: var(--surface);
  border-color: var(--border2);
  box-shadow: 0 -4px 16px rgba(0,0,0,.1);
}
[data-theme="light"] .slash-item {
  border-bottom-color: var(--border);
}
[data-theme="light"] .slash-item:hover,
[data-theme="light"] .slash-item.active {
  background: var(--accent-bg);
}
[data-theme="light"] .slash-pill {
  background: var(--accent-bg);
  color: var(--accent);
}

/* Source picker */
[data-theme="light"] .source-picker {
  background: var(--surface);
  border-color: var(--border2);
  box-shadow: 0 -4px 16px rgba(0,0,0,.1);
}
[data-theme="light"] .source-option {
  border-color: var(--border2);
}
[data-theme="light"] .source-option:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
}

/* Skills */
[data-theme="light"] .skill-card {
  background: var(--surface);
  border-color: var(--border2);
}
[data-theme="light"] .skill-card:hover {
  border-color: var(--accent);
}
[data-theme="light"] .skill-card-btn {
  border-color: var(--accent);
  color: var(--accent);
}
[data-theme="light"] .skill-card-btn:hover {
  background: var(--accent);
  color: #fff;
}
[data-theme="light"] .skill-view-btn {
  border-color: var(--border2);
  color: var(--text2);
}
[data-theme="light"] .skill-view-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}
[data-theme="light"] .skill-modal-overlay {
  background: rgba(0,0,0,.3);
}
[data-theme="light"] .skill-modal {
  background: var(--surface);
  border-color: var(--border2);
  box-shadow: 0 16px 48px rgba(0,0,0,.15);
}
[data-theme="light"] .skill-modal-header {
  border-bottom-color: var(--border);
}
[data-theme="light"] .skill-modal-body textarea {
  background: var(--bg);
  border-color: var(--border2);
  color: var(--text);
}
[data-theme="light"] .skill-modal-footer {
  border-top-color: var(--border);
}

/* Context menu */
[data-theme="light"] #ctxMenu {
  background: var(--surface) !important;
  border-color: rgba(0,0,0,.12) !important;
  box-shadow: 0 4px 16px rgba(0,0,0,.12) !important;
}
[data-theme="light"] #ctxMenu #ctxDelete {
  color: var(--text) !important;
}

/* ============ Admin page overrides ============ */
[data-theme="light"] table {
  background: var(--surface);
}
[data-theme="light"] th {
  background: var(--surface2);
  color: var(--text2);
}
[data-theme="light"] th, [data-theme="light"] td {
  border-bottom-color: var(--border);
}
[data-theme="light"] tr:hover td {
  background: rgba(0,0,0,.02);
}
[data-theme="light"] .badge-admin {
  background: var(--accent-bg);
  color: var(--accent);
}
[data-theme="light"] .badge-user {
  background: rgba(22,163,74,.1);
  color: var(--green);
}
[data-theme="light"] .btn-primary {
  background: var(--accent);
}
[data-theme="light"] .btn-primary:hover {
  background: var(--accent2);
}
/* Admin modal */
[data-theme="light"] .modal {
  background: var(--surface);
  border-color: var(--border2);
}
[data-theme="light"] .form-group input,
[data-theme="light"] .form-group select {
  background: var(--bg);
  border-color: var(--border2);
  color: var(--text);
}
[data-theme="light"] .form-group input:focus,
[data-theme="light"] .form-group select:focus {
  border-color: var(--accent);
}

/* ============ Login page overrides ============ */
[data-theme="light"] .login-card {
  background: rgba(255,255,255,.92);
  border-color: rgba(0,0,0,.08);
  box-shadow: 0 20px 60px rgba(0,0,0,.1), 0 0 0 1px rgba(0,0,0,.03) inset;
}
[data-theme="light"] .login-card:hover {
  box-shadow: 0 24px 64px rgba(0,0,0,.12), 0 0 0 1px rgba(0,0,0,.05) inset;
}
[data-theme="light"] .logo-title {
  color: var(--text);
}
[data-theme="light"] .logo-title span {
  color: var(--accent);
}
[data-theme="light"] .logo-sub {
  color: var(--text2);
}
[data-theme="light"] .form-group label {
  color: var(--text2);
}
[data-theme="light"] .form-group input {
  background: var(--bg);
  border-color: var(--border2);
  color: var(--text);
}
[data-theme="light"] .form-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,111,212,.15);
}
[data-theme="light"] .form-group input::placeholder {
  color: rgba(107,114,128,.5);
}
[data-theme="light"] .footer {
  color: var(--text2);
}
[data-theme="light"] #captchaCanvas {
  border-color: var(--border2) !important;
}

/* ============ Highlight.js light theme for code blocks ============ */
/* Keep code blocks dark in light mode for better readability */
[data-theme="light"] pre {
  background: #1e293b !important;
  color: #e2e8f0;
}

/* ============ Upload & Organize - Light Mode ============ */
[data-theme="light"] .btn-upload {
  border-color: var(--border2);
  color: var(--text2);
}
[data-theme="light"] .btn-upload:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(59,111,212,.06);
}
[data-theme="light"] .upload-notice {
  background: rgba(59,111,212,.05);
  border-color: rgba(59,111,212,.15);
}
[data-theme="light"] .upload-filename {
  color: var(--text);
}
[data-theme="light"] .upload-meta {
  color: var(--text2);
}
[data-theme="light"] .upload-action-btn.primary {
  background: var(--accent);
  color: #fff;
}
[data-theme="light"] .upload-action-btn.secondary {
  background: var(--surface2);
  border-color: var(--border2);
  color: var(--text2);
}
[data-theme="light"] .organize-step {
  color: var(--text2);
}

/* ============ Search Results in Wiki - Light Mode Fix ============ */
[data-theme="light"] .wiki-panel {
  background: var(--bg);
}
[data-theme="light"] .right-panel {
  background: var(--surface);
}
[data-theme="light"] .right-tabs {
  background: var(--surface);
  border-bottom-color: var(--border);
}
[data-theme="light"] .right-tab {
  color: var(--text2);
}
[data-theme="light"] .right-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
[data-theme="light"] .right-content {
  background: var(--bg);
}

/* =====================================================================
   MOBILE RESPONSIVE LAYER  (<=768px)
   Desktop styles are never altered. Everything below is scoped to narrow
   viewports only, so PC rendering stays pixel-identical.
   ===================================================================== */

/* Hamburger + drawer chrome are hidden on desktop by default. */
.mobile-hamburger { display: none; }
.mobile-drawer { display: none; }
.mobile-drawer-backdrop { display: none; }

@media (max-width: 768px) {
  /* ---- 1. Unlock scrolling on pages that lock the viewport ---- */
  /* Higher-specificity selectors override inline `body{height:100vh;overflow:hidden}`. */
  html, html body {
    height: auto !important;
    min-height: 100vh;
    overflow-x: hidden;
    overflow-y: auto !important;
    -webkit-text-size-adjust: 100%;
  }

  /* ---- 2. Readable base font + comfortable line height ---- */
  body {
    font-size: 15px;
  }

  /* ---- 3. Compact header: keep logo + title, push everything into drawer ---- */
  .header {
    padding: 0 12px !important;
    gap: 8px !important;
    position: sticky;
    top: 0;
    z-index: 300;
  }
  .header h1 {
    font-size: 15px !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ---- 4. Hamburger button ---- */
  .mobile-hamburger {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    margin-left: auto;
    width: 40px;
    height: 40px;
    padding: 8px;
    border: 1px solid var(--border2);
    border-radius: 8px;
    background: transparent;
    cursor: pointer;
    flex-shrink: 0;
  }
  .mobile-hamburger span {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--text);
    border-radius: 2px;
    transition: transform .2s, opacity .2s;
  }
  .mobile-hamburger.open span:nth-child(1) { transform: translateY(6px) rotate(45deg); }
  .mobile-hamburger.open span:nth-child(2) { opacity: 0; }
  .mobile-hamburger.open span:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }

  /* ---- 5. Drawer panel ---- */
  .mobile-drawer {
    display: block;
    position: fixed;
    top: 0;
    right: 0;
    width: min(82vw, 320px);
    height: 100vh;
    background: var(--surface);
    border-left: 1px solid var(--border2);
    box-shadow: -8px 0 32px rgba(0,0,0,.35);
    z-index: 1000;
    transform: translateX(100%);
    transition: transform .25s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 64px 16px 24px;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
  .mobile-drawer.open { transform: translateX(0); }

  /* Drawer item layout: stack links/controls vertically, big tap targets */
  .mobile-drawer > * {
    width: 100% !important;
    max-width: none !important;
    margin: 0 !important;
    flex: none !important;
  }
  /* Nav containers inside drawer -> vertical */
  .mobile-drawer .header-nav,
  .mobile-drawer .nav,
  .mobile-drawer > div {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 2px !important;
  }
  .mobile-drawer a {
    display: block;
    padding: 12px 12px;
    min-height: 44px;
    line-height: 20px;
    border-radius: 8px;
    font-size: 15px !important;
    color: var(--text) !important;
    border-bottom: 1px solid var(--border);
  }
  .mobile-drawer a:hover { background: var(--surface2); }
  .mobile-drawer button {
    min-height: 40px;
    font-size: 14px !important;
  }
  /* Search box inside drawer */
  .mobile-drawer .search-box,
  .mobile-drawer .kb-search-box,
  .mobile-drawer .search-box-inline {
    width: 100% !important;
    max-width: none !important;
    margin-bottom: 8px;
  }
  .mobile-drawer input[type="text"] {
    width: 100% !important;
    font-size: 16px !important;  /* prevents iOS zoom-on-focus */
    min-height: 40px;
  }
  /* Dropdown trigger / tools menu shown inline in drawer */
  .mobile-drawer .tools-dropdown,
  .mobile-drawer .tools-dropdown-menu { position: static; display: block; }
  .mobile-drawer .tools-dropdown-menu {
    box-shadow: none;
    border: none;
    margin: 0;
    min-width: 0;
    background: transparent;
  }

  /* ---- 6. Backdrop ---- */
  .mobile-drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    z-index: 999;
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s;
  }
  .mobile-drawer-backdrop.show {
    opacity: 1;
    pointer-events: auto;
  }

  /* ---- 7. Table safety net: prevent horizontal blowout ---- */
  /* Wrap-free wide tables become horizontally scrollable instead of
     stretching the viewport. Applies generically; pages may refine. */
  table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }

  /* ---- 8. Generic touch sizing for primary controls ---- */
  button, .btn, input[type="submit"], input[type="button"] {
    touch-action: manipulation;
  }
}

/* Extra-small phones */
@media (max-width: 420px) {
  .mobile-drawer { width: 88vw; }
  .header h1 { font-size: 14px !important; }
}
