:root {
  /* Dark theme — Catppuccin Macchiato */
  --bg: #24273a; /* Base */
  --bg-secondary: #1e2030; /* Mantle */
  --bg-tertiary: #2b2f47; /* between Base and Surface 0 */
  --border: #494d64; /* Surface 1 */
  --text: #b8c0e0; /* Subtext 1 (softer than Text) */
  --text-muted: #8087a2; /* Overlay 1 (softer muted) */
  --accent: #8aadf4; /* Blue */
  --success: #a6da95; /* Green */
  --error: #ed8796; /* Red */
  --font-mono: "JetBrains Mono", "Fira Code", "Courier New", monospace;
  --font-sans: "Source Sans Pro", "Microsoft Yahei", sans-serif;
  --font-heading: "Lato", "Microsoft Yahei", sans-serif;
  --radius: 6px;
}

@media (prefers-color-scheme: light) {
  :root {
    /* Light theme — Catppuccin Latte */
    --bg: #eff1f5; /* Base */
    --bg-secondary: #e6e9ef; /* Mantle */
    --bg-tertiary: #e6e9ef; /* Mantle (closer to Base) */
    --border: #bcc0cc; /* Surface 1 */
    --text: #5c5f77; /* Subtext 1 (softer than Text) */
    --text-muted: #8c8fa1; /* Overlay 1 (softer muted) */
    --accent: #1e66f5; /* Blue */
    --success: #40a02b; /* Green */
    --error: #d20f39; /* Red */
  }
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

*::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
*::-webkit-scrollbar-track {
  background: transparent;
}
*::-webkit-scrollbar-thumb {
  background: var(--border);
  border: 2px solid var(--bg);
  border-radius: 6px;
}
*::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.5;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
}

header h1 {
  font-size: 18px;
  font-weight: 600;
  font-family: var(--font-mono);
}

header h1 span {
  color: var(--accent);
}

.layout {
  display: grid;
  grid-template-columns: 270px 1fr;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.panel {
  overflow-y: auto;
  padding: 16px 20px;
}
.panel-sidebar {
  border-right: 1px solid var(--border);
  background: var(--bg-secondary);
}

.main {
  display: grid;
  /* The table grows with its rows; the output keeps whatever is left. */
  grid-template-rows: minmax(0, auto) minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}

.split {
  min-height: 0;
  overflow: auto;
  padding: 16px 20px;
}
/* Only the grid scrolls, so + Row and friends stay put on a long table. */
.split-table {
  border-bottom: 1px solid var(--border);
  max-height: 45vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.split-table .section {
  display: flex;
  flex-direction: column;
  min-height: 0;
  margin-bottom: 0;
}
.split-output {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 0;
  overflow: hidden;
}
.split-col {
  min-height: 0;
  overflow: auto;
  padding: 16px 20px;
}
.split-col:first-child {
  border-right: 1px solid var(--border);
}

.section {
  margin-bottom: 14px;
}

.section-label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

select,
input[type="text"],
input[type="search"],
textarea {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  padding: 7px 10px;
  outline: none;
  transition: border-color 0.15s;
}

select:focus,
input:focus,
textarea:focus {
  border-color: var(--accent);
}

select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23656d76' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 28px;
}

.kv-row {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 8px;
}

.action-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.btn {
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  padding: 7px 16px;
  font-family: var(--font-sans);
  background: var(--accent);
  color: var(--bg);
  transition:
    background 0.15s,
    color 0.15s,
    border-color 0.15s;
}
.btn:hover {
  filter: brightness(1.1);
}

.btn-secondary {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
  filter: none;
}

.btn-copy {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.btn-copy:hover {
  border-color: var(--accent);
  color: var(--accent);
  filter: none;
}
.btn-copy.copied {
  color: var(--success);
  border-color: var(--success);
}

.btn-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 2px 5px;
  border-radius: var(--radius);
  flex-shrink: 0;
  transition:
    color 0.15s,
    background 0.15s;
}
.btn-remove:hover:not(:disabled) {
  color: var(--error);
  background: color-mix(in srgb, var(--error) 10%, transparent);
}
.btn-remove:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
}
.hint.no-top {
  margin-top: 0;
  margin-bottom: 8px;
}
.hint code {
  font-family: var(--font-mono);
  font-size: 11px;
}

.save-msg {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
  min-height: 1em;
}
.save-msg.error {
  color: var(--error);
}

.warn-note {
  font-size: 12px;
  color: var(--error);
  margin-top: 6px;
  min-height: 1em;
}

.hidden {
  display: none !important;
}

/* ── Saved tables ──────────────────────────────────────────────────────────── */

.saved-empty {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.saved-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}

.saved-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-right: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-tertiary);
}
.saved-item:hover,
.saved-item.selected {
  border-color: var(--accent);
}
.saved-item.selected .saved-name {
  color: var(--accent);
}

.saved-name {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 13px;
  text-align: left;
  padding: 7px 9px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#save-name {
  width: 100%;
}

/* ── Editable grid ─────────────────────────────────────────────────────────── */

.grid-wrap {
  overflow: auto;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-secondary);
  padding: 6px;
}

table.grid {
  border-collapse: separate;
  border-spacing: 3px;
}

table.grid th,
table.grid td {
  padding: 0;
}

table.grid th input {
  font-weight: 600;
  font-family: var(--font-mono);
  font-size: 13px;
}

table.grid input {
  width: 150px;
  font-size: 14px;
  padding: 5px 8px;
}

/* Column controls sit in their own header row so the header inputs stay
   flush with the body inputs below them. */
.col-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}

.col-spacer {
  width: 26px;
}

.align-btn {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 5px 7px;
  margin-right: 3px;
  transition:
    color 0.15s,
    border-color 0.15s;
}
.align-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* ── Output and preview ────────────────────────────────────────────────────── */

#output {
  width: 100%;
  min-height: 220px;
  background: var(--bg-tertiary);
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: 13px;
  padding: 10px;
  resize: vertical;
  white-space: pre;
  overflow-x: auto;
}

textarea.body-input {
  width: 100%;
  min-height: 130px;
  resize: vertical;
  font-family: var(--font-mono);
  font-size: 13px;
}

.preview {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-secondary);
  padding: 10px;
}

.preview table {
  border-collapse: collapse;
  font-size: 14px;
}

.preview th,
.preview td {
  border: 1px solid var(--border);
  padding: 5px 10px;
}

.preview th {
  background: var(--bg-tertiary);
  font-family: var(--font-heading);
  font-weight: 700;
}

@media (max-width: 900px) {
  body {
    height: auto;
    overflow: visible;
  }
  .layout,
  .split-output {
    grid-template-columns: 1fr;
    overflow: visible;
  }
  .main {
    grid-template-rows: auto auto;
    overflow: visible;
  }
  .split-table {
    max-height: none;
  }
  .panel-sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .split-col:first-child {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
}

footer {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
  color: var(--text-muted);
}

footer a {
  color: var(--accent);
  text-decoration: none;
}
footer a:hover {
  text-decoration: underline;
}
