/* =================================================================== */
/*    PARTIAL: PRODUCT VISUALIZER                                      */
/*    Contains: Fullscreen studio modal, Canvas container, Toolbars,   */
/*    Laser animations, and Mobile studio stacking logic.              */
/* =================================================================== */

/* --- Product Visualizer Modal --- */
.visualizer-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10000;
  background-color: hsla(0, 0%, 0%, 0.85);
  backdrop-filter: blur(10px);
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  /* FORCE REMOVE EXTERNAL MARGINS */
  padding: 0 !important;
}

.visualizer-container.active {
  opacity: 1;
  visibility: visible;
}

.visualizer-modal {
  background: var(--eerie-black2);
  border-left: 1px solid var(--jet);
  border-right: 1px solid var(--jet);
  border-radius: 0; /* Full screen look */
  width: 100vw; 
  height: 100vh; 
  max-width: 100vw; 
  max-height: 100vh;
  display: flex;
  flex-direction: column;
  box-shadow: none;
  opacity: 0;
  transform: none; 
  transition: opacity 0.3s ease;
  align-items: stretch;
  /* SURGICAL FIX: Prevent modal from ever expanding beyond viewport */
  overflow: hidden !important; 
}

.visualizer-container {
    padding: 0 !important; /* Force remove gaps from the fixed overlay */
}

.visualizer-container.active .visualizer-modal {
  opacity: 1;
}

.visualizer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  border-bottom: 1px solid var(--jet);
  flex-shrink: 0;
  background: var(--eerie-black1);
  height: 60px;
}

.visualizer-header .modal-close-btn {
    position: static; 
    transform: none;
}

.visualizer-body {
  display: grid;
  width: 100%; 
  /* Shrink sidebar slightly more (320px) to give canvas absolute maximum room */
  grid-template-columns: 1fr 320px; 
  flex-grow: 1;
  /* SURGICAL FIX: min-height 0 allows the flex-child to shrink correctly inside the modal */
  min-height: 0; 
  overflow: hidden;
  position: relative;
}

.visualizer-canvas-container {
    position: relative;
    width: 100%;
    height: 100%;
    background-color: #1a1a1a; /* Deeper black for focus */
    display: flex;
    justify-content: center; 
    align-items: center;     
    overflow: hidden;
    padding: 0;
    margin: 0;
}

.visualizer-disclaimer {
    width: 100%;
    font-size: 10px;
    color: var(--light-gray70);
    text-align: center;
    margin-bottom: 0;
}

.visualizer-canvas-container {
  flex-grow: 1; 
  width: 100%;  
  height: 100%; 
  padding: 5px; 
  background-color: var(--onyx);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  
  /* FLUIDITY FIXES */
  overflow: hidden !important; /* Hide clunky scrollbars */
  cursor: grab;                /* Show the 'hand' icon */
}

.visualizer-canvas-container:active {
    cursor: grabbing;          /* Closes the 'hand' when clicking */
}

/* Ensure Fabric.js internal wrapper respects the flex centering */
.canvas-container {
    margin: auto !important;
    box-shadow: var(--shadow4);
}

/* Info Panel: Floating on Desktop, Block on Mobile */
.visualizer-info-panel {
  padding: 10px 20px;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(5px);
  border-top: 1px solid var(--jet);
  z-index: 10;
}

/* CONSOLIDATED DESKTOP OVERRIDES: Brute-Force Edge-to-Edge Workspace */
@media (min-width: 901px) {
    /* 1. Force backdrop to be a simple pass-through layer */
    .visualizer-container {
        display: block !important; 
        padding: 0 !important;
        margin: 0 !important;
        position: fixed !important;
        inset: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        z-index: 10000 !important;
        max-width: none !important;
    }

    /* 2. Force the Modal to span the absolute full window */
    .visualizer-modal {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        margin: 0 !important;
        border: none !important;
        border-radius: 0 !important;
        transform: none !important; 
        display: flex !important;
        flex-direction: column !important;
        background: #000 !important;
        overflow: hidden !important; 
    }

    /* 3. The Header: Guaranteed 100% Width */
    .visualizer-header {
        height: 60px !important;
        width: 100% !important;
        flex-shrink: 0;
        background: var(--eerie-black1);
        border-bottom: 1px solid var(--jet);
        padding: 0 25px;
    }

    /* 4. The Body: Edge-to-Edge Grid (Canvas + Toolbar) */
    .visualizer-body {
        display: grid !important; 
        grid-template-columns: 1fr 340px !important; 
        width: 100vw !important;
        max-width: 100vw !important;
        height: calc(100vh - 60px) !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
        min-height: 0;
    }

    /* 5. The Canvas Area: Takes all remaining space on the left */
    .visualizer-canvas-container {
        flex: 1 !important; 
        height: 100% !important;
        background-color: #080808 !important;
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
        padding: 60px !important; 
        position: relative !important;
        min-width: 0; 
    }

    /* 6. The Toolbar: Locked to the far right */
    .visualizer-toolbar {
        width: 340px !important;
        min-width: 340px !important;
        height: 100% !important;
        background: var(--eerie-black2) !important;
        border-left: 1px solid var(--jet) !important;
        padding: 25px !important;
        overflow-y: auto !important;
        flex-shrink: 0; 
    }

    /* 7. Floating Info Overlay (Gains Vertical Space) */
    .visualizer-info-panel {
        position: absolute !important;
        bottom: 30px !important;
        left: 30px !important;
        width: auto !important;
        max-width: 400px;
        background: rgba(0, 0, 0, 0.4) !important;
        backdrop-filter: blur(4px) !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        border-radius: 8px !important;
        padding: 10px 20px !important;
        z-index: 100;
        pointer-events: none;
    }

    .visualizer-disclaimer {
        text-align: left;
        font-size: 11px;
        opacity: 0.8;
        white-space: normal !important;
    }

    /* Surgical Fix: Consolidated tool-group styling inside Desktop query */
    .visualizer-toolbar .tool-group {
        background: rgba(0, 0, 0, 0.2);
        padding: 12px;
        border-radius: 10px;
        border: 1px solid var(--jet);
    }
}

/* --- UPDATED: VISUALIZER TOOLBAR (Fixed Clipping Fix) --- */
.visualizer-toolbar {
  padding: 20px; 
  border-left: 1px solid var(--jet);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 15px; 
  background: var(--eerie-black1);
  /* SURGICAL FIX: Ensure the toolbar is constrained to its parent's height */
  height: 100%;
  max-height: 100%; 
  /* Cross-browser scrollbar colors */
  scrollbar-width: thin;
  scrollbar-color: var(--orange-yellow-crayola) transparent;
}

/* Arrangement for standard tool buttons */
.visualizer-toolbar .form-btn:not([data-visualizer-save-btn]) {
    width: 100%;
    height: 60px; /* Forced height for a square tool look */
    padding: 8px;
    font-size: 10px;
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.5px;
    display: flex;
    flex-direction: column; /* SURGICAL FIX: Stacks Aa over Add Text */
    justify-content: center;
    align-items: center;
    gap: 4px;
    text-align: center;
    line-height: 1.1;
}

/* Specific styling for the "Aa" portion of the Add Text tool */
.visualizer-toolbar .form-btn[data-tool-add-text]::first-line {
    font-size: 18px;
    color: var(--orange-yellow-crayola);
}

/* Reset for standard icons in other tool buttons */
.visualizer-toolbar .form-btn ion-icon {
    font-size: 20px;
    margin-bottom: 2px;
}

/* Targeting groups of buttons to sit 2-across */
.visualizer-toolbar > .form-btn, 
.visualizer-toolbar > .tool-group {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
}

/* Action areas (Add Text, Color, Delete) look better as a grid */
.visualizer-toolbar .action-grid-tools {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

/* Webkit Scrollbar Styling */
.visualizer-toolbar::-webkit-scrollbar { 
    width: 6px; 
    display: block !important; /* Force display */
}
.visualizer-toolbar::-webkit-scrollbar-track { background: transparent; }
.visualizer-toolbar::-webkit-scrollbar-thumb { 
    background: var(--orange-yellow-crayola); 
    border-radius: 8px; 
}


.visualizer-toolbar .tool-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.visualizer-toolbar .tool-group label {
    font-size: var(--fs7);
    color: var(--light-gray70);
}

.visualizer-toolbar .form-btn {
    width: 100%;
}

.visualizer-footer {
    margin-top: auto; 
    padding-top: 20px;
    border-top: 1px solid var(--jet);
}

/* Tweak tool groups to utilize the new vertical height */
@media (min-width: 1024px) {
    .visualizer-toolbar {
        padding: 25px 20px;
        gap: 25px; /* Increased gap between tools */
    }
    
    .visualizer-toolbar .tool-group label {
        font-weight: 600;
        letter-spacing: 0.5px;
        margin-bottom: 10px;
    }
}

button[data-cursor-toggler] {
  display: none;
}

#visualizer-placeholder {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  background-color: var(--onyx);
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

#visualizer-placeholder.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

/* --- Mobile Customization Warning Logic --- */
.mobile-visualizer-msg {
  display: none; /* Hidden by default */
  width: 100%;
  margin-top: 15px;
  padding: 15px;
  background: var(--bg-gradient-jet);
  border: 1px solid var(--jet);
  border-radius: 12px;
  text-align: center;
  color: var(--light-gray70);
}

.mobile-visualizer-msg ion-icon {
  margin: 0 auto 10px;
  font-size: 24px;
  color: var(--orange-yellow-crayola);
}

.mobile-visualizer-msg p {
  font-size: var(--fs-7);
  line-height: 1.5;
}

/* Ensure the Success Modal takes up the full canvas area but stays inside the container */
#visualizer-success-modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

/* Logic for Small Phones only */
@media (max-width: 600px) {
    .mobile-visualizer-msg {
        display: block; /* Show warning only on very small screens */
    }
}

/* Layout logic for Mobile & Tablets (Up to 900px) */
@media (max-width: 900px) {
    /* 1. Reset the container to remove all padding/margins */
    .visualizer-container {
        padding: 0 !important;
    }

    /* 2. Force Modal to fixed Full Screen with NO overflow */
    .visualizer-modal {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        margin: 0 !important;
        border: none !important;
        border-radius: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        transform: none !important;
        background: var(--eerie-black2) !important;
    }

    /* 3. The Body must be the only part that scrolls */
    .visualizer-body {
        display: block !important; /* Stack children vertically */
        width: 100% !important;
        flex-grow: 1 !important;
        overflow-y: auto !important; /* Enable vertical scrolling */
        overflow-x: hidden !important; /* Kill horizontal bleeding */
        -webkit-overflow-scrolling: touch;
        /* Reset height for mobile stacking */
        height: auto !important;
    }

    /* 4. Reset every internal panel to 100% width */
    .visualizer-info-panel, 
    .visualizer-canvas-container, 
    .visualizer-toolbar {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        position: relative !important;
        border: none !important;
        box-sizing: border-box !important;
    }

    /* 5. Fix the text bleed */
    .visualizer-info-panel {
        padding: 15px !important;
        border-bottom: 1px solid var(--jet) !important;
    }

    .visualizer-disclaimer {
        white-space: normal !important;
        word-wrap: break-word !important;
        width: 100% !important;
        font-size: 11px;
    }

    /* 6. Fix the Canvas sizing so it doesn't take over the whole screen */
    .visualizer-canvas-container {
        /* SURGICAL FIX: Use dynamic viewport units to handle rotation better */
        height: 60svh !important; 
        min-height: 300px;
        width: 100% !important;
        background-color: var(--onyx);
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
        overflow: hidden !important; 
        position: relative !important;
        touch-action: none !important; 
        border-bottom: 1px solid var(--jet);
    }

    /* Extra optimization for landscape specifically */
    @media (max-height: 500px) and (orientation: landscape) {
        .visualizer-canvas-container {
            height: 70vh !important;
        }
        .visualizer-header {
            padding: 5px 20px !important;
        }
    }

    /* Force the inner Fabric interaction container to stay centered */
    .canvas-container {
        margin: 0 auto !important;
        touch-action: none !important;
        user-select: none !important;
        -webkit-user-drag: none !important;
    }

    /* 7. Force the Toolbar to appear below the image */
    .visualizer-toolbar {
        padding: 20px 15px 120px !important; /* Extra bottom padding for the mobile browser bar */
        border-top: 1px solid var(--jet) !important;
        background: var(--eerie-black2) !important;
    }

    /* Make buttons and inputs easy to hit on mobile */
    .visualizer-toolbar .tool-group {
        margin-bottom: 20px !important;
    }
    
    .visualizer-toolbar label {
        display: block !important;
        margin-bottom: 8px !important;
    }
}

/* --- VISUALIZER COACH MARKS MOBILE TOGGLE --- */
@media (max-width: 900px) {
    .coach-zoom-desktop {
        display: none !important;
    }
    .coach-zoom-mobile {
        display: block !important;
    }
}

/* --- Visualizer Disclaimer Note --- */
.visualizer-note {
  border: 1px solid var(--orange-yellow-crayola);
  background: var(--bg-gradient-jet); /* Matches the card background style */
  padding: 12px 15px;
  border-radius: 8px;
  margin-bottom: 20px;
  font-size: var(--fs-7);
  color: var(--light-gray);
  line-height: 1.5;
  box-shadow: var(--shadow1);
  animation: fade 0.3s ease-in-out;
}

/* --- Visualizer Specific Animations --- */
@keyframes laserScan {
    0% { top: 0%; opacity: 0; }
    10% { opacity: 1; }
    90% { opacity: 1; }
    100% { top: 100%; opacity: 0; }
}

.laser-active {
    animation: laserScan 1.5s linear infinite;
}

#visualizer-coach-marks {
    position: absolute;
    pointer-events: none;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 10006;
}

/* =================================================================== */
/*    VISUALIZER SET BAR & SYNC CONTROLS                               */
/* =================================================================== */

#visualizer-set-bar {
    display: none; 
    background: var(--eerie-black1);
    border-bottom: 1px solid var(--orange-yellow-crayola);
    padding: 8px 15px; /* Reduced padding */
    align-items: center;
    justify-content: space-between; 
    gap: 8px; 
    flex-wrap: nowrap; /* Force single line in header area */
    overflow-x: hidden; 
}

/* Make slots smaller on mobile to fit the sync button */
@media (max-width: 600px) {
    #visualizer-set-bar {
        padding: 15px 15px !important; /* Increased vertical breathing room to prevent clipping */
        min-height: 65px; /* Explicit height ensures slot highlights aren't squished */
    }

    #set-slots-container .form-btn {
        width: 32px !important;
        height: 32px !important;
        font-size: 10px !important;
    }
    
    #visualizer-sync-container label span {
        font-size: 9px;
    }
}

#set-slots-container {
    display: flex;
    gap: 4px; /* Tighter gap */
    flex-shrink: 0;
    align-items: center;
}

#visualizer-sync-container {
    display: flex;
    align-items: center;
    padding-left: 0;
    flex-shrink: 1; /* Allow it to shrink if needed */
}

/* Simplified Label for Mobile Space */
#visualizer-sync-container label {
    font-size: 10px;
    color: var(--orange-yellow-crayola);
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    margin: 0;
    white-space: nowrap; 
    background: rgba(243, 111, 33, 0.1);
    padding: 6px 8px;
    border-radius: 4px;
    border: 1px solid rgba(243, 111, 33, 0.3);
}

#visualizer-sync-container label:hover {
    border-color: var(--orange-yellow-crayola);
    background: var(--jet);
}

#sync-all-slots {
    accent-color: var(--orange-yellow-crayola);
    transform: scale(1.2);
    margin: 0;
}

#visualizer-set-label-group {
    display: flex;
    flex-direction: column;
    gap: 1px;
    flex-shrink: 0;
}

#visualizer-slot-label {
    font-size: 10px; /* Sized down for professional look */
    color: var(--light-gray70);
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 1px;
}

#glassware-coach-label {
    display: none; /* Controlled by JS */
    font-size: 8px;
    color: var(--orange-yellow-crayola);
    font-weight: 600;
    text-transform: uppercase;
}

#set-slots-container {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

#personalization-cost-badge {
    display: none; /* Controlled by JS */
    background: rgba(92, 184, 92, 0.1);
    border: 1px solid #5cb85c;
    padding: 4px 12px;
    border-radius: 20px;
    color: #5cb85c;
    font-size: 10px;
    font-weight: 700;
    white-space: nowrap;
}

#visualizer-orientation-container {
    display: none; /* Controlled by JS */
    border-left: 1px solid var(--jet);
    padding-left: 20px;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.viz-sub-label {
    font-size: 10px;
    color: var(--light-gray70);
    text-transform: uppercase;
    font-weight: 700;
}

.viz-toggle-pill {
    display: flex;
    background: var(--eerie-black1);
    border-radius: 20px;
    padding: 2px;
    border: 1px solid var(--jet);
}

.viz-toggle-pill button {
    border-radius: 20px;
    padding: 4px 12px;
    font-size: 9px;
    font-weight: bold;
    border: none;
    transition: var(--transition1);
}

.viz-toggle-pill button.active {
    background: var(--orange-yellow-crayola);
    color: var(--smoky-black);
}

#visualizer-sync-container {
    display: flex;
    align-items: center;
    padding-left: 10px;
    /* Removed margin-left: auto to bring it into the main flow */
}

#visualizer-sync-container label {
    font-size: 11px;
    color: var(--white2);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 0;
    white-space: nowrap; /* Surgical Fix: Forces text onto one line */
}

#sync-all-slots {
    accent-color: var(--orange-yellow-crayola);
    transform: scale(1.1);
    cursor: pointer;
}

/* --- SURGICAL ADD: Foreman's Guide Styling --- */
#visualizer-guide-text {
    font-size: 11px;
    color: var(--orange-yellow-crayola);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-left: 20px;
    padding-left: 20px;
    border-left: 1px solid var(--jet);
    display: none; /* Hidden on tiny screens */
    animation: fade 0.3s ease-in-out;
}

@media (min-width: 1024px) {
    #visualizer-guide-text {
        display: block;
    }
}

/* Tooltip Hint for Sliders */
.tool-group label {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tool-help-icon {
    font-size: 14px;
    color: var(--light-gray70);
    cursor: help;
}

/* DEV-approved mobile customizer and image-price layout. */
@media (max-width: 600px) {
  #visualizer-set-bar { display:flex !important; flex-direction:column !important; align-items:stretch !important; gap:10px !important; position:relative !important; padding:12px 12px 24px !important; min-height:158px !important; overflow:hidden !important; }
  #visualizer-set-label-group { width:100%; min-width:0; flex:0 0 auto; align-self:stretch; }
  #visualizer-slot-label { display:block; line-height:1.25; }
  #glassware-coach-label { line-height:1.25; margin-top:3px; }
  #set-slots-container { width:100%; min-width:0; flex:0 0 auto; display:flex; gap:8px; overflow-x:auto; overflow-y:hidden; padding:2px 1px 4px; scrollbar-width:none; }
  #set-slots-container::-webkit-scrollbar { display:none; }
  #set-slots-container .form-btn { flex:1 0 0; width:auto !important; min-width:0 !important; height:40px !important; margin:0 !important; padding:0 8px !important; font-size:11px !important; white-space:nowrap; }
  #personalization-cost-badge { position:absolute !important; top:12px; right:12px; display:inline-block !important; width:max-content !important; min-width:max-content !important; max-width:none !important; overflow:visible; text-overflow:clip; padding:0; background:transparent !important; border:0 !important; border-radius:0; color:#5cb85c; font-size:9px; line-height:1.2; white-space:nowrap !important; }
  #personalization-cost-badge span { display:inline !important; white-space:nowrap !important; }
  #visualizer-orientation-container { width:100%; min-width:0; flex:0 0 auto; justify-content:center; margin-top:5px; border-left:none; border-top:1px solid var(--jet); padding:9px 0 0; gap:8px; }
  #visualizer-orientation-container .viz-toggle-pill button { min-height:30px; padding:5px 10px; }
}


/* Desktop: keep the green personalization pill at its natural one-line width. */
@media (min-width: 601px) {
  #personalization-cost-badge {
    display: inline-block !important;
    width: max-content !important;
    min-width: max-content !important;
    max-width: none !important;
    flex: 0 0 auto !important;
    white-space: nowrap !important;
    padding: 4px 14px !important;
  }
  #personalization-cost-badge span {
    display: inline !important;
    white-space: nowrap !important;
  }
}
