@font-face {
  font-family: "Qalam Amiri";
  src: url("../fonts/Amiri-Regular.ttf") format("truetype");
  font-display: swap;
}
@font-face {
  font-family: "Qalam Ruqaa";
  src: url("../fonts/ArefRuqaa-Bold.ttf") format("truetype");
  font-display: swap;
}

:root {
  --bg: #0e0c0a;
  --bg-2: #15110d;
  --panel: rgba(255, 247, 232, 0.042);
  --panel-2: rgba(255, 247, 232, 0.028);
  --ink: #f1e7d5;
  --muted: #9c8f7a;
  --muted-2: #6f6455;
  --line: rgba(231, 211, 178, 0.13);
  --line-2: rgba(231, 211, 178, 0.08);
  --gold: #c9a35a;
  --gold-2: #e8c986;
  --gold-3: #8a682f;
  --danger: #d58a7a;
  --shadow: 0 26px 70px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(1100px 700px at 78% -10%, rgba(201, 163, 90, 0.10), transparent 60%),
    radial-gradient(900px 600px at -5% 25%, rgba(90, 112, 128, 0.08), transparent 55%),
    linear-gradient(135deg, #0d0b09 0%, #12100d 48%, #0a0908 100%);
  font-family: "Segoe UI", "Segoe UI Variable", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  opacity: 0.055;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

.app {
  position: relative;
  z-index: 1;
  width: min(1480px, calc(100% - 48px));
  margin: 0 auto;
  padding-bottom: 28px;
}

/* ---------------------------------------------------------------- header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 26px 2px 22px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 16px;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  font-family: "Qalam Ruqaa", "Qalam Amiri", serif;
  font-size: 48px;
  line-height: 0.72;
  color: var(--gold-2);
  transform: translateY(5px);
  text-shadow: 0 2px 22px rgba(201, 163, 90, 0.20);
}

.brand-text {
  font-size: 14px;
  letter-spacing: 0.34em;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-top: 2px;
}

.brand-text small {
  font-size: 9px;
  letter-spacing: 0.19em;
  font-weight: 500;
  color: var(--muted);
  text-transform: uppercase;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.local-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.local-badge i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #8fae7d;
  box-shadow: 0 0 0 3px rgba(143, 174, 125, 0.10);
}

/* --------------------------------------------------------------- buttons */
.ghost {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255, 248, 235, 0.035);
  color: var(--ink);
  border-radius: 9px;
  height: 36px;
  padding: 0 14px;
  font: inherit;
  font-size: 11px;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  cursor: pointer;
  transition: border-color 0.18s ease, background 0.18s ease, transform 0.18s ease;
}

.ghost:hover {
  border-color: rgba(201, 163, 90, 0.55);
  background: rgba(201, 163, 90, 0.08);
}

.ghost:active { transform: translateY(1px); }

.primary {
  width: 100%;
  height: 50px;
  margin-top: 4px;
  border: 0;
  border-radius: 12px;
  background: linear-gradient(135deg, #d8b571, #b98d42 55%, #a27632);
  color: #1a140c;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.05em;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  cursor: pointer;
  box-shadow: 0 12px 30px rgba(201, 163, 90, 0.16), inset 0 1px 0 rgba(255, 255, 255, 0.30);
  transition: filter 0.18s ease, transform 0.18s ease;
}

.primary span {
  font-family: "Qalam Amiri", serif;
  font-size: 19px;
  font-weight: 400;
  opacity: 0.82;
}

.primary:hover { filter: brightness(1.06); transform: translateY(-1px); }
.primary:disabled { opacity: 0.6; cursor: wait; transform: none; }

.panel-note {
  padding: 12px 2px 1px;
  color: var(--muted-2);
  font-size: 8.5px;
  line-height: 1.45;
  letter-spacing: 0.04em;
  text-align: center;
}

.render-pill {
  border: 0;
  border-radius: 8px;
  height: 36px;
  padding: 0 15px;
  background: linear-gradient(135deg, #d8b571, #b98d42 60%, #a27632);
  color: #1a140c;
  font: inherit;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(201, 163, 90, 0.14);
  transition: filter 0.15s ease, transform 0.15s ease;
}

.render-pill:hover { filter: brightness(1.07); transform: translateY(-1px); }
.render-pill:disabled { opacity: 0.55; cursor: wait; transform: none; }

.panel-actions {
  position: sticky;
  bottom: -1px;
  z-index: 3;
  margin: 8px -18px -18px;
  padding: 12px 18px 15px;
  border-radius: 0 0 20px 20px;
  background: linear-gradient(180deg, rgba(21, 17, 13, 0) 0%, rgba(21, 17, 13, 0.93) 32%, #17130f 100%);
}

/* --------------------------------------------------------------- layout */
.workspace {
  display: grid;
  grid-template-columns: 372px minmax(0, 1fr);
  gap: 22px;
  align-items: start;
}

.panel {
  background: linear-gradient(180deg, rgba(255, 248, 236, 0.055), rgba(255, 248, 236, 0.022));
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.22);
  max-height: calc(100vh - 126px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(201, 163, 90, 0.35) transparent;
}

.panel::-webkit-scrollbar { width: 5px; }
.panel::-webkit-scrollbar-thumb { background: rgba(201, 163, 90, 0.32); border-radius: 99px; }

/* Each composer block is boxed: text, script, ink/paper, frame. */
.field {
  padding: 13px 12px;
  border: 2px solid rgba(201, 163, 90, 0.42);
  border-radius: 12px;
  background: rgba(255, 248, 235, 0.018);
}

/* Breathing room between the composer blocks. */
.field + .field { margin-top: 14px; }

.field.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}

.label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.label {
  font-family: "Qalam Amiri", "Segoe UI", serif;
  font-size: 15px;
  color: #e5d8c3;
  line-height: 1;
}

.label span {
  font-family: "Segoe UI", sans-serif;
  font-size: 9px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted-2);
  margin-left: 3px;
}

.counter, .hint {
  font-size: 9px;
  letter-spacing: 0.08em;
  color: var(--muted-2);
  white-space: nowrap;
}

.hint {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #857863;
  font-style: italic;
  letter-spacing: 0;
}

/* ---------------------------------------------------------------- text */
textarea#text {
  width: 100%;
  min-height: 82px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(8, 7, 6, 0.52);
  color: #f6eddc;
  padding: 12px 14px 14px;
  font-family: "Qalam Amiri", serif;
  font-size: 26px;
  line-height: 1.62;
  outline: none;
  direction: rtl;
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

textarea#text::placeholder { color: #6a5f4f; }

textarea#text:focus {
  border-color: rgba(201, 163, 90, 0.60);
  box-shadow: 0 0 0 3px rgba(201, 163, 90, 0.08);
}

.quick-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 9px;
}

.quick-row button {
  border: 1px solid var(--line-2);
  background: transparent;
  color: var(--muted);
  border-radius: 99px;
  padding: 4px 9px;
  font: inherit;
  font-size: 9px;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition: all 0.15s ease;
}

.quick-row button:hover {
  color: var(--gold-2);
  border-color: rgba(201, 163, 90, 0.40);
}

/* --------------------------------------------------------------- styles */
.style-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  max-height: 228px;
  overflow-y: auto;
  align-content: start;
  padding-right: 3px;
  scrollbar-width: thin;
  scrollbar-color: rgba(201, 163, 90, 0.35) transparent;
}

.style-grid::-webkit-scrollbar { width: 4px; }
.style-grid::-webkit-scrollbar-thumb {
  background: rgba(201, 163, 90, 0.30);
  border-radius: 99px;
}

.style-divider {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 2px 2px;
  font-size: 7px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--gold);
}

.style-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, rgba(201, 163, 90, 0.35), transparent);
}

.grid-note {
  margin: 6px 2px 0;
  color: var(--muted-2);
  font-size: 8px;
  letter-spacing: 0.04em;
  text-align: right;
}

.style-card {
  text-align: left;
  border: 1px solid var(--line-2);
  background: rgba(255, 248, 235, 0.018);
  border-radius: 10px;
  padding: 6px 8px 5px;
  color: var(--ink);
  cursor: pointer;
  font: inherit;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 52px;
  transition: border-color 0.16s ease, background 0.16s ease, transform 0.16s ease;
}

.style-card:hover {
  border-color: rgba(201, 163, 90, 0.33);
  background: rgba(201, 163, 90, 0.045);
}

.style-card.is-active {
  border-color: rgba(201, 163, 90, 0.70);
  background: linear-gradient(135deg, rgba(201, 163, 90, 0.17), rgba(201, 163, 90, 0.06));
  box-shadow: inset 0 0 0 1px rgba(232, 201, 134, 0.08);
}

.style-card .ar {
  font-family: "Qalam Amiri", serif;
  font-size: 16px;
  line-height: 1.0;
  color: #f4ead7;
  direction: rtl;
  text-align: right;
  white-space: normal;
  overflow: visible;
}

.style-card .en {
  font-size: 7px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.style-card.is-active .en { color: var(--gold-2); }

/* -------------------------------------------------------------- swatches */
.swatch-row {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  align-items: center;
}

.swatch {
  width: 27px;
  height: 27px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  padding: 0;
  cursor: pointer;
  position: relative;
  outline: none;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.swatch:hover { transform: translateY(-1px) scale(1.06); }

.swatch.is-active {
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 3.5px var(--gold);
}

.swatch.paper {
  width: 30px;
  height: 23px;
  border-radius: 5px;
}

.swatch.paper.is-active {
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 3.5px var(--gold);
}

.swatch .check {
  display: none;
  position: absolute;
  inset: 0;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 11px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.65);
}

.swatch.is-active .check { display: flex; }

/* ------------------------------------------------------------- segmented */
.segmented {
  display: flex;
  gap: 3px;
  background: rgba(8, 7, 6, 0.52);
  border: 1px solid var(--line-2);
  padding: 3px;
  border-radius: 10px;
}

.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  height: 30px;
  border-radius: 7px;
  font: inherit;
  font-size: 10px;
  letter-spacing: 0.05em;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.segmented button:hover { color: var(--ink); }

.segmented button.is-active {
  background: linear-gradient(135deg, #d4b06b, #b78c43);
  color: #17120b;
  font-weight: 600;
  box-shadow: 0 2px 10px rgba(201, 163, 90, 0.18);
}

.segmented.tiny button { height: 26px; font-size: 9px; }

.type-toggles {
  display: flex;
  gap: 4px;
  flex: 1;
}

.type-toggles button {
  min-width: 34px;
  height: 26px;
  border: 1px solid var(--line-2);
  border-radius: 7px;
  background: rgba(8, 7, 6, 0.35);
  color: var(--muted);
  font: inherit;
  font-size: 10px;
  cursor: pointer;
  padding: 0 8px;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.type-toggles button:hover {
  border-color: rgba(201, 163, 90, 0.40);
  color: var(--ink);
}

.type-toggles button.is-active {
  border-color: rgba(201, 163, 90, 0.70);
  background: rgba(201, 163, 90, 0.14);
  color: var(--gold-2);
}

.type-toggles i { font-style: italic; }

.frame-picker {
  display: flex;
  align-items: center;
  gap: 10px;
}

.frame-preview {
  width: 50px;
  height: 50px;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #f4ecd8;
  display: grid;
  place-items: center;
}

.frame-preview img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.frame-picker-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.frame-picker select {
  width: 100%;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(8, 7, 6, 0.52);
  color: var(--ink);
  font: inherit;
  font-size: 10px;
  padding: 0 8px;
  outline: none;
  cursor: pointer;
}

.frame-picker select:focus {
  border-color: rgba(201, 163, 90, 0.55);
}

.frame-picker option {
  background: #17130f;
  color: var(--ink);
}

.frame-picker-note {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 7.5px;
  letter-spacing: 0.06em;
  color: var(--muted-2);
}

.crop-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 8px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  white-space: nowrap;
}

.crop-toggle input {
  width: 11px;
  height: 11px;
  margin: 0;
  accent-color: var(--gold);
  cursor: pointer;
}

.crop-toggle input:disabled { cursor: not-allowed; opacity: 0.4; }
.crop-toggle input:disabled + span { opacity: 0.4; }

.transparent-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 7.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  white-space: nowrap;
}

.transparent-toggle input {
  width: 11px;
  height: 11px;
  margin: 0;
  accent-color: var(--gold);
  cursor: pointer;
}

.transparent-toggle input:checked + span { color: var(--gold-2); }

.frame-preview {
  padding: 0;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.frame-preview:hover {
  border-color: rgba(201, 163, 90, 0.55);
  transform: translateY(-1px);
}

.frame-browse {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(8, 7, 6, 0.52);
  color: var(--ink);
  font: inherit;
  padding: 5px 9px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  cursor: pointer;
  text-align: left;
}

.frame-browse:hover { border-color: rgba(201, 163, 90, 0.45); }
.frame-browse span { font-size: 11px; letter-spacing: 0.02em; }
.frame-browse small {
  font-size: 7.5px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--gold);
}

.sub-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 13px;
}

.mini-label {
  font-size: 9px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted-2);
  white-space: nowrap;
}

.sub-row .segmented { flex: 1; }

input[type="range"] {
  flex: 1;
  accent-color: var(--gold);
  height: 24px;
  cursor: pointer;
}

output {
  font-size: 10px;
  color: var(--muted);
  width: 36px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------- formats */
.format-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.format-row button {
  border: 1px solid var(--line-2);
  background: rgba(255, 248, 235, 0.018);
  color: var(--muted);
  border-radius: 9px;
  padding: 7px 9px;
  text-align: left;
  font: inherit;
  font-size: 10px;
  letter-spacing: 0.03em;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: all 0.15s ease;
}

.format-row button small {
  font-size: 8px;
  color: var(--muted-2);
  letter-spacing: 0.04em;
}

.format-row button:hover { border-color: rgba(201, 163, 90, 0.30); }

.format-row button.is-active {
  border-color: rgba(201, 163, 90, 0.55);
  background: rgba(201, 163, 90, 0.08);
  color: var(--gold-2);
}

.format-row button.is-active small { color: #b39b71; }

.micro {
  margin: 10px 2px 0;
  font-size: 9px;
  line-height: 1.5;
  color: var(--muted-2);
  text-align: center;
}

/* ----------------------------------------------------------------- stage */
.stage {
  position: sticky;
  top: 24px;
  min-width: 0;
}

.stage-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  min-height: 36px;
  padding: 0 2px 12px;
}

.stage-meta {
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#styleLabel { color: var(--gold-2); }
.sep { margin: 0 8px; color: var(--muted-2); }

.downloads { display: flex; gap: 7px; }

.stage-tools {
  display: flex;
  align-items: center;
  gap: 10px;
}

.format-select {
  display: flex;
  align-items: center;
  gap: 7px;
}

.format-select > span {
  font-size: 8px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted-2);
}

.format-select select {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%) calc(100% - 12px) 15px / 4px 4px no-repeat,
    linear-gradient(135deg, var(--muted) 50%, transparent 50%) calc(100% - 8px) 15px / 4px 4px no-repeat,
    rgba(255, 248, 235, 0.035);
  color: var(--muted);
  height: 36px;
  padding: 0 26px 0 10px;
  font: inherit;
  font-size: 10px;
  letter-spacing: 0.03em;
  outline: none;
  cursor: pointer;
}

.format-select select:hover {
  border-color: rgba(201, 163, 90, 0.38);
  color: var(--ink);
}

.format-select option {
  background: #17130f;
  color: var(--ink);
}

.size-control {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 36px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 248, 235, 0.035);
}

.size-control > span {
  font-size: 8px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted-2);
}

.size-control input {
  width: 46px;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 11px;
  text-align: right;
  outline: none;
  padding: 0;
}

.size-control input::placeholder { color: var(--muted-3, #6f6455); }

.canvas-wrap {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 22px;
  background:
    linear-gradient(135deg, rgba(255, 248, 235, 0.035), transparent 40%),
    repeating-conic-gradient(rgba(255, 255, 255, 0.018) 0% 25%, transparent 0% 50%) 0 0 / 18px 18px,
    #0a0807;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04), 0 22px 60px rgba(0, 0, 0, 0.28);
  min-height: 430px;
  display: grid;
  place-items: center;
  padding: 22px;
  overflow: hidden;
}

.canvas-wrap.is-transparent {
  background:
    repeating-conic-gradient(#d6d1c6 0% 25%, #f7f3ea 0% 50%) 0 0 / 18px 18px;
  border-color: rgba(201, 163, 90, 0.32);
}

#preview {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 240px);
  width: auto;
  height: auto;
  border-radius: 3px;
  box-shadow: 0 28px 70px rgba(0, 0, 0, 0.58), 0 0 0 1px rgba(255, 255, 255, 0.05);
  opacity: 0;
  transition: opacity 0.25s ease;
  cursor: zoom-in;
}

#preview.is-ready { opacity: 1; }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted-2);
  font-family: "Qalam Amiri", serif;
  font-size: 19px;
  pointer-events: none;
}

.empty.is-hidden { display: none; }

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(10, 8, 7, 0.48);
  backdrop-filter: blur(2px);
  color: var(--gold-2);
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}

.loading.is-on { opacity: 1; }

.loading span {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid rgba(201, 163, 90, 0.22);
  border-top-color: var(--gold-2);
  animation: spin 0.8s linear infinite;
}

.loading em { font-style: normal; }

@keyframes spin { to { transform: rotate(360deg); } }

.stage-foot {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding: 12px 3px 0;
  font-size: 9px;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  color: var(--muted-2);
}

.stage-foot .right { text-align: right; }
#status { color: var(--muted); }
#status.is-error { color: var(--danger); }

/* ---------------------------------------------------------------- footer */
.footer {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding: 26px 3px 0;
  color: var(--muted-2);
  font-size: 9px;
  letter-spacing: 0.13em;
  text-transform: uppercase;
}

/* ----------------------------------------------------------------- modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
}

.modal.is-open { display: grid; place-items: center; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 4, 3, 0.74);
  backdrop-filter: blur(7px);
}

.modal-card {
  position: relative;
  width: min(1180px, calc(100vw - 40px));
  max-height: 88vh;
  overflow-y: auto;
  background: linear-gradient(180deg, #191510, #100d0a);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 24px;
  box-shadow: 0 50px 140px rgba(0, 0, 0, 0.7);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  padding-bottom: 18px;
}

.modal-head h2 {
  margin: 0 0 5px;
  font-family: "Qalam Amiri", serif;
  font-size: 30px;
  font-weight: 400;
  color: #f6eddc;
}

.modal-head p {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.04em;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.icon-btn:hover { border-color: var(--gold); color: var(--gold-2); }

.frame-modal-card {
  width: min(1040px, calc(100vw - 40px));
}

.frame-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 12px;
}

.frame-option {
  border: 1px solid var(--line-2);
  border-radius: 12px;
  background: rgba(255, 248, 235, 0.025);
  color: var(--ink);
  padding: 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font: inherit;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.15s ease;
}

.frame-option img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  display: block;
  border-radius: 7px;
  background: #f4ecd8;
}

.frame-option span {
  font-size: 8px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.frame-option:hover {
  border-color: rgba(201, 163, 90, 0.45);
  background: rgba(201, 163, 90, 0.06);
  transform: translateY(-1px);
}

.frame-option.is-active {
  border-color: rgba(201, 163, 90, 0.75);
  background: rgba(201, 163, 90, 0.10);
}

.frame-option.is-active span { color: var(--gold-2); }

.atlas-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.atlas-card {
  border: 1px solid var(--line-2);
  border-radius: 14px;
  overflow: hidden;
  background: #0a0807;
}

.atlas-card .img-wrap {
  position: relative;
  aspect-ratio: 8 / 5;
  display: grid;
  place-items: center;
  background: #0c0a08;
}

.atlas-card img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.atlas-card .img-wrap a {
  display: block;
  width: 100%;
  height: 100%;
}

.atlas-error {
  margin: 0;
  padding: 20px;
  color: var(--danger);
  font-size: 11px;
  line-height: 1.5;
  text-align: center;
}

.atlas-card .caption {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  padding: 9px 12px;
  border-top: 1px solid var(--line-2);
}

.atlas-card .caption b {
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold-2);
  font-weight: 600;
}

.atlas-card .caption span {
  font-family: "Qalam Amiri", serif;
  font-size: 17px;
  color: var(--muted);
}

.atlas-skeleton {
  aspect-ratio: 8 / 5;
  background: linear-gradient(100deg, rgba(255, 255, 255, 0.025) 20%, rgba(255, 255, 255, 0.06) 40%, rgba(255, 255, 255, 0.025) 60%);
  background-size: 200% 100%;
  animation: shimmer 1.2s linear infinite;
}

@keyframes shimmer { to { background-position: -200% 0; } }

/* ------------------------------------------------------------ responsive */
@media (max-width: 1120px) {
  .workspace { grid-template-columns: 340px minmax(0, 1fr); }
  .panel { max-height: none; }
  .stage { position: static; }
  #preview { max-height: 70vh; }
}

@media (max-width: 900px) {
  .app { width: min(100% - 28px, 760px); }
  .workspace { grid-template-columns: 1fr; }
  .stage { order: -1; }
  .canvas-wrap { min-height: 280px; padding: 14px; }
  #preview { max-height: 52vh; }
  .panel { max-height: none; }
  .style-grid { grid-template-columns: 1fr 1fr; }
  .topbar { padding-top: 18px; }
  .local-badge { display: none; }
}

@media (max-width: 620px) {
  .brand-mark { font-size: 38px; }
  .brand-text { font-size: 11px; letter-spacing: 0.24em; }
  .brand-text small { font-size: 7.5px; }
  .top-actions .ghost { height: 32px; padding: 0 10px; font-size: 9px; }
  .style-grid { grid-template-columns: 1fr 1fr; }
  .field.split { grid-template-columns: 1fr; gap: 14px; }
  .atlas-grid { grid-template-columns: 1fr; }
  .frame-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .stage-foot .right { display: none; }
  .footer { flex-direction: column; gap: 6px; }
  .modal-card { padding: 16px; }
  .modal-head h2 { font-size: 24px; }
}

/* Cross-app link back to the Harf STL studio. The brand mark also links home,
   so the text link is dropped on narrow screens to keep the top bar calm. */
.back-link { white-space: nowrap; }
.back-link .back-arrow::before { content: "←"; }
html[dir="rtl"] .back-link .back-arrow::before { content: "→"; }
@media (max-width: 720px) {
  .back-link { display: none; }
}

/* ------------------------------------------------------------- language */
.lang-toggle {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 248, 235, 0.035);
  color: var(--muted);
  font-family: inherit;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  direction: ltr;
  cursor: pointer;
}
.lang-toggle:hover { border-color: rgba(201, 163, 90, 0.55); color: var(--ink); }
.lang-toggle .lang-option { padding: 3px 5px; border-radius: 4px; line-height: 1; }
.lang-toggle .lang-option.is-active { background: var(--gold); color: #17120c; }
.lang-toggle .lang-sep { color: var(--muted-2); }

/* Arabic runs must not be letter-spaced: it breaks the joins. */
html[dir="rtl"] .brand-text small,
html[dir="rtl"] .local-badge,
html[dir="rtl"] .ghost,
html[dir="rtl"] .panel-note,
html[dir="rtl"] .quick-row button,
html[dir="rtl"] .style-divider,
html[dir="rtl"] .grid-note,
html[dir="rtl"] .segmented button,
html[dir="rtl"] .frame-picker-note,
html[dir="rtl"] .crop-toggle,
html[dir="rtl"] .transparent-toggle,
html[dir="rtl"] .frame-browse span,
html[dir="rtl"] .frame-browse small,
html[dir="rtl"] .mini-label,
html[dir="rtl"] .stage-meta,
html[dir="rtl"] .format-select > span,
html[dir="rtl"] .format-select select,
html[dir="rtl"] .size-control > span,
html[dir="rtl"] .loading,
html[dir="rtl"] .stage-foot,
html[dir="rtl"] .footer,
html[dir="rtl"] .modal-head p,
html[dir="rtl"] .frame-option span,
html[dir="rtl"] .atlas-card .caption b,
html[dir="rtl"] .render-pill,
html[dir="rtl"] .hint {
  letter-spacing: normal;
  text-transform: none;
}

/* Keep numeric readouts and sliders left-to-right inside the RTL layout. */
html[dir="rtl"] #charCount,
html[dir="rtl"] #dimsLabel,
html[dir="rtl"] #sizeLabel,
html[dir="rtl"] #scaleOut { direction: ltr; unicode-bidi: isolate; }
html[dir="rtl"] input[type="range"] { direction: ltr; }

/* =====================================================================
   LIGHT THEME
   The parchment palette of the main Harf studio. These rules come after
   the original dark theme, so they win without changing any layout.
   ===================================================================== */
:root {
  color-scheme: light;
  --bg: #efe7d8;
  --bg-2: #f7f0e3;
  --panel: #faf6ed;
  --panel-2: #fffdf8;
  --ink: #1b1915;
  --muted: #5b5448;
  --muted-2: #7d7262;
  --line: #d7cab4;
  --line-2: #e4d9c6;
  --gold: #c4552f;
  --gold-2: #9f3b1c;
  --gold-3: #e8c4b0;
  --danger: #b3402a;
  --shadow: 0 26px 70px rgba(38, 31, 22, 0.14);
}

html { background: var(--bg); }

body {
  color: var(--ink);
  background:
    radial-gradient(1100px 560px at 82% -12%, rgba(255, 252, 242, 0.85), transparent 64%),
    radial-gradient(900px 620px at -10% 42%, rgba(226, 210, 183, 0.55), transparent 68%),
    var(--bg);
}

.grain { opacity: 0.035; mix-blend-mode: multiply; }

/* header */
.brand-mark { color: var(--gold-2); text-shadow: 0 2px 18px rgba(196, 85, 47, 0.14); }
.brand-text small { color: var(--muted); }
.local-badge { color: var(--muted); }
.local-badge i { background: #6f8f5f; box-shadow: 0 0 0 3px rgba(111, 143, 95, 0.12); }

/* buttons */
.ghost {
  border-color: var(--line);
  background: rgba(255, 253, 248, 0.6);
  color: var(--ink);
}
.ghost:hover { border-color: var(--gold); background: rgba(196, 85, 47, 0.06); }
.primary,
.render-pill {
  background: linear-gradient(135deg, #d97757, #c4552f 55%, #9f3b1c);
  color: #fffaf2;
  box-shadow: 0 8px 22px rgba(196, 85, 47, 0.22);
}

/* panel and composer fields */
.panel {
  background: linear-gradient(180deg, #fffdf8, #f7f0e3);
  border-color: var(--line);
  box-shadow: 0 20px 50px rgba(38, 31, 22, 0.10);
}
.panel::-webkit-scrollbar-thumb { background: rgba(196, 85, 47, 0.28); }
.panel-actions {
  background: linear-gradient(180deg, rgba(250, 246, 237, 0) 0%, rgba(250, 246, 237, 0.95) 32%, #faf6ed 100%);
}
.field {
  border-color: rgba(196, 85, 47, 0.35);
  background: rgba(255, 253, 248, 0.72);
}
.label { color: #2a251d; }
.hint { color: var(--muted-2); }
textarea#text {
  background: #fffdf8;
  color: var(--ink);
  border-color: var(--line);
}
textarea#text::placeholder { color: #a29782; }
textarea#text:focus {
  border-color: rgba(196, 85, 47, 0.55);
  box-shadow: 0 0 0 3px rgba(196, 85, 47, 0.08);
}
.quick-row button { color: var(--muted); border-color: var(--line-2); }
.quick-row button:hover { color: var(--gold-2); border-color: rgba(196, 85, 47, 0.35); }

/* script */
.style-divider { color: var(--gold-2); }
.style-divider::after {
  background: linear-gradient(90deg, rgba(196, 85, 47, 0.32), transparent);
}
.style-grid::-webkit-scrollbar-thumb { background: rgba(196, 85, 47, 0.28); }
.style-card { background: #fffdf8; border-color: var(--line-2); }
.style-card:hover { border-color: rgba(196, 85, 47, 0.32); background: rgba(196, 85, 47, 0.04); }
.style-card.is-active {
  border-color: rgba(196, 85, 47, 0.62);
  background: linear-gradient(135deg, rgba(196, 85, 47, 0.10), rgba(196, 85, 47, 0.03));
  box-shadow: inset 0 0 0 1px rgba(196, 85, 47, 0.06);
}
.style-card .ar { color: #241f18; }

/* swatches */
.swatch { border-color: rgba(27, 25, 21, 0.18); }
.swatch.is-active,
.swatch.paper.is-active { box-shadow: 0 0 0 2px var(--bg), 0 0 0 3.5px var(--gold); }

/* segmented + type toggles */
.segmented { background: #f3ead9; border-color: var(--line-2); }
.segmented button { color: var(--muted); }
.segmented button.is-active {
  background: linear-gradient(135deg, #d97757, #c4552f 60%, #9f3b1c);
  color: #fffaf2;
  box-shadow: 0 2px 10px rgba(196, 85, 47, 0.18);
}
.type-toggles button { background: #f7efe2; border-color: var(--line-2); color: var(--muted); }
.type-toggles button:hover { border-color: rgba(196, 85, 47, 0.35); color: var(--ink); }
.type-toggles button.is-active {
  border-color: rgba(196, 85, 47, 0.6);
  background: rgba(196, 85, 47, 0.10);
  color: var(--gold-2);
}

/* frame picker */
.frame-preview { border-color: var(--line); }
.frame-preview:hover { border-color: rgba(196, 85, 47, 0.5); }
.frame-picker select,
.frame-browse { background: #fffdf8; border-color: var(--line); color: var(--ink); }
.frame-picker select:focus { border-color: rgba(196, 85, 47, 0.5); }
.frame-picker option,
.format-select option { background: #fffdf8; color: var(--ink); }
.frame-browse:hover { border-color: rgba(196, 85, 47, 0.4); }
.frame-browse small { color: var(--gold-2); }
.crop-toggle,
.transparent-toggle { color: var(--muted); }
.format-row button { background: #fffdf8; border-color: var(--line-2); }
.format-row button:hover { border-color: rgba(196, 85, 47, 0.28); }
.format-row button.is-active {
  border-color: rgba(196, 85, 47, 0.5);
  background: rgba(196, 85, 47, 0.08);
}
.format-row button.is-active small { color: var(--gold-2); }

/* stage */
.format-select select {
  background:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%) calc(100% - 12px) 15px / 4px 4px no-repeat,
    linear-gradient(135deg, var(--muted) 50%, transparent 50%) calc(100% - 8px) 15px / 4px 4px no-repeat,
    #fffdf8;
  color: var(--muted);
}
.format-select select:hover { border-color: rgba(196, 85, 47, 0.35); }
.size-control { background: #fffdf8; border-color: var(--line); }
.size-control input { color: var(--ink); }
.size-control input::placeholder { color: #a29782; }

.canvas-wrap {
  border-color: var(--line);
  background:
    radial-gradient(circle at 50% 42%, rgba(255, 253, 248, 0.92), rgba(241, 232, 216, 0.92) 68%),
    repeating-conic-gradient(rgba(27, 25, 21, 0.028) 0% 25%, transparent 0% 50%) 0 0 / 18px 18px,
    #efe6d5;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6), 0 18px 46px rgba(38, 31, 22, 0.12);
}
.canvas-wrap.is-transparent { border-color: rgba(196, 85, 47, 0.3); }
#preview { box-shadow: 0 24px 60px rgba(38, 31, 22, 0.22), 0 0 0 1px rgba(27, 25, 21, 0.06); }
.empty { color: var(--muted-2); }
.loading { background: rgba(255, 253, 248, 0.72); color: var(--gold-2); }
.loading span { border-color: rgba(196, 85, 47, 0.2); border-top-color: var(--gold-2); }
.stage-foot { color: var(--muted-2); }
.footer { color: var(--muted-2); }

/* modals */
.modal-backdrop { background: rgba(27, 25, 21, 0.45); }
.modal-card {
  background: linear-gradient(180deg, #fffdf8, #f8f0e1);
  border-color: var(--line);
  box-shadow: 0 40px 100px rgba(38, 31, 22, 0.28);
}
.modal-head h2 { color: var(--ink); }
.icon-btn { color: var(--ink); border-color: var(--line); }
.icon-btn:hover { border-color: var(--gold); color: var(--gold-2); }
.frame-option { background: #fffdf8; border-color: var(--line-2); color: var(--ink); }
.frame-option:hover { border-color: rgba(196, 85, 47, 0.4); background: rgba(196, 85, 47, 0.05); }
.frame-option.is-active {
  border-color: rgba(196, 85, 47, 0.65);
  background: rgba(196, 85, 47, 0.09);
}
.atlas-card { background: #fffdf8; border-color: var(--line-2); }
.atlas-card .img-wrap { background: #f6efe2; }
.atlas-skeleton {
  background: linear-gradient(100deg, rgba(27, 25, 21, 0.04) 20%, rgba(27, 25, 21, 0.08) 40%, rgba(27, 25, 21, 0.04) 60%);
}

/* language toggle */
.lang-toggle { background: #fffdf8; border-color: var(--line); color: var(--muted); }
.lang-toggle:hover { border-color: rgba(196, 85, 47, 0.5); color: var(--ink); }
.lang-toggle .lang-option.is-active { background: var(--gold); color: #fffaf2; }

