/* ================================================================
   Mashq Studio — Gold Standard tokens (matches mashq.ae theme)
   ================================================================ */
@font-face {
  font-family: 'Amiri';
  src: url('fonts/Amiri-Regular.ttf') format('truetype');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'Reem Kufi';
  src: url('fonts/ReemKufi.ttf') format('truetype');
  font-weight: 400 700;
  font-display: swap;
}
@font-face {
  font-family: 'Aref Ruqaa';
  src: url('fonts/ArefRuqaa-Regular.ttf') format('truetype');
  font-weight: 400;
  font-display: swap;
}

:root {
  --gold-900: #6E5223;
  --gold-700: #8C6A2F;
  --gold-500: #C6A15B;
  --gold-300: #DDBB74;
  --gold-100: #F0DCAD;
  --ink-950: #14100B;
  --ink-900: #1B150E;
  --ink-800: #241C10;
  --ivory-50: #FAF6EC;
  --ivory-100: #F5EFE2;
  --ivory-300: #E4DCC8;
  --maroon: #7A1830;
  --font-display: 'Amiri', serif;
  --font-ui: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', sans-serif;

  /* theme tokens — Mashq ivory & gold (single light theme) */
  --t-bg: var(--ivory-50);
  --t-glow: rgba(140, 106, 47, .07);
  --t-text: var(--ink-950);
  --t-accent-text: var(--gold-900);
  --t-stage-bg: #FFFDF6;
  --t-grid: rgba(110, 82, 35, .06);
  --t-border: rgba(140, 106, 47, .35);
  --t-border-strong: rgba(140, 106, 47, .45);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: var(--font-ui);
  background:
    radial-gradient(1200px 500px at 70% -10%, var(--t-glow), transparent 60%),
    var(--t-bg);
  color: var(--t-text);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

a { color: var(--gold-300); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--gold-300); outline-offset: 3px; }

/* ---------------- Top bar ---------------- */
.topbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 14px 28px 12px;
  border-bottom: 1px solid var(--t-border);
}
/* fixed visual order either interface direction: EN — logo — AR */
.wordmark { display: flex; align-items: center; gap: 14px; direction: ltr; }
.topbar__right {
  position: absolute;
  inset-inline-start: 28px;
  top: 50%;
  transform: translateY(-50%);
}
@media (max-width: 700px) {
  .topbar { flex-direction: column; gap: 8px; }
  .topbar__right { position: static; transform: none; }
}
.wordmark__logo { display: block; width: 38px; height: auto; }
.wordmark__ar {
  font-family: 'Aref Ruqaa', var(--font-display), serif;
  font-size: 1.3rem;
  line-height: 1.3;
  color: var(--t-accent-text);
}
.wordmark__en {
  font-size: .8rem;
  font-weight: 700;
  letter-spacing: .35em;
  color: var(--t-text);
  opacity: .9;
}
.badge {
  font-size: .7rem;
  letter-spacing: .08em;
  color: var(--t-accent-text);
  border: 1px solid var(--gold-900);
  border-radius: 2px;
  padding: 4px 10px;
  white-space: nowrap;
}

/* ---------------- Layout ---------------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 510px 1fr 148px;
  gap: 24px;
  padding: 24px 28px;
  width: 100%;
  max-width: 1560px;
  margin-inline: auto;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
}

/* Flourish strokes panel (second side panel) */
.decor-rail {
  align-self: start;
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 140px);
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--ivory-100);
  border: 1px solid var(--ivory-300);
  border-radius: 6px;
  padding: 10px;
}
.decor-rail__head {
  grid-column: 1 / -1;
  text-align: center;
  font-family: 'Aref Ruqaa', var(--font-display), serif;
  font-size: 1.15rem;
  color: var(--ink-950);
  background: var(--ivory-300);
  border-radius: 4px;
  padding: 2px 0;
  margin-top: 4px;
}
.decor-rail__head:first-child { margin-top: 0; }
.decor-key {
  font-size: 1.35rem;
  height: 52px;
  line-height: 1;
}
@media (max-width: 900px) {
  .decor-rail {
    order: -1;               /* sits right under the sticky stage */
    position: static;
    max-height: none;
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px;
  }
  .decor-rail__head {
    flex: 0 0 auto;
    writing-mode: initial;
    margin: 0 4px;
    padding: 2px 8px;
    align-self: center;
  }
  .decor-key { flex: 0 0 44px; }
}

/* My projects */
.proj-bar { display: flex; gap: 8px; }
.proj-name {
  flex: 1;
  border: 1px solid var(--ivory-300);
  border-radius: 6px;
  padding: 8px 10px;
  font-family: var(--font-ui);
  font-size: .9rem;
  background: #fff;
  color: var(--ink-950);
}
.proj-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.proj-row { display: flex; gap: 6px; align-items: center; }
.proj-open { flex: 1; text-align: start; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------------- Control rail ---------------- */
.rail {
  background: var(--ivory-100);
  color: var(--ink-950);
  border: 1px solid var(--ivory-300);
  border-radius: 6px;
  padding: 22px 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  align-self: start;
  font-family: var(--font-ui); /* Latin: clean sans; Arabic spans: Ruq'ah below */
}
.rail .btn, .rail .chip--toggle { font-family: inherit; }
.rail [lang="ar"] { font-family: 'Aref Ruqaa', var(--font-display), serif; }

/* AR interface: Arabic label leads on the right, English trails left */
html[dir="rtl"] .control__label { flex-direction: row-reverse; }

/* Ruq'ah panel type scale — bumped (Ruq'ah reads small at UI sizes) */
.rail .control__label { font-size: .95rem; }
.rail .control__label [lang="ar"] { font-size: 1.25rem; }
.rail .control__hint { font-size: .92rem; }
.rail .bg-opt { font-size: .98rem; }
.rail .mode-opt { font-size: .85rem; }
.rail .mode-opt [lang="ar"] { font-size: 1.18rem; }
.rail .script-tab { font-size: .8rem; min-height: 72px; justify-content: center; }
.rail .script-tab [lang="ar"] {
  font-size: 1.3rem;
  height: 40px;
  display: flex;
  align-items: center;
  line-height: 1;
}
/* Mashq Thuluth renders light at UI size — embolden its tab sample */
.rail .script-tab [data-ff="thuluth-mashq1"] { font-weight: 700; font-size: 1.5rem; }
.rail .style-card__name { font-size: .82rem; }
.rail .chip--toggle { font-size: .95rem; }
.rail .btn { font-size: 1.05rem; }
.rail .btn small { font-size: .82rem; }

.control__label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .03em;
  color: var(--gold-900);
  margin-bottom: 10px;
}
.control__label [lang="ar"] { font-size: .85rem; letter-spacing: 0; }
.control__label output { color: var(--ink-950); font-weight: 500; letter-spacing: 0; text-transform: none; }

#text-input {
  width: 100%;
  font-family: var(--font-display);
  font-size: 1.45rem;
  line-height: 1.6;
  padding: 10px 14px;
  background: #fff;
  border: 1px solid var(--ivory-300);
  border-radius: 3px;
  color: var(--ink-950);
  resize: vertical;
  min-height: 3.2em;
}
#text-input:focus { border-color: var(--gold-500); }

.chip--tool { margin-top: 8px; background: #fff; }

.chip--toggle { background: #fff; font-family: var(--font-ui); font-size: .72rem; }
.chip--toggle.is-on { background: var(--gold-100); border-color: var(--gold-700); box-shadow: inset 0 0 0 1px var(--gold-700); }

/* Tashkeel palette (F-008) */
.tk-palette { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; align-items: center; }
.tk-key {
  font-family: var(--font-display);
  font-size: 1.05rem;
  min-width: 40px;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 0 6px;
  background: #fff;
  border: 1px solid var(--ivory-300);
  border-radius: 3px;
  color: var(--maroon);
  cursor: pointer;
}
.tk-key:hover { background: var(--gold-100); border-color: var(--gold-500); }
.tk-key--erase { font-family: var(--font-ui); font-size: 1rem; color: var(--ink-950); }

.history-btns { display: inline-flex; gap: 2px; }

.topbar__right { display: flex; gap: 8px; align-items: baseline; }
.badge--btn { cursor: pointer; background: transparent; font-family: var(--font-ui); }
.badge--btn:hover { background: rgba(198, 161, 91, .12); }

.control__hint {
  margin-top: 8px;
  font-size: .72rem;
  line-height: 1.5;
  color: var(--gold-900);
  opacity: .85;
}

/* Composition picker */
.mode-picker { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.mode-opt {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 2px;
  font-size: .66rem;
  letter-spacing: .04em;
  background: #fff;
  border: 1px solid var(--ivory-300);
  border-radius: 3px;
  color: var(--ink-950);
  cursor: pointer;
}
.mode-opt [lang="ar"] { font-family: var(--font-display); font-size: .85rem; color: var(--gold-900); }
.mode-opt.is-selected { border-color: var(--gold-700); box-shadow: inset 0 0 0 1px var(--gold-700); background: var(--gold-100); }

.rail input[type="range"] { width: 100%; accent-color: var(--maroon); }

.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn--ghost { border-color: var(--ivory-300); background: transparent; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  font-family: var(--font-display);
  font-size: .95rem;
  padding: 4px 12px;
  background: transparent;
  border: 1px solid var(--gold-500);
  border-radius: 999px;
  color: var(--gold-900);
  cursor: pointer;
  transition: background .15s ease;
}
.chip:hover { background: var(--gold-100); }

/* Two-level script → style picker */
.script-tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 10px; }
.script-tab {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 7px 4px;
  font-size: .62rem;
  letter-spacing: .05em;
  background: transparent;
  border: 1px solid var(--ivory-300);
  border-radius: 3px;
  color: var(--gold-900);
  cursor: pointer;
}
.script-tab [lang="ar"] { font-family: 'Aref Ruqaa', serif; font-size: .95rem; color: var(--ink-950); }
.script-tab.is-selected { border-color: var(--gold-700); background: var(--gold-100); box-shadow: inset 0 0 0 1px var(--gold-700); }

.style-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
@media (max-width: 900px) {
  .style-list { grid-template-columns: 1fr 1fr; }
}
.style-card {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 12px 6px 8px;
  background: #fff;
  border: 1px solid var(--ivory-300);
  border-radius: 4px;
  cursor: pointer;
  transition: border-color .15s ease;
  min-height: 74px;
  justify-content: center;
}
.style-card:hover { border-color: var(--gold-500); }
.style-card.is-selected { border-color: var(--gold-700); box-shadow: inset 0 0 0 1px var(--gold-700); background: var(--gold-100); }
.style-card__sample { font-size: 1.5rem; line-height: 1.5; color: var(--ink-950); }
.style-card__name { font-size: .66rem; letter-spacing: .05em; color: var(--gold-900); }
.style-card.is-loading { opacity: .55; pointer-events: none; }
.style-placeholder {
  grid-column: 1 / -1;
  border: 1px dashed var(--gold-500);
  border-radius: 4px;
  padding: 14px 16px;
  font-size: .78rem;
  line-height: 1.6;
  color: var(--gold-900);
  background: #fff;
}
.style-placeholder p + p { margin-top: 6px; opacity: .8; }
.style-placeholder [lang="ar"] { font-family: var(--font-display); font-size: .95rem; }

/* Kashida joints strip */
.joints-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0;
  font-family: var(--font-display);
  font-size: 1.6rem;
  line-height: 2;
  background: #fff;
  border: 1px solid var(--ivory-300);
  border-radius: 3px;
  padding: 8px 12px;
  color: var(--ink-950);
}
.joints-strip .js-char { white-space: pre; }
.joints-strip .js-joint {
  display: inline-block;
  min-width: 14px;
  text-align: center;
  background: transparent;
  border: none;
  border-bottom: 2px dotted var(--gold-500);
  color: var(--gold-700);
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  cursor: pointer;
  padding: 0 1px;
}
.joints-strip .js-joint:hover { background: var(--gold-100); }
.joints-strip .js-joint.is-active { border-bottom-style: solid; border-bottom-color: var(--maroon); color: var(--maroon); }

.subcontrol { margin-top: 14px; }

/* Layers panel (P2) */
.layers-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.layer-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: #fff;
  border: 1px solid var(--ivory-300);
  border-radius: 3px;
  cursor: pointer;
}
.layer-row.is-active { border-color: var(--gold-700); box-shadow: inset 0 0 0 1px var(--gold-700); background: var(--gold-100); }
.layer-row__name {
  flex: 1;
  font-family: var(--font-display);
  font-size: 1.05rem;
  color: var(--ink-950);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: start;
  background: none; border: none; cursor: pointer;
}
.layer-row__tools { display: flex; gap: 2px; }
.layer-btn {
  width: 26px; height: 26px;
  display: grid; place-items: center;
  background: transparent;
  border: none;
  border-radius: 3px;
  color: var(--gold-900);
  font-size: .85rem;
  cursor: pointer;
}
.layer-btn:hover { background: var(--gold-100); }
.layer-btn[disabled] { opacity: .3; cursor: default; }
.layer-row.is-hidden .layer-row__name { opacity: .4; }

/* stage: draggable layers */
#stage-canvas svg [data-layer-id] { cursor: grab; }
#stage-canvas svg [data-layer-id].is-dragging { cursor: grabbing; }

/* English hints and numeric readouts keep LTR order inside the RTL rail */
output,
.control__hint,
.stage__hint { direction: ltr; unicode-bidi: isolate; }

/* Swatches */
.swatches { display: flex; gap: 10px; align-items: center; }
.swatches--small .swatch { width: 26px; height: 26px; }
.swatch__same { font-weight: 700; font-size: .8rem; color: var(--ink-950); display: grid; place-items: center; height: 100%; }
.swatch[data-markcolor=""] { background: #fff; }
.swatch {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--c);
  border: 1px solid rgba(0,0,0,.25);
  cursor: pointer;
  position: relative;
}
.swatch.is-selected::after {
  content: '';
  position: absolute; inset: -5px;
  border: 2px solid var(--gold-700);
  border-radius: 50%;
}
.swatch--custom {
  display: grid; place-items: center;
  background: repeating-conic-gradient(#ddd 0% 25%, #fff 0% 50%) 0 0/12px 12px;
  font-weight: 700; color: var(--ink-950);
  overflow: hidden;
}
.swatch--custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* Background picker */
.bg-picker { display: flex; gap: 8px; }
.bg-opt {
  flex: 1;
  padding: 8px 0;
  font-size: .78rem;
  letter-spacing: .06em;
  background: #fff;
  border: 1px solid var(--ivory-300);
  border-radius: 3px;
  color: var(--ink-950);
  cursor: pointer;
}
.bg-opt.is-selected { border-color: var(--gold-700); box-shadow: inset 0 0 0 1px var(--gold-700); background: var(--gold-100); }

/* Export */
.control--export { display: flex; flex-direction: column; gap: 10px; border-top: 1px solid var(--ivory-300); padding-top: 20px; }
.btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  width: 100%;
  padding: 12px 16px;
  font-family: var(--font-ui);
  font-size: .9rem;
  font-weight: 600;
  letter-spacing: .04em;
  background: #fff;
  color: var(--ink-950);
  border: 1px solid var(--gold-700);
  border-radius: 3px;
  cursor: pointer;
  transition: background .15s ease;
}
.btn small { font-weight: 400; font-size: .68rem; opacity: .7; }
.btn:hover:not(:disabled) { background: var(--gold-100); }
.btn--primary { background: var(--maroon); color: #fff; border-color: var(--maroon); }
.btn--primary small { opacity: .8; }
.btn--primary:hover:not(:disabled) { background: #8f1d3a; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---------------- Stage ---------------- */
.stage {
  display: flex; flex-direction: column; gap: 12px; min-width: 0;
  position: sticky;
  top: 16px;
  align-self: start;
  height: calc(100vh - 140px);
  min-height: 420px;
}
@media (max-width: 900px) {
  /* stacked mode: canvas first and sticky, so edits are always visible */
  .stage {
    order: -1;
    position: sticky;
    top: 0;
    z-index: 20;
    height: 44vh;
    min-height: 300px;
    background: var(--t-bg);
    padding-bottom: 8px;
  }
  .stage__hint { display: none; }
  .stage__canvas { min-height: 0; padding: 12px; }
}
.stage__canvas {
  flex: 1;
  min-height: 300px;
  display: grid;
  place-items: center;
  background:
    linear-gradient(var(--t-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--t-grid) 1px, transparent 1px),
    var(--t-stage-bg);
  background-size: 32px 32px, 32px 32px, auto;
  border: 1px solid var(--t-border-strong);
  border-radius: 6px;
  padding: 32px;
  overflow: auto;
}
.stage__canvas svg { max-width: 100%; height: auto; display: block; }
.stage__status {
  color: var(--t-accent-text);
  font-size: .95rem;
  text-align: center;
  line-height: 1.7;
  direction: ltr;
  unicode-bidi: isolate;
}
.stage__status [lang="ar"] {
  font-family: 'Aref Ruqaa', var(--font-display), serif;
  font-size: 1.35em;
}
.stage__status.is-error { color: #C0392B; }
.stage__hint {
  font-size: .75rem;
  color: var(--t-text);
  opacity: .55;
  text-align: center;
  letter-spacing: .04em;
}

/* ---------------- Footer ---------------- */
.foot {
  padding: 14px 28px 20px;
  border-top: 1px solid var(--t-border);
  font-size: .72rem;
  color: var(--t-text);
  opacity: .65;
  text-align: center;
}
.foot a { color: var(--t-accent-text); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* collapsible ornaments */
.orn-details { margin-top: 10px; }
.orn-details summary {
  cursor: pointer;
  font-size: .78rem;
  font-weight: 600;
  color: var(--gold-900);
  letter-spacing: .03em;
}
.orn-details summary [lang="ar"] { font-size: 1rem; }
.orn-details[open] summary { margin-bottom: 4px; }
.shape-add { font-family: var(--font-ui); color: var(--ink-950); }
/* shape layers: hide text-only controls */
.rail[data-kind="shape"] .sec-text,
.rail[data-kind="glyph"] .sec-text { display: none; }
