/* =====================================================================
   نظام إدارة الخطابات - القنصلية | تنسيق الواجهة
   ===================================================================== */
/* خط محلي (يعمل offline بالكامل) — بديل عن Google Fonts */
@import url('../fonts/ui-fonts.css');

:root {
  --primary: #0e2133;
  --primary-light: #1c3a55;
  --secondary: #dba227;
  --secondary-light: #e6b85c;
  --bg: #f4f7f9;
  --card: #ffffff;
  --text: #34495e;
  --text-light: #7f8c8d;
  --border: #e1e5e8;
  --radius: 12px;
  --shadow-sm: 0 2px 4px rgba(0,0,0,.05);
  --shadow-md: 0 4px 14px rgba(0,0,0,.08);
}

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

body {
  font-family: '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: 264px;
  background: linear-gradient(180deg, #0e2133 0%, #13293f 60%, #0b1a29 100%);
  color: #fff;
  display: flex;
  flex-direction: column;
  position: fixed;
  height: 100%;
  right: 0;
  top: 0;
  padding: 1.5rem 1rem 1.1rem;
  border-left: 1px solid rgba(219,162,39,.18);
  box-shadow: -2px 0 18px rgba(0,0,0,.18);
  overflow: hidden;
  transition: width .25s ease, padding .25s ease;
}

/* رأس الشريط: الشعار + العناوين */
.sidebar-header {
  text-align: center;
  padding-bottom: 1.1rem;
  margin-bottom: 1.1rem;
  border-bottom: 1px solid rgba(219,162,39,.22);
}
.sidebar-logo {
  width: 90px;
  height: 90px;
  object-fit: contain;
  display: block;
  margin: 0 auto 0.7rem;
}
.sidebar-header .logo-title {
  font-size: 0.98rem;
  font-weight: 900;
  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(--secondary);
  margin-top: 4px;
}
.sidebar-header .logo-tag {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  color: #cdd6df;
  margin-top: 8px;
  padding: 3px 12px;
  border-radius: 999px;
  background: rgba(219,162,39,.12);
  border: 1px solid rgba(219,162,39,.3);
  letter-spacing: .3px;
}

.sidebar-nav { display: flex; flex-direction: column; gap: 0.4rem; flex-grow: 1; }
.nav-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.75rem 0.9rem;
  border-radius: 10px;
  color: #cdd6df;
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 500;
  white-space: nowrap;
  transition: background .2s, color .2s, transform .12s;
}
.nav-link:hover { background: var(--primary-light); color: #fff; transform: translateX(-3px); }
.nav-link.active {
  background: linear-gradient(90deg, var(--secondary), var(--secondary-light));
  color: var(--primary);
  font-weight: 700;
  box-shadow: 0 3px 10px rgba(219,162,39,.28);
}
.nav-link .nav-ic { font-size: 1.1rem; flex-shrink: 0; }
.sidebar-footer {
  font-size: 0.72rem;
  color: #7f97af;
  text-align: center;
  padding-top: 0.9rem;
  border-top: 1px solid rgba(219,162,39,.18);
  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: 12px;
  background: linear-gradient(145deg, rgba(219,162,39,.14), rgba(255,255,255,.04));
  border: 1px solid rgba(219,162,39,.28);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.06);
}
.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: 1.5px solid rgba(219,162,39,.55);
  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: #0e2133;
  background: linear-gradient(90deg, #dba227, #e8c36a);
  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(--secondary); 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(--secondary); }

.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(--secondary); }
/* نسخة شريط أدوات المعاينة (خلفية داكنة) */
.format-toolbar .ext-header-check {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.25);
}

.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(--secondary); }
.combo-value { color: var(--text-light); }
.combo-value.selected { color: var(--primary); font-weight: 700; }
.combo-caret { color: var(--secondary); 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(--secondary); background: #fff; }
.combo-list { max-height: 340px; overflow-y: auto; padding: 0.4rem; }
.combo-group {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--secondary);
  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(--secondary); 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: 8px;
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform .1s, box-shadow .2s, background .2s;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--secondary); color: var(--primary); box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--secondary-light); }
.btn-ghost { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: #f0f3f5; }

/* ---------- حالة فارغة ---------- */
.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: 1.4rem;
}
.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(--secondary);
  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 {
  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 input:focus { outline: none; border-color: var(--secondary); background: #fff; }
.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.6rem;
  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; }

.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;
}
.fmt-btn:hover { background: var(--secondary); color: var(--primary); }
.fmt-btn.active { background: var(--secondary); color: var(--primary); border-color: var(--secondary); }
.fmt-btn-txt { font-size: 0.76rem; font-weight: 700; font-family: inherit; }
.fmt-toggle.open { background: var(--secondary); color: var(--primary); }

.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(--secondary); 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(--secondary); }
.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(--secondary); color: var(--primary); }
.zoom-btn.active { background: var(--secondary); color: var(--primary); border-color: var(--secondary); }
.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(--secondary);
  background: rgba(219,162,39,.08);
}
.smart-group.sel-active .fmt-label { color: var(--secondary-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; }
.auto-mode-note { font-size: 0.74rem; color: #b9c1c8; }
.auto-mode-note.manual { color: var(--secondary-light); }

.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: none; }

/* زر إطلاق نافذة المعاينة */
.preview-launch {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  flex-wrap: wrap;
  margin-top: 1.1rem;
  padding-top: 1rem;
  border-top: 1px dashed var(--border);
}
.btn-launch { font-size: 0.95rem; padding: 0.7rem 1.3rem; }
.preview-launch-hint { font-size: 0.8rem; color: var(--text-light); flex: 1; min-width: 220px; }

/* داخل النافذة المنفصلة: اللوحة تملأ ارتفاع النافذة */
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(--secondary);
  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; }

/* ---------- بطاقة المستخدم في الشريط العلوي ---------- */
.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; }
}
