/* ============================================
   SnapRank JSON Tools — Design System
   Dark-mode-first, developer-focused
   ============================================ */

/* --- CSS Custom Properties (Theme) --- */
:root,
[data-theme="dark"] {
  --bg-primary: #09090b;
  --bg-secondary: #111113;
  --bg-surface: #18181b;
  --bg-surface-hover: #1f1f23;
  --bg-input: #0f0f12;
  --border: #27272a;
  --border-subtle: #1e1e22;
  --text-primary: #fafafa;
  --text-secondary: #a1a1aa;
  --text-muted: #71717a;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-subtle: rgba(59, 130, 246, 0.1);
  --success: #22c55e;
  --success-subtle: rgba(34, 197, 94, 0.1);
  --error: #ef4444;
  --error-subtle: rgba(239, 68, 68, 0.08);
  --warning: #f59e0b;
  --json-key: #93c5fd;
  --json-string: #86efac;
  --json-number: #fde68a;
  --json-boolean: #c4b5fd;
  --json-null: #fca5a5;
  --json-bracket: #71717a;
  --scrollbar-track: #18181b;
  --scrollbar-thumb: #3f3f46;
  --shadow: 0 1px 3px rgba(0,0,0,0.4);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.5);
  --gutter-bg: #111113;
  --gutter-text: #52525b;
  --selection: rgba(59, 130, 246, 0.3);
}

[data-theme="light"] {
  --bg-primary: #ffffff;
  --bg-secondary: #f9fafb;
  --bg-surface: #f4f4f5;
  --bg-surface-hover: #e9e9ec;
  --bg-input: #ffffff;
  --border: #d4d4d8;
  --border-subtle: #e4e4e7;
  --text-primary: #09090b;
  --text-secondary: #52525b;
  --text-muted: #71717a;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-subtle: rgba(37, 99, 235, 0.08);
  --success: #16a34a;
  --success-subtle: rgba(22, 163, 74, 0.08);
  --error: #dc2626;
  --error-subtle: rgba(220, 38, 38, 0.06);
  --warning: #d97706;
  --json-key: #1d4ed8;
  --json-string: #15803d;
  --json-number: #b45309;
  --json-boolean: #7c3aed;
  --json-null: #dc2626;
  --json-bracket: #71717a;
  --scrollbar-track: #f4f4f5;
  --scrollbar-thumb: #d4d4d8;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --gutter-bg: #f4f4f5;
  --gutter-text: #a1a1aa;
  --selection: rgba(37, 99, 235, 0.2);
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background 0.2s ease, color 0.2s ease;
}

::selection {
  background: var(--selection);
}

/* Scrollbar styling */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* --- Typography --- */
.mono {
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
}

h1, h2, h3, h4 {
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-primary);
}

a {
  color: var(--accent);
  text-decoration: none;
  transition: color 0.15s;
}
a:hover { color: var(--accent-hover); }

/* --- Navigation --- */
.nav {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0 1rem;
  backdrop-filter: blur(8px);
}

.nav-inner {
  max-width: 1440px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  gap: 0.5rem;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text-primary);
  text-decoration: none;
  white-space: nowrap;
  flex-shrink: 0;
}

.nav-brand svg {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.nav-links::-webkit-scrollbar { display: none; }

.nav-link {
  padding: 0.375rem 0.75rem;
  border-radius: 6px;
  font-size: 0.825rem;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  white-space: nowrap;
  transition: all 0.15s;
}
.nav-link:hover {
  color: var(--text-primary);
  background: var(--bg-surface-hover);
}
.nav-link.active {
  color: var(--accent);
  background: var(--accent-subtle);
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

/* Theme Toggle */
.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.15s;
}
.theme-toggle:hover {
  color: var(--text-primary);
  border-color: var(--text-muted);
  background: var(--bg-surface-hover);
}
.theme-toggle svg { width: 18px; height: 18px; }
[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
[data-theme="dark"] .theme-toggle .icon-moon { display: block; }
[data-theme="light"] .theme-toggle .icon-sun { display: block; }
[data-theme="light"] .theme-toggle .icon-moon { display: none; }

/* Mobile nav toggle */
.nav-mobile-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0.25rem;
}
.nav-mobile-toggle svg { width: 22px; height: 22px; }

@media (max-width: 768px) {
  .nav-links {
    display: none;
    position: absolute;
    top: 52px;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-direction: column;
    padding: 0.5rem;
    gap: 0.125rem;
  }
  .nav-links.open { display: flex; }
  .nav-link { padding: 0.5rem 0.75rem; }
  .nav-mobile-toggle { display: block; }
}

/* --- Tool Container --- */
.tool-container {
  flex: 1;
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* --- Tool Header --- */
.tool-header {
  text-align: center;
  padding: 0.75rem 0 0.25rem;
}
.tool-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}
.tool-header p {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* --- Controls Bar --- */
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.625rem 0.875rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  flex-wrap: wrap;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.control-label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-right: 0.25rem;
}

/* Segmented Control (indent, view mode) */
.segmented {
  display: flex;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 7px;
  overflow: hidden;
}

.seg-btn {
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
  font-weight: 500;
  font-family: inherit;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.seg-btn:hover { color: var(--text-primary); background: var(--bg-surface-hover); }
.seg-btn.active {
  background: var(--accent);
  color: #fff;
}

/* Icon Buttons */
.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.7rem;
  font-size: 0.8rem;
  font-weight: 500;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg-surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-icon:hover {
  color: var(--text-primary);
  border-color: var(--text-muted);
  background: var(--bg-surface-hover);
}
.btn-icon.success {
  color: var(--success);
  border-color: var(--success);
  background: var(--success-subtle);
}
.btn-icon svg { width: 14px; height: 14px; flex-shrink: 0; }

/* Primary Button */
.btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.85rem;
  font-size: 0.8rem;
  font-weight: 600;
  font-family: inherit;
  border: none;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: all 0.15s;
}
.btn-primary:hover { background: var(--accent-hover); }

/* --- Editor Panels --- */
.editor-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  flex: 1;
  min-height: 0;
}

@media (max-width: 768px) {
  .editor-layout {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
}

.editor-panel {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg-surface);
  min-height: 350px;
}

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

.panel-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.panel-actions {
  display: flex;
  gap: 0.35rem;
}

.panel-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.2rem;
  border-radius: 4px;
  display: flex;
  align-items: center;
  transition: all 0.15s;
}
.panel-btn:hover { color: var(--text-primary); background: var(--bg-surface-hover); }
.panel-btn svg { width: 15px; height: 15px; }

/* Input area */
.editor-input-wrap {
  flex: 1;
  display: flex;
  position: relative;
  overflow: hidden;
}

.editor-input {
  flex: 1;
  background: var(--bg-input);
  color: var(--text-primary);
  border: none;
  outline: none;
  resize: none;
  padding: 0.875rem;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.825rem;
  line-height: 1.65;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}
.editor-input::placeholder {
  color: var(--text-muted);
  opacity: 0.7;
}

/* Output area */
.editor-output-wrap {
  flex: 1;
  overflow: auto;
  background: var(--bg-input);
  position: relative;
}

.editor-output {
  padding: 0.875rem;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.825rem;
  line-height: 1.65;
  white-space: pre;
  min-height: 100%;
  overflow-x: auto;
}

.editor-output .line {
  display: block;
}
.editor-output .line-number {
  display: inline-block;
  width: 3.5ch;
  text-align: right;
  margin-right: 1.25ch;
  color: var(--gutter-text);
  user-select: none;
  -webkit-user-select: none;
}
.editor-output .line.error-line {
  background: var(--error-subtle);
  margin: 0 -0.875rem;
  padding: 0 0.875rem;
}

/* Placeholder state */
.output-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
  gap: 0.75rem;
}
.output-placeholder svg { width: 40px; height: 40px; opacity: 0.4; }
.output-placeholder p { font-size: 0.85rem; }

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

/* --- Error Message --- */
.error-bar {
  display: none;
  align-items: flex-start;
  gap: 0.625rem;
  padding: 0.75rem 1rem;
  background: var(--error-subtle);
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: 10px;
  font-size: 0.825rem;
  line-height: 1.5;
  animation: slideDown 0.2s ease;
}
.error-bar.visible { display: flex; }
.error-bar-icon {
  flex-shrink: 0;
  color: var(--error);
  margin-top: 1px;
}
.error-bar-icon svg { width: 18px; height: 18px; }
.error-bar-content { flex: 1; }
.error-bar-title {
  font-weight: 600;
  color: var(--error);
  margin-bottom: 0.125rem;
}
.error-bar-detail {
  color: var(--text-secondary);
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- Tree View --- */
.tree-container {
  display: none;
  padding: 0.875rem;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.825rem;
  line-height: 1.65;
  overflow: auto;
  flex: 1;
  background: var(--bg-input);
}
.tree-container.active { display: block; }

.tree-node {
  padding-left: 1.25rem;
}
.tree-node.root {
  padding-left: 0;
}

.tree-header {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 1px 4px;
  border-radius: 3px;
  user-select: none;
  -webkit-user-select: none;
}
.tree-header:hover { background: var(--bg-surface-hover); }

.tree-toggle {
  display: inline-flex;
  width: 1rem;
  font-size: 0.65rem;
  color: var(--text-muted);
  flex-shrink: 0;
  justify-content: center;
}

.tree-key { color: var(--json-key); }
.tree-string { color: var(--json-string); }
.tree-number { color: var(--json-number); }
.tree-boolean { color: var(--json-boolean); }
.tree-null { color: var(--json-null); font-style: italic; }
.tree-bracket { color: var(--json-bracket); }
.tree-count {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-style: italic;
}

.tree-children {
  overflow: hidden;
}

.tree-leaf {
  padding: 1px 4px 1px calc(1rem + 4px);
}
.tree-comma { color: var(--text-muted); }

/* --- Format Button (mobile fallback + primary CTA) --- */
.format-btn-container {
  display: none;
}

@media (max-width: 768px) {
  .format-btn-container {
    display: flex;
    justify-content: center;
    padding: 0.25rem 0;
  }
}

.format-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: all 0.15s;
}
.format-btn:hover { background: var(--accent-hover); transform: translateY(-1px); }
.format-btn svg { width: 16px; height: 16px; }

/* --- Stats Bar --- */
.stats-bar {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
  padding: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.stats-bar.visible { display: flex; }
.stat-item { display: flex; align-items: center; gap: 0.35rem; }
.stat-item span { color: var(--text-secondary); font-weight: 500; }

@media (max-width: 768px) {
  .stats-bar { gap: 0.75rem; flex-wrap: wrap; }
}

/* --- SEO Content Section --- */
.seo-content {
  max-width: 800px;
  margin: 3rem auto 0;
  padding: 0 1rem 2rem;
}

.seo-content h2 {
  font-size: 1.35rem;
  margin: 2.5rem 0 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}
.seo-content h2:first-child { margin-top: 0; }

.seo-content h3 {
  font-size: 1.05rem;
  margin: 1.5rem 0 0.5rem;
  color: var(--text-secondary);
}

.seo-content p {
  color: var(--text-secondary);
  margin-bottom: 0.75rem;
  font-size: 0.925rem;
}

.seo-content ul, .seo-content ol {
  color: var(--text-secondary);
  padding-left: 1.5rem;
  margin-bottom: 0.75rem;
}
.seo-content li {
  margin-bottom: 0.35rem;
  font-size: 0.925rem;
}

.seo-content code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.825rem;
  background: var(--bg-surface);
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  color: var(--accent);
}

.seo-content pre {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  overflow-x: auto;
  margin-bottom: 1rem;
}
.seo-content pre code {
  background: none;
  padding: 0;
  font-size: 0.8rem;
  line-height: 1.6;
}

/* FAQ */
.faq-item {
  border-bottom: 1px solid var(--border);
  padding: 1rem 0;
}
.faq-item:last-child { border-bottom: none; }
.faq-q {
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 0.35rem;
  font-size: 0.95rem;
}
.faq-a {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* --- Footer --- */
.site-footer {
  border-top: 1px solid var(--border);
  padding: 1.5rem 1rem;
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.footer-links {
  display: flex;
  justify-content: center;
  gap: 1.25rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

.footer-links a {
  color: var(--text-muted);
  transition: color 0.15s;
}
.footer-links a:hover { color: var(--text-secondary); }

/* --- Legal Pages --- */
.legal-content {
  max-width: 720px;
  margin: 0 auto;
  padding: 2rem 1rem 3rem;
}
.legal-content h1 {
  font-size: 1.75rem;
  margin-bottom: 0.5rem;
}
.legal-content .legal-updated {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 2rem;
}
.legal-content h2 {
  font-size: 1.15rem;
  margin: 2rem 0 0.5rem;
}
.legal-content p, .legal-content ul, .legal-content ol {
  color: var(--text-secondary);
  margin-bottom: 0.75rem;
  font-size: 0.925rem;
  line-height: 1.7;
}
.legal-content ul, .legal-content ol { padding-left: 1.5rem; }
.legal-content li { margin-bottom: 0.25rem; }
.legal-content a { color: var(--accent); }

/* --- Coming Soon Page --- */
.coming-soon {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem 1rem;
  text-align: center;
  gap: 1rem;
}
.coming-soon svg { width: 56px; height: 56px; color: var(--text-muted); opacity: 0.5; }
.coming-soon h1 { font-size: 1.5rem; }
.coming-soon p { color: var(--text-secondary); max-width: 400px; font-size: 0.925rem; }
.coming-soon .back-link {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  transition: all 0.15s;
  margin-top: 0.5rem;
}
.coming-soon .back-link:hover {
  color: var(--text-primary);
  border-color: var(--text-muted);
  background: var(--bg-surface-hover);
}

/* --- Responsive Adjustments --- */
@media (max-width: 640px) {
  .tool-header h1 { font-size: 1.25rem; }
  .controls { padding: 0.5rem; gap: 0.5rem; }
  .control-label { display: none; }
  .editor-panel { min-height: 250px; }
}

/* --- Utilities --- */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* Print styles */
@media print {
  .nav, .controls, .panel-actions, .format-btn-container, .site-footer { display: none !important; }
  .editor-layout { display: block; }
  .editor-panel:first-child { display: none; }
  .editor-output { white-space: pre-wrap; }
}
