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

:root {
  --bg: #1e1e2e;
  --bg-surface: #252535;
  --bg-overlay: #181825;
  --text: #cdd6f4;
  --text-dim: #6c7086;
  --accent: #89b4fa;
  --accent-hover: #b4d0fb;
  --green: #a6e3a1;
  --red: #f38ba8;
  --yellow: #f9e2af;
  --border: #313244;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* Light theme */
body.theme-light {
  --bg: #f5f5f5;
  --bg-surface: #ffffff;
  --bg-overlay: #fafafa;
  --text: #1e1e2e;
  --text-dim: #6c7086;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --green: #16a34a;
  --red: #dc2626;
  --yellow: #ca8a04;
  --border: #d4d4d8;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  height: 48px;
}

.header h1 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.5px;
}

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

.btn {
  padding: 6px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-surface);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s ease;
}

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

.btn-run {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 600;
}

body.theme-light .btn-run {
  color: #fff;
}

.btn-run:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: #fff;
}

.btn-settings {
  font-family: var(--font-ui);
  font-size: 13px;
}

.btn-reset {
  font-size: 13px;
}

.shortcut-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-left: 4px;
}

.btn-run .shortcut-hint {
  opacity: 0.7;
  color: inherit;
}

/* Main layout */
.main-layout {
  display: flex;
  height: calc(100% - 48px);
}

/* Sidebar */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar.closed {
  display: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  min-height: 36px;
}

.sidebar-title {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-dim);
}

.sidebar-close {
  font-size: 14px;
  padding: 2px 6px;
}

.sidebar-tree {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
  font-family: var(--font-ui);
  font-size: 13px;
}

.tree-item {
  display: flex;
  align-items: center;
  padding: 3px 12px;
  cursor: pointer;
  color: var(--text);
  transition: background 0.1s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-item:hover {
  background: rgba(137, 180, 250, 0.1);
}

.tree-item.active {
  background: rgba(137, 180, 250, 0.15);
  color: var(--accent);
}

.tree-item .icon {
  margin-right: 6px;
  font-size: 12px;
  width: 16px;
  text-align: center;
  flex-shrink: 0;
}

.tree-item .name {
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-dir-children {
  display: none;
}

.tree-dir-children.open {
  display: block;
}

.tree-item.depth-1 { padding-left: 24px; }
.tree-item.depth-2 { padding-left: 36px; }
.tree-item.depth-3 { padding-left: 48px; }

.btn-sidebar-toggle {
  font-family: var(--font-ui);
  font-size: 13px;
}

/* Editor container */
.editor-container {
  flex: 1;
  min-width: 0;
}

.editor-container .cm-editor {
  height: 100%;
}

/* Responsive sidebar */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 48px;
    left: 0;
    bottom: 0;
    width: 280px;
    z-index: 50;
    box-shadow: 4px 0 16px rgba(0,0,0,0.3);
  }
}

.editor-container .cm-editor .cm-scroller {
  font-family: var(--font-mono);
  font-size: 15px;
  line-height: 1.6;
}

/* Light theme: CodeMirror overrides */
body.theme-light .cm-editor {
  background: var(--bg);
}

body.theme-light .cm-editor .cm-gutters {
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
}

body.theme-light .cm-editor .cm-activeLineGutter,
body.theme-light .cm-editor .cm-activeLine {
  background: rgba(0, 0, 0, 0.04);
}

/* Output overlay */
.output-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.output-overlay.visible {
  opacity: 1;
  pointer-events: all;
}

.output-panel {
  position: fixed;
  top: 24px;
  left: 24px;
  right: 24px;
  bottom: 24px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  z-index: 101;
  display: flex;
  flex-direction: column;
  transform: translateY(20px) scale(0.98);
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.output-overlay.visible .output-panel {
  transform: translateY(0) scale(1);
  opacity: 1;
  pointer-events: all;
}

.output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  border-radius: 12px 12px 0 0;
}

.output-header h2 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
}

.output-status {
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 4px;
  color: var(--text-dim);
}

.output-status.success {
  background: rgba(166, 227, 161, 0.15);
  color: var(--green);
}

.output-status.error {
  background: rgba(243, 139, 168, 0.15);
  color: var(--red);
}

.btn-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.15s ease;
}

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

/* Terminal emulator */
.output-content {
  --term-bg: #000000;
  --term-fg: #33ff33;
  --term-cursor: #33ff33;
  flex: 1;
  padding: 12px 16px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 15px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  background: var(--term-bg);
  color: var(--term-fg);
  border-radius: 0 0 12px 12px;
}

.output-content .term-text {
  color: var(--term-fg);
}

.output-content .term-error {
  color: var(--red);
}

.output-content .term-input {
  color: var(--term-fg);
  background: transparent;
  border: none;
  outline: none;
  caret-color: transparent;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  min-width: 1ch;
  display: inline;
}

.output-content .term-input[data-active="true"]::after {
  content: "▌";
  color: var(--term-cursor);
  animation: term-blink 0.7s step-end infinite;
}

.output-content .term-input-done::after {
  content: none;
}

.output-content .term-input-done {
  border-bottom: none;
  animation: none;
}

@keyframes term-blink {
  50% { border-bottom-color: transparent; }
}

/* Settings overlay (#8) */
.settings-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.settings-overlay.visible {
  opacity: 1;
  pointer-events: all;
}

.settings-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  width: 380px;
  max-height: 80vh;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  z-index: 201;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
}

.settings-overlay.visible .settings-panel {
  transform: translate(-50%, -50%) scale(1);
  opacity: 1;
  pointer-events: all;
}

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

.settings-header h2 {
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.settings-content {
  padding: 16px 20px;
}

.setting-group {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.setting-group + .setting-group {
  margin-top: 16px;
}

.setting-label {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}

.setting-select {
  font-family: var(--font-ui);
  font-size: 13px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  outline: none;
  transition: border-color 0.15s ease;
}

.setting-select:hover {
  border-color: var(--accent);
}

.setting-select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(137, 180, 250, 0.2);
}

.settings-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-family: var(--font-ui);
  font-size: 9px;
  color: var(--text-dim);
}

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

.settings-footer a:hover {
  text-decoration: underline;
}

/* Responsive */
@media (max-width: 768px) {
  .output-panel {
    top: 8px;
    left: 8px;
    right: 8px;
    bottom: 8px;
    border-radius: 8px;
  }

  .settings-panel {
    width: calc(100% - 32px);
    max-width: 380px;
  }

  .shortcut-hint {
    display: none;
  }
}
