/* ================================================================
   JSON Toolkit - Clean Edition
   Simple, flat, editor-first design system
   ================================================================ */

/* ── Tokens ───────────────────────────────────────────────────── */
body {
  color-scheme: dark;

  /* Surfaces */
  --bg:  #0d1117;
  --s1:  #0f1720;
  --s2:  #161b22;
  --s3:  #21262d;

  /* Borders */
  --b1:  #30363d;
  --b2:  #484f58;

  /* Text */
  --t1:  #e6edf3;
  --t2:  #9da7b3;
  --t3:  #6e7681;

  /* Accent */
  --ac:    #58a6ff;
  --ac-fi: rgba(88, 166, 255, 0.12);
  --ac-fg: #061526;

  /* Semantic */
  --danger: #ff7b72;
  --warn:   #d29922;
  --ok:     #3fb950;

  /* Editor inset */
  --ed-bg: #010409;

  /* Syntax */
  --tk-k: #79c0ff;
  --tk-s: #a5d6ff;
  --tk-n: #ffa657;
  --tk-b: #d2a8ff;
  --tk-z: #ff7b72;

  /* Diff bands */
  --da:   rgba(63,  185, 80, 0.16);  --da-b: #3fb950;
  --dr:   rgba(248, 81, 73, 0.16);   --dr-b: #f85149;
  --dc:   rgba(210, 153, 34, 0.18);  --dc-b: #d29922;

  /* Fonts */
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Display",
          "SF Pro Text", "Helvetica Neue", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular,
          Menlo, Monaco, Consolas, monospace;

  /* Radii */
  --r1:  6px;
  --r2:  8px;
  --r3: 12px;
  --r4: 16px;

  /* Shadows */
  --sh: 0 1px 3px rgba(0, 0, 0, 0.30);

  /* Motion */
  --ease: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  --eout: cubic-bezier(0.16, 1.00, 0.30, 1);
  --tf:  100ms;
  --tm:  170ms;
}

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

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  font-family: var(--sans);
  color: var(--t1);
  background: var(--bg);
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button, select, textarea { font: inherit; }

:focus-visible {
  outline: 2px solid var(--ac);
  outline-offset: 2px;
  border-radius: var(--r1);
}

/* ── Layout wrappers ──────────────────────────────────────────── */
.app-shell {
  width: 100%;
  margin: 0;
}

/* ── Segmented control ────────────────────────────────────────── */
.segmented {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  background: var(--s2);
  border: 1px solid var(--b1);
  border-radius: var(--r2);
}

.segment {
  min-width: 72px;
  height: 28px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: calc(var(--r2) - 2px);
  background: transparent;
  color: var(--t2);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition:
    background  var(--tf) var(--ease),
    color       var(--tf) var(--ease);
}

.segment:hover:not(.is-active) {
  color: var(--t1);
  background: var(--s3);
}

.segment.is-active {
  background: var(--s1);
  border-color: var(--b1);
  color: var(--t1);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

/* ── View toggle — compact segmented inside block-head ───────── */
.view-toggle .segment {
  min-width: 62px;
  height: 26px;
  padding: 0 10px;
  font-size: 0.79rem;
}

.field { display: grid; gap: 3px; }

.field > span {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--t3);
}

/* ── Buttons ──────────────────────────────────────────────────── */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 30px;
  padding: 0 10px;
  background: var(--s1);
  border: 1px solid var(--b1);
  border-radius: var(--r2);
  color: var(--t1);
  font-size: 0.81rem;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition:
    background    var(--tf) var(--ease),
    border-color  var(--tf) var(--ease),
    color         var(--tf) var(--ease);
}

.button:hover {
  background: var(--s2);
  border-color: var(--b2);
}

.button:active {
  background: var(--s3);
  transition-duration: 50ms;
}

.button.primary {
  background: var(--ac);
  border-color: var(--ac);
  color: var(--ac-fg);
}

.button.primary:hover {
  background: color-mix(in srgb, var(--ac) 88%, transparent);
  border-color: color-mix(in srgb, var(--ac) 88%, transparent);
}

.button.primary:active {
  background: color-mix(in srgb, var(--ac) 76%, transparent);
  border-color: color-mix(in srgb, var(--ac) 76%, transparent);
}


/* ── App shell ────────────────────────────────────────────────── */
.app-shell {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: stretch;
  padding: 0;
}

/* ── Panel ────────────────────────────────────────────────────── */
.panel {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--s1);
  border: 1px solid var(--b1);
  overflow: hidden;
  box-shadow: var(--sh);
}

.is-hidden { display: none !important; }

/* ── Panel enter — clean opacity fade ────────────────────────── */
.panel.is-entering {
  animation: panelIn var(--tm) var(--eout) both;
}

@keyframes panelIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ── Split & compare layouts ──────────────────────────────────── */
.split {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

.compare-grid {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  border-bottom: 1px solid var(--b1);
}

.split > .editor-block + .editor-block,
.compare-grid > .editor-block + .editor-block {
  border-left: 1px solid var(--b1);
}

/* ── Editor blocks ────────────────────────────────────────────── */
.editor-block {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 44px;
  padding: 0 10px 0 14px;
  background: var(--s2);
  border-bottom: 2px solid var(--b1);
  flex-shrink: 0;
}

.block-title {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.block-head h2 {
  font-size: 0.80rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  text-transform: uppercase;
  color: var(--t2);
}

.mode-switch {
  flex-shrink: 0;
}

.mode-switch .segment {
  min-width: 68px;
}

.button-row {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

/* ── Textarea & outputs ───────────────────────────────────────── */
textarea,
.code-output,
.diff-output,
.tree-output {
  flex: 1;
  min-height: 0;
  width: 100%;
  font-family: var(--mono);
  font-size: 0.9375rem;
  line-height: 1.65;
  tab-size: 2;
  color: var(--t1);
  border-radius: 0;
}

textarea {
  display: block;
  background: var(--ed-bg);
  border: none;
  border-bottom: 1px solid var(--b1);
  outline: none;
  padding: 16px 18px;
  resize: vertical;
  transition:
    background   var(--tm) var(--ease),
    box-shadow   var(--tm) var(--ease);
}

textarea:focus {
  background: var(--ed-bg);
  box-shadow: none;
}

textarea:focus-visible,
.code-output:focus-visible,
.diff-output:focus-visible,
.tree-output:focus-visible {
  outline: none;
  border-radius: 0;
}

textarea::placeholder { color: var(--t3); }

.code-output,
.diff-output,
.tree-output {
  background: transparent;
  border: none;
  outline: none;
  overflow: auto;
  padding: 16px 18px;
  white-space: pre-wrap;
  word-break: break-word;
}

.tree-output { white-space: normal; }

/* ── Status bar ───────────────────────────────────────────────── */
.status {
  flex-shrink: 0;
  min-height: 30px;
  display: flex;
  align-items: center;
  padding: 4px 14px;
  font-size: 0.79rem;
  font-weight: 400;
  color: var(--t2);
  background: var(--s2);
  border-top: 1px solid var(--b1);
  line-height: 1.4;
}

.status.is-error { color: var(--danger); }

/* ── Syntax tokens ────────────────────────────────────────────── */
.token-key     { color: var(--tk-k); font-weight: 600; }
.token-string  { color: var(--tk-s); }
.token-number  { color: var(--tk-n); }
.token-boolean { color: var(--tk-b); font-weight: 600; }
.token-null    { color: var(--tk-z); font-style: italic; }

/* ── Diff section — integrated into compare panel ─────────────── */
.diff-section {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--ed-bg);
}

.legend {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.79rem;
  color: var(--t2);
  flex-wrap: wrap;
}

.dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}

.dot.added   { background: var(--da-b); }
.dot.removed { background: var(--dr-b); }
.dot.changed { background: var(--dc-b); }

.diff-line {
  display: block;
  padding: 2px 18px;
  margin: 0 -18px;
  border-left: 2px solid transparent;
  border-radius: var(--r1);
}

.diff-line.added   { background: var(--da); border-color: var(--da-b); }
.diff-line.removed { background: var(--dr); border-color: var(--dr-b); }
.diff-line.changed { background: var(--dc); border-color: var(--dc-b); }

/* ── Tree viewer ──────────────────────────────────────────────── */
.tree { min-width: min(900px, max-content); }

.tree-node {
  --depth: 0;
  display: grid;
  gap: 1px;
}

.tree-row {
  min-height: 28px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px 2px calc(8px + var(--depth) * 16px);
  border-left: 2px solid transparent;
  border-radius: var(--r1);
  transition:
    background   var(--tf) var(--ease),
    border-color var(--tf) var(--ease);
}

.tree-row:hover,
.tree-row:focus-within {
  background: var(--ac-fi);
  border-color: var(--b2);
}

.tree-toggle,
.tree-action {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  display: inline-grid;
  place-items: center;
  padding: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--r1);
  color: var(--t3);
  cursor: pointer;
  font-family: var(--mono);
  font-size: 0.68rem;
  font-weight: 800;
  transition:
    background   var(--tf) var(--ease),
    border-color var(--tf) var(--ease),
    color        var(--tf) var(--ease);
}

.tree-toggle:hover,
.tree-action:hover {
  background: var(--s2);
  border-color: var(--b1);
  color: var(--t1);
}

.tree-toggle.placeholder { visibility: hidden; pointer-events: none; }

.tree-key   { color: var(--tk-k); font-weight: 600; font-size: 0.875rem; }
.tree-type  { color: var(--t3); font-size: 0.75rem; }
.tree-count { color: var(--t3); font-size: 0.75rem; }

.tree-path {
  color: var(--t3);
  font-size: 0.72rem;
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 1px 6px;
  background: var(--s2);
  border: 1px solid var(--b1);
  border-radius: 999px;
}

.tree-spacer   { flex: 1 1 auto; }
.tree-children { display: grid; }

.tree-node.is-collapsed > .tree-children { display: none; }

.tree-node.is-collapsed > .tree-row .tree-toggle {
  transform: rotate(-90deg);
  transition: transform var(--tm) var(--eout);
}

/* ── Icon button ──────────────────────────────────────────────── */
.btn-icon {
  width: 30px;
  padding: 0;
  flex-shrink: 0;
  display: inline-grid;
  place-items: center;
}

/* ── Fullscreen ───────────────────────────────────────────────── */
.fullscreen-backdrop {
  position: fixed;
  inset: 0;
  z-index: 199;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--tm) var(--ease);
}

.fullscreen-backdrop.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.is-fullscreen {
  position: fixed !important;
  inset: 0 !important;
  z-index: 200 !important;
  border-radius: 0 !important;
  border: none !important;
  box-shadow: none !important;
  flex: none !important;
  margin: 0 !important;
  overflow: hidden !important;
}

.editor-block.is-fullscreen { background: var(--s1); }
.diff-section.is-fullscreen { background: var(--ed-bg); }


/* ── 404 page ─────────────────────────────────────────────────── */
.not-found {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 0;
  background: var(--bg);
}

.not-found-panel {
  width: min(440px, 100%);
  display: grid;
  gap: 16px;
  padding: 32px;
  background: var(--s1);
  border: 1px solid var(--b1);
  box-shadow: var(--sh);
}

.not-found-kicker {
  width: max-content;
  padding: 3px 8px;
  background: var(--ed-bg);
  border: 1px solid var(--b1);
  color: var(--ac);
  font-family: var(--mono);
  font-size: 0.76rem;
}

.not-found-panel h1 {
  font-size: 3.25rem;
  font-weight: 700;
  color: var(--t1);
  line-height: 0.9;
}

.not-found-panel p { color: var(--t2); font-size: 0.925rem; line-height: 1.5; }

.link-button {
  display: inline-flex;
  width: max-content;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  height: 36px;
  padding: 0 16px;
  border-radius: var(--r2);
  font-weight: 500;
  font-size: 0.88rem;
}

/* ── Reduced motion ───────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ── Responsive ───────────────────────────────────────────────── */
@media (max-width: 880px) {
  .app-shell {
    flex: none;
    min-height: auto;
  }

  .split,
  .compare-grid,
  .diff-section {
    flex: none;
    min-height: auto;
  }

  textarea,
  .code-output,
  .diff-output,
  .tree-output {
    flex: none;
    min-height: 260px;
  }

  .split,
  .compare-grid { grid-template-columns: 1fr; }

  .split > .editor-block + .editor-block,
  .compare-grid > .editor-block + .editor-block {
    border-left: none;
    border-top: 1px solid var(--b1);
  }

  .compare-grid { border-bottom: none; }

  .diff-section {
    border-top: 1px solid var(--b1);
  }
}

@media (max-width: 560px) {
  .block-head { flex-direction: column; align-items: stretch; padding: 8px 10px; }

  .block-title,
  .mode-switch {
    width: 100%;
  }

  .mode-switch .segment,
  .button-row .button {
    flex: 1;
  }

  .tree-row {
    flex-wrap: wrap;
    align-items: flex-start;
    padding-left: calc(8px + var(--depth) * 12px);
  }

  .tree-path { max-width: 100%; }

  .legend { flex-wrap: wrap; gap: 8px; }
}
