:root {
  --bg: #07111f;
  --panel: #101a2b;
  --panel2: #162235;
  --border: rgba(255,255,255,.11);
  --text: #f8fafc;
  --muted: #9fb0c8;
  --red: #dc2626;
  --red2: #991b1b;
  --green: #16a34a;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: radial-gradient(circle at top, #172033, #070b14 65%);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}
a { color: inherit; text-decoration: none; }
code { background: rgba(255,255,255,.08); padding: 3px 6px; border-radius: 6px; color: #dbeafe; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 14px 22px;
  background: rgba(7, 11, 20, .94);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(10px);
}
.topbar.compact { padding: 12px 20px; }
.brand-line { display: flex; align-items: center; gap: 12px; }
.brand-badge {
  width: 54px;
  height: 54px;
  border-radius: 15px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #e22d2d, #7f1111);
  font-weight: 900;
  box-shadow: 0 12px 28px rgba(220,38,38,.28);
}
.brand-line h1, .login-brand h1 { margin: 0; font-size: 24px; }
.brand-line p, .login-brand p { margin: 4px 0 0; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 10px; color: var(--muted); }

.page { padding: 18px; max-width: 1900px; margin: 0 auto; }
.compact-page { padding-top: 12px; }
.panel, .posts-panel {
  background: linear-gradient(180deg, rgba(16,26,43,.98), rgba(10,16,28,.98));
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 20px 50px rgba(0,0,0,.22);
}
.import-panel { padding: 12px 16px; }
.section-head {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-start;
  margin-bottom: 14px;
}
.compact-head { align-items: center; margin-bottom: 4px; }
.section-head h2 { margin: 0; font-size: 22px; }
.section-head p { margin: 5px 0 0; color: var(--muted); }
.small-hint { margin: 8px 0 0; font-size: 13px; }

.posts-toolbar {
  position: sticky;
  top: 79px;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  margin: -6px -6px 14px;
  background: rgba(16,26,43,.94);
  border: 1px solid var(--border);
  border-radius: 14px;
  backdrop-filter: blur(8px);
}
.posts-toolbar span { color: var(--muted); font-size: 13px; }

label { display: block; font-weight: 700; color: #dbeafe; margin: 9px 0 5px; }
input, textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  background: #07111f;
  color: var(--text);
  border-radius: 11px;
  padding: 9px 10px;
  outline: none;
}
input[type="color"] { height: 42px; padding: 5px; cursor: pointer; }
input[type="file"] { background: rgba(255,255,255,.04); }
input[type="range"] { padding: 0; }
textarea {
  min-height: 74px;
  resize: vertical;
  line-height: 1.65;
  font-family: 'Jameel Noori Nastaleeq', 'Noto Nastaliq Urdu', 'Noto Naskh Arabic', Arial, sans-serif;
  font-size: 15px;
}
.mini-label { font-size: 12px; margin-top: 7px; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; }

.btn {
  border: 1px solid var(--border);
  background: #1f2937;
  color: var(--text);
  padding: 10px 14px;
  border-radius: 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 800;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.08); }
.btn.primary { background: linear-gradient(135deg, var(--red), var(--red2)); border-color: rgba(255,255,255,.18); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 8px; font-size: 12px; }
.btn.big { padding: 14px 22px; font-size: 16px; }
.file-btn { margin: 0; }
.bulk-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.alert, .success {
  border-radius: 14px;
  padding: 13px 16px;
  margin-bottom: 12px;
  font-weight: 800;
}
.alert { color: #fecaca; background: rgba(220,38,38,.18); border: 1px solid rgba(248,113,113,.45); }
.success { color: #bbf7d0; background: rgba(22,163,74,.18); border: 1px solid rgba(74,222,128,.4); }
.hint { color: var(--muted); }

.posts-grid.compact-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(235px, 1fr));
  gap: 12px;
}
.post-card {
  background: linear-gradient(180deg, #142033, #0b1220);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px;
}
.compact-card { min-width: 0; }
.post-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; gap: 8px; }
.post-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.post-head h3 { margin: 0; font-size: 16px; }

.mini-preview {
  --ratio: calc(var(--preview-h) / var(--preview-w));
  width: 100%;
  aspect-ratio: var(--preview-w) / var(--preview-h);
  position: relative;
  overflow: hidden;
  border-radius: 12px;
  background: #0a0a0a;
  border: 1px solid var(--border);
  margin-bottom: 8px;
  user-select: none;
  touch-action: none;
}
.compact-preview { max-height: 280px; }
.post-card .mini-preview { margin-bottom: 10px; }
.preview-photo-wrap { position:absolute; inset:0; overflow:hidden; cursor: grab; z-index: 1; }
.preview-photo-wrap:active { cursor: grabbing; }
.preview-photo { position:absolute; inset:0; width:100%; height:100%; object-fit:cover; transform-origin:center; display:block; pointer-events:none; }
.preview-photo:not([src]) { display:none; }
.preview-template { position:absolute; inset:0; width:100%; height:100%; object-fit:fill; z-index:2; pointer-events:none; }
.preview-text {
  position:absolute;
  z-index:3;
  text-align:center;
  direction:rtl;
  cursor: move;
  pointer-events:auto;
  max-width: 98%;
}
.p-line {
  display:block;
  line-height:1.35;
  font-weight:900;
  paint-order:stroke fill;
  white-space:nowrap;
  overflow:visible;
  pointer-events:none;
}
.image-resize-handle,
.text-resize-handle {
  position:absolute;
  width:16px;
  height:16px;
  border-radius:4px;
  border: 2px solid #111827;
  box-shadow: 0 3px 10px rgba(0,0,0,.35);
  cursor: nwse-resize;
  z-index:4;
}
.image-resize-handle { background: #fbbf24; }
.text-resize-handle { background: #38bdf8; }
.top-handle { right: 8px; top: 8px; }
.bottom-handle { right: -18px; bottom: -10px; }

.fine-controls { margin-top: 8px; border: 1px solid var(--border); border-radius: 12px; padding: 8px; background: rgba(255,255,255,.03); }
.fine-controls summary { cursor: pointer; font-weight: 800; color: #dbeafe; }
.adjust-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.adjust-grid label { font-size: 11px; color: var(--muted); }
.tiny-adjust { margin-top: 8px; }
.move-buttons { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.input-error { border-color: #ef4444 !important; box-shadow: 0 0 0 3px rgba(239,68,68,.15); }

.download-bar {
  position: sticky;
  bottom: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  background: rgba(7, 11, 20, .95);
  border: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  padding: 14px 18px;
  box-shadow: 0 -15px 40px rgba(0,0,0,.32);
}
.download-bar span { display:block; color: var(--muted); margin-top: 4px; }

.preview-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(220px, 1fr));
  gap: 14px;
}
.final-card {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px;
}
.final-meta { display:flex; justify-content:space-between; align-items:center; gap:10px; margin-bottom: 8px; }
.final-card img { display:block; width:100%; border-radius: 10px; background:#000; margin-bottom: 8px; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.62);
  z-index: 100;
  display: none;
  padding: 22px;
  overflow: auto;
}
.modal-backdrop.open { display: block; }
.settings-modal {
  max-width: 1480px;
  margin: 0 auto;
  background: linear-gradient(180deg, rgba(16,26,43,.99), rgba(10,16,28,.99));
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 18px;
  box-shadow: 0 35px 90px rgba(0,0,0,.55);
}
.modal-head { display:flex; justify-content:space-between; align-items:flex-start; gap:14px; margin-bottom:14px; }
.modal-head h2 { margin:0; }
.modal-head p { margin:5px 0 0; color:var(--muted); }
.settings-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr 1fr;
  gap: 12px;
}
.setting-box {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
}
.setting-box.wide { grid-row: span 2; }
.setting-box h3 { margin: 0 0 12px; font-size: 16px; color: #dbeafe; }
.template-preview-wrap {
  background: #050914;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  margin-top: 10px;
}
.template-preview { width: 100%; display: block; border-radius: 9px; background: repeating-conic-gradient(#d1d5db 0% 25%, #f8fafc 0% 50%) 50%/24px 24px; }
.modal-actions { margin-top: 14px; display:flex; gap:10px; }

.login-page { min-height: 100vh; display:grid; place-items:center; padding:20px; }
.login-card {
  width: 440px;
  max-width: 100%;
  background: linear-gradient(180deg, rgba(16,26,43,.98), rgba(10,16,28,.98));
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 26px;
  box-shadow: 0 25px 70px rgba(0,0,0,.38);
}
.login-brand { display:flex; align-items:center; gap:14px; margin-bottom:24px; }
.login-card .btn { width: 100%; margin-top: 8px; }

@media (max-width: 1500px) {
  .posts-grid.compact-grid, .preview-grid { grid-template-columns: repeat(4, minmax(230px, 1fr)); }
  .settings-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .setting-box.wide { grid-row: auto; }
}
@media (max-width: 1100px) {
  .posts-grid.compact-grid, .preview-grid { grid-template-columns: repeat(2, minmax(240px, 1fr)); }
  .settings-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .posts-toolbar, .section-head, .download-bar, .modal-head { display:block; }
  .bulk-actions, .top-actions { margin-top: 10px; }
}
@media (max-width: 700px) {
  .topbar { display:block; }
  .posts-grid.compact-grid, .preview-grid, .settings-grid { grid-template-columns: 1fr; }
  .page { padding: 12px; }
}


.line-meters {
  display: grid;
  gap: 3px;
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
}
.line-meters span.warn { color: #fbbf24; font-weight: 800; }
.line-meters span.bad { color: #fecaca; font-weight: 900; }
.line-fit-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  margin-top: 9px;
}
.line-fit-actions .btn { width: 100%; }
.layout-actions { display:flex; flex-direction:column; gap:6px; margin-top:8px; }
.layout-actions .btn { width:100%; }
.p-line.too-long { outline: 1px dashed rgba(251,191,36,.75); }


/* CMG live patch: mobile-friendly controls */
@media (max-width: 700px) {
  .topbar { position: static; }
  .brand-line h1 { font-size: 19px; }
  .brand-line p { font-size: 12px; }
  .posts-panel { padding: 10px; }
  .post-card { padding: 8px; border-radius: 14px; }
  .compact-preview { max-height: none; }
  .adjust-grid { grid-template-columns: 1fr; }
  .btn { min-height: 42px; }
  .btn.small { min-height: 38px; }
  textarea { font-size: 18px; min-height: 92px; }
  .line-fit-actions { grid-template-columns: 1fr; }
  .move-buttons { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .move-buttons .btn { width: 100%; }
}


/* CMG patch: show full 1080x1080 preview canvas */
.mini-preview,
.compact-preview {
  aspect-ratio: 1 / 1 !important;
  max-height: none !important;
  height: auto !important;
}

.preview-photo-wrap,
.preview-template,
.preview-photo {
  width: 100% !important;
  height: 100% !important;
}

.p-line {
  overflow: visible !important;
}


/* CMG patch: font controls and better Urdu line rendering */
.checkrow {
  display:flex;
  align-items:center;
  gap:8px;
  color:#dbeafe;
  font-weight:800;
}

.checkrow input {
  width:auto;
}

.preview-text .p-line,
.p-line {
  line-height: 1.9;
  overflow: visible !important;
}


/* CMG patch: exact builder/render spacing sync */
.preview-text {
  display: flex !important;
  flex-direction: column !important;
}

.preview-text .p-line,
.p-line {
  margin: 0 !important;
  padding: 0 !important;
  overflow: visible !important;
}
