:root {
  --bg-primary: #1e1e1e;
  --bg-secondary: #252526;
  --bg-tertiary: #2d2d2d; /* For code blocks, etc */
  --text-primary: #c5c8c6;
  --text-secondary: #cccccc;
  --text-muted: #888888;
  --border-color: #3e3e42;
  --accent-color: #007acc;
  --button-bg: #0e639c;
  --button-hover: #1177bb;
  --code-block-bg: #2b2b2b;
  --input-bg: transparent;
  --scrollbar-track: #1e1e1e;
  --scrollbar-thumb: #444;
}

[data-theme="light"] {
  --bg-primary: #ffffff;
  --bg-secondary: #f3f3f3;
  --bg-tertiary: #e5e5e5;
  --text-primary: #111111;
  --text-secondary: #333333;
  --text-muted: #666666;
  --border-color: #e0e0e0;
  --accent-color: #007acc;
  --button-bg: #007acc;
  --button-hover: #005a9e;
  --code-block-bg: #f5f5f5;
  --input-bg: transparent;
  --scrollbar-track: #ffffff;
  --scrollbar-thumb: #cccccc;
}

body {
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: "Consolas", "Monaco", "Courier New", monospace;
  margin: 0;
  padding: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Lock body to prevent pull-to-refresh and header scrolling away */
  position: fixed;
  width: 100%;
}

#app {
  display: flex;
  width: 100%;
  flex: 1; /* Take remaining height */
  min-height: 0;
  overflow: hidden;
}

#sidebar {
  width: 200px;
  background-color: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  padding: 10px;
}

.sidebar-item {
  padding: 10px;
  cursor: pointer;
  color: var(--text-secondary);
  border-radius: 4px;
  margin-bottom: 5px;
}

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

#main-area {
  flex: 1;
  display: flex;
  min-height: 0; /* Critical for nested flex scrolling */
  overflow: hidden;
}

#terminal-container {
  flex: 1;
  padding: 10px 10px 40px 10px; /* Generous padding */
  margin-bottom: 5px; /* Physical gap from footer */
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
}

#doc-panel {
  width: 100%;
  background-color: var(--bg-primary);
  border-left: none;
  overflow-y: auto;
  padding: 20px;
  box-sizing: border-box;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  z-index: 20;
  color: var(--text-primary);
}

#close-doc {
  position: absolute;
  top: 10px;
  right: 10px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 24px;
  cursor: pointer;
  z-index: 10;
}

#close-doc:hover {
  color: var(--text-primary);
}

#footer {
  height: 30px; /* Minimal fixed height, or use padding */
  background-color: var(--accent-color);
  color: white; /* Always white on accent color */
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  font-size: 14px;
  flex-shrink: 0;
  z-index: 100;
}

#doc-content h1,
#doc-content h2,
#doc-content h3 {
  color: #569cd6;
}

#doc-content code {
  background-color: var(--code-block-bg);
  padding: 2px 4px;
  border-radius: 3px;
  font-family: "Consolas", monospace;
}

#doc-content pre {
  background-color: var(--code-block-bg);
  padding: 10px;
  border-radius: 5px;
  overflow-x: auto;
}

/* .xterm-viewport {
  overflow-y: hidden !important; 
} */

/* Notebook Styles */
#side-panel {
  width: 50%;
  background-color: var(--bg-primary);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  position: relative;
}

#notebook-panel {
  width: 100%;
  background-color: var(--bg-primary);
  border-left: none;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
  height: 100%;
}

.notebook-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background-color: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
}

.notebook-header h3 {
  margin: 0;
  font-size: 16px;
  color: var(--text-secondary);
}

#close-notebook {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
}

#close-notebook:hover {
  color: var(--text-primary);
}

.notebook-controls {
  padding: 10px;
  background-color: var(--bg-tertiary);
  display: flex;
  gap: 10px;
  border-bottom: 1px solid var(--border-color);
}

.notebook-controls button {
  padding: 5px 10px;
  background-color: var(--button-bg);
  color: white;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
}

.notebook-controls button:hover {
  background-color: var(--button-hover);
}

#notebook-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 2px; /* Reduced from 15px */
}

.notebook-cell {
  /* Removed card styles to be seamless */
  padding: 2px 20px; /* Reduced vertical padding */
  display: flex;
  flex-direction: column;
  gap: 5px;
  position: relative;
}

.notebook-cell:hover .cell-controls {
  opacity: 1;
}

.cell-input {
  width: 100%;
  background-color: transparent;
  border: none; /* Seamless */
  color: var(--text-primary);
  font-family: inherit;
  font-size: 15px;
  padding: 5px 0;
  box-sizing: border-box;
  resize: none; /* Auto-resize via JS, manual resize looks bad */
  min-height: 24px;
  line-height: 1.5;
  outline: none;
}

.text-cell {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text-primary);
}

.text-cell-view {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text-primary);
  min-height: 24px;
  padding: 5px 0;
  cursor: text;
}

.text-cell-view p {
  margin: 0 0 10px 0;
}

.text-cell-view p:last-child {
  margin: 0;
}

.text-cell-view h1,
.text-cell-view h2,
.text-cell-view h3 {
  margin: 10px 0 5px 0;
  color: var(--text-primary);
}

.text-cell-view code {
  background-color: var(--bg-tertiary);
  padding: 2px 4px;
  border-radius: 3px;
  font-family: "Consolas", monospace;
}

.text-cell-view strong {
  font-weight: bold;
  color: var(--text-primary); /* Ensure visibility */
}

.text-cell-view em {
  font-style: italic;
}

/* Code cell container to give it a "block" feel distinct from text */
.code-block-wrapper {
  background-color: var(--code-block-bg);
  border-radius: 4px;
  padding: 10px;
  border: 1px solid var(--border-color);
  position: relative; /* Ensure controls are positioned relative to this block */
}

.code-cell {
  font-family: "Consolas", monospace;
  font-size: 14px;
  background-color: transparent;
}

.code-input-area {
  display: flex;
  gap: 10px;
}

.input-prompt {
  color: #569cd6;
  font-family: monospace;
  padding-top: 8px;
  user-select: none;
}

.cell-controls {
  position: absolute;
  top: 5px;
  right: 5px;
  display: flex;
  gap: 5px;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none; /* Let clicks pass through control area */
  z-index: 10;
}

.notebook-cell:hover .cell-controls {
  opacity: 1;
}

.cell-controls button {
  padding: 2px 6px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: none;
  border-radius: 3px;
  cursor: pointer;
  font-size: 11px;
  pointer-events: auto; /* Re-enable clicks for buttons */
}

.cell-controls button:hover {
  background-color: #333;
  color: #ddd;
}

.cell-controls .run-btn {
  color: #4ec9b0;
  border-color: #4ec9b0;
}

.cell-controls .run-btn {
  color: #4ec9b0;
  border-color: #4ec9b0;
}

.cell-controls .run-btn:hover {
  background-color: #4ec9b0;
  color: #1e1e1e;
}

/* Trace Controls */
.trace-control {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--text-secondary);
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 3px;
  cursor: pointer;
  user-select: none;
  pointer-events: auto; /* Fix for inability to click checkbox */
}

.trace-control:hover {
  background-color: #333;
  color: #ddd;
}

.trace-control input {
  margin: 0;
  cursor: pointer;
}

.granularity-btn {
  /* Inherits base button styles from .cell-controls button */
  min-width: 60px;
}

.cell-output {
  background-color: var(--bg-primary);
  border-top: 1px dashed var(--border-color);
  padding: 10px;
  margin: 0;
  font-family: "Consolas", monospace;
  font-size: 13px;
  white-space: pre-wrap;
  color: var(--text-primary);
  max-height: 200px;
  overflow-y: auto;
}

.cell-output.error {
  color: #f48771;
  border-color: #f48771;
}

.cell-output.success {
  border-color: #4ec9b0;
}

/* Sidebar Enhancements */
.sidebar-section {
  display: flex;
  flex-direction: column;
  margin-bottom: 10px;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 10px;
  color: var(--text-muted);
  font-size: 12px;
  text-transform: uppercase;
  font-weight: bold;
}

.add-notebook-btn {
  background: none;
  border: none;
  color: #888;
  cursor: pointer;
  font-size: 16px;
  padding: 0 4px;
}

.add-notebook-btn:hover {
  color: white;
}

.sidebar-subitem {
  padding: 8px 10px 8px 20px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-subitem:hover {
  background-color: var(--bg-tertiary);
}

.sidebar-subitem.active {
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
  border-left: 2px solid var(--accent-color);
}

.delete-notebook-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  opacity: 0;
  padding: 0 4px;
}

.sidebar-subitem:hover .delete-notebook-btn {
  opacity: 1;
}

.delete-notebook-btn:hover {
  color: #f48771;
}

#notebook-title {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary);
  font-size: 16px;
  font-family: inherit;
  width: 200px;
  padding: 4px;
}

#notebook-title:hover {
  border-color: var(--border-color);
}

#notebook-title:focus {
  border-color: var(--accent-color);
  outline: none;
  background-color: var(--bg-primary);
}

#save-notebook {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  padding: 4px;
}
/* Mobile Sidebar Styles */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 1000;
    width: 250px;
    transform: translateX(-100%);
    transition: transform 0.3s ease-in-out;
    box-shadow: 2px 0 5px rgba(0, 0, 0, 0.5);
  }

  #sidebar.open {
    transform: translateX(0);
  }

  #mobile-menu-btn {
    display: block !important;
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 1001;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 4px;
    padding: 5px 10px;
    cursor: pointer;
    font-size: 20px;
  }

  #sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    display: none;
  }

  #sidebar-overlay.open {
    display: block;
  }
}

#mobile-menu-btn {
  display: none;
}

#sidebar-overlay {
  display: none;
}
/* Mobile Header Styles */
#mobile-header {
  display: none; /* Hidden by default on desktop */
}

@media (max-width: 768px) {
  #mobile-header {
    display: flex;
    align-items: center;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    padding: 0 10px;
    height: 40px; /* Compact header */
    flex-shrink: 0;
  }

  .mobile-title {
    flex: 1;
    text-align: center;
    font-size: 14px;
    font-weight: bold;
    color: var(--text-primary);
    /* Offset logic to center title despite left button */
    padding-right: 32px; /* Approx width of hamburger to balance */
  }

  #mobile-menu-btn {
    display: block !important;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 20px;
    cursor: pointer;
    padding: 0 5px;
    /* Clean up previous floating styles if any remain in base css */
    position: static;
    top: auto;
    left: auto;
    z-index: auto;
  }

  /* Adjust #app height since header takes space */
  #app {
    height: calc(100% - 40px); /* Subtract header height */
  }
}
/* Mobile Layout Vertical Split */
@media (max-width: 768px) {
  #main-area {
    flex-direction: column;
  }

  #terminal-container {
    width: 100% !important; /* Override inline styles if any */
    flex: 1 1 0; /* Share space equally */
    min-height: 0;
    margin-bottom: 0; /* Remove gap if handled by flex */
  }

  #side-panel {
    width: 100% !important;
    height: auto !important;
    flex: 1 1 0; /* Share space equally */
    border-left: none;
    border-top: 1px solid var(--border-color);
    min-height: 0;
  }

  #notebook-panel,
  #doc-panel {
    width: 100% !important;
    height: 100% !important;
    border-left: none;
    border-top: none;
    min-height: 0;
  }
}
/* Mobile Cell Controls Positioning */
@media (max-width: 768px) {
  .notebook-cell {
    /* Ensure flex column for stacking controls */
    display: flex;
    flex-direction: column;
  }

  .code-block-wrapper {
    display: flex !important;
    flex-direction: column;
  }

  .cell-controls {
    position: relative !important;
    top: auto !important;
    right: auto !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    /* Move to top */
    order: -1;
    margin-bottom: 5px;
    /* Align buttons to the right */
    justify-content: flex-end;
    width: 100%;
  }
}
