/* =====================================================================
   JSON Viewer — styles.css
   Design tokens + dark/light themes, split layout, tree, search, toasts.
   ===================================================================== */

:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "SF Mono", "JetBrains Mono", "Fira Code", "Cascadia Code", ui-monospace, Menlo, Consolas, monospace;

  --radius-sm: 6px;
  --radius:    9px;
  --radius-lg: 14px;

  --sp-1: 4px;  --sp-2: 8px;  --sp-3: 12px; --sp-4: 16px; --sp-5: 24px;

  --ease: cubic-bezier(.4, 0, .2, 1);
  --shadow-1: 0 1px 2px rgba(0,0,0,.18);
  --shadow-2: 0 6px 24px rgba(0,0,0,.28);
  --shadow-3: 0 18px 50px rgba(0,0,0,.40);

  /* User-adjustable font size for editor + JSON views (set via JS). */
  --code-font-size: 13px;
}

/* ----------------------------- Dark theme ----------------------------- */
[data-theme="dark"] {
  --bg:        #0e1116;
  --bg-pane:   #161b22;
  --bg-elev:   #1c2230;
  --bg-elev-2: #232b3a;
  --bg-hover:  #232b3a;
  --border:    #2a3240;
  --border-soft:#222936;

  --text:      #e6edf3;
  --text-dim:  #9aa7b6;
  --text-faint:#677589;

  --accent:    #4c8dff;
  --accent-soft: rgba(76,141,255,.16);
  --accent-text: #8fb8ff;

  --ok:   #2ec27e;  --ok-soft: rgba(46,194,126,.16);
  --warn: #e3b341;
  --err:  #f5604d;  --err-soft: rgba(245,96,77,.14);

  /* value type colors */
  --t-string:  #7ee0a2;
  --t-number:  #ff9d6b;
  --t-boolean: #c792ea;
  --t-null:    #8a96a6;
  --t-key:     #79b8ff;
  --t-punct:   #677589;

  --mark-bg: #b6862c;
  --mark-current-bg: #f5a623;

  color-scheme: dark;
}

/* ----------------------------- Light theme ---------------------------- */
[data-theme="light"] {
  --bg:        #f4f6fa;
  --bg-pane:   #ffffff;
  --bg-elev:   #f7f9fc;
  --bg-elev-2: #eef2f8;
  --bg-hover:  #eef2f8;
  --border:    #dde3ec;
  --border-soft:#e7ebf2;

  --text:      #1c2430;
  --text-dim:  #56627a;
  --text-faint:#8a94a6;

  --accent:    #2563eb;
  --accent-soft: rgba(37,99,235,.10);
  --accent-text: #1d4ed8;

  --ok:   #15924f;  --ok-soft: rgba(21,146,79,.12);
  --warn: #b8860b;
  --err:  #d92d20;  --err-soft: rgba(217,45,32,.10);

  --t-string:  #0a7d4a;
  --t-number:  #b5530b;
  --t-boolean: #8b2bb8;
  --t-null:    #8a94a6;
  --t-key:     #1d4ed8;
  --t-punct:   #98a2b3;

  --mark-bg: #ffe487;
  --mark-current-bg: #ffb020;

  color-scheme: light;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win over author `display` rules
   (e.g. .drawer/.summary/.state-panel set display:flex). */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

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

/* Scrollbars */
.viewport, .editor, .view, .history-list, .drawer {
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.viewport::-webkit-scrollbar, .editor::-webkit-scrollbar,
.view::-webkit-scrollbar { width: 11px; height: 11px; }
.viewport::-webkit-scrollbar-thumb, .editor::-webkit-scrollbar-thumb,
.view::-webkit-scrollbar-thumb {
  background: var(--border);
  border: 3px solid transparent;
  border-radius: 8px;
  background-clip: content-box;
}
.viewport::-webkit-scrollbar-thumb:hover { background: var(--text-faint); background-clip: content-box; }

/* ============================== Header ============================== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-4);
  padding: 0 var(--sp-4);
  height: 54px;
  background: var(--bg-pane);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 20;
}
.brand { display: flex; align-items: baseline; gap: var(--sp-2); }
.brand-mark { color: var(--accent); align-self: center; }
.brand h1 { font-size: 16px; font-weight: 650; margin: 0; letter-spacing: -.01em; }
.brand-sub {
  font-size: 11px; color: var(--text-faint); text-transform: uppercase;
  letter-spacing: .08em; font-weight: 600;
}
.header-actions { display: flex; gap: var(--sp-2); }
.credit {
  flex: 1; text-align: center; min-width: 0;
  font-size: 12.5px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.credit strong { color: var(--text); font-weight: 650; }

/* The right theme icons toggle by data-theme */
[data-theme="dark"]  .icon-sun  { display: none; }
[data-theme="light"] .icon-moon { display: none; }

/* ============================== Buttons ============================== */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  font-family: inherit; font-size: 13px; font-weight: 550;
  color: var(--text);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  cursor: pointer;
  transition: background .14s var(--ease), border-color .14s var(--ease), transform .06s var(--ease), color .14s var(--ease);
  white-space: nowrap;
  user-select: none;
}
.btn:hover { background: var(--bg-hover); border-color: var(--text-faint); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn-sm { padding: 5px 9px; font-size: 12px; }
.btn-icon { padding: 7px; }
.btn-sm.btn-icon { padding: 5px; }
.btn-icon-text { }

.btn-primary {
  background: var(--accent); border-color: var(--accent); color: #fff;
}
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); border-color: var(--accent); }

.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--bg-hover); border-color: var(--border); }

/* file upload uses a <label> styled as button */
label.btn { position: relative; }

/* ============================== Split layout ============================== */
.split {
  flex: 1;
  display: grid;
  grid-template-columns: var(--left-w, 44%) 6px 1fr;
  min-height: 0;
  overflow: hidden;
}
.pane {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
  background: var(--bg-pane);
}
.pane-input { border-right: 1px solid var(--border); }

.divider {
  background: var(--border-soft);
  cursor: col-resize;
  position: relative;
  transition: background .15s var(--ease);
}
.divider::after {
  content: ""; position: absolute; inset: 0 -4px;
}
.divider:hover, .divider:focus-visible { background: var(--accent); outline: none; }

/* ============================== Toolbars ============================== */
.toolbar {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  background: var(--bg-pane);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
  flex-shrink: 0;
  min-height: 48px;
}
.toolbar-spacer { flex: 1; }
.toolbar-output { gap: var(--sp-2); flex-wrap: wrap; }
.tree-controls { display: flex; gap: var(--sp-2); }

/* Font-size stepper */
.font-control {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 2px;
}
.font-control .btn { border: none; background: transparent; }
.font-control .btn:hover { background: var(--bg-hover); }
.font-size-label {
  min-width: 24px; text-align: center;
  font-size: 12px; font-weight: 600; color: var(--text-dim);
  font-variant-numeric: tabular-nums; user-select: none;
}

/* Tabs */
.tabs {
  display: inline-flex; gap: 2px; padding: 3px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.tab {
  font-family: inherit; font-size: 12.5px; font-weight: 600;
  color: var(--text-dim); background: transparent; border: none;
  padding: 5px 13px; border-radius: 4px; cursor: pointer;
  transition: background .14s var(--ease), color .14s var(--ease);
}
.tab:hover { color: var(--text); }
.tab.is-active { background: var(--bg-pane); color: var(--text); box-shadow: var(--shadow-1); }
.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ============================== Editor ============================== */
.editor-wrap { position: relative; flex: 1; min-height: 0; display: flex; overflow: hidden; }

/* Line-number gutter — scroll-synced with the textarea via JS transform. */
.gutter {
  flex-shrink: 0;
  box-sizing: content-box;
  width: 40px;
  padding: var(--sp-4) 8px var(--sp-4) 0;
  overflow: hidden;
  background: var(--bg-pane);
  border-right: 1px solid var(--border-soft);
  user-select: none;
}
.gutter-inner {
  font-family: var(--font-mono); font-size: var(--code-font-size); line-height: 1.65;
  color: var(--text-faint);
  text-align: right;
  white-space: pre;
  will-change: transform;
}

.editor {
  flex: 1; width: 100%;
  resize: none; border: none; outline: none;
  background: var(--bg-pane); color: var(--text);
  font-family: var(--font-mono); font-size: var(--code-font-size); line-height: 1.65;
  padding: var(--sp-4) var(--sp-4) var(--sp-4) 12px;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}
.editor::placeholder { color: var(--text-faint); }

/* Highlight strip marking the line of a parse error. */
.error-line {
  position: absolute; left: 0; right: 0; top: 0;
  background: var(--err-soft);
  border-left: 2px solid var(--err);
  pointer-events: none;
  z-index: 2;
}

.drop-overlay {
  position: absolute; inset: 8px;
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  background: var(--accent-soft);
  backdrop-filter: blur(2px);
  display: none; align-items: center; justify-content: center;
  pointer-events: none; z-index: 6;
}
.editor-wrap.drag-over .drop-overlay { display: flex; }
.drop-overlay-inner { text-align: center; color: var(--accent-text); }
.drop-overlay-inner p { margin: var(--sp-2) 0 0; font-size: 15px; }

/* Status bar */
.status-bar {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-top: 1px solid var(--border);
  font-size: 12px; flex-shrink: 0;
}
.status-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-weight: 600; font-size: 11.5px;
  padding: 3px 9px; border-radius: 20px;
}
.status-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-idle  { color: var(--text-dim);  background: var(--bg-elev); }
.status-valid { color: var(--ok);  background: var(--ok-soft); }
.status-error { color: var(--err); background: var(--err-soft); }
.status-meta { color: var(--text-dim); font-family: var(--font-mono); font-size: 11.5px; }

/* ============================== Search ============================== */
.search-bar {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-icon { color: var(--text-faint); flex-shrink: 0; }
.search-input {
  flex: 1; min-width: 0;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text);
  font-family: inherit; font-size: 13px;
  padding: 6px 10px; outline: none;
  transition: border-color .14s var(--ease), box-shadow .14s var(--ease);
}
.search-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-input::-webkit-search-cancel-button { -webkit-appearance: none; }
.search-count {
  font-size: 12px; color: var(--text-dim); font-variant-numeric: tabular-nums;
  white-space: nowrap; min-width: 0;
}
.search-nav { display: flex; gap: 2px; }

/* ============================== Summary ============================== */
.summary {
  display: flex; gap: var(--sp-2); flex-wrap: wrap;
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.summary-item {
  font-size: 11.5px; color: var(--text-dim); font-family: var(--font-mono);
  background: var(--bg-elev); border: 1px solid var(--border-soft);
  border-radius: 20px; padding: 2px 10px;
}

/* ============================== Viewport / views ============================== */
.viewport { position: relative; flex: 1; min-height: 0; overflow: auto; }
.view { min-height: 100%; }

.view-tree { padding: var(--sp-3) var(--sp-2) 40px; }
.view-code {
  margin: 0; padding: var(--sp-4) var(--sp-4) 40px;
  font-family: var(--font-mono); font-size: var(--code-font-size); line-height: 1.6;
  white-space: pre; tab-size: 2; color: var(--text);
  overflow-wrap: normal;
}
.view-code .tok-key { color: var(--t-key); }
.view-code .tok-string { color: var(--t-string); }
.view-code .tok-number { color: var(--t-number); }
.view-code .tok-boolean { color: var(--t-boolean); }
.view-code .tok-null { color: var(--t-null); }
.view-code .tok-punct { color: var(--t-punct); }

/* ============================== Tree ============================== */
.node { position: relative; }
.node-row {
  display: flex; align-items: flex-start; gap: 4px;
  padding: 1px 6px 1px 0;
  border-radius: 5px;
  cursor: default;
  scroll-margin: 60px;
}
.node-row:hover { background: var(--bg-hover); }
.node-row.is-current { background: var(--accent-soft); box-shadow: inset 2px 0 0 var(--accent); }

/* indentation guides */
.node-children {
  margin-left: 9px;
  padding-left: 11px;
  border-left: 1px solid var(--border);
}
.node-children.guide-hover { border-left-color: var(--text-faint); }

/* toggle caret */
.toggle {
  flex-shrink: 0; width: 16px; height: 20px;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: none; padding: 0; margin: 0;
  color: var(--text-faint); cursor: pointer; border-radius: 4px;
}
.toggle:hover { color: var(--text); background: var(--bg-elev-2); }
/* Base caret points right (→) = closed; expanded rotates to point down (↓). */
.toggle svg { transition: transform .15s var(--ease); transform: rotate(90deg); }
.node.collapsed > .node-row .toggle svg { transform: rotate(0deg); }
.node.collapsed > .node-children { display: none; }
.toggle.leaf { visibility: hidden; cursor: default; }
.toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.node-key {
  color: var(--t-key); font-family: var(--font-mono); font-size: var(--code-font-size);
  font-weight: 550; white-space: pre;
}
.node-index { color: var(--text-faint); font-family: var(--font-mono); font-size: var(--code-font-size); }
.node-colon { color: var(--t-punct); font-family: var(--font-mono); font-size: var(--code-font-size); margin-right: 4px; }
.node-val { font-family: var(--font-mono); font-size: var(--code-font-size); white-space: pre-wrap; word-break: break-word; }

.val-string  { color: var(--t-string); }
.val-number  { color: var(--t-number); }
.val-boolean { color: var(--t-boolean); }
.val-null    { color: var(--t-null); font-style: italic; }

.node-preview {
  color: var(--text-faint); font-family: var(--font-mono); font-size: 12.5px;
  font-style: italic;
}
.node-bracket { color: var(--t-punct); font-family: var(--font-mono); }

/* count badge */
.count-badge {
  display: inline-flex; align-items: center;
  font-family: var(--font-sans); font-size: 10.5px; font-weight: 650;
  color: var(--accent-text); background: var(--accent-soft);
  border-radius: 20px; padding: 1px 8px; margin-left: 6px;
  letter-spacing: .01em; cursor: help; white-space: nowrap;
}
.count-badge.obj-badge { color: var(--text-dim); background: var(--bg-elev); }
.count-badge .matched { color: var(--ok); }

/* node action button (appears on hover) */
.node-actions {
  margin-left: auto; display: flex; gap: 2px;
  opacity: 0; transition: opacity .12s var(--ease);
}
.node-row:hover .node-actions { opacity: 1; }
.node-act {
  background: none; border: none; padding: 2px 5px; border-radius: 4px;
  color: var(--text-faint); cursor: pointer; font-size: 11px;
  display: inline-flex; align-items: center;
}
.node-act:hover { color: var(--text); background: var(--bg-elev-2); }
.node-act:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* search highlight */
mark.hl { background: var(--mark-bg); color: inherit; border-radius: 2px; padding: 0 1px; }
mark.hl.current { background: var(--mark-current-bg); color: #1a1205; }

/* load more */
.load-more {
  margin: 3px 0 3px 12px;
  font-size: 12px; color: var(--accent-text);
  background: var(--accent-soft); border: 1px solid transparent;
  border-radius: var(--radius-sm); padding: 4px 12px; cursor: pointer;
  font-weight: 600;
}
.load-more:hover { filter: brightness(1.08); border-color: var(--accent); }

/* ============================== State panels ============================== */
.state-panel {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: var(--sp-3); padding: var(--sp-5);
  color: var(--text-dim); background: var(--bg-pane);
}
.state-panel svg { color: var(--text-faint); }
.state-panel h2 { margin: 0; font-size: 17px; color: var(--text); font-weight: 600; }
.state-panel p { margin: 0; max-width: 320px; font-size: 13px; }
.state-error svg { color: var(--err); }
.state-error h2 { color: var(--err); }
.error-message {
  font-family: var(--font-mono); font-size: 12.5px;
  background: var(--err-soft); color: var(--err);
  border-radius: var(--radius-sm); padding: var(--sp-3) var(--sp-4);
  max-width: 460px; white-space: pre-wrap; text-align: left;
}

/* ============================== History drawer ============================== */
.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  z-index: 40; animation: fade .18s var(--ease);
}
.drawer {
  position: fixed; top: 0; right: 0; height: 100%;
  width: min(380px, 92vw);
  background: var(--bg-pane); border-left: 1px solid var(--border);
  box-shadow: var(--shadow-3);
  z-index: 41; display: flex; flex-direction: column;
  animation: slide-in .22s var(--ease);
}
@keyframes slide-in { from { transform: translateX(100%); } to { transform: translateX(0); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border);
}
.drawer-head h2 { margin: 0; font-size: 15px; }
.drawer-head-actions { display: flex; gap: var(--sp-2); align-items: center; }

.history-list { list-style: none; margin: 0; padding: var(--sp-2); overflow-y: auto; flex: 1; }
.history-item {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3); border-radius: var(--radius-sm);
  cursor: pointer; border: 1px solid transparent;
}
.history-item:hover { background: var(--bg-hover); border-color: var(--border); }
.history-main { flex: 1; min-width: 0; }
.history-title {
  font-family: var(--font-mono); font-size: 12.5px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.history-meta { font-size: 11px; color: var(--text-faint); margin-top: 2px; }
.history-del {
  background: none; border: none; color: var(--text-faint); cursor: pointer;
  padding: 4px; border-radius: 4px; flex-shrink: 0;
}
.history-del:hover { color: var(--err); background: var(--err-soft); }
.history-empty { padding: var(--sp-5); text-align: center; color: var(--text-faint); font-size: 13px; }

/* ============================== Toasts ============================== */
.toast-stack {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: var(--sp-2);
  z-index: 60; align-items: center; pointer-events: none;
}
.toast {
  display: flex; align-items: center; gap: var(--sp-2);
  background: var(--bg-elev-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 9px 16px; font-size: 13px; font-weight: 550;
  box-shadow: var(--shadow-2);
  animation: toast-in .22s var(--ease);
  pointer-events: auto;
}
.toast.leaving { animation: toast-out .2s var(--ease) forwards; }
.toast .toast-icon { display: inline-flex; }
.toast.ok .toast-icon { color: var(--ok); }
.toast.err .toast-icon { color: var(--err); }
@keyframes toast-in  { from { opacity: 0; transform: translateY(10px) scale(.96); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px) scale(.96); } }

/* ============================== Context menu ============================== */
.ctx-menu {
  position: fixed; z-index: 55;
  background: var(--bg-elev-2); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-2);
  padding: 4px; min-width: 168px;
  animation: fade .12s var(--ease);
}
.ctx-item {
  display: block; width: 100%; text-align: left;
  background: none; border: none; color: var(--text);
  font-family: inherit; font-size: 13px; padding: 7px 10px;
  border-radius: var(--radius-sm); cursor: pointer;
}
.ctx-item:hover, .ctx-item:focus-visible { background: var(--accent-soft); color: var(--accent-text); outline: none; }

/* ============================== Responsive ============================== */
@media (max-width: 820px) {
  body { overflow: auto; }
  .split {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(220px, 40vh) 0 1fr;
  }
  .divider { display: none; }
  .pane-input { border-right: none; border-bottom: 1px solid var(--border); }
  .pane { min-height: 0; }
  .viewport { min-height: 320px; }
  .brand-sub { display: none; }
  .toolbar { flex-wrap: wrap; min-height: auto; }
  .tree-controls .btn span { display: none; }
}

@media (max-width: 500px) {
  .header-actions #historyBtn span { display: none; }
  .summary { overflow-x: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
