/* سطح المكتب: قياس تلقائي، شبكة 3×2، الصفحة كاملة بلا تمرير */

@media (min-width: 769px) {
  html:has(#appScreen.screen.active),
  body:has(#appScreen.screen.active) {
    height: var(--bahthy-app-h, 100dvh) !important;
    max-height: var(--bahthy-app-h, 100dvh) !important;
    min-height: 0 !important;
    overflow: hidden !important;
  }

  #appScreen.screen.active {
    height: var(--bahthy-app-h, 100dvh) !important;
    max-height: var(--bahthy-app-h, 100dvh) !important;
    min-height: 0 !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: column !important;
  }

  #appScreen .main-header.bahthy-nav {
    flex: 0 0 auto !important;
  }

  #appScreen.screen.active > .bahthy-desktop-shell,
  #appScreen .bahthy-desktop-shell {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: row !important;
    width: 100% !important;
  }

  #appScreen .bahthy-desktop-column {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    height: auto !important;
  }

  #appScreen.screen.active main.bahthy-main,
  #appScreen main.bahthy-main {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    max-width: none !important;
    padding: 6px clamp(10px, 1.2vw, 18px) 0 !important;
  }

  #appScreen .bahthy-dash-frame,
  #viewDashboard.view-section.active,
  .bahthy-dash-frame > #viewDashboard {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: column !important;
  }

  #viewDashboard .dashboard-hero {
    flex: 0 0 auto !important;
    min-height: clamp(148px, 22vh, 184px) !important;
    height: clamp(148px, 22vh, 184px) !important;
    max-height: clamp(148px, 22vh, 184px) !important;
    margin-bottom: clamp(8px, 1.2vh, 12px) !important;
    overflow: hidden !important;
  }

  #viewDashboard .dashboard-hero-content {
    flex: 1 1 52% !important;
    max-width: none !important;
    padding: 16px 24px 16px 20px !important;
  }

  #viewDashboard .dashboard-hero-title {
    font-size: clamp(1.38rem, 1.9vw, 1.72rem) !important;
    margin: 0 0 6px !important;
    line-height: 1.3 !important;
  }

  #viewDashboard .dashboard-hero-desc {
    font-size: 0.92rem !important;
    margin: 0 0 12px 0 !important;
    line-height: 1.55 !important;
    display: block !important;
    -webkit-line-clamp: unset !important;
    overflow: visible !important;
  }

  .dashboard-hero-cta {
    min-height: 38px !important;
    padding: 0 20px !important;
    font-size: 0.86rem !important;
  }

  #viewDashboard .dashboard-hero-visual,
  .dashboard-hero-visual {
    flex: 0 0 44% !important;
    width: 44% !important;
    min-width: 0 !important;
    min-height: clamp(148px, 22vh, 184px) !important;
    height: auto !important;
    align-self: stretch !important;
  }

  .dashboard-hero-books {
    min-height: 0 !important;
    height: 100% !important;
    max-height: none !important;
    object-fit: cover !important;
  }

  #viewDashboard #cardsGrid.grid,
  #cardsGrid.bahthy-cards {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    grid-template-rows: repeat(2, minmax(clamp(136px, 16vh, 152px), clamp(148px, 18vh, 172px))) !important;
    gap: clamp(8px, 1vw, 12px) !important;
    flex: 0 1 auto !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    overflow: hidden !important;
    align-content: start !important;
    align-items: stretch !important;
    width: 100% !important;
  }

  #cardsGrid.bahthy-cards .card {
    height: 100% !important;
    min-height: clamp(136px, 16vh, 152px) !important;
    max-height: clamp(148px, 18vh, 172px) !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-start !important;
    justify-content: flex-start !important;
    gap: 6px !important;
    padding: 12px 14px 10px !important;
    border-radius: 13px !important;
  }

  #cardsGrid.bahthy-cards .card .card-icon-wrap {
    width: 40px !important;
    height: 40px !important;
    border-radius: 11px !important;
    margin-bottom: 0 !important;
  }

  #cardsGrid.bahthy-cards .card .card-icon-svg {
    width: 21px !important;
    height: 21px !important;
  }

  #cardsGrid.bahthy-cards .card .title {
    font-size: clamp(0.95rem, 1.15vw, 1.05rem) !important;
    line-height: 1.28 !important;
    margin: 0 !important;
  }

  #cardsGrid.bahthy-cards .card .sub {
    font-size: clamp(0.76rem, 0.9vw, 0.84rem) !important;
    line-height: 1.4 !important;
    margin: 0 !important;
    display: -webkit-box !important;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  #cardsGrid.bahthy-cards .card .card-cta,
  #cardsGrid.bahthy-cards .card .card-cta-text {
    font-size: 0.82rem !important;
    margin-top: auto !important;
  }

  .bahthy-stats-bar {
    flex: 0 0 auto !important;
    padding-block: clamp(4px, 0.8vh, 10px) !important;
  }

  .bahthy-footer {
    flex: 0 0 auto !important;
    padding-block: clamp(4px, 0.9vh, 10px) !important;
  }

  .bahthy-footer-item p {
    display: none;
  }

  /* الشريط الجانبي — رموز ملونة */
  .bahthy-sidebar-icon {
    width: 32px !important;
    height: 32px !important;
    border-radius: 9px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    background: transparent !important;
    border: none !important;
  }

  .bahthy-sidebar-icon svg {
    width: 18px !important;
    height: 18px !important;
    stroke-width: 1.75 !important;
  }

  .bahthy-sidebar-icon--home { color: #2563EB !important; background: #EFF6FF !important; }
  .bahthy-sidebar-icon--research { color: #EA580C !important; background: #FFF7ED !important; }
  .bahthy-sidebar-icon--report { color: #7C3AED !important; background: #F5F3FF !important; }
  .bahthy-sidebar-icon--sources { color: #0369A1 !important; background: #E0F2FE !important; }
  .bahthy-sidebar-icon--plagiarism { color: #E11D48 !important; background: #FFF1F2 !important; }
  .bahthy-sidebar-icon--stats { color: #15803D !important; background: #F0FDF4 !important; }
  .bahthy-sidebar-icon--discuss { color: #1D4ED8 !important; background: #EEF2FF !important; }
  .bahthy-sidebar-icon--fav { color: #D97706 !important; background: #FFFBEB !important; }
  .bahthy-sidebar-icon--history { color: #0F766E !important; background: #F0FDFA !important; }
  .bahthy-sidebar-icon--files { color: #4338CA !important; background: #EEF2FF !important; }
  .bahthy-sidebar-icon--settings { color: #475569 !important; background: #F1F5F9 !important; }
  .bahthy-sidebar-icon--contact { color: #16A34A !important; background: #F0FDF4 !important; }

  .bahthy-sidebar-link.is-active .bahthy-sidebar-icon {
    color: inherit !important;
  }

  .bahthy-sidebar-link.is-active .bahthy-sidebar-icon--home { color: #2563EB !important; background: #DBEAFE !important; }

  /* الشريط الجانبي — مسافات خفيفة وكل العناصر ظاهرة */
  .bahthy-dash-sidebar {
    display: flex !important;
    flex-direction: column !important;
    justify-content: flex-start !important;
    gap: 0 !important;
    padding: 10px 10px 10px !important;
    overflow: hidden !important;
    overflow-y: hidden !important;
    min-height: 0 !important;
  }

  .bahthy-sidebar-brand {
    padding: 0 4px 8px !important;
    margin: 0 0 6px !important;
    gap: 8px !important;
    flex-wrap: nowrap !important;
    flex: 0 0 auto !important;
  }

  .bahthy-sidebar-brand-mark,
  .bahthy-sidebar-brand-mark img {
    width: 38px !important;
    height: 38px !important;
  }

  .bahthy-sidebar-nav {
    display: flex !important;
    flex-direction: column !important;
    gap: 6px !important;
    flex: 0 0 auto !important;
    margin: 0 !important;
  }

  .bahthy-sidebar-link,
  .bahthy-sidebar-tool {
    padding: 7px 10px !important;
    min-height: 0 !important;
    gap: 10px !important;
    font-size: 0.88rem !important;
  }

  .bahthy-sidebar-icon {
    width: 32px !important;
    height: 32px !important;
    border-radius: 9px !important;
  }

  .bahthy-sidebar-icon svg {
    width: 18px !important;
    height: 18px !important;
  }

  .bahthy-sidebar-section {
    margin-top: 10px !important;
    padding-top: 10px !important;
    gap: 6px !important;
    flex: 0 0 auto !important;
  }

  .bahthy-sidebar-section-title {
    margin: 0 0 4px !important;
    padding: 0 8px !important;
    font-size: 0.72rem !important;
  }

  .bahthy-sidebar-help,
  .bahthy-sidebar-sub {
    margin-top: 10px !important;
    margin-bottom: 0 !important;
    padding: 10px 10px !important;
    flex: 0 0 auto !important;
  }

  .bahthy-sidebar-help-top {
    margin-bottom: 8px !important;
    gap: 8px !important;
  }

  .bahthy-sidebar-contact {
    padding: 8px 12px !important;
    margin: 0 !important;
    font-size: 0.82rem !important;
  }
}

html[data-theme="night"] .bahthy-sidebar-icon--home { color: #93C5FD !important; background: #1B2C4A !important; }
html[data-theme="night"] .bahthy-sidebar-icon--research { color: #FDBA74 !important; background: #3A2414 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--report { color: #C4B5FD !important; background: #2A2150 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--sources { color: #7DD3FC !important; background: #163044 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--plagiarism { color: #FDA4AF !important; background: #3F1520 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--stats { color: #86EFAC !important; background: #163024 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--discuss { color: #A5B4FC !important; background: #1E2A55 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--fav { color: #FCD34D !important; background: #3A2A10 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--history { color: #5EEAD4 !important; background: #16332F !important; }
html[data-theme="night"] .bahthy-sidebar-icon--files { color: #A5B4FC !important; background: #222A55 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--settings { color: #CBD5E1 !important; background: #1B2740 !important; }
html[data-theme="night"] .bahthy-sidebar-icon--contact { color: #86EFAC !important; background: #163024 !important; }

@media (min-width: 769px) and (max-height: 800px) {
  #viewDashboard .dashboard-hero {
    min-height: 148px !important;
    height: 148px !important;
    max-height: 148px !important;
    margin-bottom: 8px !important;
  }
  #viewDashboard .dashboard-hero-visual,
  .dashboard-hero-visual {
    min-height: 148px !important;
  }
  #viewDashboard #cardsGrid.grid,
  #cardsGrid.bahthy-cards {
    grid-template-rows: repeat(2, minmax(128px, 148px)) !important;
    gap: 8px !important;
  }
  #cardsGrid.bahthy-cards .card {
    min-height: 128px !important;
    max-height: 148px !important;
    padding: 10px 12px 8px !important;
  }
  #cardsGrid.bahthy-cards .card .card-icon-wrap {
    width: 34px !important;
    height: 34px !important;
  }
  #cardsGrid.bahthy-cards .card .card-icon-svg {
    width: 18px !important;
    height: 18px !important;
  }
  .bahthy-dash-sidebar {
    padding: 8px 8px 8px !important;
  }
  .bahthy-sidebar-nav,
  .bahthy-sidebar-section {
    gap: 4px !important;
  }
  .bahthy-sidebar-link,
  .bahthy-sidebar-tool {
    padding: 6px 8px !important;
    font-size: 0.84rem !important;
  }
  .bahthy-sidebar-help,
  .bahthy-sidebar-sub {
    padding: 8px 8px !important;
    margin-top: 8px !important;
  }
  .bahthy-footer-grid {
    gap: 8px !important;
  }
  .bahthy-footer-item h3 {
    font-size: 0.78rem !important;
  }
  .bahthy-stat-label {
    font-size: 0.68rem !important;
  }
}

@media (min-width: 769px) and (max-width: 1280px) {
  #cardsGrid.bahthy-cards .card .sub {
    -webkit-line-clamp: 2;
  }

  :root {
    --bahthy-sidebar-w: 196px;
  }

  .bahthy-dash-sidebar,
  .bahthy-dash-sidebar:not(.is-expanded) {
    flex-basis: var(--bahthy-sidebar-w) !important;
    width: var(--bahthy-sidebar-w) !important;
    padding-inline: 8px !important;
  }

  .bahthy-dash-sidebar:not(.is-expanded) .bahthy-sidebar-brand-text {
    display: flex !important;
  }

  .bahthy-dash-sidebar:not(.is-expanded) .bahthy-sidebar-link > span:not(.bahthy-sidebar-icon),
  .bahthy-dash-sidebar:not(.is-expanded) .bahthy-sidebar-tool > span:not(.bahthy-sidebar-icon) {
    display: inline !important;
  }

  .bahthy-dash-sidebar:not(.is-expanded) .bahthy-sidebar-section-title,
  .bahthy-dash-sidebar:not(.is-expanded) .bahthy-sidebar-help {
    display: block !important;
  }
}

/* أرقام الصفحات والمصادر — واضحة وليست فسفورية */
.research-form-pro--neon .rp-pages-val {
  min-width: 3.4rem;
  padding: 6px 12px;
  border-radius: 10px;
  background: #173B6C !important;
  color: #FFFFFF !important;
  -webkit-text-fill-color: #FFFFFF !important;
  font-size: 1.22rem !important;
  font-weight: 900 !important;
  letter-spacing: 0.02em;
  text-shadow: none !important;
  box-shadow: 0 2px 8px rgba(23, 59, 108, 0.28);
  line-height: 1.2;
}

.research-form-pro--neon .rp-pages-range {
  accent-color: #2563EB !important;
  height: 8px;
}

.research-form-pro--neon .rp-pages-btn {
  background: #2563EB !important;
  border-color: #1D4ED8 !important;
  color: #fff !important;
}

.research-form-pro--neon .rp-seg button.is-on,
.research-form-pro--neon .rp-chips button.is-on {
  background: #2563EB !important;
  border-color: #1D4ED8 !important;
  color: #fff !important;
  box-shadow: none !important;
}

html[data-theme="night"] .research-form-pro--neon .rp-pages-val {
  background: #2563EB !important;
  color: #FFFFFF !important;
  -webkit-text-fill-color: #FFFFFF !important;
}

/* أقسام غير الرئيسية: تمرير داخلي على كل الشاشات */
@media (min-width: 641px) {
  #appScreen main.bahthy-main > .view-section:not(#viewDashboard) {
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
}

/* تابلت (آيباد وغيره 641–1100): شبكة متناسبة، لا أشرطة الموبايل */
@media (min-width: 641px) and (max-width: 1100px) {
  html:not(.is-phone) #viewDashboard .dashboard-hero,
  html:not(.is-phone) #viewDashboard .dashboard-hero-content,
  html:not(.is-phone) #viewDashboard .dashboard-hero-title,
  html:not(.is-phone) #viewDashboard .dashboard-hero-desc,
  html:not(.is-phone) #viewDashboard .dashboard-hero-cta,
  html:not(.is-phone) #viewDashboard .dashboard-hero-visual {
    display: flex !important;
  }

  html:not(.is-phone) #viewDashboard .dashboard-hero-title,
  html:not(.is-phone) #viewDashboard .dashboard-hero-desc,
  html:not(.is-phone) #viewDashboard .dashboard-hero-cta {
    display: block !important;
  }

  html:not(.is-phone) #viewDashboard .dashboard-hero {
    flex-direction: row !important;
    min-height: clamp(120px, 16vh, 160px) !important;
    height: clamp(120px, 16vh, 160px) !important;
    max-height: clamp(120px, 16vh, 160px) !important;
    margin-bottom: 8px !important;
  }

  html:not(.is-phone) #viewDashboard .dashboard-hero-visual,
  html:not(.is-phone) .dashboard-hero-visual {
    display: block !important;
    flex: 0 0 40% !important;
    width: 40% !important;
    min-height: 0 !important;
    height: auto !important;
  }

  html:not(.is-phone) #viewDashboard #cardsGrid.grid,
  html:not(.is-phone) #cardsGrid.bahthy-cards {
    display: grid !important;
    flex-direction: unset !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    grid-template-rows: repeat(3, minmax(108px, 128px)) !important;
    gap: 8px !important;
    overflow: hidden !important;
  }

  html:not(.is-phone) #cardsGrid.bahthy-cards .card {
    display: flex !important;
    flex-direction: column !important;
    grid-template-columns: none !important;
    grid-template-rows: none !important;
    align-items: flex-start !important;
    min-height: 108px !important;
    max-height: 128px !important;
    height: 100% !important;
    padding: 10px 12px 8px !important;
    flex: unset !important;
  }

  html:not(.is-phone) #cardsGrid.bahthy-cards .card .card-text {
    display: flex !important;
    flex-direction: column !important;
  }

  html:not(.is-phone) #cardsGrid.bahthy-cards .card .card-icon-wrap {
    width: 34px !important;
    height: 34px !important;
    grid-column: auto !important;
    grid-row: auto !important;
  }

  html:not(.is-phone) #cardsGrid.bahthy-cards .card .title {
    white-space: normal !important;
    font-size: 0.95rem !important;
  }

  html:not(.is-phone) #cardsGrid.bahthy-cards .card .sub {
    display: -webkit-box !important;
    -webkit-line-clamp: 2;
    white-space: normal !important;
  }

  html:not(.is-phone) .bahthy-stats-bar {
    display: flex !important;
  }
}

@media (min-width: 769px) and (max-width: 1100px) {
  html:not(.is-phone) #viewDashboard #cardsGrid.grid,
  html:not(.is-phone) #cardsGrid.bahthy-cards {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    grid-template-rows: repeat(2, minmax(120px, 148px)) !important;
  }

  html:not(.is-phone) #cardsGrid.bahthy-cards .card {
    min-height: 120px !important;
    max-height: 148px !important;
  }
}

@media (min-width: 1440px) and (min-height: 860px) {
  #viewDashboard .dashboard-hero {
    min-height: 176px !important;
    height: 176px !important;
    max-height: 176px !important;
  }
  #viewDashboard #cardsGrid.grid,
  #cardsGrid.bahthy-cards {
    grid-template-rows: repeat(2, minmax(148px, 176px)) !important;
    gap: 12px !important;
  }
  #cardsGrid.bahthy-cards .card {
    min-height: 148px !important;
    max-height: 176px !important;
  }
}

/* هواتف مصنّفة عبر JS (آيفون/أندرويد بما فيها الوضع الأفقي) */
html.is-phone #viewDashboard .dashboard-hero,
html.is-phone #viewDashboard .dashboard-hero-content,
html.is-phone #viewDashboard .dashboard-hero-title,
html.is-phone #viewDashboard .dashboard-hero-desc,
html.is-phone #viewDashboard .dashboard-hero-cta,
html.is-phone #viewDashboard .dashboard-hero-visual {
  display: none !important;
}

html.is-phone #appScreen.screen.active,
html.is-phone body:has(#appScreen.screen.active),
html.is-phone:has(#appScreen.screen.active) {
  height: var(--bahthy-app-h, 100dvh);
  max-height: var(--bahthy-app-h, 100dvh);
}

html.is-phone .bahthy-stats-bar {
  display: none !important;
}

html.is-phone #viewDashboard #cardsGrid.grid,
html.is-phone #cardsGrid.bahthy-cards {
  display: flex !important;
  flex-direction: column !important;
  grid-template-columns: none !important;
  grid-template-rows: none !important;
  flex: 1 1 auto !important;
  gap: clamp(5px, 0.8vh, 8px) !important;
  overflow: hidden !important;
}

html.is-phone #cardsGrid.bahthy-cards .card {
  display: grid !important;
  flex-direction: unset !important;
  grid-template-columns: auto minmax(0, 1fr) auto !important;
  grid-template-rows: auto !important;
  align-items: center !important;
  flex: 1 1 0 !important;
  min-height: 0 !important;
  max-height: none !important;
  height: auto !important;
  padding: 0 12px !important;
  border-radius: 12px !important;
}

html.is-phone #cardsGrid.bahthy-cards .card .card-icon-wrap {
  width: clamp(32px, 4.4vh, 40px) !important;
  height: clamp(32px, 4.4vh, 40px) !important;
  grid-column: 1 !important;
  grid-row: 1 !important;
  margin: 0 !important;
}

html.is-phone #cardsGrid.bahthy-cards .card .card-text {
  grid-column: 2 !important;
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
}

html.is-phone #cardsGrid.bahthy-cards .card .title {
  white-space: nowrap !important;
  font-size: clamp(0.82rem, 3.2vw, 0.95rem) !important;
}

html.is-phone #cardsGrid.bahthy-cards .card .sub {
  white-space: nowrap !important;
  display: block !important;
  -webkit-line-clamp: unset !important;
  font-size: clamp(0.62rem, 2.6vw, 0.72rem) !important;
}

#appScreen.screen.active {
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}
