:root {
  --bg: #eef2f6;
  --panel: #ffffff;
  --ink: #152033;
  --muted: #5b6b7c;
  --line: #d7e0ea;
  --accent: #0f4c81;
  --accent-2: #16794f;
  --danger: #b42318;
  --shadow: 0 10px 30px rgba(21, 32, 51, 0.08);
  --radius: 16px;
  --font: "IBM Plex Sans Arabic", "Cairo", Tahoma, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --touch: 44px;
}

* { box-sizing: border-box; }
html {
  margin: 0;
  padding: 0;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
}
body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  background:
    radial-gradient(circle at top left, rgba(15, 76, 129, 0.08), transparent 28%),
    linear-gradient(180deg, #f7fafc 0%, var(--bg) 100%);
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.5;
  padding-bottom: var(--safe-bottom);
}

a { color: inherit; text-decoration: none; }
.container { width: min(1120px, calc(100% - 2rem)); margin-inline: auto; }
.page { padding: 1.5rem 0 3rem; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--line);
  padding-top: var(--safe-top);
}
.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 72px;
}
.brand {
  display: flex;
  gap: 0.85rem;
  align-items: center;
  min-width: 0;
}
.brand-mark {
  width: 46px;
  height: 46px;
  flex-shrink: 0;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #0f4c81, #16794f);
  color: #fff;
  font-weight: 800;
}
.brand strong {
  display: block;
  font-size: 1.05rem;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand small { color: var(--muted); font-size: 0.8rem; }
.nav {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}
.nav a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch);
  padding: 0.55rem 0.9rem;
  border-radius: 999px;
  background: #edf4fb;
  color: var(--accent);
  font-weight: 600;
}

.hero {
  display: grid;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}
.hero h1 {
  margin: 0;
  font-size: clamp(1.45rem, 4vw, 2.2rem);
}
.hero p {
  margin: 0;
  color: var(--muted);
  max-width: 60ch;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1rem;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.15rem;
  display: grid;
  gap: 0.7rem;
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
@media (hover: hover) {
  .card:hover {
    transform: translateY(-3px);
    box-shadow: 0 16px 36px rgba(21, 32, 51, 0.12);
  }
}
.card-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 1.25rem;
}
.card h2 {
  margin: 0;
  font-size: 1.15rem;
}
.card p {
  margin: 0;
  color: var(--muted);
  min-height: 2.6em;
}
.card .muted,
.muted {
  color: var(--muted);
  font-size: 0.92rem;
}
.card-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: 0;
  border-radius: 10px;
  min-height: var(--touch);
  padding: 0.65rem 1rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: scale(0.98); }
.btn-secondary { background: #e8eef5; color: var(--ink); }
.btn-success { background: var(--accent-2); }
.btn-danger { background: var(--danger); }
.btn-sm {
  min-height: 38px;
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.panel-head h1 {
  margin: 0;
  font-size: clamp(1.15rem, 3.5vw, 1.35rem);
  line-height: 1.35;
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-inline: -0.25rem;
  padding-inline: 0.25rem;
}
table.data {
  width: 100%;
  border-collapse: collapse;
  min-width: 640px;
}
table.data th,
table.data td {
  border-bottom: 1px solid var(--line);
  padding: 0.75rem 0.5rem;
  text-align: right;
  vertical-align: middle;
}
table.data th {
  color: var(--muted);
  font-weight: 700;
  white-space: nowrap;
}
table.data .row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.9rem;
}
.form-section {
  margin-bottom: 1.4rem;
  padding-bottom: 1rem;
  border-bottom: 1px dashed var(--line);
}
.form-section:last-of-type {
  border-bottom: 0;
}
.form-section h2 {
  margin: 0 0 0.9rem;
  font-size: 1.05rem;
  color: var(--accent);
}
.field { display: grid; gap: 0.35rem; }
.field.full { grid-column: 1 / -1; }
.field label {
  font-size: 0.92rem;
  font-weight: 600;
  color: #314255;
}
.field input,
.field textarea,
.field select {
  width: 100%;
  max-width: 100%;
  border: 1px solid #c9d5e3;
  border-radius: 10px;
  min-height: var(--touch);
  padding: 0.7rem 0.8rem;
  font: inherit;
  font-size: 16px; /* يمنع تكبير Safari عند التركيز */
  background: #fff;
  appearance: none;
  -webkit-appearance: none;
}
.field textarea {
  min-height: 110px;
  resize: vertical;
}
.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: 2px solid rgba(15, 76, 129, 0.2);
  border-color: var(--accent);
}

.repeater {
  display: grid;
  gap: 0.75rem;
}
.repeater-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) auto;
  gap: 0.5rem;
  align-items: end;
  padding: 0.75rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f8fbfe;
}
.form-actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 1rem;
}

.alert {
  padding: 0.85rem 1rem;
  border-radius: 12px;
  margin-bottom: 1rem;
  font-weight: 600;
}
.alert-success { background: #e8f7ef; color: #0f6a3d; }
.alert-error { background: #fdecec; color: #9b1c1c; }

.site-footer {
  border-top: 1px solid var(--line);
  padding: 1rem 0 calc(1.5rem + var(--safe-bottom));
  color: var(--muted);
  text-align: center;
  font-size: 0.92rem;
}

/* —— Tablet —— */
@media (max-width: 960px) {
  .cards {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  }
}

/* —— Mobile / small tablet —— */
@media (max-width: 720px) {
  .container {
    width: min(1120px, calc(100% - 1.25rem));
  }
  .page {
    padding: 1rem 0 5.5rem;
  }

  .topbar-inner {
    min-height: 60px;
    padding-block: 0.35rem;
  }
  .brand {
    gap: 0.65rem;
  }
  .brand-mark {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    font-size: 0.9rem;
  }
  .brand strong {
    font-size: 0.92rem;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .brand small { display: none; }
  .nav a {
    padding: 0.45rem 0.75rem;
    font-size: 0.9rem;
  }

  .cards {
    grid-template-columns: 1fr;
    gap: 0.85rem;
  }
  .card {
    padding: 1rem;
  }
  .card p {
    min-height: 0;
  }
  .card-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
  }
  .card-actions .btn {
    width: 100%;
  }

  .panel {
    padding: 1rem;
    border-radius: 14px;
  }
  .panel-head {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }
  .panel-head .card-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  .panel-head .card-actions .btn {
    width: 100%;
  }
  .panel-head .card-actions .btn:only-child {
    grid-column: 1 / -1;
  }

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

  .repeater-row {
    grid-template-columns: 1fr;
    align-items: stretch;
  }
  .repeater-row .btn {
    width: 100%;
  }

  /* جدول السجلات كبطاقات */
  .table-wrap {
    margin: 0;
    padding: 0;
    overflow: visible;
  }
  table.data {
    min-width: 0;
  }
  table.data thead {
    display: none;
  }
  table.data tbody {
    display: grid;
    gap: 0.75rem;
  }
  table.data tr {
    display: grid;
    gap: 0.55rem;
    padding: 0.9rem;
    background: #f8fbfe;
    border: 1px solid var(--line);
    border-radius: 12px;
  }
  table.data td {
    display: grid;
    grid-template-columns: minmax(5.5rem, 34%) 1fr;
    gap: 0.5rem;
    align-items: start;
    border: 0;
    padding: 0;
    text-align: right;
  }
  table.data td::before {
    content: attr(data-label);
    color: var(--muted);
    font-weight: 700;
    font-size: 0.82rem;
  }
  table.data td[data-label=""] {
    grid-template-columns: 1fr;
  }
  table.data td[data-label=""]::before {
    display: none;
  }
  table.data td.actions-cell {
    grid-template-columns: 1fr;
    padding-top: 0.35rem;
    border-top: 1px dashed var(--line);
    margin-top: 0.15rem;
  }
  table.data .row-actions {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.4rem;
  }
  table.data .row-actions .btn {
    width: 100%;
    min-height: 40px;
    font-size: 0.82rem;
    padding: 0.4rem 0.35rem;
  }

  table.data tr.empty-row {
    background: transparent;
    border-style: dashed;
  }
  table.data tr.empty-row td {
    grid-template-columns: 1fr;
    text-align: center;
    color: var(--muted);
    padding: 1rem 0.5rem;
  }
  table.data tr.empty-row td::before {
    display: none;
  }

  /* أزرار الحفظ ثابتة أسفل الشاشة */
  .form-actions {
    position: sticky;
    bottom: 0;
    z-index: 15;
    margin: 1.25rem -1rem -1rem;
    padding: 0.75rem 1rem calc(0.75rem + var(--safe-bottom));
    background: rgba(255, 255, 255, 0.96);
    border-top: 1px solid var(--line);
    box-shadow: 0 -8px 24px rgba(21, 32, 51, 0.06);
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
  }
  .form-actions .btn {
    width: 100%;
  }
  .form-actions .btn:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }
}

/* —— Narrow phones —— */
@media (max-width: 420px) {
  .container {
    width: min(1120px, calc(100% - 1rem));
  }
  .brand strong {
    font-size: 0.84rem;
  }
  .card-actions,
  .panel-head .card-actions {
    grid-template-columns: 1fr;
  }
  table.data .row-actions {
    grid-template-columns: 1fr;
  }
  .form-actions {
    grid-template-columns: 1fr;
  }
}

/* === الوضع الداكن === */
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0f17;
  --panel: #151b26;
  --ink: #eef2f6;
  --muted: #8e9cad;
  --line: #2a3442;
  --accent: #3a8fd8;
  --accent-2: #38b67e;
  --danger: #f87171;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

html[data-theme="dark"] body {
  background:
    radial-gradient(circle at top left, rgba(58, 143, 216, 0.08), transparent 28%),
    linear-gradient(180deg, #0d1320 0%, var(--bg) 100%);
  color: var(--ink);
}

html[data-theme="dark"] .topbar {
  background: rgba(21, 27, 38, 0.92);
  border-color: var(--line);
}

html[data-theme="dark"] .brand small { color: var(--muted); }

html[data-theme="dark"] .nav a {
  background: #1c2533;
  color: var(--ink);
}

html[data-theme="dark"] .nav a:hover { background: #243044; }

html[data-theme="dark"] .hero p,
html[data-theme="dark"] .card p,
html[data-theme="dark"] .muted,
html[data-theme="dark"] .table.data th,
html[data-theme="dark"] .field label { color: var(--muted); }

html[data-theme="dark"] .card,
html[data-theme="dark"] .panel,
html[data-theme="dark"] .login-section,
html[data-theme="dark"] .settings-section {
  background: var(--panel);
  border-color: var(--line);
}

html[data-theme="dark"] .btn-secondary {
  background: #1c2533;
  color: var(--ink);
}

html[data-theme="dark"] .btn-secondary:hover { background: #243044; }

html[data-theme="dark"] .field input,
html[data-theme="dark"] .field textarea,
html[data-theme="dark"] .field select {
  background: var(--panel);
  border-color: var(--line);
  color: var(--ink);
}

html[data-theme="dark"] .field input:focus,
html[data-theme="dark"] .field textarea:focus,
html[data-theme="dark"] .field select:focus {
  border-color: var(--accent);
  outline-color: rgba(58, 143, 216, 0.3);
}

html[data-theme="dark"] .repeater-row {
  background: var(--bg);
  border-color: var(--line);
}

html[data-theme="dark"] .alert-success {
  background: #103826;
  color: #6ee7b7;
}

html[data-theme="dark"] .alert-error,
html[data-theme="dark"] .alert-danger {
  background: #450a0a;
  color: #fca5a5;
}

html[data-theme="dark"] a:not(.btn):not(.nav a) { color: var(--accent); }

@media (max-width: 720px) {
  html[data-theme="dark"] table.data tr {
    background: var(--panel);
    border-color: var(--line);
  }
  html[data-theme="dark"] .form-actions {
    background: rgba(21, 27, 38, 0.96);
    border-color: var(--line);
  }
}

/* تقليل الحركة لمن يفضل ذلك */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .card,
  .btn {
    transition: none;
  }
  .btn:active { transform: none; }
}

/* طباعة واجهة النظام */
@media print {
  .topbar,
  .site-footer,
  .form-actions,
  .card-actions,
  .nav {
    display: none !important;
  }
  body {
    background: #fff;
  }
  .panel,
  .card {
    box-shadow: none;
    border: 0;
  }
}
