/* Copyright (c) Microsoft Corporation.
   Licensed under the MIT License. */

/* Circuit panel */
.qs-circuit-panel {
  p {
    padding: 10px 0px;
  }
}

/* Circuit widget */
.qs-circuit {
  /* Background color for the circuit diagram */
  background: var(--main-background);

  /* Error div in circuit panel */
  .qs-circuit-error {
    padding: 10px 0px;
  }

  .qs-circuit-error p {
    padding: 10px 0px;
  }

  /*
  Styles for Q# circuits.
  Copied and modified from the default CSS generated by
  https://github.com/microsoft/quantum-viz.js/blob/288e0cb506ee866ecca1b414c522e2d634f7b36d/src/styles.ts
*/

  /* Wires and gate outlines */
  line,
  circle,
  rect {
    stroke: var(--main-color);
    stroke-width: 1;
  }

  /* Text for qubit labels */
  text {
    fill: var(--main-color);
    dominant-baseline: middle;
    text-anchor: middle;
    user-select: none;
    pointer-events: none;
  }

  /* When run in VS Code, the "KaTeX_Main" and "KaTeX_Math" fonts are already present */
  /* When run in Jupyter in the browser, the MJX* (MathJax) fonts are used and present */
  .qs-maintext {
    font-family: "KaTeX_Main", "MJXZERO", "MJXTEX", sans-serif;
    font-style: normal;
  }

  .gate .qs-group-label {
    fill: var(--main-color);
    text-anchor: start;
  }

  .qs-mathtext {
    font-family: "KaTeX_Math", "MJXZERO", "MJXTEX-I", serif;
  }

  /* The math font used in KaTex (thus in VS Code) needs italic applied also, whereas
   the font used in MathJax (thus Jupyter web) doesn't. So apply if in VS Code  */
  [data-vscode-theme-kind] .qs-mathtext {
    font-style: italic;
  }

  /* Background for gates */
  .gate-unitary {
    fill: var(--vscode-input-background, #333333);
  }

  /* Text for gates */
  .gate text {
    fill: var(--vscode-input-foreground, #ffffff);
  }

  a.qs-circuit-source-link .qs-qubit-label {
    pointer-events: auto;
  }

  /* Hover effect for clickable gates */
  a.qs-circuit-source-link:hover .gate-unitary {
    fill: var(--vscode-button-secondaryHoverBackground, #4d4d4d);
  }

  a.qs-circuit-source-link:hover .gate-measure,
  a.qs-circuit-source-link:hover .gate-ket {
    fill: var(--vscode-button-hoverBackground, #005a9e);
  }

  a.qs-circuit-source-link:hover .qs-qubit-label {
    fill: var(--vscode-editorLink-activeForeground, #5d5d5d);
  }

  a.qs-circuit-source-link:hover .control-dot,
  a.qs-circuit-source-link:hover .control-line {
    fill: var(--vscode-editorLink-activeForeground, #5d5d5d);
    stroke: var(--vscode-editorLink-activeForeground, #5d5d5d);
  }

  a.qs-circuit-source-link:hover .oplus > line,
  a.qs-circuit-source-link:hover .oplus > circle {
    stroke: var(--vscode-editorLink-activeForeground, #5d5d5d);
  }

  /* Default style for arg-button */
  .arg-button {
    fill: var(--vscode-editor-foreground, #ffffff);
    text-decoration: none;
    cursor: default;
    pointer-events: none;
    word-spacing: -0.2em;
  }

  /* Edit mode style for arg-button (hyperlink appearance) */
  .arg-button.edit-mode {
    text-decoration: underline;
    cursor: pointer;
    pointer-events: all;
  }

  /* Dot and line for controlled gates */
  .control-line,
  .control-dot {
    fill: var(--main-color);
  }

  /* X gate */
  .oplus > line,
  .oplus > circle {
    fill: var(--main-background);
    stroke: var(--main-color);
    stroke-width: 2;
  }

  /* Measurement gate */
  .gate-measure {
    fill: var(--vscode-button-background, #007acc);
  }

  /* Measurement gate icon */
  .qs-line-measure,
  .arc-measure {
    stroke: var(--vscode-button-foreground, #ffffff);
    fill: none;
    stroke-width: 1;
  }

  /* Ket gate */
  .gate-ket {
    fill: var(--vscode-button-background, #007acc);
  }

  /* Ket gate text */
  text.ket-text {
    fill: var(--vscode-button-foreground, #ffffff);
    stroke: none;
  }

  /* SWAP gate */
  rect.gate-swap {
    fill: transparent;
    stroke: transparent;
  }

  /* Classical wire */
  .register-classical {
    stroke-width: 0.5;
  }

  /*
  The following styles are unused so they are unthemed.
  They relate to conditional circuits which are not currently supported.
*/

  .hidden {
    display: none;
  }

  .classically-controlled-btn circle {
    fill: var(--main-background);
    stroke-width: 1;
  }

  .classically-controlled-btn text {
    dominant-baseline: middle;
    text-anchor: middle;
    stroke: none;
    font-family: "KaTeX_Main", "MJXZERO", "MJXTEX", sans-serif;
    fill: var(--main-color);
  }

  .qviz .gate-collapse,
  .qviz .gate-expand {
    opacity: 0;
    transition: opacity 0.2s;
  }

  .qviz:hover .gate-collapse,
  .qviz:hover .gate-expand {
    visibility: visible;
    opacity: 0.2;
    transition: visibility 0.2s;
    transition: opacity 0.2s;
  }

  .qviz:hover .gate-collapse:hover,
  .qviz:hover .gate-expand:hover {
    visibility: visible;
    opacity: 1;
    transition: opacity 0.2s;
  }

  .gate-expand,
  .gate-collapse {
    cursor: pointer;
  }

  .gate-collapse circle,
  .gate-expand circle {
    fill: white;
    stroke-width: 2px;
    stroke: black;
  }

  .gate-collapse path,
  .gate-expand path {
    stroke-width: 4px;
    stroke: black;
  }

  .dropzone-layer {
    display: none;
  }
  .dropzone {
    fill-opacity: 0%;
    stroke-opacity: 0%;
  }
  .dropzone:hover {
    fill: var(--vscode-editor-selectionBackground, #ec7063);
    fill-opacity: 50%;
  }
  .dropzone-full-wire {
    fill-opacity: 0%;
    stroke-opacity: 0%;
  }
  .dropzone-full-wire:hover {
    fill: var(--vscode-editor-selectionBackground, #ec7063);
    fill-opacity: 50%;
  }
  .grab {
    cursor: grab;
  }
  .moving {
    cursor: move;
  }
  .copying {
    cursor: copy;
  }

  .panel {
    display: flex;
    flex-direction: row;
    height: 100%;
    align-items: stretch;
    min-height: 0; /* allow flex children to scroll */
  }

  .container {
    display: flex;
  }

  .edit-panel {
    display: flex;
    flex-direction: column;
  }

  .title {
    margin: 0 0 4px;
    font-size: 1.2rem;
    user-select: none;
  }

  .block {
    display: block;
  }

  .target-input,
  .controls-input,
  .parameters-input {
    margin-bottom: 8px;
  }

  .toolbox-panel {
    width: 100px;
    min-height: var(--minToolboxHeight);
    border-right: medium solid var(--vscode-editorWidget-border, #000000);
    height: 100%;
    min-height: 0; /* enable internal scroll in flex container */
    overflow-y: auto;
    overflow-x: hidden;
  }

  .toolbox-panel .title {
    margin-top: 6px;
  }

  .toolbox-panel-svg {
    width: -webkit-fill-available;
    min-height: var(--minToolboxHeight);
    height: auto; /* let SVG expand to content height */
  }

  .ghost {
    width: var(--minGateWidth);
    height: var(--gateHeight);
    position: absolute;
    z-index: 1;
    pointer-events: none;
    opacity: 0.5;
  }

  .ghost-opacity {
    opacity: 0.5;
  }

  .svg-run-button-rect {
    fill: var(--vscode-button-background, #007acc);
    rx: 8;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.15));
    transition:
      fill 0.2s,
      filter 0.2s;
  }

  .svg-run-button:hover .svg-run-button-rect,
  .svg-run-button:focus .svg-run-button-rect {
    fill: var(--vscode-button-hoverBackground, #005a9e);
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.25));
  }

  .svg-run-button:focus {
    outline: none;
  }

  .svg-run-button-text {
    fill: var(--vscode-button-foreground, #ffffff);
    font-size: 16px;
    font-family: "Segoe UI", Arial, sans-serif;
    font-weight: bold;
    text-anchor: middle;
    dominant-baseline: middle;
    pointer-events: none;
    letter-spacing: 1px;
    user-select: none;
  }

  .svg-run-button {
    cursor: pointer;
  }
}

.context-menu {
  position: absolute;
  background-color: var(--vscode-menu-background, #ffffff);
  border: 1px solid var(--vscode-menu-border, #cccccc);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  z-index: 1000;
  padding: 5px 0;
  border-radius: 4px;
}

.context-menu-option {
  padding: 8px 12px;
  cursor: pointer;
  white-space: nowrap;
  color: var(--vscode-menu-foreground, #000000);
}

.context-menu-option:hover {
  background-color: var(--vscode-menu-selectionBackground, #f0f0f0);
  color: var(--vscode-menu-selectionForeground, #000000);
}

.prompt-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.prompt-container {
  background: var(--vscode-editor-background, #ffffff);
  color: var(--vscode-editor-foreground, #000000);
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  text-align: center;
  width: 400px;
  max-width: 90%;
}

.prompt-message {
  margin-bottom: 10px;
  color: var(--vscode-editor-foreground, #000000);
  font-size: 1rem;
}

.prompt-input {
  width: 100%;
  padding: 8px;
  margin-bottom: 10px;
  background: var(--vscode-input-background, #ffffff);
  color: var(--vscode-input-foreground, #000000);
  border: 1px solid var(--vscode-input-border, #cccccc);
  font-size: 1rem;
  border-radius: 4px;
  box-sizing: border-box;
}

.prompt-buttons {
  display: flex;
  justify-content: space-between;
}

.prompt-button {
  padding: 8px 16px;
  margin: 0 5px;
  background: var(--vscode-button-background, #007acc);
  color: var(--vscode-button-foreground, #ffffff);
  border: 1px solid var(--vscode-button-border, transparent);
  cursor: pointer;
  border-radius: 4px;
}

.prompt-button:hover {
  background: var(--vscode-button-hoverBackground, #005a9e);
}

.prompt-button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.pi-button {
  width: 40px;
  height: 40px;
  padding: 0;
  text-align: center;
  line-height: 40px;
  font-size: 16px;
}

/* State visualizer panel */
.state-panel {
  flex: 0 0 0px;
  border-left: none; /* replaced by clickable edge */
  display: flex;
  flex-direction: column;
  background: var(--qdk-host-background, #ffffff);
  pointer-events: auto;
  height: 100%;
  min-height: 0; /* allow scroll in short windows */
  overflow-y: auto;
  overflow-x: hidden;
  position: relative; /* anchor toggle button */
  transition:
    flex-basis var(--stateAnimMs, 200ms) ease,
    width var(--stateAnimMs, 200ms) ease,
    padding-left var(--stateAnimMs, 200ms) ease;
  padding-left: 36px; /* reserve space for the clickable edge */
}

.state-panel:not(.collapsed) {
  flex-basis: 226px; /* 190px content + 36px edge */
}

.state-panel .state-svg {
  flex: 0 0 auto; /* size to content */
  width: 100%;
  height: auto; /* allow panel to scroll when content is taller */
  transition: opacity var(--stateAnimMs, 200ms) ease;
}

/* Loading overlay for async state compute */
.state-loading-overlay {
  position: absolute;
  top: 0;
  left: 36px; /* keep the clickable edge visible */
  right: 0;
  bottom: 0;
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 10;
  background: color-mix(
    in srgb,
    var(--qdk-host-background, #ffffff) 70%,
    transparent
  );
  backdrop-filter: blur(0.5px);
}

.state-panel.loading:not(.collapsed):not(.message) .state-loading-overlay {
  display: flex;
}

.state-loading-spinner {
  width: 22px;
  height: 22px;
  border-radius: 999px;
  border: 3px solid var(--qdk-host-foreground, #666);
  border-right-color: transparent;
  opacity: 0.8;
  animation: stateLoadingSpin 0.85s linear infinite;
}

@keyframes stateLoadingSpin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* Developer toolbar above the visualizer */
.dev-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  row-gap: 4px;
  width: 100%;
  padding: 4px 6px;
  font-size: 12px;
  border-bottom: 1px solid #ddd;
  transition: opacity var(--stateAnimMs, 200ms) ease;
}

.dev-toolbar-sep {
  opacity: 0.6;
}

.dev-toolbar input {
  width: 56px;
}

/* Collapsed state: shrink panel and hide content except the toggle */
.state-panel.collapsed {
  flex: 0 0 36px !important; /* match edge width */
  width: 36px; /* assist transition in some browsers */
  overflow: visible; /* keep toggle visible */
  padding-left: 0; /* only the edge remains visible */
}

/* Fade content while collapsing instead of instantly hiding */
.state-panel.collapsed .state-svg,
.state-panel.collapsed .dev-toolbar,
.state-panel.collapsed .state-panel-message {
  opacity: 0;
  pointer-events: none;
}

/* Full-height clickable edge with vertical text */
.state-edge {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 36px;
  background: var(--qdk-background-accent, #e6e6e6);
  border-right: 1px solid var(--qdk-widget-outline, #c8c8c8);
  box-shadow: inset -1px 0 0 rgba(0, 0, 0, 0.06);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  pointer-events: auto;
}

.state-edge:focus {
  outline: none;
}

.state-edge-text {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-size: 16px;
  font-weight: 600;
  color: var(--qdk-host-foreground, #333);
  letter-spacing: 0.65px;
  user-select: none;
}

/* Edge icons (top and bottom triangles) */
.state-edge .edge-icon {
  position: absolute;
  width: 18px;
  height: 18px;
  right: 8px;
  pointer-events: none; /* clicks go to the edge */
  transform-origin: 50% 50%;
}

.state-edge .edge-icon-top {
  top: 8px;
}
.state-edge .edge-icon-bottom {
  bottom: 8px;
}

.state-edge .edge-icon svg {
  width: 100%;
  height: 100%;
  display: block;
}
.state-edge .edge-icon path {
  fill: var(--qdk-host-foreground, #555);
  stroke: none; /* sharp corners, no outline */
}

/* When collapsed, flip icons to point left */
.state-panel.collapsed .state-edge .edge-icon {
  transform: rotate(180deg);
}

/* Empty state: hide SVG and toolbar */
.state-panel.message .state-svg,
.state-panel.message .dev-toolbar {
  height: auto;
  display: none;
}

.state-svg .state-header {
  font-size: 17px;
  font-weight: 600;
  text-anchor: start; /* override circuit default */
  dominant-baseline: hanging;
  pointer-events: none;
}

.state-svg .state-separator {
  stroke: #ddd;
  stroke-width: 1px;
}

.state-svg .state-bar {
  opacity: 0.9;
  transition:
    fill var(--stateAnimMs, 200ms) ease,
    opacity var(--stateAnimMs, 200ms) ease;
}

.state-svg .state-bar-label {
  font-size: 14px;
  text-anchor: middle;
  dominant-baseline: hanging;
  pointer-events: none;
}

.state-svg .state-phase-circle {
  fill: none;
  stroke: #666;
  stroke-width: 1px;
}

.state-svg .state-phase-text {
  text-anchor: middle;
  dominant-baseline: hanging; /* align like percentage labels */
  font-size: 14px; /* match percentage label size */
  pointer-events: none;
}

.state-svg .state-phase-dot {
  stroke: #333;
  stroke-width: 0.5px;
  transition:
    fill var(--stateAnimMs, 200ms) ease,
    opacity var(--stateAnimMs, 200ms) ease;
}

.state-svg .state-phase-wedge {
  stroke: none;
  fill-opacity: 0.75;
  transition:
    fill var(--stateAnimMs, 200ms) ease,
    opacity var(--stateAnimMs, 200ms) ease;
}

.state-svg .state-bitstring {
  font-size: 14px;
  text-anchor: middle;
  dominant-baseline: hanging;
  pointer-events: none;
}

/* Vertical orientation for long state labels to avoid collisions */

/* HTML-based vertical labels inside foreignObject */
.state-svg .state-bitstring-fo {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-size: 14px;
  line-height: 1;
  text-align: center;
  width: -moz-fit-content;
  width: fit-content; /* shrink to content so we can center via margins */
  margin-left: auto;
  margin-right: auto; /* center within the column width */
  color: var(--qdk-host-foreground, #000);
  pointer-events: none;
}

/* Centered empty-state message in the panel */
.state-panel-message {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 8px;
  text-align: center;
  font-size: 13px;
  color: var(--qdk-description-foreground);
  z-index: 20;
  pointer-events: none;
  transition: opacity var(--stateAnimMs, 200ms) ease;
}

/* Circuit container/wrapper layout extracted from inline styles */
.circuit-editor-container {
  display: flex;
  height: 80vh;
  width: 95vw;
  align-items: stretch;
}

.circuit-wrapper {
  display: block;
  overflow: auto;
  width: 100%;
  flex: 1 1 auto;
  min-width: 0;
}

/* Empty circuit message extracted from inline styles */
.empty-circuit-message {
  padding: 2em;
  text-align: center;
  color: var(--qdk-description-foreground);
  font-size: 1.1em;
}
