/* ============================================================
   Mulanui — Table Utils
   Column Visibility + Bulk Actions
   Path: shared/css/table-utils.css
   ⚠️ يعتمد على themes.css — لازم يتحمّل بعده
   ============================================================ */

/* ============================================================
   0. Table Toolbar (اللي فوق الجدول)
   ============================================================ */
.table-toolbar {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
    flex-wrap: wrap;
    position: relative;
    z-index: 400; /* أقل من navbar — عشان ما يتعارضش */
}

.table-toolbar:empty {
    display: none;
}

/* ============================================================
   1. Column Visibility Dropdown
   ============================================================ */
.col-vis-wrap {
    position: relative;
    display: inline-block;
}

/* ---- Button ---- */
.col-vis-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 12px;
    min-height: 34px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-main);
    font-family: var(--font-family);
    font-size: 13px;
    font-weight: var(--fw-medium);
    cursor: pointer;
    transition: var(--transition-fast);
    white-space: nowrap;
}

.col-vis-btn:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: var(--primary-soft);
}

.col-vis-btn:focus-visible {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.col-vis-btn .icon {
    font-size: 14px;
}

/* ---- Menu ---- */
.col-vis-menu {
    position: absolute;
    top: calc(100% + 6px);
    inset-inline-start: 0;   /* في RTL = القائمة على شمال الزر */
    min-width: 220px;
    max-width: 280px;
    max-height: 340px;
    overflow-y: auto;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-modal);
    padding: 8px;
    z-index: 2000;
    display: none;
}

.col-vis-menu.open {
    display: block;
    animation: colVisOpen 0.15s ease;
}

@keyframes colVisOpen {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ---- Menu items ---- */
.col-vis-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    color: var(--text-main);
    user-select: none;
    transition: background var(--transition-fast);
}

.col-vis-item:hover {
    background: var(--bg-hover);
}

.col-vis-item input[type="checkbox"] {
    accent-color: var(--primary-color);
    width: 15px;
    height: 15px;
    cursor: pointer;
    flex-shrink: 0;
}

/* ---- Separator + Reset ---- */
.col-vis-sep {
    height: 1px;
    background: var(--border-color);
    margin: 6px 4px;
}

.col-vis-reset {
    display: block;
    width: 100%;
    padding: 8px 10px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-family: var(--font-family);
    font-size: 12px;
    cursor: pointer;
    margin-top: 4px;
    transition: var(--transition-fast);
}

.col-vis-reset:hover {
    color: var(--text-main);
    border-color: var(--primary-color);
}

.col-vis-reset:focus-visible {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

/* ---- Hidden column ---- */
th.col-hidden,
td.col-hidden {
    display: none !important;
}

/* ============================================================
   2. Bulk Actions — Checkbox Column
   ============================================================ */
.bulk-check-col {
    width: 42px;
    max-width: 42px;
    padding-inline: 8px !important;
    text-align: center !important;
}

/* حماية من تعارض table-helper.css — الـ checkbox column يفضل في النص */
.table-container > table thead th.bulk-check-col,
.table-container > table tbody td.bulk-check-col,
.cms-table-wrapper > table.cms-table thead th.bulk-check-col,
.cms-table-wrapper > table.cms-table tbody td.bulk-check-col {
    text-align: center !important;
}

.bulk-check-col input[type="checkbox"] {
    accent-color: var(--primary-color);
    width: 16px;
    height: 16px;
    cursor: pointer;
    vertical-align: middle;
}

/* ---- Selected row ---- */
tr.row-selected {
    background: var(--primary-soft) !important;
}

tr.row-selected:hover {
    background: var(--primary-soft) !important;
}

/* ============================================================
   3. Floating Bulk Bar
   ============================================================ */
.bulk-bar {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(160%);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 18px;
    max-width: 90vw;
    flex-wrap: wrap;
    background: var(--bg-card);
    border: 1px solid var(--primary-color);
    border-radius: var(--radius-lg);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
    z-index: var(--z-toast);
    font-family: var(--font-family);
    direction: rtl;
    transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
    will-change: transform;
}

.bulk-bar.visible {
    transform: translateX(-50%) translateY(0);
}

/* ---- Count ---- */
.bulk-count {
    font-size: 13px;
    font-weight: var(--fw-bold);
    color: var(--text-main);
    padding-inline-end: 12px;
    border-inline-end: 1px solid var(--border-color);
    white-space: nowrap;
}

.bulk-count span {
    color: var(--primary-color);
    font-size: 16px;
}

/* ---- Actions ---- */
.bulk-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.bulk-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    min-height: 34px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-main);
    font-family: var(--font-family);
    font-size: 13px;
    font-weight: var(--fw-medium);
    cursor: pointer;
    white-space: nowrap;
    transition: var(--transition-fast);
}

.bulk-btn:hover {
    background: var(--primary-color);
    border-color: var(--primary-color);
    color: #fff;
}

.bulk-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.bulk-btn.danger:hover {
    background: var(--danger-color);
    border-color: var(--danger-color);
    color: #fff;
}

.bulk-btn.danger:focus-visible {
    box-shadow: 0 0 0 3px var(--danger-soft);
}

.bulk-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ---- Close ---- */
.bulk-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 22px;
    line-height: 1;
    padding: 0 4px;
    cursor: pointer;
    font-family: inherit;
    transition: color var(--transition-fast);
}

.bulk-close:hover {
    color: var(--danger-color);
}

/* ---- Loading overlay ---- */
.bulk-bar.loading::after {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    border-radius: var(--radius-lg);
    backdrop-filter: blur(1px);
    -webkit-backdrop-filter: blur(1px);
}

/* ============================================================
   4. Mobile — bottom sheet (≤640px)
   ============================================================ */
@media (max-width: 640px) {
    .bulk-bar {
        inset-inline: 12px;
        left: 12px;
        transform: translateX(0) translateY(160%);
        bottom: 12px;
        padding: 10px 14px;
        gap: 8px;
    }

    .bulk-bar.visible {
        transform: translateX(0) translateY(0);
    }

    .bulk-count {
        padding-inline-end: 8px;
        font-size: 12px;
    }

    .bulk-btn {
        padding: 6px 10px;
        font-size: 12px;
        min-height: 32px;
    }

    /* على الموبايل: أيقونة بس (توفر مساحة) */
    .bulk-btn .label {
        display: none;
    }

    .bulk-btn .icon {
        font-size: 14px;
    }
}

/* ============================================================
   5. Reduced Motion
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
    .col-vis-menu.open {
        animation: none;
    }

    .bulk-bar {
        transition: none;
    }
}

/* ============================================================
   6. Print — أخفي كل الأدوات
   ============================================================ */
@media print {
    .table-toolbar,
    .bulk-bar,
    .bulk-check-col {
        display: none !important;
    }
}

/* ============================================================
   T5.4 — Undo Toast
   ============================================================ */

.undo-toast {
    position: fixed;
    bottom: 84px;
    left: 50%;
    transform: translateX(-50%) translateY(180%);
    display: flex;
    flex-direction: column;
    min-width: 320px;
    max-width: 480px;
    background: var(--bg-card);
    border: 1px solid var(--warning-border);
    border-radius: var(--radius-lg);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
    z-index: calc(var(--z-toast) + 1);
    direction: rtl;
    font-family: var(--font-family);
    transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1),
                opacity   0.2s ease;
    overflow: hidden;
    opacity: 0;
}

.undo-toast.visible {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

.undo-toast.closing {
    transform: translateX(-50%) translateY(20px);
    opacity: 0;
}

/* ---- Progress bar (top) ---- */
.undo-toast-progress {
    height: 3px;
    width: 100%;
    background: var(--bg-elevated);
    overflow: hidden;
}

.undo-toast-progress-bar {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, var(--warning-color), var(--primary-color));
    transform-origin: right center;   /* RTL */
    animation: undoProgress linear forwards;
}

@keyframes undoProgress {
    from { transform: scaleX(1); }
    to   { transform: scaleX(0); }
}

/* ---- Body ---- */
.undo-toast-body {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
}

.undo-toast-info {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-main);
    font-size: 13px;
    font-weight: var(--fw-medium);
    min-width: 0;
}

.undo-toast-info > i {
    color: var(--warning-color);
    font-size: 15px;
    flex-shrink: 0;
}

.undo-toast-msg {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ---- Actions ---- */
.undo-toast-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.undo-toast-countdown {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    height: 24px;
    padding: 0 6px;
    border-radius: var(--radius-full);
    background: var(--bg-elevated);
    color: var(--text-muted);
    font-size: 11px;
    font-weight: var(--fw-bold);
    font-variant-numeric: tabular-nums;
    transition: color 0.2s;
}

.undo-toast-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 7px 14px;
    min-height: 32px;
    background: var(--warning-color);
    border: 1px solid var(--warning-color);
    border-radius: var(--radius-sm);
    color: #000;
    font-family: var(--font-family);
    font-size: 12px;
    font-weight: var(--fw-bold);
    cursor: pointer;
    transition: var(--transition-fast);
    white-space: nowrap;
}

.undo-toast-btn:hover {
    filter: brightness(1.1);
    transform: translateY(-1px);
}

.undo-toast-btn:active {
    transform: translateY(0);
}

/* ---- Mobile ---- */
@media (max-width: 640px) {
    .undo-toast {
        inset-inline: 12px;
        left: 12px;
        right: 12px;
        min-width: 0;
        max-width: none;
        transform: translateX(0) translateY(180%);
        bottom: 84px;
    }
    .undo-toast.visible {
        transform: translateX(0) translateY(0);
    }
    .undo-toast.closing {
        transform: translateX(0) translateY(20px);
    }
    .undo-toast-body {
        padding: 10px 14px;
    }
}

/* ---- Gaming theme ---- */
[data-theme="gaming"] .undo-toast {
    box-shadow:
        0 16px 48px rgba(0, 0, 0, 0.5),
        0 0 24px rgba(168, 85, 247, 0.15);
}

/* ---- Reduced motion ---- */
@media (prefers-reduced-motion: reduce) {
    .undo-toast {
        transition: opacity 0.1s ease;
    }
    .undo-toast-progress-bar {
        animation: none;
        transform: scaleX(0);
    }
}

/* ---- Print ---- */
@media print {
    .undo-toast {
        display: none !important;
    }
}