/* ============================================================================
   R630 · math.science.cmu.ac.th — PHASE 1 MOBILE UI (≤767px)
   Reference: CMU website UX/IA (app bar + drawer + cards). Own implementation.
   Scope: mobile only. Every rule lives inside the media query — desktop/tablet
   (≥768px) is untouched by construction.
   Colours: CMU CI purple #9E76B4 (structure) + Faculty of Science #F6E002 (accent)
   ========================================================================== */
@media (max-width: 767px) {

  /* ---------- tokens ---------------------------------------------------- */
  :root {
    --m-purple: #9E76B4;
    --m-purple-strong: #6E4E8A;
    --m-purple-deep: #3B2551;
    --m-gold: #F6E002;
    --m-gold-ink: #212529;
    --m-ink: #212529;
    --m-body: #5C636A;
    --m-line: #E9ECEF;
    --m-surface: #FFFFFF;
    --m-canvas: #F6F6F8;
    --m-radius: 8px;
    --m-shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 6px 18px rgba(16, 24, 40, .06);
  }

  /* ---------- global safety --------------------------------------------- */
  html, body { overflow-x: hidden; }
  body { background: var(--m-canvas); }
  .container { padding-left: 12px; padding-right: 12px; }
  img, iframe, table { max-width: 100%; }
  iframe { height: auto; aspect-ratio: 4 / 3; }
  .table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* ==========================================================================
     1 · HEADER / APP BAR  (compact, sticky, 56px)
     ========================================================================== */
  .navbar.navbar-default.navbar-static-top {
    position: sticky;
    top: 0;
    z-index: 1030;
    min-height: 56px;
    margin: 0;
    padding: 0;
    background: var(--m-surface);
    border: 0;
    border-bottom: 1px solid var(--m-line);
    box-shadow: 0 1px 2px rgba(16, 24, 40, .05);
    border-radius: 0;
  }
  .navbar.navbar-default.navbar-static-top .container {
    padding: 0 12px;
    display: flex;
    align-items: center;
    min-height: 56px;
  }

  /* logo shrinks to a tap-friendly wordmark, hamburger pinned right */
  .navbar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    float: none;
    width: 100%;
    min-height: 56px;
  }
  .navbar-header > br { display: none; }
  .navbar-header > a { display: flex; align-items: center; order: 1; min-width: 0; height: 56px; }
  /* desktop lockup (title_th/en.png) is hidden on mobile only — desktop rules untouched */
  .navbar-header > a > img:not(.m-logo),
  .navbar-header > a > picture { display: none !important; }
  /* Approved MATH logo (/images/math.png) on mobile only. It is a CSS background inside
     this media query, so tablet/desktop never request the file at all (an <img>, even
     display:none/lazy, still gets fetched). 1536x1024 canvas, artwork at x125-1410 /
     y295-715 → a 136x44 box with background-size:cover trims the white padding only and
     keeps the artwork's own aspect ratio; the source PNG is never modified.
     ponytail: fixed 136x44 box; swap for a pre-trimmed/smaller asset if one is approved. */
  html body .navbar-static-top .navbar-header > a > .m-logo {
    display: block !important;
    width: 136px !important;
    height: 44px !important;
    max-width: none !important;
    max-height: 44px !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0;
    /* WebP (pre-cropped to the artwork, generated from the untouched PNG) with the
       original PNG as the fallback for browsers without WebP support. Both keep the
       same 136x44 box and aspect ratio. */
    background-image: url(/images/math.png);
    background-image: image-set(
      url(/images/webp/math-272.webp) type("image/webp") 1x,
      url(/images/webp/math-408.webp) type("image/webp") 2x,
      url(/images/webp/math-816.webp) type("image/webp") 3x,
      url(/images/math.png) 1x
    );
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
  }
  .navbar-toggle.navbar-toggle {
    order: 2;
    float: none;
    margin: 0;
    padding: 0;
    width: 44px;
    height: 44px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border: 1px solid var(--m-line);
    border-radius: 10px;
    background: var(--m-surface);
  }
  .navbar-toggle .icon-bar {
    display: block;
    width: 20px;
    height: 2px;
    margin: 0 !important;
    border-radius: 2px;
    background: var(--m-purple-strong);
  }
  .navbar-toggle:focus-visible,
  .navbar-toggle:hover { background: #F3EEF8; border-color: var(--m-purple); }

  /* ==========================================================================
     2 · MOBILE DRAWER (was Bootstrap collapse) — white sheet under the app bar
     ========================================================================== */
  .navbar-collapse {
    position: fixed;
    top: 56px;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1029;
    margin: 0;
    padding: 4px 0 32px;
    background: var(--m-surface);
    border: 0;
    border-top: 1px solid var(--m-line);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
  .navbar-collapse.in,
  .navbar-collapse.collapsing { box-shadow: 0 -1px 0 var(--m-line); }

  /* group label that the legacy markup puts in <div> before each list */
  .navbar-disable > div {
    margin: 0;
    padding: 16px 16px 8px;
    border: 0 !important;
    font-size: 12.5px !important;
    font-weight: 700 !important;
    letter-spacing: .04em;
    text-transform: none;
    color: var(--m-purple-strong) !important;
    background: var(--m-surface);
  }

  /* rows: 52px tall, 16px text, thumb friendly */
  .navbar-collapse .nav.navbar-nav { margin: 0; }
  .navbar-collapse .nav.navbar-nav > li { float: none; }
  .navbar-collapse .nav.navbar-nav > li > a {
    display: flex;
    align-items: center;
    min-height: 52px;
    padding: 12px 16px !important;
    border-bottom: 1px solid var(--m-line);
    font-size: 15.5px;
    line-height: 1.4;
    color: var(--m-ink) !important;
    white-space: normal;
    background: var(--m-surface);
  }
  .navbar-collapse .nav.navbar-nav > li > a:active,
  .navbar-collapse .nav.navbar-nav > li > a:focus,
  .navbar-collapse .nav.navbar-nav > li > a:hover,
  .navbar-collapse .nav.navbar-nav > li.active > a {
    background: #F5F1FA;
    color: var(--m-purple-strong) !important;
    box-shadow: inset 3px 0 0 var(--m-gold);
  }

  /* nested lists (sub-categories) — indented, quieter */
  .navbar-collapse .nav.navbar-nav ul,
  .navbar-collapse ul.nav ul { margin: 0; padding-left: 0; list-style: none; }
  .navbar-collapse .nav.navbar-nav ul > li > a {
    display: flex;
    align-items: center;
    min-height: 46px;
    padding: 10px 16px 10px 30px !important;
    border-bottom: 1px solid #F1F1F4;
    font-size: 14.5px;
    color: #4A5057 !important;
    background: #FBFBFD;
  }
  .navbar-collapse .nav.navbar-nav ul > li > a:active { background: #F5F1FA; }

  /* the duplicated "หน้าหลัก" label rows that exist only for the old desktop bar */
  .navbar-full_Disable { display: none; }

  /* ==========================================================================
     3 · HERO  (slides are already off on mobile → static, calm block)
     ========================================================================== */
  /* the bare <br> that used to sit under the slider adds dead space */
  .container > .row > .col-md-12 > br:first-child { display: none; }

  /* header band for the news area: light surface, gold keyline, no empty box */
  .tab-content { padding-top: 4px; }
  .tab-content > .tab-pane > .row > .col-md-12 > h2,
  .tab-content > .tab-pane > .row > .col-md-12 > h4 {
    margin: 6px 0 12px;
    padding: 0 0 10px 0;
    border-bottom: 0;
    font-size: 18px;
    font-weight: 700;
    color: var(--m-purple-strong) !important;
    text-decoration: none !important;
    position: relative;
  }
  .tab-content > .tab-pane > .row > .col-md-12 > h2::after,
  .tab-content > .tab-pane > .row > .col-md-12 > h4::after {
    content: "";
    position: absolute;
    left: 0; bottom: 0;
    width: 44px; height: 4px;
    border-radius: 2px;
    background: var(--m-gold);
  }

  /* segmented control: the news tab strip, sticky under the app bar */
  ul.nav.nav-tabs {
    position: sticky;
    top: 56px;
    z-index: 1025;
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
    margin: 0 0 8px;
    padding: 8px 0 10px;
    background: var(--m-canvas);
    border-bottom: 1px solid var(--m-line);
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  ul.nav.nav-tabs::-webkit-scrollbar { display: none; }
  ul.nav.nav-tabs > li {
    float: none;
    flex: 0 0 auto;
    margin: 0;
  }
  ul.nav.nav-tabs > li > a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 56px;
    min-width: 84px;
    margin: 0 !important;
    padding: 6px 10px !important;
    border: 1px solid var(--m-line) !important;
    border-radius: 10px !important;
    background: var(--m-surface);
    font-size: 12.5px;
    line-height: 1.25;
    color: var(--m-body) !important;
  }
  ul.nav.nav-tabs > li.active > a,
  ul.nav.nav-tabs > li.active > a:hover,
  ul.nav.nav-tabs > li.active > a:focus {
    background: #F3EEF8 !important;
    border-color: var(--m-purple) !important;
    color: var(--m-purple-strong) !important;
    box-shadow: inset 0 -3px 0 var(--m-gold);
  }
  ul.nav.nav-tabs > li > a i { font-size: 22px !important; margin-bottom: 2px; }

  /* Scroll cue: the strip fades out over its last 28px so it reads as scrollable.
     A mask is used because the chips paint their own opaque background — a
     background gradient on the <ul> would sit underneath them and never show.
     A mask also never intercepts pointer events, so chips stay tappable.
     `m-at-end` is set by a 6-line guard in include/navbar.php when the strip is
     scrolled to its end (or does not overflow), which removes the fade so the
     last chip is fully readable. */
  html body ul.nav.nav-tabs {
    -webkit-mask-image: linear-gradient(to right, #000 0, #000 calc(100% - 44px), transparent 100%);
    mask-image: linear-gradient(to right, #000 0, #000 calc(100% - 44px), transparent 100%);
  }
  html body ul.nav.nav-tabs.m-at-end {
    -webkit-mask-image: none;
    mask-image: none;
  }

  /* ==========================================================================
     4 · NEWS CARDS — compact row (square thumb left, text left)
     index.css and css-info.css both style `.box-height` at ≤767px with
     !important. Every rule here is prefixed `html body .tab-content` so this
     block wins on specificity — one owner, no extra shims downstream.
     ========================================================================== */
  .tab-content .row { margin-left: -6px; margin-right: -6px; }
  .tab-content .row > [class*="col-"] { padding-left: 6px; padding-right: 6px; }

  html body .tab-content .box-height.font-news {
    display: flex !important;
    flex-direction: row !important;
    align-items: flex-start !important;
    gap: 12px !important;
    box-sizing: border-box !important;
    width: 100% !important;
    min-height: 0 !important;
    margin: 0 0 10px !important;
    padding: 10px !important;
    background: var(--m-surface);
    border-radius: 12px;
    box-shadow: var(--m-shadow);
    overflow: hidden;
    text-align: left !important;
  }
  html body .tab-content .box-height.font-news > .col-md-5 {
    flex: 0 0 96px !important;
    width: 96px !important;
    max-width: 96px !important;
    padding: 0 !important;
    margin: 0 !important;   /* index.css adds margin-bottom:15px → the dead tail gap */
    float: none !important;
  }
  html body .tab-content .box-height.font-news > .col-md-5 a { display: block; }
  /* one square thumb, object-fit:cover — no letterbox strip, no distortion */
  html body .tab-content .box-height.font-news > .col-md-5 img,
  html body .tab-content .box-height.font-news img.thumbnail,
  html body .tab-content .box-height.font-news img.img-responsive {
    width: 96px !important;
    height: 96px !important;
    min-height: 0 !important;
    max-width: 96px !important;
    max-height: 96px !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    border-radius: 10px !important;
    box-shadow: none !important;
    object-fit: cover !important;
    aspect-ratio: auto !important;
    background: #F4F4F6;
  }
  html body .tab-content .box-height.font-news > .col-md-7 {
    flex: 1 1 auto !important;
    width: auto !important;
    max-width: calc(100% - 108px) !important;
    min-width: 0 !important;
    padding: 0 !important;
    margin: 0 !important;
    float: none !important;
    text-align: left !important;
  }
  /* the legacy <br>s that spaced title/hashtag/date are now handled by margin */
  html body .tab-content .box-height.font-news > .col-md-7 > br,
  html body .tab-content .box-height.font-news .news-title br { display: none !important; }
  html body .tab-content .box-height.font-news .news-title {
    display: -webkit-box !important;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
    margin: 0 0 4px !important;
    font-size: 15px !important;
    line-height: 1.45 !important;
    font-weight: 600 !important;
    color: var(--m-ink) !important;
    text-align: left !important;
  }
  html body .tab-content .box-height.font-news > .col-md-7 > span { display: block; }
  html body .tab-content .box-height.font-news > .col-md-7 > span > a {
    color: var(--m-purple-strong) !important;
    font-size: 12.5px !important;
  }
  html body .tab-content .box-height.font-news > .col-md-7 > span:last-child {
    margin-top: 4px !important;
    color: #6B7280 !important;   /* WCAG AA 4.83 on white (was #8A8F98 = 3.25) */
    font-size: 12px !important;
    padding: 0 !important;
  }
  /* WCAG 2.5.8: the inline hashtag / category / "see all" links measured 16–21px;
     padding-block lifts them to a 24px target without moving the layout. */
  html body .tab-content .box-height.font-news > .col-md-7 > span > a,
  html body .alert > a { display: inline-block; padding-block: 4px; min-height: 24px; }

  /* card height follows content — no reserved tail gap */
  html body .tab-content .box-height.font-news > .col-md-7 > *:last-child { margin-bottom: 0 !important; }

  /* generic thumbnails elsewhere (right column, galleries) */
  .thumbnail {
    border: 0;
    border-radius: var(--m-radius);
    box-shadow: var(--m-shadow);
    background: var(--m-surface);
    padding: 0;
    overflow: hidden;
  }

  .alert { border-radius: 10px; border: 0; box-shadow: var(--m-shadow); font-size: 14px; }
  .alert-warning { background: #FFF9D6; color: var(--m-gold-ink); }
  .alert-warning a { color: var(--m-purple-strong); font-weight: 600; }

  /* ==========================================================================
     5 · TYPOGRAPHY
     ========================================================================== */
  .container { font-size: 15px; line-height: 1.7; color: var(--m-body); }
  .container h1 { font-size: 24px; line-height: 1.3; font-weight: 700; color: var(--m-ink); margin: 16px 0 10px; }
  .container h2 { font-size: 21px; line-height: 1.35; font-weight: 700; color: var(--m-ink); margin: 16px 0 10px; }
  .container h3 { font-size: 18px; line-height: 1.4; font-weight: 600; color: var(--m-ink); margin: 14px 0 8px; }
  .container h4 { font-size: 16.5px; line-height: 1.45; font-weight: 600; color: var(--m-ink); }
  .container p { margin: 0 0 12px; }
  .container a { color: var(--m-purple-strong); }
  .container a:focus-visible { outline: 2px solid var(--m-gold); outline-offset: 2px; }

  /* long legacy inline colours inside news items → CI-safe on mobile */
  .font-news a[style*="color"] { color: var(--m-purple-strong) !important; }

  /* ==========================================================================
     6 · FOOTER / misc alignment with the app bar width
     ========================================================================== */
  #cmu_global_foot_menu.cmu-footer { margin-top: 8px; }
  #cmu_global_foot_menu .container { padding-left: 16px; padding-right: 16px; }

  /* ==========================================================================
     7 · MOBILE SLIDER (CMU-style hero band; desktop keeps Ken Burns)
     ========================================================================== */
  .m-slides {
    position: relative;
    margin: 0;                 /* full-bleed like the CMU hero, no wasted gutters */
    background: var(--m-surface);
    border-radius: 0;
    box-shadow: none;
    overflow: hidden;
  }
  .m-slides-track {
    display: flex;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    scroll-behavior: smooth;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .m-slides-track::-webkit-scrollbar { display: none; }
  .m-slide { flex: 0 0 100%; min-width: 100%; scroll-snap-align: center; scroll-snap-stop: always; }
  .m-slide > a { display: block; }
  /* slides are 1170x300 CMS banners: contain keeps every pixel, no crop/distortion */
  .m-slide img {
    display: block;
    width: 100%;
    height: auto;
    max-height: 46vh;
    object-fit: contain;
    background: var(--m-surface);
  }
  .m-slides-title {
    padding: 10px 16px 0;
    font-size: 14.5px;
    line-height: 1.45;
    font-weight: 500;
    color: var(--m-ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  /* caption keeps its line height only when it actually has text; an empty
     title (CMS rows with title_th = '') collapses to nothing instead of
     reserving 1.9em of dead space under the artwork. */
  .m-slides-title:not(:empty) { min-height: 1.9em; }
  .m-slides-title:empty { display: none; }
  /* control bar under the image: 44px targets, never covers the artwork */
  .m-slides-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 4px 12px 10px;
  }
  .m-slides-btn {
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--m-line);
    border-radius: 12px;
    background: var(--m-surface);
    color: var(--m-purple-strong);
    font-size: 16px;
    line-height: 1;
    box-shadow: none;
  }
  .m-slides-btn:active { background: #F3EEF8; }
  .m-slides-btn:focus-visible { outline: 2px solid var(--m-gold); outline-offset: 2px; }
  .m-slides-dots { flex: 1 1 auto; display: flex; justify-content: center; align-items: center; gap: 7px; padding: 0; }
  .m-count { flex: 0 0 auto; min-width: 34px; text-align: right; font-size: 12px; color: #6B7280; font-variant-numeric: tabular-nums; }
  .m-dot {
    width: 8px;
    height: 8px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: #D9D2E2;
    transition: width .2s ease, background .2s ease;
  }
  .m-dot.on { width: 22px; border-radius: 4px; background: var(--m-gold); }
  @media (prefers-reduced-motion: reduce) {
    .m-slides-track { scroll-behavior: auto; }
    .m-dot { transition: none; }
  }
}

/* the mobile carousel must never appear on tablet/desktop (also a no-JS guard) */
@media (min-width: 768px) {
  .m-slides { display: none; }
  .m-logo { display: none; }
}
/* default: the mobile logo is mobile-only, never part of the desktop header */
.m-logo { display: none; }

