/* ========================================
   ERP System - Main Styles
   ======================================== */

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #3b82f6;
  --secondary: #64748b;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --bg: #f1f5f9;
  --bg-card: #ffffff;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --sidebar-width: 260px;
  --navbar-height: 60px;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
}

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

html {
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  direction: rtl;
  -webkit-tap-highlight-color: rgba(37, 99, 235, 0.15);
}

input, select, textarea, button {
  font-size: 16px; /* يمنع تكبير iOS التلقائي عند التركيز */
}

a {
  text-decoration: none;
  color: inherit;
}

/* ========== Auth Pages ========== */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #3b82f6 100%);
  padding: 20px;
}

.auth-card {
  background: var(--bg-card);
  border-radius: 16px;
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25);
  width: 100%;
  max-width: 420px;
  padding: 40px 32px;
}

.auth-logo {
  text-align: center;
  margin-bottom: 28px;
}

.auth-logo h1 {
  font-size: 28px;
  color: var(--primary);
  font-weight: 700;
}

.auth-logo p {
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 4px;
}

.auth-error {
  display: none;
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 16px;
  font-size: 14px;
}

.auth-success {
  display: none;
  background: #f0fdf4;
  color: #166534;
  border: 1px solid #bbf7d0;
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 16px;
  font-size: 14px;
}

/* ========== Forms ========== */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  transition: border-color 0.2s;
  background: #fff;
  font-family: inherit;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

@media (max-width: 600px) {
  .form-row {
    grid-template-columns: 1fr;
  }
}

/* ========== Buttons ========== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 18px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  width: 100%;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-primary:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.btn-success {
  background: var(--success);
  color: #fff;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  padding: 4px 6px;
  border-radius: 6px;
  transition: background 0.15s;
}

.btn-icon:hover {
  background: var(--bg);
}

/* ========== App Layout ========== */
.app-layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  background: #0f172a;
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  overflow-y: auto;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.sidebar-header {
  padding: 20px 18px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.sidebar-header h2 {
  font-size: 18px;
  font-weight: 700;
  color: #fff;
}

.sidebar-header span {
  font-size: 12px;
  color: #94a3b8;
  display: block;
  margin-top: 2px;
}

.sidebar-nav {
  padding: 12px 0;
  flex: 1;
}

.nav-section {
  margin-bottom: 8px;
}

.nav-section-title {
  font-size: 11px;
  font-weight: 600;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 8px 18px 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 18px;
  color: #94a3b8;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  border-right: 3px solid transparent;
}

.nav-item:hover {
  background: rgba(255,255,255,0.05);
  color: #e2e8f0;
}

.nav-item.active {
  background: rgba(37, 99, 235, 0.15);
  color: #60a5fa;
  border-right-color: var(--primary);
}

.nav-item .icon {
  font-size: 16px;
  width: 22px;
  text-align: center;
}

.main-content {
  flex: 1;
  margin-right: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.navbar {
  height: var(--navbar-height);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 50;
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
}

.page-content {
  padding: 24px;
  flex: 1;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.page-header h1 {
  font-size: 22px;
  font-weight: 700;
}

/* ========== Cards & Tables ========== */
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.card-body {
  padding: 20px;
}

.table-wrapper {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

thead {
  background: #f8fafc;
}

th {
  text-align: right;
  padding: 12px 16px;
  font-weight: 600;
  color: var(--text-muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tr:last-child td {
  border-bottom: none;
}

tr:hover td {
  background: #f8fafc;
}

/* ========== Badges ========== */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.badge-success {
  background: #dcfce7;
  color: #166534;
}

.badge-danger {
  background: #fee2e2;
  color: #991b1b;
}

.badge-warning {
  background: #fef3c7;
  color: #92400e;
}

.badge-info {
  background: #dbeafe;
  color: #1e40af;
}

/* ========== Stats ========== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.stat-card .label {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.stat-card .value {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
}

.stat-card .change {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ========== Modal ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  background: var(--bg-card);
  border-radius: 14px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 17px;
  font-weight: 700;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  padding: 0 4px;
}

.modal-body {
  padding: 20px 22px;
}

.modal-footer {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  justify-content: flex-start;
}

/* ========== Tabs ========== */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  border-bottom: 2px solid var(--border);
  padding-bottom: 0;
}

.tab-btn {
  padding: 10px 18px;
  border: none;
  background: none;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all 0.15s;
  font-family: inherit;
}

.tab-btn:hover {
  color: var(--primary);
}

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

/* ========== Misc ========== */
code {
  font-family: 'SF Mono', 'Consolas', monospace;
}

.hidden {
  display: none !important;
}

/* ========== Mobile menu button ========== */
#menu-toggle {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 8px 10px;
  border-radius: 8px;
  line-height: 1;
  color: var(--text);
  display: none;
  min-width: 44px;
  min-height: 44px;
}

#menu-toggle:hover,
#menu-toggle:active {
  background: var(--bg);
}

/* Overlay خلف الشريط الجانبي على الجوال */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 99;
  border: none;
  padding: 0;
  cursor: pointer;
}

.sidebar-overlay.visible {
  display: block;
}

/* ========== Tablet & Mobile ========== */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(100%);
    transition: transform 0.28s ease;
    box-shadow: -8px 0 30px rgba(0, 0, 0, 0.25);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .main-content {
    margin-right: 0;
  }

  #menu-toggle {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  .navbar {
    padding: 0 12px;
    padding-top: env(safe-area-inset-top, 0);
    height: calc(56px + env(safe-area-inset-top, 0));
  }

  .page-content {
    padding-bottom: calc(24px + env(safe-area-inset-bottom, 0));
  }

  .navbar-left {
    display: flex;
    align-items: center;
    gap: 4px;
  }

  .page-content {
    padding: 14px 12px 24px;
  }

  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-bottom: 14px;
  }

  .page-header h1 {
    font-size: 18px;
  }

  .page-header .btn,
  .page-header > div {
    width: 100%;
  }

  .page-header > div {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  .page-header > div .btn {
    flex: 1 1 auto;
    min-width: calc(50% - 4px);
  }

  .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 14px;
  }

  .stat-card {
    padding: 14px;
  }

  .stat-card .value {
    font-size: 20px;
  }

  .stat-card .label {
    font-size: 12px;
  }

  .user-menu > div:last-child {
    display: none;
  }

  .user-avatar {
    width: 32px;
    height: 32px;
    font-size: 13px;
  }

  #logout-btn {
    padding: 8px 10px !important;
    font-size: 12px !important;
  }

  .modal-overlay {
    padding: 0;
    align-items: flex-end;
  }

  .modal {
    max-width: 100%;
    width: 100%;
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
  }

  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    gap: 0;
  }

  .tab-btn {
    white-space: nowrap;
    padding: 10px 14px;
    font-size: 13px;
    flex-shrink: 0;
  }

  .table-wrapper {
    margin: 0 -2px;
    -webkit-overflow-scrolling: touch;
  }

  th, td {
    padding: 10px 12px;
    font-size: 13px;
  }

  .btn {
    min-height: 42px;
    padding: 10px 14px;
  }

  .btn-icon {
    min-width: 40px;
    min-height: 40px;
    font-size: 17px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .nav-item {
    padding: 12px 18px;
    font-size: 15px;
    min-height: 48px;
  }

  .sidebar-header {
    padding: 16px 18px;
  }

  .dash-grid,
  .rpt-grid {
    grid-template-columns: 1fr !important;
  }

  .card-header {
    padding: 12px 14px;
  }

  .card-body {
    padding: 14px;
  }

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

  .form-group input,
  .form-group select,
  .form-group textarea {
    font-size: 16px;
    padding: 12px 14px;
  }

  /* إخفاء بعض أعمدة الجداول الأقل أهمية على الشاشات الضيقة */
  .table-mobile-hide {
    display: none;
  }
}

@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .stat-card .value {
    font-size: 24px;
  }

  .auth-card {
    padding: 28px 20px;
  }

  .page-header > div .btn {
    min-width: 100%;
  }

  th, td {
    padding: 8px 10px;
    font-size: 12px;
  }
}

/* منع تمرير الخلفية عند فتح القائمة */
body.sidebar-open {
  overflow: hidden;
}


/* ========== أداء الجوال ========== */
@media (max-width: 900px) {
  .card, .stat-card {
    box-shadow: none;
    border: 1px solid var(--border);
  }
  .sidebar {
    will-change: transform;
  }
  .page-content {
    content-visibility: auto;
  }
  .table-wrapper {
    -webkit-overflow-scrolling: touch;
  }
  /* تقليل الحركة */
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.12s !important;
  }
  .sidebar {
    transition: transform 0.2s ease !important;
  }
}
