:root {
  --chrome-bg: #17181c;
  --chrome-bg-soft: #1d1e22;
  --chrome-bg-hover: #26272c;
  --chrome-border: #2a2b30;
  --chrome-text: #e9e9ea;
  --chrome-text-dim: #8b8c93;

  --accent: #e5342e;
  --accent-hover: #c92a25;
  --yellow: #e5bb33;
  --cyan: #3fb9cd;

  --bg: #181c1f;
  --bg-soft: #14171a;
  --border: #2b3237;
  --text: #eaeef0;
  --text-dim: #8b969c;
  --accent-soft: #3a1f1e;

  --topbar-h: 54px;
  --statusbar-h: 25px;
  --tabs-h: 40px;
  --radius: 6px;
  --sans: "Sora", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", "Fira Code", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

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

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ================= TOP BAR (chrome: always dark) ================= */
.topbar {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--chrome-bg);
  border-bottom: 1px solid var(--chrome-border);
  color: var(--chrome-text);
  z-index: 30;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: 4px;
  white-space: nowrap;
  user-select: none;
}
.brand-logo {
  display: block;
  width: 26px;
  height: 26px;
  border-radius: 6px;
}
.brand-word {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--chrome-text);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  padding: 8px 14px;
  border-radius: var(--radius);
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }

.btn-ghost {
  background: transparent;
  color: var(--chrome-text-dim);
  border: 1px solid var(--chrome-border);
}
.btn-ghost:hover { color: var(--chrome-text); background: var(--chrome-bg-hover); }

.btn-run {
  background: var(--accent);
  color: #fff;
}
.btn-run:hover { background: var(--accent-hover); }
.btn-run:disabled { opacity: .6; cursor: default; }
.btn-run.loading svg { animation: spin 1s linear infinite; }

@keyframes spin { to { transform: rotate(360deg); } }

.topbar-spacer { flex: 1; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: transparent;
  color: var(--chrome-text-dim);
  cursor: pointer;
}
.icon-btn:hover { background: var(--chrome-bg-hover); color: var(--chrome-text); }
.icon-btn.small { width: 27px; height: 27px; color: var(--text-dim); }
.icon-btn.small:hover { background: var(--accent-soft); color: var(--text); }

.only-mobile { display: none; }

/* language dropdown */
.lang-select { position: relative; }
.lang-select-btn {
  display: flex; align-items: center; gap: 8px;
  border: 1px solid var(--chrome-border);
  background: var(--chrome-bg-soft);
  padding: 7px 10px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  font-family: var(--sans);
  color: var(--chrome-text);
  cursor: pointer;
  max-width: 240px;
}
.lang-select-btn:hover { background: var(--chrome-bg-hover); }
.lang-select-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lang-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex-shrink: 0; }
.lang-select-btn svg { margin-left: auto; color: var(--chrome-text-dim); flex-shrink: 0; }

/* generic popover menu, used by lang/add/more menus */
.menu-popover {
  position: fixed;
  width: 260px;
  max-height: 60vh;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: none;
  padding: 6px;
  z-index: 500;
}
.menu-popover.open { display: block; }
.menu-item, .lang-menu-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 11px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text);
}
.menu-item:hover, .lang-menu-item:hover { background: var(--accent-soft); }
.lang-menu-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.lang-menu-item .swatch { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.lang-menu-item small { margin-left: auto; color: var(--text-dim); font-weight: 400; }
.lang-menu-group {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-dim);
  padding: 9px 11px 4px;
  font-weight: 700;
}

/* ================= MAIN LAYOUT ================= */
.main {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.sidebar-scrim {
  display: none;
  position: fixed;
  inset: var(--topbar-h) 0 var(--statusbar-h) 0;
  background: rgba(10,15,18,0.45);
  z-index: 40;
}
.sidebar-scrim.open { display: block; }

.sidebar {
  width: 0;
  min-width: 0;
  overflow: hidden;
  background: var(--chrome-bg);
  border-right: none;
  display: flex;
  flex-direction: column;
  color: var(--chrome-text);
  flex-shrink: 0;
  transition: width .16s ease;
}
.sidebar.open {
  min-width: 160px;
  border-right: 1px solid var(--chrome-border);
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 12px 8px;
  color: var(--chrome-text-dim);
}
.sidebar-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
}
.sidebar-actions { display: flex; gap: 2px; }
.file-tree {
  flex: 1;
  overflow-y: auto;
  padding: 2px 6px 12px;
}
.sidebar-hint {
  font-size: 10.5px;
  color: var(--chrome-text-dim);
  padding: 8px 12px;
  border-top: 1px solid var(--chrome-border);
  line-height: 1.4;
}
.sidebar-resizer, .pane-resizer {
  width: 4px;
  cursor: col-resize;
  background: transparent;
  flex-shrink: 0;
  position: relative;
}
.sidebar-resizer:hover, .pane-resizer:hover, .sidebar-resizer.active, .pane-resizer.active { background: var(--accent); opacity: .5; }

.tree-node { user-select: none; }
.tree-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 4px;
  font-size: 13px;
  cursor: pointer;
  color: var(--chrome-text);
  white-space: nowrap;
  border: 1px solid transparent;
}
.tree-row:hover { background: var(--chrome-bg-hover); }
.tree-row.active { background: var(--chrome-bg-hover); color: #fff; font-weight: 600; }
.tree-row.drag-over { border-color: var(--cyan); background: var(--chrome-bg-hover); }
.tree-row .chev { width: 12px; flex-shrink: 0; color: var(--chrome-text-dim); transform: rotate(90deg); }
.tree-row .chev.collapsed { transform: rotate(0deg); }
.file-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; display: inline-block; }
.tree-children { padding-left: 16px; }
.tree-children.hidden { display: none; }

/* ================= WORKSPACE ================= */
.workspace {
  flex: 1;
  display: flex;
  min-width: 0;
  min-height: 0;
}
.editor-pane {
  flex: 1;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}
.tabs {
  height: var(--tabs-h);
  display: flex;
  align-items: stretch;
  background: var(--chrome-bg);
  border-bottom: 1px solid var(--chrome-border);
  overflow-x: auto;
  flex-shrink: 0;
}
.tab {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 10px 0 12px;
  font-size: 13px;
  color: var(--chrome-text-dim);
  border-right: 1px solid var(--chrome-border);
  cursor: pointer;
  white-space: nowrap;
  position: relative;
}
.tab:hover { background: var(--chrome-bg-hover); }
.tab.active {
  background: var(--chrome-bg-soft);
  color: var(--chrome-text);
  font-weight: 600;
}
.tab.active::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 2px;
  background: var(--accent);
}
.tab .tab-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 16px; height: 16px;
  border-radius: 4px;
  color: var(--text-dim);
}
.tab .tab-close:hover { background: var(--chrome-bg-hover); color: var(--chrome-text); }

.editor-host {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--chrome-bg);
}
.editor-host .CodeMirror,
.editor-host .CodeMirror-gutters {
  background: var(--chrome-bg) !important;
}
.editor-host .CodeMirror-gutters {
  border-right: 1px solid var(--chrome-border) !important;
}
.editor-host .CodeMirror {
  height: 100%;
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.6;
}

.image-preview {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--bg-soft);
  padding: 20px;
  overflow: auto;
}
.image-preview.show { display: flex; }
.image-preview img { max-width: min(92%, 720px); max-height: 70%; border: 1px solid var(--border); background: var(--bg); }
.image-preview-meta { font-family: var(--mono); font-size: 12px; color: var(--text-dim); }

/* ================= OUTPUT PANE ================= */
.output-pane {
  width: 44%;
  min-width: 260px;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-left: 1px solid var(--border);
  min-height: 0;
}
.output-pane.hidden { display: none; }
.output-header {
  height: var(--tabs-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  padding: 0 6px 0 4px;
  flex-shrink: 0;
  background: var(--bg-soft);
}
.output-tabs { display: flex; height: 100%; }
.output-tab {
  border: none;
  background: transparent;
  padding: 0 12px;
  font-size: 12.5px;
  font-weight: 600;
  font-family: var(--sans);
  color: var(--text-dim);
  cursor: pointer;
  position: relative;
}
.output-tab.active { color: #ffffff; }
.output-tab.active::after {
  content: "";
  position: absolute;
  left: 10px; right: 10px; bottom: 0;
  height: 2px;
  background: #ffffff;
}
.output-actions { display: flex; align-items: center; gap: 2px; }
.run-meta { font-size: 11px; color: var(--text-dim); margin-right: 8px; font-family: var(--mono); }

.output-body { flex: 1; min-height: 0; position: relative; }
.output-view { position: absolute; inset: 0; display: none; }
.output-view.active { display: block; }

#previewFrame { width: 100%; height: 100%; border: none; background: #fff; }

.console-box {
  margin: 0;
  padding: 12px 14px;
  height: 100%;
  overflow: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.6;
  background: #14151d;
  color: #d7d9e3;
  white-space: pre-wrap;
  word-break: break-word;
}

.console-line.err { color: #ff6b81; }
.console-line.warn { color: #e5bb33; }
.console-line.info { color: #6db4ff; }
.console-line.exit-ok { color: #4ade80; }
.console-line.exit-err { color: #ff6b81; }
.console-line.meta { color: #8890a8; }

#stdinBox {
  width: 100%; height: 100%;
  border: none;
  resize: none;
  padding: 12px 14px;
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}

/* ================= STATUS BAR ================= */
.statusbar {
  height: var(--statusbar-h);
  min-height: var(--statusbar-h);
  background: var(--chrome-bg);
  color: var(--chrome-text-dim);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 14px;
  font-size: 11.5px;
  border-top: 1px solid var(--chrome-border);
}
.statusbar a { color: var(--chrome-text-dim); text-decoration: none; }
.statusbar-spacer { flex: 1; }
.dot-ok { width: 6px; height: 6px; border-radius: 50%; background: var(--cyan); display: inline-block; margin-right: 5px; }

/* ================= CONTEXT MENU ================= */
.ctx-menu {
  position: fixed;
  display: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px;
  z-index: 1000;
  min-width: 150px;
}
.ctx-menu.open { display: block; }
.ctx-item {
  padding: 8px 11px;
  font-size: 13px;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text);
}
.ctx-item:hover { background: var(--accent-soft); }
.ctx-item.danger { color: #dc2626; }
.ctx-item.danger:hover { background: #fee2e2; }

/* ================= TOAST ================= */
.toast {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--chrome-bg);
  color: #fff;
  padding: 9px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  border: 1px solid var(--chrome-border);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 2000;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
::-webkit-scrollbar-track { background: transparent; }

/* ================= RESPONSIVE ================= */
@media (max-width: 900px) {
  .only-desktop { display: none !important; }
  .only-mobile { display: inline-flex; }

  .sidebar {
    position: fixed;
    left: 0;
    top: var(--topbar-h);
    bottom: var(--statusbar-h);
    width: 78vw;
    max-width: 300px;
    transform: translateX(-100%);
    transition: transform .2s ease;
    z-index: 50;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-resizer { display: none; }

  .brand-word { display: none; }
  .lang-select-btn { max-width: 150px; }
}

@media (min-width: 901px) {
  .only-mobile { display: none !important; }
}

@media (max-width: 760px) {
  .workspace { flex-direction: column; }
  .pane-resizer { display: none; }
  .output-pane { width: 100% !important; height: 40vh; border-left: none; border-top: 1px solid var(--border); }
  .editor-pane { flex: 1 1 auto; min-height: 30vh; }
}

@media (max-width: 480px) {
  .btn-run-label { display: none; }
  .btn-run { padding: 8px 12px; }
  .lang-select-btn { max-width: 110px; }
}
