:root {
  --bg: #0d1117;
  --surface: #161b26;
  --surface-2: #1e2533;
  --line: #2c3547;
  --text: #e6e9f2;
  --muted: #8b93a7;
  --accent: #7c9cff;
  --green: #4ade80;
  --yellow: #fbbf24;
  --red: #f87171;
  --radius: 10px;
  --mono: Menlo, Consolas, ui-monospace, monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* {
  box-sizing: border-box;
}

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

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

/* ---- top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  flex-shrink: 0;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.2px;
}
.logo {
  white-space: nowrap;
  font-family: var(--mono);
  color: var(--accent);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2px 7px;
}
.global-controls {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}
.speed {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}
.speed output {
  font-family: var(--mono);
  color: var(--text);
  min-width: 38px;
}
.icon-btn {
  display: none;
  background: none;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  font-size: 18px;
  padding: 2px 10px;
  cursor: pointer;
}

/* ---- layout ---- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) 340px;
  min-height: 0;
}

.sidebar {
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 12px 10px 24px;
  background: var(--surface);
}
.sidebar h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin: 16px 8px 6px;
}
.sidebar a {
  display: block;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  line-height: 1.25;
}
.sidebar a small {
  display: block;
  color: var(--muted);
  font-size: 11.5px;
  margin-top: 2px;
}
.sidebar a:hover {
  background: var(--surface-2);
}
.sidebar a.active {
  background: rgba(124, 156, 255, 0.14);
  box-shadow: inset 3px 0 0 var(--accent);
}

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 12px 16px 14px;
  gap: 10px;
}
.scene-head h1 {
  margin: 0;
  font-size: 20px;
}
.scene-head p {
  margin: 2px 0 0;
  color: var(--muted);
}

.stage {
  position: relative;
  flex: 1;
  min-height: 260px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg);
}
.stage canvas {
  display: block;
}
.hud {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
  pointer-events: none;
  max-width: 70%;
}
.chip {
  background: rgba(22, 27, 38, 0.88);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}
.chip b {
  color: var(--text);
  font-weight: 600;
  margin-left: 4px;
}

/* ---- controls ---- */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
.controls .field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 11px;
  color: var(--muted);
}
.controls .sep {
  width: 1px;
  align-self: stretch;
  background: var(--line);
  margin: 0 4px;
}
.controls .break {
  flex-basis: 100%;
  height: 0;
}
.controls .range-out {
  font-family: var(--mono);
  color: var(--text);
}
input[type='text'],
input[type='number'],
select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  font-family: var(--mono);
  font-size: 13px;
  min-width: 0;
}
input[type='text']:focus,
input[type='number']:focus,
select:focus {
  outline: 2px solid rgba(124, 156, 255, 0.5);
  border-color: var(--accent);
}
input[type='range'] {
  accent-color: var(--accent);
}

.btn {
  background: var(--accent);
  color: #0d1117;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 7px 12px;
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover {
  filter: brightness(1.1);
}
.btn.secondary {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--line);
}
.btn.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--line);
  font-family: var(--sans);
}
.btn.danger {
  background: transparent;
  color: var(--red);
  border-color: rgba(248, 113, 113, 0.45);
}
.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  filter: none;
}

/* ---- side panel ---- */
.panel {
  border-left: 1px solid var(--line);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--surface);
}
.card {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}
.card h2 {
  margin: 0 0 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}
.about {
  line-height: 1.55;
  color: #c9cfdc;
}
.about p {
  margin: 0 0 8px;
}
.about p:last-child {
  margin-bottom: 0;
}
.about code,
.complexity code {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--surface-2);
  border-radius: 4px;
  padding: 1px 4px;
}
.about ul {
  margin: 4px 0 8px;
  padding-left: 18px;
}

.complexity {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.complexity td {
  padding: 5px 4px;
  border-top: 1px solid var(--line);
  vertical-align: top;
}
.complexity tr:first-child td {
  border-top: none;
}
.complexity td:nth-child(2) {
  font-family: var(--mono);
  white-space: nowrap;
  text-align: right;
}
.big-o-good {
  color: var(--green);
}
.big-o-ok {
  color: var(--yellow);
}
.big-o-bad {
  color: var(--red);
}

.code-name {
  text-transform: none;
  letter-spacing: 0;
  color: var(--accent);
  font-family: var(--mono);
  margin-left: 6px;
}
.code {
  margin: 0;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  overflow-x: auto;
  color: #aeb6c7;
}
.code .ln {
  display: block;
  padding: 0 6px;
  border-radius: 4px;
  white-space: pre;
  transition: background 0.15s;
}
.code .ln.hl {
  background: rgba(251, 191, 36, 0.16);
  color: var(--yellow);
}
.code:empty::before {
  content: 'Run an operation to see its code.';
  color: var(--muted);
  font-family: var(--sans);
}

.log-card {
  flex: 1;
  min-height: 160px;
  display: flex;
  flex-direction: column;
}
.log {
  list-style: none;
  margin: 0;
  padding: 0;
  font-family: var(--mono);
  font-size: 12px;
  overflow-y: auto;
  flex: 1;
  max-height: 260px;
}
.log li {
  padding: 4px 0;
  border-top: 1px solid var(--surface-2);
  color: #c9cfdc;
}
.log li:first-child {
  border-top: none;
}
.log li.ok {
  color: var(--green);
}
.log li.warn {
  color: var(--red);
}
.log li.step {
  color: var(--muted);
}
.log li.hint {
  color: var(--yellow);
}

/* ---- responsive ---- */
@media (max-width: 1180px) {
  .layout {
    grid-template-columns: 200px minmax(0, 1fr);
  }
  .panel {
    grid-column: 1 / -1;
    border-left: none;
    border-top: 1px solid var(--line);
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  }
  body {
    overflow: auto;
  }
  .layout {
    min-height: auto;
  }
  .main {
    min-height: 620px;
  }
}

@media (max-width: 760px) {
  .icon-btn {
    display: block;
  }
  .brand span:last-child {
    display: none;
  }
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }
  .sidebar {
    display: none;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
  body.menu-open .sidebar {
    display: block;
  }
  .main {
    padding: 10px 16px;
    min-height: 520px;
  }
  .speed span {
    display: none;
  }
  .speed input {
    width: 80px;
  }
}
