/**
 * Mobile Experience Layer — only ≤768px
 * Desktop untouched. Platform classes: html.mx-ios | html.mx-android
 */
:root {
  --mx-ease: cubic-bezier(0.22, 1, 0.36, 1);
  --mx-ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --mx-ms: 320ms;
  --mx-press: 120ms;
  --mx-blur: saturate(180%) blur(18px);
  --mx-sheet: rgba(255, 255, 255, 0.94);
  --mx-sheet-solid: #f7f7f8;
  --mx-nav: rgba(255, 255, 255, 0.88);
  --mx-ink: #0f172a;
  --mx-muted: #64748b;
  --mx-line: rgba(15, 23, 42, 0.08);
  --mx-accent: var(--brand-primary, #2563eb);
  --mx-danger: #dc2626;
  --mx-fab: 56px;
  --mx-radius: 20px;
  --mx-group: 14px;
  --mx-safe-b: env(safe-area-inset-bottom, 0px);
  --mx-safe-t: env(safe-area-inset-top, 0px);
  --mx-nav-offset: 64px;
  --mx-z-sheet: 12040;
  --mx-z-snack: 12060;
  --mx-z-fab: 12020;
  --mx-z-progress: 12010;
}

@media (prefers-color-scheme: dark) {
  :root {
    --mx-sheet: rgba(22, 24, 28, 0.94);
    --mx-sheet-solid: #16181c;
    --mx-nav: rgba(18, 20, 24, 0.9);
    --mx-ink: #f1f5f9;
    --mx-muted: #94a3b8;
    --mx-line: rgba(248, 250, 252, 0.1);
  }
}

html.mx-oled {
  --mx-sheet-solid: #000;
  --mx-sheet: rgba(0, 0, 0, 0.94);
  --mx-nav: rgba(0, 0, 0, 0.92);
}

html.mx-density-compact { --mx-fab: 48px; }
html.mx-density-comfortable { --mx-fab: 58px; }

@media (max-width: 768px) {
  body.mx-sheet-open {
    overflow: hidden;
    touch-action: none;
  }

  .mx-scroll-progress {
    position: fixed;
    top: 0;
    left: 0;
    height: 2px;
    width: 0;
    z-index: var(--mx-z-progress);
    background: linear-gradient(90deg, var(--mx-accent), #a855f7);
    pointer-events: none;
  }

  .mx-large-title {
    position: sticky;
    top: 0;
    z-index: 40;
    padding: 10px 16px 12px;
    padding-top: max(10px, var(--mx-safe-t));
    background: var(--mx-nav);
    border-bottom: 1px solid transparent;
    transition: padding var(--mx-ms) var(--mx-ease);
  }

  html.mx-ios.mx-blur-ok .mx-large-title,
  html.mx-blur-ok .mx-large-title {
    -webkit-backdrop-filter: var(--mx-blur);
    backdrop-filter: var(--mx-blur);
  }

  html.mx-reduce-transparency .mx-large-title,
  html.mx-blur-off .mx-large-title {
    background: var(--mx-sheet-solid);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  .mx-large-title__row {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 36px;
  }

  .mx-large-title__back,
  .mx-large-title__action {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: var(--mx-accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .mx-large-title__grow { flex: 1; min-width: 0; }

  .mx-large-title__h {
    margin: 0;
    font-size: clamp(1.5rem, 6vw, 1.9rem);
    font-weight: 750;
    letter-spacing: -0.03em;
    line-height: 1.15;
    color: var(--mx-ink);
    transition: font-size 180ms var(--mx-ease);
  }

  .mx-large-title__sub {
    margin: 4px 0 0;
    font-size: 0.8125rem;
    color: var(--mx-muted);
    max-height: 2.5em;
    overflow: hidden;
    transition: opacity 160ms, max-height 160ms;
  }

  .mx-large-title.is-compact {
    border-bottom-color: var(--mx-line);
    padding-bottom: 8px;
  }

  .mx-large-title.is-compact .mx-large-title__h {
    font-size: 1.05rem;
    font-weight: 680;
  }

  .mx-large-title.is-compact .mx-large-title__sub {
    opacity: 0;
    max-height: 0;
    margin: 0;
  }

  .mx-grouped { margin: 12px 16px 20px; display: grid; gap: 18px; }
  .mx-grouped__label {
    margin: 0 12px 6px;
    font-size: 0.72rem;
    font-weight: 650;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--mx-muted);
  }
  .mx-grouped__card {
    background: var(--mx-sheet-solid);
    border-radius: var(--mx-group);
    overflow: hidden;
    border: 1px solid var(--mx-line);
  }
  .mx-grouped__row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 13px 14px;
    min-height: 48px;
    width: 100%;
    border: 0;
    background: transparent;
    color: inherit;
    text-decoration: none;
    text-align: left;
    position: relative;
  }
  .mx-grouped__row + .mx-grouped__row::before {
    content: "";
    position: absolute;
    left: 14px;
    right: 0;
    top: 0;
    height: 1px;
    background: var(--mx-line);
  }
  .mx-grouped__row-icon {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 0.85rem;
  }
  .mx-grouped__row-text { flex: 1; min-width: 0; }
  .mx-grouped__row-text strong {
    display: block;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--mx-ink);
  }
  .mx-grouped__row-text small { color: var(--mx-muted); font-size: 0.78rem; }
  .mx-grouped__row-value { color: var(--mx-muted); font-size: 0.88rem; }
  .mx-grouped__row-chevron { color: color-mix(in srgb, var(--mx-muted) 70%, transparent); font-size: 0.75rem; }
  .mx-grouped__card--danger .mx-grouped__row-text strong { color: var(--mx-danger); }

  .mx-press {
    -webkit-tap-highlight-color: transparent;
    transition: transform var(--mx-press) var(--mx-ease), opacity var(--mx-press);
    touch-action: manipulation;
  }
  .mx-press:active { transform: scale(0.975); opacity: 0.92; }

  html.mx-android .mx-ripple {
    position: relative;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
  }
  html.mx-android .mx-ripple::after {
    content: "";
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at var(--mx-rx, 50%) var(--mx-ry, 50%), rgba(255,255,255,.35), transparent 55%);
    opacity: 0;
    transform: scale(.2);
    transition: opacity 280ms var(--mx-ease), transform 280ms var(--mx-ease);
    pointer-events: none;
  }
  html.mx-android .mx-ripple.is-rippling::after {
    opacity: 1;
    transform: scale(1);
  }

  .mx-chip-row {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 8px 16px 10px;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .mx-chip-row::-webkit-scrollbar { display: none; }
  .mx-chip {
    flex: 0 0 auto;
    scroll-snap-align: start;
    border: 1px solid var(--mx-line);
    background: var(--mx-sheet-solid);
    color: var(--mx-ink);
    border-radius: 999px;
    padding: 8px 14px;
    font-size: 0.8125rem;
    font-weight: 600;
    min-height: 36px;
    white-space: nowrap;
  }
  .mx-chip.is-active {
    background: color-mix(in srgb, var(--mx-accent) 14%, transparent);
    border-color: color-mix(in srgb, var(--mx-accent) 45%, transparent);
    color: var(--mx-accent);
  }

  .mx-segmented {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 2px;
    margin: 8px 16px;
    padding: 3px;
    border-radius: 12px;
    background: color-mix(in srgb, var(--mx-muted) 12%, transparent);
  }
  .mx-segmented__btn {
    border: 0;
    border-radius: 10px;
    min-height: 36px;
    background: transparent;
    color: var(--mx-muted);
    font-size: 0.8125rem;
    font-weight: 650;
  }
  .mx-segmented__btn.is-active {
    background: var(--mx-sheet-solid);
    color: var(--mx-ink);
    box-shadow: 0 1px 2px rgba(0,0,0,.06);
  }

  .mx-snap-row {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    padding: 4px 16px 14px;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .mx-snap-row::-webkit-scrollbar { display: none; }
  .mx-snap-card {
    flex: 0 0 min(78vw, 280px);
    scroll-snap-align: center;
    border-radius: 16px;
    border: 1px solid var(--mx-line);
    background: var(--mx-sheet-solid);
    padding: 14px 16px;
    min-height: 108px;
  }
  .mx-snap-card__label {
    font-size: 0.75rem;
    color: var(--mx-muted);
    font-weight: 650;
    text-transform: uppercase;
    letter-spacing: .03em;
  }
  .mx-snap-card__value {
    margin-top: 6px;
    font-size: 1.55rem;
    font-weight: 750;
    letter-spacing: -.03em;
    color: var(--mx-ink);
  }
  .mx-snap-card__hint { margin-top: 4px; font-size: .8rem; color: var(--mx-muted); }
  .mx-snap-dots { display: flex; justify-content: center; gap: 6px; padding-bottom: 8px; }
  .mx-snap-dots span {
    width: 6px; height: 6px; border-radius: 99px;
    background: color-mix(in srgb, var(--mx-muted) 35%, transparent);
  }
  .mx-snap-dots span.is-active { width: 16px; background: var(--mx-accent); }

  .mx-fab {
    position: fixed;
    right: max(16px, env(safe-area-inset-right));
    bottom: calc(var(--mx-nav-offset) + 12px);
    z-index: var(--mx-z-fab);
    min-width: var(--mx-fab);
    height: var(--mx-fab);
    padding: 0 18px;
    border: 0;
    border-radius: 999px;
    background: var(--mx-accent);
    color: #fff;
    font-weight: 700;
    font-size: .9rem;
    display: none;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 8px 24px color-mix(in srgb, var(--mx-accent) 40%, transparent);
    transition: transform 200ms var(--mx-ease), opacity 200ms;
  }
  .mx-fab.is-visible { display: inline-flex; }
  .mx-fab.is-mini { width: var(--mx-fab); padding: 0; }
  .mx-fab.is-mini .mx-fab__label { display: none; }
  html.mx-onehand-left .mx-fab {
    right: auto;
    left: max(16px, env(safe-area-inset-left));
  }
  html.mx-android .mx-fab { border-radius: 16px; }
  body.m-app-keyboard-open .mx-fab,
  html.vp-keyboard-open .mx-fab {
    opacity: 0;
    pointer-events: none;
    transform: translateY(12px);
  }

  .mx-sheet-root {
    position: fixed;
    inset: 0;
    z-index: var(--mx-z-sheet);
    pointer-events: none;
    visibility: hidden;
  }
  .mx-sheet-root.is-open {
    pointer-events: auto;
    visibility: visible;
  }
  .mx-sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(2, 6, 23, .45);
    opacity: 0;
    transition: opacity var(--mx-ms) var(--mx-ease);
  }
  .mx-sheet-root.is-open .mx-sheet-backdrop { opacity: 1; }
  .mx-sheet {
    position: absolute;
    left: 0; right: 0; bottom: 0;
    max-height: min(92dvh, 920px);
    background: var(--mx-sheet);
    border-radius: var(--mx-radius) var(--mx-radius) 0 0;
    transform: translateY(108%);
    transition: transform var(--mx-ms) var(--mx-ease-out);
    padding-bottom: max(12px, var(--mx-safe-b));
    display: flex;
    flex-direction: column;
    border: 1px solid var(--mx-line);
    border-bottom: 0;
  }
  html.mx-blur-ok .mx-sheet {
    -webkit-backdrop-filter: var(--mx-blur);
    backdrop-filter: var(--mx-blur);
  }
  html.mx-reduce-transparency .mx-sheet,
  html.mx-blur-off .mx-sheet {
    background: var(--mx-sheet-solid);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .mx-sheet-root.is-open .mx-sheet { transform: translateY(0); }
  .mx-sheet-root.mx-sheet--short .mx-sheet { max-height: 46dvh; }
  .mx-sheet-root.mx-sheet--full .mx-sheet { max-height: 96dvh; }
  .mx-sheet__grab {
    width: 36px; height: 4px; border-radius: 99px;
    background: color-mix(in srgb, var(--mx-muted) 40%, transparent);
    margin: 10px auto 6px;
  }
  .mx-sheet__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 4px 16px 10px;
  }
  .mx-sheet__title {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 720;
    color: var(--mx-ink);
  }
  .mx-sheet__close {
    width: 40px; height: 40px; border: 0; border-radius: 12px;
    background: transparent; color: var(--mx-muted);
  }
  .mx-sheet__body {
    overflow: auto;
    padding: 4px 16px 16px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .mx-action-list {
    display: grid;
    border-radius: 14px;
    overflow: hidden;
    background: var(--mx-sheet-solid);
    border: 1px solid var(--mx-line);
  }
  .mx-action-list button {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 52px;
    padding: 12px 14px;
    border: 0;
    background: transparent;
    color: var(--mx-ink);
    font-size: .95rem;
    font-weight: 600;
    text-align: left;
    border-top: 1px solid var(--mx-line);
  }
  .mx-action-list button:first-child { border-top: 0; }
  .mx-action-list button i { width: 22px; color: var(--mx-accent); text-align: center; }
  .mx-action-list button.is-danger,
  .mx-action-list button.is-danger i { color: var(--mx-danger); }
  .mx-action-cancel {
    margin-top: 10px;
    width: 100%;
    min-height: 48px;
    border-radius: 14px;
    border: 1px solid var(--mx-line);
    background: var(--mx-sheet-solid);
    font-weight: 700;
    color: var(--mx-accent);
  }

  .mx-qa-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
  }
  .mx-qa-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 14px 8px;
    border-radius: 16px;
    border: 1px solid var(--mx-line);
    background: var(--mx-sheet-solid);
    color: var(--mx-ink);
    text-decoration: none;
    min-height: 92px;
  }
  .mx-qa-item__icon {
    width: 42px; height: 42px; border-radius: 12px;
    display: inline-flex; align-items: center; justify-content: center;
    background: color-mix(in srgb, var(--mx-accent) 12%, transparent);
    color: var(--mx-accent);
    font-size: 1.05rem;
  }
  .mx-qa-item__label {
    font-size: .75rem;
    font-weight: 650;
    text-align: center;
    line-height: 1.25;
  }

  .mx-snack-host {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: calc(var(--mx-nav-offset) + 10px);
    z-index: var(--mx-z-snack);
    display: grid;
    gap: 8px;
    pointer-events: none;
  }
  .mx-snack {
    pointer-events: auto;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    border-radius: 14px;
    background: #0f172a;
    color: #f8fafc;
    box-shadow: 0 10px 30px rgba(0,0,0,.28);
    transform: translateY(16px);
    opacity: 0;
    transition: transform 220ms var(--mx-ease), opacity 220ms;
  }
  .mx-snack.is-in { transform: translateY(0); opacity: 1; }
  .mx-snack__msg { flex: 1; font-size: .875rem; font-weight: 550; }
  .mx-snack__undo {
    border: 0; background: transparent; color: #93c5fd;
    font-weight: 750; font-size: .85rem; min-height: 36px; padding: 0 6px;
  }

  .mx-empty { text-align: center; padding: 36px 24px; }
  .mx-empty__icon {
    width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 20px;
    display: flex; align-items: center; justify-content: center;
    background: color-mix(in srgb, var(--mx-accent) 10%, transparent);
    color: var(--mx-accent); font-size: 1.4rem;
  }
  .mx-empty__title { margin: 0; font-size: 1.1rem; font-weight: 720; color: var(--mx-ink); }
  .mx-empty__text { margin: 8px 0 18px; color: var(--mx-muted); font-size: .9rem; line-height: 1.45; }
  .mx-empty__actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

  html.mx-onehand .mx-thumb-actions {
    position: sticky;
    bottom: calc(var(--mx-nav-offset) + 8px);
    z-index: 30;
    padding-bottom: max(8px, var(--mx-safe-b));
  }

  .mx-skel {
    border-radius: 12px;
    background: linear-gradient(90deg,
      color-mix(in srgb, var(--mx-muted) 12%, transparent),
      color-mix(in srgb, var(--mx-muted) 22%, transparent),
      color-mix(in srgb, var(--mx-muted) 12%, transparent));
    background-size: 200% 100%;
    animation: mx-skel 1.2s ease-in-out infinite;
  }
  @keyframes mx-skel {
    0% { background-position: 100% 0; }
    100% { background-position: -100% 0; }
  }

  /* Admin/panel: sync nav offset if present */
  html.has-admin-mnav { --mx-nav-offset: calc(58px + env(safe-area-inset-bottom, 0px)); }
  body.panel-has-mobile-nav { --mx-nav-offset: calc(64px + env(safe-area-inset-bottom, 0px)); }
}

@media (max-width: 768px) and (prefers-reduced-motion: reduce) {
  .mx-press:active, .mx-sheet, .mx-sheet-backdrop, .mx-snack, .mx-large-title__h, .mx-fab, .mx-skel {
    transition: none !important;
    animation: none !important;
  }
}

@media (min-width: 769px) {
  .mx-sheet-root,
  .mx-snack-host,
  .mx-fab,
  .mx-scroll-progress { display: none !important; }
}
