@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500&family=Syne:wght@400;600;700;800&family=DM+Sans:wght@300;400;500&display=swap');

/* ── Dark theme (default) ── */
:root,
[data-theme="dark"] .latex-hero,
[data-theme="dark"] .latex-app {
  --bg:        #0e0f14;
  --surface:   #14151c;
  --surface2:  #1c1e28;
  --surface3:  #222533;
  --border:    #2a2d3e;
  --border2:   #353850;
  --accent:    #6ee7b7;
  --accent2:   #818cf8;
  --accent3:   #f472b6;
  --warn:      #fb923c;
  --error:     #f87171;
  --text:      #e2e4f0;
  --text2:     #8b8fa8;
  --text3:     #555870;
  --gutter:    #1a1c26;
  --nav-h:     var(--header-height-desktop, 72px);
  --preview-bg:     #e8e8e2;
  --preview-toolbar: #eeeee8;
  --preview-border:  #d5d5cc;
  --preview-btn-bg:  white;
  --preview-btn-clr: #555;
  --preview-text:    #777;
}

/* ── Light theme ── */
[data-theme="light"] .latex-hero,
[data-theme="light"] .latex-app {
  --bg:        #ffffff;
  --surface:   #f8f9fb;
  --surface2:  #f0f1f4;
  --surface3:  #e8eaef;
  --border:    #dde0e8;
  --border2:   #c8ccd6;
  --accent:    #059669;
  --accent2:   #6366f1;
  --accent3:   #db2777;
  --warn:      #d97706;
  --error:     #dc2626;
  --text:      #1e293b;
  --text2:     #64748b;
  --text3:     #94a3b8;
  --gutter:    #f1f3f5;
  --preview-bg:     #f5f5f0;
  --preview-toolbar: #eeeee8;
  --preview-border:  #d5d5cc;
  --preview-btn-bg:  white;
  --preview-btn-clr: #555;
  --preview-text:    #777;
}

/* ── HERO SECTION — SEO heading + ad placement ── */
.latex-hero {
  margin-top: var(--nav-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  gap: 16px;
  position: relative;
  flex-shrink: 0;
  font-family: 'DM Sans', sans-serif;
}

.latex-hero-content {
  flex-shrink: 0;
  min-width: 200px;
}

.latex-hero-title {
  font-family: 'Syne', sans-serif;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
  margin: 0;
}

.latex-hero-desc {
  font-size: 11px;
  color: var(--text2);
  margin: 2px 0 0;
  line-height: 1.3;
}

.latex-hero-ad {
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
}

.latex-hero-dismiss {
  position: absolute;
  top: 4px;
  right: 6px;
  background: none;
  border: none;
  color: var(--text3);
  font-size: 16px;
  cursor: pointer;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  transition: all 0.15s;
  line-height: 1;
}

.latex-hero-dismiss:hover {
  background: var(--surface2);
  color: var(--text);
}

/* When hero is dismissed */
.latex-hero.dismissed {
  display: none;
}

/* ── APP CONTAINER — fills viewport below hero or nav ── */
.latex-app {
  height: calc(100vh - var(--nav-h) - var(--hero-h, 0px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: 'DM Sans', sans-serif;
  font-size: 13px;
}

/* ── TITLEBAR ── */
.titlebar {
  height: 38px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 8px;
  flex-shrink: 0;
}

.titlebar-logo {
  font-family: 'Syne', sans-serif;
  font-weight: 800;
  font-size: 14px;
  color: var(--accent);
  letter-spacing: -0.5px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.titlebar-logo svg { width: 18px; height: 18px; }

.titlebar-project {
  color: var(--text2);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: 8px;
}

.titlebar-project .sep { color: var(--text3); }
.titlebar-project .project-name {
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
  padding: 1px 6px;
  border-radius: 3px;
  transition: background 0.15s;
}
.titlebar-project .project-name:hover { background: var(--surface2); }

/* Save indicator */
.save-indicator {
  font-size: 10px;
  margin-left: 6px;
  transition: opacity 0.3s;
}
.save-indicator.saved { color: var(--accent); opacity: 1; }
.save-indicator.saving { color: var(--text3); opacity: 1; }
.save-indicator.dirty { color: var(--warn); opacity: 1; }

.titlebar-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

.latex-app .nav-btn {
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 11px;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.15s;
  font-family: 'DM Sans', sans-serif;
  background: none;
  border: none;
}

.latex-app .nav-btn:hover { color: var(--text); background: var(--surface2); }

/* Primary CTA — "+ New Project" — high contrast so first-time users find it */
.latex-app .nav-btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  font-family: 'DM Sans', sans-serif;
  color: #fff;
  background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
  border: none;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(79, 70, 229, 0.25), 0 0 0 1px rgba(99, 102, 241, 0.20) inset;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}
.latex-app .nav-btn-primary:hover {
  filter: brightness(1.06);
  box-shadow: 0 2px 6px rgba(79, 70, 229, 0.35), 0 0 0 1px rgba(99, 102, 241, 0.30) inset;
  transform: translateY(-1px);
}
.latex-app .nav-btn-primary:active { transform: translateY(0); filter: brightness(0.96); }

/* Secondary — "Projects" — visible button, not just text */
.latex-app .nav-btn-secondary {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 11px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  font-family: 'DM Sans', sans-serif;
  color: var(--text);
  background: var(--surface2, #1e293b);
  border: 1px solid var(--border, #334155);
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.latex-app .nav-btn-secondary:hover {
  background: var(--surface3, #334155);
  border-color: var(--accent, #6366f1);
}
[data-theme="light"] .latex-app .nav-btn-secondary {
  background: #ffffff;
  border-color: #e2e8f0;
  color: #1e293b;
}
[data-theme="light"] .latex-app .nav-btn-secondary:hover {
  background: #f8fafc;
  border-color: #6366f1;
}

/* ── MAIN LAYOUT ── */
.latex-app .main {
  display: flex;
  flex: 1;
  overflow: hidden;
  min-height: 0; /* critical for nested flex */
}

/* ── FILE TREE ── */
.filetree {
  width: 200px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

.filetree-header {
  padding: 10px 12px 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.filetree-title {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
}

.filetree-actions { display: flex; gap: 4px; }
.filetree-actions span {
  color: var(--text3);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  transition: color 0.15s;
}
.filetree-actions span:hover { color: var(--text2); }

.file-item {
  padding: 5px 12px 5px 20px;
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.1s;
  position: relative;
}

.file-item:hover { background: var(--surface2); color: var(--text); }
.file-item.active {
  background: rgba(110,231,183,0.06);
  color: var(--accent);
}

.file-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 2px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}

.file-icon { font-size: 11px; opacity: 0.7; }

.file-actions-btn {
  display: none;
  margin-left: auto; padding: 2px 5px;
  font-size: 16px; font-weight: bold; line-height: 1;
  color: var(--text2, #8b8fa8); cursor: pointer;
  border-radius: 3px;
}
.file-actions-btn:hover { color: var(--text, #e2e4f0); background: var(--surface3, #222533); }
.file-item:hover .file-actions-btn { display: block; }
[data-theme="light"] .file-actions-btn { color: #94a3b8; }
[data-theme="light"] .file-actions-btn:hover { color: #334155; background: #f1f5f9; }

/* ── File context menu ── */
.file-ctx-menu {
  position: fixed; z-index: 9999;
  display: none;
  min-width: 140px;
  background: var(--surface2, #1c1e28);
  border: 1px solid var(--border, #2a2d3e);
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
  padding: 4px 0;
  font-size: 12px;
}
.file-ctx-menu.visible { display: block; }
.file-ctx-item {
  padding: 6px 14px; cursor: pointer;
  color: var(--text, #e2e4f0);
  transition: background 0.1s;
}
.file-ctx-item:hover { background: var(--surface3, #222533); }
.file-ctx-danger { color: var(--error, #f87171); }
.file-ctx-danger:hover { background: rgba(248,113,113,0.12); }
.file-ctx-disabled {
  opacity: 0.3; pointer-events: none;
}
.file-ctx-divider {
  height: 1px; background: var(--border, #2a2d3e);
  margin: 3px 0;
}
[data-theme="light"] .file-ctx-menu { background: #fff; border-color: #e2e8f0; box-shadow: 0 6px 20px rgba(0,0,0,0.1); }
[data-theme="light"] .file-ctx-item { color: #334155; }
[data-theme="light"] .file-ctx-item:hover { background: #f1f5f9; }
[data-theme="light"] .file-ctx-danger { color: #dc2626; }
[data-theme="light"] .file-ctx-danger:hover { background: rgba(220,38,38,0.06); }
.file-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.file-insert-btn {
  display: none;
  width: 16px; height: 16px;
  border-radius: 3px;
  background: rgba(110,231,183,0.1);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  text-align: center;
  line-height: 16px;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: auto;
  transition: background 0.15s;
}

.file-item:hover .file-insert-btn { display: block; }
.file-insert-btn:hover { background: rgba(110,231,183,0.2); }

/* ── Editing indicator bar (shown when editing a sub-file) ── */
.editing-indicator {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 12px; gap: 8px;
  background: rgba(99, 102, 241, 0.1);
  border-bottom: 1px solid rgba(99, 102, 241, 0.25);
  font-size: 11px; color: var(--accent2, #818cf8);
  flex-shrink: 0;
}
.editing-indicator strong { color: var(--text); }
.ei-back {
  background: var(--accent2, #818cf8); color: #fff;
  border: none; font-size: 11px; font-weight: 500;
  padding: 3px 10px; border-radius: 4px;
  cursor: pointer; white-space: nowrap;
}
.ei-back:hover { filter: brightness(1.15); }

[data-theme="light"] .editing-indicator { background: rgba(99,102,241,0.06); }

.outline-section {
  margin-top: 8px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.outline-item {
  padding: 3px 12px 3px 16px;
  font-size: 11px;
  color: var(--text3);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: color 0.1s;
}

.outline-item:hover { color: var(--text2); }
.outline-item.h1 { padding-left: 16px; color: var(--text2); font-weight: 500; }
.outline-item.h2 { padding-left: 24px; }
.outline-item.h3 { padding-left: 32px; font-size: 10px; }

/* ── EDITOR PANE ── */
.editor-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
  min-width: 0;
}

.editor-tabs {
  height: 34px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-end;
  padding: 0 8px;
  gap: 2px;
  flex-shrink: 0;
}

.editor-tab {
  height: 28px;
  padding: 0 12px;
  border-radius: 5px 5px 0 0;
  font-size: 11px;
  color: var(--text3);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s;
  border: 1px solid transparent;
  border-bottom: none;
}

.editor-tab:hover { color: var(--text2); }
.editor-tab.active {
  background: var(--bg);
  color: var(--text);
  border-color: var(--border);
}

.editor-body {
  flex: 1;
  overflow: auto;
  background: var(--bg);
  position: relative;
  min-height: 0; /* allow flex child to shrink */
}

/* CodeMirror override to fill container */
.editor-body .CodeMirror {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  height: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  line-height: 1.5;
}

/* ── PREVIEW PANE ── */
.preview-pane {
  width: 42%;
  background: #f5f5f0;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  position: relative;
}

.preview-toolbar {
  height: 34px;
  background: var(--preview-toolbar);
  border-bottom: 1px solid var(--preview-border);
  display: flex;
  align-items: center;
  padding: 0 10px;
  gap: 6px;
  flex-shrink: 0;
}

.preview-btn {
  height: 24px;
  padding: 0 8px;
  border-radius: 4px;
  border: 1px solid var(--preview-border);
  background: var(--preview-btn-bg);
  font-size: 11px;
  color: var(--preview-btn-clr);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  font-family: 'DM Sans', sans-serif;
}

.preview-btn:hover { background: var(--preview-toolbar); }
.preview-btn.active { background: var(--preview-border); border-color: #bbb; font-weight: 600; color: #333; }

.preview-sep { width: 1px; height: 16px; background: #ccc; margin: 0 4px; }

.page-info {
  font-size: 11px;
  color: var(--preview-text);
  margin: 0 4px;
}

.zoom-level {
  font-size: 11px;
  color: var(--preview-btn-clr);
  font-weight: 500;
  margin-left: auto;
}

.preview-body {
  flex: 1;
  overflow: auto;
  display: flex;
  justify-content: center;
  padding: 20px;
  background: var(--preview-bg);
  position: relative;
}

.preview-body canvas {
  max-width: 100%;
  box-shadow: 0 2px 20px rgba(0,0,0,0.15);
  background: white;
}

/* Native PDF iframe — fills preview pane, true PDF with text select, scroll, links */
.pdf-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: white;
}

.compiling-overlay {
  position: absolute;
  inset: 0;
  background: rgba(232,232,226,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 5;
}

.compiling-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.empty-preview {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: #999;
}

.empty-preview.hidden { display: none; }
.empty-preview-icon { font-size: 48px; opacity: 0.4; }
.empty-preview-text { font-size: 13px; font-family: 'DM Sans', sans-serif; }

.spinner {
  width: 28px; height: 28px;
  border: 2px solid rgba(0,0,0,0.1);
  border-top-color: #333;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
.spinner-text { font-size: 11px; color: #555; font-family: 'DM Sans', sans-serif; }

/* ── LOG PANEL ── */
.log-panel {
  height: 150px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: height 0.2s;
}

.log-panel.collapsed { height: 28px; overflow: hidden; }

.log-header {
  height: 28px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
}

.log-title {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
}

.log-badge {
  font-size: 9px;
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 700;
}

.log-badge.errors { background: rgba(248,113,113,0.15); color: var(--error); }
.log-badge.ok { background: rgba(110,231,183,0.1); color: var(--accent); }

.log-toggle { margin-left: auto; color: var(--text3); font-size: 10px; }

.log-body {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
}

.log-line {
  padding: 2px 14px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10.5px;
  line-height: 1.7;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  color: var(--text3);
}

.log-line.info { color: var(--text2); }
.log-line.error { color: var(--error); }
.log-line.warn { color: var(--warn); }
.log-line.success { color: var(--accent); }

.log-prefix {
  opacity: 0.5;
  flex-shrink: 0;
  user-select: none;
}

/* ── STATUS BAR ── */
.statusbar {
  height: 22px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 16px;
  flex-shrink: 0;
}

.sb-item {
  font-size: 10px;
  color: var(--text3);
  display: flex;
  align-items: center;
  gap: 4px;
}

.sb-item.accent { color: var(--accent); }
.sb-sep { width: 1px; height: 12px; background: var(--border); }

/* ── SYMBOL PICKER ── */
.symbol-picker {
  position: fixed;
  bottom: 200px;
  left: 300px;
  width: 320px;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 9px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.6);
  overflow: hidden;
  z-index: 20;
  display: none;
}

.symbol-picker.visible { display: block; }

.sp-header {
  padding: 10px 12px 8px;
  border-bottom: 1px solid var(--border);
}

.sp-search {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 10px;
  color: var(--text);
  font-size: 12px;
  font-family: 'DM Sans', sans-serif;
  outline: none;
}

.sp-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 12px 0;
  flex-wrap: wrap;
}

.sp-tab {
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 10px;
  color: var(--text3);
  cursor: pointer;
  border: none;
  background: none;
  font-family: 'DM Sans', sans-serif;
}

.sp-tab:hover { color: var(--text2); }
.sp-tab.active { background: rgba(110,231,183,0.1); color: var(--accent); }

.sp-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  padding: 8px 12px 12px;
  max-height: 200px;
  overflow-y: auto;
}

.sp-sym {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  border-radius: 5px;
  cursor: pointer;
  color: var(--text);
  transition: background 0.1s;
  border: none;
  background: none;
  font-family: 'JetBrains Mono', monospace;
}

.sp-sym:hover {
  background: rgba(110,231,183,0.1);
  color: var(--accent);
}

/* ── PROJECT MENU ── */
.project-menu {
  position: fixed;
  top: 40px;
  right: 16px;
  width: 300px;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 9px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.6);
  overflow: hidden;
  z-index: 25;
  display: none;
}

.project-menu.visible { display: block; }

.pm-header {
  padding: 10px 14px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
  border-bottom: 1px solid var(--border);
}

.pm-list {
  max-height: 240px;
  overflow-y: auto;
  padding: 4px 0;
}

.pm-item {
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  transition: background 0.1s;
}

.pm-item:hover { background: var(--surface3); }
.pm-item.active { background: rgba(110,231,183,0.06); }
.pm-item.active .pm-item-name { color: var(--accent); }

.pm-item-name {
  font-size: 12px;
  color: var(--text);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.pm-item-time {
  font-size: 10px;
  color: var(--text3);
  flex-shrink: 0;
  margin-left: 12px;
}

.pm-empty {
  padding: 16px 14px;
  text-align: center;
  font-size: 12px;
  color: var(--text3);
}

.pm-actions {
  border-top: 1px solid var(--border);
  padding: 8px 10px;
  display: flex;
  gap: 6px;
}

.pm-btn {
  flex: 1;
  padding: 5px 8px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: none;
  color: var(--text2);
  font-size: 10px;
  font-family: 'DM Sans', sans-serif;
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
}

.pm-btn:hover { background: var(--surface3); color: var(--text); }
.pm-btn.danger { color: var(--error); border-color: rgba(248,113,113,0.2); }
.pm-btn.danger:hover { background: rgba(248,113,113,0.08); }

/* ── TOAST NOTIFICATIONS ── */
.toast-container {
  position: fixed;
  top: calc(var(--nav-h, 72px) + 8px);
  right: 16px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 12px;
  font-family: 'DM Sans', sans-serif;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  animation: toastIn 0.3s ease;
  max-width: 360px;
}

.toast.error { background: #2d1418; color: var(--error); border: 1px solid rgba(248,113,113,0.2); }
.toast.success { background: #142d1c; color: var(--accent); border: 1px solid rgba(110,231,183,0.2); }
.toast.info { background: #14182d; color: var(--accent2); border: 1px solid rgba(129,140,248,0.2); }
.toast.warning { background: #2d2314; color: var(--warn); border: 1px solid rgba(251,146,60,0.2); }

/* Upload insert prompt buttons */
.upload-insert-btn {
  padding: 4px 10px;
  border-radius: 4px;
  border: 1px solid rgba(110,231,183,0.3);
  background: rgba(110,231,183,0.1);
  color: var(--accent);
  font-size: 11px;
  font-family: 'DM Sans', sans-serif;
  cursor: pointer;
  transition: all 0.15s;
}
.upload-insert-btn:hover { background: rgba(110,231,183,0.2); }
.upload-insert-btn.secondary {
  border-color: var(--border);
  background: none;
  color: var(--text3);
}
.upload-insert-btn.secondary:hover { color: var(--text2); background: var(--surface2); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── AI FIX BUTTON (inside error widget) ── */
.ai-fix-btn {
  padding: 3px 10px;
  border-radius: 4px;
  border: 1px solid rgba(129,140,248,0.3);
  background: rgba(129,140,248,0.1);
  color: #a5b4fc;
  font-size: 10px;
  font-weight: 600;
  font-family: 'DM Sans', sans-serif;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s;
  white-space: nowrap;
}

.ai-fix-btn:hover {
  background: rgba(129,140,248,0.2);
  border-color: rgba(129,140,248,0.5);
  color: #c4b5fd;
}

.ai-fix-btn:disabled {
  opacity: 0.5;
  cursor: wait;
}

[data-theme="light"] .ai-fix-btn {
  border-color: rgba(99,102,241,0.3);
  background: rgba(99,102,241,0.08);
  color: #6366f1;
}
[data-theme="light"] .ai-fix-btn:hover {
  background: rgba(99,102,241,0.15);
  color: #4f46e5;
}

/* ── AI PROMPT POPUP ── */
.ai-prompt-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.95);
  width: 440px;
  max-width: 90vw;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  z-index: 50;
  display: none;
  opacity: 0;
  transition: opacity 0.15s, transform 0.15s;
}

.ai-prompt-popup.visible {
  display: block;
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.ai-prompt-header {
  padding: 12px 16px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}

.ai-prompt-icon {
  font-size: 16px;
}

.ai-prompt-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
}

.ai-prompt-close {
  color: var(--text3);
  font-size: 18px;
  cursor: pointer;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.15s;
}

.ai-prompt-close:hover {
  background: var(--surface3);
  color: var(--text);
}

.ai-prompt-body {
  padding: 12px 16px 14px;
}

.ai-prompt-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 13px;
  font-family: 'DM Sans', sans-serif;
  line-height: 1.5;
  resize: none;
  outline: none;
  transition: border-color 0.15s;
}

.ai-prompt-input:focus {
  border-color: var(--accent2);
}

.ai-prompt-input::placeholder {
  color: var(--text3);
}

.ai-prompt-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
}

.ai-prompt-hint {
  font-size: 10px;
  color: var(--text3);
}

.ai-prompt-submit {
  padding: 6px 16px;
  border-radius: 6px;
  border: none;
  background: var(--accent2);
  color: white;
  font-size: 12px;
  font-weight: 600;
  font-family: 'DM Sans', sans-serif;
  cursor: pointer;
  transition: background 0.15s;
}

.ai-prompt-submit:hover {
  background: #6d6ff0;
}

/* ── AI REWRITE MENU ── */
.ai-rewrite-menu {
  position: fixed;
  top: 90px;
  right: 200px;
  width: 220px;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 9px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  z-index: 30;
  display: none;
  overflow: hidden;
}

.ai-rewrite-menu.visible { display: block; }

.ai-rw-header {
  padding: 8px 14px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
  border-bottom: 1px solid var(--border);
}

.ai-rw-item {
  display: block;
  width: 100%;
  padding: 8px 14px;
  border: none;
  background: none;
  text-align: left;
  font-size: 12px;
  font-family: 'DM Sans', sans-serif;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.1s;
}

.ai-rw-item:hover {
  background: var(--surface3);
  color: var(--text);
}

/* ── AI LOADING OVERLAY (on editor pane) ── */
.ai-loading-overlay {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(14,15,20,0.6);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 10;
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.ai-loading-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

[data-theme="light"] .ai-loading-overlay {
  background: rgba(248,249,251,0.7);
}

.ai-spinner {
  width: 28px;
  height: 28px;
  border: 2px solid rgba(129,140,248,0.2);
  border-top-color: var(--accent2);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.ai-spinner-text {
  font-size: 13px;
  font-family: 'DM Sans', sans-serif;
  color: var(--accent2);
  font-weight: 500;
}

.ai-spinner-cancel {
  margin-top: 4px;
  padding: 4px 14px;
  border-radius: 5px;
  border: 1px solid var(--border2);
  background: none;
  color: var(--text3);
  font-size: 11px;
  font-family: 'DM Sans', sans-serif;
  cursor: pointer;
  transition: all 0.15s;
}

.ai-spinner-cancel:hover {
  background: var(--surface2);
  color: var(--text);
}

/* ── AI Fix button inline spinner ── */
.ai-btn-spinner {
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 1.5px solid rgba(129,140,248,0.3);
  border-top-color: #a5b4fc;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
  margin-right: 4px;
}

/* ── AI STATUS BAR INDICATOR ── */
.ai-status {
  color: var(--accent2) !important;
  animation: aiPulse 1.5s ease-in-out infinite;
}

@keyframes aiPulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

/* ── AI TOOLBAR BUTTON GLOW ── */
.ai-group {
  border-right: 1px solid var(--border);
}

.ai-btn {
  background: rgba(129,140,248,0.08) !important;
  color: var(--accent2) !important;
  font-weight: 600;
}

.ai-btn:hover {
  background: rgba(129,140,248,0.15) !important;
}

/* Full preview mode */
.latex-app .main.full-preview .filetree,
.latex-app .main.full-preview .editor-pane {
  display: none;
}
.latex-app .main.full-preview .preview-pane {
  width: 100%;
}

/* ── Light theme AI overrides ── */
[data-theme="light"] .ai-prompt-popup {
  box-shadow: 0 20px 60px rgba(0,0,0,0.15);
}

[data-theme="light"] .ai-prompt-submit {
  background: #6366f1;
}

[data-theme="light"] .ai-rewrite-menu {
  box-shadow: 0 12px 40px rgba(0,0,0,0.15);
}

[data-theme="light"] .ai-btn {
  background: rgba(99,102,241,0.08) !important;
  color: #6366f1 !important;
}

[data-theme="light"] .ai-btn:hover {
  background: rgba(99,102,241,0.15) !important;
}

/* ── Mobile toggle FAB — hidden on desktop ── */
.mobile-toggle { display: none !important; }
.mobile-files-btn { display: none !important; }
.mobile-fab { display: none; }

/* ── MOBILE ── */
@media (max-width: 768px) {
  .latex-app {
    --nav-h: var(--header-height-mobile, 64px);
  }

  /* Hero section stacks on mobile */
  .latex-hero {
    flex-direction: column;
    padding: 8px 12px;
    gap: 6px;
    text-align: center;
  }

  .latex-hero-content { min-width: 0; }
  .latex-hero-title { font-size: 14px; }
  .latex-hero-desc { font-size: 10px; }

  /* File tree — slide-in drawer on mobile */
  .filetree {
    display: none;
    position: fixed; top: 0; left: 0; bottom: 0;
    width: 260px; z-index: 200;
    background: var(--surface);
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .filetree.drawer-open {
    display: flex;
    transform: translateX(0);
  }
  .filetree-backdrop {
    display: none;
    position: fixed; inset: 0; z-index: 199;
    background: rgba(0,0,0,0.4);
  }
  .filetree-backdrop.visible { display: block; }

  /* Three-dot menu always visible on mobile (no hover) */
  .file-actions-btn { display: block !important; }

  /* Larger tap targets */
  .file-item { padding: 10px 12px 10px 16px; font-size: 13px; }
  .file-ctx-item { padding: 12px 16px; font-size: 14px; }
  .filetree-actions span { font-size: 22px; padding: 6px; }

  /* Stack editor and preview — show one at a time */
  .latex-app .main { flex-direction: column; }

  .editor-pane {
    flex: 1;
    border-right: none;
  }

  .preview-pane {
    width: 100%;
    display: none;
    flex: 1;
  }

  /* When showing preview, swap panes */
  .latex-app.show-preview .editor-pane { display: none; }
  .latex-app.show-preview .preview-pane { display: flex; }

  /* Show the mobile toggle button in toolbar */
  .mobile-toggle {
    display: flex !important;
    margin-left: auto;
    background: var(--accent) !important;
    color: #0e0f14 !important;
    font-weight: 600;
    padding: 0 12px;
  }

  /* Floating action button for mobile — always visible above log/statusbar */
  .mobile-fab {
    display: flex;
    position: fixed;
    bottom: 80px;
    right: 16px;
    z-index: 30;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--accent);
    color: #0e0f14;
    border: none;
    box-shadow: 0 4px 16px rgba(0,0,0,0.35);
    align-items: center;
    justify-content: center;
    font-size: 22px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
  }

  .mobile-fab:active {
    transform: scale(0.92);
  }

  .mobile-fab .fab-label {
    position: absolute;
    right: 64px;
    background: var(--surface2);
    color: var(--text);
    font-size: 11px;
    font-family: 'DM Sans', sans-serif;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 6px;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    pointer-events: none;
    opacity: 1;
    transition: opacity 0.5s;
  }

  .mobile-fab .fab-label.hidden { opacity: 0; }

  /* Pulse animation on FAB after compile */
  .mobile-fab.pulse {
    animation: fabPulse 1.5s ease-in-out 3;
  }

  @keyframes fabPulse {
    0%, 100% { box-shadow: 0 4px 16px rgba(0,0,0,0.35); }
    50%      { box-shadow: 0 4px 24px rgba(110,231,183,0.6); transform: scale(1.08); }
  }

  /* Collapse toolbar — hide less essential groups */
  .toolbar { flex-wrap: wrap; height: auto; min-height: 44px; padding: 4px 8px; gap: 4px; }
  .toolbar-group:nth-child(4) { display: none; } /* formatting buttons */
  .tb-shortcut { display: none; }
  .tb-btn { min-height: 38px; font-size: 13px; padding: 0 10px; }
  .tb-select { min-height: 38px; font-size: 13px; }

  /* AI: hide rewrite on mobile, keep AI prompt */
  #btn-ai-rewrite { display: none; }

  /* Preview toolbar compact */
  .preview-toolbar { flex-wrap: wrap; height: auto; min-height: 38px; padding: 4px 8px; }
  .preview-btn { min-height: 36px; min-width: 36px; font-size: 13px; }

  /* Editing indicator — larger back button */
  .editing-indicator { font-size: 13px; padding: 6px 12px; }
  .ei-back { padding: 8px 14px; font-size: 13px; }

  /* Log panel */
  .log-panel { height: 100px; }
  .log-panel.collapsed { height: 38px; }
  .log-header { min-height: 38px; }
  .log-line { font-size: 12px; }
  .log-title, .log-badge, .log-toggle { font-size: 12px; }

  /* Status bar */
  .sb-item { font-size: 12px; }

  /* Diff review buttons */
  .ai-diff-accept, .ai-diff-reject { padding: 10px 18px; font-size: 13px; }
  .ai-diff-hint { font-size: 11px; }

  /* Selection popup — stack vertically */
  .sel-popup {
    flex-wrap: wrap; max-width: 280px;
    padding: 6px;
  }
  .sel-popup button { padding: 8px 12px; font-size: 13px; }
  .sel-lang-picker { width: 42px; font-size: 12px; }

  /* Symbol picker */
  .symbol-picker { left: 8px !important; right: 8px; width: auto !important; max-width: 95vw; bottom: 120px; }
  .sp-tab { font-size: 12px; padding: 8px; }

  /* AI prompt — bottom sheet */
  .ai-prompt-popup {
    top: auto !important; left: 0 !important; right: 0;
    bottom: 0; transform: none !important;
    width: 100% !important; max-width: 100vw !important;
    border-radius: 12px 12px 0 0;
  }
  .ai-prompt-close { width: 38px; height: 38px; font-size: 20px; }

  /* Toast buttons */
  .upload-insert-btn { padding: 8px 14px; font-size: 13px; }

  /* Mobile files drawer button */
  .mobile-files-btn {
    display: flex !important;
    align-items: center; gap: 4px;
  }

  /* Mobile FAB safe area */
  .mobile-fab { bottom: calc(80px + env(safe-area-inset-bottom, 0px)); }
}
