:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --side: #efede8;
  --text: #1f1e1c;
  --muted: #6b6862;
  --border: #e3e0da;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --accent-soft: #dfe9e4;
  --user-bg: #e9efe9;
  --error: #b3261e;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171716;
    --surface: #222220;
    --side: #1c1c1a;
    --text: #ecebe8;
    --muted: #a19d95;
    --border: #36352f;
    --accent: #7fb3a2;
    --accent-text: #10201b;
    --accent-soft: #2a3a34;
    --user-bg: #26322d;
    --error: #f2b8b5;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Noto Sans SC", "Microsoft YaHei", sans-serif;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.tiny { font-size: 12px; margin: 8px 2px 0; }
.error { color: var(--error); min-height: 1.4em; margin: 0; font-size: 14px; }

.btn {
  font: inherit;
  font-size: 14px;
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  position: relative;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { border-color: var(--border); }
.btn.block { width: 100%; }
.btn.icon { padding: 6px 10px; font-size: 16px; line-height: 1; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.dot {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); margin-left: 6px; vertical-align: middle;
}

input[type=password], input[type=search] {
  font: inherit; padding: 9px 12px; width: 100%;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text);
}

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  display: grid; gap: 12px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }

/* Layout */
.app { height: 100%; display: flex; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* Sidebar */
.sidebar {
  width: 270px; flex-shrink: 0;
  background: var(--side);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.side-head { padding: 12px; display: grid; gap: 8px; }
.side-head input { font-size: 14px; padding: 7px 10px; }
.convo-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.group-label { font-size: 12px; color: var(--muted); padding: 10px 8px 4px; }
.convo {
  display: flex; align-items: center; gap: 4px;
  padding: 7px 8px; border-radius: 8px; cursor: pointer;
  font-size: 14px;
}
.convo:hover { background: var(--border); }
.convo.active { background: var(--accent-soft); }
.convo .name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo .tools { display: none; gap: 2px; }
.convo:hover .tools, .convo.active .tools { display: flex; }
.convo .tools button {
  border: 0; background: none; color: var(--muted); cursor: pointer;
  font-size: 13px; padding: 2px 4px; border-radius: 4px;
}
.convo .tools button:hover { color: var(--text); background: var(--surface); }
.no-results { color: var(--muted); font-size: 13px; padding: 12px 8px; }
.side-foot { padding: 12px; border-top: 1px solid var(--border); }
.scrim { display: none; }
#toggle-side { display: none; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.title {
  margin-right: auto; min-width: 0; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw;
}
.actions { display: flex; gap: 4px; }
.model-switch { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.model-switch button {
  font: inherit; font-size: 13px; padding: 6px 12px;
  border: 0; border-right: 1px solid var(--border);
  background: transparent; color: var(--muted); cursor: pointer;
}
.model-switch button:last-child { border-right: 0; }
.model-switch button[aria-checked="true"] { background: var(--accent); color: var(--accent-text); }
.model-switch button:disabled { cursor: not-allowed; }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 20px 16px; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 12vh; }
.empty h2 { margin: 0 0 8px; font-weight: 600; }
.empty p { margin: 4px 0; }

.msg { margin-bottom: 18px; }
.msg .body { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user .body {
  background: var(--user-bg);
  padding: 10px 14px;
  border-radius: var(--radius);
  margin-left: 15%;
}
.msg.assistant .body {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 12px 16px;
  border-radius: var(--radius);
}
.msg .meta {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  font-size: 12px; color: var(--muted); margin-top: 4px; padding: 0 4px;
}
.msg.user .meta { justify-content: flex-end; }
.msg .meta button {
  font: inherit; font-size: 12px; border: 0; background: none; color: var(--muted);
  cursor: pointer; padding: 0; text-decoration: underline;
}
.msg .meta button:hover { color: var(--text); }
.msg.error .body { border-color: var(--error); color: var(--error); }
.typing::after { content: "▍"; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
}
.composer > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.quick { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.chip {
  font: inherit; font-size: 12.5px; padding: 3px 10px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--bg); color: var(--text); cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.composer textarea {
  display: block; width: 100%;
  font: inherit; resize: vertical; min-height: 72px; max-height: 40vh;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text);
}
.composer-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.composer-row #hint { margin-right: auto; }

/* Dialog */
.dialog {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 0; width: min(560px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dialog-body { padding: 20px; display: grid; gap: 10px; }
.dialog-body h2 { margin: 0; font-size: 18px; }
.dialog-body p { margin: 0; }
.dialog-body textarea {
  font: inherit; font-size: 14px; width: 100%; resize: vertical;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text);
}
.dialog-row { display: flex; align-items: center; gap: 8px; }
.dialog-row span { margin-right: auto; }

/* Narrow screens: sidebar becomes a drawer */
@media (max-width: 900px) {
  #toggle-side { display: inline-block; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20;
    width: min(300px, 85vw);
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim {
    display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, .3);
  }
  .convo .tools { display: flex; }
}
@media (max-width: 640px) {
  .topbar { padding: 8px 12px; }
  .title { max-width: 50vw; }
  .model-switch { order: 3; width: 100%; }
  .model-switch button { flex: 1; padding: 7px 4px; }
  .messages { padding: 14px 12px; }
  .msg.user .body { margin-left: 8%; }
  .composer { padding-left: 12px; padding-right: 12px; }
  #hint { display: none; }
  #count { margin-right: auto; }
}
