:root {
  /* الألوان من design/README.md: زمردي للتمييز، ذهبي للتنبيه والاختيار، والأحمر للحذف فقط */
  --bg: #eef1ef;
  --surface: #ffffff;
  --ink: #10201d;
  --muted: #5b6b68;
  --line: #d5dcd9;
  --field-line: #84948f;
  --accent: #12806f;
  --accent-soft: #e0f1ee;
  --accent-ink: #ffffff;
  --gold: #d4a853;
  --gold-soft: #f8efd9;
  --gold-ink: #5e4412;
  --warn: #e8a33d;
  --danger: #b42318;
  --danger-soft: #fbe9e7;
  --stage: #cfdad5;
  --shadow: 0 1px 2px rgba(20,25,35,.06), 0 4px 14px rgba(20,25,35,.06);
  --font-display: "Readex Pro", "IBM Plex Sans Arabic", Tahoma, sans-serif;
  --font-body: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1211; --surface: #151f1d; --ink: #edf2f0; --muted: #8fa19d; --line: #2a3634; --field-line: #66807a;
    --accent: #2fb5a0; --accent-soft: #10332e; --accent-ink: #04231e;
    --gold-soft: #3a2f16; --gold-ink: #f0d9a3;
    --danger: #f07167; --danger-soft: #3a1f1d;
    --stage: #060a09;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink); font-family: var(--font-body); font-size: 14px; line-height: 1.5;
  display: grid; grid-template-rows: auto minmax(0, 1fr); overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
h1, h2 { font-family: var(--font-display); font-weight: 600; margin: 0; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.spacer { flex: 1; }
.ltr { direction: ltr; unicode-bidi: isolate; }
svg { width: 18px; height: 18px; flex: none; }
kbd {
  display: inline-block; min-width: 22px; padding: 0 5px; text-align: center; font-family: inherit; font-size: 12px;
  border: 1px solid var(--field-line); border-bottom-width: 2px; border-radius: 6px; background: var(--bg); color: var(--ink);
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- الرأس ---------- */
header.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px;
  padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 10px; margin-inline-end: 6px; }
.brand h1 { font-size: 18px; }
.brand .sub { color: var(--muted); font-size: 12.5px; }
.hgroup { display: flex; align-items: center; gap: 4px; }
.zoom-label { min-width: 64px; font-variant-numeric: tabular-nums; }

.btn {
  min-height: 36px; padding: 5px 14px; border-radius: 10px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--surface); border: 1px solid var(--field-line); font-weight: 500;
}
.btn:hover:not(:disabled) { background: var(--accent-soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger:hover:not(:disabled) { background: var(--danger-soft); }
.btn.mini { min-height: 32px; padding: 3px 10px; font-size: 13px; border-radius: 8px; }
.btn.icon-only { padding: 0; width: 36px; }
.btn.mini.icon-only { width: 32px; }
.btn:disabled { opacity: .45; cursor: default; }

/* ---------- مساحة العمل ---------- */
.work { display: grid; grid-template-columns: 56px minmax(0, 1fr) 330px; min-height: 0; }

.toolbar {
  background: var(--surface); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; justify-content: space-between; gap: 10px; padding: 8px 0; overflow-y: auto;
}
.tools { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.tool {
  width: 40px; height: 38px; border-radius: 9px; border: 1px solid transparent; background: transparent; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; color: var(--ink);
}
.tool:hover { background: var(--accent-soft); }
.tool[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.tool svg { width: 20px; height: 20px; }
.tool.small { width: 28px; height: 28px; }
.tool.small svg { width: 15px; height: 15px; }
.tool-sep { width: 28px; height: 1px; background: var(--line); margin: 5px 0; }
.colors { position: relative; width: 56px; height: 76px; flex: none; }
.swatch {
  position: absolute; width: 30px; height: 30px; border-radius: 8px; overflow: hidden; cursor: pointer;
  border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--field-line);
}
.swatch input { width: 44px; height: 44px; margin: -7px; border: 0; padding: 0; cursor: pointer; background: none; }
.swatch.primary { top: 4px; inset-inline-start: 8px; z-index: 2; }
.swatch.secondary { top: 22px; inset-inline-start: 22px; z-index: 1; }
#btnSwap { position: absolute; bottom: 0; inset-inline-start: 6px; }

.center { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; grid-template-columns: minmax(0, 1fr); min-width: 0; min-height: 0; }
/* سطر واحد بتمرير أفقي: لو التف لتغيّر ارتفاع مساحة الرسم مع كل أداة وانزاحت الصورة */
.optbar {
  display: flex; flex-wrap: nowrap; align-items: center; gap: 6px 12px; height: 46px;
  padding: 0 12px; background: var(--surface); border-bottom: 1px solid var(--line); font-size: 13px;
  overflow-x: auto; overflow-y: hidden; white-space: nowrap; scrollbar-width: thin;
}
.optbar > * { flex: none; }
.opt-title { font-family: var(--font-display); font-weight: 600; font-size: 13.5px; margin-inline-end: 4px; }
.opt { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.opt.check { color: var(--ink); }
.opt input[type="range"] { width: 80px; accent-color: var(--accent); }
.optbar .num { width: 58px; }
.opt-label { color: var(--muted); }
.opt-sep { width: 1px; height: 22px; background: var(--line); }
.colon { color: var(--muted); }
.num {
  width: 70px; min-height: 30px; padding: 2px 6px; border-radius: 7px; text-align: center;
  border: 1px solid var(--field-line); background: var(--surface); color: var(--ink); font-variant-numeric: tabular-nums;
}
select {
  min-height: 30px; padding: 2px 8px; border-radius: 7px; border: 1px solid var(--field-line); background: var(--surface); color: var(--ink);
}
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; }
input[type="color"] { width: 36px; height: 30px; padding: 1px; border: 1px solid var(--field-line); border-radius: 7px; background: var(--surface); cursor: pointer; }
.seg { display: inline-flex; border: 1px solid var(--field-line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 4px 10px; min-height: 30px; cursor: pointer; font-size: 13px; }
.seg button + button { border-inline-start: 1px solid var(--line); }
.seg button:hover { background: var(--accent-soft); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

.stage {
  position: relative; overflow: hidden; background: var(--stage); touch-action: none; user-select: none; -webkit-user-select: none;
}
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#overlay { pointer-events: none; }
.stage[data-cursor="none"] { cursor: none; }
.stage[data-cursor="crosshair"] { cursor: crosshair; }
.stage[data-cursor="grab"], .stage.pan-ready { cursor: grab; }
.stage.panning { cursor: grabbing; }
.stage.drop { outline: 3px dashed var(--accent); outline-offset: -8px; }
.empty {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  padding: 24px; text-align: center; color: var(--ink); background: var(--bg);
}
.empty > svg { width: 64px; height: 64px; color: var(--accent); opacity: .8; }
.empty h2 { font-size: 20px; }
.empty .small { max-width: 460px; }
.new-blank {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; margin-top: 14px;
  padding: 10px 14px; border: 1px dashed var(--field-line); border-radius: 12px; color: var(--muted);
}
.mini-field { display: inline-flex; flex-direction: column; gap: 1px; font-size: 11.5px; color: var(--muted); }
.x { color: var(--muted); }

.status {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px; min-height: 30px;
  padding: 4px 12px; background: var(--surface); border-top: 1px solid var(--line); font-size: 12.5px; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.status { flex-wrap: nowrap; white-space: nowrap; height: 30px; }
#penNote { color: var(--accent); }
.status .spacer { flex: 0 0 8px; }
.hint { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

/* ---------- اللوحة الجانبية ---------- */
.side { background: var(--surface); border-inline-start: 1px solid var(--line); overflow-y: auto; min-height: 0; }
.side > details { border-bottom: 1px solid var(--line); }
.side summary {
  cursor: pointer; padding: 11px 14px; font-family: var(--font-display); font-weight: 600; font-size: 14px; list-style-position: inside;
}
.side summary:hover { background: var(--accent-soft); }
.pbody { padding: 0 14px 14px; display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.row-range { display: grid; grid-template-columns: 1fr 90px 64px; align-items: center; gap: 6px; font-size: 13px; }
.row-range input[type="range"] { width: 100%; accent-color: var(--accent); }
.row-range .num { width: 64px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; min-height: 26px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.field select { min-height: 34px; color: var(--ink); }
#imgResize { display: flex; flex-direction: column; gap: 8px; }
#selModify { display: flex; flex-direction: column; gap: 6px; }
#selModify .num { width: 58px; }
#bgBox, #refineBox, #exportBox { display: flex; flex-direction: column; gap: 8px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }
.sub-details { border: 1px solid var(--line); border-radius: 10px; }
.sub-details summary { padding: 8px 10px; font-size: 13.5px; }
.sub-details > :not(summary) { margin: 0 10px 10px; }

.reg-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.reg-item {
  display: grid; grid-template-columns: 22px 56px minmax(0, 1fr); grid-template-rows: auto auto; gap: 6px 8px; align-items: start;
  padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); cursor: pointer;
}
.reg-item.selected { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold); }
.reg-num {
  width: 22px; height: 22px; border-radius: 6px; background: var(--accent); color: var(--accent-ink);
  display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 600;
}
.reg-item.selected .reg-num { background: var(--gold); color: #2a1d05; }
.reg-item canvas { width: 56px; height: 56px; border-radius: 6px; background: var(--surface); border: 1px solid var(--line); }
.reg-body { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.reg-body input[type="text"] {
  width: 100%; min-height: 30px; padding: 2px 8px; border-radius: 7px; border: 1px solid var(--field-line); background: var(--surface);
}
.reg-meta { font-size: 12px; color: var(--muted); }
.reg-fields { display: flex; flex-wrap: wrap; gap: 4px; }
.reg-fields .num { width: 62px; }
.reg-actions { grid-column: 1 / -1; display: flex; gap: 4px; justify-content: flex-end; }

/* ---------- الرسائل ---------- */
.toast {
  position: fixed; bottom: 44px; left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: min(560px, calc(100vw - 32px)); padding: 10px 16px; border-radius: 12px;
  background: #0b1211; color: #edf2f0; box-shadow: 0 10px 30px -10px rgba(0,0,0,.5); font-size: 13.5px;
  transition: opacity .2s, transform .2s; z-index: 10; border-inline-start: 4px solid var(--accent);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.warn { border-inline-start-color: var(--warn); }
.toast.danger { border-inline-start-color: #e0355b; }
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }

/* ---------- الشاشات الصغيرة والأجهزة اللوحية العمودية ---------- */
@media (max-width: 1000px) {
  body { overflow: auto; grid-template-rows: auto auto; }
  .work { grid-template-columns: 56px minmax(0, 1fr); }
  .center { height: 72vh; }
  .side { grid-column: 1 / -1; border-inline-start: 0; border-top: 1px solid var(--line); }
  .hint { display: none; }
}
@media (max-width: 560px) {
  .brand .sub { display: none; }
  .work { grid-template-columns: minmax(0, 1fr); }
  .toolbar { flex-direction: row; overflow-x: auto; padding: 4px 8px; border-inline-end: 0; border-bottom: 1px solid var(--line); }
  .tools { flex-direction: row; }
  .tool-sep { width: 1px; height: 28px; margin: 0 4px; }
  .colors { width: 70px; height: 40px; }
  .swatch.primary { top: 2px; }
  .swatch.secondary { top: 8px; inset-inline-start: 30px; }
  #btnSwap { bottom: auto; top: 6px; inset-inline-start: auto; inset-inline-end: -6px; }
  .center { height: 70vh; }
}
