/* webmp4 viewer — theme-aware minimal UI. */
:root {
  /* Font-size multiplier for content panes (tree, fields, hex, tables, tooltip).
     Chrome (toolbar, status bar, tabs) deliberately stays at fixed size. */
  --font-scale: 1;
  --bg:            #0f1115;
  --bg-alt:        #171a22;
  --bg-panel:      #1c2029;
  --border:        #2a303d;
  --fg:            #d8dfea;
  --fg-dim:        #8b93a7;
  --accent:        #6ea8ff;
  --accent-dim:    #3f5a8f;
  --warn:          #f0b24a;
  --error:         #ef6360;
  --info:          #64b5a1;
  --select:        #2a3b60;
  --select-fg:     #fff;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  /* per-type byte-map colour stops (8 buckets, stable hash) */
  --bm-ftyp: #89b4fa;
  --bm-moov: #a6e3a1;
  --bm-mdat: #f9e2af;
  --bm-meta: #cba6f7;
  --bm-moof: #f38ba8;
  --bm-mfra: #94e2d5;
  --bm-other: #585b70;
  --bm-free: #45475a;
}
.theme-light {
  --bg: #ffffff;
  --bg-alt: #f5f6f8;
  --bg-panel: #ffffff;
  --border: #d9dce3;
  --fg: #1b1f2a;
  --fg-dim: #5b6474;
  --accent: #2962ff;
  --accent-dim: #bcd0ff;
  --warn: #b07008;
  --error: #c7321c;
  --info: #1e7a6a;
  --select: #d7e3ff;
  --select-fg: #0b1f4a;
  --bm-ftyp: #2962ff;
  --bm-moov: #2ea043;
  --bm-mdat: #b07008;
  --bm-meta: #8b4fae;
  --bm-moof: #c73860;
  --bm-mfra: #1e7a6a;
  --bm-other: #b0b5c0;
  --bm-free: #d9dce3;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; color: var(--fg); background: var(--bg); font-family: var(--sans); font-size: 13px; }
body { display: grid; grid-template-rows: auto auto 1fr auto; }

#toolbar {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 10px; background: var(--bg-alt); border-bottom: 1px solid var(--border);
}
#toolbar .tbtitle { display: flex; align-items: baseline; gap: 6px; }
#toolbar .logo { color: var(--accent); font-size: 14px; }
#toolbar strong { font-size: 14px; letter-spacing: 0.5px; }
#toolbar .muted { color: var(--fg-dim); font-size: 12px; }
#toolbar .tbactions { display: flex; gap: 6px; align-items: center; flex: 1; margin-left: 10px; }
.tabs { display: flex; gap: 2px; overflow: auto; flex: 0 0 auto; }
.tabs .tab {
  padding: 4px 10px; border: 1px solid var(--border); border-bottom: none;
  background: var(--bg-panel); color: var(--fg-dim); cursor: pointer; border-radius: 6px 6px 0 0;
  max-width: 220px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tabs .tab.active { color: var(--fg); background: var(--bg); border-color: var(--accent-dim); }
.tabs .tab .close { margin-left: 6px; color: var(--fg-dim); }
.tabs .tab .close:hover { color: var(--error); }

#toolbar button, #toolbar select {
  background: var(--bg-panel); color: var(--fg); border: 1px solid var(--border);
  border-radius: 4px; padding: 4px 10px; cursor: pointer; font-size: 12px;
}
#toolbar button:hover, #toolbar select:hover { border-color: var(--accent); }
#toolbar .filter { flex: 1 1 200px; min-width: 140px; padding: 4px 8px; border: 1px solid var(--border); background: var(--bg-panel); color: var(--fg); border-radius: 4px; font-family: var(--mono); }
#toolbar .toggle { display: flex; align-items: center; gap: 4px; color: var(--fg-dim); font-size: 11px; padding: 4px 8px; border: 1px solid var(--border); border-radius: 4px; cursor: pointer; background: var(--bg-panel); }
#toolbar .font-size-group { display: flex; gap: 0; border: 1px solid var(--border); border-radius: 4px; overflow: hidden; }
#toolbar .font-size-group button { border: none; border-right: 1px solid var(--border); border-radius: 0; padding: 4px 8px; font-size: 11px; min-width: 34px; }
#toolbar .font-size-group button:last-child { border-right: none; }
#toolbar #btn-font-reset { min-width: 52px; }
#toolbar .toggle input { margin: 0; }
body.edit-mode #toolbar .toggle { color: var(--warn); border-color: var(--warn); }
body.edit-mode table.fields .field-input { border: 1px solid var(--border); background: var(--bg-panel); color: var(--fg); font-family: var(--mono); padding: 0 4px; border-radius: 3px; font-size: 12px; }
body.edit-mode table.fields .field-input:focus { border-color: var(--accent); outline: none; }
body.edit-mode table.fields .field-input.invalid { border-color: var(--error); }
body.edit-mode table.fields tr.edited td.value::after { content: ' •'; color: var(--warn); }
body.edit-mode table.fields tr.edited-type::after { content: none; }
.edits-badge { background: var(--warn); color: #000; padding: 1px 6px; border-radius: 10px; font-size: 10px; margin-left: 6px; }
.tree-row.edited .type::after { content: ' •'; color: var(--warn); }
#toolbar button:disabled { opacity: 0.4; cursor: not-allowed; }

.bytemap { height: 12px; background: var(--bg-alt); border-bottom: 1px solid var(--border); position: relative; overflow: hidden; cursor: crosshair; }
.bytemap .cell { position: absolute; top: 0; bottom: 0; }
.bytemap .cursor { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--accent); box-shadow: 0 0 4px var(--accent); pointer-events: none; }

#main { display: grid; grid-template-columns: minmax(280px, 32%) 1fr; overflow: hidden; }
/* When a non-tree view is active the sidebar is hidden; collapse the grid to
   a single track so #detail auto-places into a full-width cell. (Using a
   zero-width first track would make #detail land there instead.) */
body.no-sidebar #main { grid-template-columns: 1fr; }
#sidebar { border-right: 1px solid var(--border); overflow: hidden; display: flex; flex-direction: column; background: var(--bg-alt); }
.tree { overflow: auto; flex: 1; position: relative; font-family: var(--mono); }
.tree-row { display: flex; align-items: center; gap: 4px; padding: 2px 8px 2px 6px; cursor: pointer; white-space: nowrap; }
.tree-row:hover { background: var(--bg-panel); }
.tree-row.selected { background: var(--select); color: var(--select-fg); }
.tree-row .caret { color: var(--fg-dim); width: 1em; text-align: center; }
.tree-row .type { font-weight: 600; color: var(--accent); }
.theme-light .tree-row .type { color: #2962ff; }
.tree-row.selected .type { color: #9ecdff; }
.tree-row .size { color: var(--fg-dim); margin-left: auto; font-size: 11px; }
.tree-row .warn-dot { color: var(--warn); }
.tree-row .err-dot { color: var(--error); }

#detail { display: grid; grid-template-rows: auto 1fr; overflow: hidden; min-width: 0; }
.tabs-detail { display: flex; gap: 2px; border-bottom: 1px solid var(--border); padding: 0 10px; background: var(--bg-alt); }
.tabs-detail .tab { padding: 6px 12px; border: none; background: transparent; color: var(--fg-dim); cursor: pointer; border-bottom: 2px solid transparent; }
.tabs-detail .tab.active { color: var(--fg); border-bottom-color: var(--accent); }
.panel { overflow: auto; padding: 10px 14px; font-family: var(--mono); font-size: 12.5px; }

table.fields { border-collapse: collapse; width: 100%; }
table.fields th, table.fields td { padding: 3px 8px; border-bottom: 1px solid var(--border); vertical-align: top; text-align: left; }
table.fields th { color: var(--fg-dim); font-weight: normal; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
table.fields td.value { color: var(--fg); word-break: break-all; }
table.fields td.value .note { display: block; color: var(--fg-dim); font-size: 11px; margin-top: 2px; }
table.fields td.range { color: var(--fg-dim); font-size: 11px; white-space: nowrap; }
table.fields tr:hover td { background: var(--bg-alt); }
table.fields .badge { display: inline-block; padding: 1px 6px; border-radius: 3px; background: var(--accent-dim); color: var(--accent); font-size: 11px; margin-left: 6px; }
.theme-light table.fields .badge { background: var(--accent-dim); color: #0b1f4a; }

.hexview { font-family: var(--mono); white-space: pre; overflow: auto; line-height: 1.35em; }
.hexview .offset { color: var(--fg-dim); }
.hexview .byte.sel { background: var(--select); color: var(--select-fg); border-radius: 2px; }
.hexview .ascii { color: var(--info); }

.warn-row { padding: 4px 0; border-bottom: 1px solid var(--border); }
.warn-row .sev { display: inline-block; width: 60px; font-size: 11px; text-transform: uppercase; }
.warn-row .sev.error { color: var(--error); }
.warn-row .sev.warning { color: var(--warn); }
.warn-row .sev.info { color: var(--info); }
.warn-row .code { color: var(--fg-dim); margin-right: 6px; }

table.list { border-collapse: collapse; width: 100%; font-family: var(--mono); font-size: 12px; }
table.list th, table.list td { border-bottom: 1px solid var(--border); padding: 4px 8px; text-align: left; }
table.list th { position: sticky; top: 0; background: var(--bg-alt); color: var(--fg-dim); font-weight: normal; text-transform: uppercase; font-size: 11px; letter-spacing: .5px; }
table.list tr.selectable { cursor: pointer; }
table.list tr.selectable:hover td { background: var(--bg-alt); }

#statusbar { display: flex; justify-content: space-between; padding: 2px 10px; background: var(--bg-alt); border-top: 1px solid var(--border); font-family: var(--mono); color: var(--fg-dim); font-size: 11px; }

.palette { position: fixed; inset: 15% 25% auto 25%; background: var(--bg-panel); border: 1px solid var(--accent); border-radius: 8px; padding: 10px; box-shadow: 0 20px 50px rgba(0,0,0,.5); z-index: 10; }
.palette.hidden { display: none; }
.palette input { width: 100%; background: var(--bg); color: var(--fg); border: 1px solid var(--border); border-radius: 4px; padding: 8px; font-family: var(--mono); }
.palette ul { list-style: none; margin: 8px 0 0 0; padding: 0; max-height: 40vh; overflow: auto; }
.palette li { padding: 4px 8px; cursor: pointer; border-radius: 4px; font-family: var(--mono); font-size: 12px; }
.palette li.active { background: var(--select); color: var(--select-fg); }

.dropzone { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; color: white; font-size: 24px; z-index: 20; pointer-events: none; letter-spacing: 2px; }
.dropzone.hidden { display: none; }

.fragtimeline { padding: 8px; overflow: auto; }
.fragtimeline .track { margin-bottom: 10px; }
.fragtimeline .track h4 { margin: 0 0 4px 0; font-family: var(--mono); font-size: 12px; color: var(--fg-dim); }
.fragtimeline .ribbon { height: 14px; background: var(--bg-alt); position: relative; border: 1px solid var(--border); }
.fragtimeline .ribbon .seg { position: absolute; top: 0; bottom: 0; background: var(--accent); opacity: .7; }
.fragtimeline .ribbon .seg:hover { opacity: 1; }
.fragtimeline .ribbon .cursor { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--warn); pointer-events: none; }
.fragtimeline .ribbon .scrub-tip { position: absolute; top: -20px; transform: translateX(-50%); background: var(--bg-panel); border: 1px solid var(--accent); padding: 1px 5px; border-radius: 3px; font-size: 10px; color: var(--fg); pointer-events: none; white-space: nowrap; }

.graphview { padding: 10px; }
.graph-node { display: inline-block; border: 1px solid var(--border); border-radius: 6px; margin: 4px; padding: 6px 10px; font-family: var(--mono); background: var(--bg-panel); }
.graph-node.track { border-color: var(--accent-dim); }
.graph-node.item { border-color: var(--warn); }
.graph-node.group { border-color: var(--info); }
.graph-edge { font-family: var(--mono); color: var(--fg-dim); margin: 2px 4px; font-size: 11px; }

.diff-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 10px; font-family: var(--mono); font-size: 12px; }
.diff-grid h3 { margin: 0 0 6px 0; font-size: 13px; color: var(--fg-dim); }
.diff-grid .diff-line { padding: 1px 4px; }
.diff-grid .diff-line.add { background: rgba(64,180,64,0.2); }
.diff-grid .diff-line.del { background: rgba(220,80,80,0.2); }
.diff-grid .diff-line.chg { background: rgba(230,180,50,0.2); }

.spec-link { color: var(--accent); text-decoration: none; margin-left: 4px; font-size: 11px; }
.spec-link::before { content: '§'; }
.spec-link:hover { text-decoration: underline; }

.empty { color: var(--fg-dim); padding: 40px; text-align: center; font-family: var(--sans); font-size: 14px; }

/* Educational tooltip (hover on any 4CC). Content is scrollable when the
 * definition runs long — the viewport clamp keeps it from falling off-screen. */
.box-tooltip {
  position: fixed; z-index: 100;
  background: var(--bg-panel); border: 1px solid var(--accent-dim);
  border-radius: 6px; padding: 10px 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  color: var(--fg); font-size: 12px; line-height: 1.45;
  pointer-events: auto;
  min-width: 280px;
  max-width: 520px;
  max-height: 70vh;
  overflow-y: auto;
  scrollbar-width: thin;
}
.box-tooltip::-webkit-scrollbar { width: 8px; }
.box-tooltip::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.box-tooltip.hidden { display: none; }
.box-tooltip .tt-close {
  position: absolute; top: 6px; right: 6px;
  width: 22px; height: 22px; padding: 0;
  background: var(--bg-alt); color: var(--fg-dim);
  border: 1px solid var(--border); border-radius: 4px;
  font-size: 16px; line-height: 1; cursor: pointer; z-index: 2;
}
.box-tooltip .tt-close:hover { color: var(--fg); background: var(--border); }
.box-tooltip .tt-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 3px; position: sticky; top: 0; background: var(--bg-panel); padding-bottom: 3px; border-bottom: 1px solid var(--border); z-index: 1; }
.box-tooltip .tt-code { font-family: var(--mono); font-weight: 700; color: var(--accent); font-size: 13px; }
.box-tooltip .tt-name { color: var(--fg); font-size: 12px; }
.box-tooltip .tt-metaline { color: var(--fg-dim); font-size: 10.5px; margin: 6px 0 8px; letter-spacing: 0.2px; }
.box-tooltip .tt-spec { color: var(--info); }
.box-tooltip .tt-meta code { font-family: var(--mono); font-size: 11px; background: var(--bg-alt); padding: 0 4px; border-radius: 2px; }
.box-tooltip .tt-section-title { margin-top: 10px; font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--fg-dim); font-weight: 600; }
.box-tooltip .tt-def p, .box-tooltip .tt-semantics p { margin: 0 0 6px 0; }
.box-tooltip .tt-def p:last-child, .box-tooltip .tt-semantics p:last-child { margin-bottom: 0; }
.box-tooltip .tt-semantics { font-size: 11.5px; color: var(--fg); background: var(--bg-alt); border-radius: 4px; padding: 6px 8px; margin-top: 4px; }
.box-tooltip .tt-syntax { margin-top: 4px; font-family: var(--mono); font-size: 11px; background: var(--bg-alt); padding: 6px 8px; border-radius: 4px; overflow-x: auto; white-space: pre; color: var(--fg); }

/* ---- Nested-boxes view ---- */
.boxnested-view { padding: 0; display: flex; flex-direction: column; }
.bn-controls { display: flex; align-items: center; gap: 14px; padding: 6px 10px; border-bottom: 1px solid var(--border); background: var(--bg-alt); color: var(--fg-dim); font-size: 11.5px; }
.bn-controls label { display: flex; align-items: center; gap: 6px; }
.bn-controls input[type=range] { accent-color: var(--accent); }
.bn-controls .bn-depth-label { color: var(--fg); font-family: var(--mono); min-width: 60px; }
.bn-controls .bn-hint { color: var(--fg-dim); }
.bn-container { flex: 1; overflow: auto; padding: 8px; background: var(--bg); }
/* Background color + text color are set inline per-box (computed from the
   palette's luminance, so text always contrasts with its background). The
   rules below only control structural things. */
.bn-box { position: relative; border-radius: 6px; margin: 3px 0; padding: 0 0 3px 0; box-shadow: inset 0 0 0 1px rgba(0,0,0,0.18); font-family: var(--mono); }
.bn-box.depth-0 { margin: 6px 0; }
/* The header uses a translucent overlay that darkens slightly to distinguish
   itself from the child area — works whether the box's text is light or dark. */
.bn-box .bn-header { display: flex; align-items: center; gap: 6px; padding: 3px 8px; font-size: 12px; cursor: pointer; user-select: none; background: rgba(0,0,0,0.10); border-radius: 6px 6px 0 0; color: inherit; }
.bn-box .bn-header:hover, .bn-box .bn-header:focus-visible { background: rgba(0,0,0,0.20); outline: none; }
.bn-box .bn-type { font-weight: 700; color: inherit; }
.bn-box .bn-meta { font-size: 10.5px; opacity: 0.82; color: inherit; }
.bn-box .bn-body { padding: 2px 8px 4px 8px; }
.bn-box .bn-ellipsis { padding: 4px 8px; font-size: 10.5px; opacity: 0.75; font-style: italic; color: inherit; }

[data-4cc] { cursor: help; }
[data-4cc]:focus-visible { outline: 1px dashed var(--accent); outline-offset: 2px; }

/* ---- Scalable content sizing. Everything below is driven by --font-scale. ---- */
.tree             { font-size: calc(12px * var(--font-scale)); }
.panel            { font-size: calc(12.5px * var(--font-scale)); }
table.fields      { font-size: calc(12.5px * var(--font-scale)); }
table.fields th   { font-size: calc(11px * var(--font-scale)); }
table.fields .note { font-size: calc(11px * var(--font-scale)); }
table.list        { font-size: calc(12px * var(--font-scale)); }
table.list th     { font-size: calc(11px * var(--font-scale)); }
.hexview          { font-size: calc(12px * var(--font-scale)); }
.box-tooltip      { font-size: calc(12px * var(--font-scale)); }
.box-tooltip .tt-code   { font-size: calc(13px * var(--font-scale)); }
.box-tooltip .tt-name   { font-size: calc(12px * var(--font-scale)); }
.box-tooltip .tt-metaline { font-size: calc(10.5px * var(--font-scale)); }
.box-tooltip .tt-syntax   { font-size: calc(11px * var(--font-scale)); }
.box-tooltip .tt-semantics { font-size: calc(11.5px * var(--font-scale)); }
.graphview .graph-node { font-size: calc(11px * var(--font-scale)); }
.graphview .graph-edge { font-size: calc(11px * var(--font-scale)); }
.warn-row         { font-size: calc(12px * var(--font-scale)); }
.fragtimeline .track h4 { font-size: calc(12px * var(--font-scale)); }
.diff-grid        { font-size: calc(12px * var(--font-scale)); }
.diff-grid h3     { font-size: calc(13px * var(--font-scale)); }

/* Focus ring */
button:focus-visible, select:focus-visible, input:focus-visible, [role="treeitem"]:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
