/* ═══════════════════════════════════════════════════════════════
   MD Preview — Styles
   A distraction-free, beautiful markdown editor
   ═══════════════════════════════════════════════════════════════ */

/* ─────────────────────────────────────────────────────────────────
   CSS Variables & Theming (Zinc/Slate Palette)
   ───────────────────────────────────────────────────────────────── */
:root {
  /* Light Theme (Zinc-based) */
  --bg-primary: #ffffff;
  --bg-secondary: #f4f4f5; /* Zinc-100 */
  --bg-tertiary: #e4e4e7; /* Zinc-200 */
  --bg-surface: #ffffff;

  --text-primary: #09090b; /* Zinc-950 */
  --text-secondary: #52525b; /* Zinc-600 */
  --text-muted: #a1a1aa; /* Zinc-400 */

  --border-color: #e4e4e7; /* Zinc-200 */
  --border-hover: #d4d4d8; /* Zinc-300 */

  --accent: #18181b; /* Zinc-950 (Black for high contrast) */
  --accent-muted: #71717a; /* Zinc-500 */
  --accent-glow: rgba(0, 0, 0, 0.05);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.02);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05);

  --code-bg: #f4f4f5;
  --blockquote-border: #d4d4d8;
  --link-color: #2563eb; /* A slight touch of blue for links is standard */

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

[data-theme='dark'] {
  /* Dark Theme (Zinc-based dark) */
  --bg-primary: #09090b; /* Zinc-950 */
  --bg-secondary: #09090b; /* Same as primary for cleaner flat look */
  --bg-tertiary: #18181b; /* Zinc-900 */
  --bg-surface: #18181b;

  --text-primary: #f4f4f5; /* Zinc-100 */
  --text-secondary: #a1a1aa; /* Zinc-400 */
  --text-muted: #52525b; /* Zinc-600 */

  --border-color: #27272a; /* Zinc-800 */
  --border-hover: #3f3f46; /* Zinc-700 */

  --accent: #fafafa; /* White accent */
  --accent-muted: #a1a1aa;
  --accent-glow: rgba(255, 255, 255, 0.05);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.5);

  --code-bg: #18181b;
  --blockquote-border: #3f3f46;
  --link-color: #60a5fa;
}

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

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

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  height: 100%;
  overflow: hidden; /* App-like feel */
  transition:
    background-color 0.3s ease,
    color 0.3s ease;
}

/* Accessibility helpers */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 2px solid var(--link-color);
  border-radius: 0.375rem;
  text-decoration: none;
  z-index: 999;
  opacity: 0;
  pointer-events: none;
}

.skip-link:focus {
  opacity: 1;
  pointer-events: auto;
}

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

/* ─────────────────────────────────────────────────────────────────
   Header (Minimal)
   ───────────────────────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.8rem 1.5rem;
  background: var(--bg-primary); /* Blend with body */
  border-bottom: 1px solid var(--border-color);
  z-index: 10;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo-icon {
  width: 20px;
  height: 20px;
  color: var(--text-primary);
}

.logo {
  font-family: 'Outfit', var(--font-sans);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
}

/* ─────────────────────────────────────────────────────────────────
   Buttons & Controls
   ───────────────────────────────────────────────────────────────── */
button {
  background: transparent;
  border: none;
  cursor: pointer;
  font-family: inherit;
  color: var(--text-secondary);
  transition: all 0.2s ease;
}

button:focus-visible,
.view-tab:focus-visible,
.collapse-btn:focus-visible,
.scroll-sync-toggle:focus-visible,
.theme-toggle:focus-visible,
.resize-handle:focus-visible {
  outline: 2px solid var(--link-color);
  outline-offset: 3px;
}

textarea:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--link-color);
}

/* Hide focus styles for mouse/touch interactions */
button:focus:not(:focus-visible),
.view-tab:focus:not(:focus-visible),
.collapse-btn:focus:not(:focus-visible),
.scroll-sync-toggle:focus:not(:focus-visible),
.theme-toggle:focus:not(:focus-visible),
.resize-handle:focus:not(:focus-visible),
textarea:focus:not(:focus-visible) {
  outline: none;
  box-shadow: none;
}

.theme-toggle,
.scroll-sync-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 0.375rem; /* sm radius */
  color: var(--text-secondary);
}

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

[data-theme='light'] .dark-icon,
[data-theme='dark'] .light-icon {
  display: none;
}

/* Scroll Sync Toggle */
.scroll-sync-toggle {
  position: relative;
}

.scroll-sync-toggle .unsynced-icon {
  display: none;
}

.scroll-sync-toggle.active {
  color: var(--link-color);
}

.scroll-sync-toggle.active:hover {
  background: rgba(37, 99, 235, 0.1);
}

[data-theme='dark'] .scroll-sync-toggle.active:hover {
  background: rgba(96, 165, 250, 0.15);
}

.scroll-sync-toggle:not(.active) .synced-icon {
  display: none;
}

.scroll-sync-toggle:not(.active) .unsynced-icon {
  display: block;
}

/* Hide scroll sync toggle on mobile */
@media (max-width: 768px) {
  .scroll-sync-toggle {
    display: none;
  }
}

/* ─────────────────────────────────────────────────────────────────
   Lint Toggle & Panel
   ───────────────────────────────────────────────────────────────── */
.lint-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  height: 2rem;
  padding: 0 0.5rem;
  border-radius: 0.375rem;
  color: var(--text-secondary);
  position: relative;
}

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

.lint-toggle.active {
  color: var(--text-primary);
}

.lint-toggle.active.has-warnings {
  color: #d97706; /* Amber-600 */
}

[data-theme='dark'] .lint-toggle.active.has-warnings {
  color: #fbbf24; /* Amber-400 */
}

.lint-toggle.active:not(.has-warnings) {
  color: #16a34a; /* Green-600 */
}

[data-theme='dark'] .lint-toggle.active:not(.has-warnings) {
  color: #4ade80; /* Green-400 */
}

.lint-count {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 600;
  min-width: 1rem;
  text-align: center;
}

.lint-count:empty {
  display: none;
}

/* Lint Panel */
.lint-panel {
  display: none;
  max-height: 120px;
  overflow-y: auto;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 0;
}

.lint-panel.visible {
  display: block;
}

.lint-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.lint-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 1rem;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 1px solid var(--border-color);
  transition: background-color 0.15s ease;
}

.lint-item:last-child {
  border-bottom: none;
}

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

.lint-item-icon {
  color: #d97706;
  flex-shrink: 0;
}

[data-theme='dark'] .lint-item-icon {
  color: #fbbf24;
}

.lint-item-line {
  color: var(--text-muted);
  white-space: nowrap;
}

.lint-item-message {
  color: var(--text-primary);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ─────────────────────────────────────────────────────────────────
   Main Container
   ───────────────────────────────────────────────────────────────── */
.editor-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
  min-height: 0; /* Allow inner panes to scroll */
}

.pane {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0; /* Prevent flex blowout */
  height: 100%;
  background: var(--bg-primary);
  min-height: 0; /* Prevent content from forcing the pane taller than viewport */
}

.field {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0; /* Let editor area shrink so textarea can scroll */
}

.editor-pane {
  flex: 0 0 50%;
  border-right: 1px solid var(--border-color);
}

/* ─────────────────────────────────────────────────────────────────
   Pane Headers (Subtle)
   ───────────────────────────────────────────────────────────────── */
.pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1.5rem;
  /* No background, just subtle text */
  flex-shrink: 0;
}

.pane-title {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

/* Collapse Button */
.collapse-btn {
  display: flex;
  width: 2rem;
  height: 2rem;
  padding: 0.25rem;
  align-items: center;
  justify-content: center;
  border-radius: 0.375rem;
}

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

.editor-pane.collapsed .collapse-icon {
  transform: rotate(180deg);
}

/* Collapsed State logic */
.editor-pane.collapsed {
  flex: 0 0 3.5rem !important; /* Narrow strip */
  min-width: 3.5rem !important;
  border-right: 1px solid var(--border-color);
  cursor: pointer; /* Entire pane is clickable */
  transition: background-color 0.15s ease;
}

.editor-pane.collapsed:hover {
  background: var(--bg-tertiary);
}

.editor-pane.collapsed .editor,
.editor-pane.collapsed .pane-title,
.editor-pane.collapsed .line-gutter,
.editor-pane.collapsed .field,
.editor-pane.collapsed .lint-panel {
  display: none;
}

.editor-pane.collapsed .pane-header {
  flex: 1; /* Fill remaining vertical space */
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0;
  height: 100%;
  pointer-events: none; /* Let clicks pass through to pane */
}

.editor-pane.collapsed .collapse-btn {
  margin: 0;
  pointer-events: none; /* Let clicks pass through to header */
}

/* ─────────────────────────────────────────────────────────────────
   Zen Mode (When Editor is Collapsed)
   ───────────────────────────────────────────────────────────────── */
.editor-pane.collapsed + .resize-handle {
  display: none;
}

/* Hide preview header for distraction-free reading */
.editor-pane.collapsed ~ .preview-pane .pane-header {
  display: none;
}

/* Hide sync/lint controls in Zen Mode */
.app.zen-mode .scroll-sync-toggle,
.app.zen-mode .lint-toggle {
  display: none;
}

/* Expand prose for wider reading experience */
.editor-pane.collapsed ~ .preview-pane .preview {
  padding: 3rem 4rem;
}

.editor-pane.collapsed ~ .preview-pane .prose {
  max-width: 90ch; /* Wider than default 65ch */
  font-size: 1.1rem; /* Larger text for easier reading */
  line-height: 1.85; /* More generous line height */
}

/* Larger headings in zen mode */
.editor-pane.collapsed ~ .preview-pane .prose h1 {
  font-size: 2.5rem;
}

.editor-pane.collapsed ~ .preview-pane .prose h2 {
  font-size: 1.75rem;
}

.editor-pane.collapsed ~ .preview-pane .prose h3 {
  font-size: 1.4rem;
}

/* More relaxed paragraph spacing */
.editor-pane.collapsed ~ .preview-pane .prose p {
  margin-bottom: 1.75em;
}

/* Resize Handle */
.resize-handle {
  width: 1px; /* Invisible functionality via hit area, visible line via border */
  background: transparent;
  cursor: col-resize;
  position: relative;
  z-index: 20;
  margin-left: -1px; /* Overlap border */
}

/* Create a larger invisible hit area */
.resize-handle::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: -4px;
  right: -4px;
  cursor: col-resize;
}

.resize-handle:hover,
.resize-handle.resizing {
  background: var(--accent); /* Show line on hover */
}

/* ─────────────────────────────────────────────────────────────────
   Editor Input with Line Gutter
   ───────────────────────────────────────────────────────────────── */
.editor-wrapper {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
  z-index: 1; /* Create stacking context */
  min-height: 0; /* Ensure child scrollers are allowed to scroll */
}

.line-gutter {
  flex-shrink: 0;
  width: 3.5rem;
  padding: 1rem 0.5rem 2rem 0.75rem;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.7;
  color: var(--text-muted);
  text-align: right;
  user-select: none;
  overflow-y: auto; /* Keep line numbers scrollable with the editor */
  overflow-x: visible; /* Allow tooltip to escape horizontally */
  position: relative;
  z-index: 10; /* Above editor content for tooltips */
}

.line-number {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.25rem;
  height: calc(0.9rem * 1.7); /* Match editor line-height */
}

.line-number-text {
  min-width: 1.5rem;
}

.line-error-icon {
  width: 14px;
  height: 14px;
  color: #d97706; /* Amber-600 */
  flex-shrink: 0;
  cursor: pointer;
  position: relative;
}

.line-error-icon svg {
  width: 100%;
  height: 100%;
  display: block;
}

[data-theme='dark'] .line-error-icon {
  color: #fbbf24; /* Amber-400 */
}

/* JS-controlled Tooltip (appended to body to escape overflow) */
.gutter-tooltip {
  position: fixed;
  padding: 8px 12px;
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 0.75rem;
  font-family: var(--font-mono);
  white-space: pre-line;
  max-width: 300px;
  z-index: 9999;
  box-shadow: var(--shadow-lg);
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.15s ease,
    visibility 0.15s ease;
}

.gutter-tooltip.visible {
  opacity: 1;
  visibility: visible;
}

.line-error-icon:focus {
  outline: none;
}

.line-number.has-error {
  color: #d97706;
}

[data-theme='dark'] .line-number.has-error {
  color: #fbbf24;
}

.editor {
  flex: 1;
  width: 100%;
  padding: 1rem 1.5rem 2rem 1rem;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 0.9rem;
  line-height: 1.7;
  resize: none;
  outline: none;
  overflow: auto; /* Explicitly enable scrolling for long documents */
  -webkit-overflow-scrolling: touch;
}

.editor::placeholder {
  color: var(--text-muted);
  opacity: 0.5;
}

/* ─────────────────────────────────────────────────────────────────
   Preview & Prose (Typography)
   ───────────────────────────────────────────────────────────────── */
.preview {
  flex: 1;
  padding: 1rem 2.5rem 2rem;
  overflow-y: auto;
  color: var(--text-primary);
  min-height: 0; /* Allow preview to scroll within the pane */
}

.prose {
  max-width: 65ch; /* Optimal reading width */
  margin: 0 auto;
}

.prose h1,
.prose h2,
.prose h3,
.prose h4 {
  font-family: var(--font-sans);
  color: var(--text-primary);
  margin-top: 2em;
  margin-bottom: 0.75em;
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -0.015em;
}

.prose h1 {
  font-size: 2rem;
}
.prose h2 {
  font-size: 1.5rem;
  margin-top: 2.5em;
  padding-bottom: 0.5em;
  border-bottom: 1px solid var(--border-color);
}
.prose h3 {
  font-size: 1.25rem;
}

.prose p {
  margin-bottom: 1.5em;
  line-height: 1.75;
}

.prose a {
  color: var(--link-color);
  text-decoration: none;
  border-bottom: 1px solid transparent;
}
.prose a:hover {
  border-bottom-color: var(--link-color);
}

.prose code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--code-bg);
  padding: 0.2rem 0.3rem;
  border-radius: 0.25rem;
  color: var(--text-primary);
}

.prose pre {
  background: var(--code-bg);
  padding: 1.25rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  margin: 1.5em 0;
  border: 1px solid var(--border-color);
}
.prose pre code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  color: inherit;
}

.prose blockquote {
  border-left: 3px solid var(--blockquote-border);
  padding-left: 1.25rem;
  margin: 1.5em 0;
  color: var(--text-secondary);
  font-style: italic;
}

.prose hr {
  border: 0;
  border-top: 1px solid var(--border-color);
  margin: 3rem 0;
}

.prose ul,
.prose ol {
  padding-left: 1.5rem;
  margin-bottom: 1.5em;
}

.prose li {
  margin-bottom: 0.5em;
}

.prose img {
  max-width: 100%;
  border-radius: 0.5rem;
  border: 1px solid var(--border-color);
}

/* ─────────────────────────────────────────────────────────────────
   Scrollbars (Minimal)
   ───────────────────────────────────────────────────────────────── */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 5px;
  border: 2px solid transparent; /* Padding trick */
  background-clip: content-box;
}
:hover::-webkit-scrollbar-thumb {
  background-color: var(--border-hover);
}

/* ─────────────────────────────────────────────────────────────────
   Mobile View Switcher
   ───────────────────────────────────────────────────────────────── */
.mobile-view-switcher {
  display: none;
}

.view-tab {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  border-radius: 0.375rem;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
}

.view-tab.active {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

@media (max-width: 768px) {
  .mobile-view-switcher {
    display: flex;
    background: var(--bg-secondary);
    padding: 0.15rem;
    border-radius: 0.5rem;
  }

  .editor-container {
    flex-direction: column;
  }

  .pane {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    opacity: 0;
    pointer-events: none;
    z-index: 0;
  }

  .pane.mobile-active {
    opacity: 1;
    pointer-events: auto;
    z-index: 1;
  }

  .editor-pane {
    border-right: none;
  }

  .resize-handle,
  .pane-header {
    display: none !important;
  }

  .prose {
    padding: 0 1rem;
    max-width: none;
  }
}
