/* mobile.css — mobile-first UX
   Loads after main.css, overrides with !important where needed.

   Breakpoints:
   ≤767px   — phones
   481–767px — large phones (sub-breakpoint)
   768–1023px — tablets portrait
   1024–1366px — tablets landscape / small desktop
*/

/* ═══════════════════════════════════════════════════
   GLOBAL RESETS (all sizes)
═══════════════════════════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; }
html, body { max-width: 100%; }
/* Только на html: корневой overflow уезжает во вьюпорт и режет горизонтальный
   скролл всей страницы. На body это же правило сделало бы body скролл-контейнером. */
html { overflow-x: hidden; }
img, video, iframe, canvas { max-width: 100%; }
table { display: block; overflow-x: auto; max-width: 100%; }

/* Sheet structure (default = desktop layout) */
.sheet-handle { display: none; }
.sheet-btns {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.sheet-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
  overflow-y: auto;
}

/* Chat back button (hidden on desktop) */
.cr-back-btn { display: none !important; }

/* ═══════════════════════════════════════════════════
   PHONES ≤ 767px
═══════════════════════════════════════════════════ */
@media (max-width: 767px) {

  /* ── Root tokens ── */
  :root {
    --topbar-h: 60px;
    /* nav = 52px items + 4px top + 6px bottom + safe-area */
    --mob-nav-h: calc(62px + env(safe-area-inset-bottom, 0px));
  }

  /* ── Scrollable body (fixes profile/friends not scrolling) ── */
  html { height: auto !important; }
  body { height: auto !important; min-height: 100dvh; }
  .app-shell { grid-template-columns: 1fr !important; min-height: 0 !important; }
  main.content {
    height: auto !important;
    overflow-y: visible !important;
  }

  /* ── Topbar: плавает поверх контента, а не отодвигает его ── */
  .topbar {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    padding: 0 12px !important;
    height: 60px !important;
  }
  .brand-title { font-size: 16px !important; }

  /* ══════════════════════════════════════════
     BOTTOM NAVIGATION (sidebar → fixed footer)
  ══════════════════════════════════════════ */
  .sidebar {
    display: flex !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    height: auto !important;
    z-index: 300 !important;
    background: var(--bg-elevated) !important;
    border-top: 1px solid var(--border) !important;
    box-shadow: 0 -4px 24px rgba(11,18,32,.12) !important;
    /* Safe area for iPhone notch */
    padding: 4px 6px calc(6px + env(safe-area-inset-bottom, 0px)) !important;
    backdrop-filter: blur(24px) !important;
    -webkit-backdrop-filter: blur(24px) !important;
    border-radius: 0 !important;
  }

  /* Grid gives each nav item exactly equal width, regardless of count */
  .sidebar-menu {
    display: grid !important;
    grid-auto-flow: column !important;
    grid-auto-columns: 1fr !important;
    align-items: stretch !important;
    width: 100% !important;
    flex: 1 !important;
    background: transparent !important;
    border: 0 !important;
    box-shadow: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    padding: 0 !important;
    gap: 0 !important;
  }

  .side-link {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    width: 100% !important;
    height: auto !important;
    min-height: 52px !important;
    padding: 6px 2px !important;
    gap: 3px !important;
    border: 0 !important;
    background: transparent !important;
    border-radius: 10px !important;
    transform: none !important;
    text-align: center !important;
    white-space: nowrap !important;
  }
  .side-link:hover {
    transform: none !important;
    background: rgba(58,160,255,.08) !important;
    box-shadow: none !important;
  }
  .side-link.active {
    background: rgba(58,160,255,.12) !important;
    border-color: transparent !important;
  }
  .side-link.active .side-ico i,
  .side-link.active .side-text { color: rgba(58,160,255,1) !important; }

  .side-ico {
    width: 22px !important;
    height: 22px !important;
    background: transparent !important;
    border: 0 !important;
    border-radius: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
  .side-ico i { font-size: 20px !important; color: rgba(11,18,32,.70) !important; }
  .side-text {
    font-size: 9px !important;
    font-weight: 700 !important;
    color: rgba(11,18,32,.55) !important;
    line-height: 1.1 !important;
    letter-spacing: .01em !important;
  }

  /* Скрываем десктопные пункты — ПОСЛЕ .side-link чтобы перебить display:flex */
  .sidebar-menu .side-link--desktop-only {
    display: none !important;
    width: 0 !important;
    overflow: hidden !important;
  }

  /* Body bottom padding (safe area aware).
     Только там, где реально есть нижняя навигация: на экранах входа и
     регистрации её нет, и этот отступ зря удлинял страницу. */
  body.has-bottom-nav {
    padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* ══════════════════════════════════════════
     ОТКРЫТА ЭКРАННАЯ КЛАВИАТУРА (kb-open ставит base.html)
     Нижняя навигация не должна висеть над клавиатурой и есть место.
  ══════════════════════════════════════════ */
  html.kb-open { --mob-nav-h: 0px; }
  html.kb-open .sidebar { display: none !important; }
  html.kb-open body.has-bottom-nav { padding-bottom: 0 !important; }
  /* Поле ввода чата прижимается к верхней кромке клавиатуры.
     --kb-h = 0 там, где оболочка ужала layout-вьюпорт сама. */
  html.kb-open .chats-shell,
  html.kb-open .chat-container {
    bottom: var(--kb-h, 0px) !important;
  }

  /* Content: no side padding (elements handle their own) */
  main.content {
    padding: 0 !important;
    overflow-x: hidden !important;
  }

  /* ══════════════════════════════════════════
     MAP PAGE — FULL SCREEN + FLOATING CONTROLS
  ══════════════════════════════════════════ */

  /* Map: full-bleed under the floating topbar pill, down to bottom nav */
  .map {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: var(--mob-nav-h) !important;
    height: auto !important;
    border-radius: 0 !important;
    outline: none !important;
    box-shadow: none !important;
    z-index: 1 !important;
  }

  /* Floating action row: tighter gap/sizing on narrow phones */
  .map-fabs {
    gap: 8px !important;
    padding: 0 12px !important;
  }
  .map-fab-pill {
    height: 50px !important;
    padding: 0 18px !important;
    font-size: 14px !important;
  }
  .map-fab-round { width: 50px !important; height: 50px !important; }

  /* Event detail modal: bottom sheet on mobile */
  #event-detail-modal {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  #event-detail-modal > div {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 88vh !important;
    border-radius: 20px 20px 0 0 !important;
    margin: 0 !important;
  }

  /* Create event modal.
     max-width здесь раньше стоял 100% и перебивал лимит из шаблона — на широких
     телефонах модалка расползалась во всю ширину. Держим единый предел 420px. */
  .cem-box {
    padding: 20px 14px 20px !important;
    border-radius: 20px !important;
    max-height: 85% !important;
    max-width: 420px !important;
  }
  .cem-grid-2 { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important; gap: 8px !important; }
  .cem-cat-grid { grid-template-columns: repeat(3, 1fr) !important; }

  /* Toast: above bottom nav */
  .notif-toast {
    left: 8px !important;
    right: 8px !important;
    bottom: calc(var(--mob-nav-h) + 8px) !important;
    max-width: none !important;
  }

  /* ══════════════════════════════════════════
     PROFILE PAGE
  ══════════════════════════════════════════ */

  /* Cover: fixed height instead of aspect-ratio */
  .cover-banner {
    height: 180px !important;
    aspect-ratio: unset !important;
    border-radius: 0 !important;
  }

  /* Profile 2-col → 1-col, less negative margin.
     display:block (not grid) — a single grid track can still be forced wider
     than the viewport by unbreakable child content (long name/city/numbers);
     plain block stacking can't do that since children just take 100% of it. */
  .profile-layout {
    display: block !important;
    margin: -50px auto 0 !important;
    padding: 0 12px !important;
    max-width: 100% !important;
  }
  .profile-layout > .profile-left { margin-bottom: 12px; }

  /* Edit form wrap */
  .edit-form-wrap {
    margin: -50px auto 0 !important;
    padding: 0 12px !important;
    max-width: 100% !important;
  }

  /* Avatar photo: smaller on mobile */
  .avatar-xl-img {
    width: 88px !important;
    height: 88px !important;
    border-width: 3px !important;
    margin-bottom: 10px !important;
  }

  /* Avatar initials size-xl → small on mobile */
  .avatar-initials.size-xl {
    width: 88px !important;
    height: 88px !important;
    font-size: 28px !important;
    box-shadow: none !important;
  }

  /* Profile left card: less padding-top (avatar is smaller) */
  .profile-left > .card-glass:first-child {
    padding: 18px 14px 14px !important;
  }

  /* Stat cards: compact */
  .stat-row { gap: 6px !important; }
  .stat-card { padding: 10px 4px !important; }
  .stat-value { font-size: 18px !important; }
  .stat-label { font-size: 9px !important; }

  /* Page header — needs its own padding since main.content has padding:0 */
  .page-hdr {
    padding: 12px 14px 8px !important;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    margin-bottom: var(--gap-md);
  }
  .page-h1, .cl-header-title { font-size: var(--page-title-size) !important; letter-spacing: -0.01em; }
  .page-subtitle { font-size: 13px; }
  .page-hdr-actions {
    width: 100%;
    flex-wrap: wrap;
    gap: 6px;
  }
  .page-hdr-actions .btn { padding: 0 12px !important; }

  /* KV table */
  .kv-table { grid-template-columns: 86px 1fr; }
  .kv-table dt, .kv-table dd { font-size: 13px; padding: 10px 0; }

  /* ══════════════════════════════════════════
     EVENT CHATS PAGE (.chat-container)
  ══════════════════════════════════════════ */

  /* Fix event chat to viewport bounds — prevents input going behind nav */
  .chat-container {
    position: fixed !important;
    top: var(--topbar-h) !important;
    left: 0 !important;
    right: 0 !important;
    bottom: var(--mob-nav-h) !important;
    height: auto !important;
    width: 100% !important;
    overflow: hidden !important;
    margin: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    transform: none !important;
    flex-direction: column !important;
    padding: 0 !important;
  }
  .chat-list-panel {
    max-height: 30% !important;
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
    flex-shrink: 0 !important;
    border-right: none !important;
    border-bottom: 1px solid rgba(11,18,32,.08) !important;
    overflow-y: auto !important;
  }
  .chat-main-panel {
    flex: 1 !important;
    min-height: 0 !important;
    height: auto !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
  }
  .chat-messages {
    flex: 1 !important;
    overflow-y: auto !important;
    min-height: 0 !important;
  }
  .chat-input {
    flex-shrink: 0 !important;
    padding-bottom: max(12px, env(safe-area-inset-bottom, 12px)) !important;
  }
  /* Stack input form (textarea above button) */
  .chat-input form { flex-direction: column !important; }
  /* Only send button goes full-width — NOT header action buttons */
  .chat-input .btn { width: 100% !important; }

  /* Chat header: clamp title, protect action buttons */
  .chat-header { gap: 6px !important; padding: 8px 10px !important; flex-wrap: nowrap !important; }
  .chat-header h2 {
    flex: 1 !important; min-width: 0 !important;
    overflow: hidden !important; text-overflow: ellipsis !important;
    white-space: nowrap !important; font-size: 13px !important;
    margin: 0 !important;
  }
  .chat-header-actions {
    flex-shrink: 0 !important; gap: 4px !important; flex-wrap: nowrap !important;
  }
  /* Compact header buttons on mobile */
  .chat-header .btn { width: auto !important; height: auto !important; min-height: var(--btn-h) !important; padding: 8px 12px !important; font-size: var(--control-font) !important; }
  .chat-menu-btn { padding: 4px 6px !important; font-size: 18px !important; }

  /* ══════════════════════════════════════════
     CHATS PAGE — MOBILE NAVIGATION
  ══════════════════════════════════════════ */

  /* Full-screen shell */
  .chats-shell {
    position: fixed !important;
    top: var(--topbar-h) !important;
    left: 0 !important;
    right: 0 !important;
    bottom: var(--mob-nav-h) !important;
    width: 100% !important;
    height: auto !important;
    flex-direction: column !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    overflow: hidden !important;
  }

  /* Chat list: full screen by default */
  .cl-panel {
    width: 100% !important;
    min-width: 0 !important;
    flex: 1 !important;
    max-height: none !important;
    border-right: none !important;
    border-bottom: none !important;
  }

  /* Chat window panel: hidden by default (show only list) */
  .cr-panel {
    display: none !important;
    position: absolute !important;
    inset: 0 !important;
    z-index: 10 !important;
    background: rgba(255,255,255,.97) !important;
  }

  /* When chat is open: hide list, show chat */
  .chats-shell.mobile-chat-open .cl-panel {
    display: none !important;
  }
  .chats-shell.mobile-chat-open .cr-panel {
    display: flex !important;
    flex-direction: column !important;
  }

  /* Back button (mobile only) */
  .cr-back-btn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: 1px solid rgba(11,18,32,.10);
    background: rgba(255,255,255,.70);
    color: rgba(11,18,32,.60);
    font-size: 22px;
    font-weight: 600;
    cursor: pointer;
    flex-shrink: 0;
    margin-right: 4px;
    line-height: 1;
    padding: 0;
    font-family: inherit;
  }

  /* Prevent iOS zoom on chat input */
  .cr-input { font-size: 16px !important; }

  /* Chat tabs */
  .cl-tabs { padding: 10px 8px 0; }
  .cl-tab { font-size: 12px !important; height: 36px !important; }

  /* Message bubbles */
  .msg-bubble { max-width: 82% !important; }

  /* ══════════════════════════════════════════
     FRIENDS / NOTIFICATIONS / OTHER PAGES
  ══════════════════════════════════════════ */
  .chips { flex-wrap: wrap; gap: 4px; }
  .chip { padding: 6px 10px; font-size: 12px; }
  .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }

  /* Profile action card: consistent full-width buttons */
  .profile-action-card .btn {
    display: flex !important;
    height: 44px !important;
    min-height: 44px !important;
    max-height: 44px !important;
    font-size: 14px !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }
  .profile-action-card form { display: block !important; width: 100% !important; }
  .notif-actions { flex-wrap: wrap; gap: 6px; }
  .card-glass { border-radius: var(--radius); }
  .notif-item { padding: 12px; }
  .rating-buttons { display: flex; flex-wrap: wrap; gap: 6px; }
  .rating-buttons button { margin: 0; }

  /* Auth pages */
  .auth-card {
    padding: 24px !important;
    border-radius: var(--radius) !important;
  }
  .unauth { padding: 16px 12px; }

  /* Old profile container (profile.css) */
  .profile-container {
    position: relative !important;
    left: auto !important;
    top: auto !important;
    transform: none !important;
    width: calc(100vw - 16px) !important;
    max-width: 100% !important;
    max-height: none !important;
    margin: 8px auto 20px !important;
    z-index: auto !important;
  }
  .profile-main {
    grid-template-columns: 1fr !important;
    margin-top: -40px !important;
  }
  .profile-pic, .no-photo {
    width: 120px !important;
    height: 120px !important;
  }
  .profile-actions { flex-wrap: wrap !important; }

  /* Modals */
  .modal-app {
    max-width: 100% !important;
    border-radius: var(--radius) !important;
    padding: var(--gap-md) !important;
  }
  .modal-content { width: calc(100vw - 24px) !important; }
  .modal { padding: 12px !important; }
  .settings-panel { width: 100vw !important; }
  .inv-box {
    border-radius: var(--radius-lg) !important;
    padding: 18px 14px !important;
  }
  .chat-modal-box {
    border-radius: var(--radius-lg) !important;
    padding: 18px 14px !important;
    min-width: 0 !important;
    width: 92vw !important;
  }
  .notifications-dropdown {
    width: calc(100vw - 28px) !important;
    right: -10px !important;
  }
}

/* Фикс карточек и кнопок на узких мобильных экранах */
@media (max-width: 480px) {
  .card-glass, .friend-card, .event-card, .notif-item,
  .fl-friend-card, .fl-req-card, .fl-result-card,
  [class*="card"], [class*="item"] {
    max-width: 100%;
    overflow: hidden;
    box-sizing: border-box;
  }

  .btn {
    min-width: 0;
    padding: 0 12px;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    box-sizing: border-box;
  }
  .btn-sm { padding: 7px 12px; font-size: var(--fs-sm); }

  .card-actions, .friend-actions, .btn-group,
  .fl-actions, .page-hdr-actions, .edit-actions,
  [class*="actions"], [class*="buttons"] {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .friend-card, .fl-friend-card, .fl-req-card, .fl-result-card {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 12px;
  }
  .friend-card .friend-actions,
  .fl-friend-card .fl-actions,
  .fl-req-card .fl-actions,
  .fl-result-card .fl-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .event-sidebar, .right-panel, .event-detail-layout > div {
    width: 100% !important;
    position: static !important;
    min-width: 0 !important;
  }

  input, select, textarea {
    max-width: 100%;
    box-sizing: border-box;
  }

  .profile-edit-form { padding: 12px; }
  .profile-edit-form .field { margin-bottom: 12px; }

  .participants-list, .participants-grid {
    max-width: 100%;
    overflow-x: auto;
  }

  .request-actions {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
  }
  .request-actions .btn {
    flex: 1;
    min-width: 80px;
  }

  .event-action-btn {
    width: 100%;
    justify-content: center;
  }

}

.field-error {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 3px rgba(239, 68, 60, .12) !important;
  animation: shake .3s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* ═══════════════════════════════════════════════════
   LARGE PHONES 481–767px (on top of base phone rules)
═══════════════════════════════════════════════════ */
@media (min-width: 481px) and (max-width: 767px) {
  .page-h1 { font-size: var(--page-title-size) !important; }
  .msg-bubble { max-width: 74% !important; }
  .cover-banner { height: 200px !important; }
  .avatar-xl-img { width: 100px !important; height: 100px !important; }
  .avatar-initials.size-xl {
    width: 100px !important;
    height: 100px !important;
    font-size: 32px !important;
  }
  .map-sidebar { height: 112px !important; max-height: 112px !important; }
  .map-sidebar.sheet-open { height: 60vh !important; max-height: 60vh !important; }
}

/* ═══════════════════════════════════════════════════
   TOUCH DEVICE: remove sticky hover states
═══════════════════════════════════════════════════ */
@media (hover: none) and (pointer: coarse) {
  .side-link:hover { transform: none; background: transparent !important; }
  .event-item:hover { transform: none; }
  .btn:hover { transform: none; box-shadow: none; }
  .notif-item:hover { transform: none; }
  .msg:hover .msg-actions { display: none; }
  .msg.actions-open .msg-actions { display: flex !important; }
  .side-link { min-height: 44px; }
  .btn { min-height: 44px; }
  .cl-row { min-height: 56px; }
  .rp-event-row:hover { transform: none !important; }
  .rp-btn:hover { transform: none !important; }
}

/* ═══════════════════════════════════════════════════
   TABLETS PORTRAIT 768–1023px
═══════════════════════════════════════════════════ */
@media (min-width: 768px) and (max-width: 1023px) {
  :root {
    --left-w: 200px;
    --right-w: 270px;
    --topbar-h: 66px;
  }

  .topbar { padding: 0 14px; }
  .brand-title { font-size: 17px; }
  .sidebar { padding: 10px; }
  .sidebar-menu { gap: 6px; }
  .side-link { height: 42px; }
  .side-text { font-size: 12px; }

  .map-sidebar {
    width: var(--right-w) !important;
    right: var(--gap) !important;
  }

  .profile-layout { grid-template-columns: 220px 1fr !important; }
  .profile-container {
    width: min(980px, calc(100vw - 210px)) !important;
  }

  .page-h1 { font-size: 24px; }
  .page-hdr { flex-wrap: wrap; }

  .cl-panel { width: 240px !important; min-width: 240px !important; }

  .cem-cat-grid { grid-template-columns: repeat(4, 1fr) !important; }
  .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }
}

/* ═══════════════════════════════════════════════════
   TABLETS LANDSCAPE / SMALL DESKTOP 1024–1366px
═══════════════════════════════════════════════════ */
@media (min-width: 1024px) and (max-width: 1366px) {
  :root {
    --left-w: 230px;
    --right-w: 300px;
  }
}

/* ═══════════════════════════════════════════════════
   REDUCED MOTION
═══════════════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
  .map-sidebar { transition: none !important; }
}
