:root {
  --bg: #ffffff;
  --panel: #f7f7f8;
  --border: #e3e3e6;
  --text: #1f2023;
  --muted: #6b6e76;
  --accent: #10a37f;
  --user-bubble: #ecf3ff;
  --code-bg: #1e1e23;
  --code-fg: #f3f3f3;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f2023; --panel: #2a2b2f; --border: #3a3b40; --text: #ececf1;
    --muted: #a0a3ad; --user-bubble: #2f4163; --code-bg: #14151a; --code-fg: #f3f3f3;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  font-family: system-ui, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  color: var(--text); background: var(--bg); font-size: 15px; line-height: 1.6;
  height: 100vh; height: 100dvh; display: flex; flex-direction: column;
  overscroll-behavior: none;
}

/* 共通トップナビ (.site-nav*) は /var/www/html/_header.php が <style> で供給する
   (index.php が require)。CSS の二重定義を避けるためここには持たない。 */

#app { display: flex; flex: 1; min-height: 0; position: relative; overflow: hidden; }

/* モバイルでサイドバーを開いた時の半透明バックドロップ (タップで閉じる)。
   デスクトップ及び閉時は非表示。表示制御は下の @media (max-width:720px) 内。 */
.scrim { display: none; }

/* ---- sidebar ---- */
#sidebar {
  width: 260px; flex: 0 0 260px; background: var(--panel);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
#sidebar.collapsed { display: none; }
.side-head, .side-foot { padding: 10px; }
.side-foot { border-top: 1px solid var(--border); }
#new-chat {
  width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); cursor: pointer; font-size: 14px;
}
#new-chat:hover { border-color: var(--accent); }
#thread-list { flex: 1; overflow-y: auto; padding: 4px; }
.thread-item {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; font-size: 14px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text);
  display: flex; align-items: center; gap: 6px;
}
.thread-item:hover { background: var(--border); }
.thread-item.active { background: var(--border); font-weight: 600; }
.thread-item .del { margin-left: auto; opacity: 0; color: var(--muted); }
.thread-item:hover .del { opacity: 1; }

/* ---- main ---- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  border-bottom: 1px solid var(--border); background: var(--bg);
}
#topbar button, #model-select {
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  border-radius: 7px; padding: 6px 10px; cursor: pointer; font-size: 13px;
}
#topbar button:hover { border-color: var(--accent); }
.thread-title { color: var(--muted); font-size: 13px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; max-width: 40vw; }
.spacer { flex: 1; }

#messages { flex: 1; overflow-y: auto; padding: 24px 0; }
.msg { max-width: 760px; margin: 0 auto 18px; padding: 0 16px; display: flex; gap: 12px; }
.msg .avatar {
  flex: 0 0 30px; width: 30px; height: 30px; border-radius: 6px; font-size: 13px;
  display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600;
}
.msg.user .avatar { background: #5a6b8c; }
.msg.assistant .avatar { background: var(--accent); }
.msg .body { min-width: 0; flex: 1; }
.msg .role { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.msg .content { word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user .content {
  background: var(--user-bubble); padding: 8px 12px; border-radius: 10px; display: inline-block;
}
.msg .content p:first-child { margin-top: 0; }
.msg .content p:last-child { margin-bottom: 0; }
.msg .content pre {
  background: var(--code-bg); color: var(--code-fg); padding: 12px; border-radius: 8px;
  overflow-x: auto; font-size: 13px;
}
.msg .content code {
  font-family: "SFMono-Regular", Consolas, monospace; font-size: 0.9em;
}
.msg .content :not(pre) > code {
  background: rgba(128,128,128,0.18); padding: 1px 5px; border-radius: 4px;
}
.msg .content table { border-collapse: collapse; }
.msg .content th, .msg .content td { border: 1px solid var(--border); padding: 4px 8px; }
.cursor::after { content: "▋"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 0%,50% { opacity: 1; } 51%,100% { opacity: 0; } }

/* ---- composer ---- */
#composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 12px 16px;
  border-top: 1px solid var(--border); max-width: 792px; margin: 0 auto; width: 100%;
}
#input {
  flex: 1; resize: none; max-height: 200px; padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font: inherit; line-height: 1.5;
}
#input:focus { outline: none; border-color: var(--accent); }
#send, #stop {
  padding: 11px 18px; border: none; border-radius: 10px; cursor: pointer; font: inherit;
  background: var(--accent); color: #fff;
}
#send:disabled { opacity: 0.5; cursor: default; }
#stop { background: #c0392b; }
.hidden { display: none !important; }

/* ---- settings modal ---- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal-box {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  padding: 20px; width: min(560px, 92vw);
}
.modal-box h2 { margin: 0 0 14px; font-size: 18px; }
.modal-box label { display: block; font-size: 13px; color: var(--muted); }
.modal-box textarea {
  width: 100%; margin-top: 6px; padding: 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); font: inherit;
}
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 14px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions button {
  padding: 8px 16px; border-radius: 8px; border: none; cursor: pointer;
  background: var(--accent); color: #fff; font: inherit;
}
.modal-actions .ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }

.empty { text-align: center; color: var(--muted); margin-top: 18vh; padding: 0 20px; }
.empty h1 { font-weight: 600; font-size: 22px; }

@media (max-width: 720px) {
  /* サイドバーは本文に重なるドロワー。閉時 (.collapsed) は display:none のまま。 */
  #sidebar {
    position: absolute; top: 0; bottom: 0; left: 0; z-index: 40;
    width: min(80vw, 300px); flex-basis: auto;
    box-shadow: 2px 0 16px rgba(0, 0, 0, 0.35);
  }
  /* サイドバーが開いている (#app.sidebar-open) ときだけ暗幕を出す。 */
  #app.sidebar-open .scrim {
    display: block; position: absolute; inset: 0;
    background: rgba(0, 0, 0, 0.45); z-index: 35;
  }
  .thread-title { display: none; }
}
