:root {
  /* Surfaces — elevation = slightly lighter fill */
  --bg: #1e1e1e;
  --panel: #252526;
  --raised: #2d2d2d;
  --card: #2d2d30;
  --card-title: #37373d;
  --btn: #3a3d41;
  --hover: #2a2a2a;

  --border: #3c3c3c;

  /* Text ladder */
  --text: #d4d4d4;
  --text-label: #bdbdbd;
  --text-muted: #9d9d9d;
  --text-faint: #808080;
  --text-ghost: #6d6d6d;

  /* Color channels — blue = interactive, teal = data */
  --accent: #0e639c;
  --accent-tint: #0e639c44;
  --data: #4ec9b0;
  --data-bright: #7ee0c8;
  --error: #f48771;
  --error-bg: #3a1d1d;

  --r-sm: 3px;
  --r-md: 4px;
  --r-lg: 6px;
  --r-track: 999px;

  --sh-pop: 0 6px 20px rgba(0, 0, 0, 0.5);

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --mono: 'SF Mono', Menlo, Consolas, monospace;
}

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

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  line-height: 1.4;
}

a {
  text-decoration: none;
  color: inherit;
}

button {
  font-family: inherit;
  cursor: pointer;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  background: var(--raised);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar h1 {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

#engine-status {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--data);
}

.topbar-note {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-faint);
  white-space: nowrap;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.toolbar-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--btn);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 2px 8px;
  font-size: 11px;
  color: var(--text);
  transition: background 0.1s, border-color 0.1s, color 0.1s;
}

.toolbar-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.toolbar-btn svg {
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

/* Panel grid — 4px resize-handle tracks separate the panels */
#content {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-areas:
    "query query query"
    "hrow  hrow  hrow"
    "input vcol  output";
  grid-template-columns: minmax(0, var(--col-l, 1fr)) 4px minmax(0, var(--col-r, 1fr));
  grid-template-rows: min(var(--query-h, 82px), 60%) 4px minmax(0, 1fr);
}

.resize-handle {
  position: relative;
  z-index: 5;
  background: var(--border);
  touch-action: none;
  user-select: none;
  transition: background 0.1s;
}

/* invisible 3px halo so the 4px strip is easier to grab */
.resize-handle::after {
  content: "";
  position: absolute;
  inset: -3px;
}

.resize-handle:hover,
.resize-handle.dragging {
  background: var(--accent);
}

.resize-handle:focus-visible {
  outline: none;
  background: var(--accent);
}

#row-handle {
  grid-area: hrow;
  cursor: row-resize;
}

#col-handle {
  grid-area: vcol;
  cursor: col-resize;
}

.panel {
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

#query {
  grid-area: query;
  position: relative;
  z-index: 100;
}

#input {
  grid-area: input;
}

#output {
  grid-area: output;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 26px;
  padding: 3px 8px 3px 10px;
  background: var(--raised);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
}

.panel-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

.panel-title .hint {
  font-size: 9px;
  font-weight: 400;
  color: var(--text-faint);
}

.panel-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* Query well */
#query .panel-body {
  flex-direction: row;
  padding: 8px;
}

#filter-container {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  overflow: hidden;
  transition: border-color 0.1s;
}

#filter-container:focus-within {
  border-color: var(--accent);
}

/* Editors — fill panel bodies edge to edge */
#input-json-container,
#output-json-container {
  flex: 1;
  min-height: 0;
  background: var(--bg);
}

/* Micro button (panel-header toolbar) */
.micro-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--btn);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 2px 7px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text);
  transition: background 0.1s, border-color 0.1s, color 0.1s;
}

.micro-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

#flag-count {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 400;
  color: var(--data);
}

#flag-count:empty {
  display: none;
}

.micro-btn:hover #flag-count {
  color: var(--data-bright);
}

/* Popovers — floating menus, the only shadowed surfaces */
.popover {
  position: absolute;
  z-index: 1000;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-pop);
  padding: 4px;
  transition: opacity 0.12s, transform 0.12s;
  opacity: 1;
  transform: translateY(0);
}

.popover.hidden {
  opacity: 0;
  transform: translateY(-4px);
  pointer-events: none;
}

#options-popover {
  top: calc(100% + 4px);
  right: 8px;
  width: 280px;
  max-width: calc(100vw - 24px);
}

.popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 3px 4px 5px 8px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.popover-header p {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.popover-close {
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--r-sm);
  color: var(--text-faint);
  font-size: 11px;
  line-height: 1;
  transition: background 0.1s, color 0.1s;
}

.popover-close:hover {
  background: var(--btn);
  color: var(--text);
}

/* Save / Saved — snapshot controls in the top bar */
.save-group {
  position: relative;
  display: flex;
  gap: 6px;
}

#saved-count {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 400;
  color: var(--data);
}

#saved-count:empty {
  display: none;
}

.toolbar-btn:hover #saved-count {
  color: var(--data-bright);
}

#save-popover,
#saved-popover {
  top: calc(100% + 9px);
  right: 0;
  max-width: calc(100vw - 24px);
}

#save-popover {
  width: 260px;
}

#saved-popover {
  width: 320px;
}

.save-form {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 6px 4px;
}

#save-name {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 3px 6px;
  font-family: var(--font);
  font-size: 11px;
  color: var(--text);
}

#save-name:focus {
  outline: none;
  border-color: var(--accent);
}

#save-name::placeholder {
  color: var(--text-ghost);
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--r-sm);
  padding: 3px 10px;
  font-size: 11px;
  font-weight: 600;
  transition: background 0.1s;
}

.primary-btn:hover {
  background: #1177b3;
}

.popover-hint {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-faint);
  padding: 2px 8px 4px;
}

.popover-hint.error {
  color: var(--error);
}

.popover-hint:empty {
  display: none;
}

#saves-list {
  list-style: none;
  max-height: 260px;
  overflow-y: auto;
}

#saves-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 20px;
  padding: 4px 8px;
  border-radius: var(--r-md);
  cursor: pointer;
}

#saves-list li:hover {
  background: var(--hover);
}

.save-meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.save-name {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-label);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.save-query {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--data);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.save-date {
  flex-shrink: 0;
  font-size: 9px;
  color: var(--text-faint);
  white-space: nowrap;
}

.save-delete {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--r-sm);
  color: var(--text-faint);
  font-size: 10px;
  line-height: 1;
  transition: background 0.1s, color 0.1s;
}

.save-delete:hover {
  background: var(--error-bg);
  color: var(--error);
}

.saves-empty {
  justify-content: center;
  padding: 12px;
  font-size: 11px;
  color: var(--text-ghost);
  cursor: default;
}

#saves-list li.saves-empty:hover {
  background: none;
}

#options {
  list-style: none;
}

#options li {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 20px;
  padding: 4px 8px;
  border-radius: var(--r-md);
}

#options li:hover {
  background: var(--hover);
}

#options label {
  flex: 1;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-label);
  cursor: pointer;
}

#options li:has(input:checked) label {
  color: var(--data);
}

/* Checkboxes — wells that fill accent when checked */
input[type="checkbox"] {
  appearance: none;
  width: 13px;
  height: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  cursor: pointer;
  flex-shrink: 0;
  position: relative;
  transition: background 0.1s, border-color 0.1s;
}

input[type="checkbox"]:hover {
  border-color: var(--accent);
}

input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  left: 3.5px;
  top: 0.5px;
  width: 4px;
  height: 8px;
  border: solid #fff;
  border-width: 0 1.5px 1.5px 0;
  transform: rotate(45deg);
}

input[type="checkbox"]:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

@media (max-width: 900px) {
  #content {
    grid-template-areas:
      "query"
      "input"
      "output";
    grid-template-columns: 1fr;
    grid-template-rows: min(var(--query-h, 82px), 40%) minmax(0, 1fr) minmax(0, 1fr);
  }

  .resize-handle {
    display: none;
  }

  #input,
  #output {
    border-top: 1px solid var(--border);
  }

  .topbar-note {
    display: none;
  }

  .toolbar-btn span {
    display: none;
  }
}
