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

:root {
  --bg: #0f0f0f;
  --card: #1a1a1a;
  --card2: #222222;
  --border: #2e2e2e;
  --text: #f0f0f0;
  --muted: #888;
  --accent: #3b82f6;
  --radius: 10px;
  --font: 'Inter', sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  height: 100%;
  overflow: hidden;
  overflow-x: hidden;
}

.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
  overflow-x: hidden;
}

/* ── LEFT PANEL ── */
.left-panel {
  width: 380px;
  min-width: 320px;
  flex-shrink: 0;
  overflow-y: auto;
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.app-title {
  margin: -20px -16px 0;
  padding: 16px 24px;
  background: #0a0a0a;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.chc-wordmark {
  font-family: var(--font);
  font-weight: 900;
  font-size: 32px;
  letter-spacing: -0.02em;
  color: #ffffff;
  line-height: 1;
  margin-bottom: 2px;
}
.chc-name {
  font-family: var(--font);
  font-weight: 800;
  font-size: 13px;
  letter-spacing: 0.15em;
  color: #ffffff;
  text-transform: uppercase;
  line-height: 1.2;
}
.chc-tagline {
  font-family: var(--font);
  font-weight: 400;
  font-size: 10px;
  letter-spacing: 0.2em;
  color: rgba(255,255,255,0.45);
  text-transform: uppercase;
  line-height: 1.4;
  margin-top: 2px;
}

.section-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.section-title {
  font-family: var(--font);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.4);
  margin-bottom: 12px;
}

.field-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}
.field-row:last-child { margin-bottom: 0; }

.field-row label {
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  color: rgba(255,255,255,0.75);
}

.field-row input[type=text],
.field-row select,
.field-row textarea {
  background: var(--card2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 13px;
  font-family: var(--font);
  width: 100%;
  transition: border-color 0.15s;
}
.field-row input[type=text]:focus,
.field-row select:focus,
.field-row textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.field-row textarea {
  resize: vertical;
  min-height: 60px;
}

.color-row {
  display: flex;
  gap: 12px;
}
.color-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.color-field label { font-family: var(--font); font-size: 13px; font-weight: 500; color: rgba(255,255,255,0.75); }
.color-field input[type=color] {
  width: 100%;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card2);
  cursor: pointer;
  padding: 2px;
}

/* Style cards */
.style-cards {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.style-card {
  background: var(--card2);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 10px 8px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
  text-align: center;
}
.style-card:hover { border-color: #555; }
.style-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 0 12px rgba(59,130,246,0.2);
}
.style-card.disabled {
  opacity: 0.4;
  pointer-events: none;
  cursor: not-allowed;
}
.style-card .card-name {
  font-family: var(--font);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: #ffffff;
  line-height: 1.3;
  margin-bottom: 4px;
}
.style-card .card-desc {
  font-family: var(--font);
  font-size: 11px;
  font-weight: 400;
  color: rgba(255,255,255,0.5);
  line-height: 1.4;
}

/* Upload fields */
.upload-field {
  margin-bottom: 10px;
}
.upload-field:last-child { margin-bottom: 0; }
.upload-field label {
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  color: rgba(255,255,255,0.75);
  display: block;
  margin-bottom: 4px;
}
.upload-field input[type=file] {
  width: 100%;
  font-size: 12px;
  color: var(--muted);
  background: var(--card2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  cursor: pointer;
}
.upload-field input[type=file]::file-selector-button {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 8px;
  font-size: 11px;
  cursor: pointer;
  margin-right: 8px;
}

/* Generate button */
.btn-generate {
  width: 100%;
  padding: 13px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.btn-generate:hover { opacity: 0.9; }
.btn-generate:active { transform: scale(0.99); }
.btn-generate:disabled { opacity: 0.5; cursor: not-allowed; }

/* Debug section */
.debug-section {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.debug-section summary {
  padding: 10px 14px;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.debug-section summary:hover { color: var(--text); }
.debug-prompt {
  padding: 0 14px 14px;
  font-size: 11px;
  color: #aaa;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.6;
  font-family: monospace;
  max-height: 300px;
  overflow-y: auto;
}

/* ── RIGHT PANEL ── */
.right-panel {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.preview-header {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  flex-shrink: 0;
  padding: 20px 20px 0;
}

.preview-header h2 {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  flex: 1;
}

.btn-download {
  padding: 8px 18px;
  background: #22c55e;
  color: #000;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: opacity 0.15s;
}
.btn-download:hover { opacity: 0.85; }
.btn-download:disabled { opacity: 0.4; cursor: not-allowed; }

/* Export format group — stacks ratio selector above download button */
.export-format-group {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}
.ratio-selector {
  display: flex;
  align-items: center;
  gap: 4px;
}
.ratio-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  margin-right: 4px;
  white-space: nowrap;
}
.ratio-pill {
  background: var(--card2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 12px;
  padding: 4px 9px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  transition: all 0.1s;
  font-family: var(--font);
}
.ratio-pill:hover { border-color: #555; color: var(--text); }
.ratio-pill.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Crop overlay bars and border */
.crop-bar {
  position: absolute;
  background: rgba(0,0,0,0.65);
  pointer-events: none;
  z-index: 5;
}
.crop-border {
  position: absolute;
  border: 1.5px dashed var(--accent);
  pointer-events: none;
  z-index: 6;
}
.crop-label {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(10,10,25,0.85);
  color: var(--accent);
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 8px;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  font-family: var(--font);
}

.preview-area {
  flex: 1;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  min-width: 0;
  overflow: hidden;
}

/* Preview canvas */
.preview-canvas-wrapper {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#previewCanvas {
  position: relative;
  width: 100%;
  max-width: 480px;
  aspect-ratio: 2/3;
  height: auto;
  background: #111;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

#previewCanvas .bg-image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.preview-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #444;
}
.preview-placeholder svg { opacity: 0.4; }
.preview-placeholder p { font-size: 12px; color: #555; }

.preview-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(0,0,0,0.7);
  z-index: 100;
}
.spinner {
  width: 36px; height: 36px;
  border: 3px solid #333;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.preview-loading p { font-size: 12px; color: #aaa; }

/* Text blocks on canvas */
.text-block {
  position: absolute;
  white-space: nowrap;
  overflow: hidden;
  box-sizing: border-box;
  pointer-events: auto;
  cursor: grab;
  z-index: 10;
  text-shadow: 0 2px 8px rgba(0,0,0,0.8);
  user-select: none;
}
.text-block.dragging {
  cursor: grabbing;
}
.text-block.editing {
  cursor: text !important;
  user-select: text !important;
  white-space: pre-wrap !important;
  overflow: visible !important;
  outline: none;
  border: 2px solid var(--accent) !important;
  box-shadow: 0 0 8px rgba(0, 180, 255, 0.35);
}

/* Photoshop-style selection box */
#selectionBox {
  position: absolute;
  pointer-events: none;
  z-index: 30;
  display: none;
}
#selectionBox.active { display: block; }
.sel-border {
  position: absolute;
  inset: 0;
  border: 2px dashed var(--accent);
  pointer-events: none;
}
.sel-label {
  position: absolute;
  top: -22px;
  left: 0;
  background: rgba(10,10,25,0.92);
  color: var(--accent);
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 10px;
  white-space: nowrap;
  pointer-events: none;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  font-family: var(--font);
}
.sel-handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #fff;
  border: 1.5px solid var(--accent);
  pointer-events: auto;
  z-index: 31;
  box-sizing: border-box;
}
.sel-handle.nw { top: -4px; left: -4px; cursor: nw-resize; }
.sel-handle.ne { top: -4px; right: -4px; cursor: ne-resize; }
.sel-handle.sw { bottom: -4px; left: -4px; cursor: sw-resize; }
.sel-handle.se { bottom: -4px; right: -4px; cursor: se-resize; }
.sel-handle.n  { top: -4px; left: calc(50% - 4px); cursor: n-resize; }
.sel-handle.s  { bottom: -4px; left: calc(50% - 4px); cursor: s-resize; }
.sel-handle.e  { right: -4px; top: calc(50% - 4px); cursor: e-resize; }
.sel-handle.w  { left: -4px; top: calc(50% - 4px); cursor: w-resize; }

/* Logo overlay */
#logoWrapper {
  position: absolute;
  z-index: 20;
  display: none;
  cursor: grab;
  user-select: none;
}
#logoWrapper.logo-selected {
  outline: 2px dashed var(--accent);
  outline-offset: 2px;
}
#logoWrapper.logo-dragging {
  cursor: grabbing;
}
#logoOverlay {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
}
#logoResizeHandle {
  position: absolute;
  bottom: -5px;
  right: -5px;
  width: 10px;
  height: 10px;
  background: var(--accent);
  cursor: se-resize;
  display: none;
  z-index: 21;
}
#logoWrapper.logo-selected #logoResizeHandle {
  display: block;
}

/* ── TEXT CONTROLS PANEL ── */
.controls-panel {
  width: 380px;
  flex-shrink: 0;
  overflow-y: auto;
  border-left: 1px solid var(--border);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.controls-title {
  font-family: var(--font);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.4);
  margin-bottom: 4px;
}

.block-accordion {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.block-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s;
}
.block-header:hover { background: var(--card2); }

.block-name {
  flex: 1;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 700;
  color: #ffffff;
}

.block-preview-text {
  font-size: 11px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 120px;
}

.visibility-toggle {
  border: none;
  border-radius: 20px;
  cursor: pointer;
  padding: 3px 9px;
  font-size: 10px;
  font-weight: 700;
  line-height: 1.4;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  flex-shrink: 0;
}
.visibility-toggle.visible-state {
  background: var(--accent);
  color: #fff;
}
.visibility-toggle.hidden-state {
  background: #333;
  color: #888;
}

.reset-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 4px;
  line-height: 1;
  font-size: 15px;
  transition: color 0.1s, transform 0.15s;
}
.reset-btn:hover { color: var(--text); transform: rotate(-30deg); }

.add-layer-btn {
  width: 100%;
  margin-top: 10px;
  padding: 8px;
  background: none;
  border: 1px dashed var(--border);
  border-radius: 6px;
  color: var(--accent);
  font-size: 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.add-layer-btn:hover { background: var(--card2); border-color: var(--accent); }

.delete-block-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  font-size: 14px;
  transition: color 0.1s;
}
.delete-block-btn:hover { color: #e55; }

.upload-cancel-btn {
  display: inline-flex;
  align-items: center;
  background: none;
  border: none;
  color: #777;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  padding: 0 4px;
  line-height: 1;
  vertical-align: middle;
  transition: color 0.1s;
  flex-shrink: 0;
}
.upload-cancel-btn:hover { color: #ccc; }

.weight-btns {
  display: flex;
  gap: 4px;
}
.weight-btn {
  flex: 1;
  background: var(--card2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 12px;
  padding: 4px 6px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 600;
  transition: all 0.1s;
}
.weight-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

#centerGuideLine {
  position: absolute;
  left: 50%;
  top: 0;
  height: 100%;
  width: 0;
  border-left: 1px dashed rgba(255,255,255,0.6);
  z-index: 50;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.4s;
}
#centerGuideLine.visible {
  opacity: 1;
  transition: opacity 0s;
}

.chevron {
  font-size: 10px;
  color: var(--muted);
  transition: transform 0.2s;
}
.block-accordion.open .chevron { transform: rotate(180deg); }

.block-body {
  display: none;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border);
  background: #181818;
}
.block-accordion.open .block-body { display: block; }

.ctrl-row {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-bottom: 8px;
}
.ctrl-row:last-child { margin-bottom: 0; }

.ctrl-row label {
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.ctrl-row label span.val {
  font-weight: 700;
  color: #aaa;
  font-variant-numeric: tabular-nums;
}

.ctrl-row input[type=text],
.ctrl-row select {
  background: var(--card2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  padding: 5px 8px;
  font-size: 12px;
  font-family: var(--font);
  width: 100%;
}
.ctrl-row input[type=text]:focus,
.ctrl-row select:focus {
  outline: none;
  border-color: var(--accent);
}

.ctrl-row textarea {
  background: var(--card2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  padding: 5px 8px;
  font-size: 12px;
  font-family: monospace;
  width: 100%;
  resize: vertical;
  min-height: 90px;
  line-height: 1.5;
}
.ctrl-row textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.ctrl-row input[type=range] {
  width: 100%;
  accent-color: var(--accent);
  height: 20px;
}

.ctrl-row input[type=color] {
  width: 100%;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--card2);
  cursor: pointer;
  padding: 2px;
}

.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.align-btns {
  display: flex;
  gap: 4px;
}
.align-btn {
  flex: 1;
  background: var(--card2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 4px;
  padding: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.1s;
}
.align-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Logo controls */
#logoControls {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px 12px;
  display: none;
}
#logoControls.visible { display: block; }

/* Error toast */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: #ef4444;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  z-index: 9999;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  max-width: 340px;
}
.toast.show { opacity: 1; transform: translateY(0); }

/* Scrollbar styling */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #444; }

/* ── TABLET: 768px – 1023px ────────────────────────────────── */
@media (min-width: 768px) and (max-width: 1023px) {
  .left-panel {
    width: 340px;
    min-width: 280px;
  }
  .right-panel {
    overflow-y: auto;
    overflow: visible;
  }
  .preview-header {
    padding: 16px 16px 0;
  }
  .preview-area {
    flex-direction: column;
    overflow: visible;
    padding: 16px;
    gap: 16px;
  }
  .preview-canvas-wrapper {
    overflow: visible;
    padding: 0;
    align-items: flex-start;
  }
  #previewCanvas {
    max-width: 480px;
  }
  .controls-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    overflow: visible;
    padding: 16px;
  }
}

/* ── MOBILE: max-width 767px ────────────────────────────────── */
@media (max-width: 767px) {
  html, body {
    height: auto;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .app {
    flex-direction: column;
    height: auto;
    overflow: visible;
    overflow-x: hidden;
  }
  .left-panel {
    width: 100%;
    min-width: 0;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow: visible;
  }
  .right-panel {
    overflow: visible;
    flex-direction: column;
  }
  .preview-header {
    padding: 16px 16px 8px;
  }
  .preview-area {
    flex-direction: column;
    overflow: visible;
    padding: 0 16px 16px;
    gap: 16px;
  }
  .preview-canvas-wrapper {
    width: 100%;
    min-width: 0;
    padding: 0;
    overflow: visible;
    align-items: flex-start;
  }
  #previewCanvas {
    max-width: 100%;
  }
  .controls-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    overflow: visible;
    padding: 16px;
  }
  .style-cards {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x mandatory;
    padding-bottom: 4px;
    margin-bottom: 12px;
  }
  .style-card {
    flex: 0 0 auto;
    min-width: 120px;
    scroll-snap-align: start;
  }
  .two-col {
    grid-template-columns: 1fr;
  }
  .btn-generate {
    width: 100%;
  }
  .ctrl-row input[type=range] {
    width: 100%;
  }
}
