.builder-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 24px;
  background: var(--color-white);
  border-bottom: 1px solid #eee;
  min-height: 56px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.03);
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 120;
}

.builder-header .builder-back-btn,
.builder-header .builder-save-btn,
.builder-header .builder-preview-btn,
.builder-header .builder-menu-btn {
  background: transparent;
  border: none;
  cursor: pointer;
}

.builder-header .layout-info {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
}

.builder-header .layout-name {
  font-size: 1.25rem;
  font-weight: 500;
  outline: none;
}

.builder-header .edit-name-icon {
  margin-left: 6px;
  cursor: pointer;
}

.builder-header .layout-name-input {
  font-size: 1.25rem;
  padding: 4px 8px;
  flex: 1;
}


.builder-header .page-link {
  color: var(--color-primary);
}

.builder-header .page-select {
  padding: 4px 8px;
}

.builder-sidebar {
  position: fixed;
  top: 56px;
  bottom: 0;
  left: 0;
  width: 64px;
  padding: 20px 16px 0;
  background: none;
  box-shadow: none;
  z-index: 100;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  overflow-x: auto;
  overflow-y: hidden;

  .builder-panel {
    position: fixed;
    top: 56px;
    left: 96px;
    width: 240px;
    height: calc(100% - 56px);
    background: var(--color-white);
    box-shadow: var(--shadow-card);
    padding: 16px;
    display: none;
    flex-direction: column;
    gap: 12px;

    .collapse-btn {
      align-self: flex-end;
      background: transparent;
      border: none;
      cursor: pointer;
    }

    .text-choice {
      width: 100%;
      padding: 8px;
      background: var(--color-light-gray, #f0f0f0);
      border: none;
      cursor: pointer;
      text-align: left;

      .preview.heading { font-size: 1.5rem; font-weight: 700; }
      .preview.subheading { font-size: 1.25rem; font-weight: 600; }
      .preview.body { font-size: 1rem; }
    }
  }

    .builder-sidebar-nav {
       width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 8px;
      
      .drag-widget-icon {
        width: 48px;
        height: 48px;
        padding: 4px;
        background: var(--color-white);
        border-radius: 50%;
        box-shadow: var(--shadow-card);
        cursor: grab;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        position: relative;
        transition: transform 0.2s, box-shadow 0.2s;

      &:hover {
        border: 1px solid var(--gradient-primary);
        box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        transform: scale(1.05);

          img {
            transform: scale(1.1);
          }

          .label {
            opacity: 1;
            transform: translateY(-50%) translateX(8px) scale(1);
          }
      }

      img,
      .icon {
        width: 24px;
        height: 24px;
        transition: transform 0.2s ease;
      }

      .label {
        position: absolute;
        left: 100%;
        top: 50%;
        transform: translateY(-50%) translateX(0) scale(0.8);
        background: var(--color-white);
        padding: 4px 8px;
        border-radius: 4px;
        box-shadow: var(--shadow-card);
        font-size: 12px;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease, transform 0.2s ease;
      }
    }
  }
}

.canvas-item .widget-remove {
  position: absolute;
  top: 2px;
  left: 2px;
  background: transparent;
  border: none;
  padding: 2px;
  cursor: pointer;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.canvas-item .widget-edit {
  display: none;
}

.canvas-item .widget-menu {
  position: absolute;
  top: 2px;
  right: 2px;
  background: transparent;
  border: none;
  padding: 2px;
  cursor: pointer;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}


.canvas-item:hover .widget-menu,
.canvas-item:hover .widget-remove {
  opacity: 0;
  pointer-events: none;
}

.widget-options-menu {
  position: fixed;
  top: 0;
  left: 0;
  background: var(--color-white);
  border-radius: 9px;
  box-shadow: var(--shadow-card);
  font-family: var(--font-body);
  display: none;
  z-index: 1000;
}

.builder-options-menu {
  position: fixed;
  background: var(--color-white);
  border-radius: 9px;
  box-shadow: var(--shadow-card);
  font-family: var(--font-body);
  display: none;
  z-index: 1000;
}

.builder-options-menu button,
.builder-options-menu label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: transparent;
  border: none;
  cursor: pointer;
  width: 100%;
  text-align: left;
  font-family: var(--font-body);
}

.builder-options-menu label {
  cursor: pointer;
}

.builder-options-menu button:hover,
.builder-options-menu label:hover {
  background: var(--color-light-gray, #f0f0f0);
}

.widget-options-menu button {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 9px;
  background: transparent;
  border: none;
  cursor: pointer;
  width: 100%;
  text-align: left;
  font-family: var(--font-body);
}

.widget-options-menu button:hover {
  background: var(--color-light-gray, #f0f0f0);
}

.widget-action-bar {
  position: absolute;
  background: var(--color-white);
  border-radius: 9px;
  box-shadow: var(--shadow-card);
  display: flex;
  font-family: var(--font-body);
  gap: 4px;
  padding: 2px;
  z-index: 1000;
}

.widget-action-bar button {
  background: transparent;
  border: none;
  padding: 2px;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.widget-code-editor {
  position: fixed;
  top: 0;
  width: 280px;
  background: white;
  border: 1px solid #ccc;
  padding: 8px;
  z-index: 1000;
  display: none;
}

.widget-code-editor.right {
  left: 0;
}

.widget-code-editor.left {
  left: 0;
}

.widget-code-editor textarea {
  width: 100%;
  height: 60px;
  margin-bottom: 4px;
  font-family: var(--font-mono);
}


.widget-code-editor .editor-actions {
  text-align: right;
}

.widget-code-editor .media-btn {
  margin-right: auto;
}


.canvas-item .widget-remove .icon {
  width: 16px;
  height: 16px;
}

// Highlight widget wrapper on hover using the active user's color
.builder-mode .canvas-item {
  border: 2px solid transparent;
  transition: border-color 0.2s ease;
}

.builder-mode .canvas-item:hover,
.builder-mode .canvas-item.selected {
  border-color: var(--user-color);
}

// Hide native resize handles when using the bounding box
.builder-mode .canvas-item > .ui-resizable-handle {
  display: none;
}

// Hide drag placeholder to avoid showing an old position
.builder-mode .canvas-placeholder {
  display: none !important;
}


body.builder-mode #content {
  margin-left: 57px;
  height: calc(100vh - 79px);
  overflow-y: auto;
  padding-top: 0; // toolbar sits inside the content area
  box-sizing: border-box;
}

body.builder-mode.panel-open #content {
  margin-left: 336px;
  transform: scale(0.9);
  transform-origin: top left;
}

body.builder-mode.panel-open .builder-panel {
  display: flex;
}

body.builder-mode.panel-open #builderGrid {
  transform: scale(0.9);
  transform-origin: top left;
  margin-top: 56px;
  height: calc(100% - 56px);
  min-height: calc(100% - 56px);
}

body.builder-mode #builderGrid {
  margin-top: 56px;
  height: calc(100% - 56px);
  min-height: calc(100% - 56px);
}

// Ensure themed widget content matches its wrapper dimensions
body.builder-mode .canvas-item-content.builder-themed {
  width: 100%;
  height: 100%;
}

body.builder-mode #top-header,
body.builder-mode #main-header {
  display: none;
}

body.preview-mode .builder-sidebar,
body.preview-mode .widget-code-editor,
body.preview-mode .widget-options-menu,
body.preview-mode .builder-options-menu,
body.preview-mode .canvas-item .widget-remove,
body.preview-mode .canvas-item .widget-edit,
body.preview-mode .canvas-item .widget-menu {
  display: none !important;
}

body.preview-mode .builder-header .builder-save-btn,
body.preview-mode .builder-header .layout-info {
  display: none;
}

body.preview-mode #builderGrid {
  pointer-events: none;
  margin-top: 56px;
  height: calc(100% - 56px);
  min-height: calc(100% - 56px);
}

body.preview-mode #content {
  margin-left: 0;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--color-white);
  border-bottom: 1px solid #eee;
  padding: 6px 12px;
  position: sticky;
  top: 0;
  z-index: 60;
}

body.preview-mobile {
  --preview-width: 400px;
}

body.preview-tablet {
  --preview-width: 800px;
}

body.preview-desktop {
  --preview-width: 1200px;
}

body.preview-mobile #content,
body.preview-tablet #content,
body.preview-desktop #content {
  margin-left: auto;
  margin-right: auto;
  max-width: none;
}

.canvas-item {
  pointer-events: auto;
  user-select: none;
}

.canvas-item.locked {
  pointer-events: auto;
  user-select: text;
}

.canvas-item[gs-locked="true"] > .ui-resizable-handle {
  display: none;
}

.canvas-item[gs-locked="true"]::after {
  content: '';
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  background: url('/assets/icons/lock.svg') no-repeat center/contain;
  pointer-events: none;
  z-index: 100;
}

.canvas-item[data-global="true"]::before {
  content: '';
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  background: url('/assets/icons/globe.svg') no-repeat center/contain;
  pointer-events: none;
  z-index: 100;
}

body.builder-mode .canvas-item[data-global="true"] {
  opacity: 0.6;
  pointer-events: none;
}

body.builder-mode[data-active-layer="0"] .canvas-item[data-global="true"] {
  opacity: 1;
  pointer-events: auto;
}

body.builder-mode .canvas-item.inactive-layer {
  opacity: 0.6;
  position: relative;
}

body.builder-mode .canvas-item.inactive-layer::after {
  content: 'change layer to edit this widget';
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.6);
  color: var(--color-text);
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

body.builder-mode .canvas-item.inactive-layer:hover::after {
  opacity: 1;
}

.builder-version {
  position: fixed;
  left: 0;
  bottom: 32px;
  padding: 2px 4px;
  font-size: 10px;
  color: var(--color-text);
  background: rgba(255, 255, 255, 0.6);
  pointer-events: none;
  z-index: 50;
}

.layout-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  gap: 4px;
  padding: 4px 8px;
  background: var(--color-white);
  border-top: 1px solid #eee;
  z-index: 60;

  button {
    background: transparent;
    border: none;
    padding: 4px 8px;
    cursor: pointer;
  }

  .active {
    font-weight: 600;
  }

}

.bounding-box {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 1px dashed var(--user-color, #333);
  pointer-events: none;
  box-sizing: border-box;
  z-index: 1000;
  .bbox-handle {
    position: absolute;
    width: 8px;
    height: 8px;
    background: var(--user-color, #333);
    border-radius: 50%;
    pointer-events: all;
    transform: scale(var(--inv-scale));
  }
  .nw {
    top: -4px;
    left: -4px;
    transform: translate(-50%, -50%) scale(var(--inv-scale));
    cursor: nwse-resize;
  }
  .n {
    top: -4px;
    left: 50%;
    transform: translateX(-50%) scale(var(--inv-scale));
    cursor: ns-resize;
  }
  .ne {
    top: -4px;
    right: -4px;
    transform: translate(50%, -50%) scale(var(--inv-scale));
    cursor: nesw-resize;
  }
  .e {
    top: 50%;
    right: -4px;
    transform: translateY(-50%) scale(var(--inv-scale));
    cursor: ew-resize;
  }
  .se {
    bottom: -4px;
    right: -4px;
    transform: translate(50%, 50%) scale(var(--inv-scale));
    cursor: nwse-resize;
  }
  .s {
    bottom: -4px;
    left: 50%;
    transform: translateX(-50%) scale(var(--inv-scale));
    cursor: ns-resize;
  }
  .sw {
    bottom: -4px;
    left: -4px;
    transform: translate(-50%, 50%) scale(var(--inv-scale));
    cursor: nesw-resize;
  }
  .w {
    top: 50%;
    left: -4px;
    transform: translateY(-50%) scale(var(--inv-scale));
    cursor: ew-resize;
  }
}

.bounding-box.disabled {
  display: none;
}

.selection-box {
  position: absolute;
  border: 1px dashed var(--user-color, #0aa);
  transform-origin: 0 0;
}

.selection-box::before {
  content: '';
  position: absolute;
  inset: -4px;
  border: 2px solid currentColor;
  transform: scale(var(--inv-scale));
  pointer-events: none;
}

.hit-layer {
  pointer-events: auto;
}

.canvas-item .editable {
  cursor: text;
}

.canvas-item:not(.editing) .hit-layer {
  cursor: move;
}

.canvas-item.editing {
  z-index: 9999;
  cursor: text;
}

.canvas-item.selected .hit-layer,
.canvas-item.editing .hit-layer {
  pointer-events: none;
  cursor: text;
}

.builder-mode .canvas-item.selected,
.builder-mode .canvas-item.editing {
  -webkit-user-select: text;
     -moz-user-select: text;
          user-select: text;
}
