/* ==========================================================================
   HomeRP app shell (#105) — mobile-first
   ==========================================================================

   The signed-in chrome, authored from the phone outwards. The phone layout is the
   primary one; the desktop layout is the phone layout plus a persistent sidebar and
   denser spacing.

   What 0.3.0 did, and why this replaces it:

   - The only navigation was a 258px off-canvas drawer — 69% of a 375px viewport —
     opened by a 34x34 burger in the top-left corner, out of thumb reach and below
     the 44px tap-target minimum.
   - There was no `dvh`, no `env(safe-area-inset-*)` and `index.html` had no
     `viewport-fit=cover`, so the sticky top bar and the fixed drawer ran under the
     notch and the iOS home indicator.
   - The declared breakpoint was 768px, which put a tablet in the desktop layout
     with a 258px sidebar on a 768px screen.

   Now: five primary destinations in a bottom tab bar in thumb reach (< 1024px), a
   52px top bar, bottom sheets for everything secondary, and the sidebar only from
   1024px up. The drawer, the burger, its checkbox and its scrim are gone.

   One rule throughout: no colour, radius, spacing or type literal — tokens only.
   ========================================================================== */

/* --- the frame ------------------------------------------------------------ */
.app-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;   /* fallback for browsers without dvh */
    min-height: 100dvh;  /* excludes the mobile browser's chrome */
}

/* The sidebar does not exist on a phone — not hidden behind a drawer, absent. */
.app-sidebar {
    display: none;
}

.app-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

/* --- top bar (phone) ------------------------------------------------------ */
.app-topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    height: var(--topbar-height-compact);
    padding: 0 var(--space-4);
    padding-left: max(var(--space-4), env(safe-area-inset-left));
    padding-right: max(var(--space-4), env(safe-area-inset-right));
    background: var(--topbar-bg);
    border-bottom: 1px solid var(--line);
}

/* The organization is a button on a phone: it opens the "More" sheet, which owns
   the switcher. On desktop the sidebar owns it, so this becomes plain text. */
.app-topbar__org {
    display: flex;
    align-items: center;
    gap: .35rem;
    max-width: 62%;
    min-height: var(--ctl-h);
    padding: 0 .25rem;
    border: 0;
    background: transparent;
    color: var(--ink);
    font: inherit;
    font-size: var(--fs-base);
    font-weight: var(--fw-semibold);
    cursor: pointer;
}

    .app-topbar__org:hover {
        color: var(--accent);
    }

.app-topbar__org-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.app-topbar__org-caret {
    flex: 0 0 auto;
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--muted);
}

.app-topbar__context {
    display: none;
}

.app-topbar .user-menu {
    margin-left: auto;
}

/* --- content -------------------------------------------------------------- */
/* The bottom padding clears the tab bar *and* its safe-area inset, so the last
   row of a page is never trapped behind the navigation. */
.app-content {
    flex: 1;
    width: 100%;
    padding: var(--space-4) var(--space-4)
             calc(var(--tabbar-height) + env(safe-area-inset-bottom) + var(--space-5));
}

/* --- bottom tab bar ------------------------------------------------------- */
.app-tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    display: flex;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface);
    border-top: 1px solid var(--line);
    box-shadow: var(--elev-2);
}

.app-tab {
    flex: 1 1 0;
    min-width: 0;
    min-height: var(--tabbar-height);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: var(--space-1) var(--space-1) 0;
    border: 0;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-size: var(--fs-sm);
    font-weight: var(--fw-medium);
    text-decoration: none;
    cursor: pointer;
}

    .app-tab:hover {
        color: var(--ink);
        text-decoration: none;
    }

    /* Colour *and* weight *and* aria-current — never colour alone. */
    .app-tab.is-active,
    .app-tab.is-open {
        color: var(--accent);
        font-weight: var(--fw-semibold);
    }

        .app-tab.is-active .nav-icon,
        .app-tab.is-open .nav-icon {
            stroke-width: 2.4;
        }

/* Five labels at 14px do not fit one line at 375px — "Household" truncated to
   "Househ…" in the first render. Wrapping to a second line beats an ellipsis, and the
   bar grew to 60px to hold it (#107). The 14px floor is not negotiable. */
.app-tab__label {
    max-width: 100%;
    max-height: 2.3em;
    overflow: hidden;
    line-height: 1.15;
    text-align: center;
    /* Never break inside a word: "Household" became "Househol / d" in the first
       render. A label that does not fit is a label to shorten, not to hyphenate. */
    overflow-wrap: normal;
}

/* --- bottom sheet --------------------------------------------------------- */
.app-sheet__scrim {
    position: fixed;
    inset: 0;
    z-index: 30;
    background: var(--scrim);
}

.app-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    max-height: 80vh;
    max-height: 80dvh;
    overflow-y: auto;
    padding: var(--space-2) var(--space-4)
             calc(var(--space-4) + env(safe-area-inset-bottom));
    background: var(--surface);
    border-radius: var(--r-card) var(--r-card) 0 0;
    box-shadow: var(--elev-2);
}

.app-sheet__head {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: 48px;
    padding-top: var(--space-2);
}

.app-sheet__grip {
    position: absolute;
    top: .3rem;
    left: 50%;
    width: 36px;
    height: 4px;
    margin-left: -18px;
    border-radius: var(--r-pill);
    background: var(--line);
}

.app-sheet__title {
    flex: 1;
    margin: 0;
    font-family: var(--font-display);
    font-size: var(--fs-lg);
    color: var(--heading);
}

.app-sheet__close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: var(--ctl-h);
    height: var(--ctl-h);
    min-height: var(--ctl-h);
    margin-right: calc(var(--space-2) * -1);
    border: 0;
    border-radius: var(--r-ctl);
    background: transparent;
    color: var(--ink);
    cursor: pointer;
}

    .app-sheet__close:hover {
        background: var(--accent-soft);
        color: var(--accent);
    }

.app-sheet__list {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin-top: var(--space-2);
}

.app-sheet__item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 56px;
    padding: var(--space-2);
    border-radius: var(--r-ctl);
    color: var(--ink);
    text-decoration: none;
}

    .app-sheet__item:hover {
        background: var(--sunken);
        text-decoration: none;
    }

.app-sheet__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    border-radius: var(--r-sm);
    background: var(--accent-soft);
    color: var(--accent);
}

.app-sheet__text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

    .app-sheet__text b {
        font-size: var(--fs-base);
        font-weight: var(--fw-semibold);
    }

    .app-sheet__text i {
        color: var(--muted);
        font-size: var(--fs-sm);
        font-style: normal;
    }

/* --- brand ---------------------------------------------------------------- */
.app-brand {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: 1.15rem 1.25rem;
    color: var(--sidebar-active-fg);
    font-size: var(--fs-md);
    font-weight: var(--fw-bold);
    letter-spacing: -.01em;
    text-decoration: none;
}

    .app-brand:hover,
    .app-brand:focus {
        color: var(--sidebar-active-fg);
        text-decoration: none;
    }

.app-brand__mark {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    flex: 0 0 26px;
    border-radius: var(--r-sm);
    background: var(--accent);
    color: var(--on-accent);
    font-size: var(--fs-sm);
    font-weight: 800;
}

/* --- navigation (desktop sidebar) ----------------------------------------- */
.app-nav {
    flex: 1;
    padding: .4rem .6rem 1rem;
    overflow-y: auto;
}

.app-nav__label {
    margin: .9rem .6rem .35rem;
    color: var(--sidebar-muted);
    font-size: var(--fs-sm);
    font-weight: var(--fw-bold);
    letter-spacing: .09em;
    text-transform: uppercase;
}

    .app-nav__label:first-child {
        margin-top: .2rem;
    }

.app-nav .nav-link {
    display: flex;
    align-items: center;
    gap: .6rem;
    min-height: var(--ctl-h);   /* 44px — was ~33px */
    margin-bottom: 2px;
    padding: .5rem .7rem;
    border-radius: var(--r-ctl);
    color: var(--sidebar-fg);
    font-size: var(--fs-base);
    font-weight: var(--fw-medium);
    text-decoration: none;
}

    .app-nav .nav-link:hover {
        background: var(--sidebar-active-bg);
        color: var(--sidebar-active-fg);
        text-decoration: none;
    }

    .app-nav .nav-link.active {
        background: var(--sidebar-active-bg);
        color: var(--sidebar-active-fg);
        font-weight: var(--fw-semibold);
    }

.nav-icon {
    flex: 0 0 auto;
}

/* --- organization switcher (sidebar and the "More" sheet) ------------------ */
.org-switcher {
    padding: var(--space-1) var(--space-3) var(--space-2);
}

.org-switcher__label {
    display: block;
    margin-bottom: .25rem;
    color: var(--sidebar-muted);
    font-size: var(--fs-sm);
    font-weight: var(--fw-bold);
    letter-spacing: .07em;
    text-transform: uppercase;
}

.org-switcher__select {
    width: 100%;
    min-height: var(--ctl-h);
    padding: .45rem .6rem;
    border: 1px solid var(--sidebar-border);
    border-radius: var(--r-ctl);
    background: var(--sidebar-bg);
    color: var(--sidebar-active-fg);
    font-size: var(--fs-input);
}

/* --- user menu (native <details>, no JavaScript) -------------------------- */
.user-menu {
    position: relative;
}

.user-menu__summary {
    display: flex;
    align-items: center;
    gap: .5rem;
    min-height: var(--ctl-h);
    padding: .25rem .6rem;
    border: 1px solid var(--line);
    border-radius: var(--r-ctl);
    background: var(--surface);
    color: var(--ink);
    font-size: var(--fs-sm);
    cursor: pointer;
    list-style: none;
}

    .user-menu__summary::-webkit-details-marker {
        display: none;
    }

    .user-menu[open] > .user-menu__summary {
        border-color: var(--accent);
    }

.user-menu__avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    flex: 0 0 28px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--on-accent);
    font-size: var(--fs-sm);
    font-weight: var(--fw-bold);
}

/* The name is desktop-only — the avatar carries the menu on a phone. */
.user-menu__name {
    display: none;
    max-width: 12rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-menu__caret {
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--muted);
}

.user-menu__panel {
    position: absolute;
    right: 0;
    top: calc(100% + .35rem);
    z-index: 50;
    min-width: 14rem;
    padding: .35rem;
    border: 1px solid var(--line);
    border-radius: var(--r-card);
    background: var(--surface);
    box-shadow: var(--elev-2);
}

.user-menu__heading {
    padding: .5rem .6rem;
}

    .user-menu__heading b {
        display: block;
        color: var(--heading);
        font-size: var(--fs-sm);
    }

    .user-menu__heading span {
        display: block;
        overflow: hidden;
        color: var(--muted);
        font-size: var(--fs-sm);
        text-overflow: ellipsis;
        white-space: nowrap;
    }

.user-menu__sep {
    margin: .3rem 0;
    border: 0;
    border-top: 1px solid var(--line);
}

.user-menu__item {
    display: block;
    width: 100%;
    min-height: var(--ctl-h);
    padding: .5rem .6rem;
    border: 0;
    border-radius: var(--r-sm);
    background: transparent;
    color: var(--ink);
    font: inherit;
    font-size: var(--fs-base);
    text-align: left;
    cursor: pointer;
}

    .user-menu__item:hover {
        background: var(--accent-soft);
        color: var(--heading);
    }

/* ==========================================================================
   Desktop: the sidebar replaces the tab bar and the sheets
   ========================================================================== */
@media (min-width: 1024px) {
    .app-shell {
        flex-direction: row;
    }

    .app-sidebar {
        display: flex;
        flex-direction: column;
        width: var(--sidebar-width);
        flex: 0 0 var(--sidebar-width);
        position: sticky;
        top: 0;
        height: 100vh;
        height: 100dvh;
        overflow-y: auto;
        background: var(--sidebar-bg);
        border-right: 1px solid var(--sidebar-border);
        color: var(--sidebar-fg);
    }

    /* The phone's navigation has no desktop presence — it is not hidden with CSS,
       the bar and the sheets are simply not part of the desktop experience. */
    .app-tabbar,
    .app-sheet,
    .app-sheet__scrim {
        display: none;
    }

    .app-topbar {
        height: var(--topbar-height);
        padding: 0 var(--content-pad-x);
    }

    .app-topbar__org {
        display: none;
    }

    .app-topbar__context {
        display: block;
        min-width: 0;
        overflow: hidden;
        color: var(--muted);
        font-size: var(--fs-base);
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-content {
        max-width: var(--content-max);
        padding: var(--space-5) var(--content-pad-x) var(--space-7);
    }

    .user-menu__name {
        display: block;
    }
}
