/* ============================================================
   APEX UI Utilities — Toast / Modal / Tooltip / Palette / Skeleton
   All RTL-aware. Works on top of theme tokens from styles.css.
   ============================================================ */

/* ============ Toast Stack ============ */
.apex-toast-stack {
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    max-width: min(380px, calc(100vw - 2rem));
    pointer-events: none;
}

.apex-toast {
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: 0.7rem;
    padding: 0.9rem 1rem;
    padding-bottom: 1.15rem;
    background: var(--surface-elev, #1a223a);
    color: var(--text-color, #e2e8f0);
    border: 1px solid var(--border, rgba(148, 163, 184, 0.18));
    border-radius: 12px;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
    backdrop-filter: blur(12px);
    overflow: hidden;
    pointer-events: auto;
    opacity: 0;
    transform: translateX(-30px);
    transition: opacity 0.25s ease, transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
    cursor: pointer;
    min-width: 280px;
}

.apex-toast--in   { opacity: 1; transform: translateX(0); }
.apex-toast--leave { opacity: 0; transform: translateX(-30px); }

.apex-toast__icon {
    font-size: 1.35rem;
    flex-shrink: 0;
    margin-top: 0.1rem;
}

.apex-toast--success .apex-toast__icon { color: #10b981; }
.apex-toast--danger  .apex-toast__icon,
.apex-toast--error   .apex-toast__icon { color: #ef4444; }
.apex-toast--warning .apex-toast__icon { color: #f59e0b; }
.apex-toast--info    .apex-toast__icon { color: #3b82f6; }

.apex-toast__body { flex: 1; min-width: 0; }
.apex-toast__title { font-weight: 600; font-size: 0.92rem; margin-bottom: 0.15rem; }
.apex-toast__msg { font-size: 0.88rem; line-height: 1.55; word-break: break-word; }

.apex-toast__close {
    background: transparent;
    border: none;
    color: var(--text-muted, #94a3b8);
    cursor: pointer;
    padding: 0.2rem;
    border-radius: 6px;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}

.apex-toast__close:hover {
    background: rgba(148, 163, 184, 0.15);
    color: var(--text-color, #e2e8f0);
}

.apex-toast__bar {
    position: absolute;
    bottom: 0;
    inset-inline: 0;
    height: 3px;
    background: linear-gradient(90deg,
        var(--primary, #6366f1),
        color-mix(in srgb, var(--primary, #6366f1) 60%, transparent));
    transform-origin: right center;
    animation: apex-toast-bar linear forwards;
}

.apex-toast--success .apex-toast__bar { background: #10b981; }
.apex-toast--danger  .apex-toast__bar,
.apex-toast--error   .apex-toast__bar { background: #ef4444; }
.apex-toast--warning .apex-toast__bar { background: #f59e0b; }
.apex-toast--info    .apex-toast__bar { background: #3b82f6; }

@keyframes apex-toast-bar {
    from { transform: scaleX(1); }
    to   { transform: scaleX(0); }
}

@media (max-width: 480px) {
    .apex-toast-stack { left: 0.5rem; right: 0.5rem; max-width: none; }
}

/* ============ Modal Overlay (confirm / help) ============ */
.apex-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.55);
    backdrop-filter: blur(6px);
    z-index: 9998;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
}

.apex-modal-overlay--in {
    opacity: 1;
    visibility: visible;
}

.apex-modal {
    width: 100%;
    max-width: 440px;
    background: var(--surface-elev, #1a223a);
    color: var(--text-color, #e2e8f0);
    border: 1px solid var(--border, rgba(148, 163, 184, 0.18));
    border-radius: 18px;
    box-shadow: 0 30px 70px rgba(0, 0, 0, 0.4);
    overflow: hidden;
    transform: scale(0.95) translateY(10px);
    transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
}

.apex-modal--help { max-width: 520px; }

.apex-modal-overlay--in .apex-modal { transform: scale(1) translateY(0); }

.apex-modal__head {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1.1rem 1.25rem;
    border-bottom: 1px solid var(--border);
}

.apex-modal__head i {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    flex-shrink: 0;
}

.apex-modal__head h3 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 600;
}

.apex-modal__head--warning i { background: rgba(245, 158, 11, 0.16); color: #fbbf24; }
.apex-modal__head--danger  i { background: rgba(239, 68, 68, 0.16);  color: #f87171; }
.apex-modal__head--info    i { background: rgba(59, 130, 246, 0.16); color: #60a5fa; }
.apex-modal__head--success i { background: rgba(16, 185, 129, 0.16); color: #34d399; }

.apex-modal__body {
    padding: 1.2rem 1.25rem;
    font-size: 0.95rem;
    line-height: 1.75;
    color: var(--text-color, #e2e8f0);
}

.apex-modal__body p { margin: 0; }

.apex-modal__foot {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    padding: 1rem 1.25rem;
    background: var(--surface-muted, rgba(0, 0, 0, 0.15));
    border-top: 1px solid var(--border);
}

.apex-btn {
    padding: 0.6rem 1.15rem;
    border: none;
    border-radius: 10px;
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s, color 0.2s;
}

.apex-btn--primary {
    background: linear-gradient(135deg, var(--primary, #6366f1), var(--primary-700, #4f46e5));
    color: white;
    box-shadow: 0 6px 18px rgba(var(--primary-rgb, 99 102 241), 0.35);
}

.apex-btn--primary:hover { transform: translateY(-1px); }

.apex-btn--danger {
    background: linear-gradient(135deg, #ef4444, #b91c1c);
    color: white;
    box-shadow: 0 6px 18px rgba(239, 68, 68, 0.35);
}

.apex-btn--danger:hover { transform: translateY(-1px); }

.apex-btn--ghost {
    background: transparent;
    color: var(--text-muted, #94a3b8);
    border: 1px solid var(--border);
}

.apex-btn--ghost:hover {
    background: var(--primary-soft, rgba(99, 102, 241, 0.12));
    color: var(--primary, #6366f1);
    border-color: var(--primary, #6366f1);
}

/* ============ Tooltip ============ */
.apex-tooltip {
    position: absolute;
    background: rgba(15, 23, 42, 0.95);
    color: #f1f5f9;
    padding: 0.4rem 0.7rem;
    border-radius: 8px;
    font-size: 0.78rem;
    font-weight: 500;
    pointer-events: none;
    opacity: 0;
    transform: translateY(2px);
    transition: opacity 0.15s, transform 0.15s;
    z-index: 9997;
    max-width: 260px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
    direction: rtl;
    text-align: center;
    white-space: nowrap;
}

.apex-tooltip--show {
    opacity: 1;
    transform: translateY(0);
}

/* ============ Command Palette ============ */
.apex-palette {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 12vh;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
}

.apex-palette--show {
    opacity: 1;
    visibility: visible;
}

.apex-palette__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(2, 6, 23, 0.6);
    backdrop-filter: blur(8px);
}

.apex-palette__box {
    position: relative;
    width: min(640px, calc(100% - 2rem));
    background: var(--surface-elev, #1a223a);
    color: var(--text-color, #e2e8f0);
    border: 1px solid var(--border, rgba(148, 163, 184, 0.18));
    border-radius: 16px;
    box-shadow: 0 40px 80px rgba(0, 0, 0, 0.5);
    overflow: hidden;
    transform: translateY(-15px);
    transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
}

.apex-palette--show .apex-palette__box { transform: translateY(0); }

.apex-palette__head {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 1rem 1.1rem;
    border-bottom: 1px solid var(--border);
}

.apex-palette__head > i {
    font-size: 1.3rem;
    color: var(--text-muted, #94a3b8);
}

.apex-palette__head input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-color);
    font-family: inherit;
    font-size: 0.98rem;
    outline: none;
}

.apex-palette__head input::placeholder {
    color: var(--text-soft, #64748b);
}

.apex-palette__head kbd {
    padding: 0.18rem 0.5rem;
    background: var(--surface-muted, rgba(0, 0, 0, 0.2));
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.72rem;
    font-family: ui-monospace, monospace;
}

.apex-palette__results {
    max-height: 50vh;
    overflow-y: auto;
    padding: 0.3rem;
}

.apex-palette__item {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.7rem 0.85rem;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.12s;
}

.apex-palette__item.is-active,
.apex-palette__item:hover {
    background: var(--primary-soft, rgba(99, 102, 241, 0.12));
}

.apex-palette__item > i {
    width: 36px;
    height: 36px;
    background: var(--surface-muted);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    color: var(--primary, #6366f1);
    flex-shrink: 0;
}

.apex-palette__item-body {
    flex: 1;
    min-width: 0;
}

.apex-palette__item-title {
    font-size: 0.92rem;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.apex-palette__item-sub {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 0.1rem;
}

.apex-palette__item-type {
    font-size: 0.72rem;
    padding: 0.18rem 0.5rem;
    background: var(--surface-muted);
    border-radius: 999px;
    color: var(--text-muted);
}

.apex-palette__foot {
    display: flex;
    gap: 1rem;
    padding: 0.65rem 1.1rem;
    border-top: 1px solid var(--border);
    background: var(--surface-muted, rgba(0, 0, 0, 0.1));
    font-size: 0.78rem;
    color: var(--text-muted);
}

.apex-palette__foot kbd {
    padding: 0.1rem 0.4rem;
    background: var(--surface-elev);
    border: 1px solid var(--border);
    border-radius: 5px;
    font-size: 0.7rem;
    font-family: ui-monospace, monospace;
}

.apex-palette__empty {
    text-align: center;
    padding: 2.5rem 1rem;
    color: var(--text-muted);
}

.apex-palette__empty i {
    font-size: 2.5rem;
    display: block;
    margin-bottom: 0.6rem;
    opacity: 0.5;
}

/* ============ Shortcut help table ============ */
.apex-shortcut-table {
    width: 100%;
    border-collapse: collapse;
}

.apex-shortcut-table td {
    padding: 0.55rem 0;
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border);
}

.apex-shortcut-table tr:last-child td { border-bottom: none; }

.apex-shortcut-table td:first-child {
    width: 130px;
    text-align: center;
}

.apex-shortcut-table kbd {
    padding: 0.2rem 0.55rem;
    background: var(--surface-muted);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: ui-monospace, monospace;
    font-size: 0.78rem;
    color: var(--text-color);
}

/* ============ Skeleton loader ============ */
.skeleton {
    background: linear-gradient(90deg,
        var(--surface-muted, rgba(0,0,0,0.06)) 0%,
        var(--border, rgba(148,163,184,0.18)) 50%,
        var(--surface-muted, rgba(0,0,0,0.06)) 100%);
    background-size: 200% 100%;
    animation: skeleton-pulse 1.4s ease-in-out infinite;
    border-radius: 8px;
    color: transparent !important;
}

.skeleton-text  { display: inline-block; width: 100%; height: 1em; border-radius: 4px; }
.skeleton-line  { height: 12px; margin-bottom: 8px; }
.skeleton-card  { height: 96px; }
.skeleton-circle { border-radius: 50%; }

@keyframes skeleton-pulse {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ============ Empty state SVG container ============ */
.apex-empty {
    text-align: center;
    padding: 3rem 1.5rem;
    color: var(--text-muted);
}

.apex-empty__art {
    width: 180px;
    height: 180px;
    margin: 0 auto 1rem;
    opacity: 0.85;
}

.apex-empty__title {
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--text-color);
    margin-bottom: 0.4rem;
}

.apex-empty__desc {
    font-size: 0.88rem;
    line-height: 1.7;
    max-width: 380px;
    margin: 0 auto 1rem;
}

/* ============ Print styles ============
   The whole UI uses dark-mode design tokens by default. When printing we
   FORCE-RESET those tokens to a light, paper-friendly palette so users
   never end up with white text on a white page. We then re-paint every
   common surface explicitly so dynamic widgets (tables, message bubbles,
   admin cards, etc.) print legibly regardless of the on-screen theme. */
@media print {
    /* ----- Reset every design token to print-friendly values ----- */
    html, html[data-theme="dark"], html[data-theme="light"], html[data-theme="auto"] {
        --bg-page: #ffffff !important;
        --bg-solid: #ffffff !important;
        --surface: #ffffff !important;
        --surface-elev: #ffffff !important;
        --surface-muted: #f7f7f7 !important;
        --border: #d0d0d0 !important;
        --border-strong: #b0b0b0 !important;
        --text-color: #111111 !important;
        --text-muted: #444444 !important;
        --text-soft: #555555 !important;
        --glass-bg: #ffffff !important;
        --glass-border: #d0d0d0 !important;
        --glass-shadow: none !important;
        --sidebar-bg: #ffffff !important;
        --header-bg: #ffffff !important;
        --input-bg: #ffffff !important;
        --input-border: #c0c0c0 !important;
        --gray-50:  #f5f5f5 !important;
        --gray-100: #ececec !important;
        --gray-200: #e0e0e0 !important;
        --gray-300: #c8c8c8 !important;
        --gray-400: #9a9a9a !important;
        --gray-500: #707070 !important;
        --gray-600: #555555 !important;
        --gray-700: #2a2a2a !important;
        --gray-800: #1a1a1a !important;
        --gray-900: #000000 !important;
        --primary-soft: #eef0ff !important;
        --primary-glow: none !important;
    }

    /* ----- Hide chrome ----- */
    .sidebar, .main-header, .theme-switcher, .apex-toast-stack,
    .apex-tooltip, .apex-cmdk, [data-apex-internal],
    .reply-form, .ticket-actions, .form-actions,
    .header-filters, .pagination,
    .quick-actions, .media-share-btn, .media-view__share,
    button.btn, .btn-glow,
    nav.media-breadcrumb,
    .filter-tabs, .filter-bar {
        display: none !important;
    }

    /* ----- Page basics ----- */
    body, body::before {
        background: #ffffff !important;
        color: #111111 !important;
        background-image: none !important;
    }
    body::before { display: none !important; }
    .main-content, .page-content, .container, .ticket-detail {
        margin: 0 !important;
        padding: 0 !important;
        background: #ffffff !important;
        color: #111111 !important;
    }

    /* ----- Generic surfaces ----- */
    .glass-card, .ticket-header, .messages-container, .sidebar-card,
    .stat-card, .data-table, table, .media-view__title-block,
    .settings-section, .admin-card, .card, .panel,
    .media-card, .media-cat-card, .media-view__side-card {
        background: #ffffff !important;
        color: #111111 !important;
        border: 1px solid #d0d0d0 !important;
        box-shadow: none !important;
        backdrop-filter: none !important;
        page-break-inside: avoid;
    }

    /* ----- Force readable text everywhere (kills dark-mode token text) ----- */
    body, body *,
    .message-body, .message-body *,
    .message-content, .message-content *,
    .message-header, .message-header *,
    .message-time, .system-message,
    th, td, p, span, div, li, h1, h2, h3, h4, h5, h6, label, strong, em {
        color: #111111 !important;
        text-shadow: none !important;
    }
    .text-muted, .message-time, .system-message,
    small, .form-hint, .media-card__meta { color: #555555 !important; }

    /* ----- Tables ----- */
    table, .data-table, .tickets-table {
        border-collapse: collapse !important;
        width: 100% !important;
    }
    th, td, .data-table th, .data-table td,
    .tickets-table th, .tickets-table td {
        border: 1px solid #d0d0d0 !important;
        padding: 6px 8px !important;
        background: #ffffff !important;
    }
    thead th, .data-table thead th, .tickets-table thead th {
        background: #f0f0f0 !important;
        font-weight: 700 !important;
    }
    tr:nth-child(even) td { background: #fafafa !important; }

    /* ----- Message bubbles (ticket detail) ----- */
    .message {
        page-break-inside: avoid;
        margin-bottom: 1rem !important;
    }
    .message-content {
        background: #ffffff !important;
        border: 1px solid #d0d0d0 !important;
        padding: 0.6rem 0.75rem !important;
    }
    .message-customer .message-content {
        background: #eef2ff !important;
        border-color: #c7d2fe !important;
    }
    .message-internal .message-content {
        background: #fef9c3 !important;
        border: 1px dashed #ca8a04 !important;
    }
    .message-avatar {
        background: #4f46e5 !important;
        color: #ffffff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    /* ----- Badges keep their meaning (forced color print) ----- */
    .badge, .priority-badge, .status-badge, .media-tag {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
        border: 1px solid #b0b0b0 !important;
    }

    /* ----- Links: black, no underline noise ----- */
    a, a:visited { color: #111111 !important; text-decoration: none !important; }

    /* ----- Images: don't break across pages ----- */
    img { max-width: 100% !important; page-break-inside: avoid; }

    /* ----- Browsers should keep accent colors when possible ----- */
    * {
        -webkit-print-color-adjust: economy;
        print-color-adjust: economy;
    }
}

/* ============ Page transition ============ */
.page-content {
    animation: page-fade-in 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes page-fade-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ============ Saved views chip ============ */
.filter-tab--saved {
    position: relative;
    background: var(--primary-soft, rgba(99,102,241,0.12));
    border: 1px dashed var(--primary, #6366f1);
    color: var(--primary, #6366f1);
}

.filter-tab--saved.active {
    background: var(--primary, #6366f1);
    color: white;
    border-style: solid;
}

.filter-tab--saved .filter-tab__del {
    background: transparent;
    border: none;
    padding: 0;
    color: inherit;
    cursor: pointer;
    opacity: 0.6;
    margin-right: 0.3rem;
    display: inline-flex;
    align-items: center;
}

.filter-tab--saved .filter-tab__del:hover { opacity: 1; }

.filter-tab--add {
    border: 1px dashed var(--border) !important;
    background: transparent !important;
    color: var(--text-muted) !important;
    cursor: pointer;
}

.filter-tab--add:hover {
    border-color: var(--primary) !important;
    color: var(--primary) !important;
}

/* ============ Quick-view modal ============ */
.apex-quickview-overlay {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.6);
    backdrop-filter: blur(8px);
    z-index: 9998;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.22s, visibility 0.22s;
}

.apex-quickview-overlay--in { opacity: 1; visibility: visible; }

.apex-quickview {
    background: var(--bg-page, #0f172a);
    color: var(--text-color);
    border: 1px solid var(--border);
    border-radius: 18px;
    width: 100%;
    max-width: 980px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 40px 100px rgba(0, 0, 0, 0.5);
    transform: scale(0.97) translateY(10px);
    transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
}

.apex-quickview-overlay--in .apex-quickview {
    transform: scale(1) translateY(0);
}

.apex-quickview__head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.9rem 1.1rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface-elev, #1a223a);
}

.apex-quickview__head-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
}

.apex-quickview__head-actions {
    display: flex;
    gap: 0.4rem;
}

.apex-quickview__body {
    flex: 1;
    overflow-y: auto;
    padding: 1.2rem;
}

.apex-quickview__loading > * { margin-bottom: 0.6rem; }

/* ============ Responsive helpers ============ */
.apex-only-mobile { display: none; }
@media (max-width: 768px) {
    .apex-only-mobile  { display: block; }
    .apex-hide-mobile  { display: none !important; }
}
