:root {
  color-scheme: dark;
  --bg-canvas: #0b1016;
  --bg-surface: #111821;
  --bg-elevated: #17212d;
  --bg-input: #0e141c;
  --border-subtle: #222f3e;
  --border-strong: #324459;

  --text-primary: #e7eef6;
  --text-muted: #93a4b8;
  --text-dim: #6e8094;

  --accent: #d89e3c;
  --accent-hover: #e5ad4d;
  --accent-ink: #11161d;

  --ok: #3fb97c;
  --ok-bg: rgba(63, 185, 124, 0.11);
  --err: #f06a6a;
  --err-bg: rgba(240, 106, 106, 0.12);
  --warn: #e0a84b;
  --warn-bg: rgba(224, 168, 75, 0.12);

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

  --text-brand: 1rem;
  --text-body: 0.875rem;
  --text-sm: 0.8125rem;

  --radius-sm: 6px;
  --radius-md: 8px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-canvas);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-body);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  overflow: hidden;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* Top bar */
.topbar {
  height: 54px;
  flex: 0 0 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-primary);
  text-decoration: none;
  font-weight: 700;
  font-size: var(--text-brand);
  letter-spacing: -0.02em;
}

.brand-mark {
  color: var(--accent);
  flex: 0 0 auto;
}

.version-label {
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
  padding-right: 6px;
  border-right: 1px solid var(--border-subtle);
}

.example-picker select {
  height: 36px;
  max-width: 240px;
  padding: 0 28px 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  cursor: pointer;
  transition: border-color 150ms cubic-bezier(0.22, 1, 0.36, 1);
}

.example-picker select:hover,
.example-picker select:focus-visible {
  border-color: var(--border-strong);
  outline: none;
}

button {
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  cursor: pointer;
}

.ghost-btn {
  height: 36px;
  min-width: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 11px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  transition: background-color 150ms cubic-bezier(0.22, 1, 0.36, 1),
    color 150ms cubic-bezier(0.22, 1, 0.36, 1),
    transform 120ms cubic-bezier(0.22, 1, 0.36, 1);
}

.ghost-btn:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.ghost-btn:active {
  transform: scale(0.96);
}

.ghost-btn:focus-visible,
.primary-btn:focus-visible,
.tab:focus-visible,
.loc-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.compact-btn {
  height: 32px;
  padding: 0 10px;
}

.primary-btn {
  height: 36px;
  min-width: 82px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.2);
  transition: background-color 150ms cubic-bezier(0.22, 1, 0.36, 1),
    transform 120ms cubic-bezier(0.22, 1, 0.36, 1);
}

.primary-btn:hover {
  background: var(--accent-hover);
}

.primary-btn:active {
  transform: scale(0.96);
}

.primary-btn.is-running {
  background: var(--err);
  color: #fff;
}

/* Main split workspace */
.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 56%) minmax(0, 44%);
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.editor-pane {
  border-right: 1px solid var(--border-subtle);
  background: var(--bg-input);
}

.output-pane {
  background: var(--bg-canvas);
}

.pane-header {
  height: 42px;
  flex: 0 0 42px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
}

.pane-title {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  color: var(--text-primary);
  font-weight: 500;
}

.pane-meta {
  font-size: var(--text-sm);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Editor shell */
.editor-shell {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

.gutter {
  flex: 0 0 52px;
  padding: 14px 10px 14px 6px;
  text-align: right;
  font-family: var(--font-mono);
  font-size: var(--text-body);
  line-height: 22px;
  color: var(--text-dim);
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  overflow: hidden;
  user-select: none;
  font-variant-numeric: tabular-nums;
}

.gutter-line {
  height: 22px;
  line-height: 22px;
}

.gutter-line.is-error {
  color: var(--err);
  font-weight: 600;
}

.editor-stage {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.highlight-layer,
.code-input {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 14px 16px;
  border: 0;
  width: 100%;
  height: 100%;
  font-family: var(--font-mono);
  font-size: var(--text-body);
  line-height: 22px;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}

.highlight-layer {
  pointer-events: none;
  color: var(--text-primary);
  background: transparent;
  overflow: hidden;
}

.highlight-layer mark {
  background: rgba(240, 106, 106, 0.24);
  color: inherit;
  border-bottom: 2px solid var(--err);
  border-radius: 2px;
}

/* Keel syntax token palette */
.tok-comment {
  color: #6b768c;
  font-style: italic;
}

.tok-string {
  color: #8fd4a4;
}

.tok-number,
.tok-boolean {
  color: #f0b37e;
}

.tok-keyword {
  color: #d39bff;
}

.tok-self {
  color: #f4978e;
}

.tok-effect {
  color: #7cd4e8;
}

.tok-type {
  color: #f2c97d;
}

.tok-variant {
  color: #e8a87c;
}

.tok-builtin {
  color: #7eb6ff;
}

.tok-macro {
  color: #f29cb8;
}

.tok-fn {
  color: #9dc5ff;
}

.tok-operator {
  color: #a8b3cf;
}

.tok-punctuation {
  color: #8892a8;
}

.tok-invalid {
  color: var(--err);
  text-decoration: wavy underline var(--err);
}

.code-input {
  background: transparent;
  color: transparent;
  -webkit-text-fill-color: transparent;
  caret-color: var(--accent);
  resize: none;
  outline: none;
}

.code-input::selection {
  background: rgba(232, 179, 86, 0.28);
  color: transparent;
  -webkit-text-fill-color: transparent;
}

/* Tabs */
.output-header {
  padding-left: 8px;
}

.tablist {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

.tab {
  position: relative;
  height: 32px;
  padding: 0 11px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-weight: 500;
  transition: color 150ms cubic-bezier(0.22, 1, 0.36, 1),
    background-color 150ms cubic-bezier(0.22, 1, 0.36, 1);
}

.tab:hover {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.tab.is-active {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.panel {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.panel[hidden] {
  display: none;
}

/* Status bar */
.status-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  font-size: var(--text-sm);
}

.status-bar[data-state="ok"] {
  background: var(--ok-bg);
}

.status-bar[data-state="ok"] .status-text {
  color: var(--ok);
}

.status-bar[data-state="panic"],
.status-bar[data-state="error"] {
  background: var(--err-bg);
}

.status-bar[data-state="panic"] .status-text,
.status-bar[data-state="error"] .status-text {
  color: var(--err);
}

.status-bar[data-state="running"] {
  background: var(--warn-bg);
}

.status-bar[data-state="running"] .status-text {
  color: var(--warn);
}

.status-text {
  font-weight: 600;
}

.metrics-text {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* Output sections */
.output-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.out-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.out-section + .out-section {
  padding-top: 16px;
  border-top: 1px solid var(--border-subtle);
}

.out-heading {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-muted);
}

.code-block {
  margin: 0;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  font-family: var(--font-mono);
  font-size: var(--text-body);
  line-height: 1.55;
  color: var(--text-primary);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.code-block.is-error {
  border-color: rgba(240, 106, 106, 0.45);
}

.event-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.event-item {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr);
  gap: 8px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
}

.event-idx {
  color: var(--text-dim);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.event-val {
  color: var(--text-primary);
  word-break: break-word;
}

/* Diagnostics & panic locations */
.diag-header {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.diag-code {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--err);
  font-size: var(--text-body);
}

.diag-msg {
  color: var(--text-primary);
  font-size: var(--text-body);
  font-weight: 500;
}

.loc-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.loc-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.loc-btn {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  transition: border-color 150ms cubic-bezier(0.22, 1, 0.36, 1),
    background-color 150ms cubic-bezier(0.22, 1, 0.36, 1);
}

.loc-btn:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--bg-elevated);
}

.loc-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.loc-label {
  color: var(--text-muted);
}

.excerpt {
  margin: 0;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  border-left: 3px solid var(--err);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  line-height: 1.5;
  color: var(--text-primary);
  overflow-x: auto;
  white-space: pre;
}

.excerpt-caret {
  color: var(--err);
  font-weight: 700;
}

/* Phase dump panel */
.dump-toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-surface);
}

.syntax-outline {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 14px;
  max-height: 152px;
  overflow: auto;
  border-bottom: 1px solid var(--border-subtle);
  background: rgba(255, 255, 255, 0.015);
}

.outline-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.outline-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.sym-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.3;
  cursor: pointer;
  transition: border-color 140ms ease, background-color 140ms ease;
}

.sym-btn.is-child {
  opacity: 0.86;
  border-style: dashed;
}

.sym-btn:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--bg-elevated);
}

.sym-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.sym-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sym-kind {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 1px 5px;
  border-radius: 3px;
  background: rgba(157, 197, 255, 0.14);
  color: #9dc5ff;
}

.sym-kind-record,
.sym-kind-enum,
.sym-kind-interface {
  background: rgba(242, 201, 125, 0.15);
  color: #f2c97d;
}

.sym-kind-macro,
.sym-kind-pattern {
  background: rgba(242, 156, 184, 0.15);
  color: #f29cb8;
}

.sym-kind-const,
.sym-kind-field,
.sym-kind-enumVariant {
  background: rgba(143, 212, 164, 0.14);
  color: #8fd4a4;
}

.sym-kind-witness,
.sym-kind-extension,
.sym-kind-constraint {
  background: rgba(124, 212, 232, 0.14);
  color: #7cd4e8;
}

.sym-line {
  color: var(--text-dim);
  font-size: 11px;
}

.dump-content {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0;
  padding: 14px 16px;
  overflow: auto;
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  line-height: 1.55;
  color: var(--text-primary);
  white-space: pre;
}

/* Responsive mobile layout */
@media (max-width: 860px) {
  body {
    overflow: auto;
  }

  .topbar {
    height: auto;
    min-height: 54px;
    padding: 8px 12px;
    flex-wrap: wrap;
  }

  .version-label {
    display: none;
  }

  .example-picker select {
    max-width: 170px;
  }

  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(320px, 50dvh) minmax(320px, auto);
  }

  .editor-pane {
    border-right: 0;
    border-bottom: 1px solid var(--border-subtle);
  }

  #runtime-summary {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
