* {
    margin: 0;
    padding: 0;

    border-width: var(--border-width);
    border-color: var(--text);
}

/* border box */
*,
*::before,
*::after {
    box-sizing: border-box;
}

/* form controls do not inherit the page font on their own */
button,
input,
select,
textarea {
    font: inherit;
}

html {
    height: 100%;
    /* iOS Safari < 15.4: height of the visible viewport */
    height: -webkit-fill-available;

    /*
     * iOS home-screen apps latch scroll gestures to the document unless it is
     * explicitly non-scrollable; then nested scrollers freeze. position: fixed
     * plus overflow: hidden is the known workaround (WebKit bug 222654).
     */
    position: fixed;
    inset: 0;
    overflow: hidden;
    overscroll-behavior: none;

    /* Safari on iPad/landscape must not inflate text */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    background-color: var(--bg);
    color: var(--text);
    font-family: var(--font-sans);
    font-size: var(--font-size-base);

    /*
     * The page must never scroll, so the shell has to match the *visible*
     * viewport. 100vh in mobile Safari is the large viewport (tall enough to
     * sit behind the toolbar), which is what pushed the footer off screen.
     * dvh follows the actual visible viewport; the earlier declarations are
     * fallbacks for browsers that do not know dvh yet.
     */
    height: 100vh;
    height: -webkit-fill-available;
    height: 100dvh;

    width: 100%;
    /* keep the shell anchored to the viewport, not the scrollable document */
    position: fixed;
    inset: 0;
    overflow: hidden;
    overscroll-behavior: none;

    /* keep the shell clear of the notch and home indicator (viewport-fit=cover) */
    padding-top: env(safe-area-inset-top, 0px);
    padding-right: env(safe-area-inset-right, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);

    display: flex;
    flex-direction: column;
    justify-content: space-between;

    --base-padding: 15px;
}


h1,
h2,
h3 {
    font-size: var(--font-size-xl);
}


/* navbar */
nav {
    flex: 0 0 auto;
    position: relative;

    height: 50px;
    width: 100%;

    padding: 0 var(--base-padding);

    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;

    border-bottom-style: solid;
}

/*
 * Fox walking on the nav's bottom border. Absolute, so it adds nothing to the
 * nav height; bottom: 0 is the padding box, which is the top edge of the
 * border, so the feet stand on the line. It is the first child in the markup,
 * so the positioned nav items paint over it.
 *
 * The mask fades it in and out at both ends of the free stretch, so it never
 * walks through the logo or the nav items. Both clearances are fixed widths:
 * the logo and the item group do not scale with the viewport, so they hold at
 * every width.
 */
.fox--nav {
    --fox-travel-duration: 16s;
    --fox-nav-clear-left: 80px;
    /* nav padding + logo + air */
    --fox-nav-clear-right: 120px;
    /* nav padding + item group + air */
    --fox-nav-fade: 40px;

    inset: auto 0 0 0;

    -webkit-mask-image: linear-gradient(to right,
            transparent calc(var(--fox-nav-clear-left) - var(--fox-nav-fade)),
            #000 var(--fox-nav-clear-left),
            #000 calc(100% - var(--fox-nav-clear-right) - var(--fox-nav-fade)),
            transparent calc(100% - var(--fox-nav-clear-right)));
    mask-image: linear-gradient(to right,
            transparent calc(var(--fox-nav-clear-left) - var(--fox-nav-fade)),
            #000 var(--fox-nav-clear-left),
            #000 calc(100% - var(--fox-nav-clear-right) - var(--fox-nav-fade)),
            transparent calc(100% - var(--fox-nav-clear-right)));
}

/* The traverse grows with the viewport, so the duration has to grow with it to
   keep the ground speed constant. */
@media (min-width: 700px) {
    .fox--nav {
        --fox-travel-duration: 27s;
    }
}

@media (min-width: 1200px) {
    .fox--nav {
        --fox-travel-duration: 36s;
    }
}

@media (min-width: 1600px) {
    .fox--nav {
        --fox-travel-duration: 48s;
    }
}

@media (prefers-reduced-motion: no-preference) {
    .fox--nav .fox__travel {
        animation: fox-nav-travel var(--fox-travel-duration) linear infinite;
    }

    .fox--nav .fox__flip {
        animation: fox-nav-flip var(--fox-travel-duration) linear infinite;
    }
}

/* In from the left, out past the right edge, then back the same way mirrored.
   Both halves cover the same distance, so linear keeps one speed. */
@keyframes fox-nav-travel {
    from {
        transform: translateX(calc(-1 * var(--fox-width)));
    }

    50% {
        transform: translateX(100%);
    }

    to {
        transform: translateX(calc(-1 * var(--fox-width)));
    }
}

/* Both flips happen while the fox is fully offscreen: mid-point on the right,
   loop boundary on the left. */
@keyframes fox-nav-flip {
    from {
        transform: scaleX(1);
    }

    49.99% {
        transform: scaleX(1);
    }

    50% {
        transform: scaleX(-1);
    }

    to {
        transform: scaleX(-1);
    }
}

/* Reduced motion: one frame, parked mid-nav instead of behind the logo. */
@media (prefers-reduced-motion: reduce) {
    .fox--nav .fox__travel {
        transform: translateX(calc(50% - var(--fox-width) / 2));
    }
}

.nav-items {
    display: flex;
    flex-direction: row;
    gap: 10px;
}

.nav-item {
    display: block;
    height: 30px;
    width: 30px;
    position: relative;
}

button.nav-item {
    background: none;
    border-style: none;
    cursor: pointer;
}

/* Icons are masked so they take the text colour; each element passes its
   own icon via the --icon custom property (set inline where it is used). */
.logo {
    display: block;
    height: 30px;
    width: 30px;

    background-color: var(--text);
    -webkit-mask: var(--icon) center / contain no-repeat;
    mask: var(--icon) center / contain no-repeat;
}

.notification-badge {
    position: absolute;
    top: 0;
    right: 0;

    width: 10px;
    height: 10px;

    background: var(--primary);
    box-shadow: 0 0 0 var(--border-width) var(--bg);
    pointer-events: none;
}

.card {
    border-style: solid;
}

.card:focus-visible,
.nav-item:focus-visible,
.footer-item:focus-visible {
    outline: var(--border-width) solid var(--text);
    outline-offset: 2px;
}


/* main part */
main {
    flex-grow: 1;
    position: relative;
    min-height: 0;
}

.content-container {
    width: 100%;
    height: 100%;
    min-height: 0;
    padding: var(--base-padding);

    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: var(--base-padding);
}

.content-block {
    flex-grow: 1;
    /* a flex item will not shrink below its content height without this */
    min-height: 0;
}


/* footer */
footer {
    flex: 0 0 auto;

    padding: 5px;

    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;

    border-style: solid;
}

.footer-item {
    display: block;
    height: 25px;
    width: 25px;
}

.footer-item .logo {
    height: 25px;
    width: 25px;
}

.footer-items {
    display: flex;
    flex-direction: row;
    gap: 10px;
}


/* overlay */

/*
 * Every overlay lives in this layer. It fills main and clips its children, so a
 * hidden overlay can sit outside main without being visible or scrollable.
 */
.overlay-layer {
    position: absolute;
    inset: 0;

    overflow: hidden;
    overflow: clip;

    pointer-events: none;
}

/* shared shell for all overlays */
.overlay {
    position: absolute;
    inset: 0;

    width: auto;
    max-width: 800px;
    margin-inline: auto;

    display: flex;
    flex-direction: column;

    background: var(--bg);
    z-index: 1000;

    pointer-events: auto;
    visibility: hidden;
    transition: transform 280ms ease, visibility 0s 280ms;
}

.overlay.open {
    visibility: visible;
    transition: transform 280ms ease;
}

.overlay.dragging {
    transition: none;
}

/* bottom sheet: the generic dialog */
.overlay--sheet {
    /*
     * The sheet starts one border-width above main so its top border is hidden
     * under the navbar while open. Dragging or closing it down makes the
     * border slide into view without any colour change or corner artifacts.
     */
    top: calc(-1 * var(--border-width));

    border-style: solid solid none;
    transform: translateY(100%);
}

.overlay--sheet.open {
    transform: translateY(0);
}

/* notifications: drops down from the navbar and grows with its content */
.overlay--notifications {
    inset: 0 0 auto;
    height: auto;
    max-height: 50%;

    border-style: none solid solid;
    transform: translateY(-100%);
}

.overlay--notifications.open {
    transform: translateY(0);
}

/* menu: slides in from the right */
.overlay--menu {
    inset: 0 0 0 auto;
    width: calc(100% - 60px);
    max-width: 420px;
    margin: 0;

    border-style: none none none solid;
    transform: translateX(100%);
}

.overlay--menu.open {
    transform: translateX(0);
}

.overlay--menu .overlay-content {
    padding: 0 0 0 28px;
    text-align: right;
}

.overlay-handle {
    flex: 0 0 auto;

    display: flex;
    align-items: center;
    justify-content: center;

    width: 100%;
    height: 32px;

    cursor: grab;
    touch-action: none;
}

.overlay-handle::before {
    content: "";

    width: 44px;
    height: 5px;

    background: var(--text);
    border-radius: 0;
}

/* vertical handle on the inner edge of the menu */
.overlay-handle--side {
    position: absolute;
    inset: 0 auto 0 0px;

    width: 24px;
    height: auto;
}

.overlay-handle--side::before {
    width: 5px;
    height: 44px;
}

.overlay-close {
    position: absolute;
    top: 6px;
    right: 8px;

    display: grid;
    place-items: center;

    width: 32px;
    height: 32px;

    background: none;
    border-style: none;
    cursor: pointer;
}

.overlay-close .logo {
    width: 16px;
    height: 16px;
}

.overlay-close:focus-visible {
    outline: var(--border-width) solid var(--text);
    outline-offset: 2px;
}

/* Thin progress bar for HTMX requests (htmx adds .htmx-request). */
.overlay-loading {
    position: absolute;
    inset: 0 0 auto;

    height: var(--border-width);
    overflow: hidden;
    z-index: 2000;
    pointer-events: none;
}

.overlay-loading__bar {
    display: block;
    height: 100%;

    background: var(--primary);
    transform: scaleX(0);
    transform-origin: left center;
}

.overlay-loading.htmx-request .overlay-loading__bar {
    animation: overlay-loading 1.2s ease-in-out infinite;
}

@keyframes overlay-loading {
    0% {
        transform: scaleX(0);
    }

    50% {
        transform: scaleX(0.6);
    }

    100% {
        transform: scaleX(1);
    }
}

.overlay-content {
    /* auto basis so the notifications sheet can grow with its content */
    flex: 1 1 auto;
    min-height: 0;
    padding: var(--base-padding);
    overflow-y: auto;
    /* keep the native bounce, but do not chain it to the fixed shell */
    overscroll-behavior-y: contain;
}

/*
 * iOS scrolls an overflow container natively (its own scroll view, momentum,
 * bounce) only once that container has a composited layer. The overlay
 * scrollers get one from the overlay transform; these in-flow scrollports do
 * not, so they fall back to main-thread scrolling and gestures are easily
 * picked up by the fixed shell instead. Promote them to match.
 */
.page__body,
.events-grid,
.polls-grid,
.quotes-list,
.abikasse,
.index-card__body,
.profile,
.user-search__results,
.introduction,
.login-page,
.confirm-dialog {
    transform: translateZ(0);
}


/* notifications */
.notifications {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.notifications__header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 8px 12px;
}

.notifications__title {
    font-size: var(--font-size-lg);
}

.notifications__action {
    padding: 6px 10px;

    border-style: solid;
    border-radius: 0;

    background: transparent;
    color: inherit;

    font-size: var(--font-size-sm);
    cursor: pointer;
}

.notifications__action:hover {
    background: var(--text);
    color: var(--bg);
}

.notifications__action:focus-visible {
    outline: var(--border-width) solid var(--text);
    outline-offset: 2px;
}

.notifications__list {
    list-style: none;
}

.notification-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;

    padding: 12px 0;

    border-width: 0 0 var(--border-width);
    border-bottom-style: dashed;
}

.notification-item:last-child {
    padding-bottom: 0;
    border-bottom-style: none;
}

.notification-item__main {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.notification-item__link {
    color: inherit;
    text-decoration: none;
    overflow-wrap: anywhere;
}

.notification-item__link:hover {
    text-decoration: underline;
}

/* popup notifications are buttons, but must look like the text links */
.notification-item__trigger {
    padding: 0;

    background: none;
    border-style: none;

    font: inherit;
    text-align: inherit;
    cursor: pointer;
}

.notification-item__time {
    font-size: var(--font-size-sm);
    opacity: 0.7;
}


/* menu */
.menu-link {
    display: block;
    width: 100%;
    padding: 12px;

    border-width: 0 0 var(--border-width);
    border-bottom-style: dashed;
    border-radius: 0;

    background: none;
    color: inherit;
    font: inherit;
    font-size: var(--font-size-lg);
    text-align: inherit;
    text-decoration: none;
    cursor: pointer;
}

.menu-link:hover {
    opacity: 0.6;
}

.menu-link:focus-visible {
    outline: var(--border-width) solid var(--text);
    outline-offset: -2px;
}

.menu-link--disabled {
    opacity: 0.45;
    cursor: default;
}

.menu-link--disabled:hover {
    opacity: 0.45;
}

@media (max-width: 800px) {

    /* full-bleed overlays only need their top/bottom borders */
    .overlay {
        border-left-style: none;
        border-right-style: none;
    }

    /* the menu still has a gap on the left, so keep its border */
    .overlay--menu {
        border-left-style: solid;
    }
}

@media (max-width: 360px) {
    .overlay-content {
        padding: 10px;
    }
}
