/* ==========================================================================
   App shell: the left menu, the top bar, the notification bell and the work area.
   One file, no !important. Values come from tokens.css.

   Class names are a contract with the page scripts (app.js, shell.js, notifications.js,
   my_leave.js): do not rename them without changing those scripts.
     .app-shell.sidebar-collapsed  desktop icon rail (toggleSidebarMobile in app.js)
     #appSidebar.open              phone drawer
     .sidebar-link.active          current screen (switchAppView sets it, with aria-current)
     .nav-badge                    count on a menu item (shown/hidden by the scripts with style.display)
     .notif-*                      the bell and its dropdown (the items are drawn by notifications.js)
   ========================================================================== */
@layer app-layout {

    .app-shell {
        --shell-sidebar: 228px;
        --shell-rail: 68px;
        --shell-topbar: 52px;
        display: flex;
        width: 100%;
        min-height: 100vh;
        background: var(--d26-surface);
        color: var(--d26-ink);
    }

    /* --- Left menu -------------------------------------------------------------- */
    .sidebar {
        position: sticky;
        top: 0;
        z-index: 50;
        display: flex;
        flex: none;
        flex-direction: column;
        width: var(--shell-sidebar);
        min-width: var(--shell-sidebar);
        height: 100vh;
        background: #fff;
        border-right: 1px solid var(--d26-line);
        transition: width 0.2s ease, min-width 0.2s ease, transform 0.25s ease-in-out;
    }
    .sidebar-brand {
        display: flex;
        align-items: center;
        gap: 10px;
        height: var(--shell-topbar);
        padding: 0 18px;
        box-sizing: border-box;
        border-bottom: 1px solid var(--d26-line);
    }
    .brand-icon-box {
        display: flex;
        flex: none;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        border-radius: 10px;
        background: var(--d26-accent);
        color: #fff;
        font-size: 0.95rem;
    }
    .brand-title { margin: 0; color: var(--d26-ink); font-size: 0.95rem; font-weight: 700; line-height: 1.2; }
    .brand-subtitle { margin: 1px 0 0; color: var(--d26-ink-muted); font-size: 0.75rem; }

    .sidebar-nav-container {
        flex: 1;
        display: flex;
        flex-direction: column;
        gap: 4px;
        padding: 8px 12px 12px;
        overflow-y: auto;
        overscroll-behavior: contain;
        scrollbar-width: thin;
    }
    .sidebar-nav-group { display: flex; flex-direction: column; gap: 2px; }
    .sidebar-group-title {
        padding: 16px 12px 6px;
        color: var(--d26-ink-muted);
        font-size: 0.75rem;
        font-weight: 600;
        letter-spacing: 0.06em;
        text-transform: uppercase;
    }

    .sidebar-link {
        position: relative;
        display: flex;
        align-items: center;
        gap: 12px;
        width: 100%;
        height: var(--d26-target);
        padding: 0 12px;
        box-sizing: border-box;
        background: transparent;
        border: 0;
        border-radius: 10px;
        color: #374151;
        font: inherit;
        font-size: 0.875rem;
        font-weight: 500;
        text-align: left;
        cursor: pointer;
        transition: background 0.12s ease, color 0.12s ease;
    }
    .sidebar-link i {
        flex: none;
        width: 20px;
        color: var(--d26-ink-muted);
        font-size: 1rem;
        text-align: center;
    }
    .sidebar-link-label { flex: 1 1 auto; overflow: hidden; text-align: left; text-overflow: ellipsis; white-space: nowrap; }
    .sidebar-link:hover { background: #f3f4f6; }
    .sidebar-link.active { background: var(--d26-accent-soft); color: var(--d26-accent); font-weight: 600; }
    .sidebar-link.active i { color: var(--d26-accent); }

    /* A count on a menu item (also used inside a tab on the My leave screen) */
    .nav-badge,
    .nav-badge-purple {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 22px;
        height: 20px;
        margin-left: auto;
        padding: 0 8px;
        border-radius: 999px;
        background: var(--d26-wait-bg);
        color: #78350f;
        font-size: 0.75rem;
        font-weight: 700;
    }

    .sidebar-footer {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        padding: 12px 14px;
        border-top: 1px solid var(--d26-line);
    }
    .sidebar-user-block {
        display: flex;
        flex: 1;
        align-items: center;
        gap: 10px;
        min-width: 0;
        min-height: var(--d26-target);
        padding: 0 6px 0 0;
        background: none;
        border: 0;
        border-radius: 10px;
        color: inherit;
        font: inherit;
        text-align: left;
        cursor: pointer;
    }
    .sidebar-user-block:hover { background: #f3f4f6; }
    .sidebar-user-avatar {
        display: flex;
        flex: none;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        overflow: hidden;
        border-radius: 50%;
        background: #f2f3f4;
        color: var(--d26-ink-2);
        font-size: 0.9rem;
    }
    .sidebar-user-avatar img { width: 100%; height: 100%; object-fit: cover; }
    .sidebar-user-info { display: flex; flex-direction: column; min-width: 0; }
    .sidebar-user-name {
        overflow: hidden;
        color: var(--d26-ink);
        font-size: 0.85rem;
        font-weight: 600;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .sidebar-user-role {
        color: var(--d26-ink-muted);
        font-size: 0.6875rem;
        font-weight: 600;
        letter-spacing: 0.03em;
        text-transform: uppercase;
    }
    .shell-logout-btn {
        display: flex;
        flex: none;
        align-items: center;
        justify-content: center;
        width: var(--d26-target);
        height: var(--d26-target);
        background: transparent;
        border: 1px solid var(--d26-line);
        border-radius: 10px;
        color: var(--d26-ink-2);
        cursor: pointer;
    }
    .shell-logout-btn:hover { border-color: var(--d26-danger); color: var(--d26-danger); }

    /* Desktop: collapse to an icon rail (the choice is remembered by app.js) */
    @media (min-width: 901px) {
        .app-shell.sidebar-collapsed .sidebar { width: var(--shell-rail); min-width: var(--shell-rail); }
        .app-shell.sidebar-collapsed .sidebar-brand { justify-content: center; padding: 0; }
        .app-shell.sidebar-collapsed .sidebar-brand > div:not(.brand-icon-box) { display: none; }
        .app-shell.sidebar-collapsed .sidebar-group-title { display: none; }
        .app-shell.sidebar-collapsed .sidebar-nav-group + .sidebar-nav-group {
            margin-top: 8px;
            padding-top: 8px;
            border-top: 1px solid var(--d26-line);
        }
        .app-shell.sidebar-collapsed .sidebar-link { justify-content: center; padding: 0; }
        .app-shell.sidebar-collapsed .sidebar-link-label { display: none; }
        /* a count becomes a small dot on the icon: the number is still in the item's title */
        .app-shell.sidebar-collapsed .nav-badge,
        .app-shell.sidebar-collapsed .nav-badge-purple {
            position: absolute;
            top: 7px;
            right: 12px;
            width: 9px;
            min-width: 9px;
            height: 9px;
            padding: 0;
            background: #d97706;
            font-size: 0;
        }
        .app-shell.sidebar-collapsed .sidebar-footer { flex-direction: column; padding: 10px 8px; gap: 6px; }
        .app-shell.sidebar-collapsed .sidebar-user-info { display: none; }
        .app-shell.sidebar-collapsed .sidebar-user-block { justify-content: center; padding: 0; }
    }

    /* Phone: the menu is a drawer that slides over the page */
    @media (max-width: 900px) {
        .sidebar {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            width: min(var(--shell-sidebar), 86vw);
            min-width: 0;
            transform: translateX(-100%);
        }
        .sidebar.open { transform: translateX(0); box-shadow: 10px 0 25px rgba(0, 0, 0, 0.4); }
    }

    /* --- Top bar -------------------------------------------------------------------- */
    .app-viewport {
        display: flex;
        flex: 1;
        flex-direction: column;
        min-width: 0;
        min-height: 100vh;
        background: var(--d26-surface);
    }
    .top-bar {
        position: sticky;
        top: 0;
        z-index: 40;
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: var(--shell-topbar);
        padding: 0 1.5rem;
        box-sizing: border-box;
        background: #fff;
        border-bottom: 1px solid var(--d26-line);
    }
    .top-bar-left { display: flex; align-items: center; gap: 0.75rem; min-width: 0; }
    .top-bar-actions { display: flex; align-items: center; gap: 0.75rem; }
    .shell-toggle-btn {
        display: inline-flex;
        flex: none;
        align-items: center;
        justify-content: center;
        width: var(--d26-target);
        height: var(--d26-target);
        padding: 0;
        background: transparent;
        border: 0;
        border-radius: 10px;
        color: var(--d26-ink-2);
        cursor: pointer;
    }
    .shell-toggle-btn:hover { background: #f0f1f3; }

    .top-bar-title-wrap { min-width: 0; }
    .top-bar-title-wrap h2 {
        margin: 0;
        overflow: hidden;
        color: var(--d26-ink);
        font-size: 1.125rem;
        font-weight: 700;
        line-height: 1.2;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .top-bar-title-wrap p {
        margin: 1px 0 0;
        overflow: hidden;
        color: var(--d26-ink-2);
        font-size: 0.8125rem;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .top-bar-back-btn {
        display: inline-flex;
        align-items: center;
        gap: 0.45rem;
        min-height: var(--d26-target);
        padding: 0 0.875rem;
        background: var(--d26-surface);
        border: 1px solid #d5d7dc;
        border-radius: var(--d26-radius-control);
        color: var(--d26-ink);
        font: inherit;
        font-size: 0.8125rem;
        font-weight: 600;
        cursor: pointer;
    }
    .top-bar-back-btn:hover { border-color: var(--d26-accent); color: var(--d26-accent); }
    .breadcrumb-nav { display: flex; align-items: center; gap: 0.5rem; color: var(--d26-ink-2); font-size: 0.875rem; }
    .breadcrumb-separator { color: var(--d26-ink-muted); font-size: 0.75rem; }
    .breadcrumb-current { color: var(--d26-ink); font-weight: 700; }

    /* "Unsaved draft changes": the script shows it with style.display */
    .top-bar-unsaved {
        align-items: center;
        gap: 0.4rem;
        padding: 0.35rem 0.75rem;
        background: #fffbeb;
        border: 1px solid #fde68a;
        border-radius: var(--d26-radius-control);
        color: #92400e;
        font-size: 0.78rem;
        font-weight: 700;
        white-space: nowrap;
        flex: 0 0 auto;
    }

    @media (max-width: 640px) {
        .top-bar { padding: 0 0.75rem; }
        .top-bar-title-wrap p { display: none; }
        /* Icon only on a phone, so the page title keeps its room; the title attribute and the icon still say it */
        .top-bar-unsaved-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
        .top-bar-unsaved { padding: 0.35rem 0.5rem; }
    }

    /* --- Notification bell and dropdown ---------------------------------------------- */
    .notif-center-wrap { position: relative; }
    .notif-bell-btn {
        position: relative;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: var(--d26-target);
        height: var(--d26-target);
        background: #fff;
        border: 1px solid var(--d26-line);
        border-radius: 10px;
        color: var(--d26-ink-2);
        cursor: pointer;
    }
    .notif-bell-btn:hover { background: var(--d26-surface); color: var(--d26-ink); }
    /* "Not clocked in yet": the clock and its warning dot pulse until the person clocks in */
    .shell-clockin-btn { color: var(--d26-wait-fg); border-color: var(--d26-wait-fg); }
    .shell-clockin-btn[hidden] { display: none; }
    .shell-clockin-btn:hover { color: var(--d26-wait-fg); }
    .shell-clockin-btn > i { animation: shell-clockin-pulse 1.6s ease-in-out infinite; }
    .shell-clockin-dot {
        position: absolute;
        top: -5px;
        right: -5px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 18px;
        height: 18px;
        border-radius: 999px;
        background: var(--d26-bad-fg);
        color: #fff;
        font-size: 0.6875rem;
        animation: shell-clockin-pulse 1.6s ease-in-out infinite;
    }
    @keyframes shell-clockin-pulse { 50% { opacity: 0.25; } }
    @media (prefers-reduced-motion: reduce) {
        .shell-clockin-btn > i, .shell-clockin-dot { animation: none; }
    }
    /* Request WFH: the same blue as the button on My attendance */
    .shell-wfh-btn { color: var(--d26-leave-fg); border-color: var(--d26-leave-fg); }
    .shell-wfh-btn:hover { color: var(--d26-leave-fg); background: var(--d26-leave-bg); }
    .notif-unread-badge {
        position: absolute;
        top: -5px;
        right: -5px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 18px;
        height: 18px;
        padding: 0 5px;
        box-sizing: border-box;
        border-radius: 999px;
        background: var(--d26-accent);
        color: #fff;
        font-size: 0.6875rem;
        font-weight: 700;
        line-height: 1;
    }
    .notif-dropdown-menu {
        position: absolute;
        top: calc(100% + 8px);
        right: 0;
        z-index: 60;
        flex-direction: column;
        width: 22rem;
        max-width: calc(100vw - 1.5rem);
        overflow: hidden;
        background: #fff;
        border: 1px solid var(--d26-line);
        border-radius: var(--d26-radius-card);
        box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.12), 0 8px 10px -6px rgba(0, 0, 0, 0.08);
    }
    .notif-dropdown-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.5rem;
        padding: 0.25rem 0.5rem 0.25rem 1rem;
        background: var(--d26-surface);
        border-bottom: 1px solid var(--d26-line);
    }
    .notif-dropdown-title { display: flex; align-items: center; gap: 0.5rem; font-size: 0.875rem; font-weight: 700; }
    .notif-dropdown-title i { color: var(--d26-accent); }
    .notif-mark-read-btn,
    .notif-view-all-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.4rem;
        min-height: var(--d26-target);
        padding: 0 0.625rem;
        background: transparent;
        border: 0;
        border-radius: var(--d26-radius-control);
        color: var(--d26-accent);
        font: inherit;
        font-size: 0.8125rem;
        font-weight: 600;
        cursor: pointer;
    }
    .notif-mark-read-btn:hover,
    .notif-view-all-btn:hover { background: var(--d26-accent-soft); }
    .notif-dropdown-list { max-height: 21rem; overflow-y: auto; }
    .notif-dropdown-footer { padding: 0.25rem 0.5rem; background: var(--d26-surface); border-top: 1px solid var(--d26-line); }
    .notif-view-all-btn { width: 100%; }
    .notif-empty-state {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 0.5rem;
        padding: 2.25rem 1rem;
        color: var(--d26-ink-2);
        text-align: center;
    }
    .notif-empty-state i { color: var(--d26-ok-fg); font-size: 1.8rem; }
    .notif-empty-state span { color: var(--d26-ink); font-size: 0.875rem; font-weight: 600; }

    .notif-item {
        display: flex;
        align-items: flex-start;
        gap: 0.75rem;
        width: 100%;
        padding: 0.75rem 1rem;
        background: transparent;
        border: 0;
        border-bottom: 1px solid var(--d26-line);
        font: inherit;
        text-align: left;
        color: inherit;
        text-decoration: none;
        cursor: pointer;
    }
    .notif-item:hover { background: var(--d26-surface); }
    .notif-item.unread { background: var(--d26-wait-bg); box-shadow: inset 3px 0 0 var(--d26-wait-fg); }
    .notif-item-icon {
        display: inline-flex;
        flex: none;
        align-items: center;
        justify-content: center;
        width: 28px;
        height: 28px;
        margin-top: 2px;
        border-radius: 50%;
        background: var(--d26-draft-bg);
        color: var(--d26-draft-fg);
        font-size: 0.78rem;
    }
    .notif-item-icon.approved,
    .notif-item-icon.settled { background: var(--d26-ok-bg); color: var(--d26-ok-fg); }
    .notif-item-icon.rejected { background: var(--d26-bad-bg); color: var(--d26-bad-fg); }
    .notif-item-icon.late { background: var(--d26-wait-bg); color: var(--d26-wait-fg); }
    .notif-item-icon.submitted { background: var(--d26-leave-bg); color: var(--d26-leave-fg); }
    .notif-item-body { display: block; flex: 1; min-width: 0; }
    .notif-item-new { flex: none; color: var(--d26-wait-fg); font-size: 0.6875rem; font-weight: 700; }
    .notif-item-title {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.5rem;
        color: var(--d26-ink);
        font-size: 0.8125rem;
        font-weight: 700;
    }
    .notif-item-desc { display: block; margin-top: 2px; color: var(--d26-ink-2); font-size: 0.75rem; line-height: 1.35; overflow-wrap: anywhere; }
    .notif-item-time { display: flex; align-items: center; gap: 0.3rem; margin-top: 4px; color: var(--d26-ink-muted); font-size: 0.6875rem; }

    .shell-chat-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        min-height: var(--d26-target);
        padding: 0 14px;
        background: var(--d26-accent-soft);
        border: 1px solid var(--d26-accent);
        border-radius: var(--d26-radius-control);
        color: var(--d26-accent-hover);
        font: inherit;
        font-weight: 600;
        cursor: pointer;
    }
    .shell-chat-btn:hover { background: #dff0ee; }
    .shell-chat-btn:focus-visible { outline: 2px solid var(--d26-accent); outline-offset: 2px; }

    .nav-badge[hidden],
    #sidebarUserAvatarImg[hidden],
    #topBarBackBtn[hidden],
    .breadcrumb-nav[hidden],
    #unsavedIndicator[hidden],
    .shell-chat-btn[hidden],
    #notifUnreadBadge[hidden],
    #notifDropdown[hidden],
    #overtimeNotifWrap[hidden],
    #overtimeNotifBadge[hidden],
    #overtimeNotifDropdown[hidden],
    #prelimNotifWrap[hidden],
    #prelimNotifBadge[hidden],
    #prelimNotifDropdown[hidden] { display: none; }

    /* --- Work area ------------------------------------------------------------------ */
    .main-view-container {
        flex: 1;
        width: 100%;
        max-width: 1160px;
        margin: 0 auto;
        padding: 30px 32px 56px;
        box-sizing: border-box;
    }
    @media (max-width: 820px) {
        .main-view-container { padding: 20px 16px 44px; }
    }

    @media print {
        .sidebar,
        .top-bar,
        .toast-container { display: none; }
        .app-viewport,
        .main-view-container { margin: 0; padding: 0; border: 0; box-shadow: none; }
    }

    /* --- Floating toast notifications (showToast in app.js) ------------------------ */
    .toast-container {
        position: fixed;
        right: 1.25rem;
        bottom: 1.25rem;
        z-index: 2000;
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        max-width: min(24rem, calc(100vw - 2rem));
        pointer-events: none;
    }
    .toast {
        display: flex;
        align-items: center;
        gap: 0.625rem;
        padding: 0.75rem 1rem;
        border-radius: var(--d26-radius-control);
        border: 1px solid var(--d26-line);
        background: #fff;
        color: var(--d26-ink);
        font-size: 0.875rem;
        font-weight: 600;
        box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.14), 0 4px 10px -4px rgba(0, 0, 0, 0.08);
        pointer-events: auto;
    }
    .toast.success { background: var(--d26-ok-bg); color: var(--d26-ok-fg); border-color: var(--d26-ok-fg); }
    .toast.error   { background: var(--d26-bad-bg); color: var(--d26-bad-fg); border-color: var(--d26-bad-fg); }
    .toast.warning { background: var(--d26-wait-bg); color: var(--d26-wait-fg); border-color: var(--d26-wait-fg); }
    .toast.info    { background: var(--d26-accent-soft); color: var(--d26-accent-hover); border-color: var(--d26-accent); }
}
