/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --color-bg: #f0f2f5;
  --color-sidebar: #1e2a3a;
  --color-sidebar-hover: #2c3e55;
  --color-panel: #ffffff;
  --color-primary: #3b82f6;
  --color-primary-dark: #2563eb;
  --color-danger: #ef4444;
  --color-danger-dark: #dc2626;
  --color-text: #1f2937;
  --color-text-light: #6b7280;
  --color-border: #e5e7eb;
  --color-city: #f59e0b;
  --color-dept: #10b981;
  --color-amb: #8b5cf6;
  --color-num: #3b82f6;
  --top-bar-height: 52px;
  --radius: 8px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.15);
}

body {
  font-family: 'Segoe UI', 'Arial', sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  height: 100vh;
  overflow: hidden;
  direction: rtl;
}

/* =============================================
   LOGIN SCREEN
   ============================================= */
#login-screen {
  position: fixed;
  inset: 0;
  background: var(--color-sidebar);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  animation: fadeIn 0.3s ease;
}

#login-card {
  background: #253448;
  border-radius: 16px;
  padding: 40px 36px 32px;
  width: 380px;
  max-width: 95vw;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
  text-align: center;
  direction: rtl;
}

.login-logo { font-size: 3rem; margin-bottom: 10px; }

#login-card h1 {
  color: #fff;
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.login-subtitle {
  color: rgba(255,255,255,0.5);
  font-size: 0.875rem;
  margin-bottom: 28px;
}

.login-form { display: flex; flex-direction: column; }

.login-form .form-group {
  margin-bottom: 14px;
  text-align: right;
}

.login-form .form-group label {
  display: block;
  color: rgba(255,255,255,0.7);
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 5px;
}

.login-form .form-group input {
  width: 100%;
  padding: 10px 12px;
  background: rgba(255,255,255,0.1);
  border: 1.5px solid rgba(255,255,255,0.15);
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
  direction: rtl;
}
.login-form .form-group input::placeholder { color: rgba(255,255,255,0.3); }
.login-form .form-group input:focus { border-color: var(--color-primary); }

.btn-login {
  width: 100%;
  padding: 11px;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
  margin-top: 4px;
  margin-bottom: 14px;
}
.btn-login:hover { background: var(--color-primary-dark); }
.btn-login:disabled { background: #4b5563; cursor: not-allowed; }

.login-toggle {
  display: block;
  color: rgba(255,255,255,0.45);
  font-size: 0.8rem;
  text-decoration: none;
  margin-top: 2px;
  margin-bottom: 6px;
  transition: color 0.15s;
}
.login-toggle:hover { color: rgba(255,255,255,0.75); }

.login-error {
  background: rgba(239,68,68,0.2);
  border: 1px solid rgba(239,68,68,0.5);
  color: #fca5a5;
  border-radius: var(--radius);
  padding: 8px 12px;
  font-size: 0.83rem;
  margin-bottom: 10px;
  text-align: right;
}

.otp-info {
  color: rgba(255,255,255,0.65);
  font-size: 0.85rem;
  margin-bottom: 14px;
  text-align: right;
}
.otp-info strong { color: #fff; }

/* =============================================
   TOP BAR
   ============================================= */
#top-bar {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  height: var(--top-bar-height);
  background: var(--color-sidebar);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  direction: rtl;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #fff;
  font-size: 1.05rem;
  font-weight: 700;
}
.top-bar-logo { font-size: 1.3rem; }

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  direction: ltr;
}

#top-bar-user {
  color: rgba(255,255,255,0.65);
  font-size: 0.82rem;
}

.btn-topbar {
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius);
  color: rgba(255,255,255,0.85);
  padding: 5px 12px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}
.btn-topbar:hover { background: rgba(255,255,255,0.22); }

/* =============================================
   LAYOUT
   ============================================= */
#app {
  display: flex;
  height: 100vh;
  padding-top: var(--top-bar-height);
  direction: rtl;
}

/* =============================================
   SIDEBAR
   ============================================= */
#sidebar {
  width: 320px;
  min-width: 280px;
  background: var(--color-sidebar);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 2px 0 12px rgba(0,0,0,0.2);
  height: 100%;
}

.sidebar-header {
  padding: 16px 16px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.sidebar-header h1 { color: #fff; font-size: 1rem; font-weight: 700; white-space: nowrap; }

#search-input {
  margin: 10px 12px;
  padding: 8px 12px;
  border-radius: var(--radius);
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.08);
  color: #fff;
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.2s;
}
#search-input::placeholder { color: rgba(255,255,255,0.4); }
#search-input:focus { border-color: var(--color-primary); }

#tree-container { flex: 1; overflow-y: auto; padding: 8px 0 16px; }
#tree-container::-webkit-scrollbar { width: 4px; }
#tree-container::-webkit-scrollbar-track { background: transparent; }
#tree-container::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 2px; }

/* =============================================
   TREE NODES
   ============================================= */
.tree-node { user-select: none; }

.tree-node-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  cursor: pointer;
  transition: background 0.15s;
  color: rgba(255,255,255,0.85);
  font-size: 0.875rem;
  position: relative;
}
.tree-node-row:hover { background: var(--color-sidebar-hover); }
.tree-node-row.active { background: var(--color-primary); color: #fff; }

.tree-toggle {
  width: 16px;
  text-align: center;
  font-size: 0.65rem;
  opacity: 0.6;
  flex-shrink: 0;
  transition: transform 0.2s;
}

.node-icon { font-size: 1rem; flex-shrink: 0; }
.node-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-phone { font-size: 0.75rem; opacity: 0.6; direction: ltr; }
.node-serial {
  font-size: 0.7rem;
  font-family: monospace;
  background: rgba(255,255,255,0.15);
  color: rgba(255,255,255,0.75);
  border-radius: 4px;
  padding: 1px 5px;
  flex-shrink: 0;
  direction: ltr;
}

.node-actions { display: none; gap: 4px; margin-right: auto; }
.tree-node-row:hover .node-actions { display: flex; }
.node-actions button {
  background: rgba(255,255,255,0.12);
  border: none;
  border-radius: 4px;
  color: #fff;
  cursor: pointer;
  font-size: 0.75rem;
  padding: 2px 6px;
  line-height: 1.4;
  transition: background 0.15s;
}
.node-actions button:hover { background: rgba(255,255,255,0.25); }
.node-actions .btn-del:hover { background: var(--color-danger); }

.tree-children { overflow: hidden; transition: max-height 0.25s ease; }

.level-0 .tree-node-row { padding-right: 12px; }
.level-1 .tree-node-row { padding-right: 28px; }
.level-2 .tree-node-row { padding-right: 44px; }
.level-3 .tree-node-row { padding-right: 60px; }

.level-0 > .tree-node-row { border-right: 3px solid var(--color-city); }
.level-1 > .tree-node-row { border-right: 3px solid var(--color-dept); }
.level-2 > .tree-node-row { border-right: 3px solid var(--color-amb); }
.level-3 > .tree-node-row { border-right: 3px solid var(--color-num); }

/* =============================================
   MAIN PANEL
   ============================================= */
#main-panel { flex: 1; overflow-y: auto; padding: 32px; display: flex; flex-direction: column; }

#welcome-screen { flex: 1; display: flex; align-items: center; justify-content: center; }
.welcome-content { text-align: center; color: var(--color-text-light); }
.welcome-icon { font-size: 4rem; margin-bottom: 16px; }
.welcome-content h2 { font-size: 1.5rem; color: var(--color-text); margin-bottom: 8px; }

#detail-panel { flex: 1; }

.detail-header { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 28px; }
.detail-badge {
  width: 56px; height: 56px; border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.75rem; flex-shrink: 0;
}
.badge-city { background: #fef3c7; }
.badge-dept { background: #d1fae5; }
.badge-amb  { background: #ede9fe; }
.badge-num  { background: #dbeafe; }

.detail-title h2 { font-size: 1.5rem; font-weight: 700; }
.detail-title .level-label {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: 0.75rem; font-weight: 600; margin-top: 4px;
}
.label-city { background: #fef3c7; color: #92400e; }
.label-dept { background: #d1fae5; color: #065f46; }
.label-amb  { background: #ede9fe; color: #5b21b6; }
.label-num  { background: #dbeafe; color: #1e40af; }

.detail-actions { margin-right: auto; display: flex; gap: 8px; flex-wrap: wrap; }

.info-card {
  background: var(--color-panel); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px 24px; margin-bottom: 24px;
}
.info-card h3 {
  font-size: 0.875rem; font-weight: 600; color: var(--color-text-light);
  text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 12px;
}
.info-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--color-border); font-size: 0.95rem;
}
.info-row:last-child { border-bottom: none; }
.info-label { color: var(--color-text-light); min-width: 90px; font-size: 0.875rem; }
.info-value { font-weight: 500; }
.info-phone { direction: ltr; font-family: monospace; font-size: 1rem; }

.children-section {
  background: var(--color-panel); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; margin-bottom: 24px;
}
.children-section-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--color-border);
}
.children-section-header h3 { font-size: 1rem; font-weight: 600; }
.child-list { padding: 8px 0; }
.child-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--color-border);
  transition: background 0.15s; cursor: pointer;
}
.child-item:last-child { border-bottom: none; }
.child-item:hover { background: #f9fafb; }
.child-icon { font-size: 1.25rem; }
.child-info { flex: 1; }
.child-name { font-weight: 500; font-size: 0.95rem; }
.child-name .node-serial {
  background: #f3f4f6;
  color: #6b7280;
  font-size: 0.7rem;
  font-family: monospace;
  border-radius: 4px;
  padding: 1px 5px;
  margin-right: 6px;
  direction: ltr;
  display: inline-block;
}
.node-serial-detail {
  font-family: monospace;
  font-weight: 700;
  font-size: 1rem;
  color: var(--color-primary);
  letter-spacing: 0.05em;
}
.child-meta { font-size: 0.8rem; color: var(--color-text-light); direction: ltr; }
.child-role { font-size: 0.8rem; color: var(--color-text-light); }
.child-actions { display: flex; gap: 6px; }
.child-actions button { padding: 4px 10px; font-size: 0.8rem; }
.empty-children { padding: 24px; text-align: center; color: var(--color-text-light); font-size: 0.875rem; }

/* =============================================
   BUTTONS
   ============================================= */
.btn {
  padding: 8px 16px; border: none; border-radius: var(--radius);
  cursor: pointer; font-size: 0.875rem; font-weight: 600;
  transition: background 0.15s, transform 0.1s;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:active { transform: scale(0.97); }
.btn-primary   { background: var(--color-primary);  color: #fff; }
.btn-primary:hover { background: var(--color-primary-dark); }
.btn-secondary { background: #f3f4f6; color: var(--color-text); }
.btn-secondary:hover { background: #e5e7eb; }
.btn-danger    { background: var(--color-danger); color: #fff; }
.btn-danger:hover { background: var(--color-danger-dark); }
.btn-outline   { background: transparent; border: 1.5px solid var(--color-border); color: var(--color-text); }
.btn-outline:hover { background: #f3f4f6; }
.btn-icon {
  background: none; border: none; cursor: pointer; padding: 4px 8px;
  font-size: 1rem; color: var(--color-text-light); border-radius: 4px; transition: background 0.15s;
}
.btn-icon:hover { background: #f3f4f6; }
.btn-sm { padding: 5px 10px; font-size: 0.8rem; }

/* =============================================
   MODALS
   ============================================= */
#modal-overlay, #confirm-overlay, #settings-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000; animation: fadeIn 0.15s ease;
}

#modal, #confirm-modal, #settings-modal {
  background: #fff; border-radius: 12px; width: 420px; max-width: 95vw;
  box-shadow: var(--shadow-lg); animation: slideUp 0.2s ease; direction: rtl;
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 24px 16px; border-bottom: 1px solid var(--color-border);
}
.modal-header h3 { font-size: 1.1rem; font-weight: 700; }

.modal-body { padding: 20px 24px; }

.form-group { margin-bottom: 16px; }
.form-group label {
  display: block; font-size: 0.875rem; font-weight: 600;
  margin-bottom: 6px; color: var(--color-text);
}
.form-group input {
  width: 100%; padding: 9px 12px; border: 1.5px solid var(--color-border);
  border-radius: var(--radius); font-size: 0.95rem; outline: none;
  transition: border-color 0.2s; direction: rtl;
}
.form-group input:focus { border-color: var(--color-primary); }
.form-group input.error { border-color: var(--color-danger); }
.form-hint { font-size: 0.78rem; color: var(--color-text-light); display: block; margin-top: 4px; }

.modal-footer {
  padding: 16px 24px 20px; display: flex; justify-content: flex-start;
  gap: 10px; border-top: 1px solid var(--color-border);
}

.modal-error {
  background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c;
  border-radius: var(--radius); padding: 8px 12px; font-size: 0.83rem;
  margin-top: -8px; margin-bottom: 4px;
}

.settings-success {
  background: #f0fdf4; border: 1px solid #86efac; color: #166534;
  border-radius: var(--radius); padding: 8px 12px; font-size: 0.83rem; margin-top: 4px;
}
.settings-error {
  background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c;
  border-radius: var(--radius); padding: 8px 12px; font-size: 0.83rem; margin-top: 4px;
}

/* =============================================
   SEARCH HIGHLIGHT
   ============================================= */
.highlight { background: #fef08a; border-radius: 2px; padding: 0 2px; }

/* =============================================
   UTILS
   ============================================= */
.hidden { display: none !important; }

/* =============================================
   ANIMATIONS
   ============================================= */
@keyframes fadeIn  { from { opacity: 0; }                             to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* =============================================
   RESPONSIVE
   ============================================= */
@media (max-width: 640px) {
  #sidebar { width: 260px; min-width: 220px; }
  #main-panel { padding: 16px; }
  #login-card { padding: 28px 20px 24px; }
}
