:root { 
  --bg:#ffffff; --muted:#f6f6f6; --line:#e8e8e8; --ink:#111; --sub:#555; --meta-w: 380px; 
  --editor-font-size: 14px;
  --editor-font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
}

/* Font family options */
[data-font-family="mono"] { --editor-font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace; }
[data-font-family="serif"] { --editor-font-family: Georgia, 'Times New Roman', Times, serif; }
[data-font-family="sans-serif"] { --editor-font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }

/* Dark mode variables */
[data-theme="dark"] {
  --bg:#1a1a1a; --muted:#2d2d2d; --line:#404040; --ink:#e0e0e0; --sub:#aaa;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }

/* App shell: header • main • footer */
.app { display: grid; grid-template-rows: auto 1fr auto; height: 100vh; }
header, #app-footer { background: var(--muted); border-bottom: 1px solid var(--line); }

/* Toolbar */
.toolbar { padding: 10px 14px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar .row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar button, .toolbar select { padding: 8px 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); border-radius: 10px; cursor: pointer; }
.toolbar button[disabled] { opacity: 0.5; cursor: not-allowed; }
.toolbar input[type="text"] { flex: 1; min-width: 220px; padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); }

/* File list dropdown */
#list-panel { max-height: 40vh; overflow: auto; border-top: 1px solid var(--line); background: var(--bg); color: var(--ink); display: none; }
#list-panel table { width: 100%; border-collapse: collapse; font-size: 13px; }
#list-panel th, #list-panel td { padding: 8px 12px; border-bottom: 1px solid var(--line); }
#list-panel tr:hover { background: var(--muted); }

/* Main editor/preview area */
main.main { display: grid; grid-template-columns: 1fr; height: 100%; min-height: 0; width: 100%; margin: 0 auto; }
#editor-wrap, #preview { min-height: 0; }
#editor { 
  width: 100%; height: 100%; padding: 16px; border: none; outline: none; resize: none;
  font: var(--editor-font-size, 14px)/1.6 var(--editor-font-family, 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace);
  max-width: none; background: var(--bg); color: var(--ink);
  /* Better mobile experience */
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-user-select: text;
  user-select: text;
  /* Better touch handling on mobile */
  touch-action: manipulation;
  /* Prevent zoom on iOS */
  font-size: max(16px, var(--editor-font-size, 14px));
}

/* Mobile-specific improvements */
@media (max-width: 768px) {
  #editor {
    /* Ensure minimum font size to prevent zoom on iOS */
    font-size: 16px;
    line-height: 1.5;
    /* Better spacing on mobile */
    padding: 20px 16px;
  }
}

/* Focus styles for better mobile UX */
#editor:focus {
  outline: none;
  /* Subtle focus indication */
  box-shadow: inset 0 0 0 2px rgba(0, 123, 255, 0.25);
}
#preview { display:none; padding: 18px; overflow:auto; background: var(--bg); color: var(--ink); border-left: 1px solid var(--line); }

/* Responsive text area constraints */
@media (min-width: 768px) {
  main.main { max-width: 1200px; margin: 0 auto; padding: 0 20px; }
  /* Add comfortable reading width for editor in non-split mode */
  body:not([data-mode="split"]) #editor { max-width: 800px; margin: 0 auto; }
}
@media (min-width: 1200px) {
  main.main { max-width: 1600px; padding: 0 40px; }
  body:not([data-mode="split"]) #editor { max-width: 900px; margin: 0 auto; }
}
@media (min-width: 1400px) {
  main.main { max-width: 1800px; padding: 0 60px; }
  body:not([data-mode="split"]) #editor { max-width: 1000px; margin: 0 auto; }
}

/* Split / Preview-only modes */
body[data-mode="split"] main.main { grid-template-columns: 1fr 1fr; }
body[data-mode="split"] #preview { display:block; }
body[data-mode="preview"] #editor-wrap { display:none; }
body[data-mode="preview"] #preview { display:block; }

/* Preview typography */
#preview h1, #preview h2, #preview h3, #preview h4, #preview h5, #preview h6 { margin: 1.2em 0 0.5em; line-height:1.2; }
#preview h1 { font-size: 1.6rem; }
#preview h2 { font-size: 1.4rem; }
#preview h3 { font-size: 1.25rem; }
#preview p { margin: 0.5em 0; }
#preview ul { margin: 0.5em 0 0.5em 1.2em; }
#preview li { margin: 0.25em 0; }

.pill { display: inline-block; padding: 3px 10px; border-radius: 999px; background: #eee; font-size: 12px; }
a { color: inherit; }
code.kbd { padding: 2px 6px; border: 1px solid var(--line); border-radius: 6px; background: #fbfbfb; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
.dot { width:8px; height:8px; border-radius:999px; background:#bbb; display:inline-block; }
.dot.saving { background:#d69e2e; }
.dot.saved { background:#38a169; }
.dot.idle { background:#bbb; }

/* ========================= */
/* Side Metadata Panel (Drawer) */
/* ========================= */
#meta-panel {
  position: fixed;
  top: 0; right: 0;
  height: 100vh; width: var(--meta-w);
  background: var(--bg);
  border-left: 1px solid var(--line);
  box-shadow: -8px 0 24px rgba(0,0,0,0.06);
  transform: translateX(100%);
  transition: transform 220ms ease;
  z-index: 50;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
}
#meta-panel[data-open="1"] { transform: translateX(0); }

/* Panel header */
#meta-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--muted); }
#meta-title { font-size: 13px; color: var(--ink); font-weight: 600; }
#meta-close, #meta-toggle, #meta-save-now { padding: 6px 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); border-radius: 8px; cursor: pointer; }

/* Panel tabs */
#meta-tabs {
  display: flex;
  border-bottom: 1px solid var(--line);
  background: var(--muted);
}
.meta-tab-btn {
  flex: 1 0 auto;
  padding: 6px 10px;
  border: none;
  background: transparent;
  font: 11px/1.3 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-weight: 500;
  color: var(--sub);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 150ms ease;
}
.meta-tab-btn:hover {
  background: var(--muted);
  color: var(--ink);
}
.meta-tab-btn.active {
  background: var(--bg);
  color: var(--ink);
  border-bottom-color: #4a90e2;
  font-weight: 600;
}

/* Panel body */
#meta-body { padding: 10px 12px; overflow: auto; position: relative; }
.tab-content {
  display: none;
}
.tab-content.active {
  display: grid;
  gap: 10px;
}
#meta-body .row { display:grid; grid-template-columns: 1fr; gap: 6px; }
#meta-body label { font-size: 12px; color: var(--ink); }
#meta-body textarea { width: 100%; min-height: 100px; max-height: 45vh; padding: 8px; resize: vertical; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
#meta-body input[type="password"], #meta-body input[type="number"], #meta-body select { width: 100%; padding: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); font: 13px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
#meta-body input[type="range"] { width: 100%; }
#meta-body input[type="password"] { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
#meta-body button { padding: 10px 12px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); border-radius: 8px; cursor: pointer; font: 13px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; font-weight: 600; }
#meta-body button:hover:not(:disabled) { background: var(--muted); }
#meta-body button:disabled { opacity: 0.5; cursor: not-allowed; }
#meta-body button.generating { background: #f0f8ff; border-color: #4a90e2; }
#next-enable-wrap { display:flex; align-items:center; justify-content:flex-end; gap: 8px; }
.range-value { font-size: 12px; color: var(--sub); margin-left: 8px; }

/* Panel footer */
#meta-foot { padding: 8px 12px; border-top: 1px solid var(--line); font-size:12px; color: var(--sub); display:flex; justify-content: space-between; align-items:center; gap: 8px; }

/* Scrim behind drawer */
#meta-scrim {
  position: fixed; inset: 0; background: rgba(0,0,0,0.18);
  opacity: 0; pointer-events: none; transition: opacity 200ms ease; z-index: 40;
}
#meta-scrim[data-open="1"] { opacity: 1; pointer-events: auto; }

/* Side tab button */
#meta-tab[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }

/* ========================= */
/* File Modal */
/* ========================= */
#file-modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
#file-modal[data-open="1"] { display: flex; }

#file-modal-content {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  width: 90vw;
  max-width: 500px;
  max-height: 80vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

#file-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--muted);
  border-radius: 12px 12px 0 0;
}

#file-modal-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
}

#file-modal-close {
  padding: 8px;
  border: none;
  background: none;
  color: var(--sub);
  cursor: pointer;
  border-radius: 6px;
  font-size: 18px;
}

#file-modal-close:hover {
  background: var(--line);
  color: var(--ink);
}

#file-modal-body {
  padding: 20px;
  overflow-y: auto;
}

.file-action {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

.file-action button {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
}

.file-action button:hover:not(:disabled) {
  background: var(--muted);
}

.file-action button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#file-list-section {
  border-top: 1px solid var(--line);
  padding-top: 16px;
  margin-top: 16px;
}

#file-list-section h3 {
  margin: 0 0 12px 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
}

/* Responsive niceties */
@media (max-width: 480px) {
  :root { --meta-w: 92vw; }
  /* Hide Split button on mobile */
  #toggle-split { display: none; }
  
  /* Hide the floating settings button on mobile */
  #meta-tab { 
    display: none;
  }
  
  /* Show mobile settings button */
  .mobile-only {
    display: inline-block;
  }
  
  /* Move Generate button to bottom left on mobile to avoid covering settings button */
  #fixed-generate-btn { left: 20px; right: auto; }
}

/* Desktop styles for settings button - keep original positioning */
@media (min-width: 481px) {
  /* Hide mobile-only elements on desktop */
  .mobile-only {
    display: none;
  }
  
  #meta-tab {
    position: fixed; 
    right: 0; 
    top: 50%; 
    transform: translate(50%, -50%);
    writing-mode: vertical-rl; 
    text-orientation: mixed;
    background: var(--bg); 
    color: var(--ink); 
    border: 1px solid var(--line); 
    border-right: none; 
    border-radius: 10px 0 0 10px;
    padding: 10px 8px; 
    font-size: 12px; 
    cursor: pointer; 
    z-index: 60;
    box-shadow: -4px 4px 12px rgba(0,0,0,0.06);
  }
}

/* ========================= */
/* Generate Button - New Positioning */
/* ========================= */
#fixed-generate-btn {
  position: fixed;
  bottom: 80px; /* Always positioned above footer */
  right: 20px;
  padding: 12px 24px;
  background: #4a90e2;
  color: white;
  border: none;
  border-radius: 12px;
  font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(74, 144, 226, 0.3);
  transition: all 200ms ease;
  z-index: 100;
  display: none;
}

#fixed-generate-btn:hover:not(:disabled) {
  background: #357abd;
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(74, 144, 226, 0.4);
}

#fixed-generate-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
  transform: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}

#fixed-generate-btn.generating {
  background: #f0c14b;
  color: #333;
}

#fixed-generate-btn.visible {
  display: block;
}

/* Adjust generate button position based on footer content */
@media (max-width: 768px) {
  #fixed-generate-btn {
    left: 20px;
    right: auto;
    bottom: 20px; /* Start at very bottom of screen */
    transition: bottom 0.3s ease;
  }
  
  /* When pagination is active, move button higher */
  body[data-pagination-active="true"] #fixed-generate-btn {
    bottom: 80px; /* Position above footer with pagination */
  }
  
  /* When user has scrolled to footer, move button up */
  body[data-footer-visible="true"] #fixed-generate-btn {
    bottom: 80px; /* Position above footer when footer is visible */
  }
}

@media (min-width: 769px) {
  /* When pagination is active on desktop, adjust slightly */
  body[data-pagination-active="true"] #fixed-generate-btn {
    bottom: 120px;
  }
}

/* Cancel button */
#cancel-generate-btn {
  position: fixed;
  bottom: 80px; /* Positioned above footer, next to generate button */
  right: 180px;
  padding: 8px 16px;
  background: #dc3545;
  color: white;
  border: none;
  border-radius: 8px;
  font: 12px/1.4 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(220, 53, 69, 0.3);
  transition: all 200ms ease;
  z-index: 100;
  display: none;
}

#cancel-generate-btn:hover {
  background: #c82333;
  transform: translateY(-1px);
}

#cancel-generate-btn.visible {
  display: block;
}

#undo-generate-btn {
  position: fixed;
  bottom: 80px; /* Positioned above footer, next to cancel button */
  right: 340px; /* Further left from cancel button */
  padding: 8px 16px;
  background: #6c757d;
  color: white;
  border: none;
  border-radius: 8px;
  font: 12px/1.4 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(108, 117, 125, 0.3);
  transition: all 200ms ease;
  z-index: 100;
  display: none;
}

#undo-generate-btn:hover {
  background: #5a6268;
  transform: translateY(-1px);
}

#undo-generate-btn.visible {
  display: block;
}

@media (max-width: 768px) {
  #cancel-generate-btn {
    left: 180px;
    right: auto;
    bottom: 80px;
  }
  
  /* Hide fixed undo button on mobile - header button takes precedence */
  #undo-generate-btn {
    display: none !important;
  }
  
  body[data-pagination-active="true"] #cancel-generate-btn {
    bottom: 140px;
  }
}

@media (min-width: 769px) {
  body[data-pagination-active="true"] #cancel-generate-btn {
    bottom: 120px;
  }
  
  body[data-pagination-active="true"] #undo-generate-btn {
    bottom: 120px; /* Keep same level as cancel button */
  }
}

/* ========================= */
/* Story Dex Modal */
/* ========================= */
.story-dex-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  display: none;
}
.story-dex-modal[data-open="1"] {
  display: block;
}
.story-dex-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}
.story-dex-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bg);
  color: var(--ink);
  border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  width: 90%;
  max-width: 800px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.story-dex-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid var(--line);
}
.story-dex-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}
.story-dex-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}
.story-dex-close:hover {
  background: var(--muted);
}
.story-dex-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
.story-dex-controls {
  margin-bottom: 20px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.add-dex-entry, .export-dex, .import-dex {
  position: sticky;
  top: 0;
  background: #007bff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  box-shadow: 0 2px 4px rgba(0, 123, 255, 0.3);
  z-index: 10;
}
.add-dex-entry:hover, .export-dex:hover, .import-dex:hover {
  background: #0056b3;
}
.export-dex {
  background: #28a745;
  box-shadow: 0 2px 4px rgba(40, 167, 69, 0.3);
}
.export-dex:hover {
  background: #1e7e34;
}
.import-dex {
  background: #6c757d;
  box-shadow: 0 2px 4px rgba(108, 117, 125, 0.3);
}
.import-dex:hover {
  background: #545b62;
}
.story-dex-entries {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

/* Two-column layout on desktop */
@media (min-width: 768px) {
  .story-dex-entries {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
  }
}
.dex-entry {
  background: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 15px;
}
.dex-entry-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 8px;
}
.remove-dex-entry:hover {
  background: #c82333 !important;
}
.dex-entry-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.dex-entry-name {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 14px;
  font-weight: 600;
  background: var(--bg);
  color: var(--ink);
}
.remove-dex-entry {
  background: #dc3545;
  color: white;
  border: none;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  margin-left: 10px;
}
.remove-dex-entry:hover {
  background: #c82333;
}
.dex-entry-description {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 8px;
  margin-bottom: 10px;
  min-height: 100px;
  resize: vertical;
  font-family: inherit;
  font-size: 14px;
  background: var(--bg);
  color: var(--ink);
}
.dex-entry-keywords {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 14px;
  background: var(--bg);
  color: var(--ink);
}
.dex-entry label {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  color: var(--sub);
  font-weight: 500;
}

/* ========================= */
/* Story Dex Generator Modal */
/* ========================= */
.story-dex-generator-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1100; /* Higher than story-dex-modal (1000) to layer on top */
  display: none;
}
.story-dex-generator-modal[data-open="1"] {
  display: block;
}
.story-dex-generator-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6); /* Slightly darker to distinguish from underlying modal */
}
.story-dex-generator-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bg);
  color: var(--ink);
  border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.2), 0 10px 10px -5px rgba(0, 0, 0, 0.1);
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.story-dex-generator-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid var(--line);
}
.story-dex-generator-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}
.story-dex-generator-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--ink);
}
.story-dex-generator-close:hover {
  background: var(--muted);
}
.story-dex-generator-body {
  flex: 1;
  overflow-y: auto;
}
.generate-dex-entry {
  background: #28a745;
  color: white;
  border: none;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  margin-left: 10px;
}
.generate-dex-entry:hover {
  background: #218838;
}

/* ========================= */
/* Preview Input Modal */
/* ========================= */
.preview-input-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  display: none;
}
.preview-input-modal[data-open="1"] {
  display: block;
}
.preview-input-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}
.preview-input-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  width: 90%;
  max-width: 800px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.preview-input-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid var(--line);
}
.preview-input-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}
.preview-input-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}
.preview-input-copy {
  padding: 8px 14px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}
.preview-input-copy:hover {
  background: var(--muted);
}
.preview-input-copy:disabled,
.preview-input-copy[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.6;
}
.preview-input-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--sub);
}
.preview-input-close:hover {
  background: var(--muted);
  color: var(--ink);
}
.preview-input-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
.preview-input-info {
  margin-bottom: 15px;
  font-size: 14px;
  color: var(--sub);
  padding: 12px;
  background: var(--muted);
  border-radius: 6px;
}
.preview-input-text {
  background: #f8f9fa;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 50vh;
  overflow-y: auto;
}


  }
  .page-progress {
    order: 3;
    flex-basis: 100%;
    margin: 8px 0 0 0;
    max-width: none;
  }
}

/* ========================= */
/* New Footer Design */
/* ========================= */
#app-footer {
  background: var(--muted);
  border-top: 1px solid var(--line);
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
  position: relative;
  z-index: 10;
}

#footer-console {
  padding: 10px 14px;
  font-size: 12px;
  color: var(--sub);
  display: flex;
  gap: 10px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  width: 100%;
  min-width: 0;
}

/* Mobile footer console improvements */
@media (max-width: 768px) {
  #footer-console {
    height: 36px; /* Fixed single line height */
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    padding: 8px 14px;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
    overscroll-behavior-inline: contain;
  }

  #footer-console > * {
    flex: 0 0 auto;
  }
  
  #footer-console::-webkit-scrollbar {
    height: 4px;
  }
  
  #footer-console::-webkit-scrollbar-track {
    background: var(--muted);
  }
  
  #footer-console::-webkit-scrollbar-thumb {
    background: var(--line);
    border-radius: 2px;
  }
  
  /* Add bottom spacing to editor on mobile to prevent AI button overlap */
  #editor {
    padding-bottom: 60px;
  }
  
  /* Hide controls when mobile settings are enabled */
  body[data-hide-markdown-controls="true"] #formatting-row {
    display: none;
  }
  
  body[data-hide-footer-console="true"] #footer-console {
    display: none;
  }
  
  /* Ensure pagination controls remain accessible on mobile with minimal content */
  body[data-pagination-active="true"] main.main {
    min-height: calc(100vh - 120px); /* Account for header and footer space */
  }
}

#footer-pagination {
  padding: 12px 16px;
  background: var(--muted);
  border-top: 1px solid var(--line);
}

/* Pagination Controls */
.pagination-nav {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.pagination-nav button {
  padding: 6px 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

.pagination-nav button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pagination-nav button:hover:not(:disabled) {
  background: var(--line);
}

#page-info {
  font-weight: 500;
  color: var(--ink);
  background: transparent;
  border: none;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 200ms ease;
}

#page-info:hover {
  background: var(--line);
}

#page-info:active {
  background: var(--muted);
}

.pagination-progress {
  display: flex;
  align-items: center;
  gap: 12px;
}

.page-progress-bar {
  flex: 1;
  height: 4px;
  background: var(--line);
  border-radius: 2px;
  overflow: hidden;
}

.page-progress-fill {
  height: 100%;
  background: #4a90e2;
  border-radius: 2px;
  transition: width 200ms ease;
}

#page-details {
  color: var(--sub);
  font-size: 11px;
  min-width: 80px;
  text-align: right;
}

/* Mobile footer adjustments */
@media (max-width: 768px) {
  #footer-pagination {
    padding: 10px 12px;
  }

  .pagination-nav {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
  }

  .pagination-nav button {
    flex: 0 0 auto;
    min-width: 70px;
    padding: 6px 8px;
    font-size: 12px;
  }

  #page-info {
    flex: 0 0 auto;
    text-align: center;
    font-size: 12px;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    background: transparent;
    border: none;
    padding: 6px 8px;
  }
}