/* ====================================================
   閉ざされた時の庭 — スタイルシート
   ゴシックダーク + 羊皮紙風吹き出し
   ==================================================== */

/* --- リセット & 基盤 --- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  /* 庭の色彩 */
  --bg-deep:        #0a0a0f;
  --bg-surface:     #12121a;
  --bg-elevated:    #1a1a28;

  /* 羊皮紙の色（魔法使いの吹き出し） */
  --parchment:        rgba(215, 198, 170, 0.92);
  --parchment-dark:   rgba(190, 170, 140, 0.95);
  --parchment-border: rgba(160, 130, 90, 0.45);
  --parchment-text:   #3a2e20;

  /* 君の吹き出し */
  --user-bg:          rgba(50, 42, 60, 0.8);
  --user-border:      rgba(120, 90, 160, 0.3);
  --user-text:        #d0c8de;

  /* アクセント */
  --accent-gold:    #c9a84c;
  --accent-wine:    #8b1a1a;
  --accent-wine-glow: rgba(139, 26, 26, 0.4);

  /* テキスト */
  --text-primary:   #e0d8c8;
  --text-secondary: #8a7f6e;
  --text-dim:       #5a5347;

  /* フォント */
  --font-jp:  'Noto Serif JP', 'Yu Mincho', serif;
  --font-en:  'Cormorant Garamond', 'Georgia', serif;

  /* トランジション */
  --ease-smooth: cubic-bezier(0.4, 0, 0.2, 1);
}

/* --- ユーティリティ --- */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-deep);
  color: var(--text-primary);
  font-family: var(--font-jp);
  font-weight: 300;
  font-size: 15px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

/* --- 幻想的な背景グラデーション --- */
body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(ellipse at 20% 50%, rgba(30, 20, 60, 0.4) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 20%, rgba(20, 30, 60, 0.3) 0%, transparent 50%),
    radial-gradient(ellipse at 50% 80%, rgba(40, 15, 30, 0.3) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}

/* --- パーティクル（浮遊する光の粒） --- */
.particle {
  position: fixed;
  width: 2px;
  height: 2px;
  background: radial-gradient(circle, rgba(201, 168, 76, 0.6) 0%, transparent 70%);
  border-radius: 50%;
  pointer-events: none;
  z-index: 1;
  animation: particleFloat linear infinite;
}

@keyframes particleFloat {
  0% {
    transform: translateY(100vh) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-10vh) rotate(360deg);
    opacity: 0;
  }
}

/* --- ヘッダー --- */
#app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: linear-gradient(
    180deg,
    rgba(10, 10, 15, 0.95) 0%,
    rgba(10, 10, 15, 0.7) 80%,
    transparent 100%
  );
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  /* タイトルやアイコンは置かない。設定ボタンのみ右端に配置 */
  justify-content: flex-end;
}

#settings-toggle {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  transition: color 0.3s var(--ease-smooth);
  display: flex;
  align-items: center;
  justify-content: center;
}

#settings-toggle:hover {
  color: var(--accent-gold);
}

/* --- トークルーム --- */
#chat-view {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 64px 16px 88px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

/* スクロールバーを控えめに */
#chat-view::-webkit-scrollbar {
  width: 3px;
}
#chat-view::-webkit-scrollbar-track {
  background: transparent;
}
#chat-view::-webkit-scrollbar-thumb {
  background: var(--text-dim);
  border-radius: 3px;
}

/* --- メッセージ --- */
#message-container {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  display: flex;
  flex-direction: column;
  animation: messageAppear 0.4s var(--ease-smooth) both;
}

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

/* --- アバターアイコン（吹き出しの横） --- */
.message.assistant {
  flex-direction: row;
  align-items: flex-start;
  gap: 10px;
}

.message-avatar {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  border: 1px solid var(--parchment-border);
  margin-top: 2px;
  color: var(--text-dim);
}

.message-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* --- 魔法使いの吹き出し（羊皮紙風） --- */

.message.assistant .bubble {
  background:
    linear-gradient(
      168deg,
      rgba(225, 210, 180, 0.95) 0%,
      rgba(210, 192, 160, 0.93) 40%,
      rgba(198, 178, 145, 0.90) 100%
    );
  border: 1px solid var(--parchment-border);
  border-radius: 2px 16px 16px 16px;
  color: var(--parchment-text);
  padding: 16px 20px;
  max-width: 85%;
  position: relative;
  font-size: 14.5px;
  line-height: 1.8;
  letter-spacing: 0.02em;

  /* 羊皮紙の質感 — 深い影と古びた紙の光沢 */
  box-shadow:
    inset 0 1px 0 rgba(255, 250, 230, 0.4),
    inset 0 -1px 3px rgba(140, 110, 70, 0.08),
    0 2px 8px rgba(0, 0, 0, 0.25),
    0 8px 24px rgba(0, 0, 0, 0.15);
}

/* 羊皮紙の端の微かな装飾線 */
.message.assistant .bubble::before {
  content: '';
  position: absolute;
  top: 5px;
  left: 5px;
  right: 5px;
  bottom: 5px;
  border: 1px solid rgba(160, 130, 90, 0.12);
  border-radius: 1px 12px 12px 12px;
  pointer-events: none;
}

/* 羊皮紙の紙の繊維感を表現する微小なノイズ */
.message.assistant .bubble::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 2px 16px 16px 16px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.03'/%3E%3C/svg%3E");
  pointer-events: none;
  mix-blend-mode: multiply;
}

/* --- 君の吹き出し --- */
.message.user {
  align-items: flex-end;
}

.message.user .bubble {
  background: var(--user-bg);
  border: 1px solid var(--user-border);
  border-radius: 16px 2px 16px 16px;
  color: var(--user-text);
  padding: 12px 18px;
  max-width: 80%;
  font-size: 14.5px;
  line-height: 1.75;
  letter-spacing: 0.02em;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* --- メッセージ内のテキスト整形 --- */
.bubble p {
  margin-bottom: 0.6em;
}
.bubble p:last-child {
  margin-bottom: 0;
}

/* 魔法使いの吹き出し内のテキスト装飾（暗い文字色に合わせる） */
.message.assistant .bubble strong {
  color: #2a1f10;
  font-weight: 500;
}
.message.assistant .bubble em {
  font-style: italic;
  color: #5a4a35;
}
.message.assistant .bubble code {
  background: rgba(120, 100, 70, 0.12);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Consolas', monospace;
  font-size: 0.9em;
  color: #3a2e20;
}

/* ユーザーの吹き出し内のテキスト装飾 */
.message.user .bubble strong {
  color: var(--accent-gold);
  font-weight: 500;
}
.bubble em {
  font-style: italic;
  opacity: 0.85;
}
.message.user .bubble code {
  background: rgba(0, 0, 0, 0.3);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Consolas', monospace;
  font-size: 0.9em;
}

/* --- 再生成ボタン --- */
.message-actions {
  display: flex;
  gap: 6px;
  margin-top: 4px;
  padding-left: 46px; /* アバター幅+gap分のオフセット */
}

.regen-button {
  background: rgba(30, 30, 40, 0.5);
  border: 1px solid rgba(139, 115, 85, 0.15);
  border-radius: 6px;
  cursor: pointer;
  padding: 5px 7px;
  opacity: 0.35;
  transition: opacity 0.3s var(--ease-smooth), border-color 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
}

.regen-button:hover,
.regen-button:active {
  opacity: 0.9;
  border-color: var(--parchment-border);
  color: var(--text-secondary);
}

/* ユーザーメッセージの再編集ボタン位置（右寄せ） */
.user-actions {
  padding-left: 0;
  justify-content: flex-end;
}

/* --- 編集モード --- */
.editing-fade {
  opacity: 0.3;
  transition: opacity 0.3s var(--ease-smooth);
  pointer-events: none;
}

#edit-cancel-bar {
  max-width: 640px;
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 14px;
  background: rgba(139, 115, 85, 0.08);
  border: 1px solid rgba(139, 115, 85, 0.18);
  border-radius: 10px;
  font-size: 12px;
  letter-spacing: 0.06em;
}

.edit-label {
  color: var(--text-dim);
}

#edit-cancel-btn {
  background: none;
  border: 1px solid rgba(139, 115, 85, 0.2);
  border-radius: 6px;
  color: var(--text-dim);
  font-family: var(--font-jp);
  font-size: 11px;
  cursor: pointer;
  padding: 3px 10px;
  letter-spacing: 0.04em;
  transition: color 0.3s, border-color 0.3s;
}

#edit-cancel-btn:hover,
#edit-cancel-btn:active {
  color: var(--text-secondary);
  border-color: rgba(139, 115, 85, 0.4);
}

/* --- 思考プロセスの折りたたみUI --- */
.thought-container {
  margin-bottom: 8px;
}

.thought-container details {
  background: rgba(120, 100, 70, 0.08);
  border: 1px solid rgba(140, 115, 80, 0.18);
  border-radius: 8px;
  overflow: hidden;
}

.thought-container summary {
  padding: 8px 12px;
  cursor: pointer;
  font-size: 12px;
  color: #7a6a50;
  letter-spacing: 0.05em;
  user-select: none;
  transition: color 0.3s;
}

.thought-container summary:hover {
  color: #4a3a25;
}

.thought-container .thought-text {
  padding: 8px 12px 12px;
  font-size: 13px;
  line-height: 1.7;
  color: #6a5a42;
  border-top: 1px solid rgba(140, 115, 80, 0.12);
  white-space: pre-wrap;
}

/* --- ストリーミング中のカーソル --- */
.streaming-cursor::after {
  content: '▍';
  animation: cursorBlink 0.8s steps(2) infinite;
  color: var(--accent-gold);
  margin-left: 2px;
}

@keyframes cursorBlink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* --- 入力ツールバー（Groundingトグル等） --- */
#input-toolbar {
  max-width: 640px;
  margin: 0 auto 6px;
  display: flex;
  gap: 6px;
}

.toolbar-btn {
  background: var(--bg-elevated);
  border: 1px solid rgba(139, 115, 85, 0.15);
  border-radius: 8px;
  color: var(--text-dim);
  cursor: pointer;
  padding: 5px 10px;
  transition: all 0.3s var(--ease-smooth);
  display: flex;
  align-items: center;
  justify-content: center;
}

.toolbar-btn:hover {
  border-color: var(--parchment-border);
  color: var(--text-secondary);
}

.toolbar-btn.active {
  background: rgba(139, 115, 85, 0.15);
  border-color: var(--accent-gold);
  color: var(--accent-gold);
  box-shadow: 0 0 8px rgba(201, 168, 76, 0.2);
}

/* --- トグルスイッチ（設定パネル内） --- */
.toggle-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
}

.toggle-label {
  font-size: 13px;
  color: var(--text-secondary);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 22px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-elevated);
  border: 1px solid rgba(139, 115, 85, 0.2);
  border-radius: 22px;
  transition: all 0.3s var(--ease-smooth);
}

.toggle-slider::before {
  content: '';
  position: absolute;
  height: 16px;
  width: 16px;
  left: 2px;
  bottom: 2px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: all 0.3s var(--ease-smooth);
}

.toggle-switch input:checked + .toggle-slider {
  background: rgba(139, 115, 85, 0.2);
  border-color: var(--accent-gold);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(18px);
  background: var(--accent-gold);
}

/* --- 入力欄 --- */
#input-area {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: linear-gradient(
    0deg,
    rgba(10, 10, 15, 0.98) 0%,
    rgba(10, 10, 15, 0.85) 70%,
    transparent 100%
  );
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

#input-wrapper {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-elevated);
  border: 1px solid rgba(139, 115, 85, 0.15);
  border-radius: 20px;
  padding: 6px 6px 6px 16px;
  transition: border-color 0.3s var(--ease-smooth);
}

#input-wrapper:focus-within {
  border-color: rgba(139, 115, 85, 0.4);
}

#user-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font-jp);
  font-size: 15px;
  font-weight: 300;
  line-height: 1.6;
  resize: none;
  max-height: 120px;
  padding: 6px 0;
}

#user-input::placeholder {
  color: var(--text-dim);
  font-style: italic;
}

#send-button {
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s var(--ease-smooth), opacity 0.2s;
  opacity: 0.8;
}

#send-button:hover {
  transform: scale(1.1);
  opacity: 1;
}

#send-button:active {
  transform: scale(0.95);
}

#send-button svg circle {
  transition: fill 0.3s var(--ease-smooth);
}

#send-button:hover svg circle {
  fill: #a52020;
}

/* --- 設定パネル --- */
#settings-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  transition: opacity 0.3s var(--ease-smooth);
}

#settings-overlay.hidden {
  display: none;
}

#settings-panel {
  width: 100%;
  max-width: 640px;
  max-height: 80vh;
  background: var(--bg-surface);
  border: 1px solid rgba(139, 115, 85, 0.15);
  border-radius: 20px 20px 0 0;
  overflow-y: auto;
  animation: slideUp 0.35s var(--ease-smooth);
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

#settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid rgba(139, 115, 85, 0.1);
}

#settings-header h2 {
  font-size: 15px;
  font-weight: 400;
  letter-spacing: 0.1em;
  color: var(--text-secondary);
}

#settings-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  transition: color 0.3s;
}

#settings-close:hover {
  color: var(--accent-gold);
}

#settings-body {
  padding: 20px 22px 32px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-group label {
  font-size: 12px;
  font-weight: 400;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
}

.setting-group textarea,
.setting-group select {
  background: var(--bg-elevated);
  border: 1px solid rgba(139, 115, 85, 0.15);
  border-radius: 10px;
  color: var(--text-primary);
  font-family: var(--font-jp);
  font-size: 14px;
  font-weight: 300;
  line-height: 1.7;
  padding: 12px 16px;
  outline: none;
  transition: border-color 0.3s var(--ease-smooth);
}

.setting-group textarea:focus,
.setting-group select:focus {
  border-color: rgba(139, 115, 85, 0.4);
}

.setting-group select {
  appearance: none;
  cursor: pointer;
  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 d='M2 4l4 4 4-4' stroke='%238a7f6e' fill='none' stroke-width='1.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 36px;
}

.setting-group select option {
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* --- 画像設定UI（アバター・背景） --- */
.image-setting {
  display: flex;
  align-items: center;
  gap: 14px;
}

.image-preview {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  border: 1px solid rgba(139, 115, 85, 0.2);
  font-size: 28px;
}

.image-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.bg-preview-box {
  border-radius: 10px;
  width: 80px;
  height: 50px;
  font-size: 12px;
  color: var(--text-dim);
}

.default-bg-text {
  font-size: 12px;
  color: var(--text-dim);
}

.image-buttons {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.file-button {
  display: inline-block;
  padding: 6px 14px;
  background: var(--bg-elevated);
  border: 1px solid rgba(139, 115, 85, 0.2);
  border-radius: 8px;
  color: var(--text-secondary);
  font-family: var(--font-jp);
  font-size: 12px;
  cursor: pointer;
  text-align: center;
  transition: border-color 0.3s var(--ease-smooth), color 0.3s;
}

.file-button:hover {
  border-color: rgba(139, 115, 85, 0.5);
  color: var(--accent-gold);
}

.reset-button {
  background: none;
  border: none;
  color: var(--text-dim);
  font-family: var(--font-jp);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 0;
  transition: color 0.3s;
}

.reset-button:hover {
  color: var(--accent-wine);
}

/* --- 応答的レイアウト --- */
@media (max-width: 480px) {
  #chat-view {
    padding: 58px 12px 84px;
  }

  .message.assistant .bubble,
  .message.user .bubble {
    max-width: 90%;
    font-size: 14px;
  }

  #header-title {
    font-size: 13px;
    letter-spacing: 0.1em;
  }
}

/* --- ローディングアニメーション --- */
.typing-indicator {
  display: flex;
  gap: 5px;
  padding: 14px 18px;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  background: var(--text-dim);
  border-radius: 50%;
  animation: typingDot 1.2s ease-in-out infinite;
}

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

.typing-indicator span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typingDot {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}
