:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #d9dde3;
  --text: #1b1f24;
  --muted: #6b7480;
  --accent: #2f6feb;
  --accent-text: #ffffff;
  --error: #c0362c;
  --error-bg: #fdf0ef;
  --warn: #96650a;
  --warn-bg: #fdf6e7;
  --ok: #1f7a46;
  --ok-bg: #eef8f2;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1c2024;
    --surface-2: #23282d;
    --border: #333a41;
    --text: #e6e9ec;
    --muted: #9aa4ae;
    --accent: #4c8dff;
    --accent-text: #0d1116;
    --error: #ff8a80;
    --error-bg: #2b1e1e;
    --warn: #f0c265;
    --warn-bg: #2a2418;
    --ok: #6ddc9b;
    --ok-bg: #16261e;
  }
}

* {
  box-sizing: border-box;
}

/* Class rules below set an explicit `display`, which would otherwise beat the
   user-agent's `[hidden] { display: none }` and leave hidden elements on screen. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ── top bar ────────────────────────────────────────────────────────────────── */

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.01em;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── buttons ────────────────────────────────────────────────────────────────── */

button {
  font: inherit;
  font-size: 13px;
  padding: 7px 13px;
  border-radius: 7px;
  border: 1px solid transparent;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}

button:hover:not(:disabled) {
  filter: brightness(1.08);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

button.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

button.ghost:hover:not(:disabled) {
  background: var(--surface-2);
  filter: none;
}

button.small {
  padding: 4px 9px;
  font-size: 12px;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── layout ─────────────────────────────────────────────────────────────────── */

main {
  display: grid;
  grid-template-columns: minmax(320px, 30%) 1fr;
  gap: 16px;
  padding: 16px 20px 24px;
  align-items: start;
}

@media (max-width: 900px) {
  main {
    grid-template-columns: 1fr;
  }
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

/* ── dropzone ───────────────────────────────────────────────────────────────── */

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 26px 16px;
  background: var(--surface);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  text-align: center;
  transition: border-color 0.12s, background 0.12s;
}

.dropzone:hover,
.dropzone.is-over {
  border-color: var(--accent);
  background: var(--surface-2);
}

.dz-icon {
  width: 26px;
  height: 26px;
  color: var(--muted);
}

.dz-main {
  margin: 4px 0 0;
  font-weight: 550;
}

.dz-main code {
  font-family: var(--mono);
  font-size: 0.9em;
}

.dz-sub {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.filebar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.filename {
  font-family: var(--mono);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.filemeta,
.hint {
  color: var(--muted);
  font-size: 12px;
}

.filebar .filemeta {
  margin-left: auto;
  white-space: nowrap;
}

/* ── load from a link ───────────────────────────────────────────────────────── */

.from-url {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.from-url input {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  font-size: 12.5px;
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.from-url input:focus {
  outline: none;
  border-color: var(--accent);
}

.from-url button {
  flex: 0 0 auto;
}

/* ── staging default ────────────────────────────────────────────────────────── */

.staging {
  display: flex;
  align-items: center;
  /* Wraps rather than pushing the strict toggle outside the panel when the
     column is narrow. */
  flex-wrap: wrap;
  gap: 6px 8px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}

.staging > label:first-of-type {
  color: var(--muted);
  white-space: nowrap;
}

.staging select {
  /* Grows into spare space but yields it before the toggle is squeezed out. */
  flex: 1 1 9ch;
  min-width: 0;
  font: inherit;
  font-size: 12.5px;
  padding: 4px 7px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.staging .toggle {
  flex: 0 0 auto;
  white-space: nowrap;
}

/* ── status ─────────────────────────────────────────────────────────────────── */

.status {
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 550;
}

.status-idle {
  background: var(--surface);
  color: var(--muted);
  font-weight: 400;
}

.status-ok {
  background: var(--ok-bg);
  border-color: color-mix(in srgb, var(--ok) 35%, transparent);
  color: var(--ok);
}

.status-warn {
  background: var(--warn-bg);
  border-color: color-mix(in srgb, var(--warn) 35%, transparent);
  color: var(--warn);
}

.status-error {
  background: var(--error-bg);
  border-color: color-mix(in srgb, var(--error) 35%, transparent);
  color: var(--error);
}

/* ── diagnostics ────────────────────────────────────────────────────────────── */

.diagnostics {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.diag-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.diag-head h2 {
  margin: 0;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.toggle {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
}

.diag-list {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 42vh;
  overflow-y: auto;
}

.diag {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  font-size: 13px;
}

.diag:first-child {
  border-top: none;
}

.diag-line {
  font-family: var(--mono);
  font-size: 12px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
  align-self: start;
}

.diag-error .diag-line {
  background: var(--error-bg);
  color: var(--error);
}

.diag-warning .diag-line {
  background: var(--warn-bg);
  color: var(--warn);
}

.diag-msg code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--surface-2);
  padding: 1px 4px;
  border-radius: 4px;
}

/* ── presets ────────────────────────────────────────────────────────────────── */

.presets {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}

.presets summary {
  cursor: pointer;
  font-size: 13px;
  font-weight: 550;
  display: flex;
  gap: 8px;
  align-items: baseline;
}

.presets textarea {
  width: 100%;
  margin-top: 8px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.45;
  padding: 8px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  resize: vertical;
}

.presets-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

/* ── output ─────────────────────────────────────────────────────────────────── */

.panel-output {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  gap: 0;
}

.tabs {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.tabs-spacer {
  flex: 1;
}

.tab {
  background: transparent;
  color: var(--muted);
  border-color: transparent;
}

.tab:hover {
  background: var(--surface-2);
  filter: none;
}

.tab.is-active {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.output-body {
  min-height: 0;
}

.out-text {
  display: block;
  width: 100%;
  height: min(72vh, 900px);
  margin: 0;
  padding: 14px 16px;
  border: 0;
  resize: vertical;
  background: var(--surface);
  color: var(--text);
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.7;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}

.out-empty {
  color: var(--muted);
}

/* ── placeholders: unset fields, shown red and editable in place ─────────────── */

.ph {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  color: var(--error);
  background: var(--error-bg);
  border: 1px solid color-mix(in srgb, var(--error) 50%, transparent);
  border-radius: 5px;
  padding: 0 3px;
  margin: 0 1px;
  cursor: pointer;
  appearance: none;
  vertical-align: baseline;
  line-height: 1.35;
}

.ph:hover {
  border-color: var(--error);
  filter: brightness(1.06);
}

.ph.is-target {
  outline: 2px solid var(--error);
  outline-offset: 1px;
}

/* Filled in this session: still highlighted, but no longer a problem. */
.ph.is-filled {
  color: var(--ok);
  background: var(--ok-bg);
  border-color: color-mix(in srgb, var(--ok) 50%, transparent);
  font-weight: 500;
}

.todo-chip {
  font-size: 12px;
  font-weight: 600;
  color: var(--error);
  background: var(--error-bg);
  border: 1px solid color-mix(in srgb, var(--error) 40%, transparent);
  border-radius: 999px;
  padding: 2px 9px;
  white-space: nowrap;
}

.todo-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--error-bg);
  color: var(--error);
  font-size: 12.5px;
}

.todo-bar.is-done {
  background: var(--ok-bg);
  color: var(--ok);
}

.todo-bar button {
  color: var(--text);
}

.out-rows {
  height: min(72vh, 900px);
  overflow: auto;
}

.out-rows table {
  border-collapse: collapse;
  width: 100%;
  font-size: 12.5px;
}

.out-rows th,
.out-rows td {
  text-align: left;
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  font-family: var(--mono);
}

.out-rows th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 550;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.out-rows td.text {
  white-space: normal;
  min-width: 22ch;
  font-family: inherit;
}

.out-rows td.num {
  color: var(--muted);
}

.out-rows tr.is-key td.type {
  color: var(--accent);
}

/* ── data editor ─────────────────────────────────────────────────────────────── */

.out-data {
  height: min(72vh, 900px);
  overflow: auto;
  padding: 14px 16px 40px;
}

.data-intro {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--muted);
  max-width: 70ch;
}

.data-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.data-problem {
  padding: 8px 12px;
  border-radius: 7px;
  background: var(--error-bg);
  border: 1px solid color-mix(in srgb, var(--error) 40%, transparent);
  color: var(--error);
  font-size: 13px;
  margin-bottom: 14px;
}

.data-section {
  margin-bottom: 22px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.data-section > summary {
  cursor: pointer;
  padding: 9px 12px;
  background: var(--surface-2);
  font-size: 13px;
  font-weight: 600;
  display: flex;
  gap: 8px;
  align-items: baseline;
}

.data-section > summary .count {
  color: var(--muted);
  font-weight: 400;
  font-size: 12px;
}

.data-body {
  padding: 10px 12px 12px;
}

.data-note {
  margin: 0 0 10px;
  font-size: 12.5px;
  color: var(--muted);
  max-width: 78ch;
}

table.tbl {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
}

table.tbl th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 550;
  padding: 2px 6px 6px;
}

table.tbl td {
  padding: 2px 4px;
  vertical-align: top;
}

table.tbl input,
table.tbl textarea {
  width: 100%;
  font: inherit;
  font-family: var(--mono);
  font-size: 12.5px;
  padding: 4px 7px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

table.tbl textarea {
  resize: vertical;
  min-height: 30px;
}

table.tbl input:focus,
table.tbl textarea:focus {
  outline: none;
  border-color: var(--accent);
}

table.tbl td.narrow {
  width: 9ch;
}

td.rowdel {
  width: 1%;
}

button.del {
  background: transparent;
  color: var(--muted);
  border-color: transparent;
  padding: 4px 8px;
  line-height: 1;
}

button.del:hover {
  background: var(--error-bg);
  color: var(--error);
  filter: none;
}

.readonly-list {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.readonly-list code {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 5px;
}

/* ── docs ───────────────────────────────────────────────────────────────────── */

.docs {
  margin: 0 20px;
  padding: 16px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.docs h2 {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 20px 0 8px;
}

.docs h2:first-child {
  margin-top: 0;
}

.docs table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
  margin-bottom: 4px;
}

.docs th,
.docs td {
  text-align: left;
  vertical-align: top;
  padding: 5px 10px 5px 0;
  border-bottom: 1px solid var(--border);
}

.docs th {
  color: var(--muted);
  font-weight: 550;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.docs code,
.docs .chips code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
}

.docs .chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 6px;
  align-items: baseline;
}

.docs .chip-label {
  font-size: 12px;
  color: var(--muted);
  margin-right: 10px;
}

.docs p {
  color: var(--muted);
  font-size: 13px;
  margin: 4px 0 0;
}
