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

:root {
  --bg: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-tertiary: #f1f3f5;
  --border: #e9ecef;
  --border-light: #f1f3f5;
  --text: #212529;
  --text-secondary: #868e96;
  --text-tertiary: #adb5bd;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: #eff6ff;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.08);
  --radius: 8px;
  --radius-sm: 6px;
  --transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --bg-secondary: #242424;
    --bg-tertiary: #2d2d2d;
    --border: #3a3a3a;
    --border-light: #2d2d2d;
    --text: #ededed;
    --text-secondary: #a1a1a1;
    --text-tertiary: #737373;
    --accent: #3b82f6;
    --accent-hover: #60a5fa;
    --accent-light: #1e3a5f;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.4);
  }
}

/* style.css - line 39 */
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg-secondary);
  color: var(--text);
  height: 100vh;
  overflow: hidden;

  /* --- ADD THESE 2 LINES --- */
  display: flex;
  flex-direction: column;
  /* ------------------------- */
}

/* style.css - line 47 */
.app {
  /* height: 100vh;  <-- DELETE OR COMMENT THIS OUT */

  /* --- ADD THIS INSTEAD --- */
  flex: 1;
  overflow: hidden;
  /* Ensures the app handles its own internal scrolling */
  /* ------------------------ */

  display: flex;
  flex-direction: column;
}

/* Header */
.header {
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
  flex: 1;
  min-width: 0;
}

.logo {
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.filename-wrapper {
  flex: 1;
  min-width: 0;
  max-width: 300px;
}

.filename {
  font-size: 14px;
  color: var(--text-secondary);
  padding: 6px 12px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  width: 100%;
  transition: all var(--transition);
  font-family: inherit;
}

.filename:hover {
  background: var(--bg-tertiary);
  border-color: var(--border);
}

.filename:focus {
  outline: none;
  background: var(--bg);
  border-color: var(--accent);
  color: var(--text);
}

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

.save-status {
  font-size: 13px;
  color: var(--text-tertiary);
  padding: 0 8px;
  white-space: nowrap;
}

.save-status.saved {
  color: #22c55e;
}

.btn {
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: all var(--transition);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

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

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-icon {
  font-size: 14px;
}

/* Dropdown */
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  z-index: 100;
  display: none;
}

.dropdown-menu.active {
  display: block;
}

.dropdown-item {
  padding: 10px 16px;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  gap: 8px;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
}

.dropdown-item:first-child {
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}

.dropdown-item:last-child {
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
}

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

/* Outline Sidebar */
.outline-sidebar {
  position: fixed;
  top: 0;
  right: 0;
  width: 280px;
  height: 100vh;
  background: var(--bg);
  border-left: 1px solid var(--border);
  z-index: 50;
  transform: translateX(100%);
  transition: transform var(--transition);
  display: flex;
  flex-direction: column;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.1);
}

.outline-sidebar.active {
  transform: translateX(0);
}

.outline-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.outline-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.outline-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  font-size: 18px;
  line-height: 1;
  transition: all var(--transition);
}

.outline-close:hover {
  background: var(--bg-tertiary);
  color: var(--text);
}

.outline-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0;
}

.outline-empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-tertiary);
  font-size: 13px;
}

.outline-item {
  padding: 8px 20px;
  cursor: pointer;
  transition: all var(--transition);
  font-size: 13px;
  color: var(--text-secondary);
  border-left: 3px solid transparent;
  display: block;
  text-align: left;
  word-wrap: break-word;
  line-height: 1.4;
}

.outline-item:hover {
  background: var(--bg-tertiary);
  color: var(--text);
  border-left-color: var(--accent);
}

.outline-item.active {
  color: var(--accent);
  background: var(--accent-light);
  border-left-color: var(--accent);
}

.outline-item.level-1 {
  padding-left: 20px;
  font-weight: 600;
  font-size: 14px;
}

.outline-item.level-2 {
  padding-left: 32px;
  font-weight: 500;
}

.outline-item.level-3 {
  padding-left: 44px;
}

.outline-item.level-4 {
  padding-left: 56px;
}

/* Cheatsheet Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: var(--bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-width: 800px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 24px;
  line-height: 1;
  transition: all var(--transition);
}

.modal-close:hover {
  background: var(--bg-tertiary);
  color: var(--text);
}

.modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

.cheat-section {
  margin-bottom: 32px;
}

.cheat-section:last-child {
  margin-bottom: 0;
}

.cheat-section-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--border);
}

.cheat-item {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.cheat-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-tertiary);
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.cheat-code {
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-tertiary);
  padding: 8px 10px;
  border-radius: 4px;
  border: 1px solid var(--border-light);
  white-space: pre-wrap;
  word-break: break-word;
}

.cheat-result {
  font-size: 13px;
  color: var(--text);
  padding: 8px 10px;
  line-height: 1.6;
}

.cheat-result strong {
  font-weight: 600;
}

.cheat-result em {
  font-style: italic;
}

.cheat-result del {
  text-decoration: line-through;
  opacity: 0.7;
}

.cheat-result code {
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 12px;
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 3px;
  border: 1px solid var(--border-light);
}

.cheat-note {
  font-size: 12px;
  color: var(--text-secondary);
  font-style: italic;
  margin-top: 8px;
}

.cheat-kbd {
  display: inline-block;
  padding: 2px 6px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 3px;
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 11px;
  font-weight: 600;
}

/* Toolbar */
.toolbar {
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.toolbar-group {
  display: flex;
  gap: 2px;
  padding: 0 8px;
  border-right: 1px solid var(--border-light);
}

.toolbar-group:last-child {
  border-right: none;
}

.toolbar-btn {
  padding: 6px 10px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: all var(--transition);
  position: relative;
}

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

.toolbar-spacer {
  flex: 1;
}

.view-toggle {
  display: flex;
  background: var(--bg-tertiary);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.view-toggle button {
  padding: 4px 12px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  border-radius: 4px;
  transition: all var(--transition);
}

.view-toggle button.active {
  background: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* Main content */
.main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
  min-height: 0;
  position: relative;
}

.main.editor-only {
  grid-template-columns: 1fr;
}

.pane {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  position: relative;
}

.pane:first-child {
  border-right: 1px solid var(--border);
}

/* FIX: Updated Pane Content 
  - Removed padding from parent (moved to children)
  - Removed generic overflow: auto (handled per pane)
*/
.pane-content {
  flex: 1;
  min-height: 0;
  /* Important for nested scroll */
  position: relative;
}

/* FIX: Editor Pane - No outer scrollbar */
#editor-pane .pane-content {
  overflow: hidden;
}

/* FIX: Preview Pane - Auto scroll */
#preview-pane .pane-content {
  overflow-y: auto;
}

/* Dividers */
.divider {
  position: absolute;
  top: 0;
  height: 100%;
  width: 12px;
  cursor: col-resize;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  touch-action: none;
  background: transparent;
  /* Ensure no grey chunk */
}

.divider::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 1px;
  background: var(--border);
  transition: background var(--transition);
}

.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 4px;
  height: 24px;
  background: var(--bg);
  /* Hide line behind handle */
  border: 1px solid var(--border);
  border-radius: 4px;
  transition: all var(--transition);
  z-index: 2;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.divider:hover::before,
.divider.dragging::before,
.divider:hover::after,
.divider.dragging::after {
  background: var(--accent);
}

.divider-horizontal {
  position: absolute;
  left: 0;
  right: 0;
  height: 8px;
  cursor: row-resize;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  user-select: none;
  touch-action: none;
}

.divider-horizontal::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 1px;
  background: var(--border);
  transition: background var(--transition);
}

.divider-horizontal::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 24px;
  height: 4px;
  background: var(--border);
  border-radius: 4px;
  transition: background var(--transition);
  z-index: 2;
}

.divider-horizontal:hover::before,
.divider-horizontal.dragging::before,
.divider-horizontal:hover::after,
.divider-horizontal.dragging::after {
  background: var(--accent);
}

.main.editor-only .divider,
.main.editor-only .divider-horizontal {
  display: none;
}

/* Find/Replace */
.find-replace {
  position: absolute;
  top: 12px;
  right: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow-lg);
  z-index: 10;
  min-width: 320px;
  display: none;
}

.find-replace.active {
  display: block;
}

.find-replace-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.find-replace-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.find-replace-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  font-size: 18px;
  line-height: 1;
}

.find-replace-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.find-replace-input {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
}

.find-replace-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg);
}

.find-replace-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}

.find-replace-btn:hover {
  background: var(--bg-tertiary);
}

.find-replace-count {
  font-size: 11px;
  color: var(--text-secondary);
  padding: 6px 10px;
  white-space: nowrap;
}

/* FIX: Editor styling 
  - Added padding here instead of parent
  - display: block prevents ghost space at bottom
*/
.editor {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, monospace;
  font-size: 14px;
  line-height: 1.6;
  tab-size: 2;
  word-wrap: break-word;
  display: block;
  padding: 24px 32px;
}

.editor::placeholder {
  color: var(--text-tertiary);
}

/* FIX: Preview styling 
  - Added padding here instead of parent
*/
.preview {
  line-height: 1.7;
  padding: 24px 32px;
}

.preview-empty {
  color: var(--text-tertiary);
  text-align: center;
  padding: 60px 20px;
}

.preview h1 {
  font-size: 28px;
  font-weight: 600;
  margin: 24px 0 16px;
  line-height: 1.3;
}

.preview h2 {
  font-size: 22px;
  font-weight: 600;
  margin: 20px 0 12px;
  line-height: 1.3;
}

.preview h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 16px 0 8px;
}

.preview p {
  margin: 12px 0;
}

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

.preview code {
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 13px;
  padding: 2px 6px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: 4px;
}

.preview pre {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  overflow-x: auto;
  margin: 16px 0;
}

.preview pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 13px;
  color: inherit;
}

.preview blockquote {
  margin: 16px 0;
  padding: 12px 20px;
  border-left: 3px solid var(--accent);
  background: var(--accent-light);
  color: var(--text);
}

.preview hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 24px 0;
}

.preview img {
  max-width: 100%;
  border-radius: 4px;
}

/* Tables */
.preview table {
  border-collapse: collapse;
  width: 100%;
  margin: 16px 0;
  border: 1px solid var(--border);
}

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

.preview table th {
  background: var(--bg-tertiary);
  font-weight: 600;
}

/* Footer */
.footer {
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 8px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
  flex-shrink: 0;
  position: relative;
  z-index: 50;
  /* Ensure footer sits on top */
}

.footer-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.hidden {
  display: none !important;
}

/* Responsive */
@media (max-width: 768px) {
  .main {
    grid-template-columns: 1fr !important;
    grid-template-rows: 1fr 1fr;
  }

  .main.editor-only {
    grid-template-rows: 1fr !important;
  }

  .pane:first-child {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .main.editor-only .pane:first-child {
    border-bottom: none;
  }

  .divider {
    display: none !important;
  }

  .divider-horizontal {
    display: flex !important;
  }

  .main.editor-only .divider-horizontal {
    display: none !important;
  }

  .header {
    flex-wrap: wrap;
  }

  .filename-wrapper {
    order: 3;
    width: 100%;
    max-width: none;
  }

  .toolbar {
    overflow-x: auto;
    flex-wrap: nowrap;
  }

  .btn>span:not(.btn-icon) {
    display: none;
  }

  .btn {
    padding: 7px 10px;
  }

  .dropdown-item span {
    display: inline !important;
  }

  .outline-sidebar {
    width: 100%;
    max-width: 280px;
  }

  .cheat-item {
    grid-template-columns: 1fr;
    gap: 12px;
  }
}