:root {
  --bg: #1e1e1e;
  --bg-2: #252526;
  --bg-3: #2d2d2d;
  --fg: #d4d4d4;
  --muted: #8a8a8a;
  --border: #333;
  --accent: #3b8eea;
  --hover: #2a2d2e;
  --select: #37373d;
  --danger: #e5534b;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}
body.light {
  --bg: #ffffff;
  --bg-2: #f3f3f3;
  --bg-3: #e8e8e8;
  --fg: #222;
  --muted: #777;
  --border: #ddd;
  --hover: #e8e8e8;
  --select: #dcdcdc;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--fg);
  font: 13px/1.4 var(--font);
  overflow: hidden;
}
[hidden] { display: none !important; }
.grow { flex: 1; }
.muted { color: var(--muted); }

button {
  font: inherit; color: var(--fg);
  background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 4px; padding: 3px 10px; cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .4; cursor: default; }

/* Toolbar */
#toolbar {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; background: var(--bg-2); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; margin-right: 8px; }
.sep { width: 1px; height: 18px; background: var(--border); margin: 0 4px; }

/* File menu */
.menu { position: relative; }
.menu-btn { background: none; border-color: transparent; }
.menu-btn:hover:not(:disabled), .menu.open .menu-btn { background: var(--hover); border-color: transparent; }
.menu-list {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 100;
  min-width: 260px; padding: 4px 0;
  background: var(--bg-3); border: 1px solid var(--border); border-radius: 5px;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
.menu-list hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }
.mi {
  position: relative; display: flex; align-items: center; gap: 24px;
  padding: 4px 14px 4px 26px; white-space: nowrap; cursor: pointer;
}
.mi span { flex: 1; }
.mi kbd { font: inherit; color: var(--muted); }
.mi:hover, .mi.focus { background: var(--accent); color: #fff; }
.mi:hover > kbd, .mi.focus > kbd { color: #fff; }
.mi.disabled { opacity: .45; cursor: default; }
.mi.disabled:hover { background: none; color: inherit; }
.mi.checked::before { content: "✓"; position: absolute; left: 9px; }
.submenu { display: none; top: -5px; left: 100%; min-width: 200px; color: var(--fg); }
.sub:hover > .submenu { display: block; }

main { flex: 1; display: flex; min-height: 0; }

/* Sidebar / file tree */
#sidebar {
  width: 240px; min-width: 140px; max-width: 60vw;
  display: flex; flex-direction: column; background: var(--bg-2);
}
.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 10px; font-weight: 600; text-transform: uppercase; font-size: 11px;
  color: var(--muted); border-bottom: 1px solid var(--border);
}
.side-head span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-actions { display: flex; gap: 2px; }
#sidebar:not(.has-root) .side-actions { display: none; }
.side-head button { display: flex; padding: 2px; border: none; background: none; color: var(--muted); }
.side-head button:hover { background: var(--hover); color: var(--fg); }
.side-head svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.2; stroke-linejoin: round; stroke-linecap: round; }
#tree { flex: 1; overflow: auto; padding: 4px 0; }
#tree ul { list-style: none; margin: 0; padding: 0; }
#tree .row {
  display: flex; align-items: center; height: 22px; padding-right: 8px;
  white-space: nowrap; cursor: pointer; user-select: none;
}
#tree .row:hover { background: var(--hover); }
#tree .row.selected { background: var(--select); }
#tree .row.current .label { color: var(--accent); }
#tree .twisty { width: 16px; flex: none; text-align: center; color: var(--muted); font-size: 10px; }
#tree .dir > .row .twisty::before { content: "▸"; }
#tree .dir.open > .row .twisty::before { content: "▾"; }
#tree .label { overflow: hidden; text-overflow: ellipsis; }
#tree .dir > .row .label { font-weight: 500; }

#resizer { width: 4px; cursor: col-resize; background: var(--border); flex: none; }
#resizer:hover, #resizer.drag { background: var(--accent); }

/* Workspace */
#workspace { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
#tabs {
  display: flex; background: var(--bg-2); overflow-x: auto; flex: none;
  scrollbar-width: thin; border-bottom: 1px solid var(--border);
}
#tabs:empty { display: none; }
.tab {
  display: flex; align-items: center; gap: 6px; flex: none;
  padding: 6px 8px 6px 12px; max-width: 220px; cursor: pointer;
  border-right: 1px solid var(--border); color: var(--muted);
}
.tab:hover { background: var(--hover); }
.tab.active { background: var(--bg); color: var(--fg); box-shadow: inset 0 2px 0 var(--accent); }
.tab .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab .close {
  width: 18px; height: 18px; line-height: 18px; text-align: center;
  border-radius: 3px; font-size: 14px;
}
.tab .close::before { content: "×"; }
.tab .close:hover { background: var(--select); }
.tab.dirty .close::before { content: "●"; font-size: 10px; }
.tab.dirty .close:hover::before { content: "×"; font-size: 14px; }

#editor { flex: 1; min-height: 0; }
#editor .CodeMirror { height: 100%; font: 13px/1.5 var(--mono); }
body.light #editor .CodeMirror-activeline-background { background: #f5f8ff; }

#welcome {
  position: absolute; inset: 0; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
#welcome h1 { margin: 0; font-weight: 300; font-size: 32px; }
#welcome .actions { display: flex; gap: 8px; margin: 20px 0; flex-wrap: wrap; justify-content: center; }
#welcome .actions button { padding: 6px 14px; }
#unsupported { color: var(--danger); max-width: 420px; text-align: center; }
#recent { text-align: center; margin-bottom: 16px; }
#recent h2 { margin: 0 0 4px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; }
#recentList { list-style: none; margin: 0; padding: 0; }
#recentList button { border: none; background: none; color: var(--accent); padding: 2px 4px; }
#recentList button:hover { text-decoration: underline; }
.keys { color: var(--muted); border-spacing: 16px 2px; }
.keys td:first-child { text-align: right; }
.keys kbd { font: 12px var(--mono); }

/* Status bar */
#status {
  display: flex; align-items: center; gap: 14px; height: 24px; padding: 0 10px;
  background: var(--bg-2); border-top: 1px solid var(--border); font-size: 12px; color: var(--muted);
}
#stMsg.error, #stJson.error { color: var(--danger); }
#stJson { max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#stLang {
  font: inherit; color: var(--fg); background: var(--bg-3);
  border: 1px solid var(--border); border-radius: 3px; padding: 0 2px;
}

/* API Explorer */
#api { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#api input, #api select {
  font: inherit; color: var(--fg); background: var(--bg);
  border: 1px solid var(--border); border-radius: 4px; padding: 4px 6px;
}
#api input:focus, #api select:focus { outline: none; border-color: var(--accent); }
.api-bar { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.api-kind { padding: 2px 6px; border-radius: 3px; font-size: 11px; font-weight: 600; color: #fff; }
.api-kind.rest { background: #2f7d4f; }
.api-kind.gql { background: #b4236b; }
#apiMethod { font-weight: 600; }
#apiUrl { flex: 1; min-width: 0; font-family: var(--mono); }
button.primary { padding: 4px 16px; color: #fff; background: var(--accent); border-color: var(--accent); }
.api-req, .api-res { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.api-res { border-top: 1px solid var(--border); }
.api-tabs { display: flex; gap: 2px; padding: 0 10px; }
.api-req > .api-tabs { border-bottom: 1px solid var(--border); }
.api-tabs button {
  padding: 6px 10px; border: none; border-radius: 0; background: none;
  color: var(--muted); border-bottom: 2px solid transparent;
}
.api-tabs button:hover:not(:disabled) { color: var(--fg); }
.api-tabs button.active { color: var(--fg); border-bottom-color: var(--accent); }
.api-sec { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: auto; }
.api-opts { display: flex; align-items: center; gap: 8px; padding: 6px 10px; }
.api-note { margin: 0; padding: 4px 10px; }
.api-cm { flex: 1; min-height: 0; }
.api-cm .CodeMirror { height: 100%; font: 13px/1.5 var(--mono); }
.kv { display: flex; flex-direction: column; gap: 4px; padding: 8px 10px; }
.kv-row { display: flex; align-items: center; gap: 6px; }
.kv-row input:not([type=checkbox]) { flex: 1; min-width: 0; font-family: var(--mono); }
.kv-del { padding: 0 6px; border: none; background: none; color: var(--muted); font-size: 16px; }
.kv-row.blank .kv-del { visibility: hidden; }
.auth-fields {
  display: grid; grid-template-columns: max-content minmax(0, 420px); align-items: center;
  gap: 6px 10px; padding: 4px 10px;
}
.auth-fields label { display: contents; }
.api-res-head { display: flex; align-items: center; min-height: 32px; padding-left: 10px; border-bottom: 1px solid var(--border); }
#apiStatus { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#apiStatus.error { color: var(--danger); white-space: normal; padding: 4px 0; }
#apiStatus .c2 { color: #3fb950; }
#apiStatus .c3 { color: var(--accent); }
#apiStatus .c4, #apiStatus .c5 { color: var(--danger); }
.api-res-headers { flex: 1; overflow: auto; padding: 6px 10px; font: 12px/1.6 var(--mono); }
.hdr { display: flex; gap: 12px; }
.hdr-name { flex: none; width: 220px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.hdr-value { word-break: break-all; }

/* About dialog */
dialog {
  width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 64px);
  padding: 0; color: var(--fg); background: var(--bg-2);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 12px 40px rgba(0,0,0,.4);
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
}
.dlg-head h2 { margin: 0; font-size: 14px; font-weight: 600; }
.dlg-head button { border: none; background: none; font-size: 18px; line-height: 1; padding: 0 6px; }
.dlg-body { overflow: auto; padding: 20px; }
.about { text-align: center; margin-bottom: 24px; }
.about .app-name { font-size: 26px; font-weight: 300; }
.about p { margin: 2px 0 12px; }
.about dl { display: grid; grid-template-columns: auto auto; justify-content: center; gap: 4px 12px; margin: 0; }
.about dt { color: var(--muted); text-align: right; }
.about dd { margin: 0; text-align: left; }
#licenses h3 { margin: 0 0 8px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; }
.lic-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.lic-table th, .lic-table td {
  padding: 6px 8px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border);
}
.lic-table th { font-size: 12px; font-weight: 600; color: var(--muted); }
.lic-table .muted { font-size: 12px; margin-top: 2px; }
details summary { cursor: pointer; padding: 4px 0; }
details pre {
  margin: 6px 0 0; padding: 10px; white-space: pre-wrap;
  font: 11px/1.5 var(--mono); background: var(--bg); border: 1px solid var(--border); border-radius: 4px;
}

/* Context menu */
#ctxmenu {
  position: fixed; z-index: 100; min-width: 150px; padding: 4px 0;
  background: var(--bg-3); border: 1px solid var(--border); border-radius: 4px;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
#ctxmenu div { padding: 4px 14px; cursor: pointer; }
#ctxmenu div:hover { background: var(--accent); color: #fff; }
#ctxmenu hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }
#ctxmenu.root-target .entry-only, #ctxmenu.root-target hr:not(.always) { display: none; }
