:root {
  --pink: #ff6b9d;
  --pink-dark: #e84d82;
  --bg: #1a1626;
  --panel: #241f33;
  --panel-2: #2e2842;
  --text: #f4f0fb;
  --muted: #a99fc4;
  --user: #6c5ce7;
  /* セーフエリア（ノッチ/ホームバー） */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  /* 画面の実高さ。JS(visualViewport)で上書きされる。dvh非対応ブラウザは100vhにフォールバック */
  --app-h: 100vh;
}
@supports (height: 100dvh) {
  :root { --app-h: 100dvh; }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, select, textarea, input { touch-action: manipulation; }

/* ---------- Login ---------- */
.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  background: radial-gradient(1200px 600px at 50% -10%, #3a2a4d 0%, var(--bg) 60%);
  padding: calc(20px + var(--safe-top)) calc(20px + var(--safe-right)) calc(20px + var(--safe-bottom)) calc(20px + var(--safe-left));
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--panel);
  border: 1px solid #39314f;
  border-radius: 20px;
  padding: 36px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,.45);
}
.login-hero { text-align: center; margin-bottom: 24px; }
.login-hero .heart { font-size: 40px; color: var(--pink); filter: drop-shadow(0 0 12px rgba(255,107,157,.6)); }
.login-hero h1 { font-size: 22px; margin: 10px 0 6px; }
.login-hero .sub { color: var(--muted); font-size: 13px; margin: 0; }
.login-form label { display: block; margin-bottom: 14px; }
.login-form label span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.login-form input {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid #3c3453; background: var(--panel-2); color: var(--text);
  font-size: 16px; /* 16px未満だとiOS Safariがフォーカス時に自動ズームする */
  outline: none;
}
.login-form input:focus { border-color: var(--pink); }
.btn-primary {
  background: linear-gradient(135deg, var(--pink), var(--pink-dark));
  color: #fff; border: 0; border-radius: 12px; padding: 12px 18px;
  font-size: 15px; font-weight: 600; cursor: pointer; width: 100%;
  transition: transform .06s ease, filter .2s ease;
}
.btn-primary:hover { filter: brightness(1.05); }
.btn-primary:active { transform: translateY(1px); }
.alert { background: #4a1f2e; color: #ffc2d4; padding: 10px 14px; border-radius: 10px; font-size: 13px; margin-bottom: 16px; }
.demo-hint { text-align: center; color: var(--muted); font-size: 12px; margin-top: 18px; }
.demo-hint code { background: var(--panel-2); padding: 2px 6px; border-radius: 6px; color: var(--pink); }

/* ---------- Chat ---------- */
.chat-body {
  overflow: hidden;
  /* iOSのラバーバンド/アドレスバー伸縮による余白を防ぐ */
  position: fixed; inset: 0;
  overscroll-behavior: none;
}
.app {
  display: flex;
  height: 100vh;
  height: var(--app-h);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.stage {
  flex: 1 1 55%;
  min-width: 0; min-height: 0;
  position: relative;
  background: radial-gradient(900px 500px at 50% 0%, #3a2a4d 0%, #151121 70%);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  touch-action: none; /* タッチでの視線追従を優先しスクロールを抑止 */
}
#live2d { width: 100%; height: 100%; display: block; }
.stage-fallback { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); }
.stage-fallback[hidden] { display: none; }
.avatar-emoji { font-size: 120px; line-height: 1; margin-bottom: 12px; }
.stage-caption {
  position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
  max-width: 80%; background: rgba(0,0,0,.45); backdrop-filter: blur(6px);
  padding: 10px 18px; border-radius: 16px; font-size: 14px; color: #fff;
  opacity: 0; transition: opacity .3s ease; text-align: center;
}
.stage-caption.show { opacity: 1; }

/* 表示切替ボタン（ステージ右上） */
.view-toggle {
  position: absolute; top: calc(12px + var(--safe-top)); right: 12px;
  width: 40px; height: 40px; border-radius: 12px;
  background: rgba(0,0,0,.4); border: 1px solid rgba(255,255,255,.18);
  color: #fff; font-size: 18px; line-height: 1; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  transition: background .2s ease, border-color .2s ease, transform .06s ease;
}
.view-toggle:hover { background: rgba(0,0,0,.55); border-color: var(--pink); }
.view-toggle:active { transform: scale(.96); }
.view-toggle .ico-bust { display: none; }
.view-toggle.is-full .ico-full { display: none; }
.view-toggle.is-full .ico-bust { display: inline; }

.panel {
  flex: 1 1 45%; max-width: 520px;
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column;
  background: var(--panel); border-left: 1px solid #39314f;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid #39314f; gap: 10px;
  flex-shrink: 0;
}
.panel-head .who { display: flex; flex-direction: column; min-width: 0; }
.who-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.panel-head .who strong { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.battery { display: inline-flex; align-items: center; gap: 4px; flex-shrink: 0; }
.battery-body {
  width: 22px; height: 11px; box-sizing: border-box;
  border: 1.5px solid var(--muted); border-radius: 2px; padding: 1px; position: relative;
}
.battery-body::after {
  content: ''; position: absolute; right: -4px; top: 2px;
  width: 2px; height: 5px; background: var(--muted); border-radius: 0 1px 1px 0;
}
.battery-fill { display: block; height: 100%; width: 0; background: var(--pink); border-radius: 1px; }
.battery.is-low .battery-fill { background: #e8b86d; }
.battery-pct { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.panel-head .who span { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.controls select {
  background: var(--panel-2); color: var(--text); border: 1px solid #3c3453;
  border-radius: 10px; padding: 7px 9px; font-size: 12px; max-width: 150px;
  min-height: 34px;
}
.btn-ghost {
  background: var(--panel-2); color: var(--text); border: 1px solid #3c3453;
  border-radius: 10px; width: 34px; height: 34px; display: inline-flex;
  align-items: center; justify-content: center; cursor: pointer; text-decoration: none;
  font-size: 16px; flex-shrink: 0;
}
.btn-ghost:hover { border-color: var(--pink); }

.panel-main { flex: 1; min-height: 0; display: flex; flex-direction: column; position: relative; }
.messages {
  flex: 1; min-height: 0; overflow-y: auto; padding: 18px;
  display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.msg { display: flex; }
.msg.assistant { flex-direction: column; align-items: flex-start; }
.msg.user { flex-direction: column; align-items: flex-end; gap: 6px; }
.msg-photo {
  display: block; max-width: min(240px, 72%); max-height: 240px;
  width: auto; height: auto; object-fit: cover; border-radius: 14px;
}
.msg.user .msg-photo { border-bottom-right-radius: 4px; }
.bubble {
  max-width: 78%; padding: 10px 14px; border-radius: 16px; font-size: 14px; line-height: 1.6;
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere;
}
.msg.assistant .bubble { background: var(--panel-2); border-bottom-left-radius: 4px; }
.msg.user .bubble { background: linear-gradient(135deg, var(--user), #5a4bd1); border-bottom-right-radius: 4px; }
.msg.typing .bubble { color: var(--muted); font-style: italic; }
.sources { display: flex; flex-direction: column; gap: 4px; max-width: 78%; margin-top: -4px; }
.sources a {
  color: var(--muted); font-size: 12px; line-height: 1.4; text-decoration: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sources a:hover { color: var(--pink); }
.usage-meta { max-width: 78%; margin-top: -4px; font-size: 11px; line-height: 1.4; color: var(--muted); }

.memory-drawer {
  position: absolute; inset: 0; z-index: 4;
  display: flex; flex-direction: column;
  background: var(--panel);
}
.memory-drawer[hidden] { display: none; }
.memory-drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid #39314f; flex-shrink: 0;
}
.memory-list { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.memory-item {
  background: var(--panel-2); border-radius: 12px; padding: 10px 12px;
}
.memory-item .meta { display: flex; gap: 8px; align-items: baseline; margin-bottom: 4px; }
.memory-item .kind {
  font-size: 11px; color: var(--pink); background: rgba(255,107,157,.12);
  border-radius: 999px; padding: 1px 8px; flex-shrink: 0;
}
.memory-item .subject { font-size: 14px; font-weight: 600; }
.memory-item .content { font-size: 13px; line-height: 1.5; color: var(--text); }
.memory-item .when { font-size: 11px; color: var(--muted); margin-top: 4px; }
.memory-empty { color: var(--muted); font-size: 13px; padding: 12px 4px; }
.bubble.streaming { cursor: pointer; }
.bubble.streaming::after {
  content: '';
  display: inline-block;
  width: 2px; height: 1em;
  margin-left: 2px;
  background: currentColor;
  vertical-align: text-bottom;
  animation: caret 1s steps(1) infinite;
}
@keyframes caret { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  .bubble.streaming::after { animation: none; }
}

.composer {
  display: flex; flex-direction: column; gap: 8px; padding: 14px; border-top: 1px solid #39314f;
  flex-shrink: 0; align-items: stretch;
  padding-bottom: calc(14px + var(--safe-bottom));
}
.composer.is-dragover { outline: 2px dashed var(--pink); outline-offset: -8px; }
.composer-row { display: flex; gap: 10px; align-items: flex-end; }
.photo-btn {
  width: 44px; height: 44px; flex-shrink: 0; border-radius: 14px;
  border: 1px solid #3c3453; background: var(--panel-2); color: var(--text);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.photo-btn:hover { border-color: var(--pink); }
.photo-btn:disabled { opacity: .45; cursor: default; }
.photo-preview { display: flex; align-items: center; gap: 8px; }
.photo-preview[hidden] { display: none; }
.photo-preview img {
  width: 64px; height: 64px; object-fit: cover; border-radius: 10px; background: var(--panel-2);
}
.composer textarea {
  flex: 1; min-width: 0; resize: none; border-radius: 14px; border: 1px solid #3c3453;
  background: var(--panel-2); color: var(--text); padding: 12px 14px; font-size: 14px;
  font-family: inherit; max-height: 140px; outline: none; line-height: 1.4;
}
.composer textarea:focus { border-color: var(--pink); }
.composer .btn-primary { width: auto; padding: 0 22px; min-height: 44px; flex-shrink: 0; }
.composer .btn-primary:disabled { opacity: .6; cursor: default; }

/* ---------- Responsive ---------- */

/* タブレット縦〜スマホ: 上にキャラ、下にチャットの縦積み */
@media (max-width: 820px) {
  .app { flex-direction: column; }
  /* テキスト欄（下パネル）を従来の約70%の高さにし、空いた分をキャラに回す */
  .stage { flex: 0 0 57%; flex-basis: calc(var(--app-h) * 0.57); min-height: 160px; }
  .panel { flex: 1 1 auto; max-width: none; border-left: 0; border-top: 1px solid #39314f; }
  .controls select { max-width: 110px; }
  .stage-caption { bottom: 10px; font-size: 13px; padding: 8px 14px; max-width: 90%; }
  .avatar-emoji { font-size: 80px; }
}

/* スマホ */
@media (max-width: 600px) {
  .stage { flex-basis: calc(var(--app-h) * 0.54); }

  /* ヘッダー: 1行に収める。文字は省略、セレクトは縮む */
  .panel-head { padding: 8px 10px 8px 14px; gap: 8px; }
  .panel-head .who { flex: 1 1 auto; }
  .panel-head .who strong { font-size: 15px; }
  .panel-head .who span { font-size: 11px; }
  .controls { flex-wrap: nowrap; gap: 6px; flex: 0 1 auto; min-width: 0; }
  .controls select { flex: 1 1 0; min-width: 0; max-width: 96px; padding: 7px 6px; font-size: 12px; text-overflow: ellipsis; }
  .btn-ghost { width: 36px; height: 36px; }

  .messages { padding: 12px; gap: 10px; }
  .bubble { max-width: 86%; font-size: 15px; padding: 10px 12px; }
  .sources { max-width: 86%; }
  .usage-meta { max-width: 86%; }

  .composer { padding: 10px; padding-bottom: calc(10px + var(--safe-bottom)); }
  .composer-row { gap: 8px; }
  .msg-photo { max-width: min(220px, 78%); }
  .composer textarea { font-size: 16px; padding: 11px 12px; max-height: 110px; }
  .composer .btn-primary { padding: 0 16px; font-size: 15px; }
}

/* 極小幅 (iPhone SE等) */
@media (max-width: 380px) {
  .panel-head .who span { display: none; }
  .controls select { max-width: 84px; font-size: 11px; }
  .composer .btn-primary { padding: 0 12px; }
}

/* スマホ横向き: 左右分割に戻し、キャラは狭めに */
@media (max-width: 920px) and (max-height: 500px) and (orientation: landscape) {
  .app { flex-direction: row; }
  .stage { flex: 0 0 38%; flex-basis: 38%; min-height: 0; }
  .panel { flex: 1 1 auto; max-width: none; border-top: 0; border-left: 1px solid #39314f; }
  .panel-head { padding: 6px 10px; }
  .panel-head .who span { display: none; }
  .messages { padding: 10px 12px; gap: 8px; }
  .composer { padding: 8px 10px; padding-bottom: calc(8px + var(--safe-bottom)); }
  .composer textarea { max-height: 80px; }
  .stage-caption { display: none; }
}

/* キーボード表示中（JSが .kbd-open を付与）: キャラ領域を縮めて入力欄を確保 */
@media (max-width: 820px) and (orientation: portrait) {
  .app.kbd-open .stage { flex-basis: calc(var(--app-h) * 0.22); min-height: 100px; }
  .app.kbd-open .stage-caption { display: none; }
}

/* Phase 2 Step 2: 背景画像と選択リスト */
.stage.has-bg { background-size: cover; background-position: center; }
.pick-row { display: flex; gap: 10px; align-items: center; }
.pick-row img.thumb { width: 64px; height: 44px; object-fit: cover; border-radius: 8px; flex: none; }
.pick-row .grow { flex: 1; }

/* 読み上げボタンとお知らせ */
.speak-btn { background: none; border: 0; cursor: pointer; font-size: 13px; opacity: .55; padding: 2px 4px; align-self: flex-end; line-height: 1; }
.speak-btn:hover { opacity: 1; }
.announce-bar { background: #43335f; color: #f3efff; font-size: 13px; padding: 8px 12px; border-radius: 10px; margin: 8px 12px 0; }
