/* Mobile experience pass ---------------------------------------------------
   Loaded after the core and glass styles so phone-specific behavior remains
   predictable without changing desktop layouts. */

html,
body {
    max-width: 100%;
    overflow-x: clip;
}

body.sidebar-open {
    overflow: hidden;
}

.mobile-bottom-nav {
    display: none;
}

@supports not (overflow: clip) {
    html,
    body {
        overflow-x: hidden;
    }
}

@media (max-width: 900px) {
    .sidebar {
        z-index: 60;
        width: min(88vw, 320px);
        max-width: 320px;
        overflow-x: hidden;
        overflow-y: auto;
        padding-top: calc(18px + env(safe-area-inset-top));
        padding-bottom: calc(18px + env(safe-area-inset-bottom));
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y;
    }

    .mobile-topbar {
        min-height: calc(62px + env(safe-area-inset-top));
        padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
    }

    .sidebar-overlay {
        z-index: 50;
    }
}

@media (max-width: 760px) {
    :root {
        --mobile-page-gutter: 16px;
    }

    body {
        -webkit-text-size-adjust: 100%;
    }

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

    .page-container {
        width: 100%;
        max-width: none;
        padding: 24px var(--mobile-page-gutter)
            calc(92px + env(safe-area-inset-bottom));
    }

    .mobile-topbar {
        gap: 10px;
        padding-inline: var(--mobile-page-gutter);
    }

    .mobile-topbar .brand {
        min-width: 0;
    }

    .mobile-topbar .brand-text {
        overflow: hidden;
        font-size: .92rem;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .mobile-menu-button,
    .mobile-theme-button,
    .dialog-close,
    .icon-button {
        min-width: 44px;
        min-height: 44px;
    }

    .page-heading {
        gap: 16px;
        margin-bottom: 22px;
    }

    .page-heading h1 {
        font-size: clamp(2rem, 11vw, 3rem);
        line-height: 1;
    }

    .page-heading p {
        font-size: .94rem;
        line-height: 1.55;
    }

    .page-heading .actions,
    .section-heading .actions {
        display: grid;
        width: 100%;
        grid-template-columns: 1fr;
    }

    .page-heading .actions > *,
    .section-heading .actions > * {
        justify-content: center;
        width: 100%;
    }

    .section {
        margin-top: 34px;
    }

    .section-heading {
        align-items: flex-start;
        flex-direction: column;
        gap: 14px;
    }

    .section-heading h2 {
        font-size: clamp(1.55rem, 8vw, 2.15rem);
    }

    /* Dashboard ----------------------------------------------------------- */
    .dashboard-hero {
        gap: 14px;
    }

    .dashboard-welcome {
        padding: 24px 20px 26px;
        border-radius: 20px 20px 20px 8px;
    }

    .dashboard-welcome h1 {
        max-width: 12ch;
        font-size: clamp(2.25rem, 12vw, 3.5rem);
        line-height: .98;
    }

    .dashboard-welcome p {
        font-size: .91rem;
        line-height: 1.55;
    }

    .progress-panel {
        min-height: 170px;
        padding: 20px;
    }

    .progress-panel__value {
        margin-top: 12px;
        font-size: 3rem;
    }

    .metric-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 10px;
        margin-top: 14px;
    }

    .metric {
        min-width: 0;
        padding: 15px;
    }

    .metric__label {
        font-size: .66rem;
        letter-spacing: .04em;
    }

    .metric__value {
        font-size: 1.9rem;
    }

    .assignment-row {
        grid-template-columns: 1fr;
        padding: 16px;
    }

    .assignment-row form,
    .assignment-row .complete-button {
        width: 100%;
    }

    .assignment-row .complete-button {
        justify-content: center;
    }

    .week-board-wrap {
        margin-inline: calc(var(--mobile-page-gutter) * -1);
        padding: 2px var(--mobile-page-gutter) 14px;
        overflow-x: auto;
        scroll-padding-inline: var(--mobile-page-gutter);
        scroll-snap-type: x mandatory;
        overscroll-behavior-inline: contain;
        -webkit-overflow-scrolling: touch;
    }

    .week-board {
        display: flex;
        width: max-content;
        min-width: 0;
        gap: 12px;
    }

    .day-column {
        width: min(84vw, 330px);
        min-height: 270px;
        flex: 0 0 min(84vw, 330px);
        scroll-snap-align: start;
        scroll-snap-stop: always;
    }

    /* Forms --------------------------------------------------------------- */
    .auth-card,
    .form-card,
    .card {
        max-width: 100%;
    }

    .form-card,
    .auth-card {
        padding: 22px 18px;
    }

    .form-field input,
    .form-field select,
    .form-field textarea,
    .filter-control,
    input,
    select,
    textarea {
        font-size: 16px;
    }

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

    .form-actions > * {
        justify-content: center;
        width: 100%;
    }

    /* Calendar ------------------------------------------------------------ */
    .calendar-shell {
        width: 100%;
        min-width: 0;
        padding: 8px;
        overflow-x: hidden;
        border-radius: 14px;
    }

    #calendar {
        width: 100%;
        min-width: 0 !important;
    }

    .calendar-tools {
        align-items: stretch;
        flex-direction: column;
        gap: 10px;
        padding: 12px;
    }

    .calendar-tools__hint {
        font-size: .77rem;
        line-height: 1.45;
    }

    .calendar-increment {
        justify-content: space-between;
        width: 100%;
    }

    .calendar-increment select {
        min-height: 44px;
    }

    .calendar-legend {
        flex-wrap: nowrap;
        margin-inline: -8px;
        padding-inline: 12px;
        overflow-x: auto;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }

    .calendar-legend::-webkit-scrollbar {
        display: none;
    }

    .legend-item {
        flex: 0 0 auto;
        white-space: nowrap;
    }

    #calendar .fc-header-toolbar {
        display: grid;
        grid-template-columns: auto minmax(0, 1fr) auto;
        gap: 8px;
        align-items: center;
        margin-bottom: 12px;
    }

    #calendar .fc-toolbar-chunk {
        display: flex;
        min-width: 0;
        align-items: center;
    }

    #calendar .fc-toolbar-chunk:nth-child(2) {
        justify-content: center;
        text-align: center;
    }

    #calendar .fc-toolbar-title {
        overflow: hidden;
        font-size: 1rem;
        line-height: 1.15;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    #calendar .fc-button {
        min-width: 42px;
        min-height: 42px;
        padding: 7px 10px;
        font-size: .72rem;
    }

    #calendar .fc-prev-button,
    #calendar .fc-next-button {
        width: 42px;
        padding-inline: 0;
    }

    #calendar .fc-timegrid-axis {
        width: 48px;
    }

    #calendar .fc-timegrid-axis-cushion,
    #calendar .fc-timegrid-slot-label-cushion {
        padding-inline: 4px;
        font-size: .68rem;
    }

    #calendar .fc-col-header-cell-cushion {
        padding: 8px 4px;
        font-size: .74rem;
    }

    #calendar .fc-timegrid-slot {
        height: 2.65em;
    }

    #calendar .fc-event {
        min-height: 28px;
        border-radius: 7px;
        font-size: .72rem;
    }

    #calendar .fc-event-main {
        padding: 2px 3px;
    }

    .shared-calendar-layout {
        grid-template-columns: 1fr;
        gap: 14px;
    }

    .shared-calendar-sidebar {
        position: static;
        grid-template-columns: 1fr;
    }

    .shared-calendar-panel {
        padding: 16px;
    }

    .shared-calendar-link {
        min-height: 58px;
        padding: 11px;
    }

    .invitation-card {
        align-items: stretch;
        flex-direction: column;
    }

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

    .invitation-card .actions > *,
    .invitation-card .actions button {
        width: 100%;
    }

    /* Dialogs become phone-friendly bottom sheets. ----------------------- */
    dialog.assignment-dialog,
    dialog.block-dialog {
        position: fixed;
        inset: auto 0 0;
        width: 100vw !important;
        max-width: none !important;
        max-height: calc(100dvh - max(8px, env(safe-area-inset-top))) !important;
        margin: 0 !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        border-right: 0;
        border-bottom: 0;
        border-left: 0;
        border-radius: 24px 24px 0 0;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    dialog.assignment-dialog::backdrop,
    dialog.block-dialog::backdrop {
        background: rgb(2 8 18 / 70%);
    }

    .dialog-heading {
        position: sticky;
        z-index: 5;
        top: 0;
        padding: 18px;
        background: var(--surface);
    }

    .dialog-heading h2 {
        font-size: 1.65rem;
    }

    .dialog-content {
        width: 100%;
        min-width: 0;
        padding: 18px 18px calc(20px + env(safe-area-inset-bottom));
        overflow-x: hidden;
    }

    .dialog-details {
        grid-template-columns: 1fr;
    }

    .item-kind-picker {
        grid-template-columns: 1fr;
    }

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

    .dialog-actions,
    .block-dialog__actions {
        display: grid !important;
        width: 100%;
        min-width: 0;
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .dialog-actions__spacer {
        display: none;
    }

    .series-delete-actions,
    .block-dialog__delete-actions,
    .block-dialog__save-actions {
        display: grid !important;
        width: 100%;
        min-width: 0;
        grid-template-columns: 1fr;
        gap: 12px !important;
    }

    .series-delete-actions > *,
    .block-dialog__delete-actions > *,
    .block-dialog__save-actions > *,
    .dialog-actions > * {
        width: 100% !important;
        min-width: 0 !important;
        justify-content: center;
        white-space: normal;
    }

    .series-delete-actions .danger-button,
    .block-dialog__delete-actions .danger-button {
        color: #fff !important;
        background: #8f1d2c !important;
        border-color: #a92335 !important;
    }

    html[data-theme="dark"] .series-delete-actions .danger-button,
    html[data-theme="dark"] .block-dialog__delete-actions .danger-button {
        color: #fff !important;
        background: #8f1d2c !important;
        border-color: #b32d43 !important;
    }

    /* Owner workspace and notification cards ----------------------------- */
    .owner-toolbar,
    .owner-notification-form,
    .upcoming-notification-grid,
    .notification-layout {
        grid-template-columns: 1fr !important;
    }

    .owner-notification-form select[multiple] {
        min-height: 180px;
    }

    .table-wrapper {
        overflow-x: auto;
        overscroll-behavior-inline: contain;
        -webkit-overflow-scrolling: touch;
    }

    .data-table {
        min-width: 720px;
    }

    .notification-card {
        grid-template-columns: 42px minmax(0, 1fr);
        gap: 12px;
        padding: 16px;
    }

    .notification-card > form {
        grid-column: 2;
        justify-self: end;
    }

    .notification-card__meta {
        align-items: flex-start;
        flex-direction: column;
        gap: 3px;
    }

    .upcoming-list__item {
        grid-template-columns: 1fr;
    }

    .upcoming-list__item time {
        text-align: left;
    }

    .study-tool-card__body {
        padding: 18px;
    }

    .study-tool-card__footer {
        align-items: stretch;
        flex-direction: column;
    }

    .study-tool-card__footer .button {
        justify-content: center;
        width: 100%;
    }

    /* Thumb-friendly bottom navigation. ---------------------------------- */
    .mobile-bottom-nav {
        position: fixed;
        z-index: 38;
        right: 10px;
        bottom: calc(8px + env(safe-area-inset-bottom));
        left: 10px;
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 4px;
        padding: 7px;
        background: color-mix(in srgb, var(--surface) 92%, transparent);
        border: 1px solid var(--ink-200);
        border-radius: 18px;
        box-shadow: 0 18px 48px rgb(2 8 18 / 24%);
        backdrop-filter: blur(18px) saturate(145%);
        -webkit-backdrop-filter: blur(18px) saturate(145%);
    }

    .mobile-bottom-nav__link,
    .mobile-bottom-nav__button {
        display: grid;
        min-width: 0;
        min-height: 54px;
        place-items: center;
        gap: 3px;
        padding: 7px 4px;
        color: var(--ink-500);
        background: transparent;
        border: 0;
        border-radius: 13px;
        font: inherit;
        font-size: .67rem;
        font-weight: 750;
        text-decoration: none;
        cursor: pointer;
    }

    .mobile-bottom-nav__link .icon,
    .mobile-bottom-nav__button .icon {
        width: 21px;
        height: 21px;
    }

    .mobile-bottom-nav__link[aria-current="page"] {
        color: var(--accent);
        background: var(--accent-soft);
    }

    .mobile-bottom-nav__item {
        position: relative;
    }

    .mobile-bottom-nav__badge {
        position: absolute;
        top: 4px;
        left: calc(50% + 7px);
        display: grid;
        min-width: 17px;
        height: 17px;
        place-items: center;
        padding-inline: 4px;
        color: #fff;
        background: #b42338;
        border: 2px solid var(--surface);
        border-radius: 999px;
        font-size: .58rem;
        font-weight: 800;
    }
}

@media (max-width: 420px) {
    :root {
        --mobile-page-gutter: 13px;
    }

    .mobile-topbar .brand-text {
        display: none;
    }

    .dashboard-welcome h1 {
        font-size: clamp(2rem, 13vw, 3rem);
    }

    .metric {
        padding: 13px;
    }

    .metric__label {
        font-size: .61rem;
    }

    .day-column {
        width: 88vw;
        flex-basis: 88vw;
    }

    #calendar .fc-toolbar-title {
        font-size: .9rem;
    }

    #calendar .fc-button {
        min-width: 40px;
        min-height: 40px;
        padding-inline: 8px;
    }

    .weekday-picker {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .invitation-card .actions {
        grid-template-columns: 1fr;
    }

    .mobile-bottom-nav {
        right: 7px;
        left: 7px;
    }

    .mobile-bottom-nav__link,
    .mobile-bottom-nav__button {
        font-size: .62rem;
    }
}

@media (hover: none) and (pointer: coarse) {
    .button,
    .secondary-button,
    .danger-button,
    .ghost-button,
    .complete-button,
    .table-action,
    .sidebar__link,
    .sidebar__logout {
        min-height: 44px;
    }

    #calendar .fc-event {
        -webkit-tap-highlight-color: transparent;
    }
}
