/* ========================================
   MATERIAL DESIGN 3 - RESPONSIVE STYLES
   Abie Messenger - Mobile First
   ======================================== */

/* ========================================
   MOBILE NAVIGATION BUTTONS
   ======================================== */
.mobile-menu-btn {
  display: none;
  width: 48px;
  height: 48px;
  padding: 0;
  background: var(--md-sys-color-surface-container-high);
  border: none;
  border-radius: var(--md-sys-shape-corner-full);
  font-size: 20px;
  cursor: pointer;
  transition: all var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
  position: relative;
  overflow: hidden;
  align-items: center;
  justify-content: center;
}

.mobile-menu-btn::before {
  content: '';
  position: absolute;
  inset: 0;
  background: currentColor;
  opacity: 0;
  transition: opacity var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
  border-radius: inherit;
}

.mobile-menu-btn:hover::before {
  opacity: var(--md-sys-state-hover-opacity);
}

.mobile-back-btn {
  display: none;
  width: 48px;
  height: 48px;
  padding: 0;
  background: var(--md-sys-color-surface-container-high);
  border: none;
  border-radius: var(--md-sys-shape-corner-full);
  font-size: 18px;
  cursor: pointer;
  margin-right: 8px;
  transition: all var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
  position: relative;
  overflow: hidden;
}

.mobile-back-btn::before {
  content: '';
  position: absolute;
  inset: 0;
  background: currentColor;
  opacity: 0;
  transition: opacity var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
  border-radius: inherit;
}

.mobile-back-btn:hover::before {
  opacity: var(--md-sys-state-hover-opacity);
}

/* ========================================
   SIDEBAR OVERLAY (Scrim)
   ======================================== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--md-sys-color-scrim);
  opacity: 0.32;
  z-index: 999;
  transition: opacity var(--md-sys-motion-duration-medium2) var(--md-sys-motion-easing-standard);
}

.sidebar-overlay.active {
  display: block;
}

/* ========================================
   TABLETS (768px - 1024px)
   ======================================== */
@media (max-width: 1024px) {
  .sidebar {
    width: 280px;
  }

  .chat-header {
    padding: 16px 20px;
  }

  .messages-container {
    padding: 16px 20px;
  }

  .message-input-container {
    padding: 16px 20px;
  }
}

/* ========================================
   MOBILE DEVICES (up to 768px)
   ======================================== */
@media (max-width: 768px) {
  /* Show mobile navigation */
  .mobile-menu-btn {
    display: flex;
  }

  .mobile-back-btn {
    display: flex;
  }

  /* Sidebar as full-screen drawer */
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 100%;
    max-width: 100vw;
    z-index: 1000;
    transition: transform var(--md-sys-motion-duration-medium4) var(--md-sys-motion-easing-emphasized);
    overflow-x: hidden;
    transform: translateX(0);
  }

  /* Hide sidebar when chat is selected */
  .sidebar.chat-selected {
    transform: translateX(-100%);
  }

  /* Chat takes full screen */
  .chat-area {
    width: 100%;
    max-width: 100vw;
    height: 100vh;
    height: 100dvh;
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
  }

  .chat-container {
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 100vw;
    overflow-x: hidden;
  }

  /* Messages container fills available space */
  .messages-container {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 12px 16px;
  }

  /* Input container stays at bottom */
  .message-input-container {
    flex-shrink: 0;
    position: relative;
    background: var(--md-sys-color-surface);
    border-top: 1px solid var(--md-sys-color-outline-variant);
    padding: 12px 16px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
  }

  /* Chat header */
  .chat-header {
    padding: 12px 16px;
  }

  .chat-header-info h2 {
    font-family: var(--md-sys-typescale-title-medium-font-family);
    font-size: var(--md-sys-typescale-title-medium-font-size);
    max-width: 150px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .chat-header-actions {
    display: flex;
    gap: 4px;
  }

  .chat-search-btn,
  .chat-info-btn {
    width: 40px;
    height: 40px;
    padding: 0;
    font-size: 18px;
  }

  /* Messages */
  .message {
    max-width: 85%;
    padding: 10px 14px;
  }

  .message-header {
    flex-wrap: wrap;
    gap: 4px;
  }

  .message-username {
    font-size: var(--md-sys-typescale-label-medium-font-size);
  }

  .message-time {
    font-size: var(--md-sys-typescale-label-small-font-size);
  }

  .message-text {
    font-size: 15px; /* Увеличен для мобильных */
  }

  /* Input area */
  .attach-btn,
  .emoji-btn {
    width: 40px;
    height: 40px;
    padding: 0;
    font-size: 18px;
  }

  #messageInput {
    font-size: var(--md-sys-typescale-body-medium-font-size);
    padding: 10px 12px;
  }

  #sendBtn {
    width: 44px;
    height: 44px;
    padding: 0;
    font-size: 20px;
    border-radius: var(--md-sys-shape-corner-full);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  /* Hide character counter on mobile */
  .char-counter {
    display: none;
  }

  /* Search panel */
  .search-panel {
    padding: 12px 16px;
    flex-wrap: wrap;
  }

  .search-mode-toggle {
    width: 100%;
    margin-bottom: 12px;
  }

  .search-mode-btn {
    flex: 1;
    padding: 10px;
  }

  .search-input-wrapper {
    flex: 1;
    min-width: 150px;
  }

  .search-input {
    padding: 10px 40px 10px 12px;
    font-size: var(--md-sys-typescale-body-medium-font-size);
  }

  .search-global-results {
    max-height: 300px;
  }

  .search-global-item {
    padding: 12px;
  }

  /* Modals */
  .modal-content {
    width: 95% !important;
    max-width: calc(100vw - 24px) !important;
    margin: 12px;
    max-height: 90vh;
    max-height: 90dvh;
    overflow-y: auto;
    overflow-x: hidden;
    box-sizing: border-box;
    border-radius: var(--md-sys-shape-corner-extra-large);
  }

  .modal-large {
    width: 95% !important;
    max-width: calc(100vw - 24px) !important;
  }

  .modal-body {
    overflow-x: hidden;
    max-width: 100%;
    padding: 16px;
  }

  .management-container {
    flex-direction: column;
  }

  .management-sidebar {
    width: 100%;
    flex-direction: row;
    overflow-x: auto;
    padding: 12px;
    gap: 8px;
  }

  .management-nav-btn {
    white-space: nowrap;
    padding: 10px 16px;
    font-size: var(--md-sys-typescale-label-medium-font-size);
  }

  .management-content {
    padding: 16px;
  }

  /* Users table */
  .users-table {
    display: block;
    overflow-x: auto;
    font-size: var(--md-sys-typescale-body-small-font-size);
  }

  .users-table th,
  .users-table td {
    padding: 10px 12px;
    white-space: nowrap;
  }

  /* Group creation form */
  .role-buttons {
    flex-wrap: wrap;
    gap: 8px;
  }

  .role-btn {
    flex: 1 1 45%;
    padding: 10px;
    font-size: var(--md-sys-typescale-label-medium-font-size);
  }

  .members-list {
    max-height: 200px;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  .member-item {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    padding: 12px;
    gap: 12px;
  }

  .member-checkbox {
    flex: 0 0 20px;
    width: 20px;
    height: 20px;
  }

  .member-info {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
  }

  .member-name,
  .member-username {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .member-role-badge {
    flex: 0 0 auto;
    font-size: var(--md-sys-typescale-label-small-font-size);
    padding: 4px 8px;
    max-width: 70px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Emoji picker */
  .emoji-picker {
    width: 280px;
    max-width: calc(100vw - 24px);
    border-radius: var(--md-sys-shape-corner-large);
  }

  .emoji-grid {
    grid-template-columns: repeat(6, 1fr);
    gap: 6px;
    padding: 12px;
  }

  /* Images */
  .message-image {
    max-width: 100%;
    max-height: 250px;
    border-radius: var(--md-sys-shape-corner-medium);
  }

  .image-preview .preview-container {
    width: 90%;
    max-width: none;
  }

  .image-preview .preview-container img {
    max-height: 50vh;
  }

  /* Image viewer */
  .image-viewer-overlay {
    background: rgba(0, 0, 0, 0.95);
  }

  .image-viewer-header {
    padding: 12px 16px;
    background: transparent;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10;
  }

  .image-viewer-back {
    font-size: 28px;
    padding: 12px 18px;
    background: var(--md-sys-color-surface-container-high);
    border-radius: var(--md-sys-shape-corner-full);
  }

  .image-viewer-content {
    padding: 0;
  }

  .image-viewer-img {
    width: 100%;
    height: 100%;
  }

  /* Chat info */
  .chat-members-list {
    max-height: 250px;
  }

  .add-member-section {
    padding: 12px;
  }

  .role-buttons-small {
    flex-wrap: wrap;
    gap: 6px;
  }

  .role-btn-small {
    flex: 1 1 45%;
    padding: 8px;
    font-size: var(--md-sys-typescale-label-small-font-size);
  }

  /* Sidebar footer with safe area */
  .sidebar-footer {
    padding-bottom: env(safe-area-inset-bottom, 12px);
  }
}

/* ========================================
   SMALL MOBILE DEVICES (up to 480px)
   ======================================== */
@media (max-width: 480px) {
  .sidebar {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .chat-header-info h2 {
    font-size: var(--md-sys-typescale-title-small-font-size);
    max-width: 120px;
  }

  .message {
    max-width: 90%;
    padding: 8px 12px;
  }

  .message-text {
    font-size: 14px; /* Минимум 14px для читаемости */
  }

  .message-input-container {
    padding: 8px 12px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0));
  }

  .scroll-down-btn {
    bottom: 70px;
    right: 12px;
    width: 44px;
    height: 44px;
  }

  .member-item {
    padding: 10px 12px;
    gap: 8px;
  }

  .member-role-badge {
    display: none;
  }

  #sendBtn {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  /* Emoji picker full width */
  .emoji-picker {
    width: calc(100vw - 24px);
    left: 12px !important;
    max-width: none;
  }

  /* Auth screen */
  .auth-box {
    width: 95%;
    padding: 28px 20px;
    border-radius: var(--md-sys-shape-corner-large);
  }

  .auth-box h1 {
    font-size: 28px;
    line-height: 36px;
  }

  .auth-form input,
  .auth-form select,
  .auth-form button {
    padding: 12px 16px;
    font-size: var(--md-sys-typescale-body-medium-font-size);
  }

  /* Load more button */
  .load-more-btn {
    padding: 10px 16px;
    font-size: var(--md-sys-typescale-label-medium-font-size);
    margin: 8px 16px;
  }

  /* Reply panel */
  .reply-panel {
    margin: 0 12px;
    padding: 10px 16px;
  }

  .reply-panel-label {
    font-size: var(--md-sys-typescale-label-small-font-size);
  }

  .reply-panel-text {
    font-size: var(--md-sys-typescale-body-small-font-size);
  }

  /* Context menu */
  .context-menu {
    min-width: 160px;
    border-radius: var(--md-sys-shape-corner-medium);
  }

  .context-menu-item {
    padding: 14px 16px;
    font-size: var(--md-sys-typescale-body-medium-font-size);
  }

  /* Forward modal */
  .forward-chat-list {
    max-height: 250px;
  }

  .forward-chat-item {
    padding: 14px;
  }
}

/* ========================================
   LANDSCAPE MODE
   ======================================== */
@media (max-height: 500px) and (orientation: landscape) {
  .messages-container {
    padding: 8px 16px;
  }

  .message {
    padding: 6px 10px;
  }

  .message-input-container {
    padding: 8px 16px;
  }

  .search-global-results {
    max-height: 200px;
  }

  .modal-content {
    max-height: 95vh;
    border-radius: var(--md-sys-shape-corner-large);
  }
}

/* ========================================
   TOUCH DEVICE OPTIMIZATIONS
   ======================================== */
@media (hover: none) and (pointer: coarse) {
  /* Increase touch targets */
  .chat-item {
    padding: 18px 20px;
    min-height: 72px;
  }

  .contact-item {
    padding: 16px 20px;
    min-height: 64px;
  }

  .sidebar-tab {
    padding: 16px;
    min-height: 48px;
  }

  button {
    min-height: 44px;
  }

  /* Remove hover effects on touch devices */
  .chat-item:hover,
  .contact-item:hover,
  .forward-chat-item:hover {
    background: inherit;
  }

  /* Use active state instead */
  .chat-item:active,
  .contact-item:active,
  .forward-chat-item:active {
    background: var(--md-sys-color-surface-container-high);
  }

  /* Context menu on touch */
  .context-menu-item {
    padding: 16px 20px;
    min-height: 48px;
  }
}

/* ========================================
   REDUCED MOTION
   ======================================== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .sidebar {
    transition: none;
  }
}

/* ========================================
   HIGH CONTRAST MODE
   ======================================== */
@media (prefers-contrast: high) {
  .message {
    border: 1px solid var(--md-sys-color-outline);
  }

  .chat-item {
    border: 1px solid transparent;
  }

  .chat-item.active {
    border-color: var(--md-sys-color-primary);
  }

  button {
    border: 1px solid currentColor;
  }
}
