/**
 * ボトムシートUI - モバイル最適化スタイル
 * AR表示領域を最大化しながら、快適なチャット操作を提供
 */

html.keyboard-overlay-active,
body.keyboard-overlay-active {
  overflow: hidden !important;
  overscroll-behavior: none !important;
  touch-action: manipulation;
}

body.keyboard-overlay-active {
  position: fixed !important;
  top: calc(-1 * var(--locked-scroll-y, 0px)) !important;
  left: 0 !important;
  right: 0 !important;
  width: 100% !important;
  height: var(--ar-layout-height, 100vh) !important;
}

.bottom-sheet {
  position: fixed;
  inset: 0;
  left: 0;
  right: 0;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
  transition: none;
  z-index: 9999; /* a-sceneより上に配置 */
  display: block;
  pointer-events: none; /* 背景はクリックを透過 */

  /* GPUアクセラレーション */
  transform: translateZ(0);
  will-change: transform;

  --keyboard-offset: 0px;
  --chat-input-height: 84px;
}

/* ドラッグハンドル */
.drag-handle {
  position: fixed;
  left: 50%;
  bottom: calc(var(--keyboard-offset) + var(--chat-input-height) + 10px);
  width: 40px;
  height: 4px;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 2px;
  margin: 0;
  cursor: grab;
  display: none;
  transform: translateX(-50%);
  pointer-events: auto;
  z-index: 10001;
}

.drag-handle:active {
  cursor: grabbing;
}

/* メッセージプレビューエリア */
.messages-preview {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: calc(var(--keyboard-offset) + var(--chat-input-height) + 18px);
  max-height: min(38dvh, 320px);
  padding: 8px 4px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 9999;
  pointer-events: auto;

  /* スムーズなスクロール */
  -webkit-overflow-scrolling: touch;

  /* 初期状態は非表示 */
  display: none;
}

.bottom-sheet.state-peek .drag-handle,
.bottom-sheet.state-expanded .drag-handle {
  display: block;
}

.bottom-sheet.state-expanded .messages-preview {
  max-height: min(58dvh, 520px);
}

.attachment-preview {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: calc(var(--keyboard-offset) + var(--chat-input-height) + 8px);
  display: none;
  z-index: 10000;
  pointer-events: auto;
}

.attachment-pill,
.attachment-error {
  width: fit-content;
  max-width: min(82vw, 420px);
  margin-left: auto;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(10px);
  color: #333;
  font-size: 13px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
}

.attachment-pill {
  display: flex;
  align-items: center;
  gap: 8px;
}

.attachment-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-remove {
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.12);
  color: #333;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
}

.attachment-error {
  color: #9b1c1c;
}

/* スクロールバーのスタイル（Webkit） */
.messages-preview::-webkit-scrollbar {
  width: 4px;
}

.messages-preview::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
}

.messages-preview::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
}

.messages-preview::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.5);
}

/* メッセージバブル */
.message-bubble {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(10px);
  max-width: 80%;
  word-wrap: break-word;
  animation: slideIn 0.2s ease-out;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message-bubble.message-user {
  align-self: flex-end;
  background: rgba(102, 126, 234, 0.78);
  color: white;
}

.message-bubble.message-assistant {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.78);
  color: #333;
}

.message-avatar {
  font-size: 20px;
  flex-shrink: 0;
}

.message-text {
  flex: 1;
  font-size: 14px;
  line-height: 1.5;
}

/* タイピングインジケーター */
.typing-indicator .message-text {
  display: flex;
  gap: 4px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  background: #666;
  border-radius: 50%;
  animation: typingBounce 1.4s infinite;
}

.typing-dot:nth-child(1) {
  animation-delay: 0s;
}

.typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typingBounce {
  0%, 60%, 100% {
    transform: translateY(0);
  }
  30% {
    transform: translateY(-10px);
  }
}

/* 入力コンテナ */
.input-container {
  position: fixed; /* fixedで配置してキーボードの影響を最小化 */
  bottom: 0;
  left: 0;
  right: 0;
  margin: 0;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.70); /* ARを隠し切らない半透明レイヤー */
  backdrop-filter: blur(14px);
  border-radius: 16px 16px 0 0;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  z-index: 10000; /* 最前面に配置 */
  pointer-events: auto; /* クリック可能 */
  transform: translate3d(0, calc(-1 * var(--keyboard-offset)), 0);
  will-change: transform;
}

.bottom-sheet-file {
  display: none;
}

.bottom-sheet-attach {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.82);
  color: #333;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.bottom-sheet-input {
  flex: 1;
  min-width: 0;
  padding: 12px 16px;
  border: none;
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.82);
  font-size: 16px;
  outline: none;
  transition: background 0.2s;
}

.bottom-sheet-input:focus {
  background: rgba(255, 255, 255, 1);
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.3);
}

.bottom-sheet-send {
  padding: 12px 24px;
  border: none;
  border-radius: 24px;
  background: #667eea;
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.bottom-sheet-attach:active,
.attachment-remove:active {
  transform: scale(0.96);
}

.bottom-sheet-send:hover {
  background: #5568d3;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.bottom-sheet-send:active {
  transform: translateY(0);
}

/* モバイル最適化 */
@media (max-width: 768px) {
  .bottom-sheet {
    /* タブレットでも同じ動作 */
  }

  .message-bubble {
    max-width: 85%;
  }

  .bottom-sheet-input {
    font-size: 16px; /* iOS Safariのズーム防止 */
  }
}

/* 横画面対応 */
@media (orientation: landscape) and (max-height: 600px) {
  .bottom-sheet {
    --chat-input-height: 68px;
  }

  .messages-preview {
    max-height: min(34dvh, 160px);
  }

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

  .bottom-sheet-attach {
    width: 40px;
    height: 40px;
  }
}

/* 小型デバイス対応 */
@media (max-width: 375px) {
  .bottom-sheet-input {
    font-size: 14px;
  }

  .bottom-sheet-send {
    padding: 12px 16px;
    font-size: 14px;
  }

  .bottom-sheet-attach {
    width: 40px;
    height: 40px;
  }

  .message-text {
    font-size: 13px;
  }
}

/* セーフエリア対応（ノッチ、ホームインジケーター） */
@supports (padding: max(0px)) {
  .input-container {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
}

/* ダークモード対応 */
@media (prefers-color-scheme: dark) {
  .message-bubble.message-assistant {
    background: rgba(50, 50, 50, 0.95);
    color: #fff;
  }

  .input-container {
    background: rgba(30, 30, 30, 0.3);
  }

  .attachment-pill,
  .attachment-error,
  .bottom-sheet-attach {
    background: rgba(50, 50, 50, 0.6);
    color: #fff;
  }

  .attachment-remove {
    background: rgba(255, 255, 255, 0.14);
    color: #fff;
  }

  .bottom-sheet-input {
    background: rgba(50, 50, 50, 0.6);
    color: #fff;
  }

  .bottom-sheet-input:focus {
    background: rgba(60, 60, 60, 0.8);
  }

  .typing-dot {
    background: #999;
  }
}

/* アクセシビリティ: 高コントラストモード */
@media (prefers-contrast: high) {
  .bottom-sheet {
    outline: 2px solid #000;
  }

  .message-bubble {
    border: 1px solid #000;
  }

  .bottom-sheet-input {
    border: 2px solid #000;
  }
}

/* リデュースモーション対応 */
@media (prefers-reduced-motion: reduce) {
  .bottom-sheet,
  .message-bubble,
  .bottom-sheet-send {
    transition: none;
    animation: none;
  }

  .typing-dot {
    animation: none;
  }
}
