:root {
  --bg: #f6f5f3;
  --surface: #ffffff;
  --sunken: #faf9f7;
  --text: #1d1c1a;
  --muted: #8b867e;
  --line: #e9e6e1;
  --accent: #4a5fd8;
  --accent-soft: #eceffc;
  --ok: #1f8a5b;
  --ok-soft: #e7f4ed;
  --bad: #c2453a;
  --bad-soft: #fbeceb;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", system-ui, sans-serif;
  --radius: 14px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #111110;
  --surface: #181716;
  --sunken: #141413;
  --text: #ebe9e5;
  --muted: #8a857d;
  --line: #292826;
  --accent: #8b9cf5;
  --accent-soft: #20233a;
  --ok: #52c08d;
  --ok-soft: #14261d;
  --bad: #ef7f73;
  --bad-soft: #2c1a18;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111110;
    --surface: #181716;
    --sunken: #141413;
    --text: #ebe9e5;
    --muted: #8a857d;
    --line: #292826;
    --accent: #8b9cf5;
    --accent-soft: #20233a;
    --ok: #52c08d;
    --ok-soft: #14261d;
    --bad: #ef7f73;
    --bad-soft: #2c1a18;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* header */
.top {
  display: flex;
  align-items: center;
  gap: 28px;
  padding: 18px 32px 6px;
}
.brand { font-weight: 650; font-size: 16px; letter-spacing: -0.01em; }
.top .seg { flex: 1; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
}
.icon-btn:hover { background: var(--line); color: var(--text); }

/* segmented controls */
.seg { display: inline-flex; gap: 2px; }
.seg button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 6px 14px;
  border-radius: 9px;
  transition: background .15s, color .15s;
}
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--line); }
.seg.small button { padding: 2px 10px; font-size: 12px; border-radius: 7px; }
.seg.small button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); box-shadow: none; }

/* layout */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 20px;
  padding: 14px 32px 32px;
}
.column { display: flex; flex-direction: column; gap: 20px; min-height: 0; min-width: 0; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}
.grow-3 { flex: 3 1 0; }
.grow-2 { flex: 2 1 0; }
.fill { flex: 1 1 0; }

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px 6px;
}
.card-head h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
}

/* editor */
.editor {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  font: 13px/21px var(--mono);
}
.editor .gutter {
  flex: none;
  min-width: 44px;
  padding: 8px 10px 8px 20px;
  text-align: right;
  color: var(--muted);
  opacity: .55;
  overflow: hidden;
  user-select: none;
  white-space: pre;
}
.editor .gutter .bad { color: var(--bad); opacity: 1; font-weight: 600; }
.editor .stage { position: relative; flex: 1; min-width: 0; }
.editor .marks {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  padding: 8px 0;
}
.editor .marks div { height: 21px; }
.editor .marks .bad { background: var(--bad-soft); box-shadow: inset 2px 0 0 var(--bad); }
.editor textarea {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 8px 20px 8px 8px;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  font: inherit;
  white-space: pre;
  overflow: auto;
  tab-size: 2;
  caret-color: var(--accent);
}

.issues {
  list-style: none;
  margin: 0;
  padding: 10px 20px 14px;
  border-top: 1px solid var(--line);
  max-height: 30%;
  overflow: auto;
  background: var(--sunken);
}
.issues li {
  display: flex;
  gap: 12px;
  padding: 3px 0;
  color: var(--bad);
  cursor: pointer;
}
.issues .pos { flex: none; font: 12px/1.7 var(--mono); opacity: .8; min-width: 44px; }
.issues li:hover .msg { text-decoration: underline; }

/* tabs */
.tabs { display: flex; gap: 22px; padding: 0 20px; border-bottom: 1px solid var(--line); }
.tabs button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 14px 0 12px;
  margin-bottom: -1px;
  border-bottom: 2px solid transparent;
  transition: color .15s;
}
.tabs button:hover { color: var(--text); }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

.panel { flex: 1; min-height: 0; overflow: auto; padding: 22px 20px 24px; display: flex; flex-direction: column; gap: 18px; }
.panel-tests { padding: 0; gap: 0; }
.panel-tests .editor { border-bottom: 1px solid var(--line); }
.tests-bar { display: flex; align-items: flex-start; gap: 18px; padding: 14px 20px 18px; max-height: 45%; overflow: auto; }
.tests-bar .result { flex: 1; min-width: 0; }

/* form */
.form { display: grid; gap: 14px; }
.form.three { grid-template-columns: 1.1fr .9fr 1.2fr; }
label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}
input {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--sunken);
  font: 13px var(--mono);
  color: var(--text);
  outline: 0;
  transition: border-color .15s, box-shadow .15s;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

button.primary {
  border: 0;
  border-radius: 10px;
  padding: 8px 20px;
  background: var(--accent);
  color: var(--surface);
  font-weight: 550;
}
button.primary:hover { filter: brightness(1.06); }

/* results */
.result { display: flex; flex-direction: column; gap: 18px; }
.verdict { display: flex; align-items: center; gap: 12px; font-size: 22px; font-weight: 600; letter-spacing: -0.01em; }
.verdict::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: currentColor; }
.verdict.ok { color: var(--ok); }
.verdict.no { color: var(--bad); }
.verdict-row { display: flex; align-items: center; gap: 14px; }
.badge {
  padding: 2px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  cursor: help;
}
.error { color: var(--bad); font: 13px/1.6 var(--mono); white-space: pre-wrap; word-break: break-word; }
.note { color: var(--muted); font-size: 12px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font: 13px/1.6 var(--mono);
}
.count { color: var(--muted); font-size: 12px; font-weight: 500; }

/* explain tree */
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { margin-left: 7px; padding-left: 20px; border-left: 1px solid var(--line); }
.tree li { position: relative; padding: 5px 0 0; }
.tree ul > li::before {
  content: "";
  position: absolute;
  left: -20px;
  top: 17px;
  width: 14px;
  border-top: 1px solid var(--line);
}
.tree .step { display: inline-flex; align-items: center; gap: 8px; font: 13px/1.6 var(--mono); }
.tree .step::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.tree .rewrite { color: var(--muted); font-style: italic; }
.tree .rewrite::before { background: var(--muted); opacity: .5; }
.tree .tuple { color: var(--ok); }
.tree .tuple::before { background: var(--ok); }

/* test report */
.report-line { display: flex; gap: 16px; align-items: baseline; flex-wrap: wrap; }
.failure { padding: 10px 0 0; font: 12px/1.6 var(--mono); }
.failure .head { font-weight: 600; color: var(--bad); }
.failure .row { color: var(--muted); }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 14px 16px 24px; }
  .top { padding: 14px 16px 4px; gap: 14px; flex-wrap: wrap; }
  .grow-3, .grow-2, .fill { flex: none; }
  .editor { flex: none; height: 300px; }
  .panel-tests .editor { height: 260px; }
  .form.three { grid-template-columns: 1fr; }
  body { height: auto; display: block; }
  html, body { height: auto; }
  .panel { flex: none; }
  .tests-bar { max-height: none; }
}

@media (min-width: 901px) {
  body { height: 100vh; }
}
