@font-face { font-family: 'Cairo'; src: url('/static/fonts/cairo-800.ttf') format('truetype'); font-weight: 800; font-display: swap; }
@font-face { font-family: 'Noto Kufi Arabic'; src: url('/static/fonts/notokufi-800.ttf') format('truetype'); font-weight: 800; font-display: swap; }
@font-face { font-family: 'Tajawal'; src: url('/static/fonts/tajawal-800.ttf') format('truetype'); font-weight: 800; font-display: swap; }
@font-face { font-family: 'Almarai'; src: url('/static/fonts/almarai-800.ttf') format('truetype'); font-weight: 800; font-display: swap; }

:root {
  --bg: #0f0f12;
  --card: #1a1a20;
  --card2: #23232b;
  --border: #2e2e38;
  --text: #f3f3f5;
  --muted: #9a9aa6;
  --accent: #B31919;
  --accent2: #d42a2a;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: 'Cairo', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif; -webkit-font-smoothing: antialiased; }
body { min-height: 100dvh; padding-bottom: calc(76px + var(--safe-b)); }
.hidden { display: none !important; }
button, select, input, textarea { font-family: inherit; }

/* Header */
#app-header { position: sticky; top: 0; z-index: 30; display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; background: rgba(15,15,18,.85); backdrop-filter: blur(12px); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 10px; }
.brand strong { display: block; font-size: 16px; }
.brand small { color: var(--muted); font-size: 12px; }
.header-actions { display: flex; gap: 6px; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--border); background: var(--card); color: var(--text); font-size: 16px; display: grid; place-items: center; cursor: pointer; }
.icon-btn:active { transform: scale(.95); }

/* Layout */
#editor { display: grid; gap: 16px; padding: 14px; max-width: 1200px; margin: 0 auto; }
@media (min-width: 900px) {
  #editor { grid-template-columns: 1fr 420px; align-items: start; }
  #preview-section { position: sticky; top: 72px; }
  body { padding-bottom: 24px; }
  #mobile-bar { display: none !important; }
}

/* Preview */
#canvas-wrap { position: relative; width: 100%; max-width: 520px; margin: 0 auto; aspect-ratio: 4/5; border-radius: var(--radius); overflow: hidden; background: #222; box-shadow: 0 20px 50px rgba(0,0,0,.5); touch-action: none; }
#canvas { width: 100%; height: 100%; display: block; cursor: grab; }
#canvas.dragging { cursor: grabbing; }
.drop-hint { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: #fff; background: rgba(0,0,0,.35); font-size: 15px; cursor: pointer; pointer-events: auto; }
.drop-hint i { font-size: 46px; opacity: .9; }
#canvas-wrap.dragover { outline: 3px dashed var(--accent2); outline-offset: -6px; }
.photo-controls { position: absolute; left: 10px; right: 10px; bottom: 10px; display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: rgba(0,0,0,.6); backdrop-filter: blur(8px); border-radius: 14px; }
.photo-controls button { width: 36px; height: 36px; border-radius: 10px; border: 0; background: rgba(255,255,255,.12); color: #fff; cursor: pointer; flex-shrink: 0; }
.photo-controls input[type=range] { flex: 1; }
.hint { text-align: center; color: var(--muted); font-size: 12px; margin: 10px 0 0; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; scroll-margin-top: 80px; }
.card h2 { margin: 0 0 12px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.step { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 13px; }

.template-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.template-item { position: relative; border: 2px solid transparent; border-radius: 12px; overflow: hidden; cursor: pointer; background: var(--card2); }
.template-item img { width: 100%; display: block; aspect-ratio: 4/5; object-fit: cover; }
.template-item span { position: absolute; bottom: 0; left: 0; right: 0; padding: 6px; font-size: 12px; text-align: center; background: linear-gradient(transparent, rgba(0,0,0,.85)); color: #fff; }
.template-item.active { border-color: var(--accent2); box-shadow: 0 0 0 3px rgba(212,42,42,.3); }
.template-item.active::after { content: '\\2713'; position: absolute; top: 6px; right: 6px; width: 22px; height: 22px; border-radius: 50%; background: var(--accent2); color: #fff; display: grid; place-items: center; font-size: 13px; }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row.small { margin-top: 10px; }
.btn { flex: 1; min-width: 100px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--card2); color: var(--text); font-size: 14px; font-weight: 700; cursor: pointer; text-align: center; }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.big { padding: 15px; font-size: 16px; }
.link-btn { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 13px; padding: 4px 6px; }
.link-btn.danger { color: #ff6b6b; }
.url-row { display: flex; gap: 8px; margin-top: 10px; }
.url-row input { flex: 1; }
.url-row .btn { flex: 0; min-width: 48px; }

textarea, input[type=url], input[type=text], select { width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--card2); color: var(--text); font-size: 16px; resize: vertical; outline: none; }
textarea { line-height: 1.6; }
textarea:focus, input:focus { border-color: var(--accent2); }
.text-meta { display: flex; align-items: center; gap: 6px; margin: 6px 0 10px; color: var(--muted); font-size: 12px; }
.text-meta span { flex: 1; }

.row { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.row label { width: 86px; flex-shrink: 0; font-size: 13px; color: var(--muted); }
.row input[type=range] { flex: 1; }
.row output { width: 42px; text-align: center; font-size: 13px; color: var(--muted); }
.row select { flex: 1; padding: 10px 12px; }
.seg { flex: 1; display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; padding: 9px; border: 0; background: transparent; color: var(--muted); font-size: 13px; cursor: pointer; }
.seg button.active { background: var(--accent); color: #fff; }

input[type=range] { -webkit-appearance: none; appearance: none; height: 6px; border-radius: 3px; background: var(--border); outline: none; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; border-radius: 50%; background: var(--accent2); border: 3px solid #fff; cursor: pointer; }
input[type=range]::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: var(--accent2); border: 3px solid #fff; cursor: pointer; }

/* Mobile bar */
#mobile-bar { position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 8px 10px calc(8px + var(--safe-b)); background: rgba(20,20,26,.92); backdrop-filter: blur(14px); border-top: 1px solid var(--border); }
#mobile-bar button, #mobile-bar label { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 4px; border-radius: 12px; border: 0; background: transparent; color: var(--text); font-size: 11px; cursor: pointer; }
#mobile-bar i { font-size: 18px; }
#mobile-bar .accent { background: var(--accent); color: #fff; }

/* Drawer */
.drawer { position: fixed; top: 0; bottom: 0; right: 0; width: min(92vw, 380px); z-index: 50; background: var(--card); border-left: 1px solid var(--border); padding: 16px; overflow-y: auto; transform: translateX(0); animation: slideIn .25s ease; display: flex; flex-direction: column; gap: 12px; }
@keyframes slideIn { from { transform: translateX(100%); } to { transform: translateX(0); } }
.drawer-head { display: flex; align-items: center; justify-content: space-between; }
.drawer-head h3 { margin: 0; font-size: 16px; }
.history-list { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; flex: 1; }
.history-item { border-radius: 12px; overflow: hidden; border: 1px solid var(--border); cursor: pointer; background: var(--card2); }
.history-item img { width: 100%; display: block; aspect-ratio: 4/5; object-fit: cover; }
.history-item p { margin: 0; padding: 6px 8px; font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-empty { grid-column: 1/-1; text-align: center; color: var(--muted); padding: 40px 0; font-size: 14px; }
#backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40; }

/* Toast & loader */
.toast { position: fixed; bottom: calc(90px + var(--safe-b)); left: 50%; transform: translateX(-50%); z-index: 100; background: #fff; color: #111; padding: 10px 18px; border-radius: 999px; font-size: 14px; font-weight: 700; box-shadow: 0 10px 30px rgba(0,0,0,.4); animation: pop .2s ease; max-width: 90vw; text-align: center; }
.toast.error { background: #ff5252; color: #fff; }
.toast.success { background: #1fc26a; color: #fff; }
@keyframes pop { from { transform: translateX(-50%) scale(.9); opacity: 0; } }
.loader { position: fixed; inset: 0; z-index: 90; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; background: rgba(0,0,0,.6); backdrop-filter: blur(4px); font-size: 14px; }
.spinner { width: 44px; height: 44px; border: 4px solid rgba(255,255,255,.2); border-top-color: var(--accent2); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== v2: mobile tabs, zones, groups ===== */
.group-title { margin: 10px 0 8px; font-size: 13px; color: var(--muted); font-weight: 700; }
.group-title:first-child { margin-top: 0; }
.template-list { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); }
.template-item .nophoto { position: absolute; top: 6px; left: 6px; width: 22px; height: 22px; border-radius: 6px; background: rgba(0,0,0,.6); color: #fff; display: grid; place-items: center; font-size: 11px; }
.note { color: var(--muted); font-size: 12px; margin: -6px 0 10px; min-height: 0; }
.note:empty { display: none; }
#photo-card.disabled .btn-row, #photo-card.disabled .url-row { opacity: .35; pointer-events: none; }
#tab-photo.disabled { opacity: .35; pointer-events: none; }
.zone { margin-bottom: 12px; }
.zone-label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.zone textarea { min-height: 84px; }
.auto-btn { width: 34px; height: 34px; border-radius: 10px; border: 1px solid var(--border); display: grid; place-items: center; color: var(--muted); flex-shrink: 0; }
.auto-btn.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.row output { width: 56px; }
.toggle-preview { display: none; }
#mobile-bar { grid-template-columns: repeat(5, 1fr); }
#mobile-bar button.active { color: var(--accent2); }
#mobile-bar button.active i { transform: translateY(-1px); }
#mobile-bar .accent.active { color: #fff; }

/* Mobile: tabbed cards + compact sticky preview */
@media (max-width: 899px) {
  #editor { padding: 10px; gap: 10px; }
  #preview-section { position: sticky; top: 60px; z-index: 20; background: var(--bg); padding-bottom: 6px; transition: max-height .25s ease; }
  #canvas-wrap { max-width: min(100%, 46vh * 0.8); margin: 0 auto; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.45); }
  #preview-section.collapsed #canvas-wrap { max-width: 120px; margin-right: 0; }
  #preview-section.collapsed .hint { display: none; }
  .toggle-preview { display: grid; place-items: center; position: absolute; top: 6px; left: 14px; z-index: 5; width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--border); background: rgba(26,26,32,.9); color: var(--text); font-size: 12px; }
  .hint { margin-top: 6px; font-size: 11px; }
  #controls .card { display: none; }
  #controls .card.active { display: block; animation: fadeIn .18s ease; }
  @keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } }
  .card h2 { font-size: 14px; }
  .btn { padding: 13px 10px; font-size: 14px; }
  .btn.big { padding: 15px; }
  textarea, input[type=url], select { font-size: 16px; }
  .row label { width: 76px; font-size: 12px; }
  body { padding-bottom: calc(84px + var(--safe-b)); }
  #mobile-bar button, #mobile-bar label { padding: 7px 2px; font-size: 10.5px; }
  #mobile-bar i { font-size: 17px; }
}
@media (max-width: 899px) and (max-height: 700px) {
  #canvas-wrap { max-width: min(100%, 38vh * 0.8); }
}

/* ===== v3 ===== */
.article-row { display: flex; gap: 8px; margin-bottom: 10px; }
.article-row input { flex: 1; border-color: var(--accent2); background: linear-gradient(90deg, rgba(212,42,42,.08), transparent); }
.article-row .btn { flex: 0; min-width: 52px; }
.adjust-box { margin-top: 10px; border: 1px solid var(--border); border-radius: 12px; padding: 0 12px; background: var(--card2); }
.adjust-box summary { padding: 11px 0; font-size: 13px; color: var(--muted); cursor: pointer; list-style: none; display: flex; align-items: center; gap: 8px; }
.adjust-box summary::-webkit-details-marker { display: none; }
.adjust-box[open] summary { border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.adjust-box .row { margin: 8px 0; }
.adjust-box .btn-row { margin: 8px 0 12px; }
.adjust-box .btn { padding: 9px; font-size: 13px; }
.tip { font-size: 12px; color: var(--muted); margin: 0 0 8px; line-height: 1.8; }
.tip b { color: var(--text); background: rgba(255,255,255,.08); padding: 0 6px; border-radius: 6px; }
.chip { border: 1px solid var(--accent2); color: var(--accent2); background: transparent; border-radius: 999px; padding: 2px 12px; font-size: 12px; cursor: pointer; }
.swatches { display: flex; gap: 8px; flex: 1; }
.swatches button { width: 30px; height: 30px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; }
.swatches button.on { border-color: #fff; box-shadow: 0 0 0 2px var(--accent2); }
.onboard { position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.7); backdrop-filter: blur(6px); display: flex; align-items: center; justify-content: center; padding: 20px; }
.onboard-card { background: var(--card); border: 1px solid var(--border); border-radius: 20px; padding: 24px; max-width: 380px; width: 100%; text-align: center; animation: pop .25s ease; }
.onboard-card img { border-radius: 14px; }
.onboard-card h3 { margin: 12px 0 14px; font-size: 18px; }
.onboard-card ol { text-align: right; margin: 0 0 18px; padding-right: 20px; line-height: 2; font-size: 14px; color: var(--muted); }
.onboard-card ol b { color: var(--text); }
.onboard-card .btn { width: 100%; }
