/* ============================================================
   SAS Support Hub — Mobile Redesign Layer (v1)
   يُحمّل بعد custom.css. كل القواعد هنا محصورة على الموبايل.
   لا يمس التصميم على الديسكتوب.
   ============================================================ */

/* ---------- Design Tokens ---------- */
:root {
  --mb-tab-height: 62px;
  --mb-tab-bg: rgba(15, 23, 42, .96);
  --mb-tab-border: rgba(255, 255, 255, .06);
  --mb-tab-active: #6366f1;
  --mb-tab-inactive: #94a3b8;
  --mb-touch-min: 44px;
  --mb-radius: 14px;
  --mb-shadow-up: 0 -4px 20px rgba(0, 0, 0, .25);
  --mb-safe-b: env(safe-area-inset-bottom, 0px);
  --mb-safe-t: env(safe-area-inset-top, 0px);
}
[data-bs-theme="light"] {
  --mb-tab-bg: rgba(255, 255, 255, .97);
  --mb-tab-border: rgba(0, 0, 0, .08);
  --mb-tab-inactive: #64748b;
}

/* ============================================================
   BOTTOM TAB BAR — Mobile Only
   ============================================================ */
.mb-bottom-tabs {
  display: none;
}
.mb-more-sheet,
.mb-more-backdrop,
.mb-info-sheet,
.mb-info-backdrop {
  display: none;
}

@media (max-width: 768px) {
  /* ---- إخفاء الـ navbar الأفقى القديم على الموبايل ---- */
  #appPage > .page > .navbar-expand-md {
    display: none !important;
  }

  /* ---- عند كتابة رسالة، نخفى الـ tabs عشان نكسب مساحة ---- */
  body.mb-input-focused .mb-bottom-tabs {
    display: none !important;
  }

  /* ---- Bottom tab bar — flex item داخل .page عشان ياخد مكانه ---- */
  .mb-bottom-tabs {
    display: flex !important;
    position: relative;
    z-index: 1040;
    background: var(--mb-tab-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid var(--mb-tab-border);
    box-shadow: var(--mb-shadow-up);
    padding-bottom: var(--mb-safe-b);
    height: calc(var(--mb-tab-height) + var(--mb-safe-b));
    align-items: stretch;
    flex-shrink: 0;
    width: 100%;
  }
  .mb-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 6px 2px;
    color: var(--mb-tab-inactive);
    font-size: .65rem;
    font-weight: 600;
    background: none;
    border: none;
    cursor: pointer;
    position: relative;
    transition: color .15s;
    min-height: var(--mb-touch-min);
    text-decoration: none;
  }
  .mb-tab svg {
    width: 22px;
    height: 22px;
    stroke-width: 2;
  }
  .mb-tab.active {
    color: var(--mb-tab-active);
  }
  .mb-tab.active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 32px;
    height: 3px;
    background: var(--mb-tab-active);
    border-radius: 0 0 3px 3px;
  }
  .mb-tab .mb-tab-badge {
    position: absolute;
    top: 2px;
    right: calc(50% - 22px);
    min-width: 17px;
    max-width: 34px;
    height: 17px;
    padding: 0 5px;
    border-radius: 9px;
    background: #ef4444;
    color: #fff;
    font-size: .62rem;
    font-weight: 700;
    line-height: 17px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    box-shadow: 0 0 0 2px var(--mb-tab-bg);
  }
  .mb-tab .mb-tab-badge.d-none { display: none !important; }

  /* ============================================================
     MORE SHEET — Bottom sheet لباقى الـ views
     ============================================================ */
  .mb-more-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .5);
    z-index: 1050;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
  }
  .mb-more-backdrop.open {
    opacity: 1;
    pointer-events: auto;
  }
  .mb-more-sheet {
    display: block;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1051;
    background: var(--tblr-bg-surface);
    border-radius: 20px 20px 0 0;
    padding: 8px 12px calc(16px + var(--mb-safe-b));
    transform: translateY(100%);
    transition: transform .25s cubic-bezier(.4, 0, .2, 1);
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: var(--mb-shadow-up);
  }
  .mb-more-sheet.open {
    transform: translateY(0);
  }
  .mb-sheet-handle {
    width: 40px;
    height: 4px;
    background: rgba(255, 255, 255, .2);
    border-radius: 2px;
    margin: 4px auto 12px;
  }
  [data-bs-theme="light"] .mb-sheet-handle { background: rgba(0, 0, 0, .15); }
  .mb-sheet-title {
    font-size: .85rem;
    font-weight: 700;
    color: var(--tblr-secondary-color);
    padding: 0 8px 8px;
    letter-spacing: .5px;
  }
  .mb-sheet-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 13px 10px;
    border-radius: 10px;
    color: var(--tblr-body-color);
    font-size: .92rem;
    font-weight: 500;
    cursor: pointer;
    background: none;
    border: none;
    width: 100%;
    text-align: right;
    text-decoration: none;
    transition: background .12s;
    min-height: var(--mb-touch-min);
  }
  .mb-sheet-item:hover,
  .mb-sheet-item:active {
    background: rgba(99, 102, 241, .08);
  }
  .mb-sheet-item svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
    color: var(--tblr-secondary-color);
  }
  .mb-sheet-item.danger { color: #ef4444; }
  .mb-sheet-item.danger svg { color: #ef4444; }

  /* ============================================================
     TOP MOBILE HEADER (بديل الـ navbar)
     ============================================================ */
  #appPage > .page > .navbar {
    padding: 6px 10px;
    min-height: 52px;
  }
  #appPage > .page > .navbar .navbar-brand {
    font-size: .95rem;
  }
  #appPage > .page > .navbar .navbar-toggler {
    display: none !important; /* مفيش داعى للهامبرجر مع bottom tabs */
  }

  /* ============================================================
     CONVERSATIONS — Chat Input محسّن للموبايل
     ============================================================ */
  /* منطقة الإدخال كاملة: مضمونة الظهور فوق الـ bottom tabs */
  .chat-input-area {
    padding: 6px 6px 8px !important;
    flex-shrink: 0 !important;
    background: var(--tblr-bg-surface) !important;
    border-top: 1px solid var(--tblr-border-color) !important;
    position: relative !important;
    z-index: 10 !important;
    min-height: 54px !important;
    display: block !important;
  }
  /* خلى chat-input-area ما يختفيش بسبب overflow parents */
  #chatInputArea:not(.d-none),
  .chat-main > .chat-input-area:not(.d-none) {
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
  }

  /* أزرار Internal / Canned لو كانت مخفية من قواعد قديمة — نعيد إظهارهم */
  .chat-input-area #btnInternal,
  .chat-input-area #btnCanned,
  .chat-input-area #btnEmoji,
  .chat-input-area #btnAttach,
  .chat-input-area #btnSpeech {
    display: inline-flex !important;
    visibility: visible !important;
  }

  /* الـ toolbar: كل الأزرار تظهر فى صف واحد — مفيش إخفاء */
  .chat-input-toolbar,
  .chat-input-actions,
  .chat-input-send {
    display: flex !important;
    align-items: center;
    gap: 3px !important;
    padding: 2px 0 4px !important;
    overflow: visible !important;
    flex-wrap: nowrap !important;
  }
  .chat-input-toolbar {
    width: 100%;
    justify-content: flex-start;
  }

  /* أزرار داخل input area — 36×36 ظاهرة للموبايل */
  .chat-input-toolbar .btn-icon,
  .chat-input-actions .btn-icon,
  .chat-input-send .btn-icon,
  .chat-input-area .btn-icon.btn-sm {
    width: 36px !important;
    height: 36px !important;
    min-width: 36px !important;
    border-radius: 8px !important;
    padding: 0 !important;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    flex-shrink: 0 !important;
    visibility: visible !important;
    opacity: 1 !important;
  }
  .chat-input-toolbar .btn-icon .icon,
  .chat-input-actions .btn-icon .icon,
  .chat-input-send .btn-icon .icon,
  .chat-input-area .btn-icon.btn-sm .icon {
    width: 19px !important;
    height: 19px !important;
  }

  /* ضمان ظهور كل أزرار الـ chat input فى المحادثات والتذاكر */
  .chat-input-area #btnInternal,
  .chat-input-area #btnCanned,
  .chat-input-area #btnEmoji,
  .chat-input-area #btnAttach,
  .chat-input-area #btnSpeech,
  .chat-input-area #btnSend,
  .chat-input-area #tkBtnEmoji,
  .chat-input-area #tkBtnAttach,
  .chat-input-area #tkBtnSpeech,
  .chat-input-area #tkBtnVoice {
    display: inline-flex !important;
    visibility: visible !important;
  }

  /* textarea أكبر ومريح */
  .chat-input-area textarea {
    font-size: 15px !important; /* ≥16px يمنع الزوم التلقائى فى iOS */
    min-height: 40px !important;
    max-height: 120px !important;
    padding: 9px 12px !important;
    border-radius: 20px !important;
  }

  /* زر الإرسال/الميكروفون */
  .chat-input-area .chat-input-send-btn,
  .chat-input-send-btn {
    width: 46px !important;
    min-width: 46px !important;
    height: 44px !important;
    border-radius: 14px !important;
  }
  .chat-input-send-btn .icon {
    width: 24px !important;
    height: 24px !important;
  }

  /* رسائل: تحسين قراءة وحجم الفقاعات */
  .chat-messages {
    padding: 10px 8px !important;
    gap: 4px;
  }
  .msg-row { max-width: 86% !important; }
  .msg-bubble {
    font-size: 14.5px !important;
    line-height: 1.45 !important;
    padding: 7px 10px !important;
    border-radius: 12px !important;
  }
  .msg-time { font-size: .68rem !important; }

  /* dropdowns فى رأس المحادثة: قابلة للسكرول ومقبولة الحجم */
  .chat-header { padding: 6px 8px !important; }
  .chat-header select.form-select-sm {
    font-size: .78rem !important;
    padding: .3rem 1.5rem .3rem .5rem !important;
    min-height: 32px !important;
    max-width: 120px !important;
  }
  .chat-header .avatar.avatar-md {
    width: 36px; height: 36px;
  }
  #chatName { font-size: .92rem; }
  #chatMeta { font-size: .72rem; }

  /* زر الرجوع يكون واضح */
  #mobileBackBtn {
    width: 38px !important;
    height: 38px !important;
    min-width: 38px !important;
    background: rgba(99,102,241,.1) !important;
  }
  #mobileBackBtn .icon {
    width: 22px !important;
    height: 22px !important;
  }

  /* ---- chat-sidebar (قائمة المحادثات) ---- */
  .chat-list-item {
    padding: 12px 12px !important;
    min-height: 68px;
  }
  .chat-list-item .avatar {
    width: 44px !important;
    height: 44px !important;
  }
  .chat-list-item .item-name {
    font-size: .95rem !important;
    font-weight: 600;
  }
  .chat-list-item .item-preview {
    font-size: .82rem !important;
  }

  /* ---- INFO PANEL كـ Bottom Sheet ---- */
  /* مخفى بالكامل بشكل افتراضى. يظهر بس لما يتضاف class "mb-open" */
  .chat-info-panel {
    display: none !important;
  }
  .chat-info-panel.mb-open {
    display: flex !important;
    flex-direction: column;
    position: fixed !important;
    inset: auto 0 0 0 !important;
    width: 100% !important;
    min-width: 100% !important;
    height: 92vh !important;
    height: 92dvh !important;
    max-height: 92dvh !important;
    border-radius: 20px 20px 0 0 !important;
    box-shadow: var(--mb-shadow-up) !important;
    z-index: 1060 !important;
    transform: translateY(0) !important;
    overflow: hidden;
    animation: mbSheetUp .28s cubic-bezier(.4, 0, .2, 1);
  }
  @keyframes mbSheetUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }
  .info-panel-header {
    padding: 14px 16px 10px !important;
    position: relative;
  }
  .info-panel-header::before {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, .2);
  }
  [data-bs-theme="light"] .info-panel-header::before { background: rgba(0, 0, 0, .12); }
  .info-panel-body {
    padding: 8px 14px 20px !important;
    overflow-y: auto;
    flex: 1;
  }

  /* ============================================================
     TICKETS — Card view بدل الجدول
     ============================================================ */
  #tkListView .card.card-md .table-responsive,
  #tkListView > div > .card .card-body {
    padding: 0 !important;
  }
  /* هيدر التذاكر مدمج */
  #tkListView .d-flex.align-items-center.justify-content-between.mb-3 {
    margin-bottom: 10px !important;
  }
  #tkListView h2.page-title {
    font-size: 1.1rem !important;
  }

  /* فلاتر الحالة تمرير أفقى */
  #tkStatusFilters {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    max-width: 100%;
    flex-wrap: nowrap;
    white-space: nowrap;
  }
  #tkStatusFilters::-webkit-scrollbar { display: none; }
  #tkStatusFilters .btn {
    flex-shrink: 0;
    font-size: .78rem;
    padding: .4rem .7rem;
  }

  /* تحويل صفوف الجدول إلى كروت مستقلة */
  #tkListView .table { border: none !important; }
  #tkListView .table thead { display: none; }
  #tkListView .table,
  #tkListView .table tbody,
  #tkListView .table tr,
  #tkListView .table td {
    display: block !important;
    width: 100% !important;
  }
  #tkListView .table tr {
    background: var(--tblr-bg-surface) !important;
    border: 1px solid var(--tblr-border-color-translucent) !important;
    border-radius: 12px !important;
    margin-bottom: 10px !important;
    padding: 12px !important;
    position: relative;
  }
  #tkListView .table tr:hover {
    border-color: rgba(99, 102, 241, .35) !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
  }
  #tkListView .table td {
    border: none !important;
    padding: 2px 0 !important;
    font-size: .82rem !important;
    text-align: right !important;
    min-height: auto !important;
  }
  /* إعادة إظهار الأعمدة المخفية فى الجدول الأصلى */
  #tkListView .table th:nth-child(n+5),
  #tkListView .table td:nth-child(n+5) {
    display: block !important;
  }
  /* عمود الـ checkbox (الأول) ثابت فى الزاوية */
  #tkListView .table td:first-child {
    position: absolute;
    top: 10px;
    left: 10px;
    width: auto !important;
    padding: 0 !important;
  }
  /* رقم التذكرة (الثانى) أكبر وبارز */
  #tkListView .table td:nth-child(2) {
    font-weight: 700 !important;
    font-size: .82rem !important;
    color: var(--tblr-primary);
    margin-bottom: 4px;
    padding-right: 32px !important;
  }
  /* العنوان/الموضوع */
  #tkListView .table td:nth-child(3),
  #tkListView .table td:nth-child(4) {
    font-size: .9rem !important;
    color: var(--tblr-body-color);
  }

  /* ============================================================
     ANALYTICS — stat cards فى 2 أعمدة
     ============================================================ */
  #analyticsView .row.row-cards > [class*="col-"] {
    flex: 0 0 50%;
    max-width: 50%;
    padding: 4px;
  }
  #analyticsView .stat-card {
    padding: 12px 8px !important;
  }
  #analyticsView .stat-card .stat-value {
    font-size: 1.35rem !important;
  }
  #analyticsView .stat-card .stat-label {
    font-size: .72rem !important;
  }
  #analyticsView .container-fluid,
  #reportsView .container-fluid,
  #lookupView .container-fluid,
  #sasMgmtView .container-fluid,
  #contactsView .container-fluid {
    padding-left: 8px !important;
    padding-right: 8px !important;
    padding-top: 8px !important;
  }

  /* ============================================================
     SETTINGS — list → detail على الموبايل
     ============================================================ */
  .settings-layout {
    position: relative;
    flex-direction: column !important;
  }
  .settings-sidebar {
    width: 100% !important;
    min-width: 100% !important;
    padding: 8px !important;
    height: auto !important;
    border: none !important;
  }
  .settings-sidebar-title {
    font-size: 1rem !important;
    padding: 6px 10px 10px !important;
    border-bottom: 1px solid var(--tblr-border-color-translucent) !important;
    margin-bottom: 8px !important;
  }
  .settings-tabs-list {
    flex-direction: column !important;
    overflow-x: visible !important;
    padding: 0 !important;
    gap: 6px !important;
  }
  .settings-main {
    padding: 12px !important;
  }
  .settings-tab-btn {
    background: var(--tblr-bg-surface) !important;
    border: 1px solid var(--tblr-border-color-translucent) !important;
    border-bottom: 1px solid var(--tblr-border-color-translucent) !important;
    border-right: 3px solid transparent !important;
    border-radius: 10px !important;
    margin-bottom: 0 !important;
    padding: 12px 14px !important;
    font-size: .88rem !important;
    min-height: var(--mb-touch-min);
    white-space: normal !important;
    text-align: right !important;
  }
  .settings-tab-btn::after {
    content: "‹";
    margin-right: auto;
    padding-left: 4px;
    font-size: 1.4rem;
    color: var(--tblr-secondary-color);
    opacity: .5;
    line-height: 1;
  }
  .settings-tab-btn.active {
    border-color: var(--mb-tab-active) !important;
    border-right-color: var(--mb-tab-active) !important;
    background: rgba(99, 102, 241, .08) !important;
  }

  /* عند اختيار tab، نخفى الـ sidebar ونظهر الـ main فقط */
  .settings-layout.mb-detail-mode .settings-sidebar { display: none !important; }
  .settings-layout.mb-detail-mode .settings-main { display: block !important; }

  /* زر رجوع داخل settings-main عند اختيار tab */
  .settings-main.mb-detail-view .settings-mb-back {
    display: inline-flex;
  }
  .settings-mb-back {
    display: none;
    align-items: center;
    gap: 6px;
    background: rgba(99, 102, 241, .1);
    color: var(--tblr-primary);
    border: none;
    padding: 8px 14px;
    border-radius: 10px;
    margin-bottom: 12px;
    font-size: .85rem;
    cursor: pointer;
    min-height: 40px;
  }

  /* ============================================================
     CONTACTS — طبق نفس نمط المحادثات (single-panel)
     ============================================================ */
  #contactsView .chat-sidebar {
    width: 100% !important;
    min-width: 100% !important;
  }
  #contactsView .chat-sidebar.mobile-hidden {
    display: none !important;
  }

  /* ============================================================
     MODALS — مناسبة للموبايل
     ============================================================ */
  .modal-dialog {
    margin: 8px !important;
    max-width: calc(100vw - 16px) !important;
  }
  .modal-content {
    border-radius: 14px !important;
    max-height: calc(100vh - 16px);
  }
  .modal-body {
    max-height: calc(100vh - 180px);
    overflow-y: auto;
    padding: 14px !important;
  }
  .modal-header { padding: 12px 14px !important; }
  .modal-footer {
    padding: 10px 14px !important;
    padding-bottom: calc(10px + var(--mb-safe-b)) !important;
  }

  /* ============================================================
     TOAST — فوق الـ bottom tabs مش تحته
     ============================================================ */
  .toast-container {
    top: auto !important;
    bottom: calc(var(--mb-tab-height) + var(--mb-safe-b) + 16px) !important;
    left: 16px !important;
    right: 16px !important;
    transform: none !important;
    align-items: stretch;
  }
  .toast {
    font-size: .88rem !important;
    padding: 10px 14px !important;
    border-radius: 10px !important;
  }

  /* ============================================================
     FAB — فوق الـ bottom tabs
     ============================================================ */
  .fab-btn {
    bottom: calc(var(--mb-tab-height) + var(--mb-safe-b) + 16px) !important;
  }

  /* ============================================================
     NOTIFICATIONS DROPDOWN — كشاشة كاملة
     ============================================================ */
  #notifDropdown {
    position: fixed !important;
    top: 52px !important;
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    max-height: calc(100vh - 60px - var(--mb-tab-height)) !important;
  }

  /* ============================================================
     PWA BANNER — بعيد عن الـ bottom tabs
     ============================================================ */
  .pwa-install-banner {
    bottom: calc(var(--mb-tab-height) + var(--mb-safe-b)) !important;
  }

  /* ============================================================
     LOGIN PAGE — تحسين الهاتف
     ============================================================ */
  #loginPage .container-tight {
    padding: 16px !important;
    max-width: 100% !important;
  }
  #loginPage .card {
    border-radius: 16px;
  }
  #loginPage .form-control {
    min-height: 44px;
    font-size: 15px;
  }

  /* ============================================================
     GENERAL — typography مقروءة
     ============================================================ */
  body {
    -webkit-text-size-adjust: 100%;
  }
  .btn { min-height: 38px; }
  .btn-sm { min-height: 34px; }
  .btn-xs { min-height: 28px; }
  .form-control, .form-select {
    font-size: 15px !important;
    min-height: 40px;
  }
  .form-control-sm, .form-select-sm {
    font-size: .85rem !important;
    min-height: 34px;
  }

  /* ---- scrollbar أرفع على الموبايل ---- */
  ::-webkit-scrollbar { width: 3px !important; }

  /* ---- تمكين dvh (حل مشكلة keyboard iOS) ---- */
  #appPage { height: 100dvh !important; }
}

/* ============================================================
   EXTRA SMALL (< 400px) — تكثيف إضافى
   ============================================================ */
@media (max-width: 399px) {
  .mb-tab { font-size: .58rem; }
  .mb-tab svg { width: 20px; height: 20px; }
  .msg-row { max-width: 92% !important; }
  #tkListView .table td:nth-child(n+5) {
    font-size: .72rem !important;
  }
}

/* ============================================================
   Reduced motion — احترام تفضيل المستخدم
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .mb-more-sheet,
  .chat-info-panel {
    transition: none !important;
  }
}
