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

/* Light theme (default) */
:root {
  --bg: #fafafa;
  --surface: #fff;
  --border: #ddd;
  --text: #1a1a1a;
  --text-secondary: #555;
  --accent: #0066cc;
  --accent-hover: #0052a3;
  --error-bg: #fef2f2;
  --error-text: #b91c1c;
  --radius: 6px;
  --json-key: #881280;
  --json-string: #1a6b1a;
  --json-number: #0066cc;
  --json-bool: #c5630a;
  --json-null: #999;
  --tree-line: #ddd;
}

/* Dark theme values */
[data-theme="dark"] {
  --bg: #111;
  --surface: #1a1a1a;
  --border: #333;
  --text: #e5e5e5;
  --text-secondary: #999;
  --accent: #4da3ff;
  --accent-hover: #7dbdff;
  --error-bg: #2d1515;
  --error-text: #f87171;
  --json-key: #c792ea;
  --json-string: #c3e88d;
  --json-number: #82aaff;
  --json-bool: #ffcb6b;
  --json-null: #666;
  --tree-line: #444;
}

/* System preference when no explicit choice */
@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    --bg: #111;
    --surface: #1a1a1a;
    --border: #333;
    --text: #e5e5e5;
    --text-secondary: #999;
    --accent: #4da3ff;
    --accent-hover: #7dbdff;
    --error-bg: #2d1515;
    --error-text: #f87171;
    --json-key: #c792ea;
    --json-string: #c3e88d;
    --json-number: #82aaff;
    --json-bool: #ffcb6b;
    --json-null: #666;
    --tree-line: #444;
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

main {
  max-width: 700px;
  margin: 0 auto;
  padding: 3rem 1rem;
}

/* Top bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1 {
  font-size: 1.8rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

#theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1.1rem;
  width: 2.2rem;
  height: 2.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
}

#theme-toggle:hover {
  color: var(--text);
  border-color: var(--text-secondary);
}

.subtitle {
  color: var(--text-secondary);
  margin-bottom: 2rem;
  font-size: 0.95rem;
}

/* Explainer */
.explainer {
  margin-bottom: 1.5rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
  line-height: 1.6;
}

.explainer p + p {
  margin-top: 0.5rem;
}

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.indent-group {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

#indent-select {
  padding: 0.3rem 0.4rem;
  font-size: 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.toolbar-actions {
  display: flex;
  gap: 0.4rem;
}

.btn-primary,
.btn-secondary {
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  border-color: var(--text-secondary);
}

/* Input area */
.input-area {
  margin-bottom: 1rem;
}

#json-input {
  width: 100%;
  min-height: 180px;
  padding: 0.8rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  resize: vertical;
  outline: none;
  tab-size: 2;
}

#json-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.2);
}

#json-input.drag-over {
  border-color: var(--accent);
  background: var(--bg);
}

/* Error */
#error {
  margin-bottom: 1rem;
  padding: 0.8rem 1rem;
  background: var(--error-bg);
  color: var(--error-text);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Output section */
#output-section {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.8rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.view-tabs {
  display: flex;
  gap: 0;
}

.tab {
  padding: 0.35rem 0.8rem;
  font-size: 0.8rem;
  font-weight: 600;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
}

.tab:first-child {
  border-radius: var(--radius) 0 0 var(--radius);
}

.tab:last-child {
  border-radius: 0 var(--radius) var(--radius) 0;
  border-left: none;
}

.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.stats {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.output-panel {
  max-height: 500px;
  overflow: auto;
}

#formatted-output {
  padding: 1rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.8rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
  margin: 0;
}

/* Syntax highlighting */
.json-key { color: var(--json-key); }
.json-string { color: var(--json-string); }
.json-number { color: var(--json-number); }
.json-bool { color: var(--json-bool); }
.json-null { color: var(--json-null); }

/* Tree view */
#tree-container {
  padding: 0.8rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.8rem;
  line-height: 1.8;
}

.tree-node {
  margin-left: 1.2rem;
  border-left: 1px solid var(--tree-line);
  padding-left: 0.6rem;
}

.tree-root {
  margin-left: 0;
  border-left: none;
  padding-left: 0;
}

.tree-toggle {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.7rem;
  width: 1rem;
  display: inline-block;
  text-align: center;
  padding: 0;
  font-family: inherit;
  vertical-align: middle;
}

.tree-toggle:hover {
  color: var(--text);
}

.tree-key {
  color: var(--json-key);
  font-weight: 600;
}

.tree-colon {
  color: var(--text-secondary);
}

.tree-value-string { color: var(--json-string); }
.tree-value-number { color: var(--json-number); }
.tree-value-bool { color: var(--json-bool); }
.tree-value-null { color: var(--json-null); }

.tree-bracket {
  color: var(--text-secondary);
}

.tree-count {
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-style: italic;
}

.tree-children.collapsed {
  display: none;
}

/* Footer */
footer {
  max-width: 700px;
  margin: 2rem auto 0;
  padding: 1rem 1rem;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}

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

footer a:hover {
  text-decoration: underline;
}

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

@media (max-width: 500px) {
  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar-actions {
    justify-content: stretch;
  }

  .toolbar-actions button {
    flex: 1;
  }

  .output-header {
    flex-direction: column;
    gap: 0.4rem;
    align-items: flex-start;
  }
}
