:root {
  color-scheme: light;
  --bg: oklch(0.965 0.01 95);
  --surface: oklch(0.992 0.006 95);
  --surface-2: oklch(0.935 0.014 100);
  --line: oklch(0.84 0.018 102);
  --text: oklch(0.22 0.02 105);
  --muted: oklch(0.48 0.025 105);
  --accent: oklch(0.58 0.14 165);
  --accent-strong: oklch(0.43 0.12 165);
  --danger: oklch(0.55 0.18 28);
  --warning: oklch(0.68 0.14 75);
  --info: oklch(0.55 0.13 245);
  --shadow: 0 16px 48px oklch(0.18 0.02 100 / 0.12);
  --row-alt: oklch(0.95 0.012 100);
  --radius: 8px;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: oklch(0.2 0.018 105);
  --surface: oklch(0.25 0.018 105);
  --surface-2: oklch(0.3 0.02 105);
  --line: oklch(0.39 0.022 105);
  --text: oklch(0.92 0.012 95);
  --muted: oklch(0.72 0.018 100);
  --accent: oklch(0.72 0.13 165);
  --accent-strong: oklch(0.82 0.12 165);
  --danger: oklch(0.7 0.15 28);
  --warning: oklch(0.78 0.13 75);
  --info: oklch(0.72 0.12 245);
  --shadow: 0 18px 50px oklch(0.12 0.02 100 / 0.28);
  --row-alt: oklch(0.275 0.018 105);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.boot-screen,
.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.boot-screen {
  color: var(--muted);
  gap: 12px;
}

.loader {
  width: 38px;
  height: 38px;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.auth-panel {
  width: min(440px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}

.brand-mark {
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: oklch(0.98 0.006 95);
  border-radius: 16px;
  font-weight: 800;
  margin-bottom: 18px;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.25;
}

p {
  line-height: 1.7;
}

.muted {
  color: var(--muted);
}

.layout {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 280px 1fr;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--surface);
  border-inline-start: 1px solid var(--line);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  z-index: 5;
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 58px;
}

.sidebar-head .brand-mark {
  margin: 0;
  width: 44px;
  height: 44px;
  border-radius: 12px;
}

.sidebar-title {
  font-size: 1rem;
  font-weight: 800;
}

.nav {
  display: grid;
  gap: 6px;
}

.nav a {
  color: var(--muted);
  text-decoration: none;
  padding: 12px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
}

.nav a.active,
.nav a:hover {
  background: var(--surface-2);
  color: var(--text);
}

.sidebar-foot {
  margin-top: auto;
  display: grid;
  gap: 10px;
}

.main {
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.mobile-menu {
  display: none;
}

.page-title {
  display: grid;
  gap: 6px;
}

.actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.btn {
  min-height: 42px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  padding: 9px 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-decoration: none;
  transition: transform 160ms ease-out, background 160ms ease-out;
}

.btn:hover {
  transform: translateY(-1px);
  background: var(--surface-2);
}

.btn.primary {
  background: var(--accent);
  color: oklch(0.98 0.006 95);
  border-color: transparent;
}

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

.btn.icon {
  width: 42px;
  padding: 0;
}

.grid {
  display: grid;
  gap: 16px;
}

.grid.cols-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.grid.cols-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.panel,
.metric,
.table-wrap,
.form-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.panel,
.form-panel {
  padding: 18px;
}

.metric {
  padding: 18px;
  min-height: 116px;
  display: grid;
  align-content: space-between;
}

.metric strong {
  font-size: 2rem;
}

.toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.search {
  flex: 1 1 280px;
  position: relative;
}

.search input,
.field input,
.field select,
.field textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  min-height: 44px;
  border-radius: var(--radius);
  padding: 10px 12px;
  outline: none;
}

.field textarea {
  min-height: 92px;
  resize: vertical;
}

.search input:focus,
.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px oklch(0.65 0.13 165 / 0.18);
}

.filters {
  display: grid;
  grid-template-columns: repeat(6, minmax(130px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

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

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

th,
td {
  padding: 13px 12px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-weight: 700;
  background: var(--surface-2);
}

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

.status {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 10px;
  background: var(--surface-2);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.88rem;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.field {
  display: grid;
  gap: 7px;
}

.field.full {
  grid-column: 1 / -1;
}

.field label {
  color: var(--muted);
  font-weight: 700;
  font-size: 0.92rem;
}

.section-title {
  margin: 20px 0 12px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  font-size: 1.05rem;
}

.child-row {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.toast {
  position: fixed;
  left: 20px;
  bottom: 20px;
  background: var(--text);
  color: var(--surface);
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 20;
}

.error {
  color: var(--danger);
  margin-top: 10px;
}

.empty {
  padding: 32px;
  text-align: center;
  color: var(--muted);
}

.profile {
  display: grid;
  gap: 18px;
}

.details {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.detail {
  padding: 12px;
  background: var(--surface-2);
  border-radius: var(--radius);
  min-width: 0;
}

.detail span {
  display: block;
  color: var(--muted);
  font-size: 0.86rem;
  margin-bottom: 6px;
}

.drawer-backdrop {
  display: none;
}

@media (max-width: 1100px) {
  .grid.cols-4,
  .grid.cols-3,
  .details {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .filters {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

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

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(320px, 86vw);
    transform: translateX(-105%);
    transition: transform 180ms ease-out;
  }

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

  .drawer-backdrop.open {
    display: block;
    position: fixed;
    inset: 0;
    background: oklch(0.18 0.02 105 / 0.45);
    z-index: 4;
  }

  .mobile-menu {
    display: inline-flex;
  }

  .main {
    padding: 16px;
  }

  .topbar {
    align-items: flex-start;
  }

  .form-grid,
  .grid.cols-4,
  .grid.cols-3,
  .details,
  .filters {
    grid-template-columns: 1fr;
  }

  .actions {
    width: 100%;
  }

  .actions .btn {
    flex: 1 1 auto;
  }

  table {
    min-width: 0;
  }

  .table-wrap table,
  .table-wrap thead,
  .table-wrap tbody,
  .table-wrap th,
  .table-wrap td,
  .table-wrap tr {
    display: block;
  }

  .table-wrap thead {
    display: none;
  }

  .table-wrap tr {
    padding: 12px;
    border-bottom: 1px solid var(--line);
  }

  .table-wrap td {
    border: 0;
    padding: 8px 0;
  }

  .table-wrap td::before {
    content: attr(data-label);
    display: block;
    color: var(--muted);
    font-size: 0.82rem;
    margin-bottom: 3px;
  }
}

/* Utility classes (CSP-safe replacements for inline styles) */
.mt-16 {
  margin-top: 16px;
}

.mt-18 {
  margin-top: 18px;
}

.mb-10 {
  margin-bottom: 10px;
}

.space-between {
  justify-content: space-between;
}

.field-grow {
  flex: 1 1 280px;
}

.loading-block {
  min-height: 180px;
}

.stat-row {
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  padding: 10px 0;
}

/* ===== صفحة السجلات: بحث متقدم احترافي ===== */
.search-panel {
  padding: 16px;
  margin-bottom: 18px;
}

.search-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.search-lg {
  flex: 1 1 320px;
}

.search-lg input {
  padding-inline-start: 42px;
  min-height: 48px;
  font-size: 1.02rem;
}

.search-ico {
  position: absolute;
  inset-inline-start: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 1.3rem;
  pointer-events: none;
}

.count-badge {
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  margin-inline-start: 8px;
  background: var(--accent);
  color: oklch(0.98 0.006 95);
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 800;
}

.adv-panel {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.adv-panel.is-hidden {
  display: none;
}

.adv-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 12px;
}

.date-range {
  display: flex;
  gap: 8px;
}

.date-range input {
  min-width: 0;
  flex: 1 1 0;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.chips:empty {
  margin-top: 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 4px 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--text);
  font-size: 0.86rem;
  transition: border-color 140ms ease-out, color 140ms ease-out;
}

.chip:hover {
  border-color: var(--danger);
  color: var(--danger);
}

.chip-x {
  font-weight: 800;
  font-size: 1.05rem;
  line-height: 1;
}

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

.results-count {
  color: var(--muted);
  font-weight: 700;
}

.results-count strong {
  color: var(--text);
  font-size: 1.05rem;
}

.col-idx {
  width: 52px;
  color: var(--muted);
  text-align: center;
}

.table-wrap tbody tr {
  transition: background 140ms ease-out;
}

.table-wrap tbody tr:nth-child(even) {
  background: var(--row-alt);
}

.table-wrap tbody tr:hover {
  background: var(--surface-2);
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 18px;
  flex-wrap: wrap;
}

.pg-pages {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.pg-num {
  min-width: 44px;
  padding: 9px 10px;
}

.pg-num.active {
  background: var(--accent);
  color: oklch(0.98 0.006 95);
  border-color: transparent;
}

.pg-ellipsis {
  display: inline-flex;
  align-items: center;
  padding: 0 4px;
  color: var(--muted);
}

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

.btn:disabled:hover {
  transform: none;
  background: var(--surface);
}
