:root {
  --bg: #0b0d1a;
  --glass: rgba(255, 255, 255, 0.06);
  --glass-strong: rgba(255, 255, 255, 0.1);
  --border: rgba(255, 255, 255, 0.12);
  --border-soft: rgba(255, 255, 255, 0.07);
  --text: #eef0ff;
  --muted: rgba(230, 233, 255, 0.55);
  --accent: #8b9bff;
  --accent-2: #5ee0c8;
  --danger: #ff8a9a;
  --ok: #6ee7b7;
  --radius: 18px;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: Inter, system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow-x: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }

/* Background */
.bg { position: fixed; inset: 0; z-index: -1; overflow: hidden;
  background: radial-gradient(1200px 800px at 10% -10%, #1c2150 0%, transparent 60%),
              radial-gradient(900px 700px at 110% 110%, #10324a 0%, transparent 60%), var(--bg); }
.orb { position: absolute; border-radius: 50%; filter: blur(70px); opacity: 0.55; animation: float 22s ease-in-out infinite; }
.o1 { width: 460px; height: 460px; background: #5b5bf0; top: -120px; left: 8%; }
.o2 { width: 380px; height: 380px; background: #12b5a5; bottom: -120px; right: 6%; animation-delay: -7s; }
.o3 { width: 300px; height: 300px; background: #c25ce6; top: 40%; left: 45%; opacity: 0.3; animation-delay: -14s; }
@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(40px, 30px) scale(1.08); }
}
@media (prefers-reduced-motion: reduce) { .orb { animation: none; } }

.glass {
  background: linear-gradient(160deg, rgba(255, 255, 255, 0.09), rgba(255, 255, 255, 0.03));
  border: 1px solid var(--border);
  backdrop-filter: blur(22px) saturate(140%);
  -webkit-backdrop-filter: blur(22px) saturate(140%);
  box-shadow: 0 20px 50px -20px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

/* Top bar */
.topbar {
  margin: 16px 16px 0;
  padding: 10px 12px 10px 16px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 16px;
  position: relative;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: -0.01em; color: var(--text); text-decoration: none; }
.logo { width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: 0 6px 18px -6px var(--accent); }
.logo svg { color: #0b0d1a; stroke-width: 2.6; }
.toolbar { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; }
.select { position: relative; display: inline-flex; align-items: center; }
.select select {
  appearance: none; font: inherit; font-weight: 500; color: var(--text);
  background: var(--glass-strong); border: 1px solid var(--border); border-radius: 12px;
  padding: 8px 36px 8px 14px; min-width: 170px; outline: none; cursor: pointer;
}
.select select:focus-visible { border-color: var(--accent); }
.select select option { background: #1a1d33; color: var(--text); }
.select svg { position: absolute; right: 12px; pointer-events: none; width: 16px; color: var(--muted); }
.version { font-size: 12px; color: var(--muted); font-family: var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.actions { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  background: transparent; border: 1px solid transparent; color: var(--muted); transition: 0.15s;
}
.icon-btn:hover, .icon-btn[aria-expanded='true'] { background: var(--glass-strong); border-color: var(--border-soft); color: var(--text); }
.icon-btn.small { width: 30px; height: 30px; }
.icon-btn.small svg { width: 16px; height: 16px; }
.run-btn {
  display: inline-flex; align-items: center; gap: 8px; margin-left: 6px;
  padding: 9px 14px 9px 14px; border-radius: 12px; border: 0; font-weight: 600; color: #0b0d1a;
  background: linear-gradient(135deg, #a3afff, #6ee7d2);
  box-shadow: 0 8px 24px -10px rgba(120, 140, 255, 0.9); transition: transform 0.12s, filter 0.15s;
}
.run-btn:hover { filter: brightness(1.07); }
.run-btn:active { transform: translateY(1px); }
.run-btn svg { width: 15px; height: 15px; fill: currentColor; stroke: none; }
.run-btn kbd { font-family: var(--mono); font-size: 11px; padding: 2px 6px; border-radius: 6px; background: rgba(11, 13, 26, 0.14); }
.run-btn.busy { pointer-events: none; filter: saturate(0.6); }
.run-btn.busy svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Workspace */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(320px, 1fr);
  gap: 16px;
  padding: 16px;
  min-height: 0;
  position: relative;
}
.workspace.with-assist { grid-template-columns: minmax(0, 1.5fr) minmax(280px, 0.9fr) minmax(300px, 0.9fr); }
.panel { border-radius: var(--radius); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.panel-head {
  display: flex; align-items: center; gap: 10px; padding: 11px 14px;
  border-bottom: 1px solid var(--border-soft); font-size: 13px; min-height: 48px;
}
.label { font-weight: 600; }
.hint { color: var(--muted); font-family: var(--mono); font-size: 12px; }
.dots { display: inline-flex; gap: 6px; }
.dots i { width: 10px; height: 10px; border-radius: 50%; background: rgba(255, 255, 255, 0.16); }
.filename { font-family: var(--mono); font-size: 12.5px; padding: 3px 10px; border-radius: 8px; background: var(--glass-strong); }
.saved { margin-left: auto; font-size: 12px; color: var(--muted); opacity: 0; transition: opacity 0.3s; }
.saved.show { opacity: 1; }

.editor-panel { min-height: 520px; }
.editor { flex: 1; min-height: 0; position: relative; }
.CodeMirror {
  position: absolute; inset: 0; height: 100%;
  background: transparent; color: #e6e8ff; font-family: var(--mono); font-size: 14px; line-height: 1.6;
}
.CodeMirror-gutters { background: transparent; border-right: 1px solid var(--border-soft); }
.CodeMirror-linenumber { color: rgba(230, 233, 255, 0.28); padding: 0 10px 0 8px; }
.CodeMirror-cursor { border-left: 2px solid var(--accent-2); }
.CodeMirror-selected, .CodeMirror-focused .CodeMirror-selected { background: rgba(139, 155, 255, 0.22) !important; }
.CodeMirror-activeline-background { background: rgba(255, 255, 255, 0.035); }
.CodeMirror-matchingbracket { color: var(--accent-2) !important; text-decoration: underline; }
.CodeMirror-lines { padding: 12px 0; }
.cm-keyword { color: #c4a7ff; }
.cm-def, .cm-variable-2 { color: #8fd3ff; }
.cm-variable, .cm-property { color: #e6e8ff; }
.cm-string, .cm-string-2 { color: #9ff0c4; }
.cm-number, .cm-atom { color: #ffc58f; }
.cm-comment { color: rgba(230, 233, 255, 0.4); font-style: italic; }
.cm-builtin, .cm-type, .cm-variable-3 { color: #7fe3e0; }
.cm-operator { color: #ffb3c8; }
.cm-meta, .cm-tag { color: #ff9bb3; }
.cm-attribute, .cm-qualifier { color: #ffd98f; }

.side { display: grid; grid-template-rows: minmax(140px, 0.45fr) minmax(240px, 1fr); gap: 16px; min-height: 0; }
textarea#stdin {
  flex: 1; resize: none; border: 0; outline: none; background: transparent; color: var(--text);
  font-family: var(--mono); font-size: 13.5px; line-height: 1.6; padding: 12px 14px;
}
textarea#stdin::placeholder { color: rgba(230, 233, 255, 0.32); }
.status { font-family: var(--mono); font-size: 12px; padding: 3px 9px; border-radius: 999px; display: none; }
.status.ok { display: inline-block; color: var(--ok); background: rgba(110, 231, 183, 0.1); }
.status.err { display: inline-block; color: var(--danger); background: rgba(255, 138, 154, 0.1); }
.status.run { display: inline-block; color: var(--accent); background: rgba(139, 155, 255, 0.12); }
.text-btn { margin-left: auto; background: none; border: 0; color: var(--muted); font-size: 12.5px; padding: 4px 8px; border-radius: 8px; }
.text-btn:hover { color: var(--text); background: var(--glass-strong); }
.output {
  flex: 1; margin: 0; padding: 12px 14px; overflow: auto; font-family: var(--mono); font-size: 13.5px;
  line-height: 1.6; white-space: pre-wrap; word-break: break-word;
}
.output .stderr { color: var(--danger); }
.output .compile { color: #ffd98f; }
.output .meta { color: var(--muted); font-size: 12px; display: block; margin-top: 10px; }

/* Helper drawer */
.assist { display: none; }
.workspace.with-assist .assist { display: flex; animation: slide 0.22s ease-out; }
@keyframes slide { from { opacity: 0; transform: translateX(14px); } }
.chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 14px 4px; }
.chip {
  font-size: 12.5px; padding: 6px 12px; border-radius: 999px; background: var(--glass);
  border: 1px solid var(--border-soft); color: var(--text); transition: 0.15s;
}
.chip:hover { background: var(--glass-strong); border-color: var(--border); }
.chip:disabled { opacity: 0.5; cursor: default; }
.assist-body { flex: 1; overflow: auto; padding: 10px 16px 14px; font-size: 14px; line-height: 1.6; }
.assist-body h1, .assist-body h2, .assist-body h3, .assist-body h4 { font-size: 14.5px; margin: 14px 0 6px; }
.assist-body p { margin: 8px 0; }
.assist-body ul, .assist-body ol { padding-left: 20px; margin: 8px 0; }
.assist-body code { font-family: var(--mono); font-size: 12.5px; background: var(--glass-strong); padding: 1px 6px; border-radius: 6px; }
.assist-body .block { position: relative; margin: 10px 0; border: 1px solid var(--border-soft); border-radius: 12px; background: rgba(0, 0, 0, 0.25); }
.assist-body .block pre { margin: 0; padding: 12px; overflow: auto; font-family: var(--mono); font-size: 12.5px; line-height: 1.55; }
.assist-body .block code { background: none; padding: 0; }
.assist-body .block-actions { display: flex; gap: 4px; justify-content: flex-end; padding: 6px 8px; border-top: 1px solid var(--border-soft); }
.assist-body .block-actions button { font-size: 12px; background: none; border: 0; color: var(--muted); padding: 4px 8px; border-radius: 7px; }
.assist-body .block-actions button:hover { background: var(--glass-strong); color: var(--text); }
.assist-body .q { color: var(--muted); font-size: 12.5px; border-left: 2px solid var(--accent); padding-left: 10px; margin: 4px 0 10px; }
.typing::after { content: '▍'; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }
.ask { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border-soft); }
.ask input {
  flex: 1; font: inherit; font-size: 13.5px; color: var(--text); background: var(--glass);
  border: 1px solid var(--border-soft); border-radius: 11px; padding: 9px 12px; outline: none;
}
.ask input:focus { border-color: var(--accent); }
.ask button { width: 38px; border-radius: 11px; border: 0; background: var(--glass-strong); display: grid; place-items: center; }
.ask button:hover { background: rgba(139, 155, 255, 0.3); }

.foot { text-align: center; font-size: 12px; color: var(--muted); padding: 0 16px 16px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px); opacity: 0;
  padding: 9px 16px; border-radius: 12px; font-size: 13px; pointer-events: none; transition: 0.25s;
  background: rgba(30, 33, 60, 0.85); border: 1px solid var(--border); backdrop-filter: blur(14px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Responsive */
@media (max-width: 1100px) {
  .workspace.with-assist { grid-template-columns: minmax(0, 1.4fr) minmax(300px, 1fr); }
  .workspace.with-assist .side { display: none; }
}
@media (max-width: 820px) {
  .topbar { flex-wrap: wrap; }
  .toolbar { order: 3; flex-basis: 100%; }
  .actions { margin-left: auto; }
  .run-btn kbd, .version { display: none; }
  .workspace, .workspace.with-assist { grid-template-columns: 1fr; }
  .workspace.with-assist .side { display: grid; }
  .editor-panel { min-height: 380px; }
  .assist { min-height: 420px; }
}
