/* Tasarim editoru - marka yesili + beyaz. Mobil oncelikli. */
:root {
  --primary: #21865f;
  --primary-dark: #176b4b;
  --primary-soft: #eaf7ef;
  --primary-line: #cbe8d9;
  --bg: #f4f7f5;
  --panel: #ffffff;
  --text: #1f2937;
  --muted: #667085;
  --line: #dce5df;
  --danger: #c24137;
  --shadow: 0 10px 28px rgba(23, 60, 45, 0.12);
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
  overscroll-behavior-y: none;
  -webkit-touch-callout: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

.editor {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Ust bar ---------- */
.top-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  background: linear-gradient(135deg, #21865f 0%, #1a7452 55%, #176b4b 100%);
  color: #fff;
  box-shadow: 0 4px 16px rgba(23, 60, 45, 0.22);
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: .2px;
}

.brand-logo {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: rgba(255, 255, 255, .15);
  border: 1px solid rgba(255, 255, 255, .25);
  color: #fff;
  display: grid;
  place-items: center;
}
.brand-logo svg { width: 19px; height: 19px; display: block; }

.top-spacer { flex: 1; }

.icon-btn {
  border: 1px solid rgba(255, 255, 255, .22);
  background: rgba(255, 255, 255, .14);
  color: #fff;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background .15s, transform .1s;
}
.icon-btn svg { width: 19px; height: 19px; display: block; }
.icon-btn:active { background: rgba(255, 255, 255, .3); transform: scale(.95); }
.icon-btn[disabled] { opacity: .38; }

.top-download {
  border: 0;
  background: #fff;
  color: var(--primary-dark);
  font-weight: 700;
  font-size: 14px;
  letter-spacing: .2px;
  padding: 9px 15px;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 7px;
  box-shadow: 0 3px 10px rgba(9, 40, 28, .22);
  transition: transform .1s;
}
.top-download svg { width: 18px; height: 18px; display: block; }
.top-download:active { transform: scale(.96); }

/* ---------- Sayfa alani ---------- */
.stage {
  flex: 1 1 auto;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.page-head {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}

.page-head b { color: var(--text); }

.page-nav { display: flex; gap: 6px; align-items: center; }

.mini-btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 9px;
  padding: 6px 10px;
  font-size: 13px;
  cursor: pointer;
}
.mini-btn:active { background: var(--primary-soft); }
.mini-btn.danger { color: var(--danger); border-color: #f0cfcb; }

/* A4 sayfa: 210 x 297 mm */
.page {
  position: relative;
  /* A4 orani BOZULMADAN ekrana sigsin: genislik, kalan yukseklikten
     hesaplanir (210/297 = 0.7071). Boylece sayfa hep tam gorunur ve
     kaydirmaya gerek kalmaz. */
  width: min(100%, 560px);                                    /* dvh desteklemeyen eski tarayicilar */
  width: min(100%, 560px, calc((100dvh - 232px) * 0.7071));
  aspect-ratio: 210 / 297;
  background: #fff;
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
  /* TARAYICI JESTLERI KAPALI: asagi surukleyince "yenilemek icin cek"
     tetiklenip calisma kayboluyordu. Sayfa zaten ekrana sigacak sekilde
     olceklendigi icin burada kaydirmaya gerek yok. */
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

/* aspect-ratio desteklemeyen eski iOS Safari icin yedek A4 orani. */
@supports not (aspect-ratio: 210 / 297) {
  .page {
    height: 0;
    padding-bottom: 141.42%;
  }
}

.page-inner {
  position: absolute;
  inset: 0; top: 0; left: 0; right: 0; bottom: 0;
  /* Sayfa yuzeyi bastan GPU katmanina alinir: ilk dokunusta katman
     olusturma maliyeti (tek uzun kare) ortadan kalkar. */
  transform: translateZ(0);
}

.el {
  position: absolute;
  transform-origin: center center;
  display: grid;
  place-items: center;
  touch-action: none;
}

/* GPU katmanina ALMA ZAMANI onemli: eger katman ilk parmak hareketinde
   olusturulursa, o tek kare pahali oluyor (olcumde 65 ms'lik tek sicrama).
   Bu yuzden oge SECILDIGI anda katmana aliniyor; hareket basladiginda
   hazir oluyor ve tum kareler esit hizda akiyor. */
.el.selected {
  will-change: transform;
  backface-visibility: hidden;
}

.el.dragging {
  will-change: transform;
  z-index: 20;
}

/* Cerceve icindeki fotograf, secili oldugu surece KENDI GPU katmaninda durur.
   Boylece kaydirirken sadece katman kaydirilir; cevresindeki cerceve, kenarlik
   ve tutamaclar yeniden cizilmez. Yalnizca secili ogeye uygulanir, bu yuzden
   bellek maliyeti tek katmanla sinirlidir. */
.el.selected .media img,
.el.dragging .media img {
  will-change: transform;
}

/* Gorsel, KENDI kirpma kutusunun icinde durur. Boylece hem grid ogesinin
   "min-width: auto" tasmasi (Safari'de gorsel cerceveden tasiyordu) engellenir,
   hem de silme/boyutlandirma tutamaclari kirpilmadan disarida kalabilir. */
.el .media {
  contain: paint;
  touch-action: none;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  display: block;
  border-radius: inherit;
}

.el img {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.el.el-sticker img { object-fit: contain; }

.el.el-emoji, .el.el-text {
  line-height: 1.15;
  text-align: center;
  white-space: pre-wrap;
  word-break: break-word;
  padding: 2px;
}

/* Yazinin ic kutusu: etiket arka plani tam yazi kadar olsun. */
.el.el-text .txt {
  display: inline-block;
  max-width: 100%;
  line-height: 1.15;
  white-space: pre-wrap;
  word-break: break-word;
}

.el.el-shape svg { width: 100%; height: 100%; display: block; }

.el.selected { outline: 2px dashed var(--primary); outline-offset: 2px; }

.el.slot {
  border: 2px dashed var(--primary-line);
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-size: 13px;
  border-radius: 6px;
  text-align: center;
  padding: 6px;
}

.handle {
  position: absolute;
  touch-action: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--primary);
  color: var(--primary-dark);
  display: grid;
  place-items: center;
  font-size: 13px;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(0, 0, 0, .18);
  touch-action: none;
  z-index: 30;
  padding: 0;
}

/* Silme: ustte ortada (kose tutamaclariyla cakismasin) */
.handle.del {
  border-color: var(--danger);
  color: var(--danger);
  left: 50%;
  top: -38px;
  margin-left: -14px;
}

/* 4 koseden boyutlandirma */
.handle.corner.nw { left: -14px; top: -14px; cursor: nwse-resize; }
.handle.corner.ne { right: -14px; top: -14px; cursor: nesw-resize; }
.handle.corner.sw { left: -14px; bottom: -14px; cursor: nesw-resize; }
.handle.corner.se { right: -14px; bottom: -14px; cursor: nwse-resize; }

.el-hint {
  position: absolute;
  left: 50%;
  bottom: -30px;
  transform: translateX(-50%);
  background: rgba(31, 41, 55, .85);
  color: #fff;
  font-size: 10px;
  line-height: 1;
  padding: 5px 8px;
  border-radius: 999px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 29;
}

.counter {
  float: right;
  font-weight: 400;
  color: var(--muted);
  font-size: 12px;
}

.range {
  width: 100%;
  accent-color: var(--primary);
  height: 32px;
}

/* ---------- Cerceve sekilleri (kolaj maskeleri) ---------- */
.shape-round .media, .el.slot.shape-round { border-radius: 8%; }
.shape-squircle .media, .el.slot.shape-squircle { border-radius: 24%; }
.shape-circle .media, .el.slot.shape-circle { clip-path: ellipse(50% 50% at 50% 50%); }
.shape-heart .media, .el.slot.shape-heart { clip-path: url(#clip-heart); }
.shape-blob .media, .el.slot.shape-blob { clip-path: url(#clip-blob); }
.shape-star .media, .el.slot.shape-star {
  clip-path: polygon(50% 2%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}
.shape-hex .media, .el.slot.shape-hex {
  clip-path: polygon(25% 3%, 75% 3%, 100% 50%, 75% 97%, 25% 97%, 0% 50%);
}

/* Sablon kucuk onizlemesindeki hucreler de ayni sekli gostersin. */
.mini-cell.shape-round { border-radius: 12%; }
.mini-cell.shape-squircle { border-radius: 26%; }
.mini-cell.shape-circle { clip-path: ellipse(50% 50% at 50% 50%); }
.mini-cell.shape-heart { clip-path: url(#clip-heart); }
.mini-cell.shape-blob { clip-path: url(#clip-blob); }
.mini-cell.shape-star { clip-path: polygon(50% 2%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); }
.mini-cell.shape-hex { clip-path: polygon(25% 3%, 75% 3%, 100% 50%, 75% 97%, 25% 97%, 0% 50%); }

/* Maskeli bos cerceve: kesik cizgi kirpilacagi icin dolgu ile gosterilir. */
.el.slot.masked {
  border: 0;
  background: var(--primary-line);
  color: var(--primary-dark);
}

/* ---------- Yazi paneli ek kontroller ---------- */
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-row button {
  flex: 1 1 auto;
  min-width: 54px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  padding: 9px 8px;
  font-size: 14px;
  cursor: pointer;
}
.btn-row button.on {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.preview-shell {
  border: 1px dashed var(--primary-line);
  border-radius: 12px;
  background: var(--primary-soft);
  padding: 10px;
  max-height: 200px;
  overflow: auto;
}

.text-preview {
  min-height: 44px;
  text-align: center;
  line-height: 1.15;
  word-break: break-word;
  white-space: pre-wrap;
  display: inline-block;
  width: 100%;
}

/* ---------- Alt arac cubugu ---------- */
.tools {
  flex: 0 0 auto;
  /* backdrop-filter KALDIRILDI: mobil tarayicida sayfadaki her hareket
     arka planin yeniden bulaniklastirilmasini tetikleyip kare dusuruyordu. */
  background: #ffffff;
  border-top: 1px solid rgba(23, 60, 45, .10);
  box-shadow: 0 -8px 24px rgba(16, 36, 28, .07);
  padding: 7px 10px calc(9px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}

.tool {
  position: relative;
  border: 0;
  background: transparent;
  color: #5b6874;
  border-radius: 16px;
  padding: 9px 4px 7px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .1px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  transition: color .16s ease, background .16s ease, transform .1s ease;
  -webkit-tap-highlight-color: transparent;
}

.tool .ico {
  width: 25px;
  height: 25px;
  display: block;
}
.tool .ico svg { width: 100%; height: 100%; display: block; }
.tool .lbl { line-height: 1; }

.tool:active { transform: scale(.95); }

.tool.active {
  color: var(--primary-dark);
  background: linear-gradient(180deg, rgba(33, 134, 95, .12), rgba(33, 134, 95, .06));
}
.tool.active::after {
  content: '';
  position: absolute;
  bottom: 2px;
  left: 50%;
  width: 18px;
  height: 3px;
  border-radius: 3px;
  background: var(--primary);
  transform: translateX(-50%);
}

@media (hover: hover) {
  .tool:hover { color: var(--primary-dark); background: var(--primary-soft); }
}

/* ---------- Segmentli secici (Stickerlar) ---------- */
.segmented {
  display: flex;
  gap: 4px;
  background: #eef2f0;
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 12px;
}
.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  color: #5b6874;
  font-size: 13px;
  font-weight: 600;
  padding: 9px 6px;
  border-radius: 9px;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.segmented button.on {
  background: #fff;
  color: var(--primary-dark);
  box-shadow: 0 1px 3px rgba(16, 36, 28, .14);
}

/* ---------- Panel (sheet) ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(16, 36, 28, .45);
  display: none;
  z-index: 40;
}
.sheet-backdrop.open { display: block; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 72vh;
  background: var(--panel);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -10px 30px rgba(16, 36, 28, .2);
  z-index: 50;
  transform: translateY(102%);
  transition: transform .18s ease-out;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.open { transform: translateY(0); }

.sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 15px; }
.sheet-head .close {
  margin-left: auto;
  border: 0;
  background: var(--primary-soft);
  color: var(--primary-dark);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
}

.sheet-body { overflow: auto; padding: 12px 14px 18px; }

.chip-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px 2px 10px;
  margin-bottom: 10px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.chip-row::-webkit-scrollbar { height: 4px; }
.chip-row::-webkit-scrollbar-thumb { background: #d3ddd7; border-radius: 4px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  border: 1.5px solid #dde5e0;
  background: #f3f6f4;
  color: #3f4b55;
  border-radius: 999px;
  padding: 9px 15px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .1px;
  white-space: nowrap;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, border-color .15s ease, box-shadow .15s ease, transform .1s ease;
}
.chip:active { transform: scale(.96); }

.chip.active {
  background: linear-gradient(180deg, #2a9a6e 0%, #21865f 100%);
  color: #fff;
  border-color: #1b7050;
  box-shadow: 0 3px 10px rgba(33, 134, 95, .32);
}

.chip-count {
  margin-left: 7px;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  background: rgba(31, 41, 55, .09);
  color: inherit;
  border-radius: 999px;
  padding: 3px 7px;
}
.chip.active .chip-count { background: rgba(255, 255, 255, .26); }

.grid-emoji {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  gap: 6px;
}
.grid-emoji button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  aspect-ratio: 1;
  font-size: 26px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.grid-emoji button:active { background: var(--primary-soft); }
.grid-emoji button svg { width: 60%; height: 60%; }
.grid-emoji button img { width: 74%; height: 74%; object-fit: contain; }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field textarea, .field input[type="text"], .field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
.field textarea { min-height: 72px; resize: vertical; }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line);
  cursor: pointer;
}
.swatch.active { box-shadow: 0 0 0 3px var(--primary); }

.btn {
  border: 0;
  background: var(--primary);
  color: #fff;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  width: 100%;
}
.btn:active { background: var(--primary-dark); }
.btn.secondary { background: var(--primary-soft); color: var(--primary-dark); }

.layout-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  gap: 10px;
}
.layout-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  padding: 8px;
  cursor: pointer;
}
.layout-card:active { border-color: var(--primary); }
.layout-card .mini {
  aspect-ratio: 210 / 297;
  display: grid;
  gap: 3px;
  background: var(--primary-soft);
  border-radius: 6px;
  padding: 3px;
}
.layout-card .mini i { background: var(--primary-line); border-radius: 3px; display: block; }
.layout-card small { display: block; text-align: center; margin-top: 6px; font-size: 11px; color: var(--muted); }

.hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
  margin: 6px 0 0;
}

/* ---------- Indirme onizleme ---------- */
.preview-wrap { text-align: center; }
.preview-wrap img {
  max-width: 100%;
  max-height: 46vh;
  border-radius: 10px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 92px;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 90;
  max-width: 88vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Genis ekran ---------- */
@media (min-width: 820px) {
  .stage { padding: 22px; }
  .page { width: min(100%, 620px); }
  .tools {
    grid-template-columns: repeat(6, minmax(0, 110px));
    justify-content: center;
    gap: 10px;
  }
  .tool { font-size: 12px; padding: 10px 6px; }
  .sheet { left: 50%; transform: translate(-50%, 102%); width: 560px; border-radius: 18px 18px 0 0; }
  .sheet.open { transform: translate(-50%, 0); }
}

/* Indirme sirasinda buton mesgul gorunumu */
.top-download.busy { opacity: .65; pointer-events: none; }
.top-download.busy svg { animation: dl-pulse 1s ease-in-out infinite; }
@keyframes dl-pulse {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(2px); }
}

/* Cikartma kutucuklari: gorsel gelene kadar bos gorunmesin */
.grid-emoji button img {
  background: repeating-conic-gradient(#f5f8f6 0% 25%, #ffffff 0% 50%) 50% / 10px 10px;
  border-radius: 8px;
}

/* ---------- Kolaj: sekil secici (metin yerine sekil ikonlari) ---------- */
.shape-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.shape-pick {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: 1.5px solid #dde5e0;
  background: #f3f6f4;
  color: #56636e;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  transition: background .15s ease, color .15s ease, border-color .15s ease, box-shadow .15s ease, transform .1s ease;
}
.shape-pick svg { width: 23px; height: 23px; display: block; fill: currentColor; }
.shape-pick:active { transform: scale(.94); }
.shape-pick.active {
  background: linear-gradient(180deg, #2a9a6e 0%, #21865f 100%);
  color: #fff;
  border-color: #1b7050;
  box-shadow: 0 3px 10px rgba(33, 134, 95, .32);
}
.shape-pick.all {
  width: auto;
  padding: 0 15px;
  font-size: 13.5px;
  font-weight: 700;
  letter-spacing: .1px;
}

.mini-apply {
  margin-top: 10px;
  border: 1.5px solid var(--primary-line);
  background: #fff;
  color: var(--primary-dark);
  border-radius: 11px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.mini-apply:active { background: var(--primary-soft); }

/* Son savunma: cerceveli fotograf her kosulda kutuyu doldursun (bos alan kalmasin) */
/* KRITIK: .el img kuralindaki max-width/max-height: 100%, cerceveyi doldurmak
   icin verilen olculeri (orn. genislik %154) geri kirpiyor ve fotograf ya
   eziliyor ya da bosluk kaliyordu. Cerceveli fotografta bu sinirlar kalkar,
   alt sinir olarak da kutunun tamami zorunlu kilinir. */
.el .media img, .el .media svg { touch-action: none; -webkit-user-drag: none; user-select: none; }

.el.el-image .media img {
  max-width: none;
  max-height: none;
  min-width: 100%;
  min-height: 100%;
}

/* Mobilde ipucu metni kisa tutulur: A4 sayfaya daha cok yer kalir. */
@media (max-width: 560px) {
  .stage .hint { font-size: 11.5px; line-height: 1.35; margin: 2px 0 0; }
  .stage { padding: 10px 12px 12px; gap: 10px; }
}
