/*
 * OSN LabBook mobile experience
 * Loaded after page-specific styles so every workspace uses one responsive shell.
 */

.public-mobile-toggle,
.sidebar-mobile-toggle {
    display: none;
}

@media (max-width: 820px) {
    html {
        overflow-x: hidden;
        scroll-padding-top: 5rem;
    }

    body {
        min-width: 0;
        overflow-x: hidden;
        -webkit-text-size-adjust: 100%;
    }

    button,
    .btn,
    input,
    select,
    textarea {
        font-size: 16px;
    }

    button,
    .btn,
    a,
    summary,
    input[type="checkbox"],
    input[type="radio"] {
        touch-action: manipulation;
    }

    .app-container {
        display: block !important;
        width: 100%;
        min-height: 100dvh;
        overflow-x: clip;
    }

    .sidebar {
        position: sticky !important;
        z-index: 80;
        top: 0;
        display: grid !important;
        width: 100% !important;
        min-width: 0 !important;
        min-height: auto !important;
        max-height: 100dvh;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center;
        padding: max(0.55rem, env(safe-area-inset-top)) 0.75rem 0.55rem !important;
        overflow-y: auto;
        border-right: 0 !important;
        border-bottom: 1px solid var(--osn-line, #dbe5e1) !important;
        background: rgba(255, 255, 255, 0.97) !important;
        box-shadow: 0 8px 24px rgba(16, 35, 31, 0.08);
        backdrop-filter: blur(18px);
    }

    .readonly-mode .sidebar {
        display: none !important;
    }

    .sidebar h2 {
        min-width: 0;
        margin: 0 !important;
        padding: 0 !important;
    }

    .app-brand-logo {
        width: 74px !important;
        max-height: 42px !important;
    }

    .sidebar-mobile-toggle {
        display: inline-flex;
        min-height: 2.75rem;
        max-width: min(64vw, 17rem);
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        padding: 0.55rem 0.75rem;
        border: 1px solid var(--osn-line-strong, #c6d6d0);
        border-radius: 10px;
        background: #fff;
        color: var(--osn-ink-soft, #30483f);
        font: inherit;
        font-size: 0.82rem;
        font-weight: 750;
        line-height: 1.15;
        cursor: pointer;
    }

    .sidebar-mobile-toggle::before {
        content: "☰";
        color: var(--osn-brand, #0f766e);
        font-size: 1rem;
    }

    .sidebar.mobile-open .sidebar-mobile-toggle::before {
        content: "×";
        font-size: 1.25rem;
    }

    .sidebar-mobile-current {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .sidebar nav,
    .sidebar nav[data-compact="true"],
    .sidebar .account-panel {
        display: none !important;
    }

    .sidebar.mobile-open nav,
    .sidebar.mobile-open nav[data-compact="true"] {
        display: grid !important;
        grid-column: 1 / -1;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        width: 100%;
        gap: 0.4rem;
        margin-top: 0.65rem;
        padding: 0.65rem 0 0 !important;
        overflow: visible !important;
        border-top: 1px solid var(--osn-line, #dbe5e1);
    }

    .sidebar.mobile-open nav > a,
    .sidebar.mobile-open .sidebar-more {
        min-width: 0;
        margin: 0 !important;
    }

    .sidebar.mobile-open nav > a,
    .sidebar.mobile-open .sidebar-more summary {
        display: flex;
        min-height: 2.85rem;
        align-items: center;
        justify-content: center;
        padding: 0.62rem 0.55rem !important;
        border: 1px solid transparent;
        border-radius: 10px;
        white-space: normal !important;
        text-align: center;
    }

    .sidebar.mobile-open nav > a.active {
        border-color: #b8d8d0;
        background: var(--osn-brand-soft, #e9f6f3);
        box-shadow: none !important;
    }

    .sidebar-more {
        grid-column: 1 / -1;
        border-top: 0 !important;
        padding: 0 !important;
    }

    .sidebar-more-links {
        display: grid !important;
        width: 100% !important;
        max-width: none !important;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.35rem !important;
        padding: 0.4rem 0 0 !important;
        overflow: visible !important;
    }

    .sidebar-more-links a {
        min-width: 0;
        white-space: normal !important;
        text-align: center;
    }

    .sidebar.mobile-open .account-panel {
        display: grid !important;
        grid-column: 1 / -1;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center;
        gap: 0.5rem;
        margin-top: 0.55rem !important;
        padding-top: 0.55rem !important;
    }

    .sidebar .account-user {
        min-width: 0;
        margin: 0 !important;
    }

    .sidebar .account-user span,
    .sidebar .account-user strong {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .sidebar .account-actions {
        display: flex !important;
        gap: 0.35rem;
    }

    .sidebar .account-actions .btn {
        min-height: 2.55rem;
        padding: 0.5rem 0.65rem;
        font-size: 0.78rem;
        white-space: nowrap;
    }

    .content,
    .chat-page {
        width: 100% !important;
        min-width: 0 !important;
        padding-right: max(0.8rem, env(safe-area-inset-right)) !important;
        padding-bottom: max(2rem, env(safe-area-inset-bottom)) !important;
        padding-left: max(0.8rem, env(safe-area-inset-left)) !important;
    }

    .content {
        padding-top: 1rem !important;
    }

    .content > h1,
    .content header h1 {
        max-width: 100%;
        font-size: clamp(1.65rem, 8vw, 2.1rem) !important;
        line-height: 1.15;
        overflow-wrap: anywhere;
    }

    .content section,
    .content article,
    .content .card,
    .project-card,
    .log-entry,
    .protocol-card,
    .inventory-card {
        min-width: 0;
        max-width: 100%;
        overflow-wrap: anywhere;
    }

    .stats,
    .stats-grid,
    .usage-grid,
    .account-settings-grid,
    .encryption-grid,
    .intelligence-form,
    .intelligence-results-grid,
    .osn-stats,
    .packet-asset-grid,
    .form-grid,
    .analysis-grid,
    .analysis-columns,
    .materials-grid {
        grid-template-columns: 1fr !important;
    }

    .stats {
        display: grid !important;
        gap: 0.75rem;
        margin-bottom: 1rem;
    }

    .content table {
        width: 100%;
        min-width: 36rem;
    }

    .content .table-wrap,
    .content .table-scroll,
    .content section:has(> table),
    .content article:has(> table) {
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .project-tabs .tabs,
    .editor-toolbar,
    .doc-toolbar,
    .toolbar,
    .format-toolbar {
        max-width: 100%;
        flex-wrap: nowrap !important;
        overflow-x: auto;
        overscroll-behavior-x: contain;
        scrollbar-width: thin;
        -webkit-overflow-scrolling: touch;
    }

    .project-tabs .tabs > *,
    .editor-toolbar > *,
    .doc-toolbar > *,
    .toolbar > *,
    .format-toolbar > * {
        flex: 0 0 auto;
    }

    .project-detail-header,
    .project-url-tools,
    .intelligence-header,
    .osn-hero,
    .page-header,
    .materials-header,
    .analysis-header,
    .account-header,
    .settings-heading,
    .billing-account-detail,
    .billing-page-header,
    .section-heading,
    .plan-heading,
    .credit-band-heading,
    .lab-pilot-inner {
        display: grid !important;
        grid-template-columns: 1fr !important;
        align-items: stretch !important;
        gap: 0.75rem !important;
    }

    .project-url-tools .btn,
    .detail-actions .btn,
    .form-actions .btn,
    .billing-actions .btn,
    .settings-form > .btn {
        width: 100%;
    }

    .detail-list-item {
        grid-template-columns: 1fr !important;
    }

    .detail-list-item small {
        grid-column: 1 !important;
    }

    .osn-row,
    .material-usage-item,
    .ledger-row {
        align-items: stretch !important;
        flex-direction: column;
    }

    .osn-phase-path {
        grid-template-columns: 1fr !important;
    }

    .osn-phase {
        min-height: auto !important;
        border-right: 0 !important;
        border-bottom: 1px solid #e2e8f0;
    }

    .osn-phase:last-child {
        border-bottom: 0;
    }

    .notes-canvas {
        min-height: 48dvh !important;
        padding: 1rem !important;
    }

    .osn-modal-overlay {
        align-items: end;
        padding: 0;
    }

    .osn-modal {
        width: 100%;
        max-height: 88dvh;
        overflow-y: auto;
        border-radius: 18px 18px 0 0;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .osn-modal-actions {
        position: sticky;
        bottom: 0;
        flex-direction: column-reverse;
        padding: 0.75rem 1rem max(1rem, env(safe-area-inset-bottom));
        background: #fff;
    }

    .osn-modal-actions .btn {
        width: 100%;
    }

    .chat-page {
        min-height: calc(100dvh - 4.3rem) !important;
        padding: 0 !important;
    }

    .chat-header {
        min-height: 0;
        padding: 0.85rem;
    }

    .chat-header,
    .chat-model-controls {
        display: grid !important;
        grid-template-columns: 1fr !important;
        align-items: stretch !important;
    }

    .chat-workspace {
        display: flex !important;
        min-height: 0;
        flex-direction: column;
    }

    .conversation-panel {
        max-height: 15rem;
        border-right: 0 !important;
        border-bottom: 1px solid #dce3ec;
    }

    .chat-thread {
        min-height: 62dvh !important;
    }

    .chat-messages {
        min-height: 18rem;
        padding: 1rem 0.8rem;
    }

    .chat-message {
        max-width: 94% !important;
    }

    .chat-composer {
        position: sticky;
        z-index: 10;
        bottom: 0;
        padding: 0.75rem 0.8rem max(0.8rem, env(safe-area-inset-bottom));
    }

    .composer-footer {
        align-items: stretch;
        flex-direction: column;
        gap: 0.5rem;
    }

    .composer-footer .btn {
        width: 100%;
    }

    .context-panel {
        border-left: 0 !important;
        border-top: 1px solid #dce3ec;
    }

    .literature-header,
    .literature-controls {
        align-items: stretch !important;
    }

    .literature-controls > *,
    .literature-controls label,
    .literature-controls select,
    .literature-controls .btn {
        width: 100%;
    }

    .literature-controls .icon-control {
        width: 2.75rem;
    }

    .literature-track {
        grid-auto-columns: minmax(16rem, calc(100vw - 3.2rem)) !important;
    }
}

@media (max-width: 760px) {
    .header {
        position: sticky !important;
        top: 0;
    }

    .header .container {
        display: grid !important;
        min-height: 0;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center !important;
        gap: 0.55rem;
        padding: max(0.5rem, env(safe-area-inset-top)) 0 0.5rem;
    }

    .header .logo {
        min-width: 0;
        margin: 0;
    }

    .header-brand-logo {
        width: 82px !important;
        max-height: 46px;
    }

    .public-mobile-toggle {
        display: inline-flex;
        min-height: 2.75rem;
        align-items: center;
        justify-content: center;
        gap: 0.45rem;
        padding: 0.55rem 0.75rem;
        border: 1px solid var(--osn-line-strong, #c6d6d0);
        border-radius: 10px;
        background: #fff;
        color: var(--osn-ink-soft, #30483f);
        font: inherit;
        font-size: 0.82rem;
        font-weight: 750;
        cursor: pointer;
    }

    .public-mobile-toggle::before {
        content: "☰";
        color: var(--osn-brand, #0f766e);
        font-size: 1rem;
    }

    .header.mobile-open .public-mobile-toggle::before {
        content: "×";
        font-size: 1.25rem;
    }

    .navigation {
        display: none !important;
        grid-column: 1 / -1;
        width: 100%;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.35rem !important;
        padding: 0.55rem 0 0.2rem !important;
        overflow: visible !important;
        border-top: 1px solid var(--osn-line, #dbe5e1);
    }

    .header.mobile-open .navigation {
        display: grid !important;
    }

    .navigation a {
        min-width: 0;
        min-height: 2.85rem !important;
        justify-content: center;
        padding: 0.55rem 0.4rem !important;
        white-space: normal;
        text-align: center;
    }

    .public-language-switch {
        grid-column: 1 / -1;
        justify-self: start;
        margin: 0 0 0.2rem !important;
    }

    .home-account-link {
        gap: 0.35rem;
    }

    .home-account-text {
        max-width: 7rem;
    }

    .container {
        width: min(calc(100% - 2rem), 1180px);
    }

    .hero,
    .page-hero,
    .about-hero,
    .pricing-intro {
        padding-top: 2.8rem !important;
        padding-bottom: 2.8rem !important;
    }

    .hero h2,
    html[lang="zh-CN"] .hero h2,
    .page-hero h2,
    .about-hero h2,
    .pricing-intro h2 {
        max-width: 100% !important;
        font-size: clamp(2rem, 10vw, 2.7rem) !important;
        line-height: 1.08 !important;
        letter-spacing: -0.035em !important;
        overflow-wrap: anywhere;
        word-break: normal !important;
    }

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

    .hero-actions .btn {
        width: 100%;
    }

    .preview-body {
        grid-template-columns: 1fr !important;
    }

    .preview-nav {
        display: flex !important;
        overflow-x: auto;
        border-right: 0 !important;
        border-bottom: 1px solid #e0e7e3;
        scrollbar-width: thin;
    }

    .preview-nav > * {
        flex: 0 0 auto;
    }

    .preview-timeline::before {
        display: none;
    }

    .pricing-grid,
    .pricing-grid.four-tier-grid,
    .plan-grid,
    .plan-grid.four-plans,
    .credit-pack-grid,
    .public-credit-grid,
    .payment-band-inner,
    .payment-method-list,
    .feature-list,
    .domain-grid,
    .about-hero-grid,
    .about-grid,
    .principle-grid,
    .pathway-grid {
        grid-template-columns: 1fr !important;
    }

    .plan {
        min-height: 0 !important;
    }

    .currency-selector {
        width: 100%;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }

    .payment-row {
        grid-template-columns: 2.8rem minmax(0, 1fr) !important;
    }

    .payment-row > :last-child {
        grid-column: 2;
        justify-self: start;
    }

    .auth-panel {
        min-height: calc(100dvh - 4.6rem) !important;
        padding-right: max(1rem, env(safe-area-inset-right)) !important;
        padding-bottom: max(1.5rem, env(safe-area-inset-bottom)) !important;
        padding-left: max(1rem, env(safe-area-inset-left)) !important;
    }

    .auth-card {
        padding: 1.25rem !important;
    }

    .auth-language-row {
        top: max(0.55rem, env(safe-area-inset-top)) !important;
        right: max(0.55rem, env(safe-area-inset-right)) !important;
    }

    .footer {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

@media (max-width: 420px) {
    .sidebar.mobile-open .account-panel {
        grid-template-columns: 1fr;
    }

    .sidebar .account-actions {
        width: 100%;
    }

    .sidebar .account-actions .btn {
        flex: 1 1 0;
    }

    .navigation,
    .sidebar.mobile-open nav,
    .sidebar-more-links {
        grid-template-columns: 1fr 1fr !important;
    }

    .chat-message {
        max-width: 98% !important;
    }

    .source-chip {
        max-width: calc(100vw - 4rem);
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }
}
