* { box-sizing: border-box; }
body {
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  margin: 0;
  background: #f4f6f9;
  color: #222;
  direction: rtl;
}
.app {
  display: flex;
  min-height: 100vh;
}
.mobile-topbar {
  display: none;
}
.sidebar-overlay {
  display: none;
}
.sidebar {
  width: 220px;
  background: #1f2937;
  color: #fff;
  padding: 20px 0;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}
.sidebar h1 {
  font-size: 18px;
  text-align: center;
  margin: 0 0 20px;
  padding: 0 10px;
}
.branch-switcher {
  padding: 0 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.branch-switcher label {
  color: #9ca3af;
  margin: 0;
}
.branch-switcher select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #374151;
  border-radius: 6px;
  background: #111827;
  color: #fff;
  font-size: 13px;
}
.branch-switcher button {
  width: 100%;
  font-size: 12px;
  padding: 6px 10px;
}
.sidebar-search {
  position: relative;
  padding: 0 16px 16px;
}
.sidebar-search input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #374151;
  border-radius: 6px;
  background: #111827;
  color: #fff;
  font-size: 13px;
}
.sidebar-search input::placeholder { color: #9ca3af; }
.search-results {
  position: absolute;
  top: 100%;
  right: 16px;
  left: 16px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
  max-height: 320px;
  overflow-y: auto;
  z-index: 100;
  display: none;
}
.search-results.active { display: block; }
.search-results .result-item {
  padding: 8px 10px;
  font-size: 13px;
  color: #222;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
  text-align: right;
}
.search-results .result-item:hover { background: #f3f4f6; }
.search-results .result-item .res-sub {
  color: #999;
  font-size: 11px;
  margin-top: 2px;
}
.search-results .no-results {
  padding: 10px;
  text-align: center;
  color: #999;
  font-size: 13px;
}
.sidebar-user {
  padding: 16px;
  border-top: 1px solid #374151;
  margin-top: auto;
}
.sidebar-user #user-info {
  color: #d1d5db;
  font-size: 13px;
  margin-bottom: 8px;
  word-break: break-all;
}
.sidebar-user button {
  width: 100%;
  font-size: 12px;
  padding: 6px 10px;
}
.sidebar nav a {
  display: block;
  padding: 12px 20px;
  color: #d1d5db;
  text-decoration: none;
  cursor: pointer;
  border-right: 3px solid transparent;
}
.sidebar nav a:hover {
  background: #374151;
  color: #fff;
}
.sidebar nav a.active {
  background: #374151;
  color: #fff;
  border-right-color: #3b82f6;
}
.main {
  flex: 1;
  padding: 24px;
  max-width: 100%;
  overflow-x: auto;
}
.page { display: none; }
.page.active { display: block; }
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.page-header h2 { margin: 0; }
.header-actions {
  display: flex;
  gap: 8px;
}
.filter-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.filter-bar label {
  margin: 0;
  white-space: nowrap;
}
.filter-bar select {
  width: auto;
  min-width: 160px;
}
button {
  background: #3b82f6;
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}
button:hover { background: #2563eb; }
button.secondary { background: #6b7280; }
button.secondary:hover { background: #4b5563; }
button.danger { background: #ef4444; }
button.danger:hover { background: #dc2626; }

table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
th, td {
  text-align: right;
  padding: 10px 12px;
  border-bottom: 1px solid #eee;
  font-size: 14px;
}
th {
  background: #f9fafb;
  font-weight: 600;
}
tr:hover td { background: #f9fafb; }
a.link {
  color: #2563eb;
  cursor: pointer;
  text-decoration: underline;
}

.status {
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 12px;
  display: inline-block;
}
.status.نشط { background: #dcfce7; color: #166534; }
.status.تحت_الصيانة, .status.صيانة { background: #fef9c3; color: #854d0e; }
.status.خارج_الخدمة, .status.تالف { background: #fee2e2; color: #991b1b; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  width: 90%;
  max-width: 520px;
  max-height: 85vh;
  overflow-y: auto;
}
.modal h3 { margin-top: 0; }
.modal .close {
  position: absolute;
  top: 12px;
  left: 12px;
  background: none;
  color: #888;
  font-size: 20px;
  padding: 0 6px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.form-grid .full { grid-column: 1 / -1; }
label {
  display: block;
  font-size: 13px;
  margin-bottom: 4px;
  color: #555;
}
input, select, textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}
.modal-actions {
  margin-top: 16px;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.detail-section h4 {
  margin: 16px 0 8px;
  border-bottom: 1px solid #eee;
  padding-bottom: 4px;
}
.detail-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 14px;
  border-bottom: 1px solid #f5f5f5;
}
.empty {
  text-align: center;
  color: #999;
  padding: 20px;
}
.loading {
  text-align: center;
  color: #999;
  padding: 20px;
}

/* ===================== لوحة التحكم ===================== */
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
.stat-card {
  background: #fff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.stat-card .label {
  color: #6b7280;
  font-size: 14px;
  margin-bottom: 8px;
}
.stat-card .value {
  font-size: 28px;
  font-weight: 700;
}
.stat-card.green .value { color: #16a34a; }
.stat-card.blue .value { color: #2563eb; }
.stat-card.amber .value { color: #b45309; }
.stat-card.red .value { color: #dc2626; }

.dashboard-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}
.dashboard-row .panel.full {
  grid-column: 1 / -1;
}
.panel {
  background: #fff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.panel h4 {
  margin: 0 0 12px;
}
.type-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.type-row .type-name {
  width: 110px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.type-row .bar-track {
  flex: 1;
  background: #f1f5f9;
  border-radius: 6px;
  height: 8px;
  overflow: hidden;
}
.type-row .bar-fill {
  background: #9ca3af;
  height: 100%;
  border-radius: 6px;
}
.type-row .type-count {
  width: 24px;
  text-align: left;
  font-size: 13px;
  font-weight: 600;
}
.transfer-item {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  padding: 8px 0;
  border-bottom: 1px solid #f5f5f5;
}
.transfer-item:last-child { border-bottom: none; }
.transfer-item .t-date {
  color: #999;
  font-size: 12px;
}

.attachments-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.attachment-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid #f0f0f0;
  font-size: 13px;
}
.attachment-item a {
  flex: 1;
  color: #2563eb;
  text-decoration: underline;
  word-break: break-all;
}
.attachment-thumb {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid #ddd;
}
.reports-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}
.report-card {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
  cursor: pointer;
  transition: box-shadow .2s;
  border: 2px solid transparent;
}
.report-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
  border-color: #3b82f6;
}
.report-card h4 { margin: 0 0 8px; color: #1f2937; }
.report-card p { margin: 0; font-size: 13px; color: #6b7280; }
.perms-grid {
  display: grid;
  gap: 8px;
}
.perm-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid #f0f0f0;
}
.perm-row label {
  flex: 1;
  margin: 0;
  font-weight: 500;
}
.perm-row select {
  width: 120px;
}

/* ===================== الجوال ===================== */
@media (max-width: 768px) {
  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    background: #1f2937;
    color: #fff;
    padding: 12px 16px;
    position: sticky;
    top: 0;
    z-index: 60;
  }
  .mobile-topbar h1 {
    font-size: 16px;
    margin: 0;
  }
  .menu-toggle {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 22px;
    padding: 4px 8px;
    line-height: 1;
  }
  .menu-toggle:hover { background: #374151; }

  .app {
    flex-direction: column;
  }
  .sidebar {
    position: fixed;
    top: 0;
    right: -260px;
    height: 100%;
    width: 240px;
    z-index: 100;
    transition: right .25s ease;
    overflow-y: auto;
    padding-top: 16px;
  }
  .sidebar.open {
    right: 0;
    box-shadow: -4px 0 16px rgba(0,0,0,.3);
  }
  .sidebar h1 {
    display: none;
  }
  .sidebar-overlay.active {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 90;
  }

  .main {
    padding: 12px;
  }

  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .header-actions {
    flex-wrap: wrap;
  }
  .header-actions button {
    flex: 1;
  }

  .filter-bar {
    flex-wrap: wrap;
  }
  .filter-bar select {
    min-width: 0;
    flex: 1;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .dashboard-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .dashboard-row {
    grid-template-columns: 1fr;
  }
  .reports-grid {
    grid-template-columns: 1fr;
  }

  table {
    display: block;
  }
  table thead {
    display: none;
  }
  table tbody {
    display: block;
  }
  table tr {
    display: block;
    border-bottom: 8px solid #f4f6f9;
    padding: 8px 12px;
  }
  table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: left;
    border-bottom: 1px solid #f5f5f5;
    gap: 12px;
  }
  table td:last-child {
    border-bottom: none;
    justify-content: flex-end;
    flex-wrap: wrap;
  }
  table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #6b7280;
    font-size: 12px;
    white-space: nowrap;
  }
  table td:last-child::before {
    content: none;
  }
}
