/* =========================================================================
   1. TRYBY MOBILNE (Ekran < 992px) - Ogólne dopasowanie profilu
   ========================================================================= */
@media (max-width: 992px) {
  .profile-container {
    max-width: 100%;
    width: 100%;
    padding: 0;
  }
  .profile-header-card,
  .cover-photo-wrapper,
  .cover-photo {
    border-radius: 0;
  }
  .profile-header-card {
    margin-bottom: 12px;
  }
  .cover-photo-wrapper {
    height: 260px;
  }
  .profile-avatar-row {
    padding: 0 16px;
    margin-top: -40px;
  }
  .avatar-wrapper {
    width: 130px;
    height: 130px;
  }
  .profile-body-layout,
  main.profile-feed {
    display: block;
    width: 100%;
    max-width: 100%;
    padding: 0;
    margin: 0;
  }
  .profile-info-sidebar {
    width: 100%;
    max-width: 100%;
    margin-bottom: 12px;
    padding: 0 8px;
  }
  main.profile-feed .accordion-container,
  main.profile-feed #accordion-posts-wrapper {
    width: 100%;
    max-width: 100%;
    padding: 0;
    margin: 0;
  }
  .main-container,
  .editorial-article {
    margin-top: 0;
    margin-bottom: 0;
  }
}

/* =========================================================================
   2. REWOLUCJA GESTÓW I STRUMIENIA (Ekran < 900px) - ARCHITEKTURA KARUZELI 1:1
   ========================================================================= */
@media (max-width: 900px) {
  body {
    max-width: 100vw;
    overflow-x: hidden;
    position: relative;
    background-color: #ffffff;
  }

  .fb-navbar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100vw !important;
    transform: none !important;
    z-index: 10005 !important;
  }

  .nav-center,
  .login-branding,
  .fb-navbar .nav-right .user-nav-link,
  .user-visitcard {
    display: none !important;
  }

  .page-main-content {
    transform: none !important;
    width: 100vw;
    display: block;
    padding-top: 0px !important;
    overflow-x: hidden;
  }

  /* Karuzela aktywuje się WYŁĄCZNIE gdy kontener nie zawiera pojedynczego artykułu */
  .main-container:not(:has(.editorial-article)) {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    width: 300vw !important;
    max-width: 300vw !important;
    padding: 0 !important;
    margin: 0 !important;
    gap: 0 !important;
    will-change: transform;
    transform: translateX(-100vw);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), width 0.1s ease-out;
  }

  /* Sztywne wymiarowanie ekranów 100vw tylko wewnątrz aktywnego kontenera karuzeli */
  .main-container:not(:has(.editorial-article)) .left-sidebar,
  .main-container:not(:has(.editorial-article)) main.feed,
  .main-container:not(:has(.editorial-article)) .right-sidebar {
    width: 100vw !important;
    max-width: 100vw !important;
    min-width: 100vw !important;
    flex-shrink: 0 !important;
    position: relative !important;
    top: 0 !important;
    left: 0 !important;
    right: auto !important;
    transform: none !important;
    box-shadow: none !important;
    display: block !important;
    margin: 0 !important;
    box-sizing: border-box;
  }

  /* Panele boczne karuzeli dopasowują swoją wysokość do okna i ukrywają scroll pionowy body */
  .main-container:not(:has(.editorial-article)) .left-sidebar,
  .main-container:not(:has(.editorial-article)) .right-sidebar {
    height: calc(100vh - 56px) !important;
    max-height: calc(100vh - 56px) !important;
    overflow-y: auto !important;
    padding: 12px 16px 20px 16px !important;
    background-color: #ffffff !important;
  }

  /* REŻIM STANÓW STAŁYCH DLA PANELÓW (Wyzwalany przez JS klasami has-left/right na body) */
  body.has-left-sidebar .main-container:not(:has(.editorial-article)) {
    transform: translateX(0) !important;
  }
  
  body.has-right-sidebar .main-container:not(:has(.editorial-article)) {
    transform: translateX(-200vw) !important; 
  }

  /* DOSTOSOWANIE STRONY JEDNEGO ARTYKUŁU (SINGLE-POST) */
  .main-container:has(.editorial-article) {
    display: block !important;
    padding: 0 !important;
    margin: 0 auto !important;
    width: 100vw !important;
    max-width: 100% !important;
    transform: none !important;
  }

  .main-container .editorial-article {
    padding: 20px 16px !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  /* PRZESŁONA BLOKUJĄCA INTERAKCJE NA FEEDZIE */
  .sidebar-overlay {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0);
    z-index: 100;
    cursor: pointer;
    animation: fadeInOverlay 0.2s ease-out;
  }

  body.has-left-sidebar main.feed .sidebar-overlay,
  body.has-right-sidebar main.feed .sidebar-overlay {
    display: block !important;
  }

  @keyframes fadeInOverlay {
    from { opacity: 0; }
    to { opacity: 1; }
  }
  
  .nav-left { flex: 1; min-width: 0; gap: 8px; }
  .nav-left form { display: flex; flex: 1; min-width: 0; }
  .fb-navbar .search-bar { width: 200px; max-width: 200px; font-size: 13px; padding: 8px 12px; }
  .nav-right { flex: 0 0 auto; min-width: 40px; }

  .post-card, .accordion-item, .login-prompt, .info-box, .comment-card {
    width: 100%; max-width: 100%; border-radius: 0; border: none; box-shadow: none; padding: 16px; margin: 0;
  }
  .post-card:not(:last-child), .accordion-item:not(:last-child) { border-bottom: 1px solid #e4e6eb; }
  
  .create-post:has(form), .login-box {
    width: 100%; max-width: 100%; margin: 0; padding: 16px; border-radius: 0; border: none; border-bottom: 2px solid #e4e6eb;
  }
  .create-post form, .post-inline-edit-mode form {
    padding: 0; margin: 0; width: 100%; display: flex; flex-direction: column; gap: 6px;
  }
  .create-post form > div:first-child, .post-inline-edit-mode form > div:first-child { margin-bottom: 4px; padding: 0 4px; }
  .edit-title-field { width: 100%; padding: 8px 10px; margin-bottom: 2px; font-size: 15px; border-radius: 4px; box-sizing: border-box; }
  .create-post .ql-toolbar.ql-snow, .post-inline-edit-mode .ql-toolbar.ql-snow { padding: 4px 6px; margin: 0; border-radius: 0; border: none; width: 100%; box-sizing: border-box; }
  .create-post .ql-container.ql-snow, .post-inline-edit-mode .ql-container.ql-snow { min-height: 220px; margin: 0; border-radius: 0; border: none; font-size: 16px; width: 100%; box-sizing: border-box; }
  .post-image-container { margin: 12px -16px; width: calc(100% + 32px); }
  #visibility-select-node { width: 100%; }
  
  .login-container { display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 16px auto; padding: 0 16px; width: 100%; }
  .login-box { width: 100%; max-width: 400px; padding: 24px 20px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); border: 1px solid #e4e6eb; border-radius: 8px; }
  .login-box input { padding: 11px 14px; margin-bottom: 10px; }
  .login-box hr { margin: 14px 0; }

  /* POPRAWKA: Przeniesione wyżej reguły pionowania nagłówka profilu (Dla wszystkich urządzeń < 900px) */
  .profile-avatar-row {
    flex-direction: column !important;
    align-items: center !important;
    text-align: center !important;
    gap: 12px !important;
    margin-top: -65px !important; /* Wyciąga awatar wyżej, na zdjęcie w tle */
  }
  .avatar-wrapper {
    margin: 0 auto !important;
    width: 130px !important;
    height: 130px !important;
  }
  .profile-user-details {
    width: 100% !important;
    margin-bottom: 0 !important;
  }
  .profile-user-details h2 {
    font-size: 24px !important;
    justify-content: center !important; /* Środkuje nazwę wraz z ikoną weryfikacji */
  }
  .profile-rank-badge {
    margin: 4px auto 0 auto !important; /* Środkuje odznakę rangi pod pseudonimem */
  }
  
  /* =========================================================================
     PEŁNOEKRANOWY MODAL TWORZENIA POSTA (MOBILE - PRZYCISK W NAGŁÓWKU)
     ========================================================================= */
  #create-post-modal {
    position: fixed !important;
    inset: 0 !important;
    padding: 0 !important;
    align-items: stretch !important;
    background-color: #ffffff !important;
    z-index: 10010 !important;
    overflow: hidden !important;
  }

  #create-post-modal .custom-modal-box {
    max-width: 100% !important;
    width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    margin: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    display: flex !important;
    flex-direction: column !important;
  }

  #create-post-modal .custom-modal-header {
    flex: 0 0 auto !important;
    background: #ffffff;
    z-index: 10;
    padding: 10px 14px !important;
  }

  #create-post-modal .custom-modal-body {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    padding: 12px 16px 24px 16px !important;
    overflow-y: auto !important;
  }

  #create-post-modal #editForm-new {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
  }

  #create-post-modal #edit-quill-new {
    flex: 1 1 auto !important;
    min-height: 180px !important;
    max-height: none !important;
    resize: none !important;
  }
  /* =========================================================================
     WSKAŹNIK AKTYWNEGO PANELU (3 SEGMENTY POD NAGŁÓWKIEM)
     ========================================================================= */
  .mobile-panel-indicator {
    display: flex;
    position: fixed;
    top: 56px; /* Dokładnie pod paskiem .fb-navbar (height: 56px) */
    left: 0;
    right: 0;
    width: 100vw;
    height: 3px;
    background-color: #ffffff;
    z-index: 10004;
    gap: 2px;
    pointer-events: none;
  }

  /* Ukrywamy wskaźnik na podstronach bez karuzeli (np. profil, ustawienia, single-post) */
  body:not(:has(.left-sidebar)) .mobile-panel-indicator,
  body:has(.editorial-article) .mobile-panel-indicator {
    display: none !important;
  }

  .mobile-panel-indicator .panel-segment {
    flex: 1;
    height: 100%;
    background-color: #ffffff; /* Nieaktywny segment: biały */
    transition: background-color 0.25s ease;
  }

  /* Domyślnie aktywny środkowy panel (Feed) */
  body:not(.has-left-sidebar):not(.has-right-sidebar) .mobile-panel-indicator .seg-center {
    background-color: #1877f2;
  }

  /* Aktywny lewy panel */
  body.has-left-sidebar .mobile-panel-indicator .seg-left {
    background-color: #1877f2;
  }
  body.has-left-sidebar .mobile-panel-indicator .seg-center {
    background-color: #ffffff;
  }

  /* Aktywny prawy panel */
  body.has-right-sidebar .mobile-panel-indicator .seg-right {
    background-color: #1877f2;
  }
  body.has-right-sidebar .mobile-panel-indicator .seg-center {
    background-color: #ffffff;
  }
    /* Wyłączenie dotyku dla paneli bocznych, gdy nie są aktywne */
  body:not(.has-left-sidebar) .main-container:not(:has(.editorial-article)) .left-sidebar,
  body:not(.has-right-sidebar) .main-container:not(:has(.editorial-article)) .right-sidebar {
    pointer-events: none !important;
  }

  body.has-left-sidebar .main-container:not(:has(.editorial-article)) .left-sidebar,
  body.has-right-sidebar .main-container:not(:has(.editorial-article)) .right-sidebar {
    pointer-events: auto !important;
  }
}

/* =========================================================================
   3. OPTYMALIZACJE DOTYKOWE (Ekran < 600px)
   ========================================================================= */
@media (max-width: 600px) {
  .profile-actions-mobile,
  .profile-guest-actions-mobile {
    flex-wrap: nowrap !important;
    gap: 8px !important;
    margin-left: 0 !important;
    width: 100% !important;
    justify-content: center !important;
    margin: 16px auto !important;
  }
  .profile-actions-mobile .btn-text,
  .profile-guest-actions-mobile .btn-text,
  .post-actions .action-btn .btn-text {
    display: none !important;
  }
  .profile-actions-mobile .upload-action-btn,
  .profile-guest-actions-mobile button,
  .profile-guest-actions-mobile .profile-block-main-btn,
  .profile-guest-actions-mobile .restricted-feature-trigger,
  .profile-guest-actions-mobile .profile-follow-btn,
  .profile-guest-actions-mobile .profile-mail-main-btn {
    width: 44px !important;
    height: 44px !important;
    min-width: 44px !important;
    padding: 0 !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    border-radius: 50% !important;
    font-size: 18px !important;
    box-sizing: border-box !important;
  }
  .profile-guest-actions-mobile i {
    font-size: 16px !important;
    margin: 0 !important;
    padding: 0 !important;
    display: inline-block !important;
    color: #ffffff !important;
  }
  
  /* SKORYGOWANIE MARGINESÓW DLA JEDNOSZPAŁTOWEGO ARTYKUŁU */
  .main-container:has(.editorial-article) {
    padding: 0 !important;
    margin: 0 auto !important;
    width: 100vw !important;
    max-width: 100% !important;
  }

  .main-container .editorial-article {
    padding: 20px 16px !important;
    border-radius: 0 !important;
    box-shadow: none !important;
  }
    /* WYŚRODKOWANIE ZAWARTOSCI STOPKI NA URZĄDZENIACH MOBILNYCH */
  footer.site-footer .footer-container {
    flex-direction: column !important;
    justify-content: center !important;
    align-items: center !important;
    text-align: center !important;
    gap: 14px !important;
    padding: 8px 16px !important;
  }

  footer.site-footer .footer-container div,
  footer.site-footer .footer-container div a {
    text-align: center !important;
    width: 100% !important;
  }

  /* Wyśrodkowanie i delikatne odseparowanie grupy linków prawnych */
  footer.site-footer .footer-container div:nth-child(2) {
    display: flex !important;
    justify-content: center !important;
    gap: 24px !important;
  }
}
