/* ───── Reset & Base ───── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Helvetica Neue', sans-serif;
  background: #111;
  color: #fff;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

/* ───── Layout ───── */
.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  max-width: 500px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ───── Header ───── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 8px;
  flex-shrink: 0;
}
header h1 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
  opacity: 0.9;
}
header .actions {
  display: flex;
  gap: 8px;
}
header button {
  background: rgba(255,255,255,0.1);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
header button:active { background: rgba(255,255,255,0.2); }

/* ───── Canvas Area ───── */
.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  min-height: 0;
  position: relative;
}
.canvas {
  width: 100%;
  max-width: 400px;
  aspect-ratio: 1;
  background: #1a1a1a;
  border-radius: 16px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
}
.cell {
  position: absolute;
  background: #222;
  background-size: cover;
  background-position: center;
  /* border-radius set dynamically via inline style (corner radius slider) */
  cursor: pointer;
  transition: opacity 0.2s, transform 0.15s;
  overflow: hidden;
}
.cell.has-photo {
  cursor: grab;
}
.cell.has-photo:active {
  cursor: grabbing;
}
.cell.add-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  color: rgba(255,255,255,0.2);
  border: 2px dashed rgba(255,255,255,0.15);
  transition: border-color 0.2s, color 0.2s;
}
.cell.add-btn:active {
  border-color: rgba(255,255,255,0.4);
  color: rgba(255,255,255,0.4);
}
.cell .remove-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  border: none;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.cell.has-photo:hover .remove-btn,
.cell.has-photo:active .remove-btn {
  opacity: 1;
}
.cell.dragging {
  opacity: 0.5;
  transform: scale(0.95);
  z-index: 10;
}
.cell.drag-over {
  outline: 2px solid #fff;
  outline-offset: -2px;
}

/* ───── Toolbar ───── */
.toolbar {
  flex-shrink: 0;
  padding: 8px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.toolbar-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  padding: 2px 0;
}
.toolbar-row::-webkit-scrollbar { display: none; }

.layout-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: rgba(255,255,255,0.08);
  border: 1.5px solid transparent;
  color: rgba(255,255,255,0.5);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px;
  transition: all 0.2s;
}
.layout-btn.active {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.4);
  color: #fff;
}
.layout-btn:active { background: rgba(255,255,255,0.2); }
.layout-btn .mini-grid {
  display: grid;
  width: 100%;
  height: 100%;
  gap: 1.5px;
}
.layout-btn .mini-grid.asym {
  display: block;
  position: relative;
  gap: 0;
}
.layout-btn .mini-cell {
  background: currentColor;
  border-radius: 1px;
  opacity: 0.6;
}
.layout-btn .mini-cell.abs {
  position: absolute;
  border-radius: 1px;
  background: currentColor;
  opacity: 0.6;
  box-sizing: border-box;
}
.layout-btn.active .mini-cell { opacity: 1; }

.slider-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}
.slider-group label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  opacity: 0.5;
  flex-shrink: 0;
}
.slider-group input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  border-radius: 2px;
  background: rgba(255,255,255,0.2);
  outline: none;
  min-width: 0;
}
.slider-group input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  cursor: pointer;
  border: none;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.slider-group input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  cursor: pointer;
  border: none;
}
.slider-group .value {
  font-size: 13px;
  font-weight: 600;
  min-width: 28px;
  text-align: center;
  opacity: 0.7;
}

.color-picker {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1.5px solid rgba(255,255,255,0.2);
  padding: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  background: none;
  flex-shrink: 0;
}
.color-picker::-webkit-color-swatch-wrapper { padding: 2px; }
.color-picker::-webkit-color-swatch {
  border: none;
  border-radius: 5px;
}
.color-picker::-moz-color-swatch {
  border: none;
  border-radius: 5px;
}

/* ───── Bottom bar ───── */
.bottom-bar {
  flex-shrink: 0;
  padding: 0 16px 20px;
  display: flex;
  gap: 10px;
}
.btn-primary {
  flex: 1;
  height: 48px;
  border-radius: 14px;
  border: none;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s;
  background: #fff;
  color: #111;
}
.btn-primary:active { transform: scale(0.97); opacity: 0.9; }
.btn-secondary {
  flex: 1;
  height: 48px;
  border-radius: 14px;
  border: 1.5px solid rgba(255,255,255,0.2);
  background: transparent;
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s;
}
.btn-secondary:active { background: rgba(255,255,255,0.1); }

/* ───── File input hidden ───── */
#fileInput { display: none; }

/* ───── Toast ───── */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(255,255,255,0.15);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 12px 24px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  transition: all 0.4s;
  pointer-events: none;
  white-space: nowrap;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ───── Empty state ───── */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: rgba(255,255,255,0.2);
  pointer-events: none;
}
.empty-state .icon {
  font-size: 48px;
  opacity: 0.4;
}
.empty-state p {
  font-size: 14px;
  font-weight: 500;
}

/* ───── Text section ───── */
.text-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 2px 0;
}
.text-input-row {
  display: flex;
  gap: 6px;
}
.text-input-row input[type="text"] {
  flex: 1;
  height: 36px;
  border-radius: 10px;
  border: 1.5px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.08);
  color: #fff;
  font-size: 14px;
  padding: 0 12px;
  outline: none;
  font-family: inherit;
  min-width: 0;
}
.text-input-row input[type="text"]::placeholder {
  color: rgba(255,255,255,0.3);
}
.text-input-row input[type="text"]:focus {
  border-color: rgba(255,255,255,0.3);
}
.btn-small {
  height: 36px;
  padding: 0 14px;
  border-radius: 10px;
  border: none;
  background: #fff;
  color: #111;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}
.btn-small:active { transform: scale(0.95); opacity: 0.9; }

.text-controls-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.pos-btns {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
.text-pos-btn {
  height: 28px;
  padding: 0 10px;
  border-radius: 7px;
  border: 1px solid rgba(255,255,255,0.15);
  background: transparent;
  color: rgba(255,255,255,0.5);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.text-pos-btn.active {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.4);
  color: #fff;
}
.text-pos-btn:active { background: rgba(255,255,255,0.1); }

.text-list {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  min-height: 28px;
  align-items: center;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  border-radius: 14px;
  border: 1.5px solid rgba(255,255,255,0.3);
  background: rgba(255,255,255,0.08);
  color: #fff;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chip:active { background: rgba(255,255,255,0.15); }
.chip-remove { font-size: 10px; opacity: 0.5; }
.chip-empty {
  font-size: 11px;
  color: rgba(255,255,255,0.2);
  font-weight: 500;
}

/* ───── Text overlays on canvas ───── */
.text-overlay {
  position: absolute;
  z-index: 20;
  pointer-events: auto;
  cursor: pointer;
  line-height: 1.2;
  letter-spacing: -0.02em;
  word-break: break-word;
  padding: 4px 0;
}
.text-overlay:active {
  opacity: 0.6;
}

/* ───── Responsive ───── */
@media (max-width: 420px) {
  header { padding: 8px 12px 4px; }
  header h1 { font-size: 16px; }
  .canvas-wrap { padding: 4px 12px; }
  .toolbar { padding: 4px 12px 12px; }
  .bottom-bar { padding: 0 12px 16px; }
  .layout-btn { width: 38px; height: 38px; }
  .btn-primary, .btn-secondary { height: 44px; font-size: 14px; }
}

/* ───── Safe area for notch ───── */
@supports (padding: max(0px)) {
  .app { padding: max(env(safe-area-inset-top), 0px) max(env(safe-area-inset-right), 0px) max(env(safe-area-inset-bottom), 0px) max(env(safe-area-inset-left), 0px); }
}