/* Phone and small-tablet layout for the board pages and the shared chrome.
   Every rule in this file sits inside a max-width media query, so nothing
   here can change the desktop layout. Colours come from the theme variables
   in styles.css, so both themes work.

   768px and below: touch-sized controls, the sideways-scrolling table, the
                    navbar and the overlay sidebar.
   640px and below: the phone layout (board header, toolbar, full-screen item
                    details, bottom-sheet pickers, docked bulk bar, agenda
                    calendar). */

@media (max-width: 768px) {
    /* ---------- App shell ---------- */

    /* dvh follows the mobile browser's collapsing address bar. */
    .app-container {
        height: calc(100dvh - 64px);
    }

    .sidebar {
        height: calc(100dvh - 64px);
        max-width: 86vw;
    }

    /* ---------- Navbar ---------- */

    .navbar-brand a,
    .nav-link {
        display: inline-flex;
        align-items: center;
        min-height: 40px;
    }

    .nav-link {
        padding: 0 4px;
    }

    .navbar-menu {
        gap: 16px;
    }

    .navbar-user {
        gap: 8px;
    }

    .theme-toggle,
    .notification-bell {
        min-width: 40px;
        min-height: 40px;
    }

    .user-avatar {
        width: 40px;
        height: 40px;
    }

    .user-dropdown-item {
        display: flex;
        align-items: center;
        min-height: 44px;
    }

    .notification-dropdown .btn-text {
        min-height: 40px;
    }

    /* Shown on hover on a desktop. A touch screen has no hover. */
    .notification-dismiss {
        top: 0;
        right: 0;
        min-width: 40px;
        min-height: 40px;
        opacity: 1;
    }

    /* ---------- Sidebar (already an overlay, closed by default) ---------- */

    .sidebar-link {
        min-height: 44px;
    }

    .sidebar-section-header,
    .workspace-dropdown-trigger,
    .workspace-dropdown-item {
        min-height: 44px;
    }

    /* The visible tab stays slim. The invisible pad makes it 40px wide to tap. */
    .sidebar-toggle::after {
        content: '';
        position: absolute;
        top: -4px;
        bottom: -4px;
        left: 0;
        width: 40px;
    }

    .sidebar-add-btn {
        position: relative;
    }

    .sidebar-add-btn::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        width: 40px;
        height: 40px;
        transform: translate(-50%, -50%);
    }

    /* ---------- Board header and toolbar ---------- */

    .board-page .breadcrumb > a {
        display: inline-flex;
        align-items: center;
        min-height: 40px;
    }

    .board-view-btn {
        min-height: 40px;
        justify-content: center;
    }

    .board-toolbar .board-shortcuts-btn {
        width: 40px;
        min-width: 40px;
        justify-content: center;
    }

    .board-filter-daterange input[type="date"] {
        min-height: 40px;
    }

    .board-filter-presets button {
        min-height: 40px;
        padding: 4px 12px;
    }

    .board-search-input,
    .board-filter-btn {
        min-height: 40px;
    }

    .board-search-clear {
        min-width: 40px;
        min-height: 40px;
        right: 0;
    }

    /* ---------- Table view ---------- */

    /* Each group is as wide as its columns and the container scrolls
       sideways. Without this the group clips its own columns and the ones
       past the first screen cannot be reached by touch. */
    .board-table-container .group-section {
        width: max-content;
        min-width: 100%;
        /* Visible, so the sticky group title below has the container to stick to. */
        overflow: visible;
    }

    .board-table-container .group-table {
        border-radius: 0 0 var(--radius) var(--radius);
    }

    /* A fixed Item column keeps a long name from stretching the group. */
    .board-table-container .cell-name {
        flex: 0 0 240px;
        width: 240px;
        min-width: 240px;
    }

    .board-table-container.has-name-width .cell-name {
        flex: 0 0 var(--name-col-width);
        width: var(--name-col-width);
        min-width: var(--name-col-width);
    }

    /* The group title and the add-item row stay on screen while the columns
       scroll underneath. 34px is the container padding plus the group border. */
    .board-table-container .group-header,
    .board-table-container .table-footer {
        position: sticky;
        left: 0;
        width: calc(100vw - 34px);
        max-width: 100%;
    }

    .board-table-container .group-header {
        padding: 6px 8px;
    }

    .board-table-container .group-header-left {
        gap: 4px;
        min-width: 0;
    }

    .group-collapse-btn {
        min-width: 40px;
        min-height: 40px;
    }

    .btn-icon {
        width: 40px;
        height: 40px;
    }

    .table-header,
    .table-row {
        min-height: 40px;
    }

    .cell-input,
    .status-label,
    .priority-label {
        min-height: 40px;
    }

    .table-row .btn-dependency,
    .table-row .btn-details {
        min-width: 40px;
        min-height: 40px;
        justify-content: center;
    }

    .table-row .cell-name {
        padding-right: 4px;
    }

    .table-cell.cell-actions {
        padding: 0 4px;
    }

    /* Hover never happens on a touch screen, so the column menu is always shown. */
    .column-actions {
        opacity: 1;
    }

    .btn-icon-sm {
        width: 40px;
        height: 40px;
    }

    .btn-add-column {
        position: relative;
    }

    .btn-add-column::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        width: 40px;
        height: 40px;
        transform: translate(-50%, -50%);
    }

    .add-item-input {
        min-height: 40px;
    }

    /* Tick boxes: a 40px tap area around an 18px box drawn from the theme. */
    .table-cell.cell-check {
        width: 40px;
        min-width: 40px;
        max-width: 40px;
    }

    .board-table-container .bulk-select,
    .board-table-container .bulk-select-all {
        -webkit-appearance: none;
        appearance: none;
        display: grid;
        place-items: center;
        flex-shrink: 0;
        width: 40px;
        height: 40px;
        background: transparent;
        border: 0;
    }

    .board-table-container .bulk-select::before,
    .board-table-container .bulk-select-all::before {
        content: '';
        grid-area: 1 / 1;
        box-sizing: border-box;
        width: 18px;
        height: 18px;
        background: var(--white);
        border: 2px solid var(--gray-400);
        border-radius: var(--radius-sm);
    }

    .board-table-container .bulk-select:checked::before,
    .board-table-container .bulk-select-all:checked::before,
    .board-table-container .bulk-select-all:indeterminate::before {
        background: var(--accent);
        border-color: var(--accent);
    }

    .board-table-container .bulk-select:checked::after,
    .board-table-container .bulk-select-all:checked::after {
        content: '';
        grid-area: 1 / 1;
        width: 5px;
        height: 10px;
        margin-top: -2px;
        border: solid var(--white);
        border-width: 0 2px 2px 0;
        transform: rotate(45deg);
    }

    .board-table-container .bulk-select-all:indeterminate::after {
        content: '';
        grid-area: 1 / 1;
        width: 10px;
        height: 2px;
        background: var(--white);
    }

    /* ---------- Timeline view ---------- */

    /* Item names stay put while the days scroll sideways. */
    .timeline-row-label-cell {
        position: sticky;
        left: 0;
        z-index: 1;
    }

    .timeline-controls select {
        min-height: 40px;
    }
}

@media (max-width: 640px) {
    /* ---------- Navbar dropdowns ---------- */

    .notification-dropdown {
        position: fixed;
        top: 64px;
        left: 8px;
        right: 8px;
        width: auto;
        max-height: calc(100dvh - 80px);
    }

    .toast-container {
        left: 12px;
        right: 12px;
        max-width: none;
    }

    /* ---------- Board header ---------- */

    .board-layout .page-header {
        padding: 12px 16px;
        gap: 12px;
        margin-bottom: 0;
    }

    .board-page .page-header-left {
        width: 100%;
    }

    /* One line: the workspace and board names shorten instead of wrapping. */
    .board-page .breadcrumb {
        flex-wrap: nowrap;
        width: 100%;
        min-width: 0;
    }

    .board-page .breadcrumb > * {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .board-page .breadcrumb > a {
        display: block;
        flex: 0 1 auto;
        max-width: 45%;
        line-height: 40px;
    }

    .board-page .breadcrumb > span:not(:last-child) {
        flex: none;
    }

    /* View switch on its own row, then the actions in an even grid. */
    .board-page .page-actions {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 8px;
        width: 100%;
    }

    .board-page .page-actions .board-view-toggle {
        grid-column: 1 / -1;
        display: flex;
    }

    .board-page .page-actions .board-view-btn {
        flex: 1 1 0;
        min-width: 0;
        padding: 6px 4px;
    }

    .board-page .page-actions .btn {
        min-width: 0;
        min-height: 40px;
        padding: 8px 4px;
        font-size: 0.8125rem;
    }

    /* ---------- Board toolbar ---------- */

    .board-toolbar {
        padding: 12px 16px;
        gap: 8px;
    }

    /* Filter panels hang off the toolbar, not their own button, so they span
       the screen and cannot run off its right edge. */
    .board-toolbar-left {
        position: relative;
        flex: 1 1 100%;
        gap: 8px;
    }

    .board-toolbar-left .board-filter {
        position: static;
    }

    .board-toolbar-left .board-filter-panel {
        left: 0;
        right: 0;
        min-width: 0;
        max-height: 50vh;
    }

    .board-filter-option {
        min-height: 40px;
    }

    .board-search {
        flex: 1 1 100%;
    }

    /* 16px text stops iOS zooming the page when a field takes focus. */
    .board-search-input {
        width: 100%;
        min-height: 44px;
        font-size: 16px;
    }

    /* Keyboard shortcuts have no use without a keyboard. */
    .board-shortcuts-btn {
        display: none;
    }

    /* ---------- Table view ---------- */

    /* Rows are dragged with a mouse, so the grip column is dead space here. */
    .table-cell.cell-drag {
        display: none;
    }

    /* ---------- Kanban view ---------- */

    /* Columns take their natural height and the page scrolls, instead of a
       scroll area inside a scroll area. */
    .kanban-board {
        padding: 12px 16px 16px;
        scroll-snap-type: x proximity;
        scroll-padding-left: 16px;
    }

    .kanban-column {
        flex: 0 0 min(280px, 82vw);
        max-height: none;
        scroll-snap-align: start;
    }

    .kanban-column-body {
        overflow-y: visible;
    }

    /* ---------- Calendar view ---------- */

    .calendar-controls {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 8px;
        margin-top: 12px;
    }

    .calendar-controls .calendar-title {
        grid-column: 1 / -1;
        order: -1;
        font-size: 1.125rem;
    }

    .calendar-controls .btn {
        min-width: 0;
        padding: 8px 4px;
        gap: 4px;
    }

    /* Seven columns cannot hold an item name at this width, so the month
       becomes a list of the days that have something due (plus today). */
    .calendar-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .calendar-weekday,
    .calendar-day-blank,
    .calendar-day:not(.calendar-day-today):not(:has(.calendar-item)) {
        display: none;
    }

    .calendar-day {
        flex-direction: row;
        align-items: flex-start;
        gap: 12px;
        min-height: 48px;
        padding: 8px 12px;
    }

    .calendar-day-number {
        flex: 0 0 56px;
        font-size: 14px;
        line-height: 32px;
    }

    /* The weekday comes from the cell's place in the seven-day grid order. */
    .calendar-day:nth-child(7n+1) .calendar-day-number::after { content: ' Sun'; }
    .calendar-day:nth-child(7n+2) .calendar-day-number::after { content: ' Mon'; }
    .calendar-day:nth-child(7n+3) .calendar-day-number::after { content: ' Tue'; }
    .calendar-day:nth-child(7n+4) .calendar-day-number::after { content: ' Wed'; }
    .calendar-day:nth-child(7n+5) .calendar-day-number::after { content: ' Thu'; }
    .calendar-day:nth-child(7n+6) .calendar-day-number::after { content: ' Fri'; }
    .calendar-day:nth-child(7n) .calendar-day-number::after { content: ' Sat'; }

    .calendar-day-number::after {
        font-weight: 400;
        color: var(--gray-500);
    }

    .calendar-day-items {
        flex: 1 1 0;
        min-width: 0;
        gap: 4px;
    }

    .calendar-item {
        padding: 6px 8px;
        font-size: 13px;
        white-space: normal;
        overflow-wrap: anywhere;
    }

    /* ---------- Timeline view ---------- */

    .timeline-controls {
        gap: 8px;
    }

    .timeline-controls label {
        flex: 1 1 100%;
    }

    .timeline-controls select {
        flex: 1 1 auto;
        min-width: 0;
        font-size: 16px;
    }

    .timeline-controls .btn {
        flex: 1 1 0;
        min-width: 0;
        padding: 8px 4px;
        gap: 4px;
    }

    .timeline-row-label-cell {
        flex-basis: 132px;
    }

    /* ---------- Modals ---------- */

    .modal {
        padding: 12px;
    }

    .modal .modal-content {
        width: 100%;
        max-width: none;
        max-height: calc(100dvh - 24px);
    }

    .modal .modal-header {
        position: sticky;
        top: 0;
        z-index: 2;
        gap: 8px;
        padding: 8px 4px 8px 16px;
        background: var(--white);
    }

    .modal .modal-header h3 {
        min-width: 0;
        font-size: 1.0625rem;
        line-height: 1.3;
        overflow-wrap: anywhere;
    }

    .modal .modal-close {
        flex-shrink: 0;
        min-width: 44px;
        min-height: 44px;
    }

    .modal .modal-content form,
    .modal .modal-body {
        padding: 16px;
    }

    .modal input[type="text"],
    .modal input[type="number"],
    .modal input[type="email"],
    .modal input[type="search"],
    .modal select,
    .modal textarea {
        min-height: 44px;
        font-size: 16px;
    }

    .modal .form-actions {
        gap: 8px;
        margin-top: 20px;
        padding-top: 16px;
    }

    .modal .form-actions .btn {
        flex: 1 1 0;
        min-width: 0;
        padding-left: 8px;
        padding-right: 8px;
    }

    .modal .dependency-search-row {
        gap: 8px;
    }

    .modal .dependency-search-row input[type="text"] {
        flex: 1 1 100%;
        min-width: 0;
    }

    .modal .dependency-search-row select {
        flex: 1 1 auto;
        min-width: 0;
    }

    .modal .checkbox-label {
        min-height: 40px;
    }

    .label-row input.label-row-colour,
    .label-row .label-row-remove {
        flex-basis: 44px;
        width: 44px;
        height: 44px;
    }

    /* ---------- Item details: the whole screen ---------- */

    #itemDetailsModal {
        padding: 0;
    }

    #itemDetailsModal .modal-content {
        display: flex;
        flex-direction: column;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        max-height: none;
        overflow: hidden;
        border: 0;
        border-radius: 0;
    }

    #itemDetailsModal .modal-header {
        position: static;
        flex: none;
    }

    /* A long name shows three lines. The full name is on the board row. */
    #itemDetailsModal .modal-header h3 {
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        overflow: hidden;
    }

    #itemDetailsModal .item-details-body {
        flex: 1 1 auto;
        min-height: 0;
        padding: 12px 16px 0;
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    #itemDetailsModal .item-details-meta {
        gap: 8px 16px;
    }

    #itemDetailsModal .item-details-tabs {
        position: sticky;
        top: -12px;
        z-index: 2;
        gap: 0;
        background: var(--white);
    }

    #itemDetailsModal .item-details-tab {
        flex: 1 1 0;
        min-width: 0;
        min-height: 44px;
        padding: 8px 4px;
    }

    #itemDetailsModal .item-details-pane {
        padding-bottom: 0;
    }

    /* The lists grow and the panel scrolls as one, with the comment box
       held at the bottom of the screen. */
    #itemDetailsModal .comments-list,
    #itemDetailsModal .attachments-list,
    #itemDetailsModal .activity-list {
        max-height: none;
        overflow: visible;
        padding-right: 0;
        padding-bottom: 16px;
    }

    #itemDetailsModal .comment-composer {
        position: sticky;
        bottom: 0;
        z-index: 3;
        margin: 0 -16px;
        padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
        background: var(--white);
    }

    #itemDetailsModal .comment-input {
        min-height: 72px;
    }

    #itemDetailsModal .comment-delete,
    #itemDetailsModal .attachment-delete {
        min-width: 40px;
        min-height: 40px;
    }

    #itemDetailsModal .attachment-name {
        overflow-wrap: anywhere;
    }

    /* Opens upwards so it is not hidden below the comment box. */
    #itemDetailsModal .mention-dropdown {
        top: auto;
        bottom: calc(100% - 8px);
        left: 16px;
        right: 16px;
        min-width: 0;
        max-height: 40vh;
    }

    /* ---------- Pickers: sheets along the bottom edge ---------- */

    /* The scripts place these next to the cell with inline styles, which can
       run off a narrow screen, so the position is overridden here. */
    .person-picker,
    .status-picker,
    .priority-picker,
    #columnMenu.dropdown-menu {
        top: auto !important;
        bottom: calc(8px + env(safe-area-inset-bottom));
        left: 8px !important;
        right: 8px;
        width: auto;
        min-width: 0;
        max-width: none;
        max-height: 60vh;
        overflow-y: auto;
        transform: none !important;
    }

    .person-picker-item,
    .status-picker-item,
    .priority-picker-item,
    .status-picker-clear,
    .priority-picker-clear,
    .status-picker-edit,
    .priority-picker-edit {
        min-height: 44px;
    }

    #columnMenu.dropdown-menu button {
        min-height: 44px;
    }

    /* ---------- Bulk action bar: docked to the bottom edge ---------- */

    body .bulk-bar {
        left: 0;
        right: 0;
        bottom: 0;
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 8px;
        max-width: none;
        padding: 4px 12px calc(8px + env(safe-area-inset-bottom));
        border-width: 2px 0 0;
        border-radius: 0;
        transform: none;
    }

    body .bulk-bar[hidden] {
        display: none;
    }

    .bulk-bar .bulk-bar-count {
        grid-column: 1 / span 3;
        order: 1;
        align-self: center;
        padding: 0;
    }

    .bulk-bar .bulk-bar-clear {
        order: 2;
        justify-self: end;
        min-width: 44px;
        min-height: 40px;
    }

    /* Menus hang off the bar, not their own button, so they span the screen. */
    .bulk-bar .bulk-bar-action {
        position: static;
        order: 3;
        min-width: 0;
    }

    .bulk-bar .bulk-bar-action .btn {
        width: 100%;
        min-width: 0;
        min-height: 44px;
        padding: 7px 4px;
        gap: 4px;
    }

    .bulk-bar .bulk-menu {
        left: 8px;
        right: 8px;
        min-width: 0;
        max-width: none;
        max-height: min(320px, 50vh);
    }

    .bulk-bar .bulk-menu-item {
        min-height: 44px;
    }

    /* Room to scroll the last rows clear of the bar while it is showing. */
    body:has(.bulk-bar:not([hidden])) .board-table-container {
        padding-bottom: 128px;
    }
}
