/*
 * Qubibyte Chemistry: the molecule builder (Build mode on the Structure tab,
 * js/build/). Tools on the left, elements and groups on the right, actions at
 * the bottom left; on phones the three bars sit under the canvas as strips.
 * Everything here keys off #app[data-build], which paint-boot.js sets before
 * the first paint, so a refresh in Build mode shows the final layout at once.
 */

.build-tools,
.build-palette,
.build-acts,
.build-empty,
.build-tip {
  display: none;
}

.mol-app[data-tab='structure'][data-build='2d'] .build-tools,
.mol-app[data-tab='structure'][data-build='2d'] .build-palette,
.mol-app[data-tab='structure'][data-build] .build-acts {
  display: flex;
}

.mol-app[data-tab='structure'][data-build='2d'][data-build-empty] .build-empty {
  display: grid;
}

.mol-app[data-kind='pdb'] #build-btn,
.mol-app[data-kind='peptide'] #build-btn {
  display: none;
}

.mol-app[data-build='2d'] .mol-2d {
  touch-action: none;
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
}

/* Nothing drawn yet: one sentence in the middle. */
.build-empty {
  position: absolute;
  inset: 0;
  place-items: center;
  padding: var(--space-6) 120px;
  color: var(--text-faint);
  font-size: var(--text-sm);
  text-align: center;
  pointer-events: none;
}

/* ── The three bars ──────────────────────────────────────────────── */

.build-tools,
.build-palette,
.build-acts {
  position: absolute;
  z-index: var(--z-overlay);
  gap: 2px;
  padding: 3px;
  border: var(--hairline) solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
}

.build-tools,
.build-palette {
  flex-direction: column;
  align-items: center;
  top: 48px;
}

.build-tools {
  left: var(--space-3);
}

.build-palette {
  right: var(--space-3);
}

.build-acts {
  left: var(--space-3);
  bottom: var(--space-3);
  align-items: center;
}

.bd-sep {
  flex: none;
  align-self: stretch;
  height: var(--hairline);
  margin: 3px 4px;
  background: var(--border);
}

.build-acts .bd-sep {
  width: var(--hairline);
  height: auto;
  margin: 4px 3px;
}

.bd-btn,
.bd-el,
.bd-grp,
.bd-bond {
  display: inline-grid;
  place-items: center;
  flex: none;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-ui);
  cursor: pointer;
  touch-action: none;
}

.bd-btn {
  width: 30px;
  height: 30px;
}

.bd-btn .icon {
  width: 16px;
  height: 16px;
  stroke-width: 1.35;
}

.bd-el,
.bd-grp,
.bd-bond {
  width: 42px;
  height: 26px;
}

.bd-bond .icon {
  width: 18px;
  height: 18px;
  stroke-width: 1.4;
}

/* A section's name over its buttons (Groups, Bonds, Rings). */
.bd-label {
  flex: none;
  align-self: stretch;
  padding: 3px 0 1px;
  color: var(--text-faint);
  font-family: var(--font-ui);
  font-size: 9px;
  font-weight: var(--weight-medium);
  letter-spacing: 0.04em;
  line-height: 1;
  text-align: center;
  text-transform: uppercase;
  user-select: none;
}

.bd-el {
  color: color-mix(in oklab, var(--c, var(--text)) 64%, var(--text));
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.bd-grp {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  white-space: nowrap;
}

.bd-grp sub {
  font-size: 0.8em;
  line-height: 0;
  vertical-align: -0.25em;
}

.bd-more {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--text-muted);
}

.bd-more .icon {
  width: 14px;
  height: 14px;
  stroke-width: 1.2;
}

.bd-more-el:empty {
  display: none;
}

.bd-more[aria-pressed='true'] .icon {
  display: none;
}

.bd-btn:hover,
.bd-el:hover,
.bd-grp:hover,
.bd-bond:hover {
  background: var(--surface-hover);
  color: var(--text);
}

/* Carbon and hydrogen are drawn in the text colour on the canvas, so here too. */
.bd-el[data-tool='el:C'],
.bd-el[data-tool='el:H'] {
  --c: var(--text);
}

.bd-el:hover {
  color: color-mix(in oklab, var(--c, var(--text)) 64%, var(--text));
}

.bd-btn[aria-pressed='true'],
.bd-el[aria-pressed='true'],
.bd-grp[aria-pressed='true'],
.bd-bond[aria-pressed='true'] {
  background: var(--accent-wash);
  box-shadow: inset 0 0 0 var(--hairline) var(--accent-line);
}

.bd-btn[aria-pressed='true'],
.bd-grp[aria-pressed='true'],
.bd-bond[aria-pressed='true'],
.bd-more[aria-pressed='true'] {
  color: var(--accent-text);
}

.bd-btn:focus-visible,
.bd-el:focus-visible,
.bd-grp:focus-visible,
.bd-bond:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.build-acts .btn.sm {
  height: var(--control-h-sm);
  border-color: transparent;
  background: transparent;
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.build-acts .btn.sm:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.build-acts .btn[aria-pressed='true'] {
  background: var(--accent-wash);
  color: var(--accent-text);
}

.build-acts .icon-btn.sm .icon {
  width: 14px;
  height: 14px;
}

.mol-app[data-build='3d'] .bd-2d,
.mol-app[data-build='2d'] .bd-3d {
  display: none;
}

/* Tooltips beside the vertical bars. */
[data-tip-pos='right']:hover::after,
[data-tip-pos='right']:focus-visible::after {
  top: 50%;
  left: calc(100% + var(--space-2));
  transform: translateY(-50%);
}

[data-tip-pos='left']:hover::after,
[data-tip-pos='left']:focus-visible::after {
  top: 50%;
  left: auto;
  right: calc(100% + var(--space-2));
  transform: translateY(-50%);
}

/* The reason a valence ring is there, next to the pointer. */
.build-tip:not([hidden]) {
  display: block;
  position: absolute;
  left: 0;
  top: 0;
  z-index: var(--z-popover);
  max-width: 280px;
  padding: var(--space-1) var(--space-2);
  border: var(--hairline) solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-raised);
  color: var(--text);
  font-size: var(--text-xs);
  line-height: var(--leading-ui);
  pointer-events: none;
  box-shadow: var(--shadow-popover);
}

/* An element or ring being dragged from the bars. */
.bd-drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: var(--z-toast);
  border: var(--hairline) solid var(--accent-line);
  background: var(--surface-raised);
  box-shadow: var(--shadow-popover);
  pointer-events: none;
}

/* ── On the canvas ───────────────────────────────────────────────── */

.m2-warn circle {
  fill: var(--danger-wash);
  stroke: color-mix(in oklab, var(--danger) 72%, transparent);
  stroke-width: 1.25;
}

.bd-hov {
  fill: var(--accent-wash);
  stroke: var(--accent-line);
  stroke-width: 1;
}

.bd-hov-b line {
  stroke: var(--accent-wash);
  stroke-linecap: round;
}

/* The atom picked as the start of a bond (click it, then another atom). */
.bd-arm {
  fill: color-mix(in oklab, var(--accent) 16%, transparent);
  stroke: var(--accent);
  stroke-width: 1.5;
}

/* A bond the valence rules refuse: shown, in the danger colour, and not made. */
.bd-hov.is-no {
  fill: var(--danger-wash);
  stroke: color-mix(in oklab, var(--danger) 72%, transparent);
}

.bd-pv.is-no line {
  stroke: var(--danger);
  stroke-dasharray: 5 4;
}

.bd-pv-end.is-no {
  fill: var(--danger);
}

.bd-sel line {
  stroke: color-mix(in oklab, var(--accent) 22%, transparent);
  stroke-linecap: round;
}

.bd-sel-a circle {
  fill: color-mix(in oklab, var(--accent) 22%, transparent);
  stroke: var(--accent-line);
  stroke-width: 1;
}

.bd-pv line {
  stroke: var(--accent);
  stroke-linecap: round;
}

.bd-pv-end {
  fill: var(--accent);
}

.bd-ghost-l {
  fill: color-mix(in oklab, var(--c) 64%, var(--text));
  font-family: var(--font-ui);
  font-weight: var(--weight-medium);
  opacity: 0.8;
}

.bd-box {
  fill: color-mix(in oklab, var(--accent) 7%, transparent);
  stroke: var(--accent-line);
  stroke-width: 1;
  stroke-dasharray: 4 3;
}

/* ── Around the stage ────────────────────────────────────────────── */

.mol-readout .ro-solo {
  margin-left: 0;
}

.mol-readout .ro-note {
  color: var(--text-faint);
}

.mol-readout .ro-warn {
  color: var(--danger);
}

.mol-readout .ro-smiles {
  min-width: 0;
  overflow: hidden;
  color: var(--text-muted);
  text-overflow: ellipsis;
}

.in-acts {
  display: flex;
  gap: var(--space-2);
  margin: var(--space-2) 0 var(--space-1);
}

.in-acts .btn.sm {
  gap: var(--space-1);
  color: var(--text-muted);
}

.in-acts .btn.sm:hover {
  color: var(--text);
}

.lib-new.btn.sm {
  flex: none;
  gap: var(--space-1);
  height: var(--control-h-sm);
  padding: 0 var(--space-2);
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.lib-new.btn.sm:hover {
  color: var(--text);
}

.lib-new .icon {
  width: 12px;
  height: 12px;
}

/* ── Medium widths: the stage is shorter, the bars a little tighter ── */

@media (max-width: 1199px) {
  .bd-btn {
    height: 28px;
  }

  .bd-el,
  .bd-grp,
  .bd-bond {
    height: 24px;
  }
}

/* ── Phones: the bars become strips under the canvas ────────────── */

@media (max-width: 759px) {
  .mol-app[data-tab='structure'][data-build='2d'] .mol-stage {
    aspect-ratio: auto;
    height: calc(100vw + 162px);
    max-height: none;
  }

  .build-tools,
  .build-palette {
    flex-direction: row;
    top: auto;
    left: var(--space-2);
    right: var(--space-2);
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
  }

  .build-tools::-webkit-scrollbar,
  .build-palette::-webkit-scrollbar {
    display: none;
  }

  .build-tools {
    bottom: 130px;
  }

  /* Elements on one row, the groups on the next. */
  .build-palette {
    bottom: 46px;
    flex-wrap: wrap;
    justify-content: space-between;
    row-gap: 2px;
    overflow: visible;
  }

  .build-palette .bd-sep {
    flex-basis: 100%;
    height: 0;
    margin: -1px 0;
  }

  /* The bonds share the groups' row, after a thin line. */
  .build-palette .bd-sep.bd-sep-v {
    flex: none;
    width: var(--hairline);
    height: auto;
    margin: 4px 2px;
  }

  .bd-label {
    display: none;
  }

  .build-palette .bd-grp,
  .build-palette .bd-bond,
  .build-palette .bd-el {
    flex: 1 1 0;
    min-width: 0;
    padding: 0;
  }

  .build-acts {
    left: var(--space-2);
    right: var(--space-2);
    bottom: var(--space-2);
    overflow-x: auto;
    scrollbar-width: none;
  }

  .build-tools .bd-sep,
  .build-palette .bd-sep {
    width: var(--hairline);
    height: auto;
    margin: 4px 3px;
  }

  .bd-btn {
    width: 32px;
    height: 30px;
  }

  .bd-el,
  .bd-grp,
  .bd-bond {
    width: auto;
    min-width: 34px;
    height: 30px;
    padding: 0 var(--space-1);
  }

  .build-empty {
    padding: var(--space-6);
    place-items: start center;
    padding-top: 30%;
  }

  .mol-app[data-build='2d'] .stage-tr {
    top: var(--space-2);
    bottom: auto;
  }

  /* 3D: the camera buttons keep the bottom right corner; the actions sit above them. */
  .mol-app[data-build='3d'] .build-acts {
    right: auto;
    bottom: 44px;
  }

  .lib-new.btn.sm {
    margin-left: auto;
  }

  .build-acts {
    gap: 0;
  }

  .build-acts .btn.sm {
    padding: 0 var(--space-2);
  }

  /* The readout keeps its hint on phones in Build 2D: a fixed second line, so nothing below moves. */
  .mol-app[data-tab='structure'][data-build='2d'] .mol-readout {
    flex-wrap: wrap;
    align-content: center;
    row-gap: 3px;
    height: calc(var(--mol-readout-h) + 30px);
  }

  /* No room for the note on a phone: the Auto H button above says it. */
  .mol-readout .ro-note {
    display: none;
  }

  .mol-app[data-tab='structure'][data-build='2d'] .mol-readout .ro-end {
    display: -webkit-box;
    flex-basis: 100%;
    margin-left: 0;
    white-space: normal;
    line-height: 1.35;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
}

/* Touch screens: no hover, so no tooltips left hanging on the bars. */
@media (hover: none) {
  .build-tools [data-tip]::after,
  .build-palette [data-tip]::after,
  .build-acts [data-tip]::after {
    content: none;
  }
}

/* ── The structure in the inspector (js/inspector-struct.js) ─────── */

.in-struct {
  position: relative;
  height: 210px;
  margin: var(--space-3) 0 var(--space-1);
  overflow: hidden;
  border: var(--hairline) solid var(--border);
  border-radius: var(--radius-md);
  background: var(--stage);
}

.in-struct-svg {
  display: block;
  width: 100%;
  height: 100%;
}

.in-struct.is-3d .mv-canvas {
  position: absolute;
  inset: 0;
  cursor: grab;
}

.in-struct.is-3d .mv-canvas:active {
  cursor: grabbing;
}

.in-struct-snap {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* ── A drawing's name and its library match ───────────────────────── */

.in-rename {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  min-height: 32px;
}

.in-rename .icon-btn {
  flex: none;
  color: var(--text-faint);
}

.in-rename .icon-btn:hover {
  color: var(--text);
}

.in-rename-input {
  flex: 1;
  min-width: 0;
  height: 32px;
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}

.in-match {
  margin: var(--space-1) 0 0;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.in-match.is-wait {
  color: var(--text-faint);
}

.in-link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent-text);
  font: inherit;
  text-decoration: underline;
  text-decoration-color: var(--accent-line);
  text-underline-offset: 2px;
  cursor: pointer;
}

.in-link:hover {
  text-decoration-color: currentColor;
}

/* ── Library rows carried onto the stage (js/build/libdrag.js) ────── */

.lib-row {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.lib-row.is-lifting {
  background: var(--surface-active);
}

.mol-app.lib-dragging,
.mol-app.lib-dragging * {
  cursor: grabbing !important;
}

.lib-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: var(--z-toast);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  max-width: 260px;
  padding: var(--space-1) var(--space-3) var(--space-1) var(--space-1);
  border-radius: var(--radius-md);
  background: var(--surface-raised, var(--surface));
  box-shadow: var(--shadow-popover);
  color: var(--text);
  pointer-events: none;
  will-change: transform;
}

.lib-ghost .lib-thumb {
  flex: none;
}

.lib-ghost-text {
  display: grid;
  min-width: 0;
  font-size: var(--text-sm);
  line-height: 1.3;
}

.lib-ghost-text b {
  overflow: hidden;
  font-weight: var(--weight-semibold);
  white-space: nowrap;
  text-overflow: ellipsis;
}

.lib-ghost-text span {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

/* The stage while a row is carried: a dashed frame, filled in when the row is over it. */
.mol-app.lib-dragging[data-tab='structure'] .mol-stage::after {
  content: 'Drop here to build with it';
  position: absolute;
  inset: var(--space-2);
  z-index: var(--z-panel);
  display: grid;
  place-items: center;
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius-lg, 10px);
  color: var(--text-muted);
  font-size: var(--text-md);
  font-weight: var(--weight-medium);
  pointer-events: none;
}

.mol-app.lib-dragging[data-tab='structure'] .mol-stage.is-lib-over::after {
  border-color: var(--accent);
  background: color-mix(in oklab, var(--accent) 12%, transparent);
  color: var(--accent-text);
}

.mol-app.lib-dragging .stage-note,
.mol-app.lib-dragging .build-empty {
  visibility: hidden;
}
