/* styles.css - Fixed mobile viewport issues with progress bar */

/* Reset and variables */
* { 
  margin: 0; 
  padding: 0; 
  box-sizing: border-box; 
}

:root {
  --bg: #fafafa;
  --panel: #fff;
  --border: #e0e0e0;
  --border-2: #ddd;
  --text: #333;
  --primary: #6c63ff;
  --primary-2: #5c53ef;
  --conflict: #ff4444;
  --success: #4caf50;
  
  --spacing: 1.2rem;
  --gap: 15px;
}

body {
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* Main grid layout - improved mobile viewport handling */
#container {
  display: grid;
  grid-template: "settings view3d" 1fr 
                 "settings components" 120px / 200px 1fr;
  height: 100vh;
  /* Fallback for mobile browsers with dynamic viewport */
  height: -webkit-fill-available;
  height: 100dvh; /* Dynamic viewport height for modern browsers */
  width: 100vw;
  position: fixed; /* Ensure it stays in viewport */
  top: 0;
  left: 0;
}

#container.layers-visible {
  grid-template: "settings view3d layers" 1fr 
                 "settings components layers" 120px / 200px 1fr 120px;
}

/* Panels */
#settings {
  grid-area: settings;
  background: var(--panel);
  padding: var(--gap);
  overflow-y: auto;
  border-right: 1px solid var(--border);
}

#view3d {
  grid-area: view3d;
  background: #f5f5f5;
  position: relative;
  overflow: hidden;
}

#layers {
  grid-area: layers;
  background: var(--panel);
  padding: 20px 10px;
  display: none;
  flex-direction: column;
  align-items: center;
  gap: var(--gap);
  border-left: 1px solid var(--border);
  overflow-y: auto;
}

#container.layers-visible #layers {
  display: flex;
}

#components {
  grid-area: components;
  background: var(--panel);
  padding: var(--gap);
  display: flex;
  gap: 20px;
  overflow-x: auto;
  border-top: 1px solid var(--border);
  align-items: center;
  /* Ensure scrolling works on mobile */
  -webkit-overflow-scrolling: touch;
}

#info {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 11px;
  opacity: 0.6;
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* Settings sections */
.section {
  margin-bottom: var(--gap);
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.section:last-child { 
  border-bottom: none; 
}

.params-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}

.setting { 
  margin-bottom: 12px; 
}

.setting label,
.section-title {
  display: block;
  margin-bottom: 5px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.6;
}

/* Controls */
.stepper {
  display: flex;
  align-items: center;
  gap: 5px;
}

.stepper button {
  width: 24px;
  height: 24px;
  border: 1px solid var(--border-2);
  background: var(--panel);
  border-radius: 3px;
  cursor: pointer;
  flex-shrink: 0;
}

.stepper button:hover {
  background: #f5f5f5;
  border-color: #bbb;
}

.stepper span {
  min-width: 20px;
  text-align: center;
  font-weight: 500;
}

/* Checkboxes */
.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.checkbox input {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.checkbox label {
  font-size: 12px;
  cursor: pointer;
  opacity: 0.8;
  margin: 0;
  text-transform: none;
  letter-spacing: normal;
}

/* Presets */
.preset-columns { 
  display: flex; 
  gap: 10px; 
}

.preset-col { 
  flex: 1; 
}

.preset-col-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  opacity: 0.5;
  margin-bottom: 6px;
  text-align: center;
}

/* Buttons */
button {
  width: 100%;
  padding: 6px;
  margin-top: 6px;
  background: var(--panel);
  border: 1px solid var(--border-2);
  color: var(--text);
  border-radius: 3px;
  cursor: pointer;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  position: relative;
  overflow: hidden;
}

button:hover { 
  background: #f5f5f5; 
  border-color: #bbb; 
}

button.primary {
  border-color: var(--primary);
  color: var(--primary);
}

button.primary:hover { 
  background: #f5f4ff; 
}

button.cta {
  padding: 9px;
  font-weight: 700;
}

button:disabled {
  opacity: 0.6;
  cursor: default;
}

/* Progress bar inside button */
#reduce {
  position: relative;
}

.btn-text {
  position: relative;
  z-index: 2;
}

.btn-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: rgba(108, 99, 255, 0.1);
  overflow: hidden;
}

.btn-progress-bar {
  height: 100%;
  background: var(--primary);
  width: 0;
  transition: width 0.2s ease;
}

button.success {
  border-color: var(--success);
  color: var(--success);
  background: #f4fff4;
}

button.success:hover {
  background: #f0fff0;
}

.hint {
  font-size: 11px;
  line-height: 1.3;
  opacity: 0.65;
  margin-top: 6px;
}

/* Layer thumbnails */
.layer-cube {
  width: 80px;
  height: 80px;
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 4px;
  padding: 4px;
  background: var(--panel);
}

.layer-cube:hover { 
  border-color: #ddd; 
}

.layer-cube.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 1px rgba(108,99,255,0.2);
}

.layer-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* Components */
.vector-labels {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  padding-right: 10px;
  border-right: 1px solid var(--border);
  margin-right: -10px;
  flex-shrink: 0;
}

.vector-label-item {
  height: 18px;
  display: flex;
  align-items: center;
  font-weight: 600;
  opacity: 0.5;
  text-transform: uppercase;
}

.component {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  background: #fafafa;
  border-radius: 4px;
  border: 1px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
}

.component:hover { 
  border-color: #ccc; 
  background: #f5f5f5; 
}

.component.active {
  border-color: var(--primary);
  background: #f9f8ff;
  box-shadow: 0 0 0 1px rgba(108,99,255,0.2);
}

.vectors { 
  display: flex; 
  flex-direction: column; 
  gap: 4px; 
}

.vector { 
  display: flex; 
  gap: 2px; 
}

.vector-values { 
  display: flex; 
  gap: 2px; 
}

/* Bit cells */
.bit {
  width: 18px;
  height: 18px;
  background: var(--panel);
  border: 1px solid var(--border-2);
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 10px;
  color: #666;
}

.bit.small { 
  width: 14px; 
  height: 14px; 
  font-size: 8px; 
}

.bit.tiny { 
  width: 10px; 
  height: 10px; 
  font-size: 7px; 
}

.bit.on {
  background: var(--primary);
  border-color: #7c72ff;
  color: #fff;
}

.bit:hover { 
  border-color: #aaa; 
}

/* Mobile menu */
.menu-toggle {
  display: none;
  position: fixed;
  top: var(--gap);
  left: var(--gap);
  z-index: 1001;
  width: 40px;
  height: 40px;
  background: var(--panel);
  border: 1px solid var(--border-2);
  border-radius: 4px;
  font-size: 20px;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.menu-toggle:hover { 
  background: #f5f5f5; 
}

.close-settings {
  display: none;
  position: absolute;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  font-size: 24px;
  color: #666;
  cursor: pointer;
}

.settings-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 998;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.settings-overlay.active {
  display: block;
  opacity: 1;
  pointer-events: auto;
}

/* Mobile responsive with improved viewport handling */
@media (max-width: 768px) {
  :root {
    --gap: 10px;
  }

  #container,
  #container.layers-visible {
    grid-template: "view3d" 1fr "components" 100px / 1fr;
    height: 100vh;
    height: -webkit-fill-available;
    height: 100dvh;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  
  .menu-toggle {
    display: block;
  }
  
  #settings {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 85vw;
    transform: translateX(-100%);
    box-shadow: 2px 0 10px rgba(0,0,0,0.2);
    padding-top: 60px;
    transition: transform 0.25s ease;
    z-index: 999;
  }
  
  #settings.active {
    transform: translateX(0);
  }
  
  .close-settings {
    display: block;
  }
  
  .settings-overlay {
    display: block;
  }
  
  #layers {
    display: none !important;
  }
  
  #container.layers-visible #layers {
    display: flex !important;
    position: fixed;
    right: 10px;
    top: 70px;
    bottom: 110px; /* Adjusted to ensure it doesn't overlap components */
    width: 90px;
    z-index: 997;
    background: rgba(255, 255, 255, 0.95);
    box-shadow: -2px 0 8px rgba(0,0,0,0.1);
    border-radius: 8px;
    padding: 10px 5px;
  }
  
  #info {
    top: 25px;
    left: 65px;
    font-size: 10px;
  }
  
  #components {
    height: 100px; /* Reduced height for better fit */
    padding: 8px var(--gap);
    gap: var(--gap);
    /* Ensure it's always visible at bottom */
    position: relative;
    z-index: 10;
  }

  .component {
    padding: 8px;
  }
  
  .bit { width: 16px; height: 16px; font-size: 9px; }
  .bit.small { width: 12px; height: 12px; font-size: 7px; }
  .bit.tiny { width: 9px; height: 9px; font-size: 6px; }
  
  .layer-cube { width: 65px; height: 65px; }
}

@media (max-width: 480px) {
  #container,
  #container.layers-visible {
    grid-template: "view3d" 1fr "components" 90px / 1fr; /* Reduced height for small screens */
  }
  
  #settings { width: 240px; }
  
  #components { 
    height: 90px; 
    padding: 6px 8px; 
    gap: 10px; 
  }
  
  #container.layers-visible #layers {
    width: 75px;
    right: 5px;
    bottom: 95px; /* Adjusted for smaller components panel */
    padding: 8px 4px;
  }
  
  .layer-cube { width: 55px; height: 55px; }
  .component { padding: 6px; }
  .bit { width: 14px; height: 14px; font-size: 8px; }
  .bit.small { width: 11px; height: 11px; font-size: 6px; }
  .bit.tiny { width: 8px; height: 8px; font-size: 5px; }
  
  /* Ensure vector labels fit */
  .vector-labels {
    padding-right: 6px;
    margin-right: -6px;
  }
  
  .vector-label-item {
    height: 16px;
    font-size: 10px;
  }
}

/* iOS specific fixes for viewport */
@supports (-webkit-touch-callout: none) {
  #container {
    height: -webkit-fill-available;
  }
}

/* Ensure components panel is always accessible */
@media (max-height: 500px) {
  #container,
  #container.layers-visible {
    grid-template: "view3d" 1fr "components" 80px / 1fr;
  }
  
  #components {
    height: 80px;
    padding: 5px 8px;
  }
  
  .component {
    padding: 5px;
  }
  
  .bit { width: 12px; height: 12px; font-size: 7px; }
  .bit.small { width: 10px; height: 10px; font-size: 6px; }
  .bit.tiny { width: 8px; height: 8px; font-size: 5px; }
}