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

:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --bg-sidebar: #111827;
  --bg-sidebar-hover: #1f2937;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --accent: #38bdf8;
  --accent-hover: #0ea5e9;
  --user-msg-bg: #0d9488;
  --ai-msg-bg: #334155;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #22c55e;
  --radius: 12px;
  --shadow: 0 4px 6px -1px rgba(0,0,0,0.3);
  --header-h: 52px;
  --sidebar-w: 280px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans JP", sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

/* === アプリ全体レイアウト === */
#app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  height: -webkit-fill-available;
  overflow: hidden;
}

/* === サイドバー === */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--bg-tertiary);
  display: flex;
  flex-direction: column;
  z-index: 60;
  transition: transform 0.25s ease;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  padding-top: max(12px, var(--safe-top));
  border-bottom: 1px solid var(--bg-tertiary);
  flex-shrink: 0;
}

.btn-new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: transparent;
  border: 1px solid var(--bg-tertiary);
  color: var(--text-primary);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-new-chat:hover {
  background: var(--bg-sidebar-hover);
}

.sidebar-section {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 0;
  -webkit-overflow-scrolling: touch;
}

.sidebar-section h3 {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  padding: 0 14px 8px;
}

.chat-list {
  display: flex;
  flex-direction: column;
}

.empty-list {
  padding: 20px 14px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  cursor: pointer;
  transition: background 0.15s;
  border-radius: 0;
  -webkit-tap-highlight-color: transparent;
}

.chat-item:hover,
.chat-item.active {
  background: var(--bg-sidebar-hover);
}

.chat-item-info {
  flex: 1;
  min-width: 0;
}

.chat-item-title {
  font-size: 0.88rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-meta {
  font-size: 0.72rem;
  color: var(--text-secondary);
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-actions {
  display: none;
  gap: 2px;
  flex-shrink: 0;
}

.chat-item:hover .chat-item-actions {
  display: flex;
}

.chat-action-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 0.85rem;
  padding: 4px;
  cursor: pointer;
  border-radius: 4px;
  -webkit-tap-highlight-color: transparent;
}

.chat-action-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.sidebar-footer {
  padding: 10px 14px;
  padding-bottom: max(10px, var(--safe-bottom));
  border-top: 1px solid var(--bg-tertiary);
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}

.btn-sidebar-action {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 8px 4px;
  text-align: left;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 6px;
  transition: color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-sidebar-action:hover {
  color: var(--text-primary);
  background: var(--bg-sidebar-hover);
}

/* モバイルサイドバー */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 55;
}

@media (max-width: 767px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
  }
}

@media (min-width: 768px) {
  .sidebar-overlay {
    display: none !important;
  }
}

/* === メインエリア === */
.main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* === ヘッダー === */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  padding-top: max(10px, var(--safe-top));
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--bg-tertiary);
  flex-shrink: 0;
  min-height: var(--header-h);
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.app-header h1 {
  font-size: 1.05rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  transition: background 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover {
  background: var(--bg-tertiary);
}

/* === チャットコンテンツ === */
.chat-container {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  position: relative;
  -webkit-overflow-scrolling: touch;
}

/* ウェルカム */
.welcome-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  padding: 24px;
}

.welcome-content {
  max-width: 480px;
  text-align: center;
}

.welcome-content h2 {
  font-size: 1.35rem;
  margin-bottom: 12px;
}

.welcome-content p {
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 14px;
}

.model-info {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 16px;
  margin: 14px 0;
  text-align: left;
}

.model-info p {
  margin-bottom: 4px;
  color: var(--text-primary);
}

.model-info .note {
  font-size: 0.82rem;
  color: var(--text-secondary);
}

.btn-primary {
  background: var(--accent);
  color: #000;
  border: none;
  padding: 12px 24px;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-primary:active {
  opacity: 0.9;
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: none;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.88rem;
  cursor: pointer;
  transition: background 0.2s;
  width: 100%;
  text-align: left;
  margin-bottom: 8px;
  -webkit-tap-highlight-color: transparent;
}

.btn-secondary:hover {
  background: var(--bg-sidebar-hover);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.88rem;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
  text-align: left;
  -webkit-tap-highlight-color: transparent;
}

.btn-danger:hover {
  background: var(--danger);
  color: #fff;
}

/* チャット画面 */
.chat-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 12px 12px 0;
}

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 8px;
  -webkit-overflow-scrolling: touch;
}

.message {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.5;
  word-wrap: break-word;
  animation: fadeIn 0.25s ease;
  font-size: 0.95rem;
}

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

.message.user {
  align-self: flex-end;
  background: var(--user-msg-bg);
  border-bottom-right-radius: 4px;
}

.message.assistant {
  align-self: flex-start;
  background: var(--ai-msg-bg);
  border-bottom-left-radius: 4px;
}

.message .role {
  font-size: 0.7rem;
  color: var(--text-secondary);
  margin-bottom: 3px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

/* ローディング */
.loading-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  gap: 14px;
  padding: 24px;
}

.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid var(--bg-tertiary);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.progress-container {
  width: 85%;
  max-width: 360px;
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.3s ease;
}

.progress-detail {
  font-size: 0.78rem;
  color: var(--text-secondary);
  text-align: center;
  max-width: 90%;
  word-break: break-all;
}

/* タイピング */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 10px 14px;
  align-self: flex-start;
  background: var(--ai-msg-bg);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  margin-bottom: 4px;
  flex-shrink: 0;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* === 入力エリア === */
.input-area {
  flex-shrink: 0;
  padding: 8px 12px;
  padding-bottom: max(8px, var(--safe-bottom));
  background: var(--bg-secondary);
  border-top: 1px solid var(--bg-tertiary);
}

.input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.input-row textarea {
  flex: 1 1 auto;
  background: var(--bg-tertiary);
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text-primary);
  font-size: 16px;
  line-height: 1.4;
  resize: none;
  outline: none;
  transition: border-color 0.2s;
  max-height: 110px;
  min-height: 44px;
  font-family: inherit;
  -webkit-appearance: none;
}

.input-row textarea:focus {
  border-color: var(--accent);
}

.input-row textarea:disabled {
  opacity: 0.45;
}

.input-row button {
  flex: 0 0 auto;
  background: var(--accent);
  color: #000;
  border: none;
  padding: 0 18px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.2s;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}

.input-row button:hover:not(:disabled) {
  background: var(--accent-hover);
}

.input-row button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.status-bar {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  font-size: 0.72rem;
  color: var(--text-secondary);
  padding: 0 2px;
}

/* === モーダル === */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  padding: 0;
}

@media (min-width: 640px) {
  .modal {
    align-items: center;
    padding: 16px;
  }
}

.modal-content {
  background: var(--bg-secondary);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  -webkit-overflow-scrolling: touch;
}

@media (min-width: 640px) {
  .modal-content {
    border-radius: var(--radius);
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--bg-tertiary);
  position: sticky;
  top: 0;
  background: var(--bg-secondary);
  z-index: 2;
}

.modal-header h2 {
  font-size: 1.05rem;
}

.modal-body {
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.modal-body section h3 {
  font-size: 0.92rem;
  margin-bottom: 8px;
  color: var(--text-primary);
}

.modal-body label {
  display: block;
  font-size: 0.88rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.modal-body input[type="text"],
.modal-body select,
.modal-body textarea {
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  background: var(--bg-tertiary);
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 16px;
  outline: none;
  font-family: inherit;
  -webkit-appearance: none;
}

.modal-body select {
  padding-right: 28px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

.modal-body input:focus,
.modal-body select:focus,
.modal-body textarea:focus {
  border-color: var(--accent);
}

.modal-body input[type="range"] {
  width: 100%;
  margin-top: 6px;
  -webkit-appearance: none;
  appearance: none;
  height: 5px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  outline: none;
}

.modal-body input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
}

.modal-body .hint {
  font-size: 0.78rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

.modal-footer {
  padding: 12px 16px;
  padding-bottom: max(12px, var(--safe-bottom));
  border-top: 1px solid var(--bg-tertiary);
  display: flex;
  justify-content: flex-end;
  position: sticky;
  bottom: 0;
  background: var(--bg-secondary);
}

/* === トースト === */
.toast {
  position: fixed;
  bottom: 88px;
  bottom: calc(88px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-secondary);
  color: var(--text-primary);
  padding: 10px 18px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  z-index: 200;
  transition: opacity 0.3s;
  max-width: 90vw;
  text-align: center;
  pointer-events: none;
}

.toast.error {
  background: var(--danger);
  color: #fff;
}

.toast.success {
  background: var(--success);
  color: #fff;
}

/* === ユーティリティ === */
.hidden {
  display: none !important;
}

/* === タブレット・iPad対応 === */
@media (min-width: 768px) {
  .message {
    max-width: 75%;
    font-size: 1rem;
    padding: 12px 16px;
  }

  .input-row textarea {
    font-size: 16px;
    padding: 12px 14px;
  }

  .input-row button {
    padding: 0 22px;
    font-size: 1rem;
  }
}

@media (min-width: 1024px) {
  .message {
    max-width: 65%;
  }
}

/* === iOSデバイス固有 === */
@supports (-webkit-touch-callout: none) {
  #app {
    height: -webkit-fill-available;
  }

  .chat-container {
    overscroll-behavior-y: contain;
  }
}
