/* ==========================================================================
   PulseOS Shell Layout
   6-tab top-bar navigation shell replacing the 3-hub sidebar layout.
   Uses design tokens from tokens.css.
   ========================================================================== */

/* ── WCAG AA: Skip Link ────────────────────────────────────────────────── */

.skip-link {
    position: absolute;
    left: -9999px;
    top: auto;
    width: 1px;
    height: 1px;
    overflow: hidden;
    z-index: 9999;
    padding: var(--sp-2, 0.5rem) var(--sp-4, 1rem);
    background: var(--brand-primary, #0072BC);
    color: #fff;
    font-weight: 700;
    font-size: var(--text-sm, 0.8125rem);
    text-decoration: none;
    border-radius: var(--radius-sm, 6px);
}
.skip-link:focus {
    position: fixed;
    left: var(--sp-4, 1rem);
    top: var(--sp-4, 1rem);
    width: auto;
    height: auto;
    overflow: visible;
}

/* ── WCAG AA: Focus Visible ────────────────────────────────────────────── */

*:focus-visible {
    outline: 2px solid var(--brand-primary, #0072BC);
    outline-offset: 2px;
    border-radius: 2px;
}

/* Remove default focus ring for mouse users (keep for keyboard) */
*:focus:not(:focus-visible) {
    outline: none;
}

/* Ensure interactive elements have visible focus */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[role="tab"]:focus-visible,
[role="button"]:focus-visible {
    outline: 2px solid var(--brand-primary, #0072BC);
    outline-offset: 2px;
}

/* ── Top Bar ────────────────────────────────────────────────────────────── */

.shell-topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 90px;
    display: flex;
    align-items: center;
    padding: 0 var(--sp-5, 1.25rem);
    gap: var(--sp-4, 1rem);
    background: linear-gradient(90deg, #f8fafc, #eef4ff, #f0f9ff);
    border-bottom: 2px solid #dbeafe;
    z-index: 50;
}

/* ── Logo ───────────────────────────────────────────────────────────────── */

.shell-logo {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    text-decoration: none;
    flex-shrink: 0;
}

.shell-logo__img {
    height: 81px;
    width: auto;
    object-fit: contain;
}

/* ── Tabs ───────────────────────────────────────────────────────────────── */

.shell-tabs {
    display: flex;
    gap: 2px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    justify-content: center;
}

.shell-tabs::-webkit-scrollbar {
    display: none;
}

.shell-tab {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 20px;
    text-decoration: none;
    color: #475569;
    font-size: 0.82rem;
    font-weight: 500;
    transition: background 200ms ease, color 200ms ease, box-shadow 200ms ease;
    white-space: nowrap;
    position: relative;
}

.shell-tab:hover {
    background: rgba(0, 114, 188, 0.06);
    color: #334155;
}

.shell-tab--active {
    color: #0072BC;
    background: #fff;
    font-weight: 600;
    box-shadow: 0 1px 3px rgba(0, 114, 188, 0.12);
    border: 1px solid #bfdbfe;
}

.shell-tab__icon {
    font-size: 1rem;
    line-height: 1;
    color: inherit;
}

.shell-tab__label {
    font-size: inherit;
    line-height: 1.2;
}

.shell-tab__badge {
    min-width: 18px;
    height: 18px;
    border-radius: var(--radius-full, 9999px);
    background: var(--danger, #c4321c);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    position: absolute;
    top: 0;
    right: 0;
}

/* ── Actions (right side) ───────────────────────────────────────────────── */

.shell-actions {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    margin-left: auto;
    flex-shrink: 0;
}

.shell-search-trigger {
    display: flex;
    align-items: center;
    gap: var(--sp-1);
    background: #fff;
    border: 1px solid #bfdbfe;
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 0.8rem;
    color: #64748b;
    cursor: pointer;
    transition: background 200ms ease, border-color 200ms ease;
}

.shell-search-trigger:hover {
    background: #f0f9ff;
    border-color: #93c5fd;
}

.shell-search-hint {
    font-size: 0.65rem;
    color: #94a3b8;
    opacity: 1;
    padding: 1px 5px;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
}

.shell-action-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--ink-soft);
    padding: var(--sp-2);
    border-radius: var(--radius-sm, 6px);
    transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
}

.shell-action-btn:hover {
    background: var(--bg-1);
    color: var(--ink);
}

.shell-website-label {
    font-size: 0.8rem;
    font-weight: 500;
    margin-left: var(--sp-1);
}

/* ── User Menu ──────────────────────────────────────────────────────────── */

.shell-user-menu {
    position: relative;
}

.shell-user-chip {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    background: none;
    border: none;
    cursor: pointer;
    padding: var(--sp-1);
    border-radius: var(--radius-sm, 6px);
    transition: background var(--duration) var(--ease);
}

.shell-user-chip:hover {
    background: rgba(0, 114, 188, 0.06);
}

.shell-avatar {
    width: 30px;
    height: 30px;
    border-radius: 9999px;
    background: #0072BC;
    color: #fff;
    font-weight: 600;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.shell-user-name {
    font-size: 0.82rem;
    font-weight: 500;
    color: #334155;
}

.shell-dropdown {
    position: absolute;
    right: 0;
    top: 100%;
    min-width: 180px;
    background: var(--card, #fff);
    border: 1px solid var(--line);
    border-radius: var(--radius-md, 10px);
    box-shadow: var(--glass-shadow-md);
    z-index: 100;
    padding: var(--sp-1);
    margin-top: var(--sp-1);
}

.shell-dropdown__item {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-3);
    text-decoration: none;
    color: var(--ink);
    font-size: var(--text-sm);
    border-radius: var(--radius-sm, 6px);
    border: none;
    background: none;
    cursor: pointer;
    width: 100%;
    text-align: left;
    transition: background var(--duration) var(--ease);
}

.shell-dropdown__item:hover {
    background: var(--bg-1);
}

/* ── Shell Layout ───────────────────────────────────────────────────────── */

.shell-layout {
    display: flex;
    margin-top: 90px;
    min-height: calc(100vh - 90px);
}

.shell-main {
    flex: 1;
    padding: var(--sp-6);
    min-height: calc(100vh - 60px);
    max-width: 100%;
    overflow-x: hidden;
}

.shell-footer {
    text-align: center;
    padding: var(--sp-4);
    font-size: var(--text-xs, 0.75rem);
    color: var(--ink-soft);
    margin-top: var(--sp-8);
}

/* ── Contextual Sidebar ─────────────────────────────────────────────────── */

.shell-sidebar {
    --sidebar-width: 220px;
    --sidebar-collapsed-width: 48px;
    width: var(--sidebar-width);
    min-height: calc(100vh - var(--header-height, 48px));
    background: linear-gradient(180deg, #EAF6F8, #F5F8FA);
    border-right: 1px solid #D5E8EC;
    overflow-y: auto;
    overflow-x: hidden;
    flex-shrink: 0;
    transition: width 200ms ease-out;
}

.shell-sidebar--collapsed {
    width: var(--sidebar-collapsed-width);
}

.shell-sidebar__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--bg-2);
    gap: var(--sp-2);
}

.shell-sidebar--collapsed .shell-sidebar__header {
    justify-content: center;
    padding: var(--sp-3) var(--sp-2);
}

.shell-sidebar__back {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    color: #0072BC;
    font-size: 0.8rem;
    text-decoration: none;
    transition: color var(--duration, 200ms) var(--ease);
    white-space: nowrap;
    overflow: hidden;
}

.shell-sidebar__back:hover {
    color: var(--ink);
}

.shell-sidebar__toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    color: var(--ink-soft);
    cursor: pointer;
    border-radius: var(--radius-sm, 6px);
    flex-shrink: 0;
    transition: background var(--duration, 200ms) var(--ease),
                color var(--duration, 200ms) var(--ease);
}

.shell-sidebar__toggle:hover {
    background: var(--bg-2);
    color: var(--ink);
}

.shell-sidebar__nav {
    padding: var(--sp-2) 0;
}

.shell-sidebar__link {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-4);
    color: #475569;
    text-decoration: none;
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    transition: background var(--duration, 200ms) var(--ease),
                color var(--duration, 200ms) var(--ease);
}

.shell-sidebar--collapsed .shell-sidebar__link {
    justify-content: center;
    padding: var(--sp-2) var(--sp-2);
}

.shell-sidebar__link:hover {
    background: #fff;
    color: var(--ink);
}

.shell-sidebar__link--active {
    background: #fff;
    border-left: 2px solid #27A9B8;
    box-shadow: 0 1px 3px rgba(0, 59, 122, 0.06);
    color: #003B7A;
    font-weight: 600;
}

.shell-sidebar__link-icon {
    font-size: 1rem;
    flex-shrink: 0;
    width: 1.1em;
    text-align: center;
}

.shell-sidebar__link-label {
    overflow: hidden;
    text-overflow: ellipsis;
}

.shell-sidebar__badge {
    margin-left: auto;
    min-width: 18px;
    height: 18px;
    border-radius: var(--radius-full, 9999px);
    background: var(--danger, #c4321c);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    flex-shrink: 0;
}

.shell-sidebar__fav-icon {
    color: var(--warning, #f59e0b);
}

.shell-sidebar__link--compact {
    font-size: 0.75rem;
    padding: var(--sp-1) var(--sp-4) var(--sp-1) var(--sp-6);
}

.shell-sidebar--collapsed .shell-sidebar__link--compact {
    padding: var(--sp-1) var(--sp-2);
}

.shell-sidebar__section {
    border-top: 1px solid var(--bg-2);
    padding: var(--sp-2) 0;
}

.shell-sidebar__section-title {
    font-size: 0.7rem;
    font-weight: 600;
    color: #0072BC;
    padding: var(--sp-2) var(--sp-4);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0;
}

/* ── Dark Mode Overrides ────────────────────────────────────────────────── */

/* ── Responsive: Tablet (768-1023px) ───────────────────────────────────── */

@media (max-width: 1023px) and (min-width: 768px) {
    .shell-tab__label {
        display: none;
    }

    .shell-tab {
        padding: var(--sp-2);
    }

    .shell-tab__icon {
        font-size: 1.25rem;
    }

    .shell-search-trigger {
        max-width: 160px;
    }

    .shell-search-hint {
        display: none;
    }

    .shell-sidebar {
        position: fixed;
        left: 0;
        top: var(--header-height, 48px);
        bottom: 0;
        z-index: 40;
        transform: translateX(-100%);
        transition: transform var(--duration) var(--ease);
        box-shadow: var(--glass-shadow-lg);
    }

    .shell-sidebar--open {
        transform: translateX(0);
    }

    .shell-main {
        padding: var(--sp-4) var(--sp-5);
    }
}

/* ── Responsive: Mobile (<768px) ──────────────────────────────────────── */

@media (max-width: 767px) {
    .shell-topbar {
        padding: 0 var(--sp-2);
        gap: var(--sp-2);
        flex-wrap: nowrap;
    }

    /* Hide desktop tabs — replaced by bottom bar */
    .shell-tabs {
        display: none;
    }

    .shell-search-trigger {
        padding: var(--sp-2);
    }

    .shell-search-trigger .shell-search-hint,
    .shell-search-trigger span:not(.shell-search-hint) {
        display: none;
    }

    .shell-search-hint {
        display: none;
    }

    .shell-user-name,
    .shell-website-label {
        display: none;
    }

    .shell-sidebar {
        position: fixed;
        left: 0;
        top: var(--header-height, 48px);
        bottom: 56px; /* above bottom tab bar */
        z-index: 40;
        transform: translateX(-100%);
        transition: transform var(--duration) var(--ease);
        box-shadow: var(--glass-shadow-lg);
    }

    .shell-sidebar--open {
        transform: translateX(0);
    }

    .shell-main {
        padding: var(--sp-3);
        padding-bottom: 72px; /* space for bottom tab bar */
    }

    .shell-footer {
        padding-bottom: 72px;
    }

    /* ── Mobile Bottom Tab Bar ── */
    .shell-bottom-tabs {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 56px;
        background: var(--surface, rgba(255, 255, 255, 0.95));
        border-top: 1px solid var(--bg-2, #e4edf5);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        z-index: 50;
        justify-content: space-around;
        align-items: center;
        padding: 0 var(--sp-1);
    }

    .shell-bottom-tabs .shell-tab {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: var(--sp-1) 0;
        min-width: 0;
        border-radius: 0;
    }

    .shell-bottom-tabs .shell-tab__label {
        display: block;
        font-size: 0.6rem;
        line-height: 1.2;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 100%;
    }

    .shell-bottom-tabs .shell-tab__icon {
        font-size: 1.2rem;
    }
}

/* ── More trigger / popup (mobile bottom nav) ─────────────────────── */

.shell-more-trigger { position: relative; cursor: pointer; }
.shell-more-popup { position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%); background: var(--card, #fff); border: 1px solid var(--bg-2); border-radius: 8px; box-shadow: 0 -4px 12px rgba(0,0,0,0.15); padding: var(--sp-2); min-width: 160px; margin-bottom: var(--sp-2); }
.shell-more-popup__item { display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2) var(--sp-3); border-radius: 6px; text-decoration: none; color: var(--ink); font-size: 0.85rem; }
.shell-more-popup__item:hover { background: var(--bg-1); }
.shell-more-popup__item--active { color: var(--brand-primary); font-weight: 600; }

/* Hide bottom tabs on desktop/tablet */
@media (min-width: 768px) {
    .shell-bottom-tabs {
        display: none;
    }
}

/* ── Dark mode: bottom tab bar ─────────────────────────────────────────── */

/* ── Messenger widget: mobile collapse to FAB ──────────────────────────── */

@media (max-width: 767px) {
    .messenger-panel {
        bottom: 72px; /* above bottom tab bar */
        max-height: calc(100vh - 128px); /* header + bottom bar */
    }

    .messenger-fab {
        bottom: 68px; /* above bottom tab bar */
    }

    .cp-dialog {
        top: 5%;
        width: 95%;
        max-height: 80vh;
    }

    .cp-results {
        max-height: 280px;
    }
}

/* ── Command Palette ────────────────────────────────────────────────────── */

.cp-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.cp-dialog {
    position: fixed;
    top: 15%;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    max-width: 640px;
    background: var(--card, #fff);
    border-radius: var(--radius-lg, 14px);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    overflow: hidden;
}

.cp-search {
    display: flex;
    align-items: center;
    padding: var(--sp-4);
    border-bottom: 1px solid var(--bg-2);
    gap: var(--sp-3);
}

.cp-search-icon {
    color: var(--ink-soft);
    font-size: 1.2rem;
    flex-shrink: 0;
}

.cp-search-input {
    flex: 1;
    border: none;
    outline: none;
    font-size: 1rem;
    background: transparent;
    color: var(--ink);
    font-family: var(--font-body);
}

.cp-search-input::placeholder {
    color: var(--ink-soft);
}

.cp-kbd {
    font-size: 0.7rem;
    padding: 2px 6px;
    background: var(--bg-1);
    border: 1px solid var(--bg-2);
    border-radius: 4px;
    color: var(--ink-soft);
    font-family: var(--font-mono);
    line-height: 1.4;
}

.cp-tabs {
    display: flex;
    gap: var(--sp-1);
    padding: var(--sp-2) var(--sp-4);
    border-bottom: 1px solid var(--bg-2);
}

.cp-tab {
    background: none;
    border: none;
    padding: var(--sp-1) var(--sp-3);
    border-radius: var(--radius-sm, 6px);
    font-size: 0.8rem;
    color: var(--ink-soft);
    cursor: pointer;
    transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
    font-family: var(--font-body);
}

.cp-tab:hover {
    background: var(--bg-1);
    color: var(--ink);
}

.cp-tab--active {
    background: var(--info-bg);
    color: var(--brand-primary);
    font-weight: 600;
}

.cp-results {
    max-height: 400px;
    overflow-y: auto;
    padding: var(--sp-2) 0;
}

.cp-section-title {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--ink-soft);
    padding: var(--sp-2) var(--sp-4);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.cp-result {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-2) var(--sp-4);
    text-decoration: none;
    color: var(--ink);
    font-size: 0.85rem;
    cursor: pointer;
    transition: background var(--duration) var(--ease);
}

.cp-result:hover,
.cp-result--active {
    background: var(--bg-1);
}

.cp-result__type {
    font-size: 0.7rem;
    padding: 2px 6px;
    background: var(--bg-1);
    border-radius: 4px;
    color: var(--ink-soft);
    flex-shrink: 0;
}

.cp-result__label {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.cp-empty {
    text-align: center;
    padding: var(--sp-6);
    color: var(--ink-soft);
    font-size: 0.9rem;
}

.cp-footer {
    display: flex;
    gap: var(--sp-4);
    padding: var(--sp-3) var(--sp-4);
    border-top: 1px solid var(--bg-2);
    font-size: 0.75rem;
    color: var(--ink-soft);
}

/* Dark mode overrides for command palette */

/* ── Print ──────────────────────────────────────────────────────────────── */

@media print {
    .shell-topbar,
    .shell-sidebar,
    .shell-footer {
        display: none;
    }

    .shell-layout {
        margin-top: 0;
    }

    .shell-main {
        padding: 0;
    }
}

/* ==========================================================================
   RTL Support — Arabic (dir="rtl")
   Flips directional properties for the shell layout.
   ========================================================================== */

html[dir="rtl"] .shell-topbar {
    direction: rtl;
}
html[dir="rtl"] .shell-logo {
    margin-right: 0;
    margin-left: auto;
}
html[dir="rtl"] .shell-actions {
    margin-left: 0;
    margin-right: auto;
}
html[dir="rtl"] .shell-user-name {
    margin-left: 0;
    margin-right: var(--sp-1, 0.25rem);
}
html[dir="rtl"] .shell-search-hint {
    margin-left: 0;
    margin-right: var(--sp-1, 0.25rem);
}

/* Sidebar RTL */
html[dir="rtl"] .shell-sidebar {
    left: auto;
    right: 0;
    border-left: 1px solid var(--line, rgba(0,59,122,0.1));
    border-right: none;
}
html[dir="rtl"] .shell-sidebar__link {
    border-left: none;
    border-right: 2px solid transparent;
    text-align: right;
}
html[dir="rtl"] .shell-sidebar__link--active {
    border-right-color: var(--brand-teal, #27A9B8);
    border-left-color: transparent;
}

/* Dropdown RTL */
html[dir="rtl"] .shell-dropdown {
    left: 0;
    right: auto;
}
html[dir="rtl"] .notification-dropdown {
    left: 0;
    right: auto;
}

/* Content area RTL */
html[dir="rtl"] .shell-main {
    direction: rtl;
    text-align: right;
}

/* Bottom tabs RTL */
html[dir="rtl"] .shell-bottom-tabs {
    direction: rtl;
}
html[dir="rtl"] .shell-more-popup {
    left: auto;
    right: 50%;
    transform: translateX(50%);
}

/* Empty state RTL */
html[dir="rtl"] .ed-empty-state {
    direction: rtl;
    text-align: center;
}
html[dir="rtl"] .ed-empty-state__clause {
    direction: rtl;
}

/* KPI card RTL */
html[dir="rtl"] .kpi-card {
    direction: rtl;
}

/* Font override for Arabic */
html[dir="rtl"] body,
html[dir="rtl"] .shell-tab__label,
html[dir="rtl"] .shell-user-name,
html[dir="rtl"] .shell-sidebar__label,
html[dir="rtl"] .shell-dropdown__item {
    font-family: var(--font-ar, 'IBM Plex Sans Arabic', 'DM Sans', sans-serif);
}
