/* مشترك بين كل الصفحات: قيم التصميم (نفس AM Support)، الإطار، والقطع المشتركة. */
@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Arabic:wght@400;500;600;700&family=Inter:wght@400;500;600;700;800&display=swap");

:root {
  --am-space-1: 4px;
  --am-space-2: 8px;
  --am-space-3: 12px;
  --am-space-4: 16px;
  --am-space-5: 20px;
  --am-space-6: 24px;
  --am-space-7: 32px;

  --am-radius-sm: 6px;
  --am-radius-md: 8px;
  --am-radius-lg: 12px;
  --am-radius-pill: 999px;

  --am-font: "Inter", "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  --am-text-xs: 12px;
  --am-text-sm: 13px;
  --am-text-md: 14px;
  --am-text-lg: 16px;

  --am-red: #a70f2f;
  --am-red-dark: #8a0c27;
  --am-red-soft: #f7eef1;
  --am-text: #0e2f62;
  --am-muted: #5b6880;
  --am-border: #e3e8f0;
  --am-border-strong: #c9d1de;
  --am-surface: #fff;
  --am-surface-muted: #f8fafc;
  --am-track: #eef1f6;
  --am-page: #f5f7fb;

  --am-success: #13805a;
  --am-success-soft: #e8f6ef;
  --am-warning: #b45f06;
  --am-warning-soft: #fdf3e4;
  --am-danger: #c0262d;
  --am-danger-soft: #fdecec;
  --am-info: #1f63c6;
  --am-info-soft: #eaf2fd;

  --am-shadow-sm: 0 1px 2px rgba(14, 47, 98, 0.06);
  --am-shadow-md: 0 8px 24px rgba(14, 47, 98, 0.1);
  --am-control-height: 40px;

  --chart-income: #2b79d6;
  --chart-out: #e8663a;
  --side-width: 250px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--am-page);
  color: var(--am-text);
  font-family: var(--am-font);
  font-size: var(--am-text-md);
  line-height: 1.5;
}

h1,
h2,
p {
  margin: 0;
}

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

:focus-visible {
  outline: 2px solid var(--am-red);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.lucide {
  width: 18px;
  height: 18px;
}

/* ---------- الإطار ---------- */
.side {
  position: fixed;
  inset-block: 0;
  inset-inline-start: 0;
  width: var(--side-width);
  display: flex;
  flex-direction: column;
  border-inline-end: 1px solid var(--am-border);
  background: var(--am-surface);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--am-space-3);
  padding: var(--am-space-5);
  border-bottom: 1px solid var(--am-border);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: var(--am-radius-md);
  background: var(--am-red);
  color: #fff;
}

.brand strong {
  display: block;
  font-size: var(--am-text-lg);
}

.brand small,
.side footer small {
  display: block;
  color: var(--am-muted);
  font-size: var(--am-text-xs);
}

.side nav {
  padding: 18px 10px;
}

.side nav a {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--am-space-3);
  min-height: 45px;
  margin-bottom: var(--am-space-1);
  padding: var(--am-space-3);
  border-radius: var(--am-radius-md);
  color: var(--am-text);
  font-weight: 500;
  text-decoration: none;
}

.side nav a > span {
  flex: 1;
}

.side nav a:hover {
  background: var(--am-page);
}

.side nav .lucide {
  color: var(--am-muted);
}

.side nav a.active {
  background: var(--am-red-soft);
  color: var(--am-red);
  font-weight: 700;
}

.side nav a.active .lucide {
  color: var(--am-red);
}

.side nav a.active::before {
  content: "";
  position: absolute;
  inset-inline-start: -10px;
  inset-block: 5px;
  width: 3px;
  border-radius: 4px;
  background: var(--am-red);
}

.side footer {
  display: flex;
  align-items: center;
  gap: var(--am-space-3);
  margin-top: auto;
  padding: var(--am-space-5);
  border-top: 1px solid var(--am-border);
}

.side footer div {
  flex: 1;
  min-width: 0;
}

.avatar {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--am-info-soft);
  color: var(--am-info);
  font-weight: 700;
}

.main {
  margin-inline-start: var(--side-width);
  padding: var(--am-space-6) var(--am-space-7) var(--am-space-7);
}

.content {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--am-space-4);
  width: min(100%, 1440px);
  margin-inline: auto;
}

.bottom-nav,
.top-bar {
  display: none;
}

/* ---------- رأس الصفحة ---------- */
.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--am-space-4);
}

.page-head h1 {
  font-size: 24px;
  font-weight: 800;
}

.page-head p {
  color: var(--am-muted);
}

.head-actions {
  display: flex;
  align-items: center;
  gap: var(--am-space-2);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--am-space-2);
  min-height: var(--am-control-height);
  padding: 0 var(--am-space-4);
  border: 1px solid var(--am-border-strong);
  border-radius: var(--am-radius-md);
  background: var(--am-surface);
  font-size: var(--am-text-sm);
  font-weight: 700;
  cursor: pointer;
}

.btn:hover {
  background: var(--am-surface-muted);
}

.btn.primary {
  border-color: var(--am-red);
  background: var(--am-red);
  color: #fff;
}

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

.btn.small {
  min-height: 32px;
  padding: 0 var(--am-space-3);
  font-size: var(--am-text-xs);
}

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

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

.field {
  display: grid;
  gap: var(--am-space-1);
  min-width: 0;
}

.field > span {
  color: var(--am-muted);
  font-size: var(--am-text-xs);
  font-weight: 600;
}

.input {
  width: 100%;
  min-width: 0;
  min-height: var(--am-control-height);
  padding: 0 var(--am-space-3);
  border: 1px solid var(--am-border-strong);
  border-radius: var(--am-radius-md);
  background: var(--am-surface);
  font-size: var(--am-text-sm);
}

.input.missing {
  border-color: #f0c98a;
  background: var(--am-warning-soft);
  color: var(--am-warning);
}

.month-picker {
  display: flex;
  align-items: center;
  gap: var(--am-space-1);
  min-height: var(--am-control-height);
  padding: 0 var(--am-space-1);
  border: 1px solid var(--am-border-strong);
  border-radius: var(--am-radius-md);
  background: var(--am-surface);
  font-weight: 700;
}

.month-picker button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: var(--am-radius-sm);
  background: none;
  color: var(--am-muted);
  cursor: pointer;
}

.month-picker button:hover {
  background: var(--am-page);
}

.month-picker span {
  min-width: 110px;
  text-align: center;
}

/* ---------- القطع المشتركة ---------- */
.card {
  min-width: 0;
  border: 1px solid var(--am-border);
  border-radius: var(--am-radius-lg);
  background: var(--am-surface);
  box-shadow: var(--am-shadow-sm);
}

.card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--am-space-2);
  padding: 18px 22px 14px;
}

.card-head h2 {
  display: flex;
  align-items: center;
  gap: var(--am-space-2);
  font-size: 15px;
  font-weight: 700;
}

.card-head h2 .lucide {
  color: var(--am-muted);
}

.card-foot {
  padding: 12px 22px 16px;
  border-top: 1px solid var(--am-border);
}

.hint {
  color: var(--am-muted);
  font-size: var(--am-text-xs);
}

.link {
  color: var(--am-red);
  font-size: var(--am-text-sm);
  font-weight: 700;
  text-decoration: none;
}

.num {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  direction: ltr;
  unicode-bidi: isolate;
}

.pos {
  color: var(--am-success);
}

.neg {
  color: var(--am-danger);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--am-space-1);
  padding: 2px 10px;
  border-radius: var(--am-radius-pill);
  font-size: var(--am-text-xs);
  font-weight: 700;
  white-space: nowrap;
}

.badge.danger {
  background: var(--am-danger-soft);
  color: var(--am-danger);
}

.badge.warning {
  background: var(--am-warning-soft);
  color: var(--am-warning);
}

.badge.success {
  background: var(--am-success-soft);
  color: var(--am-success);
}

.badge.info {
  background: var(--am-info-soft);
  color: var(--am-info);
}

.badge.neutral {
  background: var(--am-track);
  color: #3a4558;
}

.tile {
  display: grid;
  place-items: center;
  flex: 0 0 30px;
  height: 30px;
  border-radius: var(--am-radius-md);
  background: var(--tile-bg, var(--am-info-soft));
  color: var(--tile, var(--am-info));
}

.tile .lucide {
  width: 16px;
  height: 16px;
}

.tone-success {
  --tone: var(--am-success);
  --tile: var(--am-success);
  --tile-bg: var(--am-success-soft);
}

.tone-danger {
  --tone: var(--am-danger);
  --tile: var(--am-danger);
  --tile-bg: var(--am-danger-soft);
}

.tone-warning {
  --tone: var(--am-warning);
  --tile: var(--am-warning);
  --tile-bg: var(--am-warning-soft);
}

.tone-red {
  --tile: var(--am-red);
  --tile-bg: var(--am-red-soft);
}

.meter {
  height: 6px;
  border-radius: var(--am-radius-pill);
  background: var(--am-track);
  overflow: hidden;
}

.meter i {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--tone, var(--chart-income));
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--am-text-sm);
}

.table th {
  padding: 10px 22px;
  border-block: 1px solid var(--am-border);
  background: var(--am-surface-muted);
  color: var(--am-muted);
  font-size: var(--am-text-xs);
  font-weight: 700;
  text-align: start;
  white-space: nowrap;
}

.table td {
  padding: 12px 22px;
  border-bottom: 1px solid var(--am-border);
  white-space: nowrap;
}

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

.table .end {
  text-align: end;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: var(--am-radius-md);
  background: none;
  color: var(--am-muted);
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--am-page);
  color: var(--am-text);
}

/* نافذة فوق الصفحة: بالنص على الكمبيوتر، ومن تحت على الموبايل. */
.sheet {
  width: min(100% - var(--am-space-7), 760px);
  max-height: calc(100dvh - var(--am-space-7));
  padding: 0;
  border: 1px solid var(--am-border);
  border-radius: var(--am-radius-lg);
  background: var(--am-surface);
  color: var(--am-text);
  box-shadow: var(--am-shadow-md);
}

.confirm {
  width: min(100% - var(--am-space-7), 460px);
}

.confirm h2 {
  padding: var(--am-space-5) 22px 0;
  font-size: 18px;
  font-weight: 800;
}

.confirm p {
  padding: var(--am-space-2) 22px var(--am-space-5);
  color: var(--am-muted);
}

.confirm .sheet-foot {
  flex-wrap: wrap;
}

.confirm .sheet-foot .btn {
  flex: 1;
  min-width: 0;
}

.sheet::backdrop {
  background: rgba(14, 47, 98, 0.45);
}

.sheet-head,
.sheet-foot {
  position: sticky;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--am-space-3);
  padding: var(--am-space-4) 22px;
  background: var(--am-surface);
}

.sheet-head {
  top: 0;
  border-bottom: 1px solid var(--am-border);
}

.sheet-head h2 {
  font-size: 18px;
  font-weight: 800;
}

.sheet-foot {
  bottom: 0;
  border-top: 1px solid var(--am-border);
}

.sheet-foot .btn {
  min-width: 180px;
  min-height: 48px;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border-radius: var(--am-radius-md);
  background: var(--am-track);
}

.segmented button {
  min-height: 42px;
  padding: 0 var(--am-space-4);
  border: 0;
  border-radius: var(--am-radius-sm);
  background: none;
  color: var(--am-muted);
  font-weight: 700;
  cursor: pointer;
}

.segmented button[aria-pressed="true"] {
  background: var(--am-surface);
  color: var(--am-text);
  box-shadow: var(--am-shadow-sm);
}

/* سطر فيه اسم وقيمة وتحته شريط نسبة. */
.hbar {
  display: grid;
  gap: 6px;
  font-size: var(--am-text-sm);
}

.hbar div:first-child {
  display: flex;
  justify-content: space-between;
  gap: var(--am-space-2);
}

.hbar small {
  color: var(--am-muted);
  font-size: var(--am-text-xs);
}

/* خلية جدول فيها اسم وتحته سطر صغير. */
.table td strong {
  display: block;
  font-weight: 600;
}

.table td small {
  color: var(--am-muted);
  font-size: var(--am-text-xs);
}

.row-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--am-space-2);
}

/* يلي ما معه صلاحية إدارة الالتزامات والديون بيشوف بدون أزرار. */
.read-only :is([data-pay], [data-edit], #addRecord) {
  display: none;
}

.preview-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--am-space-2);
  width: min(100%, 1440px);
  margin: 0 auto var(--am-space-4);
  padding: var(--am-space-3) var(--am-space-4);
  border: 1px solid #f0c98a;
  border-radius: var(--am-radius-md);
  background: var(--am-warning-soft);
  color: var(--am-warning);
}

/* صف بطاقات الأرقام فوق الصفحة. */
.totals {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}

/* بطاقة رقم: عنوان مع أيقونة، القيمة، وملاحظة. */
.stat {
  display: grid;
  align-content: start;
  gap: 6px;
  padding: 16px 18px;
}

.stat-top {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--am-muted);
  font-size: var(--am-text-xs);
  font-weight: 700;
}

.stat strong {
  font-size: 24px;
  font-weight: 800;
  line-height: 1.2;
}

.stat small {
  color: var(--am-muted);
  font-size: var(--am-text-xs);
}

/* حالات منطقة البيانات: تحميل، فاضي، خطأ. */
.state {
  display: grid;
  justify-items: center;
  gap: var(--am-space-3);
  padding: var(--am-space-7) var(--am-space-5);
  color: var(--am-muted);
  text-align: center;
}

.state .lucide {
  width: 28px;
  height: 28px;
}

.state.error {
  color: var(--am-danger);
}

.state.loading .lucide {
  animation: spin 1s linear infinite;
}

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

.toast {
  position: fixed;
  inset-inline: var(--am-space-4);
  bottom: var(--am-space-6);
  z-index: 40;
  width: fit-content;
  max-width: calc(100% - var(--am-space-7));
  margin-inline: auto;
  padding: var(--am-space-3) var(--am-space-5);
  border-radius: var(--am-radius-md);
  background: var(--am-text);
  color: #fff;
  font-weight: 600;
  box-shadow: var(--am-shadow-md);
}

/* ---------- الموبايل ---------- */
@media (max-width: 900px) {
  .side {
    display: none;
  }

  .main {
    margin-inline-start: 0;
    padding: var(--am-space-4) var(--am-space-4) 96px;
  }

  .page-head h1 {
    font-size: 20px;
  }

  .stat strong {
    font-size: 19px;
  }

  .head-actions {
    width: 100%;
  }

  .month-picker {
    flex: 1;
    justify-content: space-between;
  }

  .toast {
    bottom: 88px;
  }

  .head-actions #openEntry,
  .head-actions #backup {
    display: none;
  }

  .head-actions {
    flex-wrap: wrap;
  }

  .top-bar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--am-space-3);
    padding: var(--am-space-2) var(--am-space-4);
    border-bottom: 1px solid var(--am-border);
    background: var(--am-surface);
  }

  .top-brand {
    display: flex;
    align-items: center;
    gap: var(--am-space-2);
    color: var(--am-text);
    text-decoration: none;
  }

  .top-brand .brand-mark {
    width: 32px;
    height: 32px;
  }

  .top-actions {
    display: flex;
    gap: var(--am-space-1);
  }

  .totals {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 10px;
  }

  .stat {
    padding: 14px;
  }

  .sheet {
    width: 100%;
    max-width: 100%;
    max-height: 92dvh;
    margin: auto 0 0;
    border-radius: var(--am-radius-lg) var(--am-radius-lg) 0 0;
  }

  .sheet-foot .btn {
    flex: 1;
  }

  .bottom-nav {
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    border-top: 1px solid var(--am-border);
    background: var(--am-surface);
    box-shadow: 0 -4px 16px rgba(14, 47, 98, 0.06);
  }

  .bottom-nav a {
    display: grid;
    justify-items: center;
    gap: 2px;
    padding: 10px 0 12px;
    color: var(--am-muted);
    font-size: 11px;
    font-weight: 600;
    text-decoration: none;
  }

  .bottom-nav a.active {
    color: var(--am-red);
  }

  .bottom-nav .add {
    display: grid;
    place-items: center;
    width: 46px;
    height: 46px;
    margin-top: -22px;
    border-radius: 50%;
    background: var(--am-red);
    color: #fff;
    box-shadow: var(--am-shadow-md);
  }
}
