.app-header {
    background: var(--viewer-header-bg);
    padding: 20px;
    border-bottom: 2px solid var(--viewer-accent);
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    position: relative;
    overflow: hidden;
}


.app-header .header-top {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas: "menu title settings";
    align-items: center;
    gap: 12px;
}

.app-header .more-menu-btn {
    grid-area: menu;
    width: 36px;
    height: 36px;
    padding: 0;
    border-radius: 8px;
    border: 1px solid var(--viewer-accent-border);
    background: var(--viewer-accent-tint);
    color: var(--viewer-accent-text);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1em;
}

.app-header h1 {
    grid-area: title;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 1.5em;
    font-weight: 800;
    background: var(--viewer-heading-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-transform: uppercase;
    letter-spacing: 0.12em;
}

.app-header .settings-btn {
    grid-area: settings;
    width: 36px;
    height: 36px;
    padding: 0;
    border-radius: 8px;
    border: 1px solid var(--viewer-accent-border);
    background: var(--viewer-accent-tint);
    color: var(--viewer-accent-text);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1em;
}

.app-header .settings-btn:hover {
    border-color: var(--viewer-accent-strong);
    background: var(--viewer-accent-soft);
}

.app-header .more-menu-btn:hover {
    border-color: var(--viewer-accent-strong);
    background: var(--viewer-accent-soft);
}

.app-header .profile-controls select {
    padding: 8px 12px;
    border: 1px solid var(--viewer-accent);
    border-radius: 6px;
    background: var(--viewer-input-bg);
    color: var(--viewer-input-text);
    font-size: 0.9em;
    cursor: pointer;
}

@media (max-width: 720px) {
    .app-header .header-top {
        grid-template-columns: auto minmax(0, 1fr) auto;
        grid-template-areas: "menu title settings";
        justify-items: end;
    }

    .app-header h1 {
        width: auto;
        justify-self: start;
        text-align: left;
    }

    .app-header .more-menu-btn {
        justify-self: start;
    }

    .app-header .settings-btn {
        justify-self: end;
    }
}

.app-header .header-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    flex-wrap: wrap;
    align-self: flex-end;
}

.app-header .action-stack {
    display: flex;
    align-items: center;
    gap: 8px;
}

.app-header .profile-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: nowrap;
}

.app-header .profile-controls label {
    font-size: 0.85em;
    color: var(--viewer-text-soft);
}

.app-header .profile-controls button {
    border: 1px solid var(--viewer-accent-border);
    background: var(--viewer-accent-tint);
    color: var(--viewer-accent-text);
    padding: 6px 10px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.85em;
    transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.app-header .profile-controls button:hover {
    border-color: var(--viewer-accent-strong);
    background: var(--viewer-accent-soft);
}

.app-header .profile-controls .icon-button {
    width: 36px;
    height: 36px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1em;
}

.app-header .nav-link {
    padding: 6px 12px;
    border-radius: 8px;
    border: 1px solid var(--viewer-accent-border);
    color: var(--viewer-accent);
    text-decoration: none;
    font-size: 0.85em;
    transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.app-header .nav-link:hover {
    background: var(--viewer-accent-tint);
}

.visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

.more-menu-btn {
    font-size: 1.1em;
    letter-spacing: 1px;
}

.menu-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(7, 10, 18, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 1200;
}

.menu-backdrop.show {
    opacity: 1;
    pointer-events: auto;
}

.menu-drawer {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(280px, 85vw);
    background: var(--viewer-dialog-bg);
    border-right: 1px solid var(--viewer-dialog-border);
    box-shadow: 10px 0 30px rgba(0, 0, 0, 0.3);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    display: flex;
    flex-direction: column;
    padding: 16px;
    overscroll-behavior: contain;
}

.menu-backdrop.show .menu-drawer {
    transform: translateX(0);
}

.settings-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(7, 10, 18, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 1200;
}

.settings-backdrop.show {
    opacity: 1;
    pointer-events: auto;
}

.settings-drawer {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: min(320px, 85vw);
    background: var(--viewer-dialog-bg);
    border-left: 1px solid var(--viewer-dialog-border);
    box-shadow: -10px 0 30px rgba(0, 0, 0, 0.3);
    transform: translateX(100%);
    transition: transform 0.25s ease;
    display: flex;
    flex-direction: column;
    padding: 16px;
    overscroll-behavior: contain;
}

.settings-backdrop.show .settings-drawer {
    transform: translateX(0);
}

.settings-section {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}

.settings-section label {
    font-size: 0.85em;
    color: var(--viewer-text-soft);
}

.settings-toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.settings-toggle-row label {
    margin: 0;
}

.settings-toggle {
    position: relative;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
}

.settings-toggle input {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
}

.settings-toggle .toggle-slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: #ccc;
    border-radius: 24px;
    transition: background 0.2s;
}

.settings-toggle .toggle-slider::before {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    left: 3px;
    bottom: 3px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.2s;
}

.settings-toggle input:checked + .toggle-slider {
    background: var(--viewer-accent, #2196F3);
}

.settings-toggle input:checked + .toggle-slider::before {
    transform: translateX(20px);
}

.settings-section select {
    padding: 8px 12px;
    border: 1px solid var(--viewer-accent);
    border-radius: 6px;
    background: var(--viewer-input-bg);
    color: var(--viewer-input-text);
    font-size: 0.9em;
    cursor: pointer;
}

.profile-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.profile-row,
.settings-section .auth-btn-google {
    width: 100%;
}

.profile-current-label {
    font-size: 0.85em;
    color: var(--viewer-text-soft);
    white-space: nowrap;
}

.profile-row select {
    flex: 1;
}

.profile-action-bar {
    display: flex;
    gap: 6px;
    justify-content: flex-end;
}

.profile-icon-btn {
    width: 28px;
    height: 28px;
    border-radius: 6px;
    border: 1px solid var(--viewer-accent-border);
    background: var(--viewer-accent-tint);
    color: var(--viewer-accent-text);
    font-size: 0.9em;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.profile-icon-btn:hover {
    border-color: var(--viewer-accent-strong);
    background: var(--viewer-accent-soft);
}

.settings-action-btn {
    border: 1px solid var(--viewer-accent-border);
    background: var(--viewer-accent-tint);
    color: var(--viewer-accent-text);
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9em;
    text-align: left;
}

.settings-action-btn:hover {
    border-color: var(--viewer-accent-strong);
    background: var(--viewer-accent-soft);
}

.menu-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.menu-title {
    font-weight: 700;
    color: var(--viewer-dialog-text);
}

.menu-close {
    border: 1px solid var(--viewer-dialog-btn-border);
    background: var(--viewer-dialog-btn-bg);
    color: var(--viewer-dialog-btn-text);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
}

.menu-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.menu-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.menu-section-title {
    font-size: 0.75rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--viewer-dialog-muted);
    margin-top: 6px;
}

.menu-item {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: 1px solid var(--viewer-dialog-btn-border);
    background: var(--viewer-dialog-btn-bg);
    color: var(--viewer-dialog-btn-text);
    padding: 10px 12px;
    border-radius: 8px;
    text-align: left;
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
    text-decoration: none;
    font: inherit;
}

a.menu-item:visited {
    color: var(--viewer-dialog-btn-text);
}

.menu-item:hover {
    border-color: var(--viewer-accent-strong);
    background: var(--viewer-accent-soft);
}

.menu-icon {
    font-size: 1rem;
}

.menu-text {
    display: flex;
    flex-direction: column;
    line-height: 1.1;
}

.menu-subtitle {
    font-size: 0.8rem;
    opacity: 0.7;
}

.menu-section-zh-only {
    display: none;
}

:lang(zh-TW) .menu-section-zh-only,
:lang(zh-HK) .menu-section-zh-only {
    display: flex;
}

.menu-qr-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 12px;
    border: 1px solid var(--viewer-dialog-btn-border);
    background: var(--viewer-dialog-btn-bg);
    border-radius: 8px;
}

.menu-qr-img {
    width: 160px;
    height: 160px;
    border-radius: 6px;
    object-fit: contain;
}

.menu-qr-label {
    font-size: 0.85rem;
    color: var(--viewer-dialog-btn-text);
    opacity: 0.8;
}

.back-to-top-btn {
    position: fixed;
    left: 16px;
    bottom: 16px;
    z-index: 30;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--viewer-card-border, #e2e8f0);
    background: var(--viewer-card-bg, #fff);
    color: var(--viewer-text, #0f172a);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1em;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.15s;
}

@media (hover: hover) {
    .back-to-top-btn:hover {
        transform: translateY(-2px);
        border-color: var(--viewer-accent, #0088ee);
    }
}

.back-to-top-btn.is-hidden {
    opacity: 0;
    pointer-events: none;
}

@media (max-width: 768px) {
    .app-header {
        padding: 12px 15px;
    }

    .app-header h1 {
        font-size: 1.1em;
        letter-spacing: 0.05em;
    }

    .app-header .profile-controls select {
        padding: 6px 10px;
        font-size: 0.85em;
    }

    .app-header .header-actions {
        width: 100%;
        align-items: flex-end;
        justify-content: flex-end;
        flex-wrap: wrap;
        align-self: stretch;
    }

    .app-header .profile-controls {
        width: auto;
        justify-content: flex-start;
    }

    .app-header .profile-controls select,
    .app-header .profile-controls button {
        flex: 1 1 140px;
    }

    .app-header .profile-controls .icon-button {
        flex: 0 0 auto;
        width: 40px;
    }
}

/* Focus-visible styles for interactive elements */
.menu-item:focus-visible,
.icon-button:focus-visible,
.menu-close:focus-visible,
.profile-icon-btn:focus-visible {
    outline: 2px solid var(--viewer-accent, #0077cc);
    outline-offset: 2px;
}

/* Reduced motion: disable animations/transitions for users who prefer it */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}