:root {
  --bg: #f6f6f6;
  --panel: #ffffff;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #e3e3e6;
  --accent: #d7262e;
  --accent-ink: #ffffff;
  --accent-soft: #fdeceb;
  --warn: #9a5b00;
  --warn-soft: #fff4e0;
  --danger: #b42318;
  --hot: rgba(215, 38, 46, 0.10);
  --hot-line: rgba(215, 38, 46, 0.55);
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.08);
  --ui: "Segoe UI", Tahoma, "Noto Sans Arabic", "IBM Plex Sans Arabic", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161616; --panel: #212121; --ink: #ededed; --muted: #a3a3a3; --line: #353535;
    --accent: #ff5a5f; --accent-ink: #1f0506; --accent-soft: #3a1a1b;
    --warn: #f0b35a; --warn-soft: #3a2c14; --danger: #f07167;
    --hot: rgba(255, 90, 95, 0.16); --hot-line: rgba(255, 90, 95, 0.7);
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161616; --panel: #212121; --ink: #ededed; --muted: #a3a3a3; --line: #353535;
  --accent: #ff5a5f; --accent-ink: #1f0506; --accent-soft: #3a1a1b;
  --warn: #f0b35a; --warn-soft: #3a2c14; --danger: #f07167;
  --hot: rgba(255, 90, 95, 0.16); --hot-line: rgba(255, 90, 95, 0.7);
  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(--ui); font-size: 15px; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: .4; cursor: default; }

.bar, .pages, .editor { -webkit-user-select: none; user-select: none; }
.ed-text { -webkit-user-select: text; user-select: text; }
.bar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
a.brand { color: inherit; text-decoration: none; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
.logo { width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 15px; }
.tools { display: flex; align-items: center; gap: 6px; flex: 1; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; margin-inline-start: auto; }
.sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 9px; padding: 2px; }
.seg-btn { border: 0; background: transparent; padding: 5px 12px; border-radius: 7px; font-size: 14px; }
.seg-btn.active { background: var(--panel); box-shadow: 0 1px 2px rgba(0,0,0,.12); color: var(--accent); font-weight: 600; }
.seg.small .seg-btn { padding: 3px 8px; }
.icon { width: 32px; height: 32px; border: 1px solid transparent; background: transparent; border-radius: 8px; font-size: 18px; line-height: 1; }
.icon:hover:not(:disabled) { background: var(--bg); border-color: var(--line); }
.zoom { min-width: 46px; text-align: center; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.btn { border: 1px solid var(--line); background: var(--panel); padding: 7px 14px; border-radius: 9px; font-weight: 600; white-space: nowrap; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost:hover { background: var(--bg); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.chip { border: 1px solid var(--line); background: var(--bg); padding: 4px 10px; border-radius: 99px; font-size: 12.5px; color: var(--muted); }
.chip.on { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.link { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 0; text-decoration: underline; }

.empty { display: grid; place-items: center; min-height: calc(100vh - 60px); padding: 24px 16px; }
.drop { max-width: 620px; width: 100%; text-align: center; background: var(--panel); border: 2px dashed var(--line); border-radius: 18px; padding: 40px 28px; }
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop-icon { width: 64px; height: 76px; margin: 0 auto 16px; border-radius: 10px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 800; letter-spacing: 1px; }
.drop h1 { font-size: 22px; margin: 0 0 8px; }
.drop p { color: var(--muted); margin: 0 0 20px; }
.points { list-style: none; padding: 0; margin: 0; text-align: start; display: grid; gap: 8px; color: var(--muted); font-size: 14px; }
.points b { color: var(--ink); }

.pages { display: flex; flex-direction: column; align-items: flex-start; gap: 18px; padding: 24px 16px 120px; overflow-x: auto; }
/* auto margins centre small pages but never push wide (zoomed) pages out of reach */
.page { position: relative; background: #fff; box-shadow: var(--shadow); flex: none; margin-inline: auto; }
.page canvas { display: block; }
.pages { cursor: grab; }
.pages.panning, .pages.panning * { cursor: grabbing !important; }
.page .num { position: absolute; bottom: -20px; inset-inline-start: 0; font-size: 12px; color: var(--muted); }
.overlay { position: absolute; inset: 0; }
.mode-add .overlay { cursor: text; }
.hot { position: absolute; border-radius: 3px; cursor: text; outline: 1px solid transparent; }
.mode-edit .hot:hover { background: var(--hot); outline-color: var(--hot-line); }
.mode-add .hot { pointer-events: none; }
.editbox { position: absolute; cursor: text; border-radius: 3px; outline: 1px dashed transparent; }
.editbox:hover { outline-color: var(--hot-line); }
.cover { position: absolute; pointer-events: none; }
.ed-text {
  position: absolute; margin: 0; padding: 0; border: 0; resize: none; overflow: hidden; white-space: pre;
  background: transparent; outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 2px;
  color: #000; z-index: 5; min-width: 40px;
}

/* edit toolbar: docked under the header, full width */
.editor {
  position: fixed; z-index: 29; left: 0; right: 0; top: 56px; background: var(--panel);
  border-bottom: 1px solid var(--line); box-shadow: 0 6px 16px rgba(0,0,0,.08); padding: 8px 16px;
}
.editor .ed-bar, .editor .ed-status { max-width: 1100px; margin-inline: auto; }
.ed-bar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.ed-bar select, .ed-bar input[type=number] { height: 30px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); font: inherit; font-size: 13px; padding: 0 6px; }
.ed-bar select { max-width: 220px; }
.ed-bar input[type=number] { width: 64px; }
.ed-bar input[type=color] { width: 32px; height: 30px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: var(--bg); }
.ed-status { font-size: 12.5px; padding: 6px 4px 2px; color: var(--muted); line-height: 1.5; }
.ed-status.ok { color: var(--accent); }
.ed-status.warn { color: var(--warn); }
.ed-status .link { font-size: 12.5px; }
.warn-inline { color: var(--warn); display: block; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; max-width: calc(100vw - 32px); font-size: 14px; }
.busy { position: fixed; inset: 0; background: rgba(0,0,0,.08); z-index: 55; display: grid; place-items: center; }
.spinner { width: 36px; height: 36px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 720px) {
  body { font-size: 14px; }
  .actions .btn { padding: 6px 10px; }
  .seg-btn { padding: 5px 9px; }
  .chip { font-size: 11.5px; }
  .editor { padding: 6px 10px; }
  .ed-bar { gap: 4px; }
  .ed-bar select { max-width: 46vw; }
  .drop { padding: 28px 16px; }
  .drop h1 { font-size: 19px; }
  .bar { padding: 8px 16px; gap: 8px; }
  .brand span:last-child { display: none; }
  .tools { order: 3; flex-basis: 100%; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: 4px; }
  .tools::-webkit-scrollbar { display: none; }
  .tools > * { flex: none; }
  .sep { display: none; }
  .pages { padding: 16px 16px 120px; }
}

/* ---------- tools v2 ---------- */
.tool { border: 1px solid transparent; background: transparent; border-radius: 9px; padding: 5px 10px; font-size: 14px; white-space: nowrap; }
.tool:hover { background: var(--bg); border-color: var(--line); }
.spacer { flex: 1; }
.hint { font-size: 13px; color: var(--muted); }

/* landing */
.empty { flex-direction: column; gap: 22px; align-content: center; }
.empty { display: flex; align-items: center; justify-content: center; }
.privacy { font-size: 13.5px; color: var(--muted); margin: 6px 0 0 !important; }
.features { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; max-width: 900px; width: 100%; }
.feature { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px; display: flex; flex-direction: column; gap: 4px; }
.feature i { font-style: normal; font-size: 22px; }
.feature b { font-size: 14.5px; }
.feature span { font-size: 13px; color: var(--muted); line-height: 1.6; }
.howto { list-style: none; counter-reset: h; display: flex; gap: 10px; padding: 0; margin: 0; flex-wrap: wrap; justify-content: center; }
.howto li { counter-increment: h; display: flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 99px; padding: 6px 14px 6px 8px; font-size: 13.5px; }
.howto li::before { content: counter(h); width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 700; font-size: 12px; }
.howto li span { color: var(--muted); }

/* workspace + page sidebar */
.workspace { display: flex; align-items: flex-start; }
.sidebar {
  position: sticky; top: 49px; flex: none; width: 190px; height: calc(100vh - 49px); overflow: auto;
  background: var(--panel); border-inline-end: 1px solid var(--line); padding: 10px; z-index: 5;
}
.sidebar.closed { display: none; }
.workspace .pages { flex: 1; min-width: 0; }
.sbHead { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.sbTitle { display: flex; justify-content: space-between; align-items: center; font-size: 14px; }
.sbTitle span { color: var(--muted); font-size: 12px; }
.sbActions { display: grid; gap: 6px; }
.sbActions .btn { width: 100%; }
.sbSel { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; font-size: 12.5px; background: var(--accent-soft); border-radius: 10px; padding: 6px 8px; }
.sbSel .link { font-size: 12.5px; }
.sbHint { margin: 0; font-size: 11.5px; color: var(--muted); }
.thumbs { display: flex; flex-direction: column; gap: 10px; }
.thumb { position: relative; border: 2px solid transparent; border-radius: 10px; padding: 6px; cursor: grab; background: var(--bg); }
.thumb:hover { border-color: var(--line); }
.thumb.current { border-color: var(--accent); }
.thumb.sel { background: var(--accent-soft); }
.thumb.dragging { opacity: .4; }
.thumb.dropBefore { box-shadow: 0 -3px 0 var(--accent); }
.thumb.dropAfter { box-shadow: 0 3px 0 var(--accent); }
.thumb .tcanvas { display: block; margin: 0 auto; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15); min-height: 60px; }
.thumb .tbar { display: flex; align-items: center; gap: 4px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.thumb .tbar input { margin: 0; }
.thumb .tbar .n { flex: 1; text-align: center; font-weight: 600; }
.thumb .tbtn { border: 0; background: transparent; border-radius: 6px; width: 24px; height: 24px; font-size: 13px; color: var(--muted); }
.thumb .tbtn:hover { background: var(--panel); color: var(--ink); }

/* image placement */
.placer { position: absolute; z-index: 6; border: 1.5px dashed var(--accent); cursor: move; touch-action: none; }
.placer img { width: 100%; height: 100%; display: block; pointer-events: none; user-select: none; }
.placer .h { position: absolute; width: 14px; height: 14px; background: var(--panel); border: 2px solid var(--accent); border-radius: 50%; touch-action: none; }
.placer .h.tl { top: -8px; left: -8px; cursor: nwse-resize; }
.placer .h.tr { top: -8px; right: -8px; cursor: nesw-resize; }
.placer .h.bl { bottom: -8px; left: -8px; cursor: nesw-resize; }
.placer .h.br { bottom: -8px; right: -8px; cursor: nwse-resize; }
.mode-edit .imgbox, .mode-add .imgbox { position: absolute; cursor: pointer; border-radius: 2px; outline: 1px dashed transparent; }
.imgbox:hover { outline-color: var(--hot-line); }

/* dialogs */
.dlg { border: 0; border-radius: 16px; padding: 0; width: min(680px, calc(100vw - 32px)); background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.dlg::backdrop { background: rgba(0, 0, 0, .35); }
.dlgBody { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dlgBody h2 { margin: 0; font-size: 18px; }
.dlgBody label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.dlgBody input[type=text], .dlgBody input[type=number], .dlgBody select { height: 36px; border: 1px solid var(--line); border-radius: 9px; background: var(--bg); color: var(--ink); font: inherit; padding: 0 8px; }
.dlgBody label.check { flex-direction: row; align-items: center; gap: 8px; align-self: end; }
.grid2 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.dlgRow { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.dlgActions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
#sigCanvas { width: 100%; height: auto; aspect-ratio: 640 / 240; background: #fff; border: 1px dashed var(--line); border-radius: 12px; touch-action: none; cursor: crosshair; }
#sigPreview { max-width: 100%; max-height: 180px; background: repeating-conic-gradient(#eee 0 25%, #fff 0 50%) 0 0 / 16px 16px; border-radius: 8px; }
.swatch { width: 24px; height: 24px; border-radius: 50%; border: 2px solid transparent; outline: 1px solid var(--line); }
.swatch.active { border-color: var(--panel); outline: 2px solid var(--accent); }
.muted { color: var(--muted); font-size: 13px; margin: 0; }

@media (max-width: 900px) {
  .features { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 720px) {
  .sidebar { position: fixed; top: auto; bottom: 0; inset-inline: 0; width: auto; height: 46vh; border-inline-end: 0; border-top: 1px solid var(--line); box-shadow: 0 -10px 30px rgba(0,0,0,.15); z-index: 35; }
  .thumbs { flex-direction: row; overflow-x: auto; }
  .thumb { flex: none; width: 120px; }
  .grid2 { grid-template-columns: 1fr 1fr; }
  .tool { padding: 5px 8px; }
}
.legal { margin: 2px 0 0; text-align: center; font-size: 12.5px; color: var(--muted); }
.legal a { color: inherit; }
