/* ============================================================
   Document editor ("معاينة وتعديل")
   Editor-only guides (writing area, protected regions, selection)
   live here and never reach the exported PDF.
   ============================================================ */
html.kde-open, html.kde-open body { overflow: hidden; }

.kde {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; flex-direction: column;
  background: var(--brand-bg, #050a11); color: var(--brand-text, #e8eef5);
  font-family: var(--brand-font, "Segoe UI", Tahoma, Arial, sans-serif);
  padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
  touch-action: manipulation;
}
.kde[hidden] { display: none; }

.kde-top {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  border-bottom: 1px solid var(--brand-border, #1e3a5f);
}
.kde-title { flex: 1; min-width: 0; font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kde-state { font-size: 12px; color: var(--brand-green, #22c55e); white-space: nowrap; }
.kde-state.kde-unsaved { color: var(--brand-gold, #d4af37); }

.kde-bar {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px;
  overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: none;
  border-bottom: 1px solid var(--brand-border, #1e3a5f); background: var(--brand-card, #0d1a2a);
}
.kde-bar::-webkit-scrollbar { display: none; }
.kde-bar[hidden] { display: none; }
.kde-bar.kde-dimmed .kde-btn:not(#kdeSafeBtn) { opacity: .35; pointer-events: none; }
.kde-bar-label { font-size: 12px; color: var(--brand-text-dim, #8fa3b8); white-space: nowrap; }
.kde-tablebar { background: #102238; }
.kde-safebar { background: #1d1a0c; }
.kde-sep { width: 1px; align-self: stretch; background: var(--brand-border, #1e3a5f); flex: none; }

.kde-btn {
  flex: none; min-width: 44px; height: 40px; padding: 0 10px;
  border-radius: 10px; border: 1px solid var(--brand-border, #1e3a5f);
  background: var(--brand-bg, #050a11); color: var(--brand-text, #e8eef5);
  font-size: 14px; white-space: nowrap; cursor: pointer;
}
.kde-btn:disabled { opacity: .35; cursor: default; }
.kde-btn:not(:disabled):active { transform: scale(.97); }
.kde-btn:focus-visible { outline: 2px solid var(--brand-gold, #d4af37); outline-offset: 1px; }
.kde-gold { background: linear-gradient(135deg, var(--brand-gold, #d4af37), var(--brand-gold-dark, #a8862a)); color: #050a11; border: none; font-weight: 700; }
.kde-strong { border-color: var(--brand-gold, #d4af37); }
.kde-close { min-width: 40px; }

.kde-msg { padding: 6px 12px; font-size: 13px; color: var(--brand-gold-light, #f0d77b); }
.kde-msg[hidden] { display: none; }
.kde-msg.kde-msg-error { color: #fca5a5; }

.kde-pages {
  flex: 1; overflow: auto; padding: 12px; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
}

/* A page = the template page as background + the blocks on top, 1:1. */
.kde-page {
  position: relative; flex: none; background: #fff center / 100% 100% no-repeat;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .5); color: #000;
  font-family: Tahoma, "Geeza Pro", "Segoe UI", Arial, sans-serif;
}
.kde-page-no {
  position: absolute; bottom: -18px; inset-inline: 0; text-align: center;
  font-size: 11px; color: var(--brand-text-dim, #8fa3b8); font-family: var(--brand-font);
}
.kde-safe { position: absolute; outline: 1.5px dashed rgba(212, 175, 55, .85); pointer-events: none; }
.kde-safe-edit { outline: 2px solid var(--brand-gold, #d4af37); background: rgba(212, 175, 55, .06); pointer-events: auto; }
.kde-protected {
  position: absolute; pointer-events: none;
  background: repeating-linear-gradient(45deg, rgba(239, 68, 68, .10) 0 6px, transparent 6px 12px);
  outline: 1px dashed rgba(239, 68, 68, .6);
}

.kde-edge { position: absolute; background: var(--brand-gold, #d4af37); border-radius: 6px; touch-action: none; z-index: 3; }
.kde-edge::after { content: ""; position: absolute; inset: -14px; }          /* big touch target */
.kde-edge-top, .kde-edge-bottom { left: 50%; width: 56px; height: 8px; margin-left: -28px; cursor: ns-resize; }
.kde-edge-top { top: -4px; } .kde-edge-bottom { bottom: -4px; }
.kde-edge-left, .kde-edge-right { top: 50%; width: 8px; height: 56px; margin-top: -28px; cursor: ew-resize; }
.kde-edge-left { left: -4px; } .kde-edge-right { right: -4px; }

/* Blocks */
.kde-block { position: absolute; min-height: 8px; }
.kde-block:hover { outline: 1px dotted rgba(56, 189, 248, .6); }
.kde-block.kde-selected { outline: 2px solid var(--brand-blue, #38bdf8); }
.kde-block.kde-overflow { outline: 2px dashed #f97316; }
.kde-text-body { white-space: pre-wrap; overflow-wrap: anywhere; min-height: 1em; outline: none; cursor: text; unicode-bidi: plaintext; }
.kde-image img { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; }
.kde-badge {
  position: absolute; top: -16px; inset-inline-end: 0; font-size: 10px; padding: 1px 6px; border-radius: 6px;
  background: #0f172a; color: #cbd5e1; font-family: var(--brand-font); pointer-events: none;
}

.kde-block.kde-review { outline: 2px dashed #f59e0b; }
.kde-badge-review { background: #f59e0b; color: #1f1300; }

.kde-grip, .kde-resize { display: none; position: absolute; touch-action: none; z-index: 2; font-family: var(--brand-font); }
.kde-selected > .kde-grip, .kde-selected > .kde-resize { display: block; }
.kde-grip {
  top: -30px; inset-inline-start: -2px; width: 34px; height: 28px; line-height: 28px; text-align: center;
  background: var(--brand-blue, #38bdf8); color: #04121f; border-radius: 8px 8px 0 0; font-size: 16px; cursor: move;
}
.kde-resize {
  top: 50%; right: -9px; width: 18px; height: 40px; margin-top: -20px; border-radius: 9px;
  background: var(--brand-blue, #38bdf8); cursor: ew-resize;
}
.kde-resize::after { content: ""; position: absolute; inset: -10px; }

/* Tables — physical order; each cell picks its own text direction. */
.kde-grid { width: 100%; table-layout: fixed; border-collapse: collapse; }
.kde-grid td { vertical-align: middle; white-space: pre-wrap; overflow-wrap: anywhere; outline: none; unicode-bidi: plaintext; }
.kde-border-grid td { border: 1px solid #595959; }
.kde-border-rows td { border-bottom: 1px solid #595959; }
.kde-border-rows tr:first-child td { border-top: 1px solid #595959; }
.kde-border-none td { box-shadow: inset 0 0 0 1px rgba(56, 189, 248, .18); }   /* editor-only hint */
.kde-grid td.kde-th-fill { background: #ebebeb; }
.kde-grid td.kde-cell-selected { box-shadow: inset 0 0 0 2px var(--brand-blue, #38bdf8); background: rgba(56, 189, 248, .10); }

/* Bottom sheets (phone-friendly editing, table library, final preview) */
.kde-sheet, .kde-preview {
  position: absolute; inset-inline: 0; bottom: 0; z-index: 10; max-height: 78%;
  display: flex; flex-direction: column; gap: 10px; padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  background: var(--brand-card, #0d1a2a); border-top: 1px solid var(--brand-gold, #d4af37);
  border-radius: 18px 18px 0 0; box-shadow: 0 -10px 30px rgba(0, 0, 0, .5);
}
.kde-sheet[hidden], .kde-preview[hidden] { display: none; }
.kde-sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.kde-sheet textarea {
  width: 100%; box-sizing: border-box; min-height: 140px; resize: vertical; font-size: 16px; line-height: 1.6;
  padding: 10px 12px; border-radius: 12px; border: 1px solid var(--brand-border, #1e3a5f);
  background: var(--brand-bg, #050a11); color: var(--brand-text, #e8eef5); unicode-bidi: plaintext;
}
.kde-sheet textarea:focus { outline: none; border-color: var(--brand-gold, #d4af37); }
.kde-sheet-actions { display: flex; flex-wrap: wrap; gap: 6px; }

.kde-library-list { overflow-y: auto; }
.kde-library-list h4 { margin: 10px 0 6px; font-size: 13px; color: var(--brand-text-dim, #8fa3b8); }
.kde-library-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.kde-library-item {
  display: flex; flex-direction: column; align-items: stretch; gap: 4px; padding: 10px; text-align: start;
  border-radius: 12px; border: 1px solid var(--brand-border, #1e3a5f); background: var(--brand-bg, #050a11);
  color: var(--brand-text, #e8eef5); cursor: pointer; min-height: 44px;
}
.kde-library-item:active, .kde-library-item:hover { border-color: var(--brand-gold, #d4af37); }
.kde-library-item small { color: var(--brand-text-dim, #8fa3b8); font-size: 11px; }
.kde-library-item .kde-library-en { text-align: end; opacity: .75; }
.kde-library-shape { display: grid; grid-template-columns: repeat(var(--cols), 1fr); gap: 2px; height: 26px; }
.kde-library-shape i { background: rgba(212, 175, 55, .35); border-radius: 2px; }
.kde-library-shape i:nth-child(-n + 5) { background: rgba(212, 175, 55, .75); }

.kde-preview { max-height: none; top: 0; border-radius: 0; border-top: none; }
.kde-preview-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.kde-preview-pages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.kde-preview-pages figure { margin: 0; width: min(100%, 720px); }
.kde-preview-pages img { width: 100%; display: block; background: #fff; box-shadow: 0 4px 18px rgba(0, 0, 0, .5); min-height: 120px; }
.kde-preview-pages figcaption { text-align: center; font-size: 12px; color: var(--brand-text-dim, #8fa3b8); margin-top: 4px; }

/* Simple mode: six big buttons at the bottom; the full toolbars appear
   only under "خيارات متقدمة". */
.kde-simplebar, .kde-advanced-toggle { display: none; }
.kde.kde-simple .kde-advanced-toggle { display: inline-flex; align-items: center; font-size: 13px; }
.kde.kde-simple:not(.kde-advanced) #kdeBar,
.kde.kde-simple:not(.kde-advanced) #kdeTableBar { display: none; }
.kde.kde-simple .kde-simplebar {
  display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; flex: none;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--brand-border, #1e3a5f); background: var(--brand-card, #0d1a2a);
}
.kde.kde-simple { padding-bottom: 0; }
.kde-simple-btn {
  min-width: 0; height: 56px; padding: 4px 2px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px; font-size: 18px; border: none; background: transparent;
}
.kde-simple-btn::after { content: attr(data-label); font-size: 11px; color: var(--brand-text-dim, #8fa3b8); }
.kde-simple-main { background: var(--brand-gold, #d4af37); color: #fff; border-radius: 12px; }
.kde-simple-main::after { color: inherit; font-weight: 700; }

/* Template-only mode (adjusting a template's writing area) */
.kde.kde-template-only #kdeBar, .kde.kde-template-only .kde-state { display: none; }



@media (prefers-reduced-motion: reduce) { .kde-btn:active { transform: none; } }
