/*
 * REKAZ on phones. Loaded only while the identity and the phone layout are on in the settings.
 * Every rule is inside a phone-width media query and starts with html.rk-mobile-on,
 * which the script sets, so a desktop screen is never affected.
 */

@media (max-width: 575.98px) {

    /* ---------- bottom bar ---------- */

    html.rk-mobile-on .mobile-bottom-menu.rk-bar {
        background: rgba(255, 255, 255, 0.97) !important;
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        border-top: 1px solid var(--rk-line, #e3e8f0) !important;
        box-shadow: 0 -10px 30px -20px rgba(15, 23, 42, 0.5);
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    html.rk-mobile-on .mobile-bottom-menu.rk-bar > div {
        display: grid !important;
        grid-auto-flow: column;
        grid-auto-columns: 1fr;
        align-items: end;
        height: 64px;
        padding: 0 6px !important;
    }

    /* home, tasks, add, chat, menu: the add button lands in the middle */
    html.rk-mobile-on .rk-tab-home { order: 1; }
    html.rk-mobile-on .rk-tab-todo { order: 2; }
    html.rk-mobile-on .rk-tab-add { order: 3; }
    html.rk-mobile-on .rk-tab-chat { order: 4; }
    html.rk-mobile-on .rk-tab-menu { order: 5; }

    html.rk-mobile-on .rk-bar .rk-tab {
        padding: 0 !important;
        margin: 0 !important;
        min-width: 0;
        display: flex !important;
        justify-content: center;
    }

    /* the element that reacts to the tap fills the cell: easier to hit */
    html.rk-mobile-on .rk-bar .rk-tab > a,
    html.rk-mobile-on .rk-bar a.rk-tab,
    html.rk-mobile-on .rk-bar .rk-tab-chat .chat-min-icon,
    html.rk-mobile-on .rk-bar .rk-tab-chat #js-init-chat-icon {
        display: flex !important;
        flex-direction: column;
        align-items: center;
        justify-content: flex-end;
        gap: 3px;
        width: 100%;
        min-height: 56px;
        padding: 6px 0 8px !important;
        color: var(--rk-muted, #64748b) !important;
        background: transparent !important;
        border: 0 !important;
        position: static !important;
        box-shadow: none !important;
        cursor: pointer;
    }

    html.rk-mobile-on .rk-bar .rk-tab-chat #js-init-chat-icon {
        min-height: 0;
        padding: 0 !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab svg {
        width: 22px !important;
        height: 22px !important;
        color: currentColor !important;
        stroke: currentColor;
        margin: 0 !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab-label {
        display: block;
        font-size: 11px;
        line-height: 1.2;
        font-weight: 500;
        white-space: nowrap;
    }

    html.rk-mobile-on .rk-bar .rk-tab.rk-active > a,
    html.rk-mobile-on .rk-bar a.rk-tab.rk-active {
        color: var(--rk-blue, #2563eb) !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab.rk-active .rk-tab-label {
        font-weight: 700;
    }

    /* the add button: orange, raised above the bar */
    html.rk-mobile-on .rk-bar .rk-tab-add #quick-add-icon {
        justify-content: flex-end;
        color: var(--rk-text, #334155) !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab-add #quick-add-icon::after {
        display: none;
    }

    html.rk-mobile-on .rk-bar .rk-tab-add #quick-add-icon svg {
        box-sizing: content-box;
        width: 26px !important;
        height: 26px !important;
        padding: 12px;
        margin-top: -26px !important;
        border-radius: 18px;
        color: #1b1203 !important;
        background: linear-gradient(135deg, var(--rk-orange, #f7931e), var(--rk-amber, #fdba2d));
        box-shadow: 0 12px 24px -10px rgba(247, 147, 30, 0.95), 0 0 0 4px #fff;
        transition: transform 0.15s ease;
    }

    html.rk-mobile-on .rk-bar .rk-tab-add #quick-add-icon:active svg {
        transform: scale(0.94);
    }

    /* the page ends above the bar, nothing is hidden behind it */
    html.rk-mobile-on body {
        padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px));
    }

    /* the AI button sits above the bar */
    html.rk-mobile-on #rekaz-ai-assist-button {
        bottom: calc(84px + env(safe-area-inset-bottom, 0px)) !important;
    }

    /* ---------- quick add as a sheet ---------- */

    html.rk-mobile-on .rk-bar .quick-add-dropdown.show {
        position: fixed !important;
        inset: auto 0 0 0 !important;
        transform: none !important;
        margin: 0 !important;
        width: 100% !important;
        max-height: 75vh;
        overflow-y: auto;
        border: 0 !important;
        border-radius: 22px 22px 0 0 !important;
        padding: 14px 12px calc(16px + env(safe-area-inset-bottom, 0px)) !important;
        box-shadow: 0 -18px 40px -14px rgba(15, 23, 42, 0.45) !important;
        z-index: 1061;
        animation: rk-sheet-up 0.22s ease both;
    }

    /* the handle at the top of the sheet */
    html.rk-mobile-on .rk-bar .quick-add-dropdown.show::before {
        content: "";
        display: block;
        width: 40px;
        height: 4px;
        border-radius: 4px;
        background: #cbd5e1;
        margin: 0 auto 12px;
    }

    html.rk-mobile-on .rk-bar .quick-add-dropdown .dropdown-item {
        display: flex;
        align-items: center;
        min-height: 48px;
        padding: 10px 14px;
        margin-bottom: 6px;
        border-radius: 12px;
        background: var(--rk-page, #f4f6fb);
        font-size: 15px;
        font-weight: 500;
        white-space: normal;
    }

    html.rk-mobile-on .rk-bar .quick-add-dropdown .dropdown-item:active {
        background: #e8f0ff;
        color: var(--rk-blue, #2563eb);
    }

    html.rk-mobile-on .rk-sheet-backdrop {
        display: none;
    }

    /* the sheet lives inside the bar, so the bar rises above the backdrop while it is open */
    html.rk-mobile-on body.rk-sheet-open .mobile-bottom-menu.rk-bar {
        z-index: 1062 !important;
    }

    html.rk-mobile-on body.rk-sheet-open .rk-sheet-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 1060;
        background: rgba(11, 23, 54, 0.45);
        animation: rk-fade-in 0.2s ease both;
    }

    /* ---------- dialogs as sheets ---------- */

    html.rk-mobile-on #ajaxModal .modal-dialog {
        position: fixed;
        inset: auto 0 0 0;
        margin: 0;
        max-width: none;
        width: 100%;
    }

    html.rk-mobile-on #ajaxModal.fade .modal-dialog {
        transform: translateY(100%);
        transition: transform 0.25s ease-out;
    }

    html.rk-mobile-on #ajaxModal.show .modal-dialog {
        transform: none;
    }

    html.rk-mobile-on #ajaxModal .modal-content {
        border-radius: 22px 22px 0 0 !important;
        max-height: 92vh;
        max-height: 92dvh;
        overflow-y: auto;
        overscroll-behavior: contain;
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    /* the handle, above the coloured line of the identity */
    html.rk-mobile-on #ajaxModal .modal-header {
        position: sticky;
        top: 0;
        z-index: 3;
        background: #fff;
    }

    /* save and close stay in reach while the form scrolls */
    html.rk-mobile-on #ajaxModal .modal-footer {
        position: sticky;
        bottom: 0;
        z-index: 3;
        background: #fff;
        box-shadow: 0 -8px 20px -16px rgba(15, 23, 42, 0.5);
    }

    html.rk-mobile-on #ajaxModal .modal-footer .btn {
        min-height: 44px;
        flex: 1 1 auto;
    }

    /* ---------- touch sizes ---------- */

    html.rk-mobile-on .btn {
        min-height: 40px;
    }

    html.rk-mobile-on .dropdown-menu .dropdown-item {
        padding-top: 10px;
        padding-bottom: 10px;
    }

    html.rk-mobile-on table .option a,
    html.rk-mobile-on table td.option a {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 38px;
        min-height: 38px;
    }

    /* inputs of 16px keep iPhone from zooming the page when a field gets the focus */
    html.rk-mobile-on input.form-control,
    html.rk-mobile-on select.form-control,
    html.rk-mobile-on textarea.form-control {
        font-size: 16px;
    }

    /* ---------- lists as cards ---------- */

    html.rk-mobile-on table.rk-cards {
        min-width: 0 !important;
        width: 100% !important;
        border: 0 !important;
        background: transparent !important;
    }

    html.rk-mobile-on table.rk-cards thead {
        display: none;
    }

    html.rk-mobile-on table.rk-cards tbody,
    html.rk-mobile-on table.rk-cards tr,
    html.rk-mobile-on table.rk-cards td {
        display: block;
        width: 100% !important;
    }

    html.rk-mobile-on table.rk-cards tbody tr {
        background: #fff !important;
        border: 1px solid var(--rk-line, #e3e8f0);
        border-radius: 14px;
        margin: 0 0 10px;
        padding: 10px 14px;
        box-shadow: 0 6px 16px -14px rgba(15, 23, 42, 0.5);
    }

    html.rk-mobile-on table.rk-cards tbody td {
        display: flex !important;
        align-items: flex-start;
        justify-content: space-between;
        gap: 12px;
        padding: 5px 0 !important;
        border: 0 !important;
        text-align: start !important;
        white-space: normal !important;
        min-height: 0;
        background: transparent !important;
    }

    html.rk-mobile-on table.rk-cards tbody td::before {
        content: attr(data-label);
        flex: none;
        max-width: 42%;
        color: var(--rk-muted, #64748b);
        font-size: 12.5px;
        font-weight: 500;
    }

    html.rk-mobile-on table.rk-cards tbody td > * {
        min-width: 0;
        text-align: end;
    }

    /* cells without a title (checkboxes, colour marks) and empty cells take no row */
    html.rk-mobile-on table.rk-cards tbody td[data-label=""]::before {
        display: none;
    }

    html.rk-mobile-on table.rk-cards tbody td:empty {
        display: none !important;
    }

    /* the first titled cell is the name of the record: the title of the card */
    html.rk-mobile-on table.rk-cards tbody td.rk-card-title {
        font-size: 15.5px;
        font-weight: 600;
        color: var(--rk-ink, #0f172a);
    }

    html.rk-mobile-on table.rk-cards tbody td.rk-card-title::before {
        display: none;
    }

    html.rk-mobile-on table.rk-cards tbody td.rk-card-title > * {
        text-align: start;
    }

    /* the actions of the row, at the bottom of the card */
    html.rk-mobile-on table.rk-cards tbody td.option,
    html.rk-mobile-on table.rk-cards tbody td:last-child.text-center {
        justify-content: flex-end;
        border-top: 1px solid var(--rk-line, #e3e8f0) !important;
        margin-top: 6px;
        padding-top: 8px !important;
    }

    html.rk-mobile-on table.rk-cards tbody td.option::before {
        display: none;
    }

    /* the message of an empty list stays a plain line */
    html.rk-mobile-on table.rk-cards tbody td.dataTables_empty {
        justify-content: center;
    }

    html.rk-mobile-on table.rk-cards tbody td.dataTables_empty::before {
        display: none;
    }
}

@keyframes rk-sheet-up {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

@keyframes rk-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    html.rk-mobile-on .rk-bar .quick-add-dropdown.show,
    html.rk-mobile-on body.rk-sheet-open .rk-sheet-backdrop,
    html.rk-mobile-on #ajaxModal .modal-dialog {
        animation: none !important;
        transition: none !important;
    }
}

/* ---------- coloured icons on phones (REKAZ colours) ---------- */

@media (max-width: 575.98px) {

    /* top bar: each icon in a soft blue tile */
    html.rk-mobile-on .navbar-custom .navbar-nav .nav-link,
    html.rk-mobile-on .navbar-custom .navbar-nav > li > a {
        color: var(--rk-blue, #2563eb) !important;
    }

    html.rk-mobile-on .navbar-custom .navbar-nav .nav-link svg,
    html.rk-mobile-on .navbar-custom .navbar-nav > li > a svg {
        color: var(--rk-blue, #2563eb) !important;
        stroke: currentColor;
    }

    html.rk-mobile-on .navbar-custom .navbar-nav .nav-link:has(> svg),
    html.rk-mobile-on .navbar-custom .navbar-nav > li > a:has(> svg) {
        background: rgba(37, 99, 235, 0.08);
        border-radius: 10px;
    }

    /* the menu button and the theme switch in navy and orange, so the bar is not one colour */
    html.rk-mobile-on .navbar-custom .navbar-nav .nav-link.sidebar-toggle-btn svg {
        color: var(--rk-navy, #0b1736) !important;
    }

    html.rk-mobile-on .navbar-custom .navbar-nav .nav-link.rk-theme-toggle svg {
        color: var(--rk-orange, #f7931e) !important;
    }

    /* bottom bar: blue icons, the open page in orange with a soft pill */
    html.rk-mobile-on .rk-bar .rk-tab > a,
    html.rk-mobile-on .rk-bar a.rk-tab {
        color: var(--rk-indigo, #1e3a8a) !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab svg {
        color: var(--rk-blue, #2563eb) !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab.rk-active svg {
        color: var(--rk-orange, #f7931e) !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab.rk-active > a,
    html.rk-mobile-on .rk-bar a.rk-tab.rk-active {
        color: var(--rk-orange, #f7931e) !important;
    }

    html.rk-mobile-on .rk-bar .rk-tab.rk-active svg {
        background: rgba(247, 147, 30, 0.14);
        border-radius: 10px;
        padding: 3px;
        box-sizing: content-box;
    }
}

/* side menu on phones: its header with the close button stays in view while the menu scrolls,
   the menu leaves a strip of the page uncovered, and a tap on that strip closes it */
@media (max-width: 575.98px) {
    html.rk-mobile-on .sidebar-toggled .sidebar {
        width: 86% !important;
        max-width: 340px;
        box-shadow: 0 0 40px rgba(2, 6, 23, 0.45);
        z-index: 1040;
    }

    html.rk-mobile-on .sidebar-toggled .sidebar .sidebar-brand.brand-logo,
    html.rk-mobile-on .sidebar-toggled .sidebar .sidebar-toggle-btn {
        position: sticky;
        top: 0;
        z-index: 3;
    }

    html.rk-mobile-on .sidebar-toggled .sidebar .sidebar-toggle-btn {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        margin: 10px 8px 0 !important;
        padding: 0 !important;
        border-radius: 12px;
        background: rgba(255, 255, 255, 0.1);
        color: #fff !important;
    }

    html.rk-mobile-on .rk-menu-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(2, 6, 23, 0.5);
        z-index: 1030;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease;
    }

    html.rk-mobile-on body.sidebar-toggled .rk-menu-backdrop {
        opacity: 1;
        pointer-events: auto;
    }
}
