:root {
  --bg-color: #f3f4f6;
  --panel-bg: #ffffff;
  --text-dark: #111827;
  --primary: #4338ca;
  --primary-hover: #3730a3;
}
body {
  font-family: 'Inter', sans-serif;
  background-color: var(--bg-color);
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

header {
  background-color: var(--panel-bg);
  padding: 15px 40px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 100;
  flex-shrink: 0;
}
.logo {
  font-size: 22px;
  font-weight: 900;
  color: var(--text-dark);
  letter-spacing: -0.5px;
}
.nav-tabs {
  display: flex;
  gap: 10px;
}
.nav-btn {
  background: none;
  border: none;
  padding: 10px 24px;
  font-size: 14px;
  font-weight: 700;
  color: #6b7280;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}
.nav-btn.active {
  background: var(--text-dark);
  color: white;
}

.view-container {
  display: none;
  padding: 40px;
  flex: 1;
  overflow-y: auto;
}
.view-container.active {
  display: flex;
}

/* --- Generator View --- */
#view-generator {
  flex-direction: column;
  align-items: center;
}
.gen-header {
  text-align: center;
  margin-bottom: 40px;
  max-width: 800px;
}
.gen-header h2 {
  font-size: 38px;
  margin-bottom: 10px;
  color: var(--text-dark);
  font-weight: 900;
  letter-spacing: -1px;
}
.gen-controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  margin-top: 20px;
  background: white;
  padding: 15px;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  flex-wrap: wrap;
}
.category-select {
  padding: 14px 20px;
  border-radius: 8px;
  border: 2px solid #e5e7eb;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  outline: none;
  background: #f9fafb;
}

.template-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 40px;
  width: 100%;
  max-width: 1200px;
  margin-bottom: 40px;
}
.template-card {
  background: white;
  border-radius: 12px;
  padding: 15px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.03);
  border: 1px solid #e5e7eb;
  transition: transform 0.3s ease;
  cursor: pointer;
  position: relative;
  width: 250px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.template-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  border-color: #d1d5db;
}

.preview-wrapper {
  width: 250px;
  overflow: hidden;
  border-radius: 8px;
  position: relative;
  background: #eee;
  margin: 0 auto;
}
.preview-canvas {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: top left;
  pointer-events: none;
  overflow: hidden;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.template-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
  z-index: 100;
}
.template-card:hover .template-overlay {
  opacity: 1;
}
.btn-use-template {
  background: var(--text-dark);
  color: white;
  border: none;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: bold;
  border-radius: 6px;
  cursor: pointer;
  pointer-events: auto;
}

.btn-action {
  background: var(--primary);
  color: white;
  border: none;
  padding: 16px 36px;
  font-size: 15px;
  font-weight: 900;
  border-radius: 8px;
  cursor: pointer;
  text-transform: uppercase;
  box-shadow: 0 4px 15px rgba(67, 56, 202, 0.3);
  transition: transform 0.2s;
  width: 100%;
}
.btn-action:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(67, 56, 202, 0.4);
  background: var(--primary-hover);
}

/* --- Editor View --- */
#view-editor {
  gap: 40px;
  justify-content: center;
  align-items: flex-start;
  padding: 20px 40px;
}
.controls {
  flex: 1;
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  border: 1px solid #e5e7eb;
  max-width: 440px;
  height: calc(100vh - 120px);
  overflow-y: auto;
}
.controls label {
  display: block;
  margin-top: 18px;
  font-weight: 700;
  margin-bottom: 6px;
  font-size: 11px;
  text-transform: uppercase;
  color: #4b5563;
}
.controls label span {
  color: var(--primary);
  font-weight: 900;
  float: right;
  font-size: 12px;
}
.controls input[type='text'],
.controls input[type='number'],
.controls textarea,
.controls input[type='file'],
.controls input[type='range'],
.controls select {
  width: 100%;
  padding: 10px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  box-sizing: border-box;
  font-family: inherit;
  font-size: 14px;
}
.controls textarea {
  resize: vertical;
  height: 50px;
}
.controls input[type='color'] {
  padding: 0;
  height: 35px;
  width: 100%;
  cursor: pointer;
  border: none;
  border-radius: 4px;
}
.flex-group {
  display: flex;
  gap: 15px;
  align-items: center;
}
.flex-group > div {
  flex: 1;
}
.checkbox-group {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}
.checkbox-group label {
  margin: 0;
  text-transform: none;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
  color: #111827;
}
.section-title {
  margin-top: 25px;
  padding-bottom: 8px;
  border-bottom: 2px solid #f3f4f6;
  color: var(--text-dark);
  font-size: 16px;
  font-weight: 900;
}
.btn-clear {
  background: #fee2e2;
  color: #991b1b;
  border: none;
  padding: 10px;
  border-radius: 6px;
  font-weight: bold;
  cursor: pointer;
  white-space: nowrap;
}
.btn-util {
  background: #f1f5f9;
  color: #475569;
  border: 1px solid #cbd5e1;
  padding: 10px;
  border-radius: 6px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
}
.btn-util:hover {
  background: #e2e8f0;
  color: #0f172a;
}

/* Format & Icon Grids */
.format-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 10px;
}
.fmt-btn {
  background: #f8fafc;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  transition: all 0.2s;
}
.fmt-btn:hover {
  border-color: #cbd5e1;
  background: #f1f5f9;
}
.fmt-btn.active {
  border-color: var(--primary);
  background: #eef2ff;
  color: var(--primary);
}
.fmt-icon {
  background: #cbd5e1;
  border-radius: 3px;
  margin-bottom: 8px;
}
.fmt-btn.active .fmt-icon {
  background: var(--primary);
}
.fmt-label {
  font-size: 11px;
  font-weight: 800;
  text-align: center;
  line-height: 1.2;
}

.icon-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  background: #f8fafc;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  margin-bottom: 15px;
  max-height: 180px;
  overflow-y: auto;
  align-items: center;
}
.icon-item {
  font-size: 22px;
  color: #475569;
  text-align: center;
  padding: 10px 0;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}
.icon-item:hover {
  background: #e2e8f0;
  color: var(--primary);
  transform: scale(1.15);
}
.icon-grid::-webkit-scrollbar {
  width: 6px;
}
.icon-grid::-webkit-scrollbar-track {
  background: transparent;
}
.icon-grid::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 10px;
}

/* Panels */
#context-panel,
#image-extended-controls,
#shape-context-panel,
#icon-context-panel,
#chart-context-panel {
  background: #f8fafc;
  border: 2px solid #cbd5e1;
  border-radius: 8px;
  padding: 15px;
  margin-top: 15px;
  display: none;
}
.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.panel-header h3 {
  margin: 0;
  font-size: 13px;
  font-weight: 900;
  color: var(--primary);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* --- THE CANVAS --- */
.workspace {
  flex: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  height: calc(100vh - 120px);
  overflow: auto;
  padding: 20px;
  background: #e2e8f0;
  border-radius: 12px;
}
.canvas-wrapper {
  position: relative;
  background: white;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  transition:
    height 0.3s ease,
    width 0.3s ease;
}
#post-canvas {
  position: absolute;
  top: 0;
  left: 0;
  background-color: #ffffff;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  overflow: hidden;
  transform-origin: top left;
}
#bg-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

.guide {
  position: absolute;
  background-color: #ec4899;
  z-index: 2000;
  display: none;
  pointer-events: none;
}
#guide-v {
  top: 0;
  bottom: 0;
  left: 50%;
  width: 4px;
  transform: translateX(-50%);
  box-shadow: 0 0 8px rgba(236, 72, 153, 0.6);
}
#guide-h {
  left: 0;
  right: 0;
  top: 50%;
  height: 4px;
  transform: translateY(-50%);
  box-shadow: 0 0 8px rgba(236, 72, 153, 0.6);
}

.dynamic-text {
  position: absolute;
  box-sizing: border-box;
  cursor: grab;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.15;
  min-width: 50px;
  min-height: 20px;
}
.dynamic-text:active,
.dynamic-icon:active,
.dynamic-shape:active,
.dynamic-image:active,
.dynamic-chart:active {
  cursor: grabbing;
}
.dynamic-text.selected {
  outline: 4px dashed var(--primary);
  outline-offset: 8px;
}

.dynamic-icon {
  position: absolute;
  box-sizing: border-box;
  cursor: grab;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.dynamic-icon.selected {
  outline: 4px dashed var(--primary);
  outline-offset: 8px;
}

.dynamic-shape {
  position: absolute;
  z-index: 15;
  cursor: grab;
  box-sizing: border-box;
  background-color: #000;
}
.dynamic-shape.selected {
  outline: 4px dashed #ec4899;
  outline-offset: 4px;
}

.dynamic-image {
  position: absolute;
  z-index: 5;
  display: none;
  cursor: grab;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  box-sizing: border-box;
}
.dynamic-image.selected {
  outline: 4px dashed #ec4899;
  outline-offset: 4px;
}

/* NEW: Infographic Chart */
.dynamic-chart {
  position: absolute;
  z-index: 15;
  cursor: grab;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dynamic-chart.selected {
  outline: 4px dashed #10b981;
  outline-offset: 4px;
}

.resize-handle {
  display: none;
  position: absolute;
  bottom: -20px;
  right: -20px;
  width: 40px;
  height: 40px;
  background: var(--primary);
  border-radius: 50%;
  cursor: nwse-resize;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  border: 4px solid #fff;
}
.dynamic-text.selected .resize-handle,
.dynamic-icon.selected .resize-handle {
  display: block;
}
.dynamic-shape.selected .resize-handle,
.dynamic-image.selected .resize-handle {
  display: block;
  background: #ec4899;
}
.dynamic-chart.selected .resize-handle {
  display: block;
  background: #10b981;
}

#crop-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  z-index: 1000;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.crop-container {
  width: 80%;
  max-width: 600px;
  height: 500px;
  background: white;
  padding: 24px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
}
.crop-area {
  flex: 1;
  overflow: hidden;
  background: #eee;
  border-radius: 8px;
}
#image-to-crop {
  max-width: 100%;
  max-height: 100%;
  display: block;
}
.btn-ratio {
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}
.btn-ratio:hover {
  background: #e5e7eb;
}

#export-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(255, 255, 255, 0.95);
  z-index: 9999;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(5px);
}
.spinner {
  width: 60px;
  height: 60px;
  border: 6px solid #e5e7eb;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 25px;
}
@keyframes spin {
  100% {
    transform: rotate(360deg);
  }
}

.controls::-webkit-scrollbar {
  width: 6px;
}
.controls::-webkit-scrollbar-track {
  background: transparent;
}
.controls::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 10px;
}

/* Bulk View Styles */
#view-bulk {
  flex-direction: column;
  align-items: center;
  padding: 40px;
}
.bulk-panel {
  background: white;
  width: 100%;
  max-width: 800px;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  border: 1px solid #e5e7eb;
  margin-bottom: 20px;
}
.bulk-panel h3 {
  margin-top: 0;
  color: var(--primary);
  font-weight: 900;
}
.map-row {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #f3f4f6;
  gap: 15px;
}
.map-label {
  flex: 1;
  font-weight: bold;
  font-size: 14px;
  color: var(--text-dark);
}
.map-select {
  flex: 1;
  padding: 10px;
  border-radius: 6px;
  border: 1px solid #cbd5e1;
}
.file-upload-box {
  border: 2px dashed #cbd5e1;
  background: #f8fafc;
  padding: 30px;
  text-align: center;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}
.file-upload-box:hover {
  border-color: var(--primary);
  background: #eef2ff;
}
.file-upload-box i {
  font-size: 30px;
  color: var(--primary);
  margin-bottom: 10px;
}

/* =========================================
   MOBILE RESPONSIVENESS (Below 768px)
   ========================================= */
@media (max-width: 768px) {
  /* Allow the whole page to scroll natively on mobile */
  body { height: auto; overflow-y: auto; }
  
  /* Stack the header buttons */
  header { flex-direction: column; padding: 15px 10px; gap: 15px; }
  .nav-tabs { flex-wrap: wrap; justify-content: center; }
  
  /* Stack the Studio Editor (Canvas on top, Controls below) */
  #view-editor { 
    flex-direction: column; 
    padding: 15px 10px; 
    gap: 20px; 
  }
  
  /* Let the controls take full width and disable their internal scroll */
  .controls { 
    max-width: 100%; 
    height: auto; 
    overflow-y: visible; 
  }
  
  /* Make the workspace fit the screen */
  .workspace { 
    width: 100%; 
    height: auto; 
    padding: 20px 0; 
    box-sizing: border-box; 
  }
  
  /* Center the canvas wrapper */
  .canvas-wrapper { margin: 0 auto; }
  
  /* Make flex groups stack on very tiny screens */
  .flex-group { flex-wrap: wrap; }
  
  /* Adjust Bulk Panel layout */
  .bulk-panel { padding: 15px; }
  .map-row { flex-direction: column; align-items: flex-start; }
}