/* リセット & ベース */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #f0f2f5;
  color: #1a1a1a;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ===== ログインページ ===== */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: #fff;
  border-radius: 16px;
  padding: 48px 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
  text-align: center;
}

.login-card h1 {
  font-size: 1.8rem;
  margin-bottom: 8px;
}

.login-card p {
  color: #666;
  margin-bottom: 24px;
}

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-card input {
  padding: 12px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}

.login-card input:focus {
  border-color: #4f87f5;
}

.login-card button {
  padding: 12px;
  background: #4f87f5;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.login-card button:hover {
  background: #3a72e0;
}

.error {
  color: #e53e3e;
  margin-top: 8px;
  font-size: 0.9rem;
  min-height: 20px;
}

/* ===== チャットページ ===== */
.chat-page {
  overflow: hidden;
}

.chat-layout {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* サイドバー */
.sidebar {
  width: 220px;
  background: #1e2a3a;
  color: #cbd5e0;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  padding: 20px 16px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-header h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #a0aec0;
  font-weight: 600;
}

.user-count {
  background: #4f87f5;
  color: #fff;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 1px 7px;
}

#userList {
  list-style: none;
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

#userList li {
  padding: 8px 16px;
  font-size: 0.9rem;
  border-radius: 6px;
  margin: 2px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

#userList li::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  background: #48bb78;
  border-radius: 50%;
  flex-shrink: 0;
}

#userList li.user-me {
  background: rgba(79, 135, 245, 0.2);
  color: #90cdf4;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.my-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: #e2e8f0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#leaveBtn {
  background: rgba(229, 62, 62, 0.2);
  color: #fc8181;
  border: 1px solid rgba(229, 62, 62, 0.4);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 0.8rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
}

#leaveBtn:hover {
  background: rgba(229, 62, 62, 0.4);
}

/* メインエリア */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  padding: 16px 24px;
  background: #fff;
  border-bottom: 1px solid #e2e8f0;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.chat-header h1 {
  font-size: 1.1rem;
  font-weight: 700;
}

/* メッセージ一覧 */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: 65%;
}

.message-other {
  align-self: flex-start;
  align-items: flex-start;
}

.message-mine {
  align-self: flex-end;
  align-items: flex-end;
}

.message-sender {
  font-size: 0.75rem;
  font-weight: 600;
  color: #718096;
  margin-bottom: 4px;
}

.message-mine .message-sender {
  display: none;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 0.95rem;
  line-height: 1.5;
  word-break: break-word;
}

.message-other .message-bubble {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-top-left-radius: 4px;
}

.message-mine .message-bubble {
  background: #4f87f5;
  color: #fff;
  border-top-right-radius: 4px;
}

.message-time {
  font-size: 0.7rem;
  color: #a0aec0;
  margin-top: 4px;
}

/* システムメッセージ */
.system-message {
  text-align: center;
  font-size: 0.8rem;
  color: #a0aec0;
  background: rgba(0, 0, 0, 0.04);
  border-radius: 999px;
  padding: 4px 16px;
  align-self: center;
}

.system-error {
  color: #e53e3e;
  background: rgba(229, 62, 62, 0.08);
}

/* 入力フォーム */
.message-form {
  display: flex;
  gap: 10px;
  padding: 16px 24px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid #e2e8f0;
}

.message-form input {
  flex: 1;
  padding: 10px 16px;
  border: 2px solid #e2e8f0;
  border-radius: 24px;
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
}

.message-form input:focus {
  border-color: #4f87f5;
}

.message-form button {
  padding: 10px 20px;
  background: #4f87f5;
  color: #fff;
  border: none;
  border-radius: 24px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.message-form button:hover {
  background: #3a72e0;
}

/* スクロールバー */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 3px;
}

/* ===== ヘッダーのメニューボタン（PC では非表示） ===== */
.menu-btn {
  display: none;
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: #1a1a1a;
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
}

.menu-btn:hover {
  background: #f0f2f5;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-header h1 {
  flex: 1;
}

.header-user-count {
  display: none;
}

/* ===== モバイル (600px 以下) ===== */
@media (max-width: 600px) {
  /* サイドバーをオーバーレイに切り替え（フローから外してメインが全幅を取る） */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    width: 240px;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 2px 0 16px rgba(0, 0, 0, 0.3);
  }

  /* サイドバーが fixed になってもレイアウト上の幅が残らないよう上書き */
  .chat-layout {
    overflow: hidden;
    width: 100vw;
  }

  .chat-main {
    width: 100vw;
    min-width: 0;
  }

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

  /* オーバーレイ背景 */
  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 99;
  }

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

  /* ハンバーガーボタンを表示 */
  .menu-btn {
    display: block;
  }

  /* ヘッダーのユーザー数バッジを表示 */
  .header-user-count {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.8rem;
    color: #718096;
    white-space: nowrap;
  }

  /* メッセージ幅を広げる */
  .message {
    max-width: 82%;
  }

  /* ログインカードの余白調整 */
  .login-card {
    border-radius: 0;
    padding: 48px 24px;
    box-shadow: none;
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  /* iOS でフォームにズームが入らないよう 16px に */
  .login-card input,
  .message-form input {
    font-size: 16px;
  }

  /* 入力エリアの余白を詰める */
  .messages {
    padding: 12px 12px;
  }

  .message-form {
    padding: 10px 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

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