/* =================================================================
 * Phase 35 — polish bundle: RTL + dark mode + a11y + mobile.
 * Loaded globally via fragments/layout.html.
 * ================================================================= */


/* ===== 35-1 — RTL =====================================================
 * Templates set dir="rtl" on <html> when locale is Arabic. Most flex/grid
 * containers already flip automatically because they default to
 * `flex-direction: row` (which respects direction); these rules patch
 * the spots that use absolute positioning, fixed margins, or text-align.
 * ===================================================================== */
[dir="rtl"] body { text-align: right; }
[dir="rtl"] .marketing-header,
[dir="rtl"] .app-topbar,
[dir="rtl"] .admin-topbar { direction: rtl; }
/* Avatars, icon chips, and toolbars stay LTR so emoji + initials don't
   mirror unexpectedly. */
[dir="rtl"] .avatar,
[dir="rtl"] .logo-mark,
[dir="rtl"] .tab-badge,
[dir="rtl"] code,
[dir="rtl"] pre,
[dir="rtl"] .admin-pill--mono { direction: ltr; unicode-bidi: isolate; }
/* Sidebar nav: flip so the active-state border lands on the inner edge. */
[dir="rtl"] .app-sidebar .nav-link,
[dir="rtl"] .admin-nav-link { text-align: right; }
[dir="rtl"] .app-sidebar .nav-link.active,
[dir="rtl"] .admin-nav-link.active {
    border-left: 0;
    border-right: 3px solid var(--color-accent, #f59e0b);
}
/* Form rows that used margin-left for icon offsets — flip to logical. */
[dir="rtl"] .form-row > *,
[dir="rtl"] .admin-form > * { margin-inline-start: 0; }


/* ===== 35-2 — Dark mode ==============================================
 * Driven by [data-theme="dark"] on <html> OR @media when no theme cookie.
 * Existing palette in app.css uses hard-coded hex values; we override the
 * key surfaces here with logical tokens so component CSS still works.
 * ===================================================================== */
:root {
    --gs-bg:        #ffffff;
    --gs-bg-soft:   #f7f8fb;
    --gs-fg:        #1a1f36;
    --gs-fg-muted:  #6b7280;
    --gs-border:    #e5e7eb;
    --gs-card:      #ffffff;
    --gs-accent:    #f59e0b;
    --gs-link:      #2563eb;
}
[data-theme="dark"] {
    --gs-bg:        #0b1020;
    --gs-bg-soft:   #131a2e;
    --gs-fg:        #e7eaf3;
    --gs-fg-muted:  #9aa3b2;
    --gs-border:    #2a3350;
    --gs-card:      #1a223a;
    --gs-accent:    #fbbf24;
    --gs-link:      #60a5fa;
    color-scheme:   dark;
}
/* Auto-dark when no manual choice and the OS asks for dark. */
@media (prefers-color-scheme: dark) {
    html:not([data-theme]) {
        --gs-bg:        #0b1020;
        --gs-bg-soft:   #131a2e;
        --gs-fg:        #e7eaf3;
        --gs-fg-muted:  #9aa3b2;
        --gs-border:    #2a3350;
        --gs-card:      #1a223a;
        --gs-accent:    #fbbf24;
        --gs-link:      #60a5fa;
        color-scheme:   dark;
    }
}

/* Apply tokens to the broadest surfaces. App.css inline styles will still
   leak — these target the structural shell + cards so a dark-theme view
   doesn't look broken. */
[data-theme="dark"] body,
@media (prefers-color-scheme: dark) {
    html:not([data-theme]) body {
        background: var(--gs-bg);
        color: var(--gs-fg);
    }
}
[data-theme="dark"] body { background: var(--gs-bg); color: var(--gs-fg); }
[data-theme="dark"] .app-main,
[data-theme="dark"] .workspace-body,
[data-theme="dark"] .container { background: var(--gs-bg); color: var(--gs-fg); }
[data-theme="dark"] .app-sidebar,
[data-theme="dark"] .admin-sidebar { background: var(--gs-bg-soft); border-color: var(--gs-border); }
[data-theme="dark"] .marketing-header,
[data-theme="dark"] .app-topbar,
[data-theme="dark"] .admin-topbar { background: var(--gs-bg-soft); border-bottom-color: var(--gs-border); }
[data-theme="dark"] .group-card,
[data-theme="dark"] .community-post,
[data-theme="dark"] .admin-card,
[data-theme="dark"] .note-card,
[data-theme="dark"] .analytics-card { background: var(--gs-card); border-color: var(--gs-border); color: var(--gs-fg); }
[data-theme="dark"] .muted,
[data-theme="dark"] .admin-fg-muted { color: var(--gs-fg-muted); }
[data-theme="dark"] a { color: var(--gs-link); }
[data-theme="dark"] input,
[data-theme="dark"] select,
[data-theme="dark"] textarea {
    background: var(--gs-card);
    color: var(--gs-fg);
    border-color: var(--gs-border);
}

/* Theme switcher button in the header. */
.theme-switcher {
    background: transparent;
    border: 1px solid var(--gs-border, #e5e7eb);
    border-radius: 999px;
    width: 32px; height: 32px;
    cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 16px;
    color: inherit;
}
.theme-switcher:hover { background: var(--gs-bg-soft, #f7f8fb); }


/* ===== 35-3 — Accessibility =========================================
 * - Skip-to-content link (visible on focus only)
 * - :focus-visible outline ring for every interactive control
 * - aria-only utility (visually hidden but accessible)
 * - prefers-reduced-motion neutralises any duration > 0
 * ===================================================================== */
.skip-link {
    position: absolute;
    top: 0; left: 0;
    padding: 10px 16px;
    background: #1a1f36;
    color: #fff;
    font-weight: 700;
    text-decoration: none;
    z-index: 9999;
    transform: translateY(-150%);
    transition: transform .15s ease-out;
}
.skip-link:focus { transform: translateY(0); }

:where(button, a, input, select, textarea, [role="button"], [tabindex]):focus-visible {
    outline: 2px solid var(--gs-accent, #f59e0b);
    outline-offset: 2px;
    border-radius: 4px;
}

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

@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;
    }
}


/* ===== 35-4 — Keyboard shortcut help dialog =========================
 * Shown via /js/keybindings.js when the user presses '?'.
 * ===================================================================== */
.kbd-dialog {
    position: fixed;
    top: 50%; left: 50%; transform: translate(-50%, -50%);
    background: var(--gs-card, #fff);
    color: var(--gs-fg, #1a1f36);
    border: 1px solid var(--gs-border, #e5e7eb);
    border-radius: 12px;
    padding: 24px 28px;
    box-shadow: 0 20px 50px rgba(0,0,0,.25);
    z-index: 10000;
    min-width: 320px;
    max-width: 480px;
}
.kbd-dialog h2 { margin: 0 0 14px; font-size: 17px; }
.kbd-dialog__row {
    display: flex; justify-content: space-between; align-items: center;
    padding: 6px 0;
}
.kbd-dialog__keys { font-family: 'JetBrains Mono', monospace; font-size: 13px; }
.kbd-key {
    display: inline-block;
    padding: 2px 8px;
    background: var(--gs-bg-soft, #f3f4f6);
    border: 1px solid var(--gs-border, #d1d5db);
    border-radius: 4px;
    box-shadow: 0 1px 0 var(--gs-border, #d1d5db);
    font-size: 12px;
}
.kbd-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 9999;
}


/* ===== 36 — Mobile shell rebuild ===================================
 * Hamburger drawer (≤960px), topbar density, profile-page polish.
 * The actual `display: none` of `.app-sidebar` at ≤960px lives in
 * app.css; this section adds the new elements introduced by Phase 36.
 * ===================================================================== */

/* Hamburger button — hidden ≥961px so the desktop sidebar is the only nav. */
.app-topbar__menu {
    display: none;
    background: transparent;
    border: 0;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    padding: 4px 10px;
    color: inherit;
    border-radius: 6px;
}
.app-topbar__menu:hover { background: var(--gs-bg-soft, #f3f4f6); }
@media (max-width: 960px) {
    .app-topbar__menu { display: inline-flex; align-items: center; }
}

/* Drawer + backdrop — off-screen by default. CSS transitions in/out. */
.mobile-drawer__backdrop {
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, .45);
    z-index: 9000;
    opacity: 0;
    transition: opacity .2s ease-out;
}
.mobile-drawer__backdrop.is-open { opacity: 1; }
.mobile-drawer {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(82vw, 320px);
    background: var(--gs-card, #fff);
    color: var(--gs-fg, #1a1f36);
    box-shadow: 4px 0 24px rgba(0, 0, 0, .18);
    z-index: 9001;
    transform: translateX(-100%);
    transition: transform .22s ease-out;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.mobile-drawer.is-open { transform: translateX(0); }
/* RTL: drawer slides in from the right instead of the left. */
[dir="rtl"] .mobile-drawer {
    left: auto; right: 0;
    transform: translateX(100%);
    box-shadow: -4px 0 24px rgba(0, 0, 0, .18);
}
[dir="rtl"] .mobile-drawer.is-open { transform: translateX(0); }

.mobile-drawer__head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--gs-border, #e5e7eb);
}
.mobile-drawer__close {
    background: transparent; border: 0;
    font-size: 18px; cursor: pointer;
    padding: 6px 10px; border-radius: 6px;
    color: inherit;
}
.mobile-drawer__close:hover { background: var(--gs-bg-soft, #f3f4f6); }
.mobile-drawer__body {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 10px 12px 90px; /* extra bottom-padding clears the bottom-nav */
}
.mobile-drawer__body .nav-section { margin-bottom: 14px; }
.mobile-drawer__body .nav-section__label {
    font-size: 11px; font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--gs-fg-muted, #6b7280);
    padding: 8px 12px 4px;
}
.mobile-drawer__body .nav-link {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 12px;
    border-radius: 8px;
    color: inherit;
    text-decoration: none;
    font-size: 15px;
}
.mobile-drawer__body .nav-link:hover { background: var(--gs-bg-soft, #f3f4f6); }
.mobile-drawer__body .nav-link__icon { font-size: 16px; width: 20px; text-align: center; }
/* Lock scroll under the open drawer. */
body.drawer-open { overflow: hidden; }

/* Topbar density tweaks at ≤960px — the search field shrinks to leave
   room for the hamburger + bell + theme toggle. */
@media (max-width: 960px) {
    .app-topbar { gap: 6px; padding: 10px 12px; }
    .app-topbar .header-search,
    .app-topbar [class*="search"] { flex: 1 1 auto; min-width: 0; }
    .app-topbar input[type="search"] { width: 100% !important; font-size: 14px; }
}

/* Profile-page polish at narrow widths — before: "Aminur" / "R." on
   separate lines + multi-line "@aminur · UTC- · 5" pile because the
   container narrowed faster than the text could fit. */
@media (max-width: 760px) {
    .profile-head {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }
    .profile-head__meta { width: 100%; }
    .profile-head__name { font-size: 22px; line-height: 1.2; }
    /* Force the first span (the display name) to stay on one line. */
    .profile-head__name > span:first-child { white-space: nowrap; }
    /* The meta lines are plain <div class="muted"> blocks inside .profile-head__meta;
       overflow-wrap keeps long @handles / timezones from punching out. */
    .profile-head__meta .muted {
        font-size: 13px;
        overflow-wrap: anywhere;
    }
    .profile-head__actions {
        flex-wrap: wrap;
        gap: 8px;
        width: 100%;
        margin-top: 4px;
    }
    .avatar-lg { width: 64px; height: 64px; font-size: 22px; }
}


/* ===== 35-5 — Mobile workspace v2 ===================================
 * Sticky bottom tab bar replaces the horizontal tab strip on phones.
 * Touch-friendly hit targets (≥44×44). Safe-area inset for iOS notch.
 * ===================================================================== */
@media (max-width: 640px) {
    /* Sticky bottom tab strip on the workspace. */
    .workspace .tabs {
        position: fixed;
        bottom: 0; left: 0; right: 0;
        background: var(--gs-card, #fff);
        border-top: 1px solid var(--gs-border, #e5e7eb);
        padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
        z-index: 100;
        justify-content: space-around;
        margin: 0;
        overflow-x: visible;
    }
    .workspace .tabs a {
        flex: 1 1 0;
        text-align: center;
        font-size: 12px;
        padding: 8px 4px;
        min-height: 44px;
        border-bottom: 0;
    }
    .workspace .tabs a.active {
        background: var(--gs-bg-soft, #f7f8fb);
        border-radius: 8px;
        color: var(--gs-accent, #f59e0b);
    }
    .workspace-body { padding-bottom: 80px; }
    /* Chat composer respects iOS notch. */
    .chat-composer,
    .community-compose {
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }
}
