:root {
  --bg: #faf6f0;
  --surface: #ffffff;
  --surface-2: #f3ece2;
  --ink: #2b2420;
  --muted: #7d6e60;
  --line: #e4d8c8;
  --line-soft: #cdbca5;
  --accent: #b5523b;
  --accent-ink: #ffffff;
  --accent-soft: #f5e1d9;
  --ok: #3d7a4f;
  --ok-soft: #e1efe3;
  --warn: #9a6a12;
  --warn-soft: #f8ecd0;
  --danger: #b3261e;
  --danger-soft: #f6d5cc;
  --fabric: #f6efe4;
  --c1: #ebc9b5; --c2: #cfe0d0; --c3: #d6d3ec; --c4: #f1dfaa; --c5: #c9dde9; --c6: #ead0de;
  --piece-fill: #fffdf9;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(60 40 20 / 6%), 0 4px 16px rgb(60 40 20 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1714; --surface: #24201c; --surface-2: #2e2823; --ink: #f1e9df; --muted: #b3a493;
    --line: #3b332c; --line-soft: #5a4d41; --accent: #e07a5f; --accent-ink: #1b1714; --accent-soft: #3d2a23;
    --ok: #7fbf8f; --ok-soft: #22342a; --warn: #e3b55a; --warn-soft: #3a3020; --danger: #f08a80; --danger-soft: #3f2420;
    --fabric: #2c2621; --c1: #6b4a3a; --c2: #3e5a45; --c3: #4a4766; --c4: #6a5a2a; --c5: #3b5566; --c6: #5e4352;
    --piece-fill: #24201c; --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1714; --surface: #24201c; --surface-2: #2e2823; --ink: #f1e9df; --muted: #b3a493;
  --line: #3b332c; --line-soft: #5a4d41; --accent: #e07a5f; --accent-ink: #1b1714; --accent-soft: #3d2a23;
  --ok: #7fbf8f; --ok-soft: #22342a; --warn: #e3b55a; --warn-soft: #3a3020; --danger: #f08a80; --danger-soft: #3f2420;
  --fabric: #2c2621; --c1: #6b4a3a; --c2: #3e5a45; --c3: #4a4766; --c4: #6a5a2a; --c5: #3b5566; --c6: #5e4352;
  --piece-fill: #24201c; --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  font-size: 16px;
  line-height: 1.6;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.num { direction: ltr; unicode-bidi: isolate; font-variant-numeric: tabular-nums; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 700; font-size: 20px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent);
  mask: none; position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 5px 7px 5px 7px;
  border: 2px dashed var(--accent-ink); border-radius: 2px 6px 2px 2px;
}
.brand-tag { color: var(--muted); font-size: 14px; }

main { max-width: 980px; margin: 0 auto; padding: 16px 16px 96px; outline: none; }

h1 { font-size: 26px; line-height: 1.3; margin: 8px 0 4px; }
h2 { font-size: 19px; margin: 24px 0 10px; }
h3 { font-size: 16px; margin: 0 0 6px; }
.lead { color: var(--muted); margin: 0 0 16px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font-weight: 500; text-decoration: none; cursor: pointer;
}
.btn:hover { border-color: var(--line-soft); }
.btn:focus-visible, .chip:focus-visible, .choice:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.05); border-color: var(--accent); }
.btn-big { min-height: 64px; font-size: 19px; width: 100%; border-radius: 16px; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.hero {
  display: grid; gap: 16px; align-items: center;
  background: linear-gradient(135deg, var(--accent-soft), var(--surface-2));
  border-radius: 20px; padding: 22px; margin-bottom: 8px;
}
.hero p { margin: 0; color: var(--muted); }

.list { display: flex; flex-direction: column; gap: 8px; }
.list-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line);
  text-decoration: none; color: var(--ink);
}
.list-item .grow { flex: 1; min-width: 0; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700;
  background: var(--accent-soft); color: var(--accent);
}
.thumb { width: 52px; height: 52px; border-radius: 10px; object-fit: cover; flex: none; background: var(--surface-2); display: grid; place-items: center; color: var(--muted); }
.empty { padding: 18px; text-align: center; color: var(--muted); border: 1px dashed var(--line-soft); border-radius: 12px; }

.photo-drop {
  display: grid; place-items: center; gap: 8px; text-align: center;
  min-height: 220px; padding: 20px; border-radius: 18px;
  border: 2px dashed var(--line-soft); background: var(--surface);
}
.photo-preview { width: 100%; max-height: 360px; object-fit: contain; border-radius: 14px; background: var(--surface-2); }
.choice-grid { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
@media (max-width: 560px) { .choice-grid { grid-template-columns: 1fr; } }
.choice {
  text-align: start; display: flex; flex-direction: column; gap: 4px;
  padding: 16px; border-radius: 14px; border: 2px solid var(--line); background: var(--surface); cursor: pointer;
}
.choice strong { font-size: 17px; }
.choice span { color: var(--muted); font-size: 14px; }
.choice[aria-pressed="true"], .choice:hover { border-color: var(--accent); }

.garments { display: grid; gap: 10px; grid-template-columns: repeat(5, 1fr); }
@media (max-width: 640px) { .garments { grid-template-columns: repeat(3, 1fr); } }
.garment {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 12px 6px; border-radius: 14px; border: 2px solid var(--line); background: var(--surface); cursor: pointer;
}
.garment svg { width: 46px; height: 46px; color: var(--muted); }
.garment[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.garment[aria-pressed="true"] svg { color: var(--accent); }

.opt { padding: 12px 0; border-top: 1px solid var(--line); }
.opt:first-child { border-top: 0; }
.opt-label { display: flex; align-items: center; gap: 8px; font-weight: 500; margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px; padding: 6px 14px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface);
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.flag { font-size: 13px; padding: 2px 8px; border-radius: 999px; background: var(--warn-soft); color: var(--warn); font-weight: 500; }
.flag-ok { background: var(--ok-soft); color: var(--ok); }

.analysis-summary { display: grid; grid-template-columns: 120px 1fr; gap: 14px; align-items: start; }
.analysis-summary img { width: 120px; height: 150px; object-fit: cover; border-radius: 12px; }
@media (max-width: 480px) { .analysis-summary { grid-template-columns: 84px 1fr; } .analysis-summary img { width: 84px; height: 110px; } }

.note { padding: 12px 14px; border-radius: 12px; background: var(--surface-2); font-size: 14px; }
.note-warn { background: var(--warn-soft); color: var(--warn); }
.note-danger { background: var(--danger-soft); color: var(--danger); }
.note-ok { background: var(--ok-soft); color: var(--ok); }

.field { display: grid; grid-template-columns: 1fr 120px; gap: 6px 12px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.field:first-of-type { border-top: 0; }
.field label { font-weight: 500; }
.field .hint { grid-column: 1 / -1; font-size: 13px; color: var(--muted); margin-top: -4px; }
.field .err { grid-column: 1 / -1; font-size: 13px; color: var(--danger); }
.field .warn { grid-column: 1 / -1; font-size: 13px; color: var(--warn); }
.unit-input { position: relative; }
.unit-input input {
  width: 100%; min-height: 44px; padding: 8px 12px 8px 36px; border-radius: 10px;
  border: 1px solid var(--line-soft); background: var(--surface); text-align: center; direction: ltr;
}
.unit-input::after { content: "سم"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 13px; }
.unit-input input[aria-invalid="true"] { border-color: var(--danger); }
.text-input, select {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: 10px;
  border: 1px solid var(--line-soft); background: var(--surface);
}
.suggest { grid-column: 1 / -1; font-size: 13px; }

.range-row { display: flex; align-items: center; gap: 12px; }
.range-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.range-val { min-width: 56px; text-align: center; font-weight: 700; }

.steps { display: flex; gap: 6px; margin: 4px 0 14px; }
.steps span { flex: 1; height: 4px; border-radius: 4px; background: var(--line); }
.steps span.on { background: var(--accent); }

.sticky-actions {
  position: fixed; inset: auto 0 0 0; z-index: 9;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
}
.sticky-actions .inner { max-width: 980px; margin: 0 auto; display: flex; gap: 10px; }
.sticky-actions .btn { flex: 1; }

.yardage {
  display: flex; align-items: center; gap: 14px; padding: 16px; border-radius: 16px;
  background: var(--accent-soft); color: var(--ink);
}
.yardage .big { font-size: 30px; font-weight: 700; color: var(--accent); line-height: 1; white-space: nowrap; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 18px 0 14px; overflow-x: auto; }
.tab {
  padding: 10px 14px; border: 0; background: none; cursor: pointer; white-space: nowrap;
  border-bottom: 3px solid transparent; color: var(--muted); font-weight: 500;
}
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }

.pieces { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.piece-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px;
  cursor: zoom-in; color: var(--ink);
}
.piece-card svg { display: block; height: auto; }
.layout-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.layout-svg { width: 100%; max-width: 520px; height: auto; display: block; margin: 0 auto; font-family: inherit; }
.piece-svg { font-family: inherit; }

.checks { width: 100%; border-collapse: collapse; font-size: 14px; }
.checks th, .checks td { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: start; vertical-align: top; }
.checks td.n { direction: ltr; text-align: end; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ok-dot { color: var(--ok); font-weight: 700; }
.bad-dot { color: var(--warn); font-weight: 700; }

ol.sew { padding-inline-start: 22px; }
ol.sew li { margin-bottom: 8px; }

.tweak { display: grid; gap: 14px; margin-top: 12px; }
.tweak-box summary { cursor: pointer; display: flex; gap: 8px; align-items: baseline; min-height: 28px; }
.tweak-box:not([open]) { padding-block: 12px; }

dialog.zoom {
  width: min(96vw, 900px); max-height: 92vh; border: 0; border-radius: 16px; padding: 12px;
  background: var(--surface); color: var(--ink);
}
dialog.zoom::backdrop { background: rgb(0 0 0 / 45%); }
dialog.zoom svg { width: 100%; height: auto; max-height: 78vh; }

.spinner {
  width: 42px; height: 42px; border-radius: 50%;
  border: 4px solid var(--line); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.scan { position: relative; overflow: hidden; border-radius: 14px; }
.scan::after {
  content: ""; position: absolute; inset-inline: 0; height: 30%; top: -30%;
  background: linear-gradient(to bottom, transparent, color-mix(in srgb, var(--accent) 35%, transparent), transparent);
  animation: scan 1.6s ease-in-out infinite;
}
@keyframes scan { to { top: 100%; } }
@media (prefers-reduced-motion: reduce) { .spinner, .scan::after { animation: none; } }

#toast {
  position: fixed; inset-inline: 16px; bottom: 90px; margin: 0 auto; max-width: 420px; z-index: 20;
  padding: 12px 16px; border-radius: 12px; background: var(--ink); color: var(--bg);
  opacity: 0; transform: translateY(8px); transition: 0.2s; pointer-events: none; text-align: center;
}
#toast.show { opacity: 1; transform: none; }

.feedback-q { padding: 12px 0; border-top: 1px solid var(--line); }
.feedback-q:first-child { border-top: 0; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 13px; color: var(--muted); margin-top: 10px; }
.legend i { display: inline-block; width: 26px; vertical-align: middle; border-top: 2px solid var(--ink); margin-inline-end: 6px; }
.legend i.dash { border-top-style: dashed; opacity: 0.7; }

/* ---------- القياس من الصورة ---------- */
.scan-cta { display: flex; align-items: center; gap: 12px; margin-top: 12px; text-decoration: none; color: var(--ink); border-color: var(--accent); }
.scan-cta-icon { font-size: 28px; }
.scan-howto { display: grid; gap: 12px; }
.scan-illus { width: 100%; max-width: 320px; height: auto; margin: 0 auto; display: block; border-radius: 10px; }
.scan-editor { position: relative; margin-top: 12px; border-radius: 14px; overflow: hidden; background: #111; user-select: none; -webkit-user-select: none; }
.scan-editor > svg { display: block; width: 100%; height: auto; max-height: 72vh; }
.scan-editor .handle { touch-action: none; }
.loupe {
  position: absolute; top: 10px; right: 10px; width: 132px; height: 132px; border-radius: 50%;
  overflow: hidden; border: 3px solid #fff; box-shadow: 0 4px 18px rgb(0 0 0 / 45%); pointer-events: none; background: #111;
}
.loupe.left { right: auto; left: 10px; }
.loupe svg { width: 100%; height: 100%; display: block; }
.point-guide { display: flex; gap: 14px; align-items: center; margin-top: 12px; }
.point-guide .grow { flex: 1; min-width: 0; }
.schematic { width: 78px; height: 94px; flex: none; }
.pt-chips { margin-top: 10px; flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; scrollbar-width: thin; }
.pt-chips .chip { font-size: 13px; min-height: 34px; padding: 4px 10px; white-space: nowrap; flex: none; }

/* ---------- التدريج ---------- */
.nest-wrap { display: flex; justify-content: center; }
.nest-svg { width: 100%; max-width: 420px; max-height: 60vh; height: auto; display: block; }
.size-open { min-height: 36px; padding: 4px 10px; }
