:root {
  --face-bg: #0a0e13;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1b1f24;
  --muted: #6b7280;
  --user-bubble: #1f8fff;
  --user-text: #ffffff;
  --bot-bubble: #ffffff;
  --bot-text: #1b1f24;
  --border: #e3e6ea;
  --accent: #1f8fff;
  --danger: #e5484d;
  --ok: #30a46c;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC",
    "Microsoft JhengHei", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1318;
    --panel: #151a21;
    --text: #e6e8eb;
    --muted: #8b949e;
    --bot-bubble: #1d232b;
    --bot-text: #e6e8eb;
    --border: #262d36;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1318;
  --panel: #151a21;
  --text: #e6e8eb;
  --muted: #8b949e;
  --bot-bubble: #1d232b;
  --bot-text: #e6e8eb;
  --border: #262d36;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

body { overflow: hidden; }

/* 手機（直式）：臉在上、對話在下 */
.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.face-panel {
  position: relative;
  flex: 0 0 auto;
  height: min(42dvh, 100vw);
  background: var(--face-bg);
  padding-top: env(safe-area-inset-top);
}

#face {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
  touch-action: manipulation;
}

.face-bar {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 10px);
  left: 14px;
  right: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.name {
  color: #9fb3c2;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f5a524;
}
.status[data-state="online"] { background: var(--ok); }
.status[data-state="offline"] { background: var(--danger); }

.icon-btn {
  margin-left: auto;
  pointer-events: auto;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.07);
  color: #9fb3c2;
  display: grid;
  place-items: center;
  cursor: pointer;
  font-size: 16px;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.14); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn.dark { background: var(--bg); color: var(--muted); }

/* 放在臉的下方（和「查詢中」同一個位置），不擋住眼睛 */
.toast {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 7px 14px;
  border-radius: 12px;
  background: rgba(16, 44, 52, 0.92);
  color: #cdeff5;
  font-size: 13px;
  line-height: 1.45;
  text-align: center;
  animation: toast-in 0.25s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -6px); } }

/* 記憶清單：手機上從底部滑上來，電腦上在中間 */
.sheet {
  width: min(520px, 100%);
  max-height: min(80dvh, 640px);
  margin: auto auto 0;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  border: none;
  border-radius: 20px 20px 0 0;
  background: var(--panel);
  color: var(--text);
  display: none;
  flex-direction: column;
}
.sheet[open] { display: flex; }
.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
@media (min-width: 860px) {
  .sheet { margin: auto; border-radius: 20px; }
}
.sheet-head { display: flex; align-items: center; }
.sheet-head h2 { margin: 0; font-size: 19px; }
.sheet-note { margin: 6px 0 12px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.mem-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 60px;
}
.mem-list li {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 2px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
  line-height: 1.5;
}
.mem-list li span { flex: 1; }
.mem-list li small { display: block; color: var(--muted); font-size: 12px; }
.mem-list li button {
  flex: none;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--danger);
  border-radius: 8px;
  padding: 4px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  min-height: 32px;
}
.mem-list .none { color: var(--muted); justify-content: center; border: none; }
.mem-form { display: flex; gap: 8px; margin-top: 12px; }
.mem-form input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px;
}
.mem-form button {
  border: none;
  border-radius: 10px;
  padding: 0 16px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  cursor: pointer;
}

.tool {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 5px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: #b8c7d3;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-panel {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overscroll-behavior: contain;
}

.msg {
  max-width: 82%;
  padding: 9px 13px;
  border-radius: 18px;
  line-height: 1.5;
  font-size: 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 6px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--bot-bubble);
  color: var(--bot-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}
.msg.assistant.streaming::after {
  content: "▍";
  margin-left: 2px;
  color: var(--muted);
  animation: caret 1s steps(2) infinite;
}
@keyframes caret { 50% { opacity: 0; } }

.msg.system {
  align-self: center;
  max-width: 92%;
  background: transparent;
  color: var(--danger);
  font-size: 13px;
  padding: 2px 8px;
}
.msg a { color: inherit; }

.sources {
  align-self: flex-start;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: -2px 0 4px 4px;
  max-width: 82%;
}
.sources a {
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 9px;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sources a::before { content: "↗ "; }

.empty {
  margin: auto;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
  line-height: 1.7;
}

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--border);
}

#input {
  flex: 1;
  resize: none;
  max-height: 140px;
  padding: 10px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* 16px 以上，iPhone 才不會自動放大畫面 */
  line-height: 1.35;
  outline: none;
}
#input:focus { border-color: var(--accent); }

#send {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
}
#send:disabled { opacity: 0.4; cursor: default; }

/* 寬螢幕（電腦）：臉在左、對話在右 */
@media (min-width: 860px) {
  .app { flex-direction: row; }
  .face-panel {
    height: 100dvh;
    flex: 1 1 55%;
  }
  .chat-panel {
    flex: 0 0 420px;
    border-left: 1px solid var(--border);
  }
}

/* ── 桌面寵物（pet.html）──────────────────────────── */
html:has(body.pet), body.pet { background: transparent; }
.pet .app {
  height: 100vh;
  border-radius: 28px;
  overflow: hidden;
  background: var(--face-bg);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.06) inset;
}
/* min-height: 0：不然畫布會撐住舊的高度，展開時臉不肯縮小、對話區被擠掉 */
.pet .face-panel { flex: 1 1 auto; height: auto; min-height: 0; padding-top: 0; cursor: grab; }
.pet .face-panel:active { cursor: grabbing; }
.pet .chat-panel { display: none; }
.pet .face-bar { top: 10px; left: 12px; right: 12px; opacity: 0; transition: opacity 0.2s; }
.pet .app:hover .face-bar { opacity: 1; }
.pet .name { font-size: 12px; }
.pet .icon-btn { width: 30px; height: 30px; }
.pet #petCollapse { display: none; margin-left: auto; font-size: 18px; line-height: 1; }
.pet #petCollapse + #memBtn { margin-left: 6px; }
.pet .tool { bottom: 8px; font-size: 11px; }
.pet .toast { bottom: 8px; font-size: 11px; padding: 5px 10px; }

/* 展開：臉在上、對話在下 */
.pet.expanded .face-panel { flex: 0 0 210px; }
.pet.expanded .chat-panel { display: flex; border-top: 1px solid var(--border); }
.pet.expanded .face-bar { opacity: 1; }
.pet.expanded #petCollapse { display: grid; }
.pet .msg { font-size: 14px; }
.pet #input { font-size: 14px; }
@media (min-width: 860px) {
  .pet .app { flex-direction: column; }
  .pet .face-panel { height: auto; }
  .pet .chat-panel { flex: 1 1 auto; border-left: none; }
}
