:root {
  /* 暖色系ライトテーマ「あんずみるく」 */
  --bg: #fff7ee;          /* 淡いあんずミルク */
  --surface: #ffffff;
  --surface-2: #ffefdd;
  --border: #f3dcbe;
  --text: #5b4636;        /* 柔らかいブラウン */
  --muted: #a88f76;
  --accent: #e9834b;      /* あんずオレンジ */
  --accent-2: #d8712f;    /* 濃いオレンジ */

  /* ユーザーの吹き出し（色付き・白文字） */
  --user-bubble-a: #f6a874;
  --user-bubble-b: #e9834b;
  --user-text: #fffdf9;
  --user-act: rgba(255,255,255,.82);   /* ユーザー吹き出し内の動作 */

  /* キャラの吹き出し（淡いクリーム・濃い文字） */
  --char-bubble: #fbebd9;
  --char-text: #5b4636;               /* セリフ */
  --char-act: #a88f76;                /* 吹き出し内の動作（イタリック・淡色） */

  /* ナレーション（吹き出しの外・ハート付き独立行） */
  --narration: #9c8265;
  --narr-icon-bg: #f6ddbe;
  --narr-icon-fg: #c9873f;

  --danger: #d9534f;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  overscroll-behavior-y: none;
}
#app { height: 100%; }
body, #app { overflow-x: hidden; max-width: 100%; }

button {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  cursor: pointer;
}
button:active { transform: scale(0.98); }
button.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
button.ghost { background: transparent; border-color: transparent; color: var(--text); }
button.danger { color: var(--danger); }
button.icon { padding: 8px 10px; }

input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
}
textarea { resize: vertical; min-height: 80px; }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }

/* --- レイアウト --- */
.screen { display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar .title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .sub { font-size: 12px; color: var(--muted); }

/* モデル（API）切替ピル */
.model-pill {
  display: inline-flex; align-items: center; gap: 4px; flex-shrink: 0;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--accent-2);
  font-size: 12px; font-weight: 700; padding: 5px 10px; border-radius: 999px; cursor: pointer;
  max-width: 40vw; overflow: hidden;
}
.model-pill .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-pill .chev { font-size: 9px; opacity: .6; }
.model-menu {
  position: fixed; z-index: 60; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: 0 12px 30px -8px rgba(90, 60, 30, .35); overflow: hidden;
  min-width: 200px;
}
.model-menu .mrow {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 11px 14px; font-size: 14px; cursor: pointer;
}
.model-menu .mrow + .mrow { border-top: 1px solid var(--border); }
.model-menu .mrow.active { background: var(--surface-2); color: var(--accent-2); font-weight: 700; }
.model-menu .mrow .ck { color: var(--accent-2); }
.menu-backdrop { position: fixed; inset: 0; z-index: 55; }
.content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px; }
.content.list { padding: 8px 14px 24px; }

/* --- ログイン --- */
.center-box { max-width: 380px; margin: 12vh auto 0; padding: 24px; }
.center-box h1 { font-size: 22px; text-align: center; color: var(--accent-2); }

/* --- アバター --- */
.avatar {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--surface-2); display: flex; align-items: center; justify-content: center;
  font-size: 24px; flex-shrink: 0; overflow: hidden;
  border: 1px solid var(--border);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* --- キャラ一覧 --- */
.char-card {
  display: flex; align-items: center; gap: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 14px; margin-bottom: 10px;
  box-shadow: 0 1px 3px rgba(120, 80, 40, .06);
}
.char-card .meta { flex: 1; min-width: 0; }
.char-card .meta .name { font-weight: 600; }
.char-card .meta .desc { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- チャット一覧（ホーム） --- */
.chatrow {
  display: flex; align-items: center; gap: 13px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 12px 14px; margin-bottom: 10px;
  box-shadow: 0 1px 3px rgba(120, 80, 40, .06);
}
.chatrow .chatrow-av { width: 52px; height: 52px; font-size: 25px; }
.chatrow-body { flex: 1; min-width: 0; }
.chatrow-name { font-size: 14.5px; font-weight: 700; color: var(--text); margin-bottom: 3px; }
.chatrow-name .hearttag { color: var(--narr-icon-fg); font-size: 12px; font-weight: 400; }
.chatrow-preview {
  font-size: 13px; color: var(--muted); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; overflow-wrap: anywhere;
}
.chatrow .rowmenu { flex-shrink: 0; align-self: flex-start; color: var(--muted); font-size: 18px; }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-bottom));
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent-2); color: #fff; border: none;
  font-size: 28px; box-shadow: 0 6px 20px rgba(200, 110, 40, .4); z-index: 20;
}

/* --- チャット --- */
.messages { display: flex; flex-direction: column; gap: 16px; }
.msg { display: flex; flex-direction: column; width: 100%; min-width: 0; gap: 8px; }

/* ナレーション：吹き出しの外・ハート付きの独立行 */
.narr-row { display: flex; gap: 8px; align-items: flex-start; padding: 0 4px; }
.narr-heart {
  width: 22px; height: 22px; border-radius: 50%; flex-shrink: 0; margin-top: 2px;
  background: var(--narr-icon-bg); color: var(--narr-icon-fg);
  display: flex; align-items: center; justify-content: center;
}
.narr-heart svg { display: block; }
.narr-text {
  color: var(--narration); font-style: italic; font-size: 14px; line-height: 1.75;
  white-space: pre-wrap; overflow-wrap: anywhere; padding-top: 2px;
}

/* 発話ターン：アイコン＋名前を上端で揃え、吹き出しはアイコンの途中から始まる */
.turn { position: relative; max-width: 86%; }
.turn.char { align-self: flex-start; padding-left: 34px; }
.turn.user { align-self: flex-end; padding-right: 34px; text-align: right; }
.turn-av {
  position: absolute; top: 0; width: 28px; height: 28px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center; font-size: 15px; overflow: hidden;
}
.turn-av img { width: 100%; height: 100%; object-fit: cover; }
.turn.char .turn-av { left: 0; }
.turn.user .turn-av { right: 0; }
.turn-name { font-size: 11px; font-weight: 700; color: var(--muted); line-height: 1.3; margin: 0 2px 2px; }
.turn-bubbles { display: flex; flex-direction: column; gap: 6px; }
.turn.user .turn-bubbles { align-items: flex-end; }

.bubble {
  display: inline-block; text-align: left; padding: 9px 13px; border-radius: 16px;
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word;
  min-width: 0; max-width: 100%;
  box-shadow: 0 1px 3px rgba(150, 100, 50, .10);
}
.turn.char .bubble { background: var(--char-bubble); color: var(--char-text); border-bottom-left-radius: 5px; }
.turn.user .bubble {
  background: linear-gradient(150deg, var(--user-bubble-a), var(--user-bubble-b));
  color: var(--user-text); border-bottom-right-radius: 5px;
}
/* 吹き出し内：セリフ＝素の文（dlg・装飾なし）／動作・描写＝イタリック淡色（act）。「」は表示しない */
.bubble .dlg { display: block; }
.bubble .act { display: block; font-style: italic; font-size: .94em; }
.bubble > span + span { margin-top: 5px; }
.turn.char .bubble .act { color: var(--char-act); }
.turn.user .bubble .act { color: var(--user-act); }

/* 再生成候補のスワイプ・操作ボタン */
.msg-foot { display: flex; align-items: center; gap: 10px; padding: 0 4px; }
.msg.user .msg-foot { justify-content: flex-end; }
.swipe { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.swipe button { background: transparent; border: none; color: var(--accent-2); font-size: 19px; line-height: 1; padding: 0 4px; }
.msg .actions { display: flex; align-items: center; gap: 2px; }
.msg .actions button { padding: 2px 6px; font-size: 13px; background: transparent; border: none; color: var(--muted); }

.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; min-width: 0; min-height: 44px; max-height: 40vh; border-radius: 22px; padding: 11px 16px;
}
.composer button.send { flex-shrink: 0; }
.composer button.send { border-radius: 22px; height: 44px; padding: 0 18px; background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.composer button.send:disabled { opacity: 0.5; }
.composer .auto-btn {
  flex-shrink: 0; width: 44px; height: 44px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--accent-2);
  font-size: 15px; display: flex; align-items: center; justify-content: center; padding: 0;
}
.composer .auto-btn.running { background: var(--danger); border-color: var(--danger); color: #fff; }

.banner {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; font-size: 13px; color: var(--muted);
  display: flex; align-items: center; gap: 10px; margin-bottom: 10px;
}
.banner button { padding: 6px 10px; font-size: 12px; }

.typing { color: var(--muted); font-size: 13px; }
.dots::after { content: '…'; animation: blink 1.2s steps(4) infinite; }
@keyframes blink { 0%,100%{opacity:.3} 50%{opacity:1} }

/* --- フォーム画面 --- */
.form { max-width: 640px; margin: 0 auto; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.section-title { font-size: 13px; color: var(--accent-2); font-weight: 600; letter-spacing: .02em; margin: 20px 0 6px; }
.footer-actions { display: flex; gap: 10px; margin-top: 24px; }
.footer-actions .primary { flex: 1; }

.avatar-preview-row { display: flex; align-items: center; gap: 12px; }
.avatar-preview-row .avatar { width: 52px; height: 52px; }
.avatar-preview-row input { flex: 1; }

/* --- リスト内アイテム（ロア/メモリー） --- */
.list-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; margin-bottom: 10px;
}
.list-item .keys { font-size: 12px; color: var(--accent-2); }
.list-item .body { font-size: 14px; margin-top: 4px; white-space: pre-wrap; }
.list-item .item-actions { display: flex; gap: 6px; justify-content: flex-end; margin-top: 8px; }

.empty { text-align: center; color: var(--muted); margin-top: 20vh; }
.toast {
  position: fixed; bottom: calc(80px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; border-radius: 10px;
  padding: 10px 16px; font-size: 13px; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.3);
}
