/*
 * Magdy Physics Platform — responsive stability layer v2.0.1
 * Loaded after app.css and premium-2026.css.
 * Scope: public, student, parent and admin interfaces. No backend behavior changes.
 */

html {
  width: 100%;
  max-width: 100%;
  overflow-x: clip;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: clip;
}

main,
section,
header,
footer,
.container,
.app-shell,
.app-main,
.app-content,
.panel,
.card,
.form-card,
.table-wrap,
.hero-grid,
.hero-copy,
.hero-visual,
.nav-wrap,
.user-chip,
.app-title-wrap,
.app-heading {
  min-width: 0;
  max-width: 100%;
}

img,
svg,
video,
iframe,
canvas {
  max-width: 100%;
}

iframe,
video {
  width: 100%;
}

pre,
code,
.long-text,
.breakable {
  overflow-wrap: anywhere;
  word-break: break-word;
}

input,
select,
textarea,
button {
  max-width: 100%;
  min-width: 0;
}

.table-wrap {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: visible;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
}

.table-wrap table {
  width: 100%;
}

/* Public navigation: overlay stays below the header stacking context. */
.mobile-nav-overlay {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 220ms ease, visibility 220ms ease;
}

.mobile-nav-overlay.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

body.menu-open .main-header {
  z-index: 2100;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}

body.menu-open .mobile-nav-overlay {
  z-index: 2050;
}

/* Sidebar overlay follows the same predictable stacking model. */
.mobile-overlay {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 220ms ease, visibility 220ms ease;
}

.mobile-overlay.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

body.sidebar-open .sidebar {
  z-index: 2200;
}

body.sidebar-open .mobile-overlay {
  z-index: 2150;
}

/* Keep headings readable and prevent Arabic glyph collision. */
.public-premium-theme h1,
.public-premium-theme h2,
.public-premium-theme h3,
.app-body h1,
.app-body h2,
.app-body h3 {
  text-wrap: balance;
  overflow-wrap: normal;
  word-break: normal;
}

.public-premium-theme p,
.app-body p {
  text-wrap: pretty;
}

@media (max-width: 1180px) {
  .nav-wrap {
    gap: 12px;
  }

  .brand-text {
    min-width: 0;
  }

  .nav-actions {
    flex: 0 0 auto;
  }
}

@media (max-width: 980px) {
  /* Public header */
  .main-header {
    overflow: visible;
  }

  .nav-wrap {
    width: min(100% - 24px, 900px);
    min-height: 70px;
    height: auto;
    padding-block: 9px;
    gap: 10px;
  }

  .nav-wrap > .brand {
    flex: 1 1 auto;
    min-width: 0;
    max-width: calc(100% - 58px);
  }

  .nav-wrap > .brand .brand-text {
    min-width: 0;
    max-width: 100%;
  }

  .nav-wrap > .brand strong,
  .nav-wrap > .brand small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .menu-btn {
    display: inline-flex;
    flex: 0 0 46px;
    position: relative;
    z-index: 2;
  }

  .nav-links {
    display: flex;
    position: fixed;
    z-index: 2201;
    top: 0;
    right: 0;
    bottom: auto;
    left: auto;
    width: min(360px, calc(100vw - 36px));
    max-width: 100vw;
    height: 100vh;
    height: 100dvh;
    padding: 18px 16px max(18px, env(safe-area-inset-bottom));
    border: 0;
    border-radius: 0;
    background: #fff;
    box-shadow: -22px 0 60px rgba(7, 26, 56, .28);
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    transform: translate3d(105%, 0, 0);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: transform 240ms ease, opacity 180ms ease, visibility 240ms ease;
    will-change: transform;
  }

  .nav-links.open {
    transform: translate3d(0, 0, 0);
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  .nav-links a {
    flex: 0 0 auto;
    width: 100%;
    min-height: 46px;
    padding: 12px 14px;
    border-radius: 12px;
    display: flex;
    align-items: center;
  }

  .nav-links a::after {
    display: none;
  }

  .nav-links a.active {
    box-shadow: inset -4px 0 var(--brand-maroon, #8F1028);
  }

  .mobile-nav-head {
    display: flex;
    flex: 0 0 auto;
    width: 100%;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin: 0 0 12px;
    padding: 0 0 14px;
  }

  .mobile-nav-head .brand {
    min-width: 0;
    max-width: calc(100% - 52px);
  }

  .mobile-nav-head .brand > span:last-child {
    min-width: 0;
  }

  .mobile-nav-head .brand strong,
  .mobile-nav-head .brand small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .nav-close {
    display: grid;
    flex: 0 0 42px;
    width: 42px;
    height: 42px;
  }

  .mobile-nav-actions {
    display: grid;
    flex: 0 0 auto;
    width: 100%;
    gap: 9px;
    margin-top: auto;
    padding-top: 16px;
  }

  .mobile-nav-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(4, 17, 37, .58);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }

  /* Public hero */
  .hero.premium-hero {
    min-height: 0;
    padding: 36px 0 48px;
  }

  .hero-grid {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 30px;
  }

  .hero-copy {
    order: 1;
    width: 100%;
    max-width: none;
  }

  .hero-visual {
    order: 2;
    width: min(100%, 560px);
    min-height: 420px;
    margin-inline: auto;
  }

  .hero h1 {
    max-width: 17ch;
    font-size: clamp(36px, 8vw, 52px);
    line-height: 1.22;
    letter-spacing: -.025em;
    margin: 15px 0 16px;
  }

  .hero p {
    max-width: 64ch;
    font-size: 16px;
    line-height: 1.85;
  }

  .hero-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
  }

  .hero-actions .btn {
    width: 100%;
    min-width: 0;
    padding-inline: 12px;
    text-align: center;
  }

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

  .trust-item {
    min-width: 0;
    padding: 9px;
    border: 1px solid var(--border, #DDE3EC);
    border-radius: 12px;
    background: rgba(255, 255, 255, .72);
  }

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

  /* Application shell */
  .app-shell {
    display: block;
    width: 100%;
  }

  .app-main {
    width: 100%;
    min-width: 0;
  }

  .sidebar {
    position: fixed;
    top: 0;
    right: 0;
    bottom: auto;
    left: auto;
    width: min(320px, calc(100vw - 32px));
    max-width: 100vw;
    height: 100vh;
    height: 100dvh;
    transform: translate3d(105%, 0, 0);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    transition: transform 240ms ease, opacity 180ms ease, visibility 240ms ease;
    will-change: transform;
  }

  .sidebar.open {
    transform: translate3d(0, 0, 0);
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  .mobile-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(4, 17, 37, .58);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }

  .app-topbar {
    position: sticky;
    top: 0;
    z-index: 80;
    width: 100%;
  }

  .app-title-wrap {
    flex: 1 1 auto;
    min-width: 0;
  }

  .app-heading {
    min-width: 0;
  }

  .app-heading h1,
  .app-heading small,
  .app-breadcrumb {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-content {
    width: 100%;
    max-width: 100%;
    padding: 18px;
  }

  .grid,
  .dashboard-grid,
  .form-grid,
  .grid-2,
  .grid-3,
  .grid-4 {
    min-width: 0;
  }

  .panel,
  .card,
  .form-card {
    width: 100%;
  }
}

@media (max-width: 720px) {
  body {
    font-size: 14px;
    line-height: 1.65;
  }

  .container {
    width: calc(100% - 22px);
  }

  /* Header remains compact and unclipped. */
  .nav-wrap {
    width: calc(100% - 18px);
    min-height: 64px;
    padding-block: 7px;
  }

  .brand-mark {
    flex: 0 0 42px;
    width: 42px;
    height: 42px;
    border-radius: 13px;
    font-size: 20px;
  }

  .nav-wrap > .brand strong {
    font-size: 12.5px;
  }

  .nav-wrap > .brand small {
    font-size: 9px;
  }

  .nav-actions > a {
    display: none;
  }

  .menu-btn {
    display: inline-flex;
    flex-basis: 44px;
    width: 44px;
    height: 44px;
  }

  /* Hero mobile composition */
  .hero.premium-hero {
    padding: 28px 0 40px;
  }

  .hero-grid {
    gap: 24px;
  }

  .hero-copy {
    text-align: right;
  }

  .hero .eyebrow {
    max-width: 100%;
    white-space: normal;
    line-height: 1.55;
  }

  .hero h1 {
    max-width: 100%;
    font-size: clamp(31px, 10.2vw, 42px);
    line-height: 1.28;
    letter-spacing: -.02em;
    margin-block: 13px 14px;
  }

  .hero p {
    font-size: 14px;
    line-height: 1.8;
    margin-bottom: 18px;
  }

  .hero-actions {
    grid-template-columns: 1fr;
    gap: 9px;
  }

  .hero-actions .btn {
    min-height: 48px;
  }

  .hero-trust {
    grid-template-columns: 1fr;
    margin-top: 18px;
  }

  .trust-item {
    width: 100%;
    min-height: 50px;
  }

  .hero-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 18px;
  }

  .hero-stat {
    min-width: 0;
    padding: 12px 8px;
  }

  .hero-stat strong {
    font-size: 18px;
  }

  .hero-stat span {
    font-size: 10px;
    line-height: 1.45;
  }

  .hero-visual {
    width: 100%;
    min-height: 350px;
  }

  .teacher-stage {
    inset: 16px 2px 0;
  }

  .teacher-image {
    max-width: 78%;
    margin-inline: auto;
  }

  .science-badge {
    max-width: 148px;
    padding: 8px 9px;
    gap: 7px;
    font-size: 10px;
  }

  .science-badge small {
    font-size: 8.5px;
  }

  .science-badge.one {
    top: 18px;
    right: 0;
  }

  .science-badge.two {
    top: 155px;
    left: 0;
  }

  .science-badge.three {
    right: 4px;
    bottom: 8px;
  }

  .science-symbol.atom {
    left: 2px;
    top: 38px;
  }

  .science-symbol.wave {
    right: 4px;
    top: 165px;
  }

  .science-symbol.formula {
    display: none;
  }

  /* General public sections */
  .section {
    padding: 44px 0;
  }

  .section-sm {
    padding: 32px 0;
  }

  .section-head {
    gap: 13px;
    margin-bottom: 21px;
  }

  .section-head h2,
  .page-hero h1 {
    font-size: clamp(27px, 8vw, 36px);
    line-height: 1.3;
  }

  .grid-2,
  .grid-3,
  .grid-4,
  .form-grid,
  .kpi-grid {
    grid-template-columns: 1fr;
  }

  .card,
  .panel,
  .form-card {
    padding: 17px;
    border-radius: 17px;
  }

  .course-card {
    padding: 0;
  }

  .actions,
  .form-actions,
  .panel-head,
  .course-meta,
  .footer-bottom {
    flex-wrap: wrap;
  }

  .actions .btn,
  .form-actions .btn {
    flex: 1 1 100%;
    width: 100%;
  }

  .cta-box {
    grid-template-columns: 1fr;
    padding: 24px 18px;
    border-radius: 23px;
  }

  .cta-box h2 {
    font-size: 27px;
    line-height: 1.35;
  }

  /* App header and content */
  .app-topbar {
    min-height: 66px;
    padding: 8px 10px;
    gap: 8px;
  }

  .app-title-wrap {
    gap: 8px;
  }

  .app-breadcrumb,
  .app-heading small {
    display: none;
  }

  .app-heading h1 {
    font-size: 15px;
    line-height: 1.4;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .user-chip {
    flex: 0 0 auto;
    gap: 6px;
  }

  .user-chip .avatar {
    width: 38px;
    height: 38px;
    border-radius: 11px;
  }

  .user-chip form .btn,
  .user-chip > .btn {
    min-width: 42px;
    width: 42px;
    height: 42px;
    padding: 0;
    font-size: 0;
  }

  .user-chip form .btn .ui-icon,
  .user-chip > .btn .ui-icon {
    width: 18px;
    height: 18px;
  }

  .app-content {
    padding: 12px;
  }

  .dashboard-hero {
    padding: 18px;
    border-radius: 18px;
  }

  .dashboard-hero h2 {
    font-size: 23px;
    line-height: 1.35;
  }

  .kpi-grid {
    gap: 9px;
  }

  .kpi {
    width: 100%;
  }

  .panel-head {
    align-items: stretch;
    gap: 10px;
  }

  .panel-head > .btn,
  .panel-head > .actions {
    width: 100%;
  }

  .table-wrap table {
    min-width: 680px;
  }

  .table-wrap::after {
    content: "مرّر أفقياً لعرض بقية الجدول";
    display: block;
    position: sticky;
    right: 0;
    width: max-content;
    margin: 8px 0 0 auto;
    padding: 5px 9px;
    border-radius: 999px;
    background: #EEF3FA;
    color: #5F6D82;
    font-size: 9px;
    font-weight: 700;
  }

  .form-control,
  input,
  select,
  textarea {
    width: 100%;
  }

  dialog,
  .modal,
  .ui-confirm-card {
    max-width: calc(100vw - 22px);
  }

  .site-footer {
    margin-top: 34px;
    padding-top: 46px;
  }
}

@media (max-width: 420px) {
  .container {
    width: calc(100% - 18px);
  }

  .nav-wrap > .brand {
    max-width: calc(100% - 52px);
  }

  .nav-wrap > .brand small {
    display: none;
  }

  .nav-wrap > .brand strong {
    max-width: 185px;
    font-size: 11.5px;
  }

  .brand-mark {
    flex-basis: 40px;
    width: 40px;
    height: 40px;
  }

  .nav-links {
    width: min(300px, calc(100vw - 22px));
    padding-inline: 13px;
  }

  .hero h1 {
    font-size: clamp(29px, 9.8vw, 38px);
    line-height: 1.31;
  }

  .hero-visual {
    min-height: 320px;
  }

  .science-badge.two {
    display: none;
  }

  .science-badge.one {
    max-width: 135px;
  }

  .science-badge.three {
    max-width: 138px;
  }

  .teacher-image {
    max-width: 84%;
  }

  .hero-stats {
    grid-template-columns: 1fr 1fr;
  }

  .app-topbar {
    padding-inline: 8px;
  }

  .app-heading h1 {
    max-width: 145px;
  }

  .sidebar {
    width: min(300px, calc(100vw - 22px));
  }

  .table-wrap table {
    min-width: 620px;
  }
}

@media (max-width: 340px) {
  .nav-wrap > .brand strong {
    max-width: 145px;
  }

  .hero h1 {
    font-size: 28px;
  }

  .hero-stats {
    grid-template-columns: 1fr;
  }

  .hero-visual {
    min-height: 295px;
  }

  .science-badge.one {
    top: 5px;
  }

  .science-badge.three {
    bottom: 0;
  }

  .app-heading h1 {
    max-width: 120px;
  }
}

@media (max-width: 980px) and (orientation: landscape) and (max-height: 520px) {
  .nav-links,
  .sidebar {
    width: min(390px, 70vw);
  }

  .hero-visual {
    min-height: 320px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .nav-links,
  .sidebar,
  .mobile-nav-overlay,
  .mobile-overlay {
    transition-duration: .01ms;
  }
}
