/* ========================================================
   Mobile — responsive layout for iPhone / iPad / small screens.
   Breakpoints:
     <= 480px   → Phone portrait
     <= 768px   → Phone landscape / small tablet
     <= 1024px  → Tablet portrait
   ======================================================== */

/* ==========================================================
   Phone: <= 480px (iPhone portrait)
   ========================================================== */

@media (max-width: 480px) {
  :root {
    --sidebar-w: 280px;
    --topbar-h: 48px;
    --font-size-sm: 14px;
    --font-size-base: 15px;
    --spacing-xl: 14px;
    --spacing-2xl: 16px;
  }

  /* -- Viewport height: use dvh to handle Safari address bar -- */
  html, body, #app {
    height: 100dvh;
  }

  .app-shell {
    height: 100dvh;
  }

  /* Prevent iOS text size adjustment on rotate */
  html {
    -webkit-text-size-adjust: 100%;
  }

  /* --- Sidebar: overlay mode --- */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    width: var(--sidebar-w);
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: var(--shadow-lg);
  }

  .sidebar:not(.collapsed) {
    transform: translateX(0);
  }

  .sidebar.collapsed {
    width: var(--sidebar-w);
    transform: translateX(-100%);
  }

  /* Swipe hint on left edge */
  .swipe-hint {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 20px;
    z-index: 50;
    /* invisible touch zone for edge-swipe detection */
  }

  /* Backdrop when sidebar is open */
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 99;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
  }

  .sidebar:not(.collapsed) ~ .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* Sidebar toggle — always visible hamburger */
  .sidebar-toggle {
    opacity: 1;
    right: -20px;
    border-right: 1px solid var(--c-border);
    border-left: none;
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  }

  .sidebar:not(.collapsed) .sidebar-toggle {
    right: 0;
    border-right: none;
    border-left: 1px solid var(--c-border);
    border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  }

  /* --- Topbar --- */
  .topbar {
    height: var(--topbar-h);
    padding: 0 var(--spacing-lg);
    gap: var(--spacing-sm);
  }

  .topbar .title {
    font-size: var(--font-size-base);
  }

  .topbar .stats {
    font-size: var(--font-size-xs);
  }

  .topbar-menu-btn {
    width: 36px;
    height: 36px;
  }

  /* --- Messages --- */
  .messages {
    padding: var(--spacing-lg) var(--spacing-md);
    gap: var(--spacing-lg);
  }

  .msg-row {
    max-width: 95%;
    gap: var(--spacing-sm);
  }

  .msg-avatar {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }

  /* --- Message bubbles --- */
  .msg-bubble {
    font-size: var(--font-size-base);
    line-height: 1.55;
    padding: 10px var(--spacing-md);
    border-radius: var(--radius-lg);
  }

  .msg-row.user .msg-bubble {
    border-top-right-radius: var(--radius-sm);
  }

  .msg-row.assistant .msg-bubble {
    border-top-left-radius: var(--radius-sm);
  }

  /* --- Code blocks --- */
  .msg-bubble pre {
    font-size: var(--font-size-sm);
    padding: var(--spacing-sm);
    margin: var(--spacing-sm) -8px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    max-width: calc(100vw - 80px);
  }

  .msg-bubble code {
    font-size: var(--font-size-sm);
  }

  /* --- Tool cards --- */
  .tool-card {
    margin: var(--spacing-sm) -4px;
  }

  .tool-card .tool-header {
    font-size: var(--font-size-sm);
    padding: 8px 10px;
  }

  /* --- Input area --- */
  .input-area {
    padding: 8px 12px;
    padding-bottom: max(8px, env(safe-area-inset-bottom, 8px));
    transition: padding-bottom 0.2s ease;
  }

  .input-area .input-row {
    gap: 6px;
  }

  /* Keyboard avoidance */
  .input-area.keyboard-open {
    padding-bottom: calc(var(--keyboard-h, 0px) + 8px);
  }

  .input-area textarea,
  .input-area .input-field {
    min-height: 40px;
    max-height: 120px;
    font-size: var(--font-size-base);
    padding: 8px 12px;
    border-radius: 20px;
  }

  .input-area button[type="primary"] {
    min-width: 60px !important;
    padding: 0 10px !important;
    font-size: 14px !important;
  }

  .input-hint {
    display: none;
  }

  /* --- File bar --- */
  .file-bar {
    padding: var(--spacing-sm) var(--spacing-md);
    gap: 6px;
    font-size: var(--font-size-xs);
  }

  .file-bar .file-item .file-name {
    max-width: 100px;
  }

  /* --- Empty state --- */
  .empty-state .logo {
    font-size: 48px;
  }

  .empty-state .title {
    font-size: var(--font-size-xl);
  }

  .empty-state .hint {
    font-size: var(--font-size-base);
    max-width: 280px;
  }

  /* --- Dialogs / Modals --- */
  .dialog-overlay .dialog {
    width: calc(100vw - 40px);
    max-width: 400px;
    margin: 20px;
    border-radius: var(--radius-xl);
  }

  .dialog-overlay .dialog-title {
    font-size: var(--font-size-lg);
  }

  .dialog-overlay .dialog-body {
    font-size: var(--font-size-base);
  }

  /* --- Plan approval --- */
  .plan-approval {
    max-height: 60vh;
  }

  /* --- Permission dialogs --- */
  .permission-dialog {
    max-width: calc(100vw - 32px);
  }

  /* --- Search --- */
  .search-dialog .search-results {
    max-height: 50vh;
  }

  /* --- Task panel: full-width bottom sheet --- */
  .task-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100vw;
    max-width: 100vw;
    max-height: 55vh;
    z-index: 80;
    border-left: none;
    border-top: 1px solid var(--c-border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
  }

  /* --- Settings --- */
  .settings-panel {
    width: 100vw;
    max-width: 100vw;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }
}

/* ==========================================================
   Phone landscape / small tablet: 481px — 768px
   ========================================================== */

@media (min-width: 481px) and (max-width: 768px) {
  :root {
    --sidebar-w: 240px;
    --topbar-h: 52px;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    width: var(--sidebar-w);
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: var(--shadow-lg);
  }

  .sidebar:not(.collapsed) {
    transform: translateX(0);
  }

  .sidebar.collapsed {
    width: var(--sidebar-w);
    transform: translateX(-100%);
  }

  .messages {
    padding: var(--spacing-2xl) var(--spacing-xl);
  }

  .msg-row {
    max-width: 80%;
  }

  .input-area {
    padding-bottom: max(var(--spacing-sm), env(safe-area-inset-bottom, 8px));
  }
}

/* ==========================================================
   Tablet: 769px — 1024px
   ========================================================== */

@media (min-width: 769px) and (max-width: 1024px) {
  :root {
    --sidebar-w: 230px;
    --topbar-h: 56px;
  }

  .msg-row {
    max-width: 78%;
  }
}

/* ==========================================================
   Universal: safe-area handling for notched iPhones
   ========================================================== */

@supports (padding: env(safe-area-inset-top)) {
  /* Notched devices — ensure content stays in safe area */
  .topbar {
    padding-top: env(safe-area-inset-top, 0px);
  }

  .sidebar {
    padding-top: env(safe-area-inset-top, 0px);
  }

  .sidebar-brand {
    padding-top: max(var(--spacing-xl), env(safe-area-inset-top, 16px));
  }

  .input-area {
    padding-bottom: max(var(--spacing-sm), env(safe-area-inset-bottom, 8px));
  }
}

/* ==========================================================
   Universal: touch-friendly interactions
   ========================================================== */

@media (pointer: coarse) {
  /* Remove tap highlight */
  * {
    -webkit-tap-highlight-color: transparent;
  }

  /* Increase tap target sizes for touch */
  .sidebar-item {
    padding-top: 12px;
    padding-bottom: 12px;
    min-height: 44px;
  }

  button, .btn, [role="button"] {
    min-height: 44px;
    min-width: 44px;
  }

  /* Disable hover states on touch (they stick after tap) */
  .sidebar-item:hover {
    background: transparent;
  }

  .sidebar-item:active {
    background: var(--c-surface-hover);
  }

  .sidebar-item.active:hover {
    background: var(--c-accent-light);
  }

  /* Scroll momentum */
  .messages, .sidebar-list {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  /* Prevent pull-to-refresh on body */
  body {
    overscroll-behavior: none;
  }
}

/* ==========================================================
   Universal: dark-mode notch safe area on OLED iPhones
   ========================================================== */

@media (prefers-color-scheme: dark) {
  /* iPhone OLED: pure black background behind notch for seamless look */
  [data-theme="dark"] {
    --c-bg-safe: #000000;
  }
}

/* ==========================================================
   Voice input — record button with pulse animation
   ========================================================== */

.voice-record-btn {
  flex: 1;
  height: 44px;
  border-radius: 22px;
  border: none;
  background: #ef4444;
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.voice-record-btn.recording {
  animation: voice-pulse 1.2s ease-in-out infinite;
  background: #dc2626;
  transform: scale(1.03);
}
.voice-record-btn.uploading {
  background: #6b7280;
  animation: none;
}
.voice-record-btn:disabled {
  opacity: 0.8;
}
@keyframes voice-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  50%      { box-shadow: 0 0 0 14px rgba(239, 68, 68, 0); }
}
