:root {
  --bg: #faf7f2;
  --bg-panel: #ffffff;
  --bg-bubble-user: #4a6b57;
  --bg-bubble-ai: #efe9df;
  --text: #2b2622;
  --text-soft: #8a8178;
  --text-on-user: #ffffff;
  --border: #e3dcd2;
  --accent: #4a6b57;
  --danger: #b0563f;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #17150f;
    --bg-panel: #201d16;
    --bg-bubble-user: #3d5a49;
    --bg-bubble-ai: #2a261e;
    --text: #e8e2d8;
    --text-soft: #8f887c;
    --text-on-user: #f2f0ea;
    --border: #363126;
    --accent: #7fa88f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: 'Noto Sans TC', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
}
header h1 { font-size: 16px; margin: 0; font-weight: 600; }
header .spacer { flex: 1; }
nav button {
  background: none;
  border: none;
  color: var(--text-soft);
  font-size: 14px;
  padding: 6px 10px;
  cursor: pointer;
  border-radius: 8px;
}
nav button.active { color: var(--accent); font-weight: 600; }

main { flex: 1; overflow: hidden; display: flex; }
.panel { flex: 1; display: none; flex-direction: column; overflow: hidden; }
.panel.active { display: flex; }

/* 聊天 */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.msg { max-width: 78%; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; line-height: 1.6; font-size: 15px; }
.msg.user { align-self: flex-end; background: var(--bg-bubble-user); color: var(--text-on-user); border-bottom-right-radius: 4px; }
.msg.ai { align-self: flex-start; background: var(--bg-bubble-ai); border-bottom-left-radius: 4px; }
.msg.tool { align-self: flex-start; font-size: 12px; color: var(--text-soft); background: none; padding: 0 14px; }
.msg.error { align-self: center; color: var(--danger); font-size: 13px; background: none; }

#composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
}
#composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
  font-size: 15px;
  max-height: 140px;
}
#composer button {
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  padding: 0 18px;
  font-size: 15px;
  cursor: pointer;
}
#composer button:disabled { opacity: 0.5; cursor: default; }

/* 記憶 */
#memory { overflow-y: auto; padding: 16px; gap: 16px; }
.mem-section { background: var(--bg-panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px; }
.mem-section h2 { font-size: 14px; margin: 0 0 8px; color: var(--accent); }
.mem-section pre { white-space: pre-wrap; font: inherit; margin: 0; line-height: 1.6; font-size: 14px; }
.mem-item { padding: 6px 0; border-bottom: 1px dashed var(--border); font-size: 14px; }
.mem-item:last-child { border-bottom: none; }
.mem-item .tag { font-size: 11px; color: var(--text-soft); margin-right: 6px; }
.diary-entry { cursor: pointer; }
.diary-entry:hover { color: var(--accent); }
.muted { color: var(--text-soft); font-size: 13px; }

/* 登入 */
#login {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  flex-direction: column; gap: 12px;
}
#login input {
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px; font-size: 15px; background: var(--bg-panel); color: var(--text);
  width: min(320px, 80vw);
}
#login button { border: none; border-radius: var(--radius); background: var(--accent); color: #fff; padding: 10px 24px; font-size: 15px; cursor: pointer; }
.hidden { display: none !important; }

@media (max-width: 600px) {
  .msg { max-width: 88%; }
}
