/* ============================================================
   Ulgamda CMS — Mobile Stylesheet  v9-improved
   FIXES: duplicate media queries removed, sidebar order fixed,
          conflicting pm-layout heights consolidated
   ============================================================ */

/* ── Tablet (≤1024px): sidebar still shows, narrower ─────── */
@media (max-width: 1024px) {
  .page-wrap { grid-template-columns: 1fr 260px; gap: 24px; padding: 24px 16px 48px; }
}

/* ── Phablet / large tablet (≤900px): single column ─────── */
@media (max-width: 900px) {
  .page-wrap { grid-template-columns: 1fr; gap: 20px; padding: 20px 14px 48px; }
  /* FIX: sidebar BELOW content (was conflicting: order:-1 vs order:2) */
  .sidebar { order: 2; }
  main     { order: 1; }
  /* Sidebar becomes horizontal scroll grid */
  .sidebar {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 16px;
  }
  /* PM layout narrower */
  .pm-layout { height: calc(100vh - 120px); }
  /* Chat */
  .chat-page-wrap { padding: 12px 12px 32px; }
  .chat-container  { height: calc(100vh - 180px); }
  /* Admin */
  .admin-dashboard-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  #admin-sidebar-close { display: flex !important; }
}

/* ── Tablet portrait (≤860px) ────────────────────────────── */
@media (max-width: 860px) {
  .admin-layout { grid-template-columns: 1fr; }
  .admin-dashboard-grid { grid-template-columns: 1fr; }
  .admin-files-cats-grid { grid-template-columns: 1fr; }
  .pm-layout { height: calc(100vh - 110px); }
  div[style*="grid-template-columns:1fr 1fr"],
  div[style*="grid-template-columns: 1fr 1fr"] { grid-template-columns: 1fr !important; }
  div[style*="grid-template-columns:2fr 1fr"],
  div[style*="grid-template-columns: 2fr 1fr"] { grid-template-columns: 1fr !important; }
}

/* ── 768px (admin mobile) ─────────────────────────────────── */
@media (max-width: 768px) {
  .admin-main { padding: 20px; }
  .footer-grid { grid-template-columns: 1fr 1fr; gap: 28px; }
  .mobile-menu-footer { flex-direction: column; gap: 6px; padding: 12px 20px; }
  .mobile-menu-footer .btn { width: 100%; justify-content: center; }
  /* Prevent iOS font-size zoom (16px min) */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  textarea,
  select,
  .pm-text-input,
  .chat-text-input,
  .write-body-input,
  .write-title-input { font-size: 16px !important; }
}

/* ── Tablet portrait (681–899px) header ──────────────────── */
@media (min-width: 681px) and (max-width: 900px) {
  .header-search { max-width: 220px; }
  .header-inner  { gap: 12px; }
}

/* ── Sidebar single column ≤600px ────────────────────────── */
@media (max-width: 600px) {
  .sidebar { grid-template-columns: 1fr; }
  .search-header form { flex-direction: column; }
  .search-header input { width: 100%; }
  .admin-table th, .admin-table td { padding: 8px 10px; font-size: .85rem; }
  .admin-header h1 { font-size: 1.2rem; }
}

/* ── Large mobile / small tablet (≤680px) ────────────────── */
@media (max-width: 680px) {
  .header-inner   { padding: 0 12px; gap: 8px; }
  .header-search  { display: none; }
  .hamburger      { display: flex; }
  .header-actions { gap: 6px; }
  /* Keep login + register always visible */
  .header-actions .btn-ghost.btn-sm  { display: inline-flex !important; font-size: .78rem; padding: 5px 9px; }
  .header-actions .btn-primary.btn-sm { font-size: .78rem; padding: 5px 9px; }
  .post-card.has-image { grid-template-columns: 1fr; }
  .post-card-image { order: -1; }
  .post-card-image img { min-height: 180px; max-height: 220px; width: 100%; }
  .write-page  { padding: 16px 12px 40px; }
  .write-card  { padding: 20px 16px; }
  .write-title-input { font-size: 1.4rem; }
  .write-meta  { grid-template-columns: 1fr; }
  .write-toolbar { gap: 6px; }
  .write-toolbar-btn { padding: 5px 8px; font-size: .75rem; }
  /* PM: full-screen mobile */
  .pm-layout {
    height: calc(100dvh - 66px);
    border-radius: 0;
    border-left: none; border-right: none;
    margin: 0;
  }
  .pm-sidebar { width: 100%; min-width: unset; border-right: none; display: flex; flex: 1; }
  .pm-chat {
    position: absolute; inset: 0; display: none; z-index: 10;
    background: var(--bg-white); flex-direction: column;
  }
  .pm-layout.chat-open .pm-chat    { display: flex; }
  .pm-layout.chat-open .pm-sidebar { display: none; }
  .pm-back-btn { display: flex !important; }
  .pm-msg-bubble { max-width: 86%; }
  /* Admin settings */
  .admin-settings-wrap { max-width: 100%; }
  /* Files grid */
  div[style*="auto-fill, minmax(260px"] { grid-template-columns: 1fr !important; }
  form[method="get"] { flex-wrap: wrap; }
  form[method="get"] input[type="search"],
  form[method="get"] select { max-width: 100%; width: 100%; }
}

/* ── Standard mobile (≤640px) ────────────────────────────── */
@media (max-width: 640px) {
  .header-inner  { padding: 0 12px; gap: 10px; }
  .site-logo small { display: none; }
  .site-logo { font-size: 1.4rem; }
  .profile-trigger { padding: 3px; border-radius: 50%; }
  .profile-trigger span { display: none; }
  .page-wrap { padding: 16px 12px 40px; gap: 20px; }
  /* Post card */
  .post-card { flex-direction: column !important; }
  .post-card-image { width: 100% !important; height: 200px !important; }
  .post-card-image img { width: 100%; height: 100%; object-fit: cover; }
  .post-card.has-image .post-card-image img { border-radius: var(--radius-lg) var(--radius-lg) 0 0; }
  /* Post full */
  .post-full-header { padding: 24px 20px 18px; }
  .post-full-title  { font-size: 1.5rem; }
  .post-full-body   { padding: 20px; font-size: .97rem; }
  .post-full-tags   { padding: 0 20px 20px; }
  .post-actions     { padding: 16px 20px; }
  .comments-header, .comment-form-wrap { padding-left: 20px; padding-right: 20px; }
  .comment-list { padding: 16px 20px; }
  /* Write */
  .write-card { padding: 18px 16px; }
  .write-toolbar { overflow-x: auto; flex-wrap: nowrap; padding-bottom: 4px; }
  .write-toolbar::-webkit-scrollbar { display: none; }
  /* Profile hero */
  .profile-hero { flex-direction: column; align-items: center; text-align: center; padding: 24px 16px; gap: 16px; }
  .profile-hero img { margin: 0 auto; }
  .profile-hero-stats { justify-content: center; flex-wrap: wrap; gap: 16px; }
  .profile-hero-info h1 { font-size: 1.3rem; justify-content: center; text-align: center; }
  .profile-hero-actions { margin-left: 0; width: 100%; justify-content: center; }
  .profile-hero-actions .btn { flex: 1; justify-content: center; min-width: 110px; }
  /* Admin */
  .admin-main { padding: 16px; }
  .admin-header { flex-direction: column; align-items: flex-start; }
  .admin-title  { font-size: 1.25rem; }
  .admin-main > div[style*="max-width"] { max-width: 100% !important; }
  .admin-main .write-card { padding: 18px 14px; }
  .admin-main .divider { margin: 16px 0; }
  .admin-table td:nth-child(4),
  .admin-table th:nth-child(4) { display: none; }
  .admin-table .hide-mobile-col { display: none !important; }
  /* Featured hero */
  .featured-hero { border-radius: var(--radius-lg); }
  .featured-hero-body { padding: 20px; }
  .featured-hero-body h2 { font-size: 1.3rem; }
  /* Members */
  .members-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; }
  .member-avatar { width: 52px; height: 52px; }
  .member-name { font-size: .82rem; }
  .staff-cards { grid-template-columns: 1fr; }
  .members-title { font-size: 1.3rem; }
  /* Chat */
  .chat-page-wrap { padding: 8px 8px 24px; }
  .chat-rooms-bar { margin-bottom: 8px; border-radius: 10px; }
  .chat-rooms-inner { padding: 6px 8px; gap: 4px; }
  .chat-room-tab { padding: 6px 12px; font-size: .8rem; }
  .chat-container { height: calc(100vh - var(--header-h) - 62px - 24px); min-height: 360px; border-radius: 12px; }
  /* Scroll top */
  #scroll-top { bottom: 16px; right: 14px; width: 40px; height: 40px; }
  /* Pagination */
  .pagination { gap: 5px; }
  /* View toggle */
  .view-toggle-btns { gap: 5px; }
  .view-toggle-btn { padding: 5px 9px; font-size: .73rem; }
  /* PM chat header sticky */
  .pm-chat-header { position: sticky; top: 0; z-index: 5; }
  .pm-input-area  { position: sticky; bottom: 0; z-index: 5; }
  /* Shop grid */
  .shop-grid, [class*="shop"] .grid { grid-template-columns: 1fr !important; }
  /* Search */
  .search-header { flex-direction: column; gap: 8px; }
  .search-header form { flex-direction: column; width: 100%; }
  .search-header button { width: 100%; justify-content: center; }
  /* Files view */
  .files-view-layout { flex-direction: column !important; }
  .files-view-cover  { width: 100% !important; text-align: center; }
  /* Notification dropdown */
  .notif-dropdown { right: 8px; left: 8px; width: auto; border-radius: 16px; top: calc(var(--header-h) + 8px); }
  .notif-dropdown::before { right: 52px; }
  /* Profile drawer */
  .profile-drawer { width: 280px; }
  /* Footer */
  .footer-bottom { flex-direction: column; gap: 4px; text-align: center; }
}

/* ── Large mobile (≤560px) ───────────────────────────────── */
@media (max-width: 560px) {
  .write-meta { grid-template-columns: 1fr; }
  .featured-hero { min-height: 260px; }
  .featured-hero-body { padding: 20px; }
  .featured-hero-body h2 { font-size: 1.2rem; }
  div[style*="auto-fill, minmax(260px"] { grid-template-columns: repeat(2, 1fr) !important; }
  .container > .card > div[style*="display:flex"] { flex-direction: column !important; }
  .container > .card div[style*="width:180px"] { width: 100% !important; }
  .admin-header { flex-direction: column; align-items: flex-start; }
  .admin-header > div { width: 100%; display: flex; flex-wrap: wrap; gap: 8px; }
  .form-grid-2col { grid-template-columns: 1fr; gap: 0; }
}

/* ── Standard mobile (≤520px) ────────────────────────────── */
@media (max-width: 520px) {
  .form-grid-2col { grid-template-columns: 1fr; gap: 0; }
}

/* ── Phone (≤480px) ──────────────────────────────────────── */
@media (max-width: 480px) {
  body { font-size: 15px; }
  .hide-xs { display: none !important; }
  .hero-card { padding: 24px 16px; }
  .hero-card h2 { font-size: 1.3rem; }
  .auth-page { padding: 20px 12px; align-items: flex-start; padding-top: 40px; }
  .auth-card { padding: 24px 18px; border-radius: var(--radius-lg); }
  .auth-title { font-size: 1.4rem; }
  .header-actions .btn-ghost.btn-sm  { display: inline-flex !important; }
  .header-actions .btn-sm { padding: 5px 8px; font-size: .75rem; }
  .featured-hero { min-height: 260px; }
  .admin-main { padding: 12px; }
  .admin-header { flex-direction: column; align-items: flex-start; }
  .admin-title  { font-size: 1.3rem; }
  .stat-card    { padding: 14px; }
  .stat-card-value { font-size: 1.6rem; }
  .post-full-header { padding: 18px 16px 14px; }
  .post-full-body   { padding: 16px; font-size: .97rem; }
  .post-full-title  { font-size: 1.35rem; }
  .post-actions     { padding: 12px 16px; gap: 8px; }
  .post-full-tags   { padding: 0 16px 16px; }
  .like-btn         { padding: 8px 14px; font-size: .82rem; }
  .comments-header  { padding: 14px 16px; }
  .comment-form-wrap{ padding: 16px; }
  .comment-list     { padding: 12px 16px; }
  .flash-messages   { right: 10px; left: 10px; max-width: 100%; }
  #scroll-top       { bottom: 16px; right: 14px; width: 40px; height: 40px; }
  .pm-modal { border-radius: var(--radius-lg); }
  .pm-modal-header { padding: 14px 16px; }
  .pm-modal-body   { padding: 12px; }
  .members-page { padding: 0 12px 40px; }
  .members-hero-inner { flex-wrap: wrap; gap: 8px; }
  .members-title { font-size: 1.2rem; }
  .members-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
  .member-card { padding: 14px 10px; }
  .member-avatar { width: 48px; height: 48px; }
  .member-name { font-size: .8rem; }
  .staff-cards { grid-template-columns: 1fr; }
  .admin-filter-row { flex-direction: column; align-items: stretch; }
  .admin-filter-row input,
  .admin-filter-row select,
  .admin-filter-row .btn { width: 100%; }
  .pm-layout { border-radius: 0; margin: 0 -14px; }
}

/* ── Small mobile (≤420px) ───────────────────────────────── */
@media (max-width: 420px) {
  .notif-dropdown { right: 4px; left: 4px; width: auto; border-radius: 14px; }
  .notif-dropdown::before { display: none; }
  .notif-item { padding: 12px 14px; gap: 10px; }
  .notif-avatar { width: 36px; height: 36px; }
  .search-form-row { flex-direction: column; }
  .search-form-row .btn { width: 100%; justify-content: center; }
  .chat-container { height: calc(100vh - 136px); }
  .chat-header-meta { gap: 6px; }
  .chat-total-msgs { display: none; }
  .pm-chat-header { padding: 10px 12px; gap: 8px; }
  .pm-chat-avatar { width: 34px; height: 34px; }
  .pm-chat-name   { font-size: .88rem; }
}

/* ── Tiny mobile (≤400px) ────────────────────────────────── */
@media (max-width: 400px) {
  .hide-xs { display: none !important; }
  .container { padding: 0 10px; }
  .page-wrap  { padding: 14px 10px 40px; }
  .members-page { padding: 0 10px 40px; }
  .post-card-body { padding: 14px; gap: 8px; }
  .post-card-title { font-size: 1.08rem; }
  .post-card-excerpt { -webkit-line-clamp: 2; }
  .comment { gap: 10px; }
  .comment-avatar img { width: 30px; height: 30px; }
  .pagination a, .pagination span { width: 34px; height: 34px; font-size: .82rem; }
  .header-inner { gap: 8px; }
  .site-logo { font-size: 1.2rem; }
  .notif-bell { width: 32px; height: 32px; }
  .header-actions { gap: 4px; }
  .btn-sm { padding: 5px 7px; font-size: .72rem; }
  .write-title-input { font-size: 1.2rem; }
  .write-body-input  { font-size: .95rem; }
  .write-submit-row  { flex-direction: column; }
  .write-submit-row .btn { width: 100%; justify-content: center; }
  .pm-msg-bubble { max-width: 88%; font-size: .88rem; }
  .pm-input-area { padding: 8px 10px; }
}

/* ── Galaxy Fold / very tiny (≤360px) ───────────────────── */
@media (max-width: 360px) {
  :root { --header-h: 56px; }
  .site-logo { font-size: 1.2rem; }
  .container { padding: 0 10px; }
  .page-wrap  { padding: 12px 10px 40px; }
  .btn        { padding: 6px 12px; font-size: .78rem; }
  .post-card-body  { padding: 12px; }
  .post-card-title { font-size: 1rem; }
  .auth-card       { padding: 20px 14px; }
  .stat-card-value { font-size: 1.4rem; }
  .members-grid    { grid-template-columns: repeat(2, 1fr) !important; gap: 8px; }
  .member-card     { padding: 12px 8px; }
  .member-avatar   { width: 42px; height: 42px; }
}

/* ── Messages page full screen on mobile ─────────────────── */
@media (max-width: 700px) {
  body.page-messages {
    overflow: hidden;
  }
  .page-messages .pm-layout,
  body.page-messages .pm-layout {
    height: calc(100dvh - 60px);
    border-radius: 0;
    border: none;
    margin: 0;
    position: relative; /* required for pm-chat absolute positioning */
  }
  .pm-chat {
    position: absolute;
    inset: 0;
    display: none;
    z-index: 10;
    background: var(--bg-white);
    flex-direction: column;
  }
  .pm-layout.chat-open .pm-chat { display: flex; }
  .pm-input-area {
    position: sticky;
    bottom: 0;
    z-index: 20;
    background: var(--bg-white);
    border-top: 1px solid var(--border);
  }
  .pm-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 8px;
  }
  .pm-back-btn { display: flex !important; }
  .pm-msg-bubble { max-width: 88%; font-size: .88rem; }
  .pm-text-input { font-size: 16px !important; } /* prevents iOS zoom */
}

/* ── Chat layout fine-tuning ─────────────────────────────── */
@media (max-width: 640px) {
  .chat-header     { padding: 10px 12px; }
  .chat-header-title { font-size: .92rem; }
  .chat-messages   { padding: 10px 8px; }
  .chat-msg-wrap   { max-width: 92%; }
  .chat-msg-bubble { padding: 8px 11px; }
  .chat-msg-body   { font-size: .88rem; }
  .chat-input-area { padding: 8px 10px; }
  .chat-emoji-picker { width: min(300px, calc(100vw - 20px)); left: 0; }
  .emoji-grid { grid-template-columns: repeat(7, 1fr); }
  .chat-mention-dropdown { left: 0; width: min(240px, calc(100vw - 20px)); }
  .chat-rooms-inner { gap: 4px; }
  .chat-room-tab { padding: 6px 12px; font-size: .8rem; }
}

/* ── Admin table columns hide on small screens ───────────── */
@media (max-width: 560px) {
  .admin-table .hide-mobile-col { display: none !important; }
}

/* ── Admin filter full width on mobile ───────────────────── */
@media (max-width: 480px) {
  .admin-table th, .admin-table td { padding: 9px 10px; font-size: .8rem; }
}

/* ── Admin main post-card ────────────────────────────────── */
@media (max-width: 640px) {
  .admin-main > div > .write-card { padding: 18px 14px; }
}


/* ── Profile edit page mobile ────────────────────────────── */
@media (max-width: 600px) {
  .profile-edit-wrap { padding: 0 0 32px; }
  .profile-edit-wrap .write-card { padding: 18px 14px; }
  .avatar-upload-row { flex-direction: column; align-items: flex-start; gap: 12px; padding: 14px; }
  .profile-edit-wrap [style*="grid-template-columns:1fr 1fr"] { grid-template-columns: 1fr !important; }
  .profile-edit-wrap [style*="grid-template-columns: 1fr 1fr"] { grid-template-columns: 1fr !important; }
}
