  :root {
    --brand-blue: #295DE7;
    --brand-blue-hover: #1d4ed8;
    --bg: #F6F6F6;
    --card-bg: #ffffff;
    --text-primary: #333333;
    --text-secondary: #6c757d;
    --border-color: #dee2e6;
    --border-focus: #8cb1f3;
  }
  * { box-sizing: border-box; }
  html, body {
    height: 100%; margin: 0; background: var(--bg); color: var(--text-primary);
    font-family: 'Poppins', system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial;
    font-size: 16px;
    line-height: 1.5;
    overflow: hidden; 
    width: 100%;
  }
  
  #app-header {
    position: fixed; top: 0; left: 0; right: 0;
    height: 65px; background: var(--card-bg);
    border-bottom: 1px solid var(--border-color);
    z-index: 100; padding: 0 24px;
  }
  .header-content {
    max-width: 1200px; margin: 0 auto;
    height: 100%; display: flex;
    justify-content: space-between; align-items: center;
  }
  .logo { display: flex; align-items: center; gap: 12px; font-weight: 600; font-size: 20px; }
  .user-info { display: flex; align-items: center; gap: 16px; }
  .btn.btn-secondary { background: #f1f3f5; border-color: #f1f3f5; }
  .btn.btn-secondary:hover { background: #e9ecef; }
  .logo-img { height: 32px; width: auto; }
  .logo-img-auth { height: 60px; width: auto; }

.wrap {
    padding-top: 65px;
    height: 100dvh; /* This RESPECTS the browser's UI bars */
    display: grid; grid-template-rows: 1fr;
  }

  main {
    position: relative;
    height: 100%;
    overflow-y: auto;
  }

  .page-section {
    padding: 40px 20px;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    position: absolute;
    width: 100%;
    top: 0; left: 0;
    opacity: 1;
    transition: opacity 0.3s ease-in-out;
  }
  .hidden {
    opacity: 0;
    pointer-events: none;
    height: 0; 
    overflow: hidden;
    /* display: none !important; */
  }
  .d-none {
      display: none !important;
  }

  /* --- AUTH PAGE --- */
  .auth-container {
    max-width: 800px; margin: 0 auto; background: var(--card-bg);
    border: 1px solid var(--border-color); border-radius: 12px;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.07);
    display: flex; overflow: hidden;
    width: 100%;
    position: relative; /* Needed for positioning the fullscreen button */
  }
  .auth-info {
    flex-basis: 45%; background: #fcfdff; padding: 40px 32px;
    border-right: 1px solid var(--border-color);
  }
  .auth-info h3 { margin: 0 0 10px; font-size: 22px; color: var(--brand-blue); }
  .auth-info p { margin: 0; font-size: 14px; color: var(--text-secondary); line-height: 1.6; }
  .auth-form { flex-basis: 55%; padding: 40px 32px; }
  .auth-logo { text-align: center; margin-bottom: 24px; }
  .auth-form h2 { margin: 0 0 24px; font-size: 24px; font-weight: 600; text-align: center; }
  .row { display: flex; flex-direction: column; gap: 6px; margin-top: 16px; }
  .row label { font-weight: 500; font-size: 14px; }
  .row input {
    padding: 12px; background: #fff; color: var(--text-primary);
    border: 1px solid var(--border-color); border-radius: 8px; font-size: 16px;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
  }
  .row input:focus {
    outline: none; border-color: var(--brand-blue);
    box-shadow: 0 0 0 3px var(--border-focus);
  }
  .signin-actions { margin-top: 24px; }
  .forgot-password { text-align: center; margin-top: 16px; }
  .forgot-password a { font-size: 13px; color: var(--text-secondary); text-decoration: none; }
  .forgot-password a:hover { color: var(--brand-blue); text-decoration: underline;}
  .error-message {
    padding: 12px 15px; margin-top: 20px; background-color: #f8d7da;
    color: #721c24; border: 1px solid #f5c6cb; border-radius: 8px;
    font-size: 14px; text-align: center;
  }
  
  .fullscreen-btn-auth {
    position: absolute;
    top: 16px;
    right: 16px;
    padding: 8px 10px;
    z-index: 5;
    display: none; /* Hidden on mobile by default */
  }

  @media (min-width: 769px) {
    .fullscreen-btn-auth {
      display: inline-flex; /* Shown on screens wider than 768px */
    }
  }

  .btn {
    font: inherit; font-weight: 500; border-radius: 8px; padding: 12px 16px;
    cursor: pointer; border: 1px solid var(--border-color); background: var(--card-bg);
    color: var(--text-primary); transition: all 0.2s;
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    -webkit-tap-highlight-color: transparent;
  }
  .btn:hover { background: #f1f3f5; }
  .btn.brand { background: var(--brand-blue); color: #ffffff; border-color: var(--brand-blue); }
  .btn.brand:hover { background: var(--brand-blue-hover); border-color: var(--brand-blue-hover); }
  .btn.full-width { width: 100%; }

  /* --- FILE BROWSER --- */
  #file-browser { padding-top: 30px; padding-bottom: 30px; }
  .file-browser-container {
    max-width: 900px; margin: 0 auto;
    background: #fff; border-radius: 12px;
    padding: 24px; box-shadow: 0 6px 24px rgba(0,0,0,.07);
    width: 100%;
  }
  .file-browser-header {
    display: flex; justify-content: space-between; align-items: center;
    padding-bottom: 15px; gap: 15px;
  }
  .file-browser-header h2 { margin: 0; font-size: 22px; }
  .file-browser-actions { display: flex; gap: 10px; }
  .search-bar { position: relative; }
  .search-bar .bi-search { position: absolute; top: 13px; left: 14px; color: var(--text-secondary); }
  .search-bar input {
    width: 100%; padding: 12px 12px 12px 40px; font-family: inherit; font-size: 15px;
    border-radius: 8px; border: 1px solid var(--border-color);
  }
  .search-bar input:focus { outline: none; border-color: var(--brand-blue); box-shadow: 0 0 0 3px var(--border-focus); }
  .table-responsive { overflow-x: auto; }
  .file-table {
    width: 100%; border-collapse: collapse; margin-top: 20px; table-layout: fixed;
  }
  .file-table th, .file-table td {
    padding: 20px 16px; text-align: left; border-bottom: 1px solid var(--border-color); vertical-align: middle;
  }
  .file-table th { font-weight: 600; font-size: 14px; color: var(--text-secondary); padding-top: 0; }
  .file-table th:nth-child(1) { width: 70%; }
  .file-table th:nth-child(2) { width: 30%; }
  .file-table tbody tr { cursor: pointer; transition: background-color 0.2s; }
  .file-table tbody tr:hover { background-color: #f5f8ff; }
  .file-table .file-name-cell { font-weight: 500; display: flex; align-items: center; gap: 15px; }
  .file-table .file-name-cell .bi { font-size: 20px; color: var(--brand-blue); }
  .no-files td { text-align: center; padding: 40px; color: var(--text-secondary); }

  
  #viewer {
    padding: 0; display: flex; min-height: 0;
    height: 100%;
  }
  .viewer-container { display: flex; width: 100vw; height: 100%; }
  /* .viewer-container {
    display: grid;
    grid-template-columns: 90% 10%; 
    grid-template-rows: auto 1fr; 
    grid-template-areas: 
      "content sidebar";
    height: calc(100vh - 65px);
    width: 100vw;
  } */

  .content-area {
    /* grid-area: content;  */
    display: flex;
    flex:1;
    /* min-height: 50vh; */
    flex-direction: column; /* Stacks children vertically */
    height: 100%; /* IMPORTANT: Makes it fill the entire 80% grid cell height */
    overflow: hidden; /* Prevents scrollbars on the main area */
  }

  #canvas-wrapper {
      /* grid-area: canvas; */
      flex-grow: 1;
      order: 1;
      position: relative;
      overflow: hidden;
      /* min-height: 400px; */
      min-height: 70vh;
      /* height: 100%;
      width: 100%; */
  }

  #static-viewer-overlay {
    position: absolute;
    top: 15px;
    right: 15px;
    
    /* Layering: 1000 ensures it is above the loader and 3D scene */
    z-index: 1000; 
    
    /* DYNAMIC SIZING */
    width: 25%;        /* Takes up 20% of the canvas wrapper width */
    min-width: 400px;  /* Never shrinks smaller than 100px */
    max-width: 1000px;  /* Optional: prevents it from getting too large */
    height: auto;
    
    /* Interaction */
    pointer-events: none; /* Allows you to click "through" the image */
    border-radius: 4px;
    filter: drop-shadow(0px 2px 4px rgba(0,0,0,0.5));
}

  #carousel-container {
    flex-shrink: 0; 
    order: 2;
    overflow-x: hidden; 
    
    /* CRITICAL: Default to hidden, collapse space */
    display: none;
    opacity: 0;
    height: 0; /* Ensures the container collapses completely when hidden */
    padding-top: 0;
    transition: opacity 0.3s, height 0.3s, padding 0.3s; 
}

  /* 2. Show State: Applied by JavaScript (class="visible") */
  #carousel-container.visible {
      /* Restore visibility and space */
      display: flex; /* Or block, depending on final structure */
      opacity: 1;
      height: 150px; /* Allow height to expand vertically based on content */
      padding-top: 10px; /* Add vertical padding above the carousel */
      width: 100%;
  }
  /* #canvas-wrapper {
    flex-grow: 1;
    position: relative;
  } */
  #canvas { width: 100%; height: 100%; display: block; }
  #viewer-sidebar {
    /* grid-area: sidebar; */
    /* overflow: visible !important; */
    position: relative; 
    flex-basis: 280px; flex-shrink: 0;
    background: var(--card-bg); border-left: 1px solid var(--border-color);
    padding: 24px; z-index: 10;
    display: flex; flex-direction: column; gap: 15px;
    box-shadow: none; overflow-y: auto;
  }
  /* #viewer-sidebar {
    grid-area: sidebar; 
    
    background: var(--card-bg); 
    border-left: 1px solid var(--border-color);
    padding: 24px; 
    z-index: 10;
    
    display: flex; 
    flex-direction: column; 
    gap: 15px;
    
    box-shadow: none; 
    overflow-y: auto;
} */
  #viewer-sidebar h3 { margin: 0 0 10px 0; font-size: 18px; }
  .sidebar-btn { width: 100%; }
  a.btn { text-decoration: none; }
  .checkbox-label {
    display: flex; align-items: center; gap: 10px; padding: 8px 12px;
    cursor: pointer; user-select: none; font-size: 14px;
    border-radius: 8px; transition: background-color 0.2s;
  }
  .checkbox-label:hover { background-color: rgba(0,0,0,.05); }
  input[type="checkbox"] { opacity: 0; position: absolute; width: 0; height: 0; }
  .custom-checkbox {
    width: 20px; height: 20px; background: #fff;
    border: 2px solid var(--border-color); border-radius: 5px;
    position: relative; transition: background-color 0.2s, border-color 0.2s;
  }
  .custom-checkbox::after {
    content: ''; position: absolute; display: none; left: 6px; top: 2px;
    width: 5px; height: 11px; border: solid white;
    border-width: 0 2px 2px 0; transform: rotate(45deg);
  }
  input[type="checkbox"]:checked + .custom-checkbox {
    background-color: var(--brand-blue); border-color: var(--brand-blue);
  }
  input[type="checkbox"]:checked + .custom-checkbox::after { display: block; }
  
  .btn {
    font: inherit; font-weight: 500; border-radius: 8px; padding: 12px 16px;
    cursor: pointer; border: 1px solid var(--border-color); background: var(--card-bg);
    color: var(--text-primary); transition: all 0.2s;
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    -webkit-tap-highlight-color: transparent;
  }
  .btn:hover { background: #f1f3f5; }
  .btn.brand {
    background: var(--brand-blue); color: #ffffff; border-color: var(--brand-blue);
  }
  .btn.brand:hover { background: var(--brand-blue-hover); border-color: var(--brand-blue-hover); }

  #loader-overlay {
    position: absolute; inset: 0; background: rgba(246, 246, 246, 0.7);
    display: flex; justify-content: center; align-items: center;
    z-index: 20;
    transition: opacity 0.3s ease-in-out;
  }
   #loader-overlay.hidden {
    opacity: 0;
    pointer-events: none;
  }
  .spinner {
    width: 56px; height: 56px; border-radius: 50%;
    border: 6px solid #e9ecef;
    border-top-color: var(--brand-blue);
    animation: spin 1s ease-in-out infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  .measurement-label {
    color: #333333; background: rgba(255, 255, 255, 0.75);
    padding: 4px 8px; border-radius: 5px; font-size: 14px;
    font-weight: 500; border: 1px solid rgba(0,0,0,0.1);
    backdrop-filter: blur(2px); box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  }

  .section-slider {
    position: absolute;
    bottom: 20px;
    left: 50%; 
    transform: translateX(-50%);
    width: 60%;
    max-width: 500px;
    z-index: 10;
    cursor: ew-resize;
  }
  
  .axis-btn {
    width: 50px;
  }
  .axis-btn.active {
    background-color: var(--brand-blue);
    color: white;
    border-color: var(--brand-blue);
    pointer-events: none;
  }
  .no-scroll { overflow: hidden; }
  /* .hidden {
    display: none !important;
  } */

  #section-axis-controls {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: rgba(255, 255, 255, 0.75);
    padding: 10px;
    border-radius: 12px;
    backdrop-filter: blur(5px);
    border: 1px solid rgba(0,0,0,0.1);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  }
  
  .disclaimer-text {
    font-size: 12px;
    color: var(--text-secondary);
    text-align: center;
    font-size: x-small;
    margin-top: auto;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
  }

  /* --- CORRECTED MOBILE STYLES --- */
  @media (max-width: 500px) {
    .page-section { padding: 20px 15px; justify-content: flex-start; }
    #auth { padding-top: 40px; }
    .auth-container { display: block; }
    .auth-info { display: none; }
    .file-browser-header { flex-direction: column; align-items: stretch; text-align: center; }
    .viewer-container { flex-direction: column; }
    #canvas-wrapper { order: 1; }

    #viewer-sidebar {
      flex-basis: auto;
      border-left: none;
      border-top: 1px solid var(--border-color);
      order: 2;
      display: flex;
      flex-direction: column; /* Main components stack vertically */
      gap: 15px;
      padding: 15px;
    }

    .sidebar-controls-grid {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      gap: 10px;
      align-items: stretch; /* Fixes button height issue */
    }
    
    .sidebar-controls-grid > * {
      flex-basis: calc(50% - 8px); /* Creates 2x2 grid for buttons */
    }

    /* Styling for the label associated with the disabled/faded option */
    label.faded {
      color: #999; /* Light gray text */
      opacity: 0.6;
      cursor: not-allowed;
      /* Optional: Disable pointer events on the label itself */
      pointer-events: none; 
    }
    /* Ensure the disabled checkbox also looks dimmed */
    #toggle-image-panel:disabled {
      cursor: not-allowed;
    }

    /* Container for the tooltip */
    .tooltip-wrapper {
        position: relative;
        display: flex;
        width: 100%;
        /* align-items: center; */
    }

    /* The actual message box */
    .viewer-container.sidebar-controls-grid.tooltip-text {
        visibility: hidden;
        width: 200px;
        background-color: #333;
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 8px;
        position: absolute;
        z-index: 100;
        bottom: 125%; /* Position above the toggle */
        left: 50%;
        transform: translateX(-50%);
        opacity: 0;
        transition: opacity 0.3s;
        font-size: 12px;
        line-height: 1.4;
        pointer-events: none; /* Prevents the tooltip from interfering with clicks */
    }

    #static-viewer-overlay {
        top: 10px;      /* Move it closer to the edge */
        right: 10px;
        
        /* On mobile, let it take up more of the screen width */
        width: 60%;     
        
        /* CRITICAL: Lower the min-width so it can actually shrink */
        min-width: 150px; 
        
        /* Optional: Add a slight shadow to help it pop against the 3D scene */
    }

    /* Tooltip arrow */
    .tooltip-text::after {
        content: "";
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: #333 transparent transparent transparent;
    }

    /* Show the tooltip on hover */
    .tooltip-wrapper:hover .tooltip-text {
        visibility: visible;
        opacity: 1;
    }

    .disclaimer-text {
      flex-basis: 100%; /* Ensures disclaimer is full-width */
      text-align: center;
      margin-top: 10px;
      padding-top: 10px;
      font-size: x-small;
      border-top: 1px solid var(--border-color);
    }
  }

  @media (orientation: landscape) and (max-height: 500px) {
    .section-slider {
      bottom: 45px;
    }
    #auth.page-section { justify-content: center; padding: 0px 15px; }
    .auth-form { padding: 0px 10px; }
    .auth-logo, .auth-form h2 { margin-bottom: 15px; }
  }

  /* --- 1. Carousel Background --- */
  #image-carousel-track {
      background-color: rgba(54, 54, 54, 1);
      padding: 20px;
      display: flex; /* Helps align images in a row */
      /* overflow-x: auto;  */
      gap: 15px;
      /* position: relative; */

      flex-wrap: wrap;
      min-width: unset;
      width: 100%;
      height: fit-content;
      
  }

  .sidebar-controls-grid img {
    width: 100%;       /* Forces it to match the width of the sidebar column */
    height: auto;      /* Maintains aspect ratio */
    display: block;    /* Removes bottom whitespace */
    margin: 10px 0;    /* Optional: Adds space above and below the colormap */
    border-radius: 4px; /* Optional: Matches the aesthetic of your buttons */
}

  /* --- 2. Thumbnail Styles --- */
  .carousel-item-wrapper {
      position: relative; /* Essential for positioning the icon */
      /* width: 200px; */
      /* height: 150px; */
      cursor: pointer;
      overflow: hidden;
      transition: transform 0.3s ease; /* For smooth feedback on hover */

      /* CRITICAL FIX 3: Define a fixed size so it never shrinks */
      min-width: 150px; /* Set your desired minimum width */
      width: 150px; 
      
      /* CRITICAL FIX 4: Maintain Aspect Ratio (e.g., 4:3 ratio) */
      height: auto; /* Let height be determined by padding-bottom */
      aspect-ratio: 4 / 3; /* Use the aspect-ratio property for consistent scaling */
  }

  .carousel-thumbnail {
      width: 100%;
      height: 100%;
      object-fit: cover;
      display: block;
  }

  .zoom-icon-overlay {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.4); /* Semi-transparent dark overlay */
      color: white;
      font-size: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      opacity: 0; /* Hidden by default */
      transition: opacity 0.3s ease;
  }

  /* Show icon and slightly scale thumbnail on hover */
  .carousel-item-wrapper:hover .zoom-icon-overlay {
      opacity: 1;
  }
  .carousel-item-wrapper:hover {
      transform: scale(1.05);
  }

  /* --- 3. Full-Screen Hover/Modal Styles --- */
  #full-image-modal {
      position: fixed; /* Fixed position covers the entire viewport */
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
      background-color: rgba(0, 0, 0, 0.9); /* Very dark, transparent background */
      z-index: 1000; /* Ensures it sits above all other content */
      display: flex;
      align-items: center;
      justify-content: center;
  }
  #full-image-modal.hidden {
      display: none;
  }
  #modal-image {
      max-width: 90%;
      max-height: 90%;
      object-fit: contain; /* Ensures the whole image fits in the view */
  }

  #close-modal-btn {
    /* Positioning */
    position: absolute;
    top: 20px;
    right: 30px;

    /* Appearance */
    font-size: 28px; /* Larger icon for better visibility */
    line-height: 1; /* Ensures the 'X' sits neatly in the circle */
    font-weight: 300; /* Use a lighter weight for a modern look */
    
    /* Color Scheme */
    color: #fff; /* White 'X' */
    background: rgba(50, 50, 50, 0.7); /* Semi-transparent dark gray circle */
    
    /* Shape and Sizing */
    border: none;
    border-radius: 50%; /* Makes it a perfect circle */
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    
    /* Interaction */
    cursor: pointer;
    transition: background 0.2s ease;
  }

  /* Add a subtle hover effect */
  #close-modal-btn:hover {
      background: rgba(255, 255, 255, 0.9); /* Lighter gray/white on hover */
      color: #333; /* Dark text on hover */
  }