:root {
  --bg: #fbf7f8;
  --panel: #ffffff;
  --text: #2a2226;
  --muted: #8a7b82;
  --accent: #d6457a;
  --accent-soft: #fbe3ec;
  --mine: #2a2226;
  --mine-text: #ffffff;
  --line: #eadfe3;
  --error: #c0392b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1418;
    --panel: #251d22;
    --text: #f2e9ed;
    --muted: #a3929a;
    --accent: #ff6fa3;
    --accent-soft: #3a2530;
    --mine: #f2e9ed;
    --mine-text: #1a1418;
    --line: #3a2e34;
    --error: #ff8a7a;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Noto Sans JP", sans-serif;
  font-size: 16px; line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
.screen { height: 100dvh; display: flex; flex-direction: column; }

/* login */
#login { align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; text-align: center; }
.login-card h1 { margin: 12px 0 4px; font-size: 24px; }
.hint { color: var(--muted); font-size: 14px; margin: 0 0 20px; }
#login-form { display: flex; flex-direction: column; gap: 10px; }
input, textarea {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }
button {
  font: inherit; border: 0; border-radius: 12px; padding: 12px 16px;
  background: var(--accent); color: #fff; cursor: pointer;
}
button:disabled { opacity: .45; }
button.ghost { background: transparent; color: var(--muted); padding: 6px 10px; font-size: 13px; border: 1px solid var(--line); }
.error { color: var(--error); font-size: 14px; min-height: 1.4em; }

.avatar {
  width: 34px; height: 34px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700;
  background: var(--accent-soft); color: var(--accent);
}
.avatar.big { width: 72px; height: 72px; font-size: 30px; margin: 0 auto; }

/* chat */
header {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.title { flex: 1; font-weight: 700; }
#status { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 8px; }
#log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 85%; padding: 10px 14px; border-radius: 16px; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 4px; white-space: normal; }
.msg.assistant p { margin: 0; }
.msg.assistant p.h { font-weight: 700; margin-top: 4px; }
.msg.assistant .gap { height: .5em; }
.msg.assistant ul, .msg.assistant ol { margin: 4px 0; padding-left: 1.3em; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant strong { color: var(--accent); }
.msg.assistant code { font-family: ui-monospace, Menlo, monospace; font-size: .9em; background: var(--accent-soft); padding: 1px 5px; border-radius: 6px; }
.msg.assistant pre { margin: 6px 0; padding: 10px; background: var(--bg); border-radius: 10px; overflow-x: auto; }
.msg.assistant pre code { background: none; padding: 0; }
.msg.assistant .tbl { overflow-x: auto; margin: 6px 0; }
.msg.assistant table { border-collapse: collapse; font-size: 14px; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; white-space: nowrap; }
.msg.assistant th { background: var(--accent-soft); }
.msg.assistant.pending::after { content: "▍"; color: var(--accent); animation: blink 1s steps(1) infinite; }
.msg.error { align-self: center; color: var(--error); font-size: 13px; background: none; }
.empty { margin: auto; color: var(--muted); text-align: center; font-size: 14px; }
@keyframes blink { 50% { opacity: 0; } }

#composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--panel);
}
#input { flex: 1; resize: none; max-height: 140px; }
#send { width: 46px; height: 46px; padding: 0; display: grid; place-items: center; border-radius: 50%; }

.icon-btn {
  width: 46px; height: 46px; padding: 0; flex: none; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
header .icon-btn { width: 36px; height: 36px; background: transparent; color: var(--muted); border: 1px solid var(--line); }
#speaker .on, #speaker .off { display: none; }
#speaker[aria-pressed="true"] .on { display: block; }
#speaker[aria-pressed="false"] .off { display: block; }
#speaker[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); }
.mic.recording { background: var(--accent); color: #fff; animation: pulse 1.2s ease-in-out infinite; }
.mic.working { opacity: .5; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 45%, transparent); }
  50% { box-shadow: 0 0 0 10px transparent; }
}

/* 記憶パネル */
.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.sheet-card {
  width: 100%; max-width: 560px; max-height: 82dvh; overflow-y: auto;
  background: var(--panel); border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head b { font-size: 17px; }
.sheet-hint { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }
#mem-list { list-style: none; margin: 0 0 12px; padding: 0; }
#mem-list li { display: flex; gap: 8px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
#mem-list li span { flex: 1; }
#mem-list li small { color: var(--muted); font-size: 11px; margin-left: 4px; }
#mem-list li button { flex: none; background: none; color: var(--muted); padding: 0 6px; font-size: 18px; line-height: 1.3; }
#mem-list .none { color: var(--muted); border: 0; }
#mem-form { display: flex; gap: 8px; }
#mem-form input { flex: 1; min-width: 0; }

/* リンク・出典 */
.msg.assistant a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; word-break: break-all; }
.msg.assistant p.src { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* メイの顔アイコン */
.avatar { overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* メイの姿（表情と口パク） */
#stage {
  flex: none; height: 50dvh; min-height: 240px; max-height: 560px;
  background: linear-gradient(#c3c4c3, #b9bab9); overflow: hidden; position: relative;
  border-bottom: 1px solid var(--line);
}
#stage img {
  width: 100%; height: 100%; object-fit: cover; object-position: 50% 0%;
  display: block; animation: breathe 5s ease-in-out infinite; transform-origin: 50% 80%;
}
@keyframes breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.012); } }
#stage-toggle { cursor: pointer; }
