:root {
  --bg: #f6f4f1;
  --panel: #ffffff;
  --panel-2: #efece7;
  --line: #ddd8d0;
  --text: #24211d;
  --muted: #756f66;
  --accent: #b4476b;
  --accent-text: #ffffff;
  --user-bubble: #b4476b;
  --user-text: #ffffff;
  --ai-bubble: #ffffff;
  --danger: #b3261e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17151a;
    --panel: #201d24;
    --panel-2: #2a2630;
    --line: #38333f;
    --text: #ece8ee;
    --muted: #a39cab;
    --accent: #d8698d;
    --accent-text: #1b1218;
    --user-bubble: #8f3a58;
    --user-text: #ffffff;
    --ai-bubble: #2a2630;
    --danger: #f2877f;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Yu Gothic UI", "Hiragino Sans", "Noto Sans JP", "Meiryo", sans-serif;
  font-size: 15px;
  line-height: 1.7;
}
button, input, textarea, select { font: inherit; color: inherit; }

.app { display: flex; height: 100dvh; }

/* ---- サイドバー ---- */
.sidebar {
  width: 260px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.sidebar-head { padding: 16px; border-bottom: 1px solid var(--line); }
.sidebar-head h1 { margin: 0; font-size: 16px; }
.char-list { list-style: none; margin: 0; padding: 8px; flex: 1; overflow-y: auto; }
.char-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 8px 10px;
  border: 0; border-radius: 10px; background: none;
  text-align: left; cursor: pointer;
}
.char-item:hover { background: var(--panel-2); }
.char-item.active { background: var(--panel-2); font-weight: 600; }
.avatar {
  width: 34px; height: 34px; flex: none;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  font-weight: 700;
}
.char-item-text { display: flex; flex-direction: column; min-width: 0; text-align: left; }
.char-item-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.char-item-tagline { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; font-weight: 400; color: var(--muted); }
.sidebar-foot { padding: 12px; display: grid; gap: 8px; border-top: 1px solid var(--line); }
.backdrop { display: none; }

/* ---- ボタン ---- */
.btn {
  padding: 8px 14px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { filter: brightness(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
.btn.ghost { background: none; }
.btn.danger { background: none; color: var(--danger); border-color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.link-btn { text-align: center; text-decoration: none; }
[hidden] { display: none !important; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn { border: 0; background: none; font-size: 20px; cursor: pointer; padding: 4px 8px; }
.menu-btn { display: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- チャット ---- */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-head {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.chat-title { flex: 1; min-width: 0; }
.chat-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.messages { flex: 1; overflow-y: auto; padding: 20px 16px; }
.msg { max-width: 760px; margin: 0 auto 14px; display: flex; flex-direction: column; align-items: flex-start; }
.msg.user { align-items: flex-end; }
.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  background: var(--ai-bubble);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-color: transparent; }
.bubble.typing::after { content: "…"; color: var(--muted); }
.msg-actions { display: flex; gap: 10px; margin-top: 2px; padding: 0 6px; opacity: 0; }
.msg:hover .msg-actions, .msg:focus-within .msg-actions, .msg:last-child .msg-actions { opacity: 1; }
.msg-actions button {
  border: 0; background: none; padding: 0;
  font-size: 12px; color: var(--muted); cursor: pointer;
}
.msg-actions button:hover { color: var(--text); text-decoration: underline; }
.edit-area { width: min(100%, 640px); }
.edit-area textarea { width: 100%; min-height: 90px; }
.edit-area .edit-buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.empty { max-width: 760px; margin: 40px auto; text-align: center; color: var(--muted); }

.error {
  max-width: 760px; width: calc(100% - 32px);
  margin: 0 auto 8px; padding: 8px 12px;
  border: 1px solid var(--danger); border-radius: 10px;
  color: var(--danger); font-size: 13px;
}

.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 12px 16px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
textarea, input[type="text"], input[type="password"], input[type="number"], select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg);
}
.composer textarea { flex: 1; resize: none; max-height: 180px; }

/* ---- ダイアログ ---- */
dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 20px;
  border: 1px solid var(--line); border-radius: 16px;
  background: var(--panel); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
dialog label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
dialog label > * { margin-top: 4px; font-size: 15px; color: var(--text); }
dialog textarea { resize: vertical; }
.row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.row.two { grid-template-columns: repeat(2, 1fr); }
dialog label.check { display: flex; gap: 8px; align-items: flex-start; padding-top: 20px; }
dialog label.check input { width: auto; margin-top: 3px; }
dialog label.check span { margin-top: 0; font-size: 13px; color: var(--text); }
dialog .error { margin: 0 0 8px; }
/* メッセージ風のキャラは、改行ごとに吹き出しを分けて連投っぽく見せる */
.msg .bubble + .bubble { margin-top: 4px; }
.hint { margin: -4px 0 12px; font-size: 12px; color: var(--muted); }
.dialog-actions { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.spacer { flex: 1; }

/* ---- ログイン ---- */
.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 16px; }
.login-card {
  width: min(380px, 100%);
  padding: 24px;
  display: grid; gap: 14px;
  background: var(--panel);
  border: 1px solid var(--line); border-radius: 16px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card .hint { margin: 0; font-size: 13px; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card label.check { display: flex; gap: 8px; align-items: flex-start; color: var(--text); }
.login-card label.check input { width: auto; margin-top: 5px; }
.login-card .error { width: 100%; margin: 0; }
.notice {
  margin: 0; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel-2);
  font-size: 13px;
}

/* ---- 管理画面 ---- */
.admin-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.admin-head h1 { margin: 0; font-size: 17px; flex: 1; }
.admin-head a { text-decoration: none; }
.admin-main { max-width: 920px; margin: 0 auto; padding: 20px 16px 60px; display: grid; gap: 16px; }
.admin-main .error { width: 100%; margin: 0; }
.card { padding: 18px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.card-head h2 { margin: 0; font-size: 16px; }
.card .hint { margin: 0 0 12px; font-size: 13px; }
.card-actions { display: flex; gap: 8px; }
.badge {
  padding: 1px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 600;
  background: var(--panel-2); color: var(--muted);
}
.badge.on { background: #1f7a4d; color: #ffffff; }
.badge.wait { background: #a86a00; color: #ffffff; }
.url-row, .inline-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.url-row input { flex: 1; min-width: 200px; }
.inline-form input, .inline-form select { width: auto; flex: 1 1 180px; max-width: 320px; }
.table-wrap { overflow-x: auto; margin-bottom: 12px; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table th { font-size: 12px; font-weight: 600; color: var(--muted); }
.table code { font-family: Consolas, "Cascadia Mono", monospace; font-size: 14px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.card details { margin-top: 12px; font-size: 13px; }
.card summary { cursor: pointer; color: var(--muted); }
pre.log {
  margin: 8px 0 0; padding: 10px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  font-family: Consolas, "Cascadia Mono", monospace; font-size: 12px; line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.log-view { display: grid; gap: 4px; }
.log-item { padding: 12px 0; border-top: 1px solid var(--line); }
.log-item .msg { max-width: none; margin: 6px 0 0; }
.log-meta { font-size: 12px; color: var(--muted); }
.log-system { margin-top: 4px !important; }

/* ---- スマホ ---- */
@media (max-width: 720px) {
  .menu-btn { display: block; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .backdrop { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, 0.5); }
  .bubble { max-width: 92%; }
  .row { grid-template-columns: 1fr; }
  .chat-head .btn.small { padding: 5px 8px; font-size: 12px; }
}
