*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0d0d11;
  --surface:     #141419;
  --surface2:    #1c1c24;
  --border:      #26263a;
  --text:        #d0d0e8;
  --muted:       #52527a;
  --accent:      #9b87f5;
  --ll:          #c792ea;
  --lp:          #7ab8ff;
  --lv:          #d0d0e8;
  --ld:          #484870;
  --lpar:        #3e3e60;
  --redex-bg:    rgba(255, 155, 40, 0.13);
  --redex-line:  #ff8c00;
  --green:       #3ecf8e;
  --red:         #f07178;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
}

/* ── Header ────────────────────────────────────────────────────────────── */

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  height: 48px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.hd-logo {
  font-size: 22px;
  color: var(--ll);
  font-family: Georgia, 'Times New Roman', serif;
  line-height: 1;
  user-select: none;
}

.hd-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  letter-spacing: 0.015em;
  flex: 1;
}

.hd-strategy {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--muted);
}

.hd-strategy select {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 5px;
  cursor: pointer;
  outline: none;
  appearance: none;
  padding-right: 22px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%2352527a'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 7px center;
}

.hd-strategy select:focus { border-color: var(--accent); }

/* ── Layout ────────────────────────────────────────────────────────────── */

.layout {
  display: flex;
  height: calc(100vh - 48px - 36px);
}

/* ── Sidebar ───────────────────────────────────────────────────────────── */

aside {
  width: 210px;
  min-width: 210px;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  background: var(--surface);
  padding-bottom: 16px;
}

.sb-title {
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 14px 14px 8px;
}

.sb-group-label {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 10px 14px 5px;
  border-top: 1px solid var(--border);
}

.sb-group:first-of-type .sb-group-label {
  border-top: none;
}

.ex-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 5px 14px;
  cursor: pointer;
  transition: background 0.1s;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.ex-item:hover { background: var(--surface2); }
.ex-item:focus {
  outline: none;
  background: var(--surface2);
  box-shadow: inset 2px 0 0 var(--accent);
}

.ex-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
}

.ex-src {
  font-size: 11px;
  font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', Menlo, monospace;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Main ──────────────────────────────────────────────────────────────── */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* Input */
.input-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 14px 20px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.input-row textarea {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', Menlo, monospace;
  font-size: 13px;
  padding: 7px 12px;
  resize: none;
  outline: none;
  transition: border-color 0.15s;
  line-height: 1.5;
}

.input-row textarea:focus { border-color: var(--accent); }
.input-row textarea.has-error { border-color: var(--red); }

.btn-parse {
  background: var(--accent);
  color: #0d0d11;
  border: none;
  border-radius: 6px;
  padding: 8px 20px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.12s;
  flex-shrink: 0;
}

.btn-parse:hover { opacity: 0.82; }
.btn-parse:active { opacity: 0.65; }

.parse-error {
  padding: 0 20px 6px;
  font-size: 12px;
  color: var(--red);
  font-family: 'Fira Code', 'Cascadia Code', Menlo, monospace;
  display: none;
  flex-shrink: 0;
}

/* Expression display */
.expr-wrap {
  flex-shrink: 0;
  padding: 18px 22px 10px;
  min-height: 88px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.expr-display {
  font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', Menlo, monospace;
  font-size: 17px;
  line-height: 1.75;
  color: var(--text);
  word-break: break-all;
}

.expr-display.empty {
  font-family: inherit;
  font-size: 13px;
  color: var(--muted);
}

.interp-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 22px;
}

.interp-value {
  font-size: 13px;
  color: var(--green);
  font-weight: 500;
}

.badge-nf {
  display: inline-block;
  background: rgba(62, 207, 142, 0.12);
  color: var(--green);
  border: 1px solid rgba(62, 207, 142, 0.28);
  border-radius: 4px;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

/* Lambda term colors */
.ll   { color: var(--ll); }
.lp   { color: var(--lp); }
.ld   { color: var(--ld); }
.lv   { color: var(--lv); }
.lpar { color: var(--lpar); }

/* Redex highlight */
.redex {
  background: var(--redex-bg);
  border-bottom: 2px solid var(--redex-line);
  border-radius: 3px;
  padding: 0 2px;
  transition: background 0.1s;
}

/* Controls */
.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 20px;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.controls button {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  padding: 5px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s, color 0.1s;
  white-space: nowrap;
}

.controls button:hover:not(:disabled) {
  background: var(--border);
  border-color: var(--accent);
}

.controls button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.btn-run {
  background: var(--accent) !important;
  border-color: var(--accent) !important;
  color: #0d0d11 !important;
  font-weight: 700 !important;
}

.btn-run:hover:not(:disabled) {
  opacity: 0.82;
  background: var(--accent) !important;
}

.btn-run.running {
  background: transparent !important;
  border-color: var(--red) !important;
  color: var(--red) !important;
  font-weight: 700 !important;
}

.btn-run.running:hover:not(:disabled) {
  background: rgba(240, 113, 120, 0.1) !important;
}

.speed-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  margin-left: 4px;
}

.speed-wrap input[type=range] {
  width: 72px;
  accent-color: var(--accent);
  cursor: pointer;
}

.step-count {
  font-size: 12px;
  color: var(--muted);
  margin-left: auto;
  font-family: 'Fira Code', Menlo, monospace;
}

.kb-hint {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

kbd {
  display: inline-block;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  font-family: inherit;
  font-size: 10px;
  color: var(--muted);
}

/* History */
.history {
  flex: 1;
  overflow-y: auto;
  padding: 10px 20px 16px;
}

.history-empty {
  color: var(--muted);
  font-size: 13px;
  padding-top: 4px;
}

.h-entry {
  display: flex;
  gap: 14px;
  padding: 7px 6px;
  margin: 0 -6px;
  border-bottom: 1px solid var(--border);
  align-items: baseline;
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.1s;
}

.h-entry:last-child { border-bottom: none; }

.h-entry:hover { background: var(--surface2); }

.h-entry:focus {
  outline: none;
  box-shadow: inset 2px 0 0 var(--accent);
}

.h-entry.h-current {
  background: rgba(155, 135, 245, 0.1);
  box-shadow: inset 2px 0 0 var(--accent);
}

.h-entry.h-current .h-num { color: var(--accent); }

.h-num {
  font-size: 11px;
  font-family: 'Fira Code', Menlo, monospace;
  color: var(--muted);
  min-width: 20px;
  text-align: right;
  flex-shrink: 0;
  padding-top: 2px;
}

.h-body { flex: 1; min-width: 0; }

.h-expr {
  font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', Menlo, monospace;
  font-size: 12px;
  color: var(--text);
  word-break: break-all;
  line-height: 1.5;
}

.h-desc {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
  font-family: 'Fira Code', Menlo, monospace;
}

/* Footer */
footer {
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}

footer a {
  color: var(--muted);
  text-decoration: none;
}

footer a:hover { color: var(--text); }

/* Scrollbars */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* Responsive */
@media (max-width: 600px) {
  aside { display: none; }
  .kb-hint { display: none; }
  .layout { height: calc(100vh - 48px - 36px); }
}
