/* Global variables and base styles */
:root {
  --sidebar-width: 280px;
  --topbar-width: calc(100vw);
  --primary-bg: #121212;
  --secondary-bg: rgba(30, 30, 30, 0.9);
  --accent-color: #64ffda;
  --text-color: #e0e0e0;
  --border-color: #444;
  --hover-bg: rgba(100, 255, 218, 0.2);
}


html, body {
  margin: 0;
  padding: 0;
  font-family: 'Roboto', sans-serif;
  background-color: var(--primary-bg);
  color: var(--text-color);
  height: 100%;
  overflow: hidden;
}

/* Canvas and Shadows */
canvas {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
}


/* Top Bar */
#topBar {
  position: fixed;
  top: 0;
  left: var(--sidebar-width);
  width: calc(100vw - var(--sidebar-width));
  height: 50px;
  background: transparent;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  padding: 0 20px;
  z-index: 301;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.8);
  transition: left 0.3s ease, width 0.3s ease;
}
#topBar label {
  margin-right: 20px;
  font-size: 14px;
  display: flex;
  align-items: center;
}
#topBar input[type="checkbox"] {
  margin-right: 8px;
  accent-color: var(--accent-color);
}
#toggleSidebar {
  background: rgba(0, 0, 0, 0.3);
  border: none;
  color: var(--text-color);
  font-size: 24px;
  cursor: pointer;
  margin-right: 20px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* Sidebar */
#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 260px;
  background: transparent;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  padding: 10px;
  overflow-y: auto;
  transition: width 0.3s ease, padding 0.3s ease;
  z-index: 200;
  font-size: 12px;
}
#sidebar.collapsed {
  width: 0;
  padding: 0;
  overflow: hidden;
}

/* Accordion in Sidebar */
.accordion {
  width: 100%;
}
.accordion-item {
  border-bottom: 1px solid #333;
  margin-bottom: 10px;
}
.accordion-header {
  background: rgba(39, 39, 39, 0.5);
  color: var(--text-color);
  cursor: pointer;
  padding: 10px;
  font-size: 12px;
  text-align: left;
  border: none;
  outline: none;
  width: 100%;
  transition: background 0.3s, color 0.3s;
}
.accordion-header:hover,
.accordion-header.active {
  background: var(--accent-color);
  color: #000;
}
.accordion-content {
  background: rgba(30, 30, 30, 0.5);
  overflow: hidden;
  transition: max-height 0.3s ease;
  max-height: 0;
  padding: 0 10px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.accordion-content.open {
  padding-top: 5px;
  padding-bottom: 5px;
}

/* Control Groups */
.control-group {
  margin-bottom: 15px;
}
.control-group label {
  display: block;
  margin-bottom: 5px;
  font-size: 12px;
  color: #ccc;
}
.control-group input[type="range"],
.control-group input[type="number"],
.control-group select,
.control-group textarea {
  width: 100%;
  padding: 4px;
  background: rgba(51, 51, 51, 0.5);
  border: none;
  border-radius: 4px;
  color: #fff;
  margin-bottom: 3px;
  box-sizing: border-box;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  font-size: 12px;
}
.control-group input[type="checkbox"] {
  margin-right: 8px;
  accent-color: var(--accent-color);
}
hr {
  border: none;
  border-top: 1px solid #444;
  margin: 15px 0;
}
#sidebar::-webkit-scrollbar {
  width: 8px;
}
#sidebar::-webkit-scrollbar-track {
  background: #222;
}
#sidebar::-webkit-scrollbar-thumb {
  background-color: #555;
  border-radius: 4px;
  border: 2px solid #222;
}
#measurementsContent p {
  font-size: 12px;
}
.control-group button {
  background: rgba(0, 0, 0, 0.3);
  border: none;
  color: var(--text-color);
  padding: 6px 8px;
  font-size: 12px;
  cursor: pointer;
  border-radius: 4px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition: background 0.3s;
  margin-top: 3px;
}
.control-group button:hover {
  background: rgba(0, 0, 0, 0.5);
}

/* Secondary Sidebar */
.secondary-sidebar {
  position: fixed;
  top: 50px;
  right: 0;
  width: 280px;
  height: calc(100% - 50px);
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.3s ease;
  z-index: 250;
  padding: 10px;
  font-size: 12px;
}
.secondary-sidebar.open {
  transform: translateX(0);
}

/* Toolbox (Floating Tools Panel) */
#toolbox , #connectionCounter{
  position: fixed;
  background: var(--secondary-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  box-shadow: 2px 2px 6px rgba(0,0,0,0.5);
  z-index: 1000;
  user-select: none;
}
#toolbox-header, #connectionCounter-header {
  cursor: move;
  font-weight: bold;
  margin-bottom: 5px;
  padding: 3px;
  background: rgba(39, 39, 39, 0.8);
  border-bottom: 1px solid var(--border-color);
  color: var(--text-color);
}
#toolbox-content , #connectionCounter-content{
  display: grid;
  grid-template-columns: repeat(2, auto);
  grid-gap: 5px;
}
.tool-button {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  margin: 0 5px;
  color: var(--text-color);
  transition: color 0.2s;
}
.tool-button:hover {
  color: var(--accent-color);
}
.tool-button.active {
  color: var(--accent-color);
}
/* Red cell icon for 1 Numbered Cell Tool */
.red-cell-icon {
  width: 24px;
  height: 24px;
  background: red;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  font-weight: bold;
  font-size: 16px;
}

/* Custom Context Menu */
#customContextMenu {
  position: absolute;
  display: none;
  background: var(--secondary-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  z-index: 2000;
  padding: 5px;
  box-shadow: 2px 2px 6px rgba(0,0,0,0.5);
  font-family: sans-serif;
  color: var(--text-color);
}
#customContextMenu div {
  padding: 4px 8px;
  cursor: pointer;
  transition: background 0.2s;
}
#customContextMenu div:hover {
  background: var(--hover-bg);
}

/* Pseudo Terminal Window */
#terminalWindow {
  position: fixed;
  bottom: 20px;
  left: 20px;
  width: 400px;
  height: 250px;
  background: #000;
  color: #0f0;
  border: 1px solid #444;
  border-radius: 4px;
  display: none;
  z-index: 3000;
  font-family: monospace;
}
#terminalHeader {
  cursor: move;
  background: #222;
  padding: 5px;
  font-weight: bold;
  color: #0f0;
}
#terminalContent {
  padding: 10px;
  overflow-y: auto;
  height: calc(100% - 30px);
  font-size: 14px;
}

 .color-icon {
      width: 100%;
      height: 100%;
      border-radius: 4px;
      color: #000;
      font-weight: bold;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    /* Color Selection Panel (draggable) */
    #colorSelector {
      position: fixed;
      top: 150px;
      left: 50%;
      transform: translateX(-50%);
      width: 250px;
      padding: 10px;
      background: #f5f5f5;
      border: 1px solid #ccc;
      border-radius: 4px;
      z-index: 1500;
      display: none;
      opacity: 0;
      transition: opacity 0.5s;
    }
    #colorSelector.show {
      display: block;
      opacity: 1;
    }
    #colorSelector h4 {
      margin: 0 0 5px;
      font-size: 14px;
      text-align: center;
    }
    #colorGrid {
      display: grid;
      grid-template-columns: repeat(5, 40px);
      grid-gap: 5px;
      justify-content: center;
    }
    #colorGrid button {
      width: 40px;
      height: 40px;
      border: none;
      cursor: pointer;
      font-size: 16px;
      font-weight: bold;
      color: #000;
      border-radius: 4px;
    }
    #colorGrid button.active {
      outline: 3px solid #000;
    }
    /* Canvas and Shadows */
    canvas {
      display: block;
    }
    /* Styles for the 3D preview panel - fixed size and draggable */
    #preview3d {
      position: fixed;
      top: 150px;
      left: 100px;
      width: 400px;
      height: 300px;
      background: rgba(0, 0, 0, 0.8);
      z-index: 2000;
      display: none;
      opacity: 0;
      transition: opacity 0.5s, all 0.5s ease;
      border: 1px solid #fff;
      border-radius: 4px;
      overflow: hidden;
    }
    #preview3d.show {
      display: block;
      opacity: 1;
    }
    /* Header positioned on top so it can be dragged */
    #preview3dHeader {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 30px;
      background: #444;
      color: #fff;
      padding: 5px;
      cursor: move;
      user-select: none;
      border-top-left-radius: 4px;
      border-top-right-radius: 4px;
      font-size: 14px;
      z-index: 2100;
      box-sizing: border-box;
    }
    /* Close button for the 3D preview panel */
    #close3dPreview {
      position: absolute;
      top: 5px;
      right: 5px;
      z-index: 2200;
      background: #fff;
      border: none;
      padding: 2px 6px;
      cursor: pointer;
      font-size: 12px;
      border-radius: 3px;
    }
    /* The Three.js container sits below the header */
    #threejs-container {
      position: absolute;
      top: 30px;
      left: 0;
      width: 100%;
      bottom: 0;
      z-index: 1;
    }
    /* Top Bar */
    
    /* Terminal Window */
    #terminalWindow {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 400px;
      height: 200px;
      background: #222;
      color: #0f0;
      font-family: monospace;
      z-index: 1500;
    }
    #terminalHeader {
      background: #444;
      padding: 5px;
      cursor: move;
    }
    #terminalContent {
      padding: 5px;
      overflow-y: auto;
      height: calc(100% - 30px);
    }

    /* Fade transition for the color selector panel */
    #colorSelector {
      transition: opacity 0.5s ease;
    }
    #colorSelector:not(.show) {
      opacity: 0;
    }
    #colorSelector.show {
      opacity: 1;
    }
    /* Show a red border around the active tool */
    .tool-button.active {
      border: 2px solid red;
    }
    /* Custom cursor element for paintbrush and smartbrush modes */
    #customCursor {
      position: absolute;
      pointer-events: none;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      display: none;
      opacity: 0.5;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      color: black;
      border: 1px solid #00000055;
    }
    /* Hide the default cursor on the canvas when needed */
    #gridCanvas.paint-cursor {
      cursor: none;
    }

    
       /*— toolbar shell —*/
    #tool-panel-container{
      position:fixed;top:0;left:0;right:0;height:400px;
      background:#f0f0f0;border-bottom:1px solid #ccc;
      padding:10px;box-sizing:border-box;overflow-x:auto;
      z-index:1000;
      background-color:transparent;
    }

    /*— grid layout —*/
    #tool-panel-container .grid{
      display:grid;
      grid-template-columns:repeat(auto-fill,minmax(80px,1fr));
      grid-auto-rows:120px;          /* equal rows for tidy layout */
      gap:8px;height:100%;align-items:center;
      background-color:transparent;
    }

    /*— button base —*/
    .tool-item{
      display:flex;flex-direction:column;align-items:center;
      justify-content:center;cursor:pointer;padding:6px;
      border-radius:6px;transition:background .2s, box-shadow .2s;user-select:none;
      background:#fff;border:2px solid transparent;
      background-color:green;
    }
    .tool-item:hover{background:#e0ffe0;color:black}

    /*— active/toggled state —*/
    .tool-item.active{
      background:green;
      border-color:red;
      box-shadow:0 0 8px rgba(255,0,0,0.8);
    }

    /*— selected tool (exclusive) —*/
    .tool-item.selected{
      background:#fff6d6;
      border-color:#ff9800;
      box-shadow:0 0 8px rgba(255,152,0,0.8);
    }

    /*— icon + label —*/
    .tool-item .icon{font-size:20px;margin-bottom:4px;}

    /* reserve space for 2 lines (≈ 32 px) even if only one is used */
    .tool-item .label{
      font-size:12px;text-align:center;margin-bottom:4px;
      min-height:32px;               /* two 16-px lines */
      line-height:16px;
      display:flex;justify-content:center;align-items:center;
      white-space:pre-line;          /* allow \n to create breaks */
    }

    .tool-item .slider{width:100%;}


#stlViewerWindow {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 400px;
  height: 300px;


  display: flex;
  flex-direction: column;
  background: var(--secondary-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.5);
  z-index: 1000;
   display:none;
   overflow: visible;      /* allow resizers to sit outside if needed */
  min-width: 100px;       /* prevent it collapsing too small */
  min-height: 100px;

}

#stlViewerWindow-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 8px;
  background: rgba(39, 39, 39, 0.8);
  border-bottom: 1px solid var(--border-color);
  color: var(--text-color);
  font-weight: bold;
  cursor: move;
  user-select: none;
  flex-shrink: 0;
    display:none;
}

/* style the control buttons to match your other windows */
#stlViewerWindow-header .window-controls button {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 14px;
  margin-left: 6px;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 2px;
  transition: background 0.2s;

}
#stlViewerWindow-header .window-controls button:hover {
  background: var(--hover-bg);
}

#viewerContainer {
  flex: 1;
  position: relative;
  overflow: hidden;
   display:none;
}


/* Generic resizer handle */
#stlViewerWindow .resizer {
  position: absolute;
  width: 12px;
  height: 12px;
  background: transparent;
  z-index: 1001;       /* above the window but below controls */
}

/* Corner positions & cursors */
#stlViewerWindow .resizer.top-left {
  top: -6px; left: -6px;
  cursor: nwse-resize;
}
#stlViewerWindow .resizer.top-right {
  top: -6px; right: -6px;
  cursor: nesw-resize;
}
#stlViewerWindow .resizer.bottom-left {
  bottom: -6px; left: -6px;
  cursor: nesw-resize;
}
#stlViewerWindow .resizer.bottom-right {
  bottom: -6px; right: -6px;
  cursor: nwse-resize;
}

/* Nuke the mystery “bottom border” for good */
#tool-panel-container,
#tool-panel-container.slide-panel {
  border-bottom: none !important;      /* no actual border */
  /* If the faint line is really the shadow’s bottom feather,
     keep the shadow but lift it 2 px so it doesn’t paint the floor: */
  box-shadow: 2px 0 8px -2px rgba(0,0,0,.3);
}

.wb-max {
  display: none !important;   /* removes it from view but keeps layout space */
  pointer-events: none !important; /* makes sure clicks pass through */
  /* or */
  opacity: 0 !important;           /* makes it fully transparent */
}


#envStatusContainer {
  display: flex;
  align-items: center;
  padding: 4px 10px;
  font-family: Roboto, sans-serif;
  font-size: 13px;
  gap: 8px;
}
#envStatusLabel {
  flex-shrink: 0;
}
#envStatusBarBg {
  flex: 1;
  height: 8px;
  background: #ddd;
  border-radius: 4px;
  overflow: hidden;
}
#envStatusBarFill {
  width: 0%;
  height: 100%;
  background: #4caf50;
  transition: width 0.3s ease;
}
#envStatusPercent {
  flex-shrink: 0;
  width: 32px;
  text-align: right;
}

