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

:root {
  --bg:            #f3f4f6;
  --surface:       #ffffff;
  --surface-2:     #f0f1f5;
  --surface-3:     #e8eaf0;
  --border:        #e2e4ec;
  --border-strong: #c8cad8;
  --text:          #16192b;
  --text-dim:      #484d6d;
  --text-muted:    #7b809e;
  --accent:        #4f5ef0;
  --accent-h:      #3d4cde;
  --accent-light:  rgba(79,94,240,.1);
  --html:          #c94a0a;
  --json:          #15803d;
  --csv:           #1d4ed8;
  --error:         #dc2626;
  --r:             8px;
  --r-sm:          5px;
  --mono:          'Fira Code','Cascadia Code','Consolas',monospace;
  --sh-sm:         0 1px 3px rgba(0,0,0,.07),0 1px 2px rgba(0,0,0,.05);
  --sh:            0 4px 16px rgba(0,0,0,.09),0 1px 4px rgba(0,0,0,.05);
  --sh-lg:         0 16px 48px rgba(0,0,0,.13),0 4px 16px rgba(0,0,0,.07);
  --toolbar-h:     50px;
  --divider-w:     5px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}

/* ── App shell ── */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ── Toolbar ── */
.toolbar {
  height: var(--toolbar-h);
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--sh-sm);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 14px;
  gap: 12px;
  z-index: 10;
}

.toolbar-left  { display: flex; align-items: center; gap: 6px; }
.toolbar-center { display: flex; justify-content: center; }
.toolbar-right { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border-radius: var(--r-sm);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .12s, color .12s, border-color .12s;
  white-space: nowrap;
  user-select: none;
  line-height: 1;
  text-decoration: none;
}
.btn-secondary {
  background: var(--surface);
  color: var(--text-dim);
  border-color: var(--border);
  box-shadow: var(--sh-sm);
}
.btn-secondary:hover { background: var(--surface-2); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:disabled { opacity: .45; cursor: not-allowed; }

.btn-xs {
  padding: 4px 9px;
  font-size: 11.5px;
  background: var(--surface-2);
  color: var(--text-dim);
  border-color: var(--border);
}
.btn-xs:hover { background: var(--surface-3); color: var(--text); }

/* ── Type switcher ── */
.type-switcher {
  display: flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 3px;
  gap: 2px;
}
.type-btn {
  padding: 5px 20px;
  font-size: 12.5px;
  font-weight: 600;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background: transparent;
  color: var(--text-muted);
  transition: background .12s, color .12s, box-shadow .12s;
  letter-spacing: .02em;
}
.type-btn:hover:not(.active) { color: var(--text); background: var(--surface-3); }
.type-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--sh-sm);
}
.type-btn[data-type="html"].active { color: var(--html); }
.type-btn[data-type="json"].active { color: var(--json); }
.type-btn[data-type="csv"].active  { color: var(--csv);  }

/* ── Format controls ── */
.format-controls { display: flex; gap: 6px; }
.format-controls.hidden { visibility: hidden; pointer-events: none; }

/* ── Workspace (split) ── */
.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

/* ── Panes ── */
.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}
.pane-editor  { width: 50%; }
.pane-preview { flex: 1; }

/* ── Right sections (Preview + Output stacked) ── */
.right-section {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
/* Preview takes all remaining space when Output is hidden */
#section-preview { flex: 1; }
/* When Output is visible, Preview shrinks to share space */
#section-preview.has-output { flex: 1; min-height: 120px; }
/* Output starts at a fixed height; user can resize */
#section-output {
  height: 260px;
  flex-shrink: 0;
  min-height: 80px;
}

/* ── Horizontal divider (between Preview and Output) ── */
.h-divider {
  height: 5px;
  flex-shrink: 0;
  background: var(--border);
  cursor: row-resize;
  position: relative;
  transition: background .15s;
  z-index: 5;
}
.h-divider:hover, .h-divider.dragging { background: var(--accent); }
.h-divider::after {
  content: '';
  position: absolute;
  inset: -4px 0;   /* wider grab area */
}

/* ── Output content ── */
.output-content {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: #fafbfc;
  position: relative;
  border-top: none;
}

.output-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 12.5px;
  pointer-events: none;
  user-select: none;
}

/* Output plain text (for formatted/minified code) */
.output-text {
  margin: 0;
  padding: 16px 18px;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.7;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-all;
  overflow-wrap: break-word;
}

/* ── Output header ── */
.output-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}
.output-header-right {
  display: flex;
  align-items: center;
  gap: 6px;
}
.output-badge {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .04em;
  padding: 2px 8px;
  border-radius: 100px;
  text-transform: uppercase;
  background: var(--accent-light);
  color: var(--accent);
}
.pane-header {
  height: 38px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
.pane-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.editor-meta {
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--mono);
}
.preview-toolbar-extras {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ── Editor wrap (gutter + textarea) ── */
.editor-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;           /* single scroll context */
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: relative;
}

/* ── Line gutter ── */
.line-gutter {
  flex-shrink: 0;
  width: 52px;
  padding: 16px 0;
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  overflow-y: auto;           /* needs scrollTop to be settable */
  overflow-x: hidden;
  pointer-events: none;
  user-select: none;
}
/* hide gutter's own scrollbar — it's driven by the textarea */
.line-gutter::-webkit-scrollbar { display: none; }
.line-gutter { scrollbar-width: none; }

.line-gutter span {
  display: block;
  height: calc(13px * 1.7);  /* font-size × line-height — must match .editor */
  line-height: calc(13px * 1.7);
  text-align: right;
  padding-right: 12px;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--text-muted);
}
.line-gutter span.active-line {
  color: var(--text-dim);
  font-weight: 600;
}

/* ── Editor textarea ── */
.editor {
  flex: 1;
  min-width: 0;
  min-height: 0;
  resize: none;
  border: none;
  outline: none;
  background: var(--surface);
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.7;
  padding: 16px 10px 16px 12px;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
  /* scroll-padding keeps caret visible */
  scroll-padding: 16px;
}
.editor::placeholder { color: var(--text-muted); font-family: inherit; }

/* ── Divider ── */
.divider {
  width: var(--divider-w);
  flex-shrink: 0;
  background: var(--border);
  cursor: col-resize;
  position: relative;
  transition: background .15s;
  z-index: 5;
}
.divider:hover, .divider.dragging { background: var(--accent); }
.divider::after {
  content: '';
  position: absolute;
  inset: 0 -4px;          /* wider grab area */
}

/* ── Preview content ── */
.preview-content {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: var(--bg);
  position: relative;
}

/* ── Empty state ── */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  pointer-events: none;
  user-select: none;
}
.empty-state p { font-size: 13px; }

/* ── HTML iframe ── */
.html-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: none;
  background: #fff;
}

/* ── JSON tree ── */
.json-preview {
  padding: 16px 20px;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.75;
}
.json-error {
  padding: 16px 20px;
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--error);
}
.json-node { display: block; }
.json-toggle {
  cursor: pointer;
  user-select: none;
  display: inline-flex;
  align-items: center;
}
.json-toggle::before {
  content: '▾';
  font-size: 9px;
  width: 14px;
  color: var(--text-muted);
  transition: transform .1s;
  flex-shrink: 0;
}
.json-toggle.collapsed::before { transform: rotate(-90deg); }
.json-children {
  padding-left: 20px;
  border-left: 2px solid var(--border);
  margin-left: 5px;
}
.json-children.hidden { display: none; }
.json-key     { color: #6d28d9; }
.json-string  { color: #15803d; }
.json-number  { color: #b45309; }
.json-boolean { color: #1d4ed8; }
.json-null    { color: var(--text-muted); font-style: italic; }
.json-bracket { color: var(--text-muted); }
.json-count   { font-size: 10.5px; color: var(--text-muted); margin-left: 4px; }
.json-comma   { color: var(--text-muted); }

/* ── CSV table ── */
.csv-wrapper { height: 100%; overflow: auto; }
.csv-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}
.csv-table thead { position: sticky; top: 0; z-index: 2; }
.csv-table th {
  background: var(--surface);
  padding: 8px 14px;
  text-align: left;
  font-weight: 600;
  font-size: 11.5px;
  letter-spacing: .03em;
  color: var(--text-dim);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  user-select: none;
  cursor: pointer;
  transition: color .1s;
}
.csv-table th:hover { color: var(--text); }
.csv-table th .si { display: inline-block; margin-left: 3px; font-size: 9px; }
.csv-table th.sort-asc  .si::after { content: '▲'; color: var(--accent); }
.csv-table th.sort-desc .si::after { content: '▼'; color: var(--accent); }
.csv-table td {
  padding: 7px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.csv-table tr:hover td { background: var(--surface-2); color: var(--text); }
.csv-table tr.hidden-row { display: none; }
.csv-rn {
  color: var(--text-muted) !important;
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  font-family: var(--mono);
  user-select: none;
  width: 44px;
  min-width: 44px;
}

/* ── CSV search ── */
.search-label {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 3px 9px;
  color: var(--text-muted);
  transition: border-color .1s;
}
.search-label:focus-within { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-light); }
.search-label input {
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 12px;
  width: 130px;
}
.search-label input::placeholder { color: var(--text-muted); }
.row-count { font-size: 11.5px; color: var(--text-muted); white-space: nowrap; }

/* ── JSON / CSV extras ── */
.json-extras, .csv-extras {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--text);
  color: var(--surface);
  border-radius: var(--r);
  padding: 9px 16px;
  font-size: 12.5px;
  box-shadow: var(--sh);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity .16s, transform .16s;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Type badge (used in download filename helper) ── */
.type-badge {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .06em;
  padding: 2px 8px;
  border-radius: 100px;
  text-transform: uppercase;
}
.type-html { background: rgba(201,74,10,.1);  color: var(--html); }
.type-json { background: rgba(21,128,61,.1);  color: var(--json); }
.type-csv  { background: rgba(29,78,216,.1);  color: var(--csv);  }

/* ── Utility ── */
.hidden { display: none !important; }

/* ── Scrollbars ── */
::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
