:root {
  --bg-primary: #121212;
  --bg-secondary: #1e1e1e;
  --bg-tertiary: #252525;
  --accent-color: #4a6fa5;
  --text-primary: #f0f0f0;
  --text-secondary: #b0b0b0;
  --border-color: #333;
  --border-radius: 6px;
  --shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
  --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  height: 100dvh; /* Dynamic viewport height */
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#tools {
  z-index: 1000 !important;
  pointer-events: auto !important;
  position: relative !important;
  float: right;
  width: 340px;
  padding: 20px;
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height */
  overflow-y: auto;
  background-color: var(--bg-secondary);
  border-left: 1px solid var(--border-color);
  scrollbar-width: thin;
  scrollbar-color: var(--bg-tertiary) var(--bg-secondary);
}

#tools::-webkit-scrollbar {
  width: 8px;
}

#tools::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}

#tools::-webkit-scrollbar-thumb {
  background-color: var(--bg-tertiary);
  border-radius: 4px;
}

#tools * {
  pointer-events: auto !important;
}

/* Make sure tools area blocks pointer events to SVG below */
#tools::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
  pointer-events: auto;
}

/* SVG styling - both canvas and grid */
#canvas, #grid {
  width: calc(100vw - 340px);
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height */
  position: absolute;
  top: 0;
  left: 0;
}

#canvas {
  z-index: 10;
  background-color: var(--bg-primary);
  pointer-events: none;
}

#grid {
  z-index: 20;
  pointer-events: all;
}

#canvas *, #grid * {
  pointer-events: auto;
}

/* Color palette - fixed to show all colors without scrollbar */
.color-palette {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 12px 0;
  width: 100%;
}

.color-box {
  flex: 0 0 18px;
  height: 18px;
  cursor: pointer;
  border: 2px solid var(--border-color);
  border-radius: 50%;
  transition: var(--transition);
  box-shadow: var(--shadow);
}

.color-box:hover {
  transform: scale(1.15);
  border-color: var(--text-primary);
}

label {
  display: inline-block;
  color: var(--text-secondary);
  font-size: 0.85rem;
  margin-bottom: 4px;
}

/* Filename label with overflow ellipsis */
#fnamelabel {
  display: block;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-secondary);
  font-size: 0.8rem;
  margin-top: 4px;
  padding: 4px 8px;
  background-color: var(--bg-tertiary);
  border-radius: var(--border-radius);
}

button {
  width: 100%;
  padding: 10px 0;
  margin: 8px 0;
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
  border: none;
  border-radius: var(--border-radius);
  cursor: pointer;
  transition: var(--transition);
  font-weight: 500;
  letter-spacing: 0.5px;
  box-shadow: var(--shadow);
}

button:hover {
  background-color: var(--accent-color);
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

/* UNIFIED CHECKBOX STYLING - applies to ALL checkboxes in toolbar */
.checkbox-button {
  position: relative;
  display: inline-block;
  width: 100%;
  padding: 10px 0;
  margin: 8px 0;
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
  border: none;
  border-radius: var(--border-radius);
  cursor: pointer;
  transition: var(--transition);
  font-weight: 500;
  letter-spacing: 0.5px;
  box-shadow: var(--shadow);
  text-align: center;
}

.checkbox-button:hover {
  background-color: var(--accent-color);
  transform: translateY(-1px);
}

.checkbox-button:active {
  transform: translateY(0);
}

.checkbox-button::after {
  content: "✓";
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0;
  transition: var(--transition);
  font-weight: bold;
  color: var(--text-primary);
}

/* Show checkmark when ANY checkbox in toolbar is checked */
input[type="checkbox"]:checked + .checkbox-button::after {
  opacity: 1;
}

/* Hide ALL actual checkboxes in toolbar */
#tools input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.file-button {
  position: relative;
  overflow: hidden;
}

/* Hide standard file input */
.fileInput {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

/* Coordinates display */
.coords-display {
  display: block;
  text-align: center;
  margin: 12px 0;
  padding: 6px 0;
  background-color: var(--bg-tertiary);
  border-radius: var(--border-radius);
  color: var(--text-primary);
  font-family: 'Roboto Mono', monospace;
  font-size: 0.9rem;
}

input[type="range"] {
  width: 100%;
  height: 6px;
  margin: 12px 0;
  background: var(--bg-tertiary);
  border-radius: 3px;
  -webkit-appearance: none;
  appearance: none;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  background: var(--text-primary);
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid var(--accent-color);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  background: var(--text-primary);
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid var(--accent-color);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

input[type="text"] {
  width: 100%;
  padding: 8px 12px;
  margin: 8px 0;
  background-color: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  border-radius: var(--border-radius);
  font-size: 0.9rem;
  transition: var(--transition);
}

input[type="text"]:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px rgba(74, 111, 165, 0.3);
}

.copyright {
  position: absolute;
  left: 20px;
  bottom: 20px;
  z-index: 100;
  color: var(--text-secondary);
  font-size: 0.75rem;
  opacity: 0.7;
}

/* Value labels styling */
.value-label {
  display: inline-block;
  min-width: 36px;
  padding: 2px 6px;
  background-color: var(--bg-tertiary);
  border-radius: 12px;
  text-align: center;
  color: var(--text-primary);
  font-family: 'Roboto Mono', monospace;
  font-size: 0.8rem;
  margin-left: 8px;
}

.slider-container {
  margin: 16px 0;
}

.slider-container label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.slider-container .slider-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* SVG content styling */
#canvas path, 
#canvas line, 
#canvas polyline {
  stroke: var(--text-primary);
  stroke-width: 0.5;
  stroke-linecap: round;
}

#canvas polygon {
  stroke: var(--border-color);
  stroke-width: 0.5;
}

/* Grid polygon styling */
#grid polygon {
  stroke: black;
  stroke-width: 1;
  transition: stroke 0.2s ease, stroke-width 0.2s ease;
}

/* Section separator */
.section-separator {
  height: 1px;
  background-color: var(--border-color);
  margin: 20px 0;
  opacity: 0.3;
}

/* Tooltip styles */
[data-tooltip] {
  position: relative;
}

[data-tooltip]:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
  padding: 4px 8px;
  border-radius: var(--border-radius);
  font-size: 0.75rem;
  white-space: nowrap;
  z-index: 1000;
  box-shadow: var(--shadow);
}

/* MOBILE PORTRAIT */
@media (max-width: 768px) and (orientation: portrait) {
  html, body {
    height: 100dvh; /* Dynamic viewport height */
  }

  body {
    overflow: visible;
    display: flex;
    flex-direction: column;
    height: 100dvh;
  }

  /* SVG Container - takes up most of the screen at TOP */
  .svg-container {
    position: relative;
    width: 100vw;
    height: 70dvh; /* Dynamic viewport height */
    order: 1;
    flex-shrink: 0;
    overflow: hidden;
  }

  #canvas, #grid {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }

  /* Force SVG elements to use container bounds for viewBox calculation */
  #canvas svg, #grid svg {
    width: 100%;
    height: 100%;
  }

  /* Tools at BOTTOM */
  #tools {
    position: relative;
    width: 100%;
    height: 30dvh; /* Dynamic viewport height */
    float: none;
    border-left: none;
    border-top: 1px solid var(--border-color);
    padding: 10px;
    overflow-y: auto;
    order: 2;
    flex-shrink: 0;
  }

  .controls-container {
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 1000;
  }

  .copyright {
    position: fixed;
    right: 10px;
    left: auto;
    bottom: 10px;
  }

  /* Stack slider groups vertically */
  .slider-group.compact {
    display: block;
  }

  /* Make buttons more touch-friendly */
  button {
    padding: 12px 0;
    margin: 10px 0;
  }

  /* Adjust file input area */
  .file-actions {
    display: flex;
    flex-direction: column;
  }

  .filename-input {
    margin-top: 10px;
  }

  /* Adjust color boxes for smaller screens - still no scrollbar */
  .color-box {
    flex: 0 0 18px;
    height: 18px;
  }

  .color-palette {
    gap: 4px;
  }
}

/* MOBILE LANDSCAPE */
@media (max-width: 768px) and (orientation: landscape) {
  html, body {
    height: 100dvh; /* Dynamic viewport height */
  }

  body {
    overflow: visible;
    display: flex;
    flex-direction: row;
    height: 100dvh;
  }

  /* SVG Container - takes up most of the screen at LEFT */
  .svg-container {
    position: relative;
    width: 70vw;
    height: 100dvh; /* Dynamic viewport height */
    order: 1;
    flex-shrink: 0;
    overflow: hidden;
  }

  #canvas, #grid {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }

  /* Force SVG elements to use container bounds for viewBox calculation */
  #canvas svg, #grid svg {
    width: 100%;
    height: 100%;
  }

  /* Tools at RIGHT */
  #tools {
    position: relative;
    width: 30vw;
    height: 100dvh; /* Dynamic viewport height */
    float: none;
    border-left: 1px solid var(--border-color);
    border-top: none;
    padding: 8px;
    overflow-y: auto;
    order: 2;
    flex-shrink: 0;
    font-size: 0.85rem; /* Smaller text for landscape */
  }

  .controls-container {
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 1000;
  }

  .copyright {
    position: fixed;
    left: 10px;
    bottom: 10px;
    right: auto;
  }

  /* Compact layout for landscape */
  .slider-container {
    margin: 12px 0;
  }

  button {
    padding: 8px 0;
    margin: 6px 0;
    font-size: 0.8rem;
  }

  .value-label {
    min-width: 30px;
    font-size: 0.7rem;
  }

  .coords-display {
    font-size: 0.8rem;
  }

  /* Smaller color boxes for landscape */
  .color-box {
    flex: 0 0 16px;
    height: 16px;
  }

  .color-palette {
    gap: 3px;
  }
}

/* For very small screens (portrait) */
@media (max-width: 480px) and (orientation: portrait) {
  .svg-container {
    height: 65dvh; /* Dynamic viewport height */
  }

  #tools {
    height: 35dvh; /* Dynamic viewport height */
    padding: 8px;
  }

  .value-label {
    min-width: 30px;
    font-size: 0.7rem;
  }

  .slider-container label span:first-child {
    font-size: 0.8rem;
  }

  /* Even smaller color boxes for very small screens */
  .color-box {
    flex: 0 0 16px;
    height: 16px;
  }

  .color-palette {
    gap: 3px;
  }
}

/* For very small screens (landscape) */
@media (max-width: 768px) and (max-height: 480px) and (orientation: landscape) {
  #tools {
    width: 35vw; /* More space for tools on small landscape */
    font-size: 0.8rem;
    padding: 6px;
  }

  .svg-container {
    width: 65vw;
  }

  .slider-container {
    margin: 8px 0;
  }

  button {
    padding: 6px 0;
    margin: 4px 0;
    font-size: 0.75rem;
  }

  .color-box {
    flex: 0 0 14px;
    height: 14px;
  }

  .value-label {
    min-width: 25px;
    font-size: 0.65rem;
  }
}

@media (max-width: 1300px) {
 .copyright {
   display: none;
 }
}