/* Floating panels. Panels open near their launch buttons and avoid the toolbar, top bar, zoom rail, and corner buttons. */
.panel {
  position: absolute;
  z-index: 30;
  right: 110px;
  top: 112px;
  width: 330px;
  max-height: min(74vh, 680px);
  overflow-y: auto;
  padding: 14px;
  border-radius: 22px;
  background: var(--glass);
  border: 1px solid rgba(0,0,0,.08);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px) scale(.985);
  transition: opacity 160ms ease, transform 160ms ease;
}
.panel.open { opacity: 1; pointer-events: auto; transform: translateY(0) scale(1); z-index: 40; }
#surfacePanel,
.image-panel {
  left: 22px;
  right: auto;
  top: auto;
  bottom: 112px;
  width: 340px;
  max-height: min(62vh, 560px);
}

.panel-title { font-weight: 700; font-size: 14px; margin-bottom: 4px; }
.panel-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.panel-subtitle { color: var(--muted); font-size: 12px; line-height: 1.25; }
.mini-close { width: 30px; height: 30px; padding: 0; font-size: 18px; line-height: 1; }
.panel-copy, .preview-note { margin: 0 0 12px; color: var(--muted); font-size: 12px; line-height: 1.35; }
.vector-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.vector-grid label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.vector-grid input { width: 100%; }
.panel-actions { display: flex; justify-content: flex-end; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.panel-toggles, .surface-section {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(0,0,0,.035);
}
.panel-toggles { display: flex; gap: 10px; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
.panel-toggles label { display: inline-flex; gap: 5px; align-items: center; }
.surface-section-title { color: var(--muted); font-size: 12px; font-weight: 700; margin-bottom: 8px; }
.segmented-control { display: inline-flex; gap: 3px; padding: 3px; border-radius: 999px; background: rgba(0,0,0,.055); }
.opacity-control { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.opacity-control input { flex: 1; min-width: 0; accent-color: var(--ink); }
.opacity-control span { min-width: 42px; text-align: right; color: var(--muted); font-weight: 700; }
.panel-wide-button { display: block; width: 100%; margin-top: 9px; background: rgba(255,255,255,.46); text-align: center; }
.image-upload-control { margin-top: 0; }
.image-actions-section { display: grid; gap: 8px; }
.help-panel {
  left: auto;
  right: 84px;
  top: auto;
  bottom: 112px;
  width: 300px;
  max-height: min(62vh, 520px);
  overflow-y: auto;
}
.shortcut-grid { display: grid; grid-template-columns: auto 1fr; gap: 8px 12px; align-items: center; color: var(--muted); font-size: 13px; }
kbd { display: inline-block; min-width: 26px; padding: 3px 8px; border-radius: 999px; background: #fbfaf7; border: 1px solid rgba(0,0,0,.08); color: var(--ink); font: inherit; font-size: 12px; text-align: center; }
.preview-panel,
.preview-warning-panel {
  left: auto;
  right: 84px;
  top: auto;
  bottom: 112px;
  width: 450px;
  max-height: min(62vh, 520px);
}
.preview-warning-panel { width: 370px; }
#preview3dCanvas { width: 100%; height: 300px; display: block; border-radius: 16px; background: #fffdf8; border: 1px solid rgba(0,0,0,.08); touch-action: none; }
.preview-note { margin-top: 10px; margin-bottom: 0; }
@media (max-width: 940px) {
  .panel { right: 12px; left: 12px; top: 92px; bottom: auto; width: auto; max-height: calc(100vh - 180px); }
  .help-panel, .preview-panel, .preview-warning-panel { left: 12px; right: 64px; top: auto; bottom: 108px; width: auto; max-height: calc(100vh - 220px); }
  #surfacePanel, .image-panel { left: 12px; right: 12px; top: auto; bottom: 108px; width: auto; max-height: calc(100vh - 220px); }
}
.edge-editor { padding-bottom: 12px; }
.edge-diagram {
  display: block;
  width: 100%;
  height: 170px;
  border-radius: 16px;
  background: rgba(255,255,255,.48);
  border: 1px solid rgba(0,0,0,.06);
  cursor: pointer;
}
.edge-diagram .cell-edge { stroke: rgba(31,31,31,.36); stroke-width: 2; fill: none; }
.edge-diagram .edge-hit { stroke: transparent; stroke-width: 22; fill: none; cursor: pointer; }
.edge-diagram .edge-arrow { stroke-width: 2.8; stroke-linecap: round; fill: none; }
.edge-diagram .edge-label { font: 700 13px system-ui, sans-serif; fill: rgba(31,31,31,.78); pointer-events: none; }
.edge-diagram .edge-open { stroke: rgba(31,31,31,.16); stroke-dasharray: 5 5; }
.edge-status { margin-top: 8px; color: var(--muted); font-size: 12px; line-height: 1.32; }
.edge-actions { display: flex; gap: 6px; margin-top: 8px; }
.edge-actions .ghost-button { flex: 1; background: rgba(255,255,255,.46); }


/* Layers panel */
.layers-panel {
  width: 380px;
}
.layer-panel-toolbar {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 10px;
}
.layer-list {
  display: grid;
  gap: 6px;
}
.layer-row {
  width: 100%;
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto 28px;
  gap: 8px;
  align-items: center;
  padding: 7px;
  border: 1px solid rgba(0,0,0,.075);
  border-radius: 15px;
  background: rgba(255,255,255,.50);
  text-align: left;
}
.layer-row:hover {
  background: rgba(255,255,255,.74);
}
.layer-row.active {
  border-color: rgba(31,31,31,.32);
  box-shadow: inset 0 0 0 1px rgba(31,31,31,.10);
}
.layer-row.hidden-layer {
  opacity: .56;
}
.layer-row.dragging {
  transform: scale(.99);
}
 
.layer-thumb canvas {
  display: block;
  width: 54px;
  height: 36px;
  border-radius: 10px;
  background: #fffdf8;
}
.layer-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 650;
}
.layer-percent {
  color: var(--muted);
  font-size: 12px;
}
.layer-eye {
  text-align: center;
  color: var(--ink);
  font-size: 12px;
}
.selected-layer-name {
  margin: 0 0 8px;
  font-weight: 700;
}
.layer-action-row {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  margin-top: 9px;
}
.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}


.layer-row {
  cursor: grab;
  position: relative;
}
.layer-row:active {
  cursor: grabbing;
}
.layer-row.drag-source {
  opacity: .22;
}
.layer-ghost {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto 28px;
  gap: 8px;
  align-items: center;
  padding: 7px;
  border-radius: 15px;
  background: rgba(255,255,255,.94);
  border: 1px solid rgba(31,31,31,.18);
  box-shadow: 0 22px 64px rgba(0,0,0,.20), 0 4px 16px rgba(0,0,0,.10);
  transform: translate3d(0,0,0) scale(1.015);
}
.layer-drop-placeholder {
  border-radius: 15px;
  border: 1px dashed rgba(31,31,31,.26);
  background: linear-gradient(180deg, rgba(31,31,31,.052), rgba(31,31,31,.032));
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.64), 0 8px 24px rgba(0,0,0,.05);
  transition: transform 120ms ease, height 120ms ease, opacity 120ms ease;
}
.layer-eye {
  border: 0;
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 999px;
  background: rgba(0,0,0,.045);
  cursor: pointer;
}
.layer-eye:hover {
  background: rgba(0,0,0,.09);
}
.image-layer-card {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 7px;
  border-radius: 15px;
  background: rgba(255,255,255,.46);
  border: 1px solid rgba(0,0,0,.075);
}
.image-layer-card.hidden-layer {
  opacity: .58;
}

.layer-ghost {
  opacity: .98;
  cursor: grabbing;
  will-change: transform;
}
.layer-ghost canvas {
  width: 54px;
  height: 36px;
}

.image-actions-section .opacity-control,
.selected-layer-section .opacity-control {
  position: relative;
  z-index: 3;
  pointer-events: auto;
}
.image-actions-section .opacity-control input,
.selected-layer-section .opacity-control input {
  pointer-events: auto;
}


#imageOpacityInput {
  position: relative;
  z-index: 5;
  pointer-events: auto;
  cursor: pointer;
  touch-action: none;
}


.preview-toggle-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
  user-select: none;
}
.preview-toggle-control input {
  width: 34px;
  height: 20px;
  accent-color: var(--ink);
  cursor: pointer;
}


/* Compact layer cards: visibility, delete, and opacity now live on each card. */
.layer-row {
  display: block;
  padding: 8px;
  cursor: grab;
}

.layer-card-main {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto 30px 30px;
  gap: 8px;
  align-items: center;
}

.layer-opacity-slider {
  display: block;
  width: 100%;
  margin: 7px 0 0;
  accent-color: var(--ink);
  cursor: pointer;
}

.layer-delete,
.layer-eye {
  border: 0;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 999px;
  background: rgba(0,0,0,.045);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.layer-delete:hover {
  background: #f3e8e5;
}

.layer-eye:hover {
  background: rgba(0,0,0,.09);
}

.eye-icon {
  width: 17px;
  height: 11px;
  border: 1.8px solid currentColor;
  border-radius: 100% 100% 100% 100% / 80% 80% 80% 80%;
  position: relative;
  display: inline-block;
}

.eye-icon::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 4.5px;
  height: 4.5px;
  background: currentColor;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}

.layer-eye.is-hidden .eye-icon {
  height: 7px;
  border-top-color: transparent;
  border-left-color: transparent;
  border-right-color: transparent;
  border-radius: 0 0 999px 999px;
  transform: translateY(-1px);
}

.layer-eye.is-hidden .eye-icon::after {
  display: none;
}

.layer-ghost {
  display: block;
}

.layer-ghost .layer-card-main {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto 30px 30px;
  gap: 8px;
  align-items: center;
}

.selected-layer-section,
.selected-layer-name,
.layer-action-row {
  display: none !important;
}

/* Keep the surface action row visible while scrolling long surface settings. */
#surfacePanel .panel-actions {
  position: sticky;
  bottom: -14px;
  z-index: 4;
  margin-left: -14px;
  margin-right: -14px;
  margin-bottom: -14px;
  padding: 10px 14px 14px;
  background: linear-gradient(180deg, rgba(255,253,248,0), rgba(255,253,248,.92) 18%, rgba(255,253,248,.98));
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  align-items: center;
}
#surfacePanel .sticky-grid-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  padding: 8px 9px;
  border-radius: 999px;
  background: rgba(255,255,255,.58);
  border: 1px solid rgba(0,0,0,.06);
  white-space: nowrap;
}
#surfacePanel .sticky-grid-toggle input { margin: 0; }
#surfacePanel #updateSurfaceButton {
  box-shadow: 0 8px 24px rgba(0,0,0,.14);
}

.preview-canvas-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px;
  gap: 10px;
  align-items: stretch;
}
.preview-canvas-wrap #preview3dCanvas {
  width: 100%;
}
.preview-twist-rail {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-width: 38px;
  padding: 8px 5px;
  border-radius: 999px;
  background: rgba(0,0,0,.045);
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  user-select: none;
}
.preview-twist-rail input {
  width: 28px;
  height: 206px;
  margin: 0;
  writing-mode: vertical-lr;
  direction: rtl;
  accent-color: var(--ink);
  cursor: pointer;
}
.preview-twist-rail button {
  width: 28px;
  height: 28px;
  padding: 0;
}
@media (max-width: 940px) {
  .preview-canvas-wrap { grid-template-columns: minmax(0, 1fr) 38px; }
  .preview-twist-rail input { height: 160px; }
}
.preview-twist-rail input:disabled,
.preview-twist-rail button:disabled {
  opacity: .38;
  cursor: not-allowed;
}

/* --- Panel close controls and mobile bottom-sheet behavior ------------------- */
.panel-title-row .panel-title { margin-bottom: 0; }
.mini-close {
  flex: 0 0 auto;
  background: rgba(0,0,0,.045);
  color: var(--muted);
}
.mini-close:hover { background: rgba(0,0,0,.09); color: var(--ink); }

/* Keep sticky Surface controls usable even when they wrap. Hide grid stays just
   before Update surface in the DOM and remains beside it in the sticky area. */
#surfacePanel .panel-actions > * { flex: 0 0 auto; }
#surfacePanel .panel-actions .sticky-grid-toggle { order: 20; }
#surfacePanel .panel-actions #updateSurfaceButton { order: 21; }

@media (max-width: 760px) {
  .panel,
  #surfacePanel,
  .image-panel,
  .help-panel,
  .preview-panel,
  .preview-warning-panel {
    left: calc(8px + env(safe-area-inset-left));
    right: calc(8px + env(safe-area-inset-right));
    top: calc(68px + env(safe-area-inset-top));
    bottom: calc(132px + env(safe-area-inset-bottom));
    width: auto;
    max-height: none;
    padding: 12px;
    border-radius: 22px;
  }
  .panel-title-row {
    position: sticky;
    top: -12px;
    z-index: 8;
    margin: -12px -12px 8px;
    padding: 12px 12px 8px;
    border-radius: 22px 22px 14px 14px;
    background: linear-gradient(180deg, rgba(255,253,248,.98), rgba(255,253,248,.90));
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }
  .panel-copy { font-size: 11px; }
  .vector-grid { grid-template-columns: 1fr 1fr; gap: 7px; }
  .preset-row,
  .layer-panel-toolbar,
  .edge-actions,
  .segmented-control {
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    flex-wrap: nowrap;
  }
  .preset-row::-webkit-scrollbar,
  .layer-panel-toolbar::-webkit-scrollbar,
  .edge-actions::-webkit-scrollbar,
  .segmented-control::-webkit-scrollbar { display: none; }
  .preset-row > *,
  .layer-panel-toolbar > *,
  .edge-actions > *,
  .segmented-control > * { flex: 0 0 auto; }

  #surfacePanel .panel-actions {
    bottom: -12px;
    margin-left: -12px;
    margin-right: -12px;
    margin-bottom: -12px;
    padding: 9px 12px calc(12px + env(safe-area-inset-bottom));
    justify-content: flex-start;
    gap: 6px;
  }
  #surfacePanel .panel-actions button { flex: 1 1 88px; padding-left: 9px; padding-right: 9px; }
  #surfacePanel .panel-actions .sticky-grid-toggle { flex: 1 1 112px; justify-content: center; }
  #surfacePanel .panel-actions #updateSurfaceButton { flex: 1.25 1 142px; }

  .preview-canvas-wrap {
    grid-template-columns: minmax(0, 1fr) 38px;
    gap: 8px;
  }
  #preview3dCanvas {
    height: clamp(190px, calc(100dvh - 350px), 300px);
  }
  .preview-twist-rail { min-width: 36px; padding: 6px 4px; }
  .preview-twist-rail input { height: clamp(128px, calc(100dvh - 420px), 180px); }

  .layer-card-main,
  .layer-ghost .layer-card-main {
    grid-template-columns: 46px minmax(0, 1fr) auto 30px 30px;
    gap: 6px;
  }
  .layer-thumb canvas,
  .layer-ghost canvas { width: 46px; height: 32px; }
  .image-layer-card { grid-template-columns: 46px minmax(0, 1fr) auto; }
}

@media (max-width: 430px) {
  .panel,
  #surfacePanel,
  .image-panel,
  .help-panel,
  .preview-panel,
  .preview-warning-panel {
    left: calc(6px + env(safe-area-inset-left));
    right: calc(6px + env(safe-area-inset-right));
  }
  .shortcut-grid { grid-template-columns: minmax(80px, auto) 1fr; gap: 7px 9px; font-size: 12px; }
  kbd { padding-left: 6px; padding-right: 6px; }
  .edge-diagram { height: 142px; }
  #preview3dCanvas { height: clamp(176px, calc(100dvh - 360px), 270px); }
}

@media (max-width: 380px) {
  .panel,
  #surfacePanel,
  .image-panel,
  .help-panel,
  .preview-panel,
  .preview-warning-panel {
    bottom: calc(174px + env(safe-area-inset-bottom));
  }
  #surfacePanel .panel-actions button { flex-basis: 80px; }
  #surfacePanel .panel-actions #updateSurfaceButton { flex-basis: 132px; }
  #surfacePanel .panel-actions .sticky-grid-toggle { flex-basis: 108px; }
}

/* Phone-first bottom sheets and new 3D/more panels. */
.compact-panel-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.display-mode-control { margin-top: 10px; flex-wrap: wrap; }
.surface-accuracy-text {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.38;
}
.preview-actions-section .preview-toggle-control { margin-top: 10px; }

@media (max-width: 760px) {
  body.compact-ui .panel,
  body.compact-ui #surfacePanel,
  body.compact-ui .image-panel,
  body.compact-ui .help-panel,
  body.compact-ui .preview-panel {
    left: calc(env(safe-area-inset-left));
    right: calc(env(safe-area-inset-right));
    top: auto;
    bottom: 0;
    width: auto;
    max-height: min(82dvh, 720px);
    border-radius: 26px 26px 0 0;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
  body.compact-ui .panel-title-row {
    position: sticky;
    top: 0;
    z-index: 4;
    margin: -16px -16px 10px;
    padding: 14px 16px 10px;
    background: rgba(255,255,255,.96);
    border-radius: 26px 26px 0 0;
  }
  .preview-canvas-wrap { min-height: 260px; }
}

/* Open panels and drag previews must stay above the bottom tool dock on compact screens. */
.panel.open { z-index: 40; }
.layer-ghost { z-index: 60; }
.layer-drop-placeholder { position: relative; z-index: 1; }
