:root {
  --bg: #1e1e1e;
  --bg-2: #252526;
  --bg-3: #1a1a1a;
  --fg: #e6e6e6;
  --muted: #9aa0a6;
  --accent: #4f9cf9;
  --accent-hover: #6cb0ff;
  --border: #333;
  --err: #ff6b6b;
  --ok: #6fcf97;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
}

header h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

button {
  background: #3a3a3a;
  color: var(--fg);
  border: 1px solid #4a4a4a;
  padding: 6px 14px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
}

button:hover:not(:disabled) {
  background: #4a4a4a;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.status {
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

.template-label {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: 4px;
}

.template-label select {
  background: #3a3a3a;
  color: var(--fg);
  border: 1px solid #4a4a4a;
  padding: 5px 8px;
  border-radius: 4px;
  font-family: inherit;
  font-size: 13px;
}

main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.file-pane {
  width: 200px;
  flex-shrink: 0;
  min-width: 80px;
  background: var(--bg-2);
}

.editor-pane {
  flex: 1;
  min-width: 200px;
  position: relative;
}

.preview-pane {
  width: 55%;
  flex-shrink: 0;
  min-width: 200px;
  background: var(--bg-3);
}

/* --- gutters --- */

.gutter {
  background: var(--border);
  flex-shrink: 0;
  z-index: 5;
  transition: background 0.12s;
  user-select: none;
}

.gutter.gutter-x {
  width: 5px;
  cursor: col-resize;
}

.gutter.gutter-y {
  height: 5px;
  cursor: row-resize;
}

.gutter:hover,
.gutter.dragging {
  background: var(--accent);
}

/* --- console section --- */

.console-section {
  display: flex;
  flex-direction: column;
  height: 180px;
  flex-shrink: 0;
  min-height: 0;
}

.console-section #console {
  flex: 1;
  height: auto;
  min-height: 0;
  border-top: none;
}

#fileTree {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.file-folder {
  margin: 6px 0 0;
  border-radius: 3px;
}

.file-folder.drop-target {
  background: rgba(79, 156, 249, 0.12);
  outline: 1px dashed var(--accent);
  outline-offset: -2px;
}

.folder-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 10px;
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.icon-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 0 6px;
  font-size: 14px;
  line-height: 1.4;
  cursor: pointer;
  border-radius: 3px;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--fg);
  border-color: var(--border);
}

.file-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3px 10px 3px 18px;
  font-size: 13px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  cursor: default;
  color: var(--fg);
}

.file-item:hover {
  background: rgba(255, 255, 255, 0.04);
}

.file-item.file-main {
  font-weight: 600;
  padding: 8px 10px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.file-item .file-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.file-item .del-btn {
  visibility: hidden;
  flex-shrink: 0;
}

.file-item:hover .del-btn {
  visibility: visible;
}

.folder-empty {
  padding: 2px 18px 4px;
  color: #555;
  font-size: 11px;
  font-style: italic;
}

/* --- tab strip above the editor --- */

.tab-strip {
  display: flex;
  align-items: stretch;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
  scrollbar-width: thin;
  /* Lock the strip to a stable height so CodeMirror measurements don't
   * shift between the moment the page loads (empty strip) and the moment
   * the first tab appears. */
  min-height: 30px;
}

.tab-strip .tab {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--muted);
  border-right: 1px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.tab-strip .tab:hover {
  background: rgba(255, 255, 255, 0.04);
  color: var(--fg);
}

.tab-strip .tab.active {
  background: var(--bg);
  color: var(--fg);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.tab-strip .tab-label {
  pointer-events: none;
}

.tab-strip .tab-close {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0 3px;
  font-size: 14px;
  line-height: 1;
  border-radius: 3px;
  visibility: hidden;
}

.tab-strip .tab:hover .tab-close,
.tab-strip .tab.active .tab-close {
  visibility: visible;
}

.tab-strip .tab-close:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--fg);
}

.tab-strip .tab-add {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.4;
  font-weight: 600;
}

.tab-strip .tab-add:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--fg);
}

/* --- editor status bar --- */

.editor-pane {
  position: relative;
}

.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 12px;
  background: var(--bg-2);
  border-top: 1px solid var(--border);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11px;
  color: var(--muted);
  flex-shrink: 0;
}

.save-status.saved { color: var(--ok); }
.save-status.saving { color: var(--accent); }
.save-status.modified { color: #d6a04c; }

/* --- error line highlight --- */

.line-error {
  background: rgba(255, 80, 80, 0.18) !important;
  box-shadow: inset 3px 0 0 var(--err);
}

/* --- trailing whitespace --- */

.cm-trailingspace {
  background: rgba(255, 0, 0, 0.18);
}

/* --- settings menu --- */

.settings-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg);
  font-size: 16px;
  padding: 2px 9px;
  line-height: 1;
}

.settings-btn:hover {
  background: rgba(255, 255, 255, 0.06);
}

.settings-panel {
  position: absolute;
  top: 100%;
  right: 12px;
  margin-top: 6px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  min-width: 240px;
  max-width: 300px;
  z-index: 20;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
}

.settings-panel.hidden {
  display: none;
}

.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  gap: 12px;
}

.settings-row label {
  font-size: 12px;
  color: var(--muted);
}

.settings-row select {
  background: #3a3a3a;
  color: var(--fg);
  border: 1px solid #4a4a4a;
  padding: 4px 6px;
  border-radius: 3px;
  font-family: inherit;
  font-size: 12px;
  min-width: 130px;
}

.settings-hint {
  margin-top: 6px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 10px;
  row-gap: 3px;
  align-items: center;
}

.settings-hint kbd {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 10px;
  color: var(--fg);
  white-space: nowrap;
  justify-self: start;
}

/* Make the controls row position the settings panel correctly. */
header {
  position: relative;
}
.controls {
  position: relative;
}

/* --- CodeMirror tweaks for our themes --- */

.CodeMirror-foldgutter {
  width: 12px;
}

.CodeMirror-foldgutter-open,
.CodeMirror-foldgutter-folded {
  cursor: pointer;
  color: var(--muted);
}

/* search dialog uses .CodeMirror-dialog -- give it a dark backdrop for our
 * dark themes; the eclipse light theme will look slightly off but legible. */
.CodeMirror-dialog {
  background: var(--bg-2);
  color: var(--fg);
  border-bottom: 1px solid var(--border);
}

.CodeMirror-dialog input {
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: 2px 6px;
}

/* hint popup */
.CodeMirror-hints {
  background: var(--bg-2);
  border: 1px solid var(--border);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
}

.CodeMirror-hint {
  color: var(--fg);
  padding: 2px 8px;
}

li.CodeMirror-hint-active {
  background: var(--accent);
  color: #fff;
}

/* --- find / replace panel --- */

.find-panel {
  position: absolute;
  top: 30px;
  right: 14px;
  z-index: 15;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

.find-panel.hidden {
  display: none;
}

.find-row, .replace-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.find-input, .replace-input {
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 4px 6px;
  font-family: inherit;
  font-size: 12px;
  width: 220px;
  outline: none;
}

.find-input:focus, .replace-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.find-input.find-input-error {
  border-color: var(--err);
  box-shadow: 0 0 0 1px var(--err);
}

.find-panel button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 2px 8px;
  border-radius: 3px;
  font-family: inherit;
  font-size: 11px;
  cursor: pointer;
  line-height: 1.4;
}

.find-panel button:hover {
  background: rgba(255, 255, 255, 0.06);
}

.find-panel .opt-btn {
  min-width: 26px;
  text-align: center;
  font-weight: 600;
}

.find-panel .opt-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.find-panel .nav-btn,
.find-panel .close-btn {
  min-width: 26px;
  text-align: center;
}

.find-panel .find-count {
  color: var(--muted);
  font-size: 11px;
  min-width: 56px;
  text-align: center;
  padding: 0 2px;
}

.find-panel .replace-input {
  /* match the find input's width minus the option-button strip so the two
   * inputs line up visually */
  width: 220px;
}

.find-panel .action-btn {
  padding: 2px 10px;
}

/* match highlights inside the editor */
.search-match {
  background: rgba(255, 200, 60, 0.28);
  outline: 1px solid rgba(255, 200, 60, 0.6);
}

.search-match-current {
  background: rgba(255, 130, 0, 0.55);
  outline: 1px solid rgba(255, 160, 40, 0.95);
}

/* selection scope when "Find in selection" is on */
.search-scope {
  background: rgba(79, 156, 249, 0.10);
  border-radius: 2px;
}

/* brief flash when "Sel" is clicked with no selection */
@keyframes find-btn-flash {
  0%, 100% { background: transparent; }
  50% { background: rgba(255, 80, 80, 0.5); }
}

.find-panel .opt-btn.flash {
  animation: find-btn-flash 0.25s ease;
}

.pane-header {
  background: var(--bg-2);
  padding: 6px 12px;
  font-size: 12px;
  color: var(--muted);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

/* --- collapsible pane headers --- */

.pane-header.collapsible {
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.pane-header.collapsible:hover {
  color: var(--fg);
  background: rgba(255, 255, 255, 0.03);
}

.pane-header.collapsible::after {
  content: "\25BC"; /* down triangle */
  font-size: 9px;
  color: var(--muted);
  transition: transform 0.15s;
}

.file-pane.collapsed .pane-header.collapsible::after,
.console-section.collapsed .pane-header.collapsible::after {
  transform: rotate(-90deg);
}

/* File pane in collapsed mode: hide the label and the contents, shrink to a
 * thin strip with just the chevron. */
.file-pane.collapsed .pane-header.collapsible .label {
  display: none;
}
.file-pane.collapsed .pane-header.collapsible {
  justify-content: center;
  padding: 6px 0;
}
.file-pane.collapsed #fileTree {
  display: none;
}

/* Console collapsed: hide the pre, header stays visible. */
.console-section.collapsed #console {
  display: none;
}

#editor {
  display: none;
}

.CodeMirror {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 14px;
}

.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  padding: 8px;
  min-height: 0;
  overflow: auto;
}

#canvas {
  background: #000;
  image-rendering: pixelated;
  box-shadow: 0 0 0 1px var(--border);
  /* Render at native pixel size always. If the preview pane is too narrow,
   * canvas-wrap scrolls (it has overflow: auto). CSS-scaling the canvas at
   * non-integer ratios looks soft/glitchy and tanks animation feel. */
  flex-shrink: 0;
}

/* --- preview popped-out / preview-mode body classes --- */

/* Editor window when its preview is popped out into a separate window. */
body.preview-popped-out #gutterPreview,
body.preview-popped-out .preview-pane {
  display: none;
}

.popout-status {
  background: var(--accent);
  color: #fff;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  margin-left: 6px;
}

.popout-status.hidden {
  display: none;
}

/* --- toast notification --- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 16px);
  background: var(--accent);
  color: #fff;
  padding: 10px 18px;
  border-radius: 6px;
  font-size: 13px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* Preview mode: this window is the canvas-only popup. Hide everything that
 * isn't the canvas + Run/Stop. */
body.preview-mode .file-pane,
body.preview-mode #gutterFile,
body.preview-mode .editor-pane,
body.preview-mode #gutterPreview,
body.preview-mode #importBtn,
body.preview-mode #exportBtn,
body.preview-mode #fullscreenBtn,
body.preview-mode #popoutBtn,
body.preview-mode #popoutStatus,
body.preview-mode #shareBtn,
body.preview-mode .template-label,
body.preview-mode #resetBtn,
body.preview-mode #settingsBtn,
body.preview-mode #settingsPanel,
body.preview-mode header h1,
body.preview-mode .controls .status,
body.preview-mode .preview-pane > .pane-header {
  display: none !important;
}

/* Slim down the header to just the Run/Stop strip. */
body.preview-mode header {
  padding: 6px 10px;
  gap: 8px;
}

/* Canvas fills its container without a padding gap. */
body.preview-mode .canvas-wrap {
  padding: 0;
}

body.preview-mode .preview-pane {
  flex: 1;
  width: auto;
  min-width: 0;
  border-left: none;
}

/* Fullscreen canvas: scale to fill the screen while keeping aspect ratio. */
.canvas-wrap:fullscreen {
  background: #000;
  padding: 0;
  width: 100vw;
  height: 100vh;
}

.canvas-wrap:fullscreen #canvas {
  width: 100%;
  height: 100%;
  max-width: none;
  object-fit: contain;
  box-shadow: none;
}

#console {
  height: 180px;
  margin: 0;
  padding: 8px 12px;
  background: var(--bg-3);
  color: var(--fg);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  overflow-y: auto;
  white-space: pre-wrap;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#console .info { color: var(--muted); }
#console .ok { color: var(--ok); }
#console .stdout { color: var(--fg); }
#console .stderr { color: var(--err); }
