:root {
  --bg: #f4f7fb;
  --panel: #ffffff;
  --ink: #10233a;
  --muted: #4f6178;
  --accent: #1c7c54;
  --accent-2: #0f4c81;
  --line: #ced8e5;
  --editor: #ffffff;
  --editor-ink: #10233a;
  --error: #a61b1b;
  --warn: #b7791f;
  --good: #17633b;
  --ide-code-font-size: 0.95rem;
  --ide-console-font-size: 0.92rem;
  --ide-variables-font-size: 0.84rem;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, #dff6f2 0%, transparent 28%),
    radial-gradient(circle at 88% 5%, #e1eefb 0%, transparent 32%),
    var(--bg);
}

.app-shell {
  max-width: 1300px;
  margin: 0 auto;
  padding: 1rem;
  display: grid;
  gap: 0.9rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: linear-gradient(120deg, #185f5b, #0d5f93);
  color: #f7fffe;
  border-radius: 16px;
  padding: 0.9rem 1rem;
  box-shadow: 0 10px 24px rgba(13, 95, 147, 0.24);
}

.toolbar h1 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0.01em;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

button {
  border: 0;
  border-radius: 10px;
  padding: 0.55rem 0.85rem;
  font-weight: 700;
  cursor: pointer;
  background: var(--accent);
  color: #ffffff;
  transition: transform 120ms ease, filter 120ms ease, background-color 120ms ease;
}

button:hover {
  transform: translateY(-1px);
  filter: brightness(1.05);
}

button:active {
  transform: translateY(0);
}

button:disabled {
  cursor: not-allowed;
  background: repeating-linear-gradient(-45deg, #8b95a5, #8b95a5 10px, #a3acba 10px, #a3acba 20px);
  color: #eef2f7;
  transform: none;
  filter: none;
  opacity: 1;
}

#runBtn {
  background: linear-gradient(135deg, #ffb703, #fb8500);
  padding: 0.74rem 1.28rem;
  font-weight: 800;
  color: #1f1300;
  box-shadow: 0 12px 24px rgba(251, 133, 0, 0.34);
  border: 1px solid rgba(140, 60, 0, 0.22);
}

#runBtn.is-stop {
  background: linear-gradient(135deg, #ef4444, #b91c1c);
  color: #fff5f5;
  box-shadow: 0 12px 24px rgba(185, 28, 28, 0.28);
  border: 1px solid rgba(127, 29, 29, 0.3);
}

#debugStartBtn,
#debugStepBtn,
#debugStopBtn {
  background: var(--error);
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.6rem;
  min-width: 2.6rem;
  padding: 0.55rem;
}

.icon-button svg {
  width: 1.1rem;
  height: 1.1rem;
  stroke: currentColor;
  stroke-width: 2.2;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(320px, 0.9fr);
  gap: 0.9rem;
}

.panel {
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 620px;
  max-height: 760px;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  background: var(--panel);
}

.editor-panel {
  grid-template-rows: auto auto 1fr;
}

.panel-title {
  padding: 0.6rem 0.8rem;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
  font-weight: 700;
  background: linear-gradient(180deg, #eef3f9, #e2ebf6);
}

.panel-title-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.text-size-controls {
  display: inline-flex;
  gap: 0.35rem;
}

.text-size-btn {
  min-width: 2rem;
  padding: 0.25rem 0.4rem;
  border-radius: 8px;
  background: #ffffff;
  border: 1px solid #c8d7e8;
  color: #29405d;
  font-weight: 700;
  box-shadow: none;
}

.text-size-btn:hover {
  background: #f8fbff;
}

.text-size-glyph {
  font-size: 0.95rem;
  line-height: 1;
}

.text-size-glyph.small {
  font-size: 0.8rem;
}

.text-size-mark {
  font-size: 0.7rem;
  vertical-align: super;
  margin-left: 0.05rem;
}

.editor-tabs {
  display: flex;
  align-items: stretch;
  gap: 0.15rem;
  padding: 0.45rem 0.55rem 0;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #f8fafc, #edf3fa);
  overflow-x: auto;
  position: relative;
}

.editor-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  border: 1px solid #9bb2cf;
  border-bottom-color: #dce6f3;
  background: linear-gradient(180deg, #f5f9ff, #e7eef8);
  color: #29405d;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  padding: 0;
  font-size: 0.8rem;
  font-weight: 650;
  white-space: nowrap;
  margin-bottom: -1px;
}

.editor-tab.active {
  border-bottom-color: #ffffff;
  background: #ffffff;
  color: #10233a;
}

.tab-open-btn,
.tab-settings-btn,
.tab-add-btn {
  border: 0;
  margin: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  box-shadow: none;
  transform: none;
  filter: none;
}

.tab-open-btn:hover,
.tab-settings-btn:hover,
.tab-add-btn:hover,
.tab-open-btn:active,
.tab-settings-btn:active,
.tab-add-btn:active {
  transform: none;
  filter: none;
}

.tab-open-btn {
  padding: 0.38rem 0.72rem;
  font: inherit;
}

.tab-settings-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.4rem;
  padding: 0.34rem 0.45rem;
  border-left: 1px solid #ced8e5;
  font-size: 1rem;
  line-height: 1;
  color: #5b6f86;
}

.tab-settings-btn:hover {
  background: #edf6ff;
  color: #0f4c81;
}

.tab-add-btn {
  margin-left: auto;
  min-width: 2.6rem;
  padding: 0.38rem 0.78rem;
  border: 1px dashed #9bb2cf;
  border-bottom-color: #c8d7e8;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  color: #29405d;
  font-size: 1.25rem;
  font-weight: 700;
}

.tab-add-btn:hover {
  background: #ffffff;
}

.tab-context-menu {
  position: absolute;
  min-width: 9rem;
  background: #ffffff;
  border: 1px solid #c8d7e8;
  border-radius: 12px;
  box-shadow: 0 18px 34px rgba(15, 76, 129, 0.2);
  padding: 0.35rem;
  z-index: 20;
}

.tab-context-menu.hidden {
  display: none;
}

.tab-menu-item {
  width: 100%;
  display: block;
  text-align: left;
  border-radius: 8px;
  background: transparent;
  color: #24364d;
  padding: 0.55rem 0.7rem;
  font-weight: 650;
}

.tab-menu-item:hover {
  background: #edf6ff;
}

.tab-menu-item.danger {
  color: #b42318;
}

.tab-menu-item.danger:hover {
  background: #fff1f1;
}

.editor-body {
  position: relative;
  min-height: 0;
}

.code-editor {
  position: relative;
  height: 100%;
  display: grid;
  grid-template-columns: 3.6rem 1fr;
  background: #ffffff;
}

#editor {
  grid-column: 2;
  width: 100%;
  height: 100%;
  resize: none;
  border: 0;
  outline: none;
  padding: 0.9rem;
  font-family: "JetBrains Mono", "Fira Code", "Menlo", monospace;
  font-size: var(--ide-code-font-size);
  line-height: 1.45;
  white-space: pre;
  overflow-wrap: normal;
  word-break: normal;
  background: transparent;
  color: transparent;
  caret-color: #111827;
  tab-size: 4;
  overflow: auto;
}

.line-numbers {
  grid-column: 1;
  margin: 0;
  padding: 0.9rem 0.45rem 0.9rem 0;
  border-right: 1px solid #d8e2f0;
  text-align: right;
  color: #7b91ae;
  background: #f1f6fc;
  overflow: hidden;
  user-select: none;
}

.line-number {
  display: block;
  padding-right: 0.05rem;
  cursor: pointer;
}

.line-number.breakpoint {
  background: #fee2e2;
  color: #991b1b;
  border-right: 2px solid #ef4444;
}

.line-number.current-debug {
  background: #dff3ff;
  color: #0f4c81;
  border-right: 2px solid #0f4c81;
}

.highlight-layer {
  position: absolute;
  top: 0;
  left: 3.6rem;
  right: 0;
  bottom: 0;
  margin: 0;
  padding: 0.9rem;
  overflow: hidden;
  pointer-events: none;
  white-space: pre;
}

.line-numbers,
.highlight-layer {
  font-family: "JetBrains Mono", "Fira Code", "Menlo", monospace;
  font-size: var(--ide-code-font-size);
  line-height: 1.45;
}

.token-keyword {
  color: #0f4c81;
  font-weight: 600;
}

.token-builtin {
  color: #8b5cf6;
}

.token-string {
  color: #0f766e;
}

.token-comment {
  color: #64748b;
  font-style: italic;
}

.token-number {
  color: #b45309;
}

.token-operator {
  color: #334155;
}

.autocomplete {
  position: absolute;
  left: calc(3.6rem + 1rem);
  top: 1rem;
  min-width: 220px;
  max-height: 220px;
  overflow: auto;
  background: #ffffff;
  border: 1px solid #c8d7e8;
  border-radius: 10px;
  box-shadow: 0 16px 30px rgba(15, 76, 129, 0.18);
  z-index: 10;
}

.autocomplete.hidden {
  display: none;
}

.autocomplete-item {
  padding: 0.45rem 0.7rem;
  font-family: "JetBrains Mono", "Fira Code", "Menlo", monospace;
  font-size: 0.84rem;
  cursor: pointer;
}

.autocomplete-item.active,
.autocomplete-item:hover {
  background: #edf6ff;
}

.console-stack {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(180px, 1fr) minmax(200px, 0.9fr);
}

.runtime-status-bar {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.8rem;
  border-bottom: 1px solid var(--line);
  background: #fbfdff;
  color: var(--muted);
  font-size: 0.84rem;
}

#runtimeStatus.ready {
  color: var(--good);
}

#runtimeStatus.error {
  color: var(--error);
}

#debugStatus.active {
  color: var(--accent-2);
}

#console,
#variablesView {
  margin: 0;
  padding: 0.9rem;
  overflow: auto;
  white-space: pre-wrap;
  font-family: "JetBrains Mono", "Fira Code", "Menlo", monospace;
}

#console {
  background: #0b1322;
  color: #c6defe;
  font-size: var(--ide-console-font-size);
}

.console-error {
  color: #fecaca;
}

.variables-panel {
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 0;
}

.variables-title {
  padding: 0.6rem 0.8rem;
  font-size: 0.84rem;
  font-weight: 700;
  background: #f8fbff;
  border-bottom: 1px solid var(--line);
}

#variablesView {
  background: #f9fbfe;
  color: #24364d;
  font-size: var(--ide-variables-font-size);
}

.reference {
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  background: #ffffff;
}

.reference-collapsible summary {
  list-style: none;
  cursor: pointer;
  padding: 0.9rem 1rem;
  font-weight: 800;
  background: linear-gradient(180deg, #eef3f9, #e2ebf6);
}

.reference-collapsible summary::-webkit-details-marker {
  display: none;
}

.ref-table {
  width: 100%;
  border-collapse: collapse;
}

.ref-table th,
.ref-table td {
  border-top: 1px solid var(--line);
  padding: 0.72rem 0.8rem;
  text-align: left;
  vertical-align: top;
  font-size: 0.9rem;
}

.ref-table th {
  background: #f8fbff;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #5a6f88;
}

code {
  font-family: "JetBrains Mono", "Fira Code", "Menlo", monospace;
  font-size: 0.92em;
}

.download-bar {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
}

.download-pdf-btn {
  background: var(--accent-2);
}

.inline-game-window {
  display: none;
  margin-bottom: 0.9rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  background: linear-gradient(180deg, #08111f, #0b1522);
}

.inline-game-window.is-active {
  display: grid;
}

.inline-game-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  padding: 0.75rem 1rem;
  background: linear-gradient(120deg, #0d5f93, #185f5b);
  color: #f4fbff;
  font-weight: 700;
}

.inline-game-window .canvas-wrap {
  display: grid;
  place-items: center;
  padding: 1rem;
  background: #08111f;
}

.inline-game-window canvas {
  background: #000;
  border: 1px solid rgba(190, 220, 255, 0.2);
  box-shadow: 0 16px 32px rgba(0, 0, 0, 0.35);
  image-rendering: pixelated;
}

.inline-game-window #gameLog {
  margin: 0;
  padding: 0.75rem 1rem 1rem;
  max-height: 180px;
  overflow: auto;
  border-top: 1px solid rgba(190, 220, 255, 0.16);
  background: #091525;
  color: #aed3ff;
  font-family: "JetBrains Mono", "Fira Code", "Menlo", monospace;
  font-size: 0.8rem;
  white-space: pre-wrap;
}

body.popup-mode {
  overflow: hidden;
}

body.popup-mode .app-shell {
  max-width: none;
  width: 100vw;
  height: 100vh;
  min-height: 100vh;
  padding: 0;
  gap: 0;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
}

body.popup-mode .toolbar,
body.popup-mode .panel,
body.popup-mode .reference {
  border-radius: 0;
}

body.popup-mode .toolbar {
  margin: 0;
  flex: 0 0 auto;
}

body.popup-mode .workspace {
  gap: 0;
  min-height: 0;
  align-items: stretch;
}

body.popup-mode .panel {
  min-height: 0;
  max-height: none;
  height: 100%;
}

body.popup-mode .editor-body,
body.popup-mode .console-stack {
  min-height: 0;
  height: 100%;
}

body.popup-mode .download-bar {
  flex: 0 0 auto;
  padding: 0.75rem 1.25rem 1.25rem;
}

body.popup-mode .reference {
  flex: 0 0 auto;
  max-height: 32vh;
  overflow: auto;
}

body.embed-mode .app-shell {
  max-width: none;
  padding: 0;
  gap: 0.75rem;
}

body.embed-mode .toolbar,
body.embed-mode .workspace,
body.embed-mode .reference,
body.embed-mode .download-bar {
  border-radius: 0;
}

body.demo-mode {
  background: #08111f;
  overflow: hidden;
  height: 400px;
}

body.demo-mode .app-shell {
  max-width: none;
  width: 600px;
  padding: 0;
}

html:has(body.demo-mode) {
  overflow: hidden;
}

body.game-only-mode {
  margin: 0;
  overflow: hidden;
  background: #000;
  width: 600px;
  height: 400px;
}

body.game-only-mode .app-shell,
body.game-only-mode #openFilesInput,
body.game-only-mode #openFolderInput {
  display: none;
}

body.game-only-mode #inlineGameWindow {
  display: block;
  margin: 0;
  border: 0;
  border-radius: 0;
  background: #000;
  width: 600px;
  height: 400px;
  overflow: hidden;
}

body.game-only-mode .game-only-canvas-wrap {
  display: block;
  padding: 0;
  background: #000;
  width: 600px;
  height: 400px;
  overflow: hidden;
}

body.game-only-mode canvas {
  display: block;
  width: 600px;
  height: 400px;
  max-width: none;
  border: 0;
  box-shadow: none;
}

body.demo-mode .toolbar,
body.demo-mode .editor-panel,
body.demo-mode .reference,
body.demo-mode .download-bar,
body.demo-mode #console,
body.demo-mode .variables-panel,
body.demo-mode .panel-title,
body.demo-mode .runtime-status-bar,
body.demo-mode .inline-game-header,
body.demo-mode .inline-game-window #gameLog {
  display: none;
}

body.demo-mode .workspace {
  display: block;
}

body.demo-mode .console-panel {
  min-height: 0;
  max-height: none;
  border: 0;
  border-radius: 0;
}

body.demo-mode .console-stack {
  gap: 0;
  min-height: 0;
  background: #08111f;
}

body.demo-mode .inline-game-window {
  display: grid;
  margin: 0;
  min-height: 0;
  border: 0;
  border-radius: 0;
}

body.demo-mode .inline-game-window .canvas-wrap {
  padding: 0;
  place-items: start;
}

body.demo-mode .inline-game-window canvas {
  width: 600px;
  height: 400px;
  max-width: 100%;
  max-height: none;
  border: 0;
  box-shadow: none;
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .panel,
  .reference {
    min-height: 420px;
    max-height: none;
  }

  .runtime-status-bar {
    flex-direction: column;
  }
}
