html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #1e1e1e;
  color: #e6e6e6;
  font-family: Arial, Helvetica, sans-serif;
}

:root {
  --explorer-width: 320px;
  --inspector-width: 360px;
  --topbar-height: 76px;
  --toolbar-bg: rgba(22, 27, 34, 0.96);
  --panel-border: #394b5f;
  --muted-text: #9cabb9;
  --accent: #4f8cc9;
}

#circuit-canvas {
  position: fixed;
  inset: 0 0 0 auto;
  width: 100vw;
  height: 100vh;
  display: block;
  background: #1e1e1e;
}

body.explorer-open #circuit-canvas {
  width: calc(100vw - var(--explorer-width));
}

.toolbar {
  position: fixed;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 0;
  background: rgba(22, 27, 34, 0.92);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
  user-select: none;
}

#topbar {
  top: 0;
  left: 0;
  right: 0;
  display: grid;
  grid-template-columns: minmax(240px, 300px) minmax(180px, 1fr) auto 310px;
  height: var(--topbar-height);
  box-sizing: border-box;
}

.scenario-control {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-width: 0;
}

#scenario-select {
  min-width: 0;
  width: 100%;
  text-overflow: ellipsis;
}

#program-controls {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 5px;
  font-size: 11px;
}

#program-controls select {
  min-width: 0;
  width: 100%;
  padding: 3px;
  font-size: 11px;
}

body.program-view { --topbar-height: 104px; }

.timeline-track {
  display: flex;
  min-width: 0;
}

.timeline-controls {
  display: flex;
  gap: 8px;
  min-width: max-content;
}

.timeline-readouts {
  display: grid;
  align-content: center;
  min-width: 0;
  height: 56px;
  line-height: 1.25;
}

#viewbar {
  z-index: 16;
  left: 12px;
  right: 12px;
  bottom: 12px;
  height: 60px;
  padding: 8px;
  box-sizing: border-box;
  transition: left 160ms ease;
}

.panel-toggles,
.view-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-toggles,
#viewbar button {
  flex: 0 0 auto;
}

.view-actions {
  flex: 1;
  min-width: 0;
  height: 44px;
  overflow-x: auto;
  scrollbar-width: thin;
  white-space: nowrap;
}

#viewbar button[aria-pressed="true"] {
  background: #256aa7;
  border-color: #7cc5ff;
}

body.explorer-open #viewbar {
  left: calc(var(--explorer-width) + 12px);
}

button,
select,
input {
  height: 36px;
  border: 1px solid #4f6f93;
  background: #3d5a80;
  color: #ffffff;
  font: inherit;
  padding: 0 12px;
  border-radius: 6px;
}

button:hover,
select:hover,
input:hover {
  background: #4f739f;
}

button:active {
  background: #293241;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

button.primary {
  border-color: #6aa7e0;
  background: #256aa7;
}

button.primary:hover {
  background: #307fbd;
}

input[type="color"] {
  padding: 2px;
}

input[type="range"] {
  padding: 0;
}

#time-slider {
  min-width: 0;
  width: 100%;
}

#time-label {
  display: block;
  min-width: 0;
  color: #dddddd;
  font-size: 12px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  white-space: normal;
}

#save-status,
.scenario-label {
  color: #b9c6d3;
  font-size: 14px;
}

#stats-label {
  display: block;
  min-width: 0;
  color: #8fa2b5;
  font-size: 11px;
  line-height: 1.15;
  overflow-wrap: anywhere;
  white-space: normal;
}

#tooltip {
  position: fixed;
  z-index: 20;
  display: none;
  max-width: 420px;
  padding: 6px 8px;
  border: 1px solid #dce6f0;
  border-radius: 4px;
  background: rgba(18, 24, 32, 0.95);
  color: #f5f5f5;
  font-size: 13px;
  pointer-events: none;
  white-space: nowrap;
}

/* Component fidelity explorer */

#component-explorer {
  position: fixed;
  z-index: 14;
  top: var(--topbar-height);
  bottom: 0;
  left: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  width: var(--explorer-width);
  box-sizing: border-box;
  border-right: 1px solid var(--panel-border);
  background: #181d24;
  box-shadow: 8px 0 24px rgba(0, 0, 0, 0.24);
  transform: translateX(-100%);
  transition: transform 160ms ease;
}

body.explorer-open #component-explorer {
  transform: translateX(0);
}

.explorer-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px 10px;
}

.panel-close {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  padding: 0;
  font-size: 22px;
}

.explorer-title {
  color: #f2f5f8;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.explorer-subtitle {
  margin-top: 4px;
  color: var(--muted-text);
  font-size: 11px;
  line-height: 1.35;
}

.explorer-search-wrap {
  padding: 0 10px 10px;
}

#explorer-search {
  width: 100%;
  height: 30px;
  box-sizing: border-box;
  border-color: #3d4c5e;
  background: #242b34;
  padding: 0 9px;
  font-size: 12px;
}

#explorer-viewport {
  position: relative;
  min-height: 0;
  overflow: auto;
  border-top: 1px solid #242d38;
  border-bottom: 1px solid #242d38;
  scrollbar-color: #4b5b6c #181d24;
  scrollbar-width: thin;
}

#explorer-spacer {
  width: 1px;
  pointer-events: none;
}

#explorer-rows {
  position: absolute;
  inset: 0 0 auto;
}

.explorer-row {
  position: absolute;
  right: 0;
  left: 0;
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr) auto 24px;
  align-items: center;
  height: 28px;
  box-sizing: border-box;
  padding-right: 7px;
  color: #cdd6df;
  font-size: 12px;
  cursor: default;
  user-select: none;
}

.explorer-row:hover {
  background: #252e39;
}

.explorer-row.selected {
  background: #23486b;
  color: #ffffff;
}

.explorer-row.explicit-override::before {
  position: absolute;
  top: 5px;
  bottom: 5px;
  left: 2px;
  width: 2px;
  border-radius: 2px;
  background: #5ba6e6;
  content: "";
}

.explorer-arrow,
.explorer-reset {
  width: 22px;
  height: 24px;
  border: 0;
  border-radius: 3px;
  background: transparent;
  padding: 0;
  color: #c9d3dd;
  font-size: 15px;
}

.explorer-arrow:hover,
.explorer-reset:hover {
  background: #344252;
}

.explorer-arrow:disabled {
  background: transparent;
  color: #637180;
  opacity: 1;
}

.explorer-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.explorer-badge {
  min-width: 17px;
  border: 1px solid #4b5968;
  border-radius: 8px;
  padding: 1px 5px;
  color: #aeb9c4;
  font-size: 9px;
  line-height: 13px;
  text-align: center;
  text-transform: uppercase;
}

.explorer-badge.structural {
  border-color: #4e87b9;
  color: #83b9e7;
}

.explorer-badge.behavioral {
  border-color: #8b6aa8;
  color: #c29be4;
}

.explorer-footer {
  display: grid;
  gap: 8px;
  padding: 10px;
  background: #151a20;
}

#explorer-status {
  min-height: 16px;
  color: var(--muted-text);
  font-size: 11px;
}

#explorer-status.error {
  color: #ff8989;
}

.explorer-actions {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 8px;
}

.explorer-actions button {
  height: 32px;
  padding: 0 8px;
  font-size: 12px;
}

#explorer-resizer {
  position: absolute;
  z-index: 2;
  top: 0;
  right: -4px;
  bottom: 0;
  width: 8px;
  cursor: col-resize;
}

body:not(.explorer-open) #explorer-resizer {
  pointer-events: none;
}

.inspector {
  position: fixed;
  z-index: 12;
  top: calc(var(--topbar-height) + 8px);
  right: 12px;
  bottom: 84px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: min(var(--inspector-width), calc(100vw - 24px));
  max-height: none;
  box-sizing: border-box;
  overflow: hidden;
  border: 1px solid rgba(96, 125, 156, 0.8);
  border-radius: 12px;
  background: rgba(18, 24, 32, 0.96);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.46), 0 2px 10px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(12px);
}

.inspector-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  padding: 14px 14px 12px;
  border-bottom: 1px solid rgba(79, 111, 147, 0.5);
  background: linear-gradient(180deg, rgba(48, 65, 84, 0.72), rgba(27, 36, 47, 0.72));
}

.inspector-heading {
  min-width: 0;
}

.inspector-eyebrow {
  margin-bottom: 5px;
  color: #77b5ea;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.inspector-title {
  min-width: 0;
  color: #f5f5f5;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inspector-subtitle,
.inspector-value {
  color: #aebdcb;
  font-size: 12px;
}

.inspector-subtitle {
  display: -webkit-box;
  margin-top: 3px;
  line-height: 1.35;
  overflow: hidden;
  overflow-wrap: anywhere;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.inspector-path {
  max-height: 48px;
  margin-top: 9px;
  padding: 6px 8px;
  border: 1px solid rgba(79, 111, 147, 0.46);
  border-radius: 6px;
  background: rgba(8, 13, 19, 0.46);
  color: #c9d7e4;
  font-family: Consolas, "Liberation Mono", monospace;
  font-size: 11px;
  line-height: 1.35;
  overflow: auto;
  overflow-wrap: anywhere;
}

.inspector-close {
  flex: 0 0 32px;
  width: 32px;
  height: 32px;
  padding: 0;
  border-color: rgba(121, 151, 180, 0.55);
  background: rgba(44, 59, 76, 0.8);
  color: #dce8f2;
  font-size: 20px;
  line-height: 1;
}

.inspector-body {
  display: grid;
  align-content: start;
  gap: 10px;
  min-height: 0;
  padding: 12px 12px 18px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  scrollbar-color: #536d88 #18202a;
  scrollbar-width: thin;
}

.inspector-body::-webkit-scrollbar,
.inspector-path::-webkit-scrollbar {
  width: 9px;
  height: 9px;
}

.inspector-body::-webkit-scrollbar-track,
.inspector-path::-webkit-scrollbar-track {
  background: #18202a;
}

.inspector-body::-webkit-scrollbar-thumb,
.inspector-path::-webkit-scrollbar-thumb {
  border: 2px solid #18202a;
  border-radius: 999px;
  background: #5d7894;
}

.inspector-section {
  display: grid;
  gap: 8px;
  min-width: 0;
  margin: 0;
  padding: 12px;
  border: 1px solid rgba(79, 111, 147, 0.42);
  border-radius: 9px;
  background: rgba(44, 57, 73, 0.48);
}

.inspector-section-title {
  color: #e3edf6;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.035em;
  text-transform: uppercase;
}

.inspector-field {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  color: #b9c6d3;
  font-size: 12px;
}

.inspector-field input {
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
}

.inspector-table {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  gap: 8px 10px;
  min-width: 0;
  color: #b9c6d3;
  font-size: 12px;
}

.inspector-key {
  color: #8fa7bf;
  line-height: 1.35;
}

.inspector-data {
  min-width: 0;
  color: #e8eef5;
  font-family: Consolas, "Liberation Mono", monospace;
  line-height: 1.35;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.pin-list {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.pin-group {
  display: grid;
  gap: 5px;
}

.pin-group-title {
  color: #99b4cd;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.pin-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  height: auto;
  min-height: 26px;
  padding: 4px 8px;
  box-sizing: border-box;
  border-color: rgba(89, 124, 159, 0.68);
  background: rgba(54, 76, 100, 0.5);
  text-align: left;
}

.pin-row:hover {
  border-color: #6fa7d8;
  background: rgba(67, 100, 133, 0.68);
}

.pin-row-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pin-row-value {
  max-width: 132px;
  overflow: hidden;
  color: #ffffff;
  font-family: Consolas, "Liberation Mono", monospace;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.color-control {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.color-control input[type="color"] {
  width: 42px;
  padding: 2px;
}

.color-code-input {
  font-family: Consolas, "Liberation Mono", monospace;
  text-transform: lowercase;
}

.color-code-input.invalid {
  border-color: #ff6b6b;
  background: #4a2630;
}

.inspector-section > button,
.inspector-actions button {
  width: 100%;
  height: 34px;
  box-sizing: border-box;
  background: rgba(61, 90, 128, 0.72);
  font-size: 12px;
}

.inspector-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

#loading {
  position: fixed;
  z-index: 30;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 14px 18px;
  border: 1px solid #5e7b9b;
  background: rgba(18, 24, 32, 0.96);
  color: #f5f5f5;
}

#loading::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  margin-right: 10px;
  border: 2px solid rgba(245, 245, 245, 0.28);
  border-top-color: #7cc5ff;
  border-radius: 50%;
  vertical-align: -2px;
  animation: loading-spin 0.8s linear infinite;
}

@keyframes loading-spin {
  to {
    transform: rotate(360deg);
  }
}

body.explorer-open #loading {
  margin-left: calc(var(--explorer-width) / 2);
}

@media (max-width: 1180px) {
  #topbar {
    grid-template-columns: 200px minmax(120px, 1fr) auto 280px;
    gap: 7px;
  }

  .scenario-label {
    display: none;
  }

  .scenario-control {
    grid-template-columns: minmax(0, 1fr);
  }

  .timeline-controls {
    gap: 5px;
  }

  .timeline-controls button {
    padding: 0 8px;
  }
}

/* An iframe or a narrow standalone window is a circuit-first view. Panels
   open on demand above the canvas, never over the persistent panel controls. */
body.compact-view {
  --topbar-height: 128px;
}

body.compact-view #topbar {
  grid-template-columns: minmax(32px, 1fr) auto;
  grid-template-rows: 32px 32px minmax(0, 1fr);
  grid-template-areas: "scenario scenario" "timeline controls" "readouts readouts";
  gap: 6px;
  padding: 8px;
}

body.compact-view.program-view { --topbar-height: 166px; }
body.compact-view.program-view #topbar {
  grid-template-rows: 70px 32px minmax(0, 1fr);
}

body.compact-view .scenario-control {
  grid-area: scenario;
  grid-template-columns: minmax(0, 1fr);
}
body.compact-view .scenario-label { display: none; }
body.compact-view .timeline-track { grid-area: timeline; }
body.compact-view .timeline-controls { grid-area: controls; gap: 4px; }
body.compact-view .timeline-readouts { grid-area: readouts; height: auto; }
body.compact-view #topbar button,
body.compact-view #topbar select,
body.compact-view #topbar input {
  height: 32px;
  font-size: 12px;
}
body.compact-view .timeline-controls button { padding: 0 6px; }
body.compact-view #time-label,
body.compact-view #stats-label { font-size: 10px; }

body.compact-view.explorer-open #circuit-canvas { width: 100vw; }
body.compact-view.explorer-open #viewbar { left: 12px; }
body.compact-view #component-explorer {
  max-width: min(320px, calc(100vw - 24px));
  bottom: 84px;
}
body.compact-view #explorer-resizer { display: none; }
body.compact-view .inspector {
  width: min(320px, calc(100vw - 24px));
}
body.compact-view.explorer-open #loading { margin-left: 0; }

.hidden {
  display: none !important;
}
.memory-address-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  margin-top: 12px;
}
.memory-address-form label { grid-column: 1 / -1; font-size: 12px; }
.memory-address-form input { min-width: 0; width: 100%; box-sizing: border-box; }
.memory-page-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.memory-page-actions button { font-size: 12px; padding: 6px; }
#memory-follow[aria-pressed="true"] { border-color: #59d68d; }
