* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --secondary: #64748b;
  --background: #ffffff;
  --sidebar-bg: #f8f9fa;
  --border: #e2e8f0;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --success: #10b981;
  --canvas-grid: #f1f5f9;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color: var(--text-primary);
  background: var(--background);
  overflow: hidden;
}

.app-container {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
}

/* Header */
header {
  background: #2c3e50;
  color: white;
  padding: 8px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

header h1 {
  font-size: 16px;
  font-weight: 600;
}

.export-btn {
  background: var(--primary);
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s ease;
}

.export-btn:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
}

/* Main Layout */
.main-content {
  display: grid;
  grid-template-columns: 380px 1fr;
  height: 100%;
  overflow: hidden;
}

/* Full width canvas when sidebar is hidden */
.main-content.full-width {
  grid-template-columns: 1fr;
}

@media (max-width: 1200px) {
  .main-content:not(.full-width) {
    grid-template-columns: 350px 1fr;
  }
}

@media (max-width: 900px) {
  .main-content:not(.full-width) {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .sidebar {
    max-height: 40vh;
    overflow-y: auto;
  }

  /* Stack header items vertically on mobile */
  header {
    padding: 12px;
  }

  header > div {
    flex-wrap: wrap;
    gap: 8px !important;
  }

  header h1 {
    font-size: 18px;
    margin-bottom: 8px;
    width: 100%;
  }

  #lotTitle {
    width: 100% !important;
    order: 3;
  }

  #lotSelector {
    flex: 1;
    min-width: 120px;
  }

  .generate-btn, .export-btn {
    font-size: 13px;
    padding: 6px 12px;
  }
}

@media (max-width: 600px) {
  header h1 {
    font-size: 16px;
  }

  .toggle-item {
    font-size: 12px;
  }

  .sidebar {
    padding: 12px;
  }

  .section-title {
    font-size: 11px;
  }

  input, select {
    font-size: 14px;
  }

  .side-row {
    gap: 4px;
  }
}

/* Sidebar */
.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  padding: 12px;
  overflow-y: auto;
}

.section {
  margin-bottom: 12px;
}

.section-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 8px;
  letter-spacing: 0.5px;
}

.input-group {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.input-group label {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  display: block;
}

input[type="number"],
input[type="text"],
select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
  transition: border-color 0.2s;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
}

.coordinate-inputs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.coordinate-inputs input {
  font-family: 'SF Mono', Monaco, monospace;
}

/* Sides Table */
.sides-container {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: white;
}

.side-row {
  display: grid;
  grid-template-columns: 110px 70px 2fr 32px;
  gap: 6px;
  padding: 6px;
  border-bottom: 1px solid var(--border);
}

.adjacent-input {
  min-width: 0; /* Allow flex shrinking */
}

.side-row:last-child {
  border-bottom: none;
}

.side-row input {
  padding: 6px 8px;
  font-size: 12px;
}

.remove-btn {
  background: #ef4444;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.remove-btn:hover {
  background: #dc2626;
}

.add-btn {
  width: 100%;
  padding: 6px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-secondary);
  transition: all 0.2s;
  margin-top: 6px;
}

.add-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.generate-btn {
  width: 100%;
  padding: 6px 12px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s;
}

.generate-btn:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
}

/* Canvas Area */
.canvas-area {
  display: flex;
  flex-direction: column;
  background: white;
}

.canvas-toolbar {
  height: 48px;
  background: white;
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.toolbar-group {
  display: flex;
  gap: 8px;
}

.toolbar-btn {
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.toolbar-btn:hover {
  background: var(--sidebar-bg);
  border-color: var(--primary);
}

.zoom-indicator {
  font-size: 13px;
  color: var(--text-secondary);
  font-family: 'SF Mono', Monaco, monospace;
}

.canvas-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background:
    repeating-linear-gradient(0deg, transparent, transparent 19px, var(--canvas-grid) 19px, var(--canvas-grid) 20px),
    repeating-linear-gradient(90deg, transparent, transparent 19px, var(--canvas-grid) 19px, var(--canvas-grid) 20px);
  overflow: auto;
  position: relative;
}

#bgCanvas {
  position: absolute;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

#canvas, #combinedCanvas {
  position: absolute;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  cursor: grab;
}

#canvas.grabbing, #combinedCanvas.grabbing {
  cursor: grabbing;
  border: 1px solid #93c5fd;
}

.toggles-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px 12px;
}

.toggle-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-primary);
  cursor: pointer;
  user-select: none;
}

.toggle-item input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

/* Starting point positioned below sides */
.sidebar .section:nth-child(1) {
  order: 2;
}

.sidebar .section:nth-child(2) {
  order: 1;
}

.sidebar .section:nth-child(3) {
  order: 3;
}

.sidebar .section:nth-child(4) {
  order: 4;
}

code {
  background: rgba(37, 99, 235, 0.08);
  padding: 2px 5px;
  border-radius: 3px;
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 10px;
  white-space: nowrap;
}

/* Modal Styles */
.modal {
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 16px;
  color: var(--text-primary);
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}

.modal-close:hover {
  background: var(--border);
}

.modal-body {
  padding: 20px;
}

.modal-body p {
  margin: 8px 0;
  font-size: 14px;
  color: var(--text-primary);
}

.modal-body label {
  display: block;
  margin: 16px 0 8px 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
}

.modal-body select {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 14px;
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-footer button {
  padding: 8px 16px;
  font-size: 13px;
  border-radius: 4px;
  cursor: pointer;
}

.modal-footer button:first-child {
  background: white;
  border: 1px solid var(--border);
  color: var(--text-primary);
}

.modal-footer button:first-child:hover {
  background: var(--border);
}

/* Link Button */
.link-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.link-btn:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
