:root {
  --bg: #1a1b1e;
  --bg-elevated: #25262b;
  --bg-input: #2c2e33;
  --border: #34353a;
  --text: #e6e6e6;
  --text-dim: #9aa0a6;
  --accent: #4c8dff;
  --accent-hover: #3a7af0;
  --user-bubble: #2f3b52;
  --assistant-bubble: #25262b;
  --danger: #e5484d;
  --green: #3dd68c;
  --yellow: #f5c542;
  --red: #f5544c;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN",
    "Noto Sans JP", Meiryo, sans-serif;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
}

#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
#sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  margin-bottom: 12px;
}
.brand-mark { color: var(--accent); font-size: 20px; }
.brand-name { font-size: 15px; }

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conv-item {
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 2px;
}
.conv-item:hover { background: var(--bg-input); color: var(--text); }
.conv-item.active { background: var(--bg-input); color: var(--text); }
.conv-item .conv-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.conv-item .conv-del {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
}
.conv-item:hover .conv-del { opacity: 1; }
.conv-item .conv-del:hover { color: var(--red); }

.sidebar-footer {
  padding: 10px;
  border-top: 1px solid var(--border);
}

/* ---------- Main ---------- */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.chat-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-picker {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}
.status-ok { background: var(--green); }
.status-error { background: var(--red); }
.status-unknown { background: var(--yellow); }

/* ---------- API key banner ---------- */
.api-key-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: #2a2410;
  border-bottom: 1px solid #4a3f15;
  font-size: 13px;
  color: #f0d98a;
}
.api-key-banner input {
  flex: 1;
  min-width: 180px;
  max-width: 360px;
}
.api-key-banner .btn-primary { padding: 7px 12px; }

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
}

.msg {
  max-width: 800px;
  margin: 0 auto 18px;
  padding: 0 20px;
  display: flex;
  gap: 12px;
}
.msg .avatar {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 700;
}
.msg.user .avatar { background: var(--user-bubble); color: #cfe0ff; }
.msg.assistant .avatar { background: #3a2f52; color: #d9c8ff; }

.msg .body { flex: 1; min-width: 0; }
.msg .role-name {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.msg .content {
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg .content p { margin: 0 0 0.7em; }
.msg .content p:last-child { margin-bottom: 0; }
.msg .content pre {
  background: #16171a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 13px;
}
.msg .content code {
  font-family: var(--mono);
  font-size: 13px;
  background: #16171a;
  padding: 1px 5px;
  border-radius: 4px;
}
.msg .content pre code { background: none; padding: 0; }
.msg .content table { border-collapse: collapse; }
.msg .content th, .msg .content td {
  border: 1px solid var(--border);
  padding: 5px 9px;
}

.cursor-blink::after {
  content: "▋";
  animation: blink 1s steps(2) infinite;
  color: var(--accent);
}
@keyframes blink { 0%,50% { opacity: 1; } 50.01%,100% { opacity: 0; } }

.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  gap: 10px;
  text-align: center;
}
.empty-state .big { font-size: 42px; color: var(--accent); }

/* ---------- Composer ---------- */
#composer {
  border-top: 1px solid var(--border);
  padding: 14px 16px 10px;
}
.composer-inner {
  max-width: 800px;
  margin: 0 auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 8px 8px 14px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.composer-inner:focus-within { border-color: var(--accent); }
#prompt-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  resize: none;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  max-height: 200px;
  outline: none;
  padding: 6px 0;
}
.composer-actions { display: flex; gap: 8px; }
.composer-hint {
  max-width: 800px;
  margin: 6px auto 0;
  font-size: 12px;
  color: var(--text-dim);
  min-height: 16px;
}

/* ---------- Buttons / inputs ---------- */
button { font-family: var(--font); }
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  width: 100%;
}
.btn-ghost:hover { background: var(--bg-input); }
.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 9px 18px;
  cursor: pointer;
  font-weight: 600;
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-stop {
  background: var(--bg-elevated);
  color: var(--red);
  border: 1px solid var(--red);
  border-radius: 8px;
  padding: 9px 14px;
  cursor: pointer;
}
.btn-icon {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
  padding: 4px 6px;
  border-radius: 6px;
}
.btn-icon:hover { background: var(--bg-input); color: var(--text); }

select, input[type="text"], input[type="number"], input[type="url"], textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 7px;
  padding: 7px 9px;
  font-family: var(--font);
  font-size: 14px;
  outline: none;
}
select:focus, input:focus, textarea:focus { border-color: var(--accent); }
select { max-width: 220px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: min(640px, 92vw);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
}
.modal-header, .modal-footer {
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header { border-bottom: 1px solid var(--border); }
.modal-footer { border-top: 1px solid var(--border); justify-content: flex-end; }
.modal-header h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 20px; overflow-y: auto; }

.settings-section { margin-bottom: 26px; }
.settings-section h3 { margin: 0 0 4px; font-size: 14px; }
.hint { color: var(--text-dim); font-size: 13px; margin: 0 0 12px; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--text-dim);
}
.field > span { font-weight: 500; }
.field input[type="range"] { accent-color: var(--accent); }

.provider-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 12px;
}
.provider-card .row {
  display: grid;
  grid-template-columns: 100px 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.provider-card .row label { font-size: 13px; color: var(--text-dim); }
.provider-card .row input, .provider-card .row select { width: 100%; max-width: none; }
.provider-card .card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.provider-card .card-head strong { font-size: 14px; }
.provider-test {
  font-size: 12px;
  margin-top: 4px;
}
.provider-test.ok { color: var(--green); }
.provider-test.err { color: var(--red); }

/* scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #3a3b40; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #46474d; }

@media (max-width: 720px) {
  #sidebar { width: 200px; }
  .model-picker select { max-width: 120px; }
}
