:root{
  --bg: #0b1220;
  --panel: #0f1a2d;
  --panel2:#0d1628;
  --text: #e7edf8;
  --muted:#a9b7d0;
  --line:#2a3a58;
  --green:#26d64b;
  --blue:#2e6bff;
  --gray:#9aa6b2;
}

html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial; }

.app { display: grid; grid-template-columns: 380px minmax(0, 1fr); height: 100%; }
.app.collapsed { grid-template-columns: 0 minmax(0, 1fr); }
.app.collapsed .sidebar { padding: 0; border-right: 0; width: 0; overflow: hidden; }

.sidebar {
  background: linear-gradient(180deg, var(--panel), var(--panel2));
  border-right: 1px solid var(--line);
  padding: 14px 14px 10px;
  overflow: auto;
}

.main { position: relative; overflow: hidden; min-width: 0; min-height: 0; }

.title { font-weight: 800; font-size: 16px; margin: 0 0 10px; }
[hidden] { display: none !important; }
#datasetCatalog a { color: #63cddd; font-size: 13px; }
#datasetProgress { width: 100%; accent-color: #63cddd; }
#datasetStatus, #datasetDescription { overflow-wrap: anywhere; }
.row { margin: 10px 0; }
.row label { display:block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
label.inline { display:flex; align-items:center; gap:8px; margin-bottom: 6px; }
label.inline input { margin: 0; }
.view-toggles { display: grid; gap: 8px; }
.view-toggles [hidden] { display: none; }

.row input[type="text"], .row input[type="number"], .row select{
  width: 100%;
  box-sizing: border-box;
  padding: 9px 10px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.03);
  color: var(--text);
  border-radius: 10px;
  outline: none;
}

.row input[type="file"]{ width: 100%; padding: 8px 0; color: var(--muted); }
.row input[type="range"]{ width: 100%; }
.row input[type="color"]{
  width: 100%;
  height: 38px;
  padding: 0;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.03);
  border-radius: 10px;
}

.controls { display: grid; gap: 10px; }
.sidebar-section { border-top: 1px solid var(--line); margin: 12px 0; padding-top: 10px; }
.sidebar-section > summary { cursor: pointer; font-size: 14px; font-weight: 700; padding: 7px 0; }
.sidebar-section > summary:focus-visible { outline: 2px solid #63cddd; outline-offset: 3px; }
.sidebar-section[open] > summary { margin-bottom: 8px; }
.sidebar-section.nested { margin: 8px 0; padding: 4px 0 0 10px; border-left: 2px solid var(--line); border-top: 0; }
.sidebar-section.nested > summary { font-size: 12px; color: var(--muted); }
.mode-help dt { color: var(--text); font-weight: 700; margin-top: 12px; }
.mode-help dd { margin: 4px 0 0; }
.grid2 { display:grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.small { font-size: 12px; color: var(--muted); line-height: 1.35; }
#savedFileStatus { overflow-wrap: anywhere; margin: 6px 0; }
#preferencesStatus { margin-top: 8px; }
#branchStatus { margin: 8px 0; overflow-wrap: anywhere; }
#searchStatus { margin-top: 6px; overflow-wrap: anywhere; }
#searchResults { margin-top: 8px; }
#searchSelection { margin-top: 8px; }
#searchSelection[hidden] { display: none; }
#searchSelectionSummary { margin-bottom: 6px; }
#searchResults[hidden], #moreSearchResults[hidden], #selectAllSearchMatches[hidden] { display: none; }
#selectAllSearchMatches { margin-top: 6px; }
#searchMatches { list-style: none; margin: 6px 0; padding: 0; max-height: 260px; overflow-y: auto;
  border: 1px solid var(--line); border-radius: 8px; overscroll-behavior: contain; }
#searchMatches li + li { border-top: 1px solid var(--line); }
.search-result-row { display: grid; grid-template-columns: 32px minmax(0, 1fr); align-items: center; }
.search-result-check { justify-self: center; cursor: pointer; accent-color: #f6d75c; }
.search-result-check:focus-visible { outline: 2px solid #63cddd; outline-offset: 3px; }
.search-result { display: block; width: 100%; padding: 9px 10px; border: 0;
  background: transparent; color: var(--text); text-align: left; cursor: pointer; }
.search-result-url { display: block; overflow-wrap: anywhere; font-size: 12px; line-height: 1.45; }
.search-result:hover { background: rgba(255,255,255,0.06); }
.search-result[aria-pressed="true"] { background: #3c3517; color: #fff1a8; box-shadow: inset 3px 0 #f6d75c; }
.search-result:focus-visible { outline: 2px solid #63cddd; outline-offset: -3px; }
#branchLegend[hidden] { display: none; }
.node-label.prefix-node { border-color: #63cddd; color: #b4eef5; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; }

.btn {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.05);
  color: var(--text);
  cursor: pointer;
  font-weight: 700;
}
.btn:hover { background: rgba(255,255,255,0.08); }
.btn:disabled { opacity: 0.45; cursor: default; }
.trace-controls { display: grid; gap: 8px; }
.trace-controls [hidden] { display: none; }
#traceSelection { overflow-wrap: anywhere; color: var(--text); }
.trace-path { margin: 0; padding-left: 24px; max-height: 260px; overflow: auto; }
.trace-path li { padding: 4px 0; color: var(--muted); font-size: 11px; overflow-wrap: anywhere; }
.trace-path button { font: inherit; text-align: left; color: var(--text); background: none;
  border: 0; cursor: pointer; padding: 2px; overflow-wrap: anywhere; }
.trace-path button:hover { text-decoration: underline; }
.trace-path .trace-relation { display: block; color: var(--muted); font-size: 10px; }

.toggle-btn {
  position: absolute;
  left: 14px;
  top: 14px;
  width: auto;
  padding: 8px 12px;
  font-size: 12px;
  z-index: 3;
}

.hr { height: 1px; background: var(--line); margin: 14px 0; }

.kicker { font-size: 12px; color: var(--text); font-weight: 800; margin-bottom: 6px; }
.val { float: right; color: var(--muted); font-size: 12px; }

.legend {
  position:absolute; right: 14px; top: 14px;
  background: rgba(12,18,32,0.72);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 10px;
  color: var(--muted);
  font-size: 12px;
  z-index: 2;
  user-select: none;
}

.legend .item { display:flex; gap:8px; align-items:center; margin: 6px 0; }
.swatch { width: 10px; height: 10px; border-radius: 999px; display:inline-block; }
.swatch.green { background: var(--green); }
.swatch.blue { background: var(--blue); }
.swatch.gray { background: var(--gray); }
.swatch.base { background: #ffe17d; border-radius: 1px; width: 12px; height: 12px; }
.swatch.trace-glow { border: 1px solid #6abbff; box-shadow: 0 0 7px 3px #259fff99; }

.hud {
  position: absolute;
  right: 14px;
  bottom: 14px;
  background: rgba(12,18,32,0.82);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  color: var(--muted);
  font-size: 12px;
  z-index: 4;
  user-select: none;
  pointer-events: none;
  min-width: 120px;
  text-align: right;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.hud div { line-height: 1.4; }
.hud { max-width: calc(100% - 48px); }
.hud .frame-status {
  /* Reserve this line so labels do not shift when the fit status changes. */
  visibility: hidden;
  margin-top: 3px;
  color: #e9f990;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.hud.all-in-frame {
  background: linear-gradient(135deg, rgba(218,242,83,0.10), transparent 70%), rgba(12,18,32,0.92);
  border-color: rgba(218,242,83,0.65);
  box-shadow: 0 0 16px rgba(218,242,83,0.12), inset 0 0 12px rgba(218,242,83,0.04), 0 10px 30px rgba(0,0,0,0.3);
}
.hud.all-in-frame #nodeCountsText { color: #f1f5cd; }
.hud.all-in-frame .frame-status { visibility: visible; }
.auto-actions { margin: 10px 0; }
.auto-actions .btn { font-size: 11px; padding: 8px; }
.val.manual::after { content: " · manual"; color: #d3b98b; font-size: 10px; }
.annotation-controls label.small { opacity: 0.8; font-size: 11px; }
#annotations { position: absolute; inset: 0; z-index: 3; pointer-events: none; overflow: hidden; }
.node-label, .connection-card, .branch-toggle {
  position: absolute; left: 0; top: 0; box-sizing: border-box;
  background: rgba(11, 18, 32, 0.9); color: var(--text);
  border: 1px solid rgba(115, 143, 188, 0.3); border-radius: 5px;
}
.node-label { padding: 3px 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: ui-monospace, Menlo, Consolas, monospace; line-height: 1.2;
  pointer-events: auto; cursor: pointer; text-align: left; }
.node-label.selected { background: #3c3517; color: #fff1a8; border: 2px solid #f6d75c;
  box-shadow: 0 0 0 2px rgba(246,215,92,0.2); z-index: 2; }
.node-label:focus-visible { outline: 2px solid #fff1a8; outline-offset: 2px; }
.node-label.trace-node { border-color: #d8b443; }
.node-label.base-node { border-color: #ffe17d; color: #fff1b3; font-weight: 700; }
.branch-toggle { pointer-events: auto; cursor: pointer; padding: 0; color: #b4eef5;
  border-color: #63cddd; font: bold 16px/1 ui-monospace, Menlo, monospace; }
.branch-toggle[aria-expanded="false"] { background: #163a48; }
.branch-toggle:focus-visible { outline: 2px solid #fff1a8; outline-offset: 2px; z-index: 4; }
.node-label.collapsed-branch { border-style: dashed; }
.connection-card { padding: 5px 7px; pointer-events: auto; text-align: center;
  box-shadow: 0 3px 10px rgba(0,0,0,0.25); font-size: 11px; }
.connection-title { color: var(--muted); overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  font-size: 10px; line-height: 15px; padding-right: 12px; }
.connection-close { position: absolute; top: 2px; right: 3px; background: none; border: 0;
  color: var(--muted); cursor: pointer; font-size: 13px; padding: 0 2px; }
.connection-counts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 3px 6px; max-height: 64px; overflow: auto; overscroll-behavior: contain;
  font-variant-numeric: tabular-nums; line-height: 16px; }
.connection-counts span { white-space: nowrap; }
.connection-counts b { color: #e7edf8; }
.connection-more { grid-column: 1 / -1; background: none; color: var(--muted); border: 0;
  cursor: pointer; font-size: 10px; padding: 2px; }

.details h3 { margin: 0 0 8px; font-size: 14px; }
.kv { font-size: 12px; color: var(--muted); line-height: 1.4; }
.kv b { color: var(--text); font-weight: 800; }
.details-items { display:grid; gap: 4px; margin: 6px 0 10px; }
.details-list { margin: 4px 0 10px; }
.details-list summary { cursor: pointer; font-size: 12px; color: var(--muted); }
.list-link {
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 12px;
  text-align: left;
  padding: 2px 0;
  cursor: pointer;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}
.list-link:hover { color: var(--green); }

.progress {
  width: 100%;
  height: 8px;
  background: rgba(255,255,255,0.08);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--line);
  margin-top: 8px;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #2e6bff, #26d64b);
  transition: width 0.2s ease-out;
}

.progress-meta { margin-top: 6px; }

.three-wrap {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  background: var(--bg);
}

/* Canvas must fill container; JS sets backing resolution for DPI */
#canvas {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
}

@media (max-width: 700px) {
  .terrain-public .app, .terrain-public .app.collapsed { grid-template-columns: minmax(0, 1fr); }
  .terrain-public .main { grid-column: 1; grid-row: 1; }
  .terrain-public .sidebar { position: absolute; z-index: 10; top: 0; bottom: 0; left: 0;
    width: min(340px, 100vw); box-sizing: border-box; padding-top: 58px; }
  .terrain-public .app.collapsed .sidebar { width: 0; padding: 0; }
  .terrain-public .toggle-btn { z-index: 20; }
  .terrain-public .legend { top: 60px; max-width: calc(100% - 48px); }
}

/* Large, keyboard-accessible view switches retain native checkbox semantics. */
.row label.view-toggle { display: flex; margin-bottom: 0; color: var(--text); align-items: center; justify-content: center; gap: 10px; min-height: 44px; box-sizing: border-box; }
.view-toggle:has(input:checked) { border-color: #82b9ff; background: #203b60; }
.view-toggle:focus-within { outline: 2px solid #82b9ff; outline-offset: 2px; }
.node-label.text-full, .node-label.text-wrap { text-overflow: clip; }
.node-label.text-wrap { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-all; }

.control-group { margin: 0; padding: 1px 0; border: 0; min-width: 0; }
.control-group + .control-group { border-top: 1px solid var(--line); }
.control-group > legend { color: var(--text); font-size: 14px; font-weight: 750; padding: 0; }
.control-label { display: flex; align-items: baseline; gap: 4px; }
.help-button { background: none; border: 0; padding: 0 2px; color: #82b9ff; cursor: pointer; font: inherit; font-size: 12px; }
.help-button:focus-visible { outline: 2px solid #82b9ff; }
.primary-view { border-color: #476c9f; background: #172b45; }
.rotation-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; align-items: center; }
.row .rotation-speed label { display: flex; justify-content: space-between; gap: 4px; margin-bottom: 2px; }
.rotation-speed output { color: var(--text); font-variant-numeric: tabular-nums; }
#labelControls .view-toggle { justify-content: flex-start; text-align: left; padding: 9px 8px; }
.view-history { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin: 8px 0 12px; }
.icon-button { width: 42px; min-height: 38px; padding: 4px; font-size: 22px; flex-shrink: 0; }
.view-history .btn { min-height: 38px; padding: 4px; font-size: 22px; }
#historyStatus { grid-column: 1 / -1; overflow-wrap: anywhere; }
#historyStatus:empty { display: none; }
#datasetCatalog { margin-bottom: 0; }
.dataset-actions { display: grid; gap: 8px; margin-top: 8px; }
.selected-name-toggle { margin-top: 8px; }
.selected-node-name { position: absolute; z-index: 4; box-sizing: border-box;
  display: flex; align-items: center; justify-content: center; padding: 0 8px;
  border: 1px solid var(--line); border-radius: 8px; background: rgba(12,18,32,0.6);
  color: var(--text); font-family: ui-monospace, Menlo, Consolas, monospace;
  white-space: nowrap; overflow: hidden; pointer-events: auto; user-select: text; }
.degree-threshold-row { display: grid; grid-template-columns: minmax(0,1fr) 36px; align-items: end; gap: 8px; margin-bottom: 8px; }
.degree-threshold-row .btn { min-height: 36px; padding: 4px; }
#degreeThresholdStatus:empty { display: none; }
#traceSection.inactive { opacity: 0.5; }
.viewer-dialog { width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 48px); box-sizing: border-box; overflow: auto; padding: 20px; color: var(--text); background: var(--panel); border: 1px solid #476c9f; border-radius: 12px; }
.viewer-dialog::backdrop { background: #0009; }
.dialog-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.dialog-heading h2 { margin: 0; font-size: 18px; }
.viewer-dialog h3 { margin: 16px 0 8px; font-size: 13px; }
.hotkeys-list { display: grid; grid-template-columns: max-content minmax(0, 1fr); align-items: baseline; gap: 8px 14px; }
.hotkeys-list dd { margin: 0; }
.hotkeys-list kbd { display: inline-block; padding: 2px 5px; border: 1px solid var(--line); border-radius: 4px; color: var(--text); }
.shortcut-feedback { position: absolute; bottom: 12px; left: 12px; z-index: 5; max-width: calc(100% - 24px); box-sizing: border-box; padding: 8px 12px;
  background: var(--panel); color: var(--text); border: 1px solid #476c9f; border-radius: 8px; font-size: 13px; pointer-events: none; }
.shortcut-feedback:empty { opacity: 0; }
.overlay-disclosure { pointer-events: auto; cursor: pointer; z-index: 4; }
.overlay-disclosure > summary { list-style: none; display: flex; align-items: center; gap: 7px; cursor: pointer; }
.overlay-disclosure > summary::-webkit-details-marker { display: none; }
.overlay-disclosure > summary:focus-visible { outline: 2px solid #82b9ff; outline-offset: 3px; }
.overlay-disclosure .disclosure-arrow { width: 10px; flex-shrink: 0; }
.overlay-disclosure:not([open]) { min-width: 0; }
#legendSummary { display: flex; align-items: center; gap: 8px; }
#fpsHud > summary { justify-content: flex-end; }
.swatch.prefix { background: #63cddd; }

.loading-timers { font-size: 11px; color: var(--muted); margin: 3px 0 10px; }
#datasetProgress { width: 100%; }
