/* =====================================================================
   نظام إدارة الخطابات - القنصلية | تنسيق الواجهة
   ===================================================================== */
@import url('unified-theme.css');
@import url('ui-typography.css');

:root {
  /* المتغيرات من unified-theme.css — aliases إضافية لشاشة الإصدار فقط */
  --secondary-light: var(--navy-light);
  --accent-hover: var(--gold-light);
  --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

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

body {
  font-family: var(--ui-font, 'Tajawal', sans-serif);
  background: var(--bg);
  color: var(--text);
  direction: rtl;
  line-height: 1.7;
  font-size: 14px;
}

.page-wrapper { display: flex; min-height: 100vh; }

/* ---------- الشريط الجانبي (ستايل المستشارية) ---------- */
.sidebar {
  width: 260px;
  background-color: var(--primary);
  color: #fff;
  display: flex;
  flex-direction: column;
  position: fixed;
  height: 100%;
  right: 0;
  top: 0;
  padding: 1.5rem 1rem 1.1rem;
  overflow: hidden;
  transition: width .25s ease, padding .25s ease;
  z-index: 110;
}

/* رأس الشريط: الشعار + العناوين */
.sidebar-header {
  text-align: center;
  padding-bottom: 1.5rem;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.sidebar-logo {
  width: 72px;
  height: 72px;
  object-fit: contain;
  display: block;
  margin: 0 auto 1rem;
  padding: 8px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--primary);
  box-shadow: 0 0 0 4px var(--accent);
  box-sizing: border-box;
}
.sidebar-header .logo-title {
  font-size: 0.98rem;
  font-weight: 800;
  color: #fff;
  line-height: 1.5;
  white-space: normal;
  word-break: break-word;
  padding: 0 2px;
}
.sidebar-header .logo-sub {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--accent);
  margin-top: 4px;
}
.sidebar-header .logo-tag {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  color: #e2e8f0;
  margin-top: 8px;
  padding: 3px 12px;
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  letter-spacing: .3px;
}

.sidebar-nav { display: flex; flex-direction: column; gap: 0.35rem; flex-grow: 1; }
.nav-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  color: #e2e8f0;
  text-decoration: none;
  font-size: 0.95rem;
  font-weight: 500;
  white-space: nowrap;
  transition: var(--transition);
  background: transparent;
  border: none;
}
.nav-link:hover {
  background: rgba(255, 255, 255, 0.05);
  color: #fff;
  transform: none;
}
.nav-link.active {
  background: var(--accent);
  color: var(--primary);
  font-weight: 700;
  box-shadow: 0 4px 10px rgba(219, 162, 39, 0.3);
}
.nav-link .nav-ic,
.nav-link > i {
  font-size: 1.1rem;
  flex-shrink: 0;
  width: 1.25em;
  text-align: center;
}
.sidebar-footer {
  font-size: 0.75rem;
  color: #95a5a6;
  text-align: center;
  padding-top: 0.9rem;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  margin-top: 0.6rem;
}
.sidebar-user {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  text-align: right;
  margin-bottom: 0.75rem;
  padding: 0.7rem 0.75rem;
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: none;
}
.sidebar-user-avatar {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
  background: rgba(14, 33, 51, .55);
  border: 2px solid var(--accent);
  color: #f3e6c0;
}
.sidebar-user-meta {
  min-width: 0;
  flex: 1;
  line-height: 1.35;
}
.sidebar-user-name {
  font-size: 0.9rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar-user-role {
  display: inline-block;
  margin-top: 0.35rem;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--primary);
  background: var(--accent);
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  line-height: 1.4;
}
.sidebar-footer-note {
  font-size: 0.72rem;
  color: #7f97af;
  opacity: 0.9;
}
.nav-link[hidden] { display: none !important; }

/* ---------- حالة الطي (شريط مصغّر: أيقونات فقط) ---------- */
.main-content { transition: margin-right .25s ease; }
.page-wrapper.sidebar-collapsed .sidebar {
  width: 74px;
  padding-left: 0.6rem;
  padding-right: 0.6rem;
}
.page-wrapper.sidebar-collapsed .main-content { margin-right: 74px; }
.page-wrapper.sidebar-collapsed .sidebar-logo { width: 44px; height: 44px; margin-bottom: 0; }
.page-wrapper.sidebar-collapsed .sidebar-header {
  padding-bottom: 0.8rem;
  margin-bottom: 0.8rem;
}
.page-wrapper.sidebar-collapsed .logo-title,
.page-wrapper.sidebar-collapsed .logo-sub,
.page-wrapper.sidebar-collapsed .logo-tag,
.page-wrapper.sidebar-collapsed .nav-link > span:not(.nav-ic),
.page-wrapper.sidebar-collapsed .sidebar-footer,
.page-wrapper.sidebar-collapsed .sidebar-user {
  opacity: 0;
  width: 0;
  height: 0;
  overflow: hidden;
  margin: 0;
  padding: 0;
  display: none;
}
.page-wrapper.sidebar-collapsed .nav-link { justify-content: center; padding: 0.75rem 0; }

/* زر الطي في الشريط العلوي */
.topbar-left { display: flex; align-items: center; gap: 0.8rem; }
.sidebar-toggle {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--primary);
  border-radius: 10px;
  font-size: 1.15rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, color .15s, border-color .15s;
}
.sidebar-toggle:hover { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---------- اختيار النوع + القائمة المنسدلة الذكية ---------- */
.doc-select {
  display: flex;
  align-items: flex-end;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.4rem;
}
.select-block { display: flex; flex-direction: column; }
.combo-block { flex: 1; min-width: 300px; max-width: 460px; }
.select-block > label { display: block; font-size: 0.82rem; font-weight: 700; color: var(--text-light); margin-bottom: 0.4rem; }

.seg {
  display: inline-flex;
  background: #eef2f5;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
}
.seg-btn {
  padding: 0.55rem 1.4rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.seg-btn:hover { background: #e2e8ec; }
.seg-btn.active { background: var(--accent); color: var(--primary); box-shadow: var(--shadow-sm); }

.consul-select {
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--primary);
  cursor: pointer;
  min-width: 190px;
}
.consul-select:focus { outline: none; border-color: var(--accent); }

.ext-header-check {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text);
  user-select: none;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
}
.ext-header-check input { width: 16px; height: 16px; cursor: pointer; accent-color: var(--accent); }
/* نسخة شريط أدوات المعاينة (خلفية داكنة) */
.format-toolbar .ext-header-check {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.25);
}

.header-style-select {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text);
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
}
.header-style-select .header-style-label { white-space: nowrap; }
.header-style-select select {
  border: none;
  background: transparent;
  font: inherit;
  font-weight: 700;
  color: inherit;
  cursor: pointer;
  max-width: 9.5rem;
}
.format-toolbar .header-style-select {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.25);
}
.format-toolbar .header-style-select select {
  color: #fff;
}
.format-toolbar .header-style-select select option {
  color: #111;
}

.combo { position: relative; }
.combo.disabled .combo-trigger, .combo-trigger:disabled { opacity: 0.6; cursor: not-allowed; }
.combo-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  font-family: inherit;
  font-size: 0.95rem;
  color: var(--text);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color .2s;
}
.combo.open .combo-trigger { border-color: var(--accent); }
.combo-value { color: var(--text-light); }
.combo-value.selected { color: var(--primary); font-weight: 700; }
.combo-caret { color: var(--accent); font-size: 0.85rem; transition: transform .2s; }
.combo.open .combo-caret { transform: rotate(180deg); }

.combo-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
  z-index: 50;
  overflow: hidden;
}
.combo-search { padding: 0.6rem; border-bottom: 1px solid var(--border); }
.combo-search input {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.9rem;
  background: #fbfcfd;
}
.combo-search input:focus { outline: none; border-color: var(--accent); background: #fff; }
.combo-list { max-height: 340px; overflow-y: auto; padding: 0.4rem; }
.combo-group {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 1px;
  margin: 0.6rem 0.5rem 0.3rem;
}
.combo-item {
  display: block;
  width: 100%;
  text-align: right;
  padding: 0.55rem 0.7rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.combo-item:hover { background: #f0f3f5; }
.combo-item.active { background: var(--accent); color: var(--primary); font-weight: 700; }
.combo-empty { color: var(--text-light); font-size: 0.88rem; padding: 1rem; text-align: center; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- المحتوى الرئيسي ---------- */
.main-content {
  margin-right: 260px;
  flex-grow: 1;
  padding: 1.4rem 1.8rem;
  min-height: 100vh;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.2rem;
  gap: 1rem;
}
.topbar h1 { font-size: 1.4rem; font-weight: 700; color: var(--primary); }
.topbar-actions { display: flex; gap: 0.6rem; }

.btn {
  padding: 0.6rem 1.1rem;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform .1s, box-shadow .2s, background .2s, color .2s;
}
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover { background: var(--primary-light); }
.btn-accent,
.btn-secondary {
  background: var(--accent);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}
.btn-accent:hover,
.btn-secondary:hover { background: var(--accent-hover); }
.btn-ghost { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: #f0f3f5; border-color: var(--secondary); }

/* ---------- حالة فارغة ---------- */
.empty-state {
  background: var(--card);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 4rem 2rem;
  text-align: center;
  color: var(--text-light);
  max-width: 640px;
  margin: 3rem auto;
}
.empty-state .empty-icon { font-size: 3rem; margin-bottom: 1rem; }

/* ---------- منطقة المحرر (حقول + معاينة جنب بعض) ---------- */
.editor-area {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.editor-area.editor-split {
  /* يُظهره JS عبر style.display — يبقى عمود: أزرار ثم صف التقسيم */
  min-height: calc(100vh - 11rem);
}
.editor-split-main {
  display: flex;
  flex-direction: row;
  gap: 1rem;
  align-items: stretch;
  flex: 1 1 auto;
  min-height: 0;
}
.split-controls {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
  padding: 0.4rem 0.15rem;
}
.split-controls button {
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.35rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  cursor: pointer;
  color: var(--text);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.split-controls button:hover {
  border-color: var(--accent);
  background: #f4f8fb;
}
.split-controls button[aria-pressed="true"] {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* لوحة الحقول: عرض ثابت قابل للإخفاء */
#fields-panel {
  width: 280px;
  flex: 0 0 280px;
  overflow-x: hidden;
  overflow-y: auto;
  max-height: calc(100vh - 12rem);
  transition: width 0.25s ease, opacity 0.2s ease, padding 0.25s ease, flex-basis 0.25s ease;
  box-sizing: border-box;
}
#fields-panel .fields-grid {
  grid-template-columns: 1fr;
}

/* المعاينة: باقي العرض */
#preview-home.preview-home {
  display: flex !important;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
}
#preview-home .preview-panel {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
}
#preview-home .preview-scroll {
  flex: 1 1 auto;
  max-height: none;
  height: calc(100vh - 16rem);
  overflow: auto;
}

/* إخفاء الحقول */
.editor-split.fields-hidden #fields-panel {
  width: 0 !important;
  flex-basis: 0 !important;
  opacity: 0;
  overflow: hidden;
  padding: 0 !important;
  margin: 0;
  border: none;
  pointer-events: none;
}

/* إخفاء شريط الأدوات */
.editor-split.toolbar-hidden .format-toolbar {
  display: none !important;
}
.editor-split.toolbar-hidden #preview-home .preview-scroll {
  height: calc(100vh - 10rem);
}

.form-panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 1.2rem;
}
.fields-title {
  font-weight: 700;
  color: var(--primary);
  border-bottom: 2px solid var(--accent);
  padding-bottom: 0.4rem;
  margin-bottom: 0.9rem;
}
.fields-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.9rem;
}
.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field label { font-size: 0.8rem; font-weight: 500; color: var(--text-light); }
.field input,
.field select {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: #fbfcfd;
}
.field select {
  cursor: pointer;
  appearance: auto;
}
.field input:focus,
.field select:focus { outline: none; border-color: var(--accent); background: #fff; }
.field input[type="date"] { direction: ltr; text-align: right; }
.field-select .select-other {
  margin-top: 0.25rem;
}
.field textarea {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: #fbfcfd;
  resize: vertical;
  min-height: 5.5rem;
  line-height: 1.45;
}
.field textarea:focus { outline: none; border-color: var(--accent); background: #fff; }

/* حقل جواز السفر: حرف + أرقام (مثل المرجع) */
.passport-field {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  direction: ltr;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #fbfcfd;
}
.passport-field:focus-within {
  border-color: var(--accent);
  background: #fff;
}
.passport-field .passport-letter {
  flex: 0 0 auto;
  min-width: 3.2rem;
  padding: 0.55rem 0.35rem;
  border: none;
  border-right: 1px solid var(--border);
  background: #f1f5f9;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  text-align: center;
}
.passport-field .passport-digits {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0.55rem 0.7rem;
  border: none !important;
  border-radius: 0 !important;
  background: transparent !important;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  letter-spacing: 0.04em;
}
.passport-field .passport-digits:focus {
  outline: none;
}

/* قائمة الموكلين (توكيل جماعي) */
.field-people .people-rows {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.field-people .people-row {
  display: grid;
  grid-template-columns: 1.6rem 1fr 1fr 1.8rem;
  gap: 0.35rem;
  align-items: center;
}
.field-people .people-idx {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-light);
  text-align: center;
}
.field-people .people-row input {
  width: 100%;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border, #d0d5dd);
  border-radius: 6px;
  background: #fafbfc;
  font: inherit;
  font-size: 0.85rem;
}
.field-people .people-row input:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
}
.field-people .btn-people-remove {
  border: none;
  background: transparent;
  color: #b42318;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  border-radius: 4px;
}
.field-people .btn-people-remove:hover {
  background: #fef3f2;
}
.field-people .btn-people-add {
  margin-top: 0.35rem;
  align-self: start;
  border: 1px dashed var(--border, #d0d5dd);
  background: #fff;
  color: var(--accent);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.35rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
}
.field-people .btn-people-add:hover {
  border-color: var(--accent);
  background: #fffaf0;
}
.form-hint { font-size: 0.78rem; color: var(--text-light); margin-top: 1rem; }

/* ---------- لوحة المعاينة ---------- */
.preview-panel {
  background: #55606b;
  border-radius: var(--radius);
  padding: 1.2rem;
  box-shadow: var(--shadow-md);
}
.preview-scroll { max-height: calc(100vh - 8rem); overflow: auto; }
.preview { display: flex; justify-content: center; }

/* ---------- شريط التنسيق ---------- */
.format-toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: #3f4750;
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.9rem;
  position: sticky;
  top: 0;
  z-index: 5;
}
.fmt-row {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  flex-wrap: wrap;
}
.fmt-group { display: flex; align-items: center; gap: 0.35rem; }
.fmt-sep { width: 1px; height: 22px; background: #626b74; flex: none; }

/* صف العرض/الحالة منفصل بخط رفيع عن صف التنسيق لتمييز الوظيفتين بصرياً */
.fmt-row-view {
  border-top: 1px solid #565f68;
  padding-top: 0.5rem;
}

/* حاوية تمرير أفقي: على الشاشات الواسعة تتصرف كصف عادي قابل للالتفاف،
   وعلى الشاشات الضيقة (انظر media query أدناه) تتحول لتمرير أفقي واحد
   بدل الالتفاف لعدة أسطر يبتلع مساحة المعاينة الرأسية. */
.fmt-scroll { overflow: visible; }

.fmt-btn {
  min-width: 30px;
  height: 30px;
  padding: 0 0.4rem;
  border: 1px solid #626b74;
  border-radius: 6px;
  background: #4b545e;
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background .15s, color .15s;
  flex: none;
}
.fmt-btn:hover { background: var(--accent); color: var(--primary); }
.fmt-btn.active { background: var(--accent); color: var(--primary); border-color: var(--accent); }
.fmt-btn-txt { font-size: 0.76rem; font-weight: 700; font-family: inherit; }
.fmt-toggle.open { background: var(--accent); color: var(--primary); }

/* حالة التركيز عبر لوحة المفاتيح — واضحة لمن يتنقّل بـ Tab بدل الفأرة */
.fmt-btn:focus-visible,
.fmt-select:focus-visible,
.zoom-btn:focus-visible,
.numctl-btn:focus-visible,
.numctl-input:focus-visible,
.fmt-color:focus-within {
  outline: 2px solid var(--accent-light);
  outline-offset: 2px;
}

.fmt-select {
  height: 30px;
  border: 1px solid #626b74;
  border-radius: 6px;
  background: #4b545e;
  color: #fff;
  font-family: inherit;
  font-size: 0.8rem;
  padding: 0 0.4rem;
  cursor: pointer;
}
.fmt-color {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  height: 30px;
  padding: 0 6px;
  border: 1px solid #626b74;
  border-radius: 6px;
  background: #4b545e;
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
}
.fmt-color input[type="color"] {
  width: 20px;
  height: 20px;
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
}

.fmt-label { font-size: 0.76rem; color: #b9c1c8; margin-left: 2px; }

/* التحكم الرقمي الموحّد */
.numctl-row { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.numctl {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #4b545e;
  border: 1px solid #626b74;
  border-radius: 8px;
  padding: 2px 5px;
}
.numctl-label { font-size: 0.72rem; color: #cbd3da; white-space: nowrap; }
.numctl-btn {
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 5px;
  background: #5b656f;
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  touch-action: none;
}
.numctl-btn:hover { background: var(--accent); color: var(--primary); }
.numctl-input {
  width: 46px;
  height: 24px;
  text-align: center;
  border: 1px solid #626b74;
  border-radius: 5px;
  background: #2c3138;
  color: #fff;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  direction: ltr;
}
.numctl-input:focus { outline: none; border-color: var(--accent); }
.numctl-unit { font-size: 0.7rem; color: #9aa4ad; min-width: 16px; text-align: center; }

/* الزووم */
.zoom-group { display: flex; align-items: center; gap: 0.35rem; flex-wrap: wrap; }
.zoom-btn {
  height: 26px;
  padding: 0 0.5rem;
  border: 1px solid #626b74;
  border-radius: 6px;
  background: #4b545e;
  color: #fff;
  font-family: inherit;
  font-size: 0.74rem;
  font-weight: 700;
  cursor: pointer;
}
.zoom-btn:hover { background: var(--accent); color: var(--primary); }
.zoom-btn.active { background: var(--accent); color: var(--primary); border-color: var(--accent); }
.numctl-zoom { padding: 1px 4px; }
.numctl-zoom .numctl-input { width: 40px; }

/* أداة القياسات الذكية: تعدّل المحدَّد إن وُجد تحديد، وإلا كامل المستند */
.smart-group {
  gap: 0.4rem;
  padding: 2px 6px;
  border: 1px dashed transparent;
  border-radius: 8px;
  transition: border-color .15s, background .15s;
}
.smart-group .fmt-label { min-width: 108px; white-space: nowrap; }
.smart-group.sel-active {
  border-color: var(--accent);
  background: rgba(219,162,39,.08);
}
.smart-group.sel-active .fmt-label { color: var(--accent-light); font-weight: 700; }

/* لوحة المسافات القابلة للطي */
.spacing-panel {
  border-top: 1px solid #565f68;
  padding-top: 0.55rem;
}

.fit-badge {
  font-size: 0.8rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 20px;
  background: #2c3138;
  color: #cdd6df;
}
.fit-badge.fit-ok { background: rgba(39,174,96,.18); color: #6be08a; }
.fit-badge.fit-bad { background: rgba(192,57,43,.2); color: #ff8a80; }
/* يحتوي داخل الصفحة لكن بخط صغير جداً — تحذير مقروئية منفصل عن حالة "لا يحتوي" */
.fit-badge.fit-warn { background: rgba(219,162,39,.22); color: #f0c766; }
/* توقّف صريح: الروافع كلها استُنفدت (حتى أرضية المقروئية) والمحتوى لا يزال
   أطول من صفحة واحدة — أحمر أشد وحد جانبي مميّز، مختلف بصرياً عن fit-bad
   العادية (تجاوز بسيط) وعن fit-warn الكهرمانية (خط صغير في التحكم اليدوي) */
.fit-badge.fit-limit {
  background: rgba(192,57,43,.32);
  color: #ffb4ab;
  border: 1px solid rgba(255,138,128,.55);
}
.auto-mode-note { font-size: 0.74rem; color: #b9c1c8; }
.auto-mode-note.manual { color: var(--accent-light); }

/* زر تجاوز حد المقروئية: يظهر فقط عند التوقف الصريح، قرار واعٍ موثَّق */
.fit-override-btn {
  background: transparent;
  border: 1px dashed #ff8a80;
  color: #ff8a80;
}
.fit-override-btn:hover { background: rgba(192,57,43,.12); }
.fit-override-btn.active { border-style: solid; background: rgba(192,57,43,.18); color: #ffb4ab; }

/* مؤشر الطاقة الحي: يعرض أثناء الكتابة نسبة امتلاء المحتوى عند أدنى إعدادات
   مقروءة مقابل ارتفاع الصفحة — وقاية قبل الكتابة الطويلة، لا علاج بعدها */
.fit-energy { display: inline-flex; align-items: center; gap: 6px; font-size: 0.74rem; color: #b9c1c8; }
.fit-energy-bar {
  width: 46px;
  height: 6px;
  border-radius: 3px;
  background: #2c3138;
  overflow: hidden;
  flex: none;
}
.fit-energy-fill { display: block; height: 100%; width: 0%; background: #6be08a; transition: width .2s ease, background-color .2s ease; }
.fit-energy.warn .fit-energy-fill { background: #f0c766; }
.fit-energy.bad .fit-energy-fill { background: #ff8a80; }
.fit-energy.warn .fit-energy-text { color: #f0c766; }
.fit-energy.bad .fit-energy-text { color: #ff8a80; }

.btn-sm { padding: 0.4rem 0.8rem; font-size: 0.78rem; }

/* معاينة صفحة A4 على الشاشة */
.preview .letter-format-a4 {
  background: #fff;
  box-shadow: 0 0 15px rgba(0,0,0,.35);
  transform-origin: top center;
}

/* ---------- المعاينة مدمجة في الصفحة (جنب الحقول) ---------- */
.preview-home { display: flex; flex-direction: column; min-width: 0; }

.preview-launch { display: none !important; }
.btn-launch { display: none !important; }

/* شريط نجاح الإصدار */
.issue-success-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 9000;
  background: linear-gradient(90deg, #1a5f3a, #1e7a4a);
  color: #fff;
  box-shadow: 0 -4px 20px rgba(0,0,0,.18);
  padding: 0.7rem 1rem;
}
.issue-success-inner {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  flex-wrap: wrap;
}
.issue-success-icon {
  width: 2rem; height: 2rem; border-radius: 50%;
  background: rgba(255,255,255,.2);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; flex-shrink: 0;
}
.issue-success-text { display: flex; flex-direction: column; gap: 0.15rem; flex: 1; min-width: 140px; font-size: 0.9rem; }
.issue-success-text strong { font-size: 0.95rem; }
.issue-success-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.issue-success-bar .btn-ghost { background: rgba(255,255,255,.15); color: #fff; }
.issue-success-bar .btn-ghost:hover { background: rgba(255,255,255,.28); }
.issue-success-bar .btn-primary { background: #fff; color: #1a5f3a; }
.issue-success-bar .btn-primary:hover { background: #f0f7f3; }

.app-toast {
  position: fixed;
  top: 1.2rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9500;
  background: #1a3a5c;
  color: #fff;
  padding: 0.65rem 1.2rem;
  border-radius: 10px;
  font-size: 0.88rem;
  box-shadow: 0 6px 24px rgba(0,0,0,.2);
  max-width: 90vw;
}
.app-toast.ok { background: #1a5f3a; }
.app-toast.err { background: #c0392b; }

/* داخل النافذة المنفصلة: اللوحة تملأ ارتفاع النافذة */
body.preview-window {
  margin: 0;
  background: #55606b;
  min-height: 100vh;
}
.preview-window-host { height: 100vh; }
.preview-window .preview-panel {
  display: flex;
  flex-direction: column;
  height: 100vh;
  border-radius: 0;
  box-shadow: none;
}
.preview-window .preview-scroll {
  flex: 1;
  max-height: none;
  min-height: 0;
}

/* ---------- شريط الأدوات المضغوط ---------- */
.fmt-row-main { row-gap: 0.5rem; }
.fmt-spacer { flex: 1 1 12px; min-width: 8px; }
.fmt-fit { flex-wrap: wrap; gap: 0.5rem; }

/* قائمة "المزيد" المنسدلة */
.fmt-more { position: relative; }
.fmt-more-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 30;
  min-width: 270px;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 0.9rem;
  background: #3f4750;
  border: 1px solid #626b74;
  border-radius: 12px;
  box-shadow: 0 12px 30px rgba(0,0,0,.4);
}
.fmt-more-panel[hidden] { display: none; }
.more-section { display: flex; flex-direction: column; gap: 0.5rem; }
.more-label {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--accent);
  border-bottom: 1px solid #565f68;
  padding-bottom: 0.3rem;
}
.numctl-col { flex-direction: column; align-items: stretch; gap: 0.4rem; }
.numctl-col .numctl { justify-content: space-between; }
.numctl-col .numctl-label { flex: 1; }

/* ---------- إدراج الجداول: منتقي الشبكة (صفوف × أعمدة) ---------- */
.fmt-table { position: relative; }
.fmt-table-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.7rem;
  background: #3f4750;
  border: 1px solid #626b74;
  border-radius: 12px;
  box-shadow: 0 12px 30px rgba(0,0,0,.4);
}
.fmt-table-panel[hidden] { display: none; }

/* ---------- إدراج الأشكال الهندسية ---------- */
.fmt-shapes { position: relative; }
.fmt-shapes-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  padding: 0.7rem;
  background: #3f4750;
  border: 1px solid #626b74;
  border-radius: 12px;
  box-shadow: 0 12px 30px rgba(0,0,0,.4);
  min-width: 11rem;
}
.fmt-shapes-panel[hidden] { display: none; }
.shape-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.shape-pick {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  border: 1px solid #626b74;
  border-radius: 8px;
  background: #4b545e;
  cursor: pointer;
  padding: 0;
  transition: background .12s, border-color .12s;
}
.shape-pick:hover { background: var(--accent); border-color: var(--accent-light); }
.sp-ico {
  display: block;
  width: 22px;
  height: 16px;
  box-sizing: border-box;
  border: 2px solid #fff;
  background: rgba(255,255,255,.12);
}
.sp-rect { border-radius: 0; }
.sp-round { border-radius: 6px; }
.sp-ellipse { border-radius: 50%; width: 22px; height: 16px; }
.sp-line {
  height: 0;
  border-width: 2px 0 0 0;
  border-style: solid;
  border-color: #fff;
  background: none;
  width: 22px;
  margin: 7px 0;
}
.sp-diamond {
  width: 14px;
  height: 14px;
  transform: rotate(45deg);
  border-radius: 1px;
}
.sp-triangle {
  width: 0;
  height: 0;
  border: 0;
  background: transparent;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-bottom: 16px solid #fff;
}
.shape-picker-hint {
  font-size: 0.7rem;
  color: #c5ced6;
  line-height: 1.35;
  max-width: 12rem;
}

/* مقابض الشكل في المعاينة فقط */
.preview .letter-shape {
  cursor: move;
  outline: none;
  line-height: 0;
  font-size: 0;
}
.preview .letter-shape.selected {
  outline: 1.5px dashed #2b6cb0;
  outline-offset: 2px;
}
.preview .letter-shape .letter-shape-fill {
  position: absolute;
  inset: 0;
}
.preview .letter-shape .letter-shape-fill svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
.preview .letter-shape .letter-shape-handles {
  display: none;
  position: absolute;
  inset: -4px;
  pointer-events: none;
  z-index: 2;
}
.preview .letter-shape.selected .letter-shape-handles {
  display: block !important;
}
.preview .letter-shape .lsh {
  position: absolute;
  width: 9px;
  height: 9px;
  background: #fff;
  border: 1.5px solid #2b6cb0;
  border-radius: 1px;
  pointer-events: auto;
  box-sizing: border-box;
}
.preview .letter-shape .lsh[data-h="nw"] { top: -4px; left: -4px; cursor: nwse-resize; }
.preview .letter-shape .lsh[data-h="n"]  { top: -4px; left: 50%; margin-left: -4px; cursor: ns-resize; }
.preview .letter-shape .lsh[data-h="ne"] { top: -4px; right: -4px; cursor: nesw-resize; }
.preview .letter-shape .lsh[data-h="e"]  { top: 50%; right: -4px; margin-top: -4px; cursor: ew-resize; }
.preview .letter-shape .lsh[data-h="se"] { bottom: -4px; right: -4px; cursor: nwse-resize; }
.preview .letter-shape .lsh[data-h="s"]  { bottom: -4px; left: 50%; margin-left: -4px; cursor: ns-resize; }
.preview .letter-shape .lsh[data-h="sw"] { bottom: -4px; left: -4px; cursor: nesw-resize; }
.preview .letter-shape .lsh[data-h="w"]  { top: 50%; left: -4px; margin-top: -4px; cursor: ew-resize; }

.table-grid {
  display: grid;
  grid-template-columns: repeat(8, 22px);
  grid-auto-rows: 22px;
  gap: 3px;
  direction: rtl;
}
.tg-cell {
  width: 22px;
  height: 22px;
  padding: 0;
  border: 1px solid #626b74;
  border-radius: 4px;
  background: #4b545e;
  cursor: pointer;
  transition: background .1s, border-color .1s;
}
.tg-cell.on { background: var(--accent); border-color: var(--accent-light); }
.table-grid-label {
  font-size: 0.74rem;
  font-weight: 700;
  color: #cbd3da;
  text-align: center;
  min-height: 1.1em;
}

/* مؤشر تغيير عرض العمود قرب حد الفاصل وأثناء السحب — كل جداول المتن */
.preview.tbl-col-hover .body-content table td,
.preview.tbl-col-hover .body-content table th { cursor: col-resize; }
.preview.tbl-col-drag,
.preview.tbl-col-drag * {
  cursor: col-resize !important;
  user-select: none !important;
  -webkit-user-select: none !important;
}

/* مؤشر تغيير ارتفاع الصف قرب الحد الأفقي وأثناء السحب */
.preview.tbl-row-hover .body-content table td,
.preview.tbl-row-hover .body-content table th { cursor: row-resize; }
.preview.tbl-row-drag,
.preview.tbl-row-drag * {
  cursor: row-resize !important;
  user-select: none !important;
  -webkit-user-select: none !important;
}

/* ---------- الشريط العائم لإدارة صفوف/أعمدة الجدول ---------- */
.table-mini-toolbar {
  position: fixed;
  z-index: 9200;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 7px;
  background: #2c3138;
  border: 1px solid #626b74;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.45);
  flex-wrap: wrap;
  max-width: calc(100vw - 16px);
}
.table-mini-toolbar[hidden] { display: none; }
.tmt-btn {
  height: 28px;
  padding: 0 8px;
  border: 1px solid #626b74;
  border-radius: 6px;
  background: #4b545e;
  color: #fff;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, color .15s;
}
.tmt-btn:hover { background: var(--accent); color: var(--primary); }
.tmt-btn.tmt-danger:hover { background: #c0392b; color: #fff; }
.tmt-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.tmt-sep { width: 1px; height: 18px; background: #626b74; flex: none; }
.tmt-field {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: #e8eef4;
  font-size: 0.68rem;
  font-weight: 700;
  white-space: nowrap;
}
.tmt-input {
  width: 3.4rem;
  height: 26px;
  padding: 0 4px;
  border: 1px solid #626b74;
  border-radius: 6px;
  background: #1f242a;
  color: #fff;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  text-align: center;
}
.tmt-input:focus {
  outline: none;
  border-color: var(--secondary, #5b9bd5);
}
.table-mini-toolbar.tmt-locked .tmt-struct[hidden] { display: none !important; }

/* ---------- بطاقة المستخدم في الشريط العلوي ---------- */
.user-chip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.55rem;
  background: #f1f4f8;
  border: 1px solid #dbe2ea;
  border-radius: 10px;
}
.user-chip-info { display: flex; flex-direction: column; line-height: 1.2; }
.user-chip-name { font-weight: 700; font-size: 0.85rem; color: #0e2133; }
.user-chip-role { font-size: 0.72rem; color: #7f8c8d; }
.user-chip-link {
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 700;
  color: #235583;
  padding: 0.3rem 0.5rem;
  border-radius: 8px;
  white-space: nowrap;
}
.user-chip-link:hover { background: #e4ebf3; }
.user-chip-logout {
  border: 0;
  cursor: pointer;
  background: #c0392b;
  color: #fff;
  font-weight: 700;
  font-size: 0.78rem;
  padding: 0.35rem 0.7rem;
  border-radius: 8px;
  font-family: inherit;
}
.user-chip-logout:hover { background: #a5281c; }

/* ---------- تجاوب ---------- */
@media (max-width: 760px) {
  .sidebar { position: static; width: 100%; height: auto; }
  .main-content { margin-right: 0; }
  .page-wrapper { flex-direction: column; }
  .sidebar-logo { width: 60px; height: 60px; }
  .sidebar-header .logo-title { font-size: 0.95rem; }
}

/* ---------- شريط الأدوات على الشاشات الضيقة (تابلت فأصغر) ----------
   بدل الالتفاف لعدة أسطر (يبتلع مساحة معاينة الصفحة)، يتحول الصف
   الرئيسي لتمرير أفقي واحد بأزرار ثابتة العرض، مع نقاط توقف لطيفة
   عند حدود كل مجموعة (scroll-snap). صف العرض/الحالة أسفله يبقى
   يلتف بشكل عادي لأنه أقل ازدحاماً. */
@media (max-width: 900px) {
  .editor-split-main {
    flex-direction: column;
  }
  #fields-panel {
    width: 100%;
    flex-basis: auto;
    max-height: none;
  }
  .editor-split.fields-hidden #fields-panel {
    display: none;
    width: 0 !important;
    flex-basis: 0 !important;
    opacity: 0;
    padding: 0 !important;
    pointer-events: none;
  }
  .split-controls { display: none; }
  #preview-home .preview-scroll {
    height: auto;
    max-height: calc(100vh - 8rem);
  }
  .fmt-scroll {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scroll-snap-type: x proximity;
    margin: 0 -0.8rem;
    padding: 0 0.8rem;
  }
  .fmt-row-main {
    flex-wrap: nowrap;
    width: max-content;
    min-width: 100%;
  }
  .fmt-row-main .fmt-group { scroll-snap-align: start; }
  .fmt-row-view { flex-wrap: wrap; }
  /* تلميح بصري خفيف بوجود محتوى إضافي قابل للتمرير */
  .fmt-scroll::after {
    content: '';
  }
}

/* لمسات أكبر على الجوال — الحد الأدنى الموصى به لهدف لمس مريح هو ٤٠px تقريباً */
@media (max-width: 640px) {
  .fmt-btn, .zoom-btn { min-height: 38px; min-width: 38px; }
  .numctl-btn { width: 30px; height: 30px; }
  .numctl-input { height: 30px; }
  .fmt-select, .fmt-color { height: 36px; }
  /* اللوحة تصبح ورقة سفلية مثبّتة بالنافذة، مستقلة عن حاوية التمرير الأفقي
     التي يقع بداخلها الزر — بذلك تبقى مرئية بالكامل بصرف النظر عن موضع التمرير */
  .fmt-more-panel,
  .fmt-table-panel {
    position: fixed;
    top: auto;
    bottom: 1rem;
    left: 1rem;
    right: 1rem;
    min-width: 0;
    width: auto;
    max-height: 60vh;
    overflow-y: auto;
  }
  .fmt-table-panel { align-items: center; }
}

/* ---------- الطباعة: أخفِ واجهة المحرر دون إخفاء الورقة ----------
   لا تستخدم display:none على .preview-scroll (حاوية #preview) — كانت
   تسبب ورقة بيضاء عند طباعة تبويبة المعاينة بالخطأ. */
@media print {
  .sidebar,
  .topbar,
  .form-panel,
  .format-toolbar,
  #format-toolbar,
  .preview-toolbar,
  .fit-badge,
  .auto-mode-note,
  .zoom-controls,
  .btn,
  #btn-print,
  #btn-print-preview,
  #btn-open-preview,
  .app-toast,
  .ui-dlg-root,
  .table-toolbar,
  .table-mini-toolbar,
  .fmt-more-panel,
  .fmt-table-panel,
  .fmt-shapes-panel,
  .fmt-scroll,
  .fmt-row-view,
  #letter-print-frame {
    display: none !important;
  }
  body {
    margin: 0 !important;
    padding: 0 !important;
    background: #fff !important;
  }
  .page-wrapper,
  .preview-panel,
  #preview-home,
  .preview-window-host,
  .preview-scroll,
  #preview {
    position: static !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    background: transparent !important;
    box-shadow: none !important;
    width: auto !important;
    height: auto !important;
    max-height: none !important;
  }
  .letter-format-a4 {
    position: relative !important;
    left: auto !important;
    top: auto !important;
    margin: 0 auto !important;
    box-shadow: none !important;
    transform: none !important;
  }
}


/* ---- توحيد السايدبار: إلغاء قواعد السايدبار القديمة عند unified-shell ---- */
.page-wrapper.unified-shell > .sidebar {
  width: var(--sidebar-w) !important;
  padding: 0 !important;
  position: fixed !important;
  height: 100% !important;
  right: 0 !important;
  top: 0 !important;
  background: var(--navy) !important;
}
.page-wrapper.unified-shell.sidebar-collapsed > .sidebar {
  width: var(--sidebar-collapsed) !important;
}
.page-wrapper.unified-shell > .main-content {
  margin-right: var(--sidebar-w) !important;
  width: auto !important;
}
.page-wrapper.unified-shell.sidebar-collapsed > .main-content {
  margin-right: var(--sidebar-collapsed) !important;
}
.page-wrapper.unified-shell .sidebar-header,
.page-wrapper.unified-shell .sidebar-nav,
.page-wrapper.unified-shell .sidebar-footer,
.page-wrapper.unified-shell .nav-link {
  /* قواعد قديمة لا تُطبَّق على عناصر sb-* */
}