:root {
  --blue: #2B7B82;
  --blue-dark: #23656b;
  --ink: #243038;
  --muted: #6d7b84;
  --cream: #F7FAF9;
  --page: #F8F1F1;
  --line: #e4eceb;
  --card: #FFFFFF;
  --ok: #1f7a45;
  --ok-bg: #e8f5ee;
  --warn: #b54708;
  --warn-bg: #fff4e8;
  --danger: #a33b3b;
  --danger-bg: #fdf2f0;
  --shadow: 0 10px 28px rgba(36, 48, 56, 0.07);
  --shadow-soft: 0 6px 16px rgba(36, 48, 56, 0.05);
  --sidebar-w: 248px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: "Noto Sans Arabic", Tahoma, sans-serif;
  background: var(--page);
  color: var(--ink);
}

.shell {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: linear-gradient(180deg, #2B7B82 0%, #23656b 100%);
  color: #fff;
  padding: calc(18px + var(--safe-top)) 14px calc(18px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  z-index: 40;
}

.brand-row {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 0 8px 18px;
}

.brand-text { min-width: 0; flex: 1; }

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.16);
  display: grid;
  place-items: center;
  font-size: 1.2rem;
  flex: 0 0 auto;
}

.brand { font-weight: 700; font-size: 1.25rem; line-height: 1.1; }
.brand-sub { color: rgba(255, 255, 255, 0.78); font-size: 0.8rem; }

.sidebar-close {
  display: none;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  place-items: center;
  flex: 0 0 auto;
}

.side-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 8px;
  flex: 1;
  min-height: 0;
}

.sidebar a.nav-link {
  color: #fff;
  display: flex;
  gap: 10px;
  align-items: center;
  border-radius: 14px;
  padding: 12px 12px;
  text-decoration: none;
  min-height: 46px;
}

.sidebar a.nav-link i { font-size: 1.05rem; width: 1.2rem; flex: 0 0 auto; }
.sidebar a.nav-link span { min-width: 0; }
.sidebar a.nav-link.active,
.sidebar a.nav-link:hover { background: rgba(255, 255, 255, 0.16); color: #fff; }
.logout { margin-top: auto; }

.content {
  flex: 1;
  min-width: 0;
  padding: calc(22px + var(--safe-top)) calc(26px + var(--safe-left)) calc(48px + var(--safe-bottom)) calc(26px + var(--safe-right));
}

.topbar {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 18px;
}

.topbar-text { min-width: 0; flex: 1; }

.page-title {
  font-size: clamp(1.25rem, 4vw, 1.55rem);
  font-weight: 700;
  margin: 0;
  line-height: 1.35;
  word-break: break-word;
}

.page-sub {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
}

.menu-btn, .nav-backdrop { display: none; }

.flash {
  background: #fff;
  border: 1px solid #cfe3e5;
  color: var(--blue-dark);
  border-radius: 16px;
  padding: 12px 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-soft);
  word-break: break-word;
}

.panel, .entity-card, .stat-card, .login-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.panel { padding: 20px; margin-bottom: 18px; }

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.panel-head h2, .form-section h3 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.form-section {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  height: 100%;
}

.form-section h3 { margin-bottom: 14px; }

.btn {
  min-height: 42px;
}

.btn-primary {
  --bs-btn-bg: var(--blue);
  --bs-btn-border-color: var(--blue);
  --bs-btn-hover-bg: var(--blue-dark);
  --bs-btn-hover-border-color: var(--blue-dark);
  --bs-btn-active-bg: var(--blue-dark);
  --bs-btn-active-border-color: var(--blue-dark);
  border-radius: 12px;
  font-weight: 600;
}

.btn-soft {
  background: #e8f3f4;
  color: var(--blue-dark);
  border: 1px solid #cfe3e5;
  border-radius: 12px;
}

.btn-soft-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid #f0d4d0;
  border-radius: 12px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.78rem;
  font-weight: 700;
  background: #eef5f5;
  color: var(--blue-dark);
}

.pill-on { background: var(--ok-bg); color: var(--ok); }
.pill-off { background: #f1f1f1; color: var(--muted); }

.current-file { color: var(--muted); font-size: 0.85rem; margin-top: 6px; }
.current-file img { max-width: min(180px, 100%); height: auto; }

.entity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 14px;
}

.entity-grid--books {
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 18px;
}

.entity-grid--banners {
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

.entity-card {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.entity-media {
  background:
    radial-gradient(ellipse at 50% 0%, #f7fbfb 0%, #e8f0f0 55%, #dde8e8 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

.entity-media.media-wide {
  aspect-ratio: 16 / 10;
  padding: 12px;
}

.entity-media.media-banner {
  aspect-ratio: 16 / 9;
  padding: 12px;
}

.entity-media.media-book {
  aspect-ratio: auto;
  min-height: 260px;
  padding: 18px 20px 14px;
  background:
    linear-gradient(160deg, #f4f8f8 0%, #e7efef 100%);
}

.entity-media.media-plain {
  aspect-ratio: 16 / 10;
}

.entity-cover {
  width: 100%;
  height: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 12px;
  background: #fff;
  border: 2px solid #8FB4B8;
  box-shadow:
    0 10px 22px rgba(36, 48, 56, 0.14),
    0 2px 6px rgba(0, 0, 0, 0.06);
}

.entity-media.media-wide .entity-cover,
.entity-media.media-banner .entity-cover,
.entity-media.media-plain .entity-cover {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.entity-media.media-book .entity-cover {
  width: auto;
  max-width: min(100%, 168px);
  height: 224px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  object-position: center top;
  border-radius: 10px;
  border-width: 2.5px;
  box-shadow:
    4px 0 0 rgba(43, 123, 130, 0.12),
    0 12px 24px rgba(36, 48, 56, 0.16),
    0 2px 6px rgba(0, 0, 0, 0.06);
}

.entity-cover.placeholder {
  display: grid;
  place-items: center;
  color: var(--blue);
  font-size: 1.8rem;
  min-height: 120px;
}

.entity-media.media-book .entity-cover.placeholder {
  width: 150px;
  height: 200px;
  max-width: 150px;
}

.entity-body {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.entity-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.entity-tags .pill {
  width: auto;
}

.entity-body strong {
  font-size: 1rem;
  line-height: 1.4;
  word-break: break-word;
}

.entity-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
  word-break: break-word;
}

.entity-card--books .entity-body {
  text-align: center;
  align-items: center;
}

.entity-card--books .entity-tags,
.entity-card--books .card-actions {
  justify-content: center;
  width: 100%;
}

.card-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 4px;
}

.card-actions form { margin: 0; }
.card-actions .btn { flex: 1 1 auto; min-width: 110px; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.kpi-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.kpi-card strong { font-size: 1.55rem; color: var(--blue); }
.kpi-label { color: var(--muted); font-size: 0.82rem; font-weight: 700; }

.login-body {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
  background:
    radial-gradient(circle at top right, rgba(43, 123, 130, 0.16), transparent 40%),
    var(--page);
}

.login-card {
  width: min(420px, 100%);
  padding: 28px 24px;
  text-align: center;
}

.login-mark {
  width: 56px;
  height: 56px;
  margin: 0 auto 12px;
  border-radius: 18px;
  background: linear-gradient(160deg, #2B7B82, #83AEB2);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 1.5rem;
}

.empty-inline {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  padding: 18px 8px;
  text-align: center;
}

.list-scroll-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
  padding: 14px 8px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.list-scroll-footer[data-loading="1"] .list-scroll-spinner {
  opacity: 1;
}

.list-scroll-spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2.5px solid #d5e2e3;
  border-top-color: var(--blue);
  opacity: 0.35;
  animation: admin-spin 0.7s linear infinite;
}

@keyframes admin-spin {
  to { transform: rotate(360deg); }
}

.list-scroll-done {
  margin: 12px 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.86rem;
}

.form-control, .form-select, textarea.form-control {
  border-radius: 12px;
  border-color: #d5e2e3;
  min-height: 44px;
  font-size: 16px; /* prevents iOS zoom on focus */
}

textarea.form-control {
  min-height: 120px;
}

.form-label {
  font-weight: 600;
  margin-bottom: 6px;
}

.point-row .btn {
  min-width: 44px;
}

/* Tablet */
@media (max-width: 1100px) {
  .content {
    padding: 18px 18px 40px;
  }

  .entity-grid {
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  }

  .entity-grid--books {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }
}

/* Mobile / small tablet */
@media (max-width: 960px) {
  .shell { display: block; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    right: 0;
    left: auto;
    width: min(86vw, 300px);
    flex: none;
    height: 100vh;
    height: 100dvh;
    transform: translateX(110%);
    transition: transform .22s ease;
    box-shadow: -12px 0 32px rgba(36, 48, 56, 0.18);
    padding-right: calc(14px + var(--safe-right));
    padding-left: 14px;
  }

  body.nav-open .sidebar { transform: translateX(0); }

  .sidebar-close {
    display: grid;
  }

  .menu-btn {
    display: inline-grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: 14px;
    background: #fff;
    color: var(--blue);
    box-shadow: var(--shadow-soft);
    flex: 0 0 auto;
    margin-top: 2px;
  }

  .nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.34);
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
    z-index: 30;
  }

  body.nav-open .nav-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .content {
    width: 100%;
    padding: calc(14px + var(--safe-top)) 14px calc(28px + var(--safe-bottom));
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    background: rgba(248, 241, 241, 0.92);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    margin: -14px -14px 14px;
    padding: 12px 14px 10px;
    border-bottom: 1px solid rgba(228, 236, 235, 0.9);
  }

  .panel {
    padding: 16px;
    border-radius: 18px;
  }

  .form-section {
    padding: 14px;
    height: auto;
  }

  .entity-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .entity-grid--books {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }

  .entity-media.media-book {
    min-height: 200px;
    padding: 12px 10px 10px;
  }

  .entity-media.media-book .entity-cover {
    height: 180px;
  }

  .entity-media.media-wide,
  .entity-media.media-banner,
  .entity-media.media-plain {
    aspect-ratio: 16 / 10;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .kpi-card {
    padding: 14px;
    border-radius: 16px;
  }

  .kpi-card strong { font-size: 1.35rem; }

  .card-actions .btn {
    flex: 1 1 calc(50% - 4px);
    justify-content: center;
  }

  .panel-head .btn {
    width: 100%;
  }

  .d-flex.gap-3 {
    flex-wrap: wrap;
  }
}

/* Narrow phones */
@media (max-width: 480px) {
  .page-sub { font-size: 0.84rem; }

  .kpi-grid {
    grid-template-columns: 1fr 1fr;
  }

  .login-card {
    padding: 22px 16px;
    border-radius: 18px;
  }

  .entity-grid--books {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .entity-media.media-book .entity-cover {
    height: 160px;
  }

  .entity-card--books .card-actions {
    flex-direction: column;
  }

  .card-actions {
    flex-direction: column;
  }

  .card-actions .btn {
    width: 100%;
    flex: 1 1 auto;
  }

  .point-row.input-group {
    flex-wrap: wrap;
  }

  .point-row .input-group-text {
    width: 100%;
    border-radius: 12px 12px 0 0 !important;
    justify-content: center;
  }

  .point-row textarea.form-control {
    width: 100%;
    border-radius: 0 !important;
  }

  .point-row .remove-point {
    width: 100%;
    border-radius: 0 0 12px 12px !important;
  }
}
