/* ─── Theme Variables ─── */
[data-theme="dark"] {
  --bg: #0c0e16;
  --surface: #141620;
  --surface-raised: #1b1d2a;
  --surface-hover: #242738;
  --border: #2c3048;
  --border-subtle: #21243a;
  --text: #cbd0e0;
  --text-secondary: #838898;
  --text-muted: #4c5068;
  --accent: #22d3ee;
  --accent-hover: #3de0f5;
  --accent-dim: #19b5cc;
  --accent-bg: rgba(34, 211, 238, 0.08);
  --accent-border: rgba(34, 211, 238, 0.22);
  --error: #e55565;
  --error-bg: rgba(229, 85, 101, 0.08);
  --success: #50c880;
  --success-bg: rgba(80, 200, 128, 0.08);
  --warning: #e8b84a;
  --info: #5a9ef5;
  --topbar-bg: #12141e;
  --scrollbar-thumb: #2c3048;
  --scrollbar-track: transparent;
  --cm-bg: #141620;
  --preview-bg: #141620;
  --code-bg: #1b1d2a;
  --blockquote-border: #22d3ee;
  --table-stripe: rgba(34, 211, 238, 0.04);
  --table-border: #2c3048;
  --link-color: #22d3ee;
}

[data-theme="light"] {
  --bg: #e8e9f0;
  --surface: #ffffff;
  --surface-raised: #f3f3f8;
  --surface-hover: #eaebf2;
  --border: #d6d8e0;
  --border-subtle: #e2e3ec;
  --text: #1c1e24;
  --text-secondary: #4a4d58;
  --text-muted: #8c8f98;
  --accent: #0891b2;
  --accent-hover: #0e7490;
  --accent-dim: #06b6d4;
  --accent-bg: rgba(8, 145, 178, 0.06);
  --accent-border: rgba(8, 145, 178, 0.2);
  --error: #c8303e;
  --error-bg: rgba(200, 48, 62, 0.06);
  --success: #1e8a50;
  --success-bg: rgba(30, 138, 80, 0.06);
  --warning: #b88a18;
  --info: #2060c0;
  --topbar-bg: #ffffff;
  --scrollbar-thumb: #c2c4cc;
  --scrollbar-track: transparent;
  --cm-bg: #ffffff;
  --preview-bg: #ffffff;
  --code-bg: #f3f3f8;
  --blockquote-border: #0891b2;
  --table-stripe: rgba(8, 145, 178, 0.04);
  --table-border: #d6d8e0;
  --link-color: #0891b2;
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

/* ─── App Layout ─── */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ─── Top Bar ─── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 46px;
  padding: 0 16px;
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 8px rgba(0, 0, 0, 0.12);
  flex-shrink: 0;
  z-index: 10;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-logo {
  font-family: 'JetBrains Mono', monospace;
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
  letter-spacing: -0.03em;
  text-shadow: 0 0 12px var(--accent-bg);
}

.app-title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.app-subtitle {
  font-size: 11px;
  color: var(--text-muted);
  padding-left: 10px;
  border-left: 1px solid var(--border);
}

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

.privacy-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--accent);
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  border-radius: 20px;
  cursor: help;
}

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: none;
  border-radius: 6px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: #0c0e16;
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-hover); }

[data-theme="light"] .btn-primary {
  color: #ffffff;
}

.btn-secondary {
  background: var(--surface-raised);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover {
  background: var(--surface-hover);
  border-color: var(--accent-border);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}
.btn-ghost:hover {
  background: var(--surface-raised);
  color: var(--text);
}

.btn-sm { padding: 4px 10px; font-size: 11px; }

/* ─── Theme Toggle ─── */
[data-theme="dark"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }

/* ─── Export Dropdown ─── */
.export-dropdown-wrap {
  position: relative;
}

.export-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 160px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  z-index: 50;
  overflow: hidden;
}

.export-dropdown.visible {
  display: block;
}

.export-option {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 14px;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s;
}

.export-option:hover {
  background: var(--surface-hover);
}

.export-ext {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 11px;
  color: var(--accent);
  min-width: 40px;
}

.export-label {
  color: var(--text-secondary);
}

/* ─── Formatting Toolbar ─── */
.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 38px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
  overflow-x: auto;
}

.toolbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  height: 28px;
  padding: 0 6px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
}

.toolbar-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.toolbar-btn:active {
  background: var(--accent-bg);
  color: var(--accent);
}

.toolbar-divider {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 4px;
  flex-shrink: 0;
}

/* ─── Split Pane Layout ─── */
.editor-layout {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.editor-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.preview-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 32px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.panel-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.panel-stats {
  font-size: 10px;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* ─── Resize Handle ─── */
.resize-handle {
  width: 6px;
  background: var(--border);
  cursor: col-resize;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  position: relative;
}

.resize-handle:hover,
.resize-handle.active {
  background: var(--accent);
}

.resize-grip {
  width: 2px;
  height: 24px;
  border-radius: 1px;
  background: var(--text-muted);
  opacity: 0.4;
}

.resize-handle:hover .resize-grip,
.resize-handle.active .resize-grip {
  opacity: 0;
}

/* ─── Editor Wrap (CodeMirror) ─── */
.editor-wrap {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.editor-wrap .CodeMirror {
  height: 100%;
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  line-height: 1.65;
  padding: 4px 0;
  background: var(--cm-bg);
}

.editor-wrap .CodeMirror-gutters {
  background: var(--surface-raised);
  border-right: 1px solid var(--border-subtle);
}

[data-theme="light"] .editor-wrap .CodeMirror {
  color: var(--text);
}

[data-theme="light"] .editor-wrap .CodeMirror-linenumber {
  color: var(--text-muted);
}

[data-theme="light"] .editor-wrap .CodeMirror-activeline-background {
  background: var(--accent-bg);
}

[data-theme="light"] .editor-wrap .CodeMirror-cursor {
  border-left-color: var(--text);
}

[data-theme="light"] .editor-wrap .CodeMirror-selected {
  background: rgba(8, 145, 178, 0.12);
}

[data-theme="light"] .editor-wrap .CodeMirror-gutters {
  background: var(--surface-raised);
}

/* CM dark theme overrides */
[data-theme="dark"] .editor-wrap .cm-s-material-darker .CodeMirror-cursor { border-left-color: var(--accent); }
[data-theme="dark"] .editor-wrap .cm-s-material-darker .CodeMirror-activeline-background { background: rgba(34, 211, 238, 0.04); }
[data-theme="dark"] .editor-wrap .cm-s-material-darker .CodeMirror-selected { background: rgba(34, 211, 238, 0.15); }
[data-theme="dark"] .editor-wrap .cm-s-material-darker .CodeMirror-linenumber { color: var(--text-muted); }

/* GFM mode syntax colors */
[data-theme="dark"] .editor-wrap .cm-header { color: #22d3ee; font-weight: 700; }
[data-theme="dark"] .editor-wrap .cm-strong { color: #f0f0f4; font-weight: 700; }
[data-theme="dark"] .editor-wrap .cm-em { color: #e0e0e8; font-style: italic; }
[data-theme="dark"] .editor-wrap .cm-link { color: #5a9ef5; }
[data-theme="dark"] .editor-wrap .cm-url { color: #50c898; }
[data-theme="dark"] .editor-wrap .cm-string { color: #50c898; }
[data-theme="dark"] .editor-wrap .cm-comment { color: #4c5068; }
[data-theme="dark"] .editor-wrap .cm-quote { color: #838898; font-style: italic; }
[data-theme="dark"] .editor-wrap .cm-tag { color: #e0709a; }
[data-theme="dark"] .editor-wrap .cm-attribute { color: #e8a44c; }

[data-theme="light"] .editor-wrap .cm-header { color: #0891b2; font-weight: 700; }
[data-theme="light"] .editor-wrap .cm-strong { color: #1c1e24; font-weight: 700; }
[data-theme="light"] .editor-wrap .cm-em { color: #2a2d38; font-style: italic; }
[data-theme="light"] .editor-wrap .cm-link { color: #2060c0; }
[data-theme="light"] .editor-wrap .cm-url { color: #1e8a50; }
[data-theme="light"] .editor-wrap .cm-string { color: #1e8a50; }
[data-theme="light"] .editor-wrap .cm-comment { color: #8c8f98; }
[data-theme="light"] .editor-wrap .cm-quote { color: #4a4d58; font-style: italic; }
[data-theme="light"] .editor-wrap .cm-tag { color: #c03068; }
[data-theme="light"] .editor-wrap .cm-attribute { color: #c07820; }

/* ─── Preview Wrap ─── */
.preview-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 24px 32px;
  background: var(--preview-bg);
}

/* ─── Markdown Body (Preview Styles) ─── */
.markdown-body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 15px;
  line-height: 1.7;
  color: var(--text);
  word-wrap: break-word;
}

.markdown-body > *:first-child { margin-top: 0; }
.markdown-body > *:last-child { margin-bottom: 0; }

.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  margin-top: 1.5em;
  margin-bottom: 0.6em;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.02em;
  color: var(--text);
}

.markdown-body h1 { font-size: 2em; border-bottom: 1px solid var(--border-subtle); padding-bottom: 0.3em; }
.markdown-body h2 { font-size: 1.5em; border-bottom: 1px solid var(--border-subtle); padding-bottom: 0.3em; }
.markdown-body h3 { font-size: 1.25em; }
.markdown-body h4 { font-size: 1em; }
.markdown-body h5 { font-size: 0.875em; }
.markdown-body h6 { font-size: 0.85em; color: var(--text-secondary); }

.markdown-body p { margin-bottom: 1em; }

.markdown-body a {
  color: var(--link-color);
  text-decoration: none;
}
.markdown-body a:hover { text-decoration: underline; }

.markdown-body strong { font-weight: 700; }
.markdown-body em { font-style: italic; }
.markdown-body del { text-decoration: line-through; color: var(--text-secondary); }

.markdown-body code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85em;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--code-bg);
  border: 1px solid var(--border-subtle);
}

.markdown-body pre {
  margin-bottom: 1em;
  padding: 16px;
  border-radius: 8px;
  background: var(--code-bg);
  border: 1px solid var(--border-subtle);
  overflow-x: auto;
}

.markdown-body pre code {
  padding: 0;
  border: none;
  background: transparent;
  font-size: 13px;
  line-height: 1.6;
}

.markdown-body blockquote {
  margin: 0 0 1em;
  padding: 0.5em 1em;
  border-left: 3px solid var(--blockquote-border);
  color: var(--text-secondary);
  background: var(--accent-bg);
  border-radius: 0 6px 6px 0;
}

.markdown-body blockquote p:last-child { margin-bottom: 0; }

.markdown-body ul, .markdown-body ol {
  margin-bottom: 1em;
  padding-left: 2em;
}

.markdown-body li { margin-bottom: 0.25em; }
.markdown-body li > ul, .markdown-body li > ol { margin-bottom: 0; }

/* Task lists */
.markdown-body ul.contains-task-list {
  list-style: none;
  padding-left: 0;
}

.markdown-body .task-list-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 2px 0;
}

.markdown-body .task-list-item input[type="checkbox"] {
  margin-top: 5px;
  accent-color: var(--accent);
}

.markdown-body table {
  width: 100%;
  margin-bottom: 1em;
  border-collapse: collapse;
  border: 1px solid var(--table-border);
  border-radius: 6px;
  overflow: hidden;
}

.markdown-body th, .markdown-body td {
  padding: 8px 12px;
  border: 1px solid var(--table-border);
  text-align: left;
}

.markdown-body th {
  font-weight: 600;
  background: var(--surface-raised);
}

.markdown-body tr:nth-child(even) {
  background: var(--table-stripe);
}

.markdown-body hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin: 2em 0;
}

.markdown-body img {
  max-width: 100%;
  border-radius: 8px;
}

/* Mermaid diagrams */
.markdown-body .mermaid-wrapper {
  margin-bottom: 1em;
  padding: 16px;
  border-radius: 8px;
  background: var(--code-bg);
  border: 1px solid var(--border-subtle);
  overflow-x: auto;
  text-align: center;
}

.markdown-body .mermaid-wrapper svg {
  max-width: 100%;
  height: auto;
}

.markdown-body .mermaid-error {
  margin-bottom: 1em;
  padding: 12px 16px;
  border-radius: 8px;
  background: var(--error-bg);
  border: 1px solid var(--error);
  color: var(--error);
  font-size: 13px;
  font-style: italic;
}

/* ─── Drop Overlay ─── */
.drop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 150;
}

.drop-overlay.visible {
  display: flex;
}

.drop-overlay-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 40px 60px;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: var(--surface);
  color: var(--accent);
  font-size: 16px;
  font-weight: 600;
}

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--surface-raised);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 18px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 200;
  pointer-events: none;
}

.toast.visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ─── Light Theme Depth ─── */
[data-theme="light"] .topbar {
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

[data-theme="light"] .toolbar {
  background: var(--surface);
}

[data-theme="light"] .panel-header {
  background: var(--surface);
}

[data-theme="light"] .editor-wrap .CodeMirror {
  background: var(--surface);
}

[data-theme="light"] .preview-wrap {
  background: var(--surface);
}

[data-theme="light"] .toast {
  background: #2a2c36;
  color: #e8e8ec;
  border-color: transparent;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

[data-theme="light"] .btn-secondary {
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

[data-theme="light"] .privacy-badge {
  background: rgba(8, 145, 178, 0.06);
  border-color: rgba(8, 145, 178, 0.18);
}

[data-theme="light"] .export-dropdown {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

/* ─── Fullscreen Preview ─── */
.icon-collapse { display: none; }
.preview-panel.fullscreen .icon-expand { display: none; }
.preview-panel.fullscreen .icon-collapse { display: block; }

.preview-panel.fullscreen {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  min-height: unset;
  max-height: none;
}

.preview-panel.fullscreen .panel-header {
  border-bottom: 1px solid var(--border);
}

.preview-panel.fullscreen .preview-wrap {
  height: calc(100vh - 32px);
  padding: 24px 32px;
}

/* ─── Responsive ─── */
@media (max-width: 768px) {
  .app-subtitle { display: none; }
  .privacy-badge span { display: none; }
  .toolbar { gap: 1px; padding: 0 8px; }
  .toolbar-btn { min-width: 26px; height: 26px; padding: 0 4px; font-size: 11px; }
  .preview-wrap { padding: 16px; }
  .preview-panel.fullscreen .preview-wrap { padding: 16px; }
}

@media (max-width: 560px) {
  .topbar { padding: 0 10px; }
  .topbar-right { gap: 4px; }
  .btn-text { display: none; }
  .export-chevron { display: none; }
  .btn { padding: 6px 8px; min-width: 36px; min-height: 36px; justify-content: center; }
  .btn-sm { min-width: unset; min-height: unset; }
  #btnThemeToggle {
    flex-shrink: 0;
    min-width: 36px;
    min-height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .editor-layout {
    flex-direction: column;
  }
  .resize-handle {
    width: auto;
    height: 6px;
    cursor: row-resize;
  }
  .resize-grip {
    width: 24px;
    height: 2px;
  }
  .editor-panel, .preview-panel {
    flex: 1;
    min-height: 200px;
  }
  .preview-panel.fullscreen {
    min-height: unset;
  }
}
