/* ============================================
   MOBILE RESPONSIVE — Claude Pro SPA
   Breakpoints: 768px (tablet), 480px (phone)
   COMPLETE MOBILE-FIRST REWRITE
   ============================================ */

/* ── Global Mobile Safeguards ── */
html, body {
  overflow-x: hidden !important;
  -webkit-text-size-adjust: 100%;
}

/* iOS safe-area support */
@supports (padding: env(safe-area-inset-bottom)) {
  .input-area {
    padding-bottom: calc(12px + env(safe-area-inset-bottom)) !important;
  }
}

/* ── Mobile Hamburger Button ── */
#mobile-menu-btn {
  display: none;
  position: fixed;
  top: 14px;
  left: 14px;
  z-index: 1001;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: rgba(30, 30, 35, 0.9);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.5);
  padding: 0;
}

#mobile-menu-btn svg {
  width: 20px;
  height: 20px;
  color: #fff;
  transition: transform 0.3s ease;
}

#mobile-menu-btn:active {
  transform: scale(0.92);
  background: rgba(139, 92, 246, 0.3);
}

/* ── Sidebar Overlay ── */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  -webkit-tap-highlight-color: transparent;
}

#sidebar-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

/* ============================================
   TABLET BREAKPOINT — 768px
   ============================================ */
@media (max-width: 768px) {

  /* Show hamburger */
  #mobile-menu-btn {
    display: flex;
  }

  /* Sidebar becomes a slide-out drawer */
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    height: 100dvh;
    width: 280px;
    max-width: 85vw;
    transform: translateX(-100%);
    transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
    border-right: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 4px 0 32px rgba(0, 0, 0, 0.5);
    border-radius: 0 !important;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #sidebar.open {
    transform: translateX(0);
  }

  #sidebar-overlay.visible {
    display: block;
  }

  /* Main content takes full width */
  #app-container {
    flex-direction: column;
  }

  #main-content {
    width: 100vw;
    height: 100vh;
    height: 100dvh;
  }

  /* ── Pages padding ── */
  .page {
    padding: 12px;
    padding-top: 68px; /* Space for hamburger */
    overflow-y: hidden; /* Kilidləyirik ki, input qaçmasın! */
    overflow-x: hidden;
  }

  /* ── Chat Page Mobile ── */
  .chat-layout {
    flex: 1;
    min-height: 0;
    width: 100%;
    display: flex;
    flex-direction: column;
  }

  .messages {
    padding: 10px 6px;
    gap: 14px;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .msg-row {
    gap: 8px;
    max-width: 100%;
  }

  .msg-row .avatar {
    width: 28px;
    height: 28px;
    font-size: 11px;
    flex-shrink: 0;
  }

  .msg-body {
    padding: 10px 12px;
    min-width: 0;
    overflow-x: hidden;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }

  .msg-name {
    font-size: 11px;
    margin-bottom: 4px;
  }

  .msg-text {
    font-size: 14px;
    line-height: 1.55;
    word-break: break-word;
    overflow-wrap: break-word;
  }

  /* Code blocks in messages — prevent horizontal overflow */
  .msg-body pre {
    padding: 8px 10px;
    font-size: 11.5px;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: pre;
    word-break: normal;
  }

  .msg-body code {
    font-size: 12px;
    word-break: break-all;
  }

  /* Message actions (copy/delete) — bigger touch targets */
  .msg-actions {
    margin-top: 6px;
    display: flex;
    gap: 4px;
  }

  .msg-actions button,
  .msg-actions .action-btn {
    min-width: 36px !important;
    min-height: 36px !important;
    padding: 6px !important;
    font-size: 16px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border-radius: 8px !important;
    background: rgba(255, 255, 255, 0.05) !important;
    border: 1px solid rgba(255, 255, 255, 0.08) !important;
    cursor: pointer;
  }

  .msg-actions button:active,
  .msg-actions .action-btn:active {
    background: rgba(139, 92, 246, 0.2) !important;
  }

  /* ── Chat Input Area ── */
  .input-area {
    padding: 10px 8px;
    background: rgba(15, 15, 20, 0.9);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
  }

  .input-container {
    gap: 6px;
    max-width: 100%;
    flex-wrap: nowrap;
  }

  .chat-textarea {
    min-height: 44px;
    max-height: 120px;
    padding: 10px 12px;
    font-size: 15px;
    border-radius: 10px;
    flex: 1;
    min-width: 0; /* prevent flex overflow */
  }

  .chat-btn {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    border-radius: 50%;
  }

  .chat-btn svg {
    width: 18px;
    height: 18px;
  }

  /* ── Welcome Screen ── */
  #welcome {
    padding: 20px 16px !important;
    max-width: 100% !important;
  }

  #welcome h2 {
    font-size: 22px !important;
    line-height: 1.3 !important;
  }

  #welcome p {
    font-size: 13px !important;
    line-height: 1.5 !important;
  }

  /* ── Code Page Mobile ── */
  #code-page .glass-panel.code-layout {
    flex-direction: column !important;
    gap: 8px !important;
    padding: 8px !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
  }

  #monaco-editor-container {
    min-height: 200px !important;
    flex: none !important;
    height: 42vh !important;
    max-height: 350px;
  }

  #code-output-container {
    flex: none !important;
    height: 30vh !important;
    min-height: 120px;
    max-height: 250px;
  }

  #run-code-btn {
    width: 100%;
    padding: 12px;
    font-size: 14px;
    min-height: 44px;
    border-radius: 10px;
  }

  #code-console {
    font-size: 12px !important;
    word-break: break-all;
  }

  /* ── Cowork Page Mobile ── */
  #cowork-page .glass-panel {
    padding: 14px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  .tasks-header h2 {
    font-size: 16px !important;
  }

  .tasks-header p {
    font-size: 12px !important;
    line-height: 1.4 !important;
  }

  .task-add-box {
    flex-direction: column !important;
    gap: 8px !important;
  }

  .task-add-box input {
    width: 100% !important;
    min-height: 44px !important;
    font-size: 14px;
    padding: 10px 12px !important;
  }

  .task-add-box button {
    width: 100% !important;
    height: 44px !important;
    justify-content: center;
    font-size: 14px;
    display: flex !important;
    align-items: center !important;
    border-radius: 10px;
  }

  .task-item {
    padding: 12px;
    word-break: break-word;
  }

  .task-item button {
    min-width: 44px;
    min-height: 44px;
  }

  /* Task item delete buttons */
  .btn-ghost {
    min-width: 44px !important;
    min-height: 36px !important;
    padding: 6px 12px !important;
    font-size: 13px !important;
  }

  /* ── Research Page Mobile ── */
  #research-page .glass-panel {
    padding: 14px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  .research-header h2 {
    font-size: 16px !important;
  }

  .research-header p {
    font-size: 12px !important;
    line-height: 1.4 !important;
  }

  #research-textarea {
    min-height: 70px !important;
    font-size: 14px;
    width: 100% !important;
  }

  .research-actions {
    flex-direction: column !important;
    gap: 8px !important;
    align-items: stretch !important;
  }

  .research-actions select {
    max-width: 100% !important;
    width: 100% !important;
    min-height: 44px;
    font-size: 14px;
    padding: 10px 30px 10px 12px;
  }

  .research-actions button {
    width: 100% !important;
    min-height: 44px;
    justify-content: center;
    font-size: 14px;
    display: flex !important;
    align-items: center !important;
    border-radius: 10px;
  }

  .research-step {
    padding: 10px 12px;
    word-break: break-word;
    overflow-wrap: break-word;
  }

  .research-step-title {
    font-size: 14px;
  }

  .research-output {
    -webkit-overflow-scrolling: touch;
  }

  /* Research step content overflow fix */
  .step-body {
    overflow-x: hidden;
    word-break: break-word;
    overflow-wrap: break-word;
  }

  .step-body pre {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 11px;
    max-width: 100%;
  }

  /* ── Sidebar Footer Mobile ── */
  .sidebar-footer {
    padding: 16px;
  }

  .model-selector {
    font-size: 14px;
    min-height: 44px;
    padding: 10px 30px 10px 12px;
  }

  #export-btn {
    min-height: 44px !important;
    font-size: 14px !important;
  }

  /* ── Nav Links Touch Targets ── */
  .nav-link {
    padding: 14px 16px;
    font-size: 15px;
    min-height: 48px;
    display: flex;
    align-items: center;
  }

  .nav-link svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
  }

  /* ── Toast Mobile ── */
  #toast {
    bottom: 16px;
    right: 16px;
    left: 16px;
    text-align: center;
    font-size: 13px;
  }

  /* ── File strip overflow fix ── */
  #file-strip {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }

  /* ── Images in messages ── */
  .msg-body img,
  .msg-text img {
    max-width: 100% !important;
    height: auto !important;
    border-radius: 8px;
  }

  /* ── Glass panel overflow ── */
  .glass-panel {
    max-width: 100%;
    overflow-x: hidden;
  }

  /* ── Table overflow in messages ── */
  .msg-body table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 12px;
  }

  /* ── Prevent body scroll when sidebar is open ── */
  body.sidebar-open {
    overflow: hidden !important;
    position: fixed;
    width: 100%;
  }
}

/* ============================================
   PHONE BREAKPOINT — 480px
   ============================================ */
@media (max-width: 480px) {

  .page {
    padding: 8px;
    padding-top: 60px;
  }

  /* ── Chat ── */
  .messages {
    padding: 6px 2px;
    gap: 10px;
  }

  .msg-row {
    gap: 6px;
  }

  .msg-row .avatar {
    width: 24px;
    height: 24px;
    font-size: 10px;
  }

  .msg-body {
    padding: 8px 10px;
  }

  .msg-name {
    font-size: 10px;
  }

  .msg-text {
    font-size: 13.5px;
    line-height: 1.5;
  }

  .msg-body pre {
    font-size: 10.5px;
    padding: 6px 8px;
  }

  .input-area {
    padding: 8px 6px;
  }

  .input-container {
    gap: 4px;
  }

  .chat-textarea {
    min-height: 40px;
    max-height: 100px;
    padding: 8px 10px;
    font-size: 14px;
  }

  .chat-btn {
    width: 36px;
    height: 36px;
  }

  .chat-btn svg {
    width: 16px;
    height: 16px;
  }

  /* ── Code ── */
  #monaco-editor-container {
    height: 38vh !important;
    min-height: 180px !important;
    max-height: 300px;
  }

  #code-output-container {
    height: 28vh !important;
    min-height: 100px;
  }

  /* ── Cowork ── */
  .tasks-header h2 {
    font-size: 15px !important;
  }

  /* ── Research ── */
  .research-header h2 {
    font-size: 15px !important;
  }

  #research-textarea {
    min-height: 60px !important;
  }

  /* ── Welcome ── */
  #welcome h2 {
    font-size: 19px !important;
  }

  #welcome p {
    font-size: 12px !important;
  }

  /* ── Sidebar ── */
  #sidebar {
    width: 260px;
  }
}

/* ============================================
   SMALL PHONE — 360px and below
   ============================================ */
@media (max-width: 360px) {
  .page {
    padding: 6px;
    padding-top: 56px;
  }

  .chat-btn {
    width: 34px;
    height: 34px;
  }

  .chat-textarea {
    font-size: 13px;
    padding: 7px 8px;
  }

  #welcome h2 {
    font-size: 17px !important;
  }

  #sidebar {
    width: 240px;
  }
}

/* ============================================
   LANDSCAPE PHONE FIX
   ============================================ */
@media (max-height: 500px) and (max-width: 900px) {
  .page {
    padding-top: 52px;
  }

  .messages {
    padding: 6px;
  }

  .input-area {
    padding: 6px 8px;
  }

  .chat-textarea {
    min-height: 36px;
    max-height: 80px;
  }

  #monaco-editor-container {
    height: 50vh !important;
    min-height: 180px !important;
  }

  /* Welcome screen condensed for landscape */
  #welcome h2 {
    font-size: 18px !important;
    margin-bottom: 4px !important;
  }

  #welcome p {
    font-size: 12px !important;
  }
}

/* ============================================
   SMOOTH TRANSITIONS & TOUCH ENHANCEMENTS
   ============================================ */

/* Prevent pull-to-refresh interfering */
@media (max-width: 768px) {
  body {
    overscroll-behavior-y: contain;
  }

  /* Prevent text selection on interactive elements */
  button, a, .nav-link {
    -webkit-user-select: none;
    user-select: none;
  }
}

/* Touch highlight removal & target sizing */
@media (pointer: coarse) {
  * {
    -webkit-tap-highlight-color: transparent;
  }

  /* Ensure all interactive elements meet touch target */
  button, a, select, input[type="button"], input[type="submit"] {
    min-height: 44px;
    min-width: 44px;
  }

  /* Smooth momentum scrolling */
  .messages,
  .tasks-list,
  .research-output,
  #code-console,
  #research-output {
    -webkit-overflow-scrolling: touch;
  }

  /* Hover effects disabled for touch */
  .nav-link:hover {
    background: transparent;
  }
  .nav-link.active:hover {
    background: rgba(139, 92, 246, 0.4);
  }
}

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