/* =========================================================
   MOBILE BOTTOM TAB BAR & SUBMENU SHEET (Mini-program Style)
   Adaptive across all themes: sovereign / classic / orbit / clarity
   Active only at <= 900px; desktop (> 900px) remains untouched.
   ========================================================= */

@media (min-width: 901px) {
  .m-tabbar,
  .m-sheet,
  .m-sheet-backdrop {
    display: none !important;
  }
}

@media (max-width: 900px) {
  :root {
    --mtb-height: 60px;
    --mtb-bg: rgba(255, 255, 255, 0.94);
    --mtb-border: var(--line, rgba(15, 23, 42, 0.1));
    --mtb-ink: var(--muted, #64748B);
    --mtb-active-ink: var(--navy-900, #0B1528);
    --mtb-active-accent: var(--brass, #C29B38);
    --mtb-active-pill: color-mix(in srgb, var(--brass, #C29B38) 14%, transparent);
    --mtb-shadow: 0 -6px 24px rgba(6, 11, 20, 0.09);

    --ms-bg: var(--paper, #FFFFFF);
    --ms-surface: var(--paper-alt, #F6F8FB);
    --ms-ink: var(--ink, #0F172A);
    --ms-muted: var(--muted, #64748B);
    --ms-border: var(--line, #E2E8F0);
    --ms-accent: var(--brass, #C29B38);
    --ms-accent-text: var(--navy-900, #0B1528);
    --ms-radius: 18px;
    --ms-shadow: 0 -16px 40px rgba(6, 11, 20, 0.18);
  }

  /* ---- Theme-Specific Tuning ---- */
  [data-theme="sovereign"] {
    --mtb-bg: rgba(255, 255, 255, 0.94);
    --mtb-border: rgba(194, 155, 56, 0.28);
    --mtb-ink: #5A677D;
    --mtb-active-ink: var(--navy-900, #0B1528);
    --mtb-active-accent: var(--brass-dark, #9B7829);
    --mtb-active-pill: rgba(194, 155, 56, 0.14);
    --mtb-shadow: 0 -8px 26px rgba(11, 21, 40, 0.1);

    --ms-bg: #FFFFFF;
    --ms-surface: #F6F8FB;
    --ms-ink: #0B1528;
    --ms-muted: #5A677D;
    --ms-border: rgba(15, 23, 42, 0.1);
    --ms-accent: var(--brass-dark, #9B7829);
    --ms-accent-text: var(--navy-900, #0B1528);
  }

  [data-theme="classic"] {
    --mtb-bg: rgba(255, 255, 255, 0.95);
    --mtb-border: var(--line, #E7E5DF);
    --mtb-ink: var(--muted, #5F6470);
    --mtb-active-ink: var(--navy-900, #0A1F3C);
    --mtb-active-accent: var(--brass, #A9873B);
    --mtb-active-pill: rgba(169, 135, 59, 0.14);
    --mtb-shadow: 0 -6px 24px rgba(10, 31, 60, 0.09);

    --ms-bg: var(--paper, #FFFFFF);
    --ms-surface: var(--paper-alt, #F9F8F4);
    --ms-ink: var(--ink, #17171F);
    --ms-muted: var(--muted, #5F6470);
    --ms-border: var(--line, #E7E5DF);
    --ms-accent: var(--brass-dark, #7A5F2D);
    --ms-accent-text: var(--navy-900, #0A1F3C);
  }

  [data-theme="clarity"] {
    --mtb-bg: rgba(255, 255, 255, 0.92);
    --mtb-border: var(--line, #DEDEE3);
    --mtb-ink: var(--quiet, #6E6E73);
    --mtb-active-ink: var(--brass, #005BB5);
    --mtb-active-accent: var(--brass, #005BB5);
    --mtb-active-pill: rgba(0, 91, 181, 0.11);
    --mtb-shadow: 0 -6px 24px rgba(0, 0, 0, 0.08);

    --ms-bg: var(--surface, #FFFFFF);
    --ms-surface: var(--canvas, #F5F5F7);
    --ms-ink: var(--ink, #1D1D1F);
    --ms-muted: var(--quiet, #6E6E73);
    --ms-border: var(--line-light, #EAEAF0);
    --ms-accent: var(--brass, #005BB5);
    --ms-accent-text: var(--brass, #005BB5);
    --ms-radius: 20px;
  }

  [data-theme="orbit"] {
    --mtb-bg: color-mix(in srgb, var(--base, #04070E) 92%, transparent);
    --mtb-border: color-mix(in srgb, var(--brass, #22D3EE) 26%, var(--line, #1E293B));
    --mtb-ink: var(--muted, #8B9BB4);
    --mtb-active-ink: #FFFFFF;
    --mtb-active-accent: var(--brass, #22D3EE);
    --mtb-active-pill: color-mix(in srgb, var(--brass, #22D3EE) 16%, transparent);
    --mtb-shadow: 0 -10px 28px rgba(0, 0, 0, 0.55);

    --ms-bg: var(--paper-alt, #0E1525);
    --ms-surface: var(--paper-raised, #162035);
    --ms-ink: var(--ink, #E6EDF7);
    --ms-muted: var(--muted, #8B9BB4);
    --ms-border: var(--line, rgba(255, 255, 255, 0.12));
    --ms-accent: var(--brass, #22D3EE);
    --ms-accent-text: var(--brass, #22D3EE);
    --ms-radius: 14px;
    --ms-shadow: 0 -18px 44px rgba(0, 0, 0, 0.65);
  }

  /* ---- Streamlined Mobile Topbar (Logo + Language Only) ---- */
  .site-header .nav-toggle,
  .site-header .main-nav {
    display: none !important;
  }
  .site-header .header-inner {
    height: 56px;
    gap: 10px;
  }
  .site-header .logo {
    min-width: 0;
    flex: 0 1 auto;
    margin-right: auto;
  }
  .site-header .logo-img {
    height: 32px;
    max-height: 32px;
    width: auto;
    max-width: min(200px, 56vw);
  }
  .site-header .lang-switcher {
    margin-left: auto;
    margin-right: 0;
    flex-shrink: 0;
  }

  /* ---- Bottom Clearance & AI Launcher Avoidance ---- */
  html {
    scroll-padding-bottom: calc(var(--mtb-height) + 12px + env(safe-area-inset-bottom, 0px));
  }
  .site-footer {
    padding-bottom: calc(var(--mtb-height) + 14px + env(safe-area-inset-bottom, 0px)) !important;
  }
  .ide-launcher {
    bottom: calc(var(--mtb-height) + 14px + env(safe-area-inset-bottom, 0px)) !important;
    right: 16px;
    transition: opacity 0.18s ease, transform 0.2s ease;
  }
  html.m-sheet-locked .ide-launcher {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: scale(0.9);
  }

  /* ---- Scroll Lock When Sheet Is Open ---- */
  html.m-sheet-locked,
  html.m-sheet-locked body {
    overflow: hidden;
  }

  [hidden] {
    display: none !important;
  }

  /* ---- Fixed Bottom Tab Bar ---- */
  .m-tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9020;
    display: flex;
    align-items: stretch;
    justify-content: space-around;
    background: var(--mtb-bg);
    border-top: 1px solid var(--mtb-border);
    box-shadow: var(--mtb-shadow);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    padding: 0 max(4px, env(safe-area-inset-right, 0px)) env(safe-area-inset-bottom, 0px) max(4px, env(safe-area-inset-left, 0px));
    font-family: var(--font-sans, -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif);
    user-select: none;
  }

  .m-tabbar-item {
    position: relative;
    flex: 1 1 0;
    min-width: 0;
    min-height: var(--mtb-height);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 6px 4px 5px;
    margin: 0;
    border: 0;
    background: transparent;
    color: var(--mtb-ink);
    text-decoration: none;
    font: inherit;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: color 0.18s ease;
  }

  .m-tabbar-item::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    width: 24px;
    height: 2.5px;
    border-radius: 0 0 3px 3px;
    background: var(--mtb-active-accent);
    transform: translateX(-50%) scaleX(0);
    opacity: 0;
    transition: transform 0.2s cubic-bezier(0.22, 0.61, 0.36, 1), opacity 0.2s ease;
  }

  .m-tabbar-icon {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 26px;
    border-radius: 999px;
    transition: background-color 0.18s ease, transform 0.18s ease, color 0.18s ease;
  }

  .m-tabbar-icon svg {
    width: 20px;
    height: 20px;
    display: block;
  }

  .m-tabbar-label {
    display: block;
    max-width: 100%;
    font-size: 11px;
    font-weight: 500;
    line-height: 1.2;
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .m-tabbar-badge {
    position: absolute;
    top: -2px;
    right: 2px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 999px;
    background: var(--mtb-active-accent);
    color: #FFFFFF;
    font-size: 10px;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.22);
  }
  [data-theme="orbit"] .m-tabbar-badge {
    color: #04070E;
  }

  .m-tabbar-item.is-active,
  .m-tabbar-item[aria-current="page"],
  .m-tabbar-item.is-sheet-open {
    color: var(--mtb-active-ink);
  }

  .m-tabbar-item.is-active::before,
  .m-tabbar-item[aria-current="page"]::before,
  .m-tabbar-item.is-sheet-open::before {
    transform: translateX(-50%) scaleX(1);
    opacity: 1;
  }
  html.m-sheet-locked .m-tabbar-item.is-active:not(.is-sheet-open)::before,
  html.m-sheet-locked .m-tabbar-item[aria-current="page"]:not(.is-sheet-open)::before {
    opacity: 0;
  }

  .m-tabbar-item.is-active .m-tabbar-icon,
  .m-tabbar-item[aria-current="page"] .m-tabbar-icon,
  .m-tabbar-item.is-sheet-open .m-tabbar-icon {
    background: var(--mtb-active-pill);
    color: var(--mtb-active-accent);
  }

  .m-tabbar-item.is-active .m-tabbar-label,
  .m-tabbar-item[aria-current="page"] .m-tabbar-label,
  .m-tabbar-item.is-sheet-open .m-tabbar-label {
    font-weight: 700;
  }

  .m-tabbar-item:active .m-tabbar-icon {
    transform: scale(0.92);
  }

  .m-tabbar-item:focus-visible {
    outline: 2px solid var(--mtb-active-accent);
    outline-offset: -3px;
    border-radius: 8px;
  }

  /* ---- Backdrop & Bottom Sheet ---- */
  .m-sheet-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: calc(var(--mtb-height) + 1px + env(safe-area-inset-bottom, 0px));
    z-index: 9010;
    background: rgba(6, 11, 20, 0.46);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    opacity: 0;
    transition: opacity 0.22s ease;
  }
  .m-sheet-backdrop.is-visible {
    opacity: 1;
  }

  .m-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--mtb-height) + 1px + env(safe-area-inset-bottom, 0px));
    z-index: 9015;
    max-height: min(70dvh, calc(100dvh - 124px));
    display: flex;
    flex-direction: column;
    background: var(--ms-bg);
    color: var(--ms-ink);
    border-top: 1px solid var(--mtb-border);
    border-radius: var(--ms-radius) var(--ms-radius) 0 0;
    box-shadow: var(--ms-shadow);
    overflow: hidden;
    transform: translateY(18px);
    opacity: 0;
    transition: transform 0.24s cubic-bezier(0.22, 0.61, 0.36, 1), opacity 0.2s ease;
    font-family: var(--font-sans, -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif);
  }
  .m-sheet.is-visible {
    transform: translateY(0);
    opacity: 1;
  }

  .m-sheet-handle {
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: var(--ms-border);
    margin: 9px auto 4px;
    flex-shrink: 0;
  }

  .m-sheet-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
    flex: 1 1 auto;
  }

  .m-sheet-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 16px 12px;
    border-bottom: 1px solid var(--ms-border);
    flex-shrink: 0;
  }

  .m-sheet-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--ms-ink);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .m-sheet-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }

  .m-sheet-overview {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    min-height: 32px;
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--mtb-active-pill);
    color: var(--ms-accent);
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
    white-space: nowrap;
  }

  .m-sheet-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 0;
    background: var(--ms-surface);
    color: var(--ms-muted);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .m-sheet-body {
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 10px 14px 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .m-sheet-section-label {
    margin-top: 8px;
    padding: 4px 4px 2px;
    font-size: 11.5px;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--ms-muted);
  }

  .m-sheet-row,
  .m-sheet-group {
    background: var(--ms-surface);
    border: 1px solid var(--ms-border);
    border-radius: 12px;
  }

  .m-sheet-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 46px;
    padding: 10px 14px;
    color: var(--ms-ink);
    font-size: 14px;
    font-weight: 600;
    text-decoration: none;
  }

  .m-sheet-row[aria-current="page"] {
    border-color: var(--ms-accent);
    color: var(--ms-accent-text);
    background: var(--mtb-active-pill);
  }

  .m-sheet-row-text {
    min-width: 0;
    flex: 1 1 auto;
  }

  .m-sheet-row-meta {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }

  .m-sheet-inline-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--ms-accent);
    color: #FFFFFF;
    font-size: 11px;
    font-weight: 700;
  }
  [data-theme="orbit"] .m-sheet-inline-badge {
    color: #04070E;
  }

  .m-sheet-chevron {
    color: var(--ms-muted);
    font-size: 18px;
    line-height: 1;
    flex-shrink: 0;
  }

  .m-sheet-group-head {
    display: flex;
    align-items: stretch;
    min-height: 46px;
  }

  .m-sheet-group-link {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    align-items: center;
    padding: 10px 10px 10px 14px;
    color: var(--ms-ink);
    font-size: 14px;
    font-weight: 600;
    text-decoration: none;
  }

  .m-sheet-group-link[aria-current="page"] {
    color: var(--ms-accent-text);
  }

  .m-sheet-acc-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-width: 56px;
    min-height: 44px;
    padding: 0 12px;
    border: 0;
    border-left: 1px solid var(--ms-border);
    background: transparent;
    color: var(--ms-muted);
    font: inherit;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .m-sheet-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 22px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--mtb-active-pill);
    color: var(--ms-accent);
    font-size: 11px;
    font-weight: 700;
  }

  .m-sheet-acc-icon {
    transition: transform 0.2s ease;
  }
  .m-sheet-acc-btn[aria-expanded="true"] .m-sheet-acc-icon {
    transform: rotate(180deg);
  }

  .m-sheet-l3 {
    list-style: none;
    margin: 0;
    padding: 4px 10px 10px 14px;
    border-top: 1px solid var(--ms-border);
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .m-sheet-l3 a {
    display: flex;
    align-items: center;
    min-height: 40px;
    padding: 7px 10px;
    border-radius: 8px;
    color: var(--ms-muted);
    font-size: 13.5px;
    font-weight: 500;
    text-decoration: none;
  }

  .m-sheet-l3 li.m-sheet-l3-sub a {
    padding-left: 22px;
    font-size: 13px;
  }

  .m-sheet-l3 a[aria-current="page"] {
    background: var(--mtb-active-pill);
    color: var(--ms-accent-text);
    font-weight: 600;
  }
}

@media (min-width: 481px) and (max-width: 900px) {
  .ide-panel {
    bottom: calc(var(--mtb-height) + 14px + env(safe-area-inset-bottom, 0px));
    max-height: calc(100dvh - var(--mtb-height) - 32px - env(safe-area-inset-bottom, 0px));
  }
}

@media (max-width: 900px) and (prefers-reduced-motion: reduce) {
  .m-tabbar-item,
  .m-tabbar-item::before,
  .m-tabbar-icon,
  .m-sheet-backdrop,
  .m-sheet,
  .m-sheet-acc-icon {
    transition: none;
  }
}

@media (max-width: 900px) and (forced-colors: active) {
  .m-tabbar,
  .m-sheet {
    border-top: 1px solid CanvasText;
  }
  .m-tabbar-item.is-active,
  .m-tabbar-item[aria-current="page"],
  .m-sheet-row[aria-current="page"],
  .m-sheet-l3 a[aria-current="page"] {
    outline: 2px solid Highlight;
  }
}
