:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede5;
  --text: #22201c;
  --muted: #6b665c;
  --line: #ddd8cc;
  --accent: #7a3e1d;
  --accent-text: #ffffff;
  --accent-soft: #f3e6dc;
  --honbun: #2f5d50;
  --kaisetsu: #5a4a8a;
  --error: #b3261e;
  --radius: 10px;
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic UI", "Yu Gothic", "Meiryo", system-ui, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a17;
    --surface: #24221e;
    --surface-2: #2d2a25;
    --text: #ece8df;
    --muted: #a39d90;
    --line: #3b3731;
    --accent: #e0a37c;
    --accent-text: #1b1a17;
    --accent-soft: #3a2c22;
    --honbun: #8cc7b4;
    --kaisetsu: #b9aae6;
    --error: #f2b8b5;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.7; }
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.error { color: var(--error); min-height: 1.5em; margin: 0; }

button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 6px 12px; }
button:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.link { border: none; background: none; color: var(--accent); padding: 0 4px; text-decoration: underline; }
button.icon { border: none; background: none; font-size: 22px; line-height: 1; padding: 4px 8px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ログイン */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px 24px; display: grid; gap: 12px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.login-card input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }

/* 全体 */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface); }
.title { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.book { font-weight: 700; font-size: 17px; white-space: nowrap; }
.volume { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user { display: flex; align-items: center; gap: 10px; font-size: 13px; white-space: nowrap; }
.layout { flex: 1; min-height: 0; display: flex; }
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 16px; }
.messages > * { width: 100%; max-width: 780px; margin: 0 auto; }

.welcome h2 { font-size: 18px; margin: 8px 0; }
.welcome p { margin: 6px 0; }
.examples { display: flex; flex-wrap: wrap; gap: 8px; }
.example { font-size: 13px; background: var(--surface-2); }

/* 質問と回答 */
.q { align-self: flex-end; background: var(--accent-soft); border-radius: var(--radius); padding: 10px 14px; white-space: pre-wrap; max-width: 780px; }
.q-wrap { display: flex; justify-content: flex-end; }
.a { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.a.notfound { border-style: dashed; }
.a.pending { color: var(--muted); }
.a .body p { margin: 0 0 .6em; }
.a .body p:last-child { margin-bottom: 0; }
.cite { display: inline-block; min-width: 1.6em; padding: 0 4px; margin: 0 1px; font-size: 12px; line-height: 1.5; vertical-align: 2px; border-radius: 6px; border: 1px solid var(--accent); color: var(--accent); background: transparent; }
.cite:hover { background: var(--accent); color: var(--accent-text); }
.sources { margin: 12px 0 0; padding: 10px 0 0; border-top: 1px solid var(--line); font-size: 13px; }
.sources h3 { font-size: 13px; margin: 0 0 6px; color: var(--muted); font-weight: 600; }
.sources ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.src { display: grid; grid-template-columns: auto auto minmax(0, 1fr) auto; align-items: center; gap: 8px; }
.src .n { font-weight: 700; color: var(--accent); min-width: 2em; }
.src .c { min-width: 0; overflow-wrap: anywhere; }
.badge { white-space: nowrap; }
.src button { white-space: nowrap; }
.badge { font-size: 11px; padding: 0 6px; border-radius: 4px; border: 1px solid currentColor; }
.badge.honbun { color: var(--honbun); }
.badge.kaisetsu { color: var(--kaisetsu); }
.src .reason { color: var(--muted); font-size: 12px; }
.src button { font-size: 12px; padding: 2px 8px; }
.sources details { margin-top: 8px; }
.sources summary { cursor: pointer; color: var(--muted); }
.meta { margin-top: 8px; font-size: 12px; color: var(--muted); }
.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; vertical-align: -1px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }

/* 入力 */
.composer { display: flex; gap: 8px; padding: 12px 16px 4px; width: 100%; max-width: 812px; margin: 0 auto; }
.composer textarea { flex: 1; resize: vertical; min-height: 48px; max-height: 200px; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.disclaimer { font-size: 12px; color: var(--muted); margin: 0 auto 10px; padding: 0 16px; max-width: 812px; width: 100%; }

/* 出典の頁 */
.preview { width: min(46vw, 620px); border-left: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; }
.preview-head { display: flex; justify-content: space-between; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.pv-citation { font-weight: 600; font-size: 14px; }
.preview-body { flex: 1; overflow: auto; padding: 12px; background: var(--surface-2); display: grid; place-items: start center; }
.preview-body img { max-width: 100%; height: auto; box-shadow: 0 1px 6px rgba(0, 0, 0, .15); background: #fff; }
.preview-nav { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--line); }
.preview-nav a { color: var(--accent); font-size: 13px; }
.in-source { color: var(--accent); font-weight: 600; }

@media (max-width: 860px) {
  .preview { position: fixed; inset: 0; width: 100%; z-index: 10; border-left: none; }
  .volume { display: none; }
  #remaining { display: none; }
}
