:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --muted: #9aa3b2;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --user-bubble: #2563eb;
  --assistant-bubble: #20242e;
  --danger: #ef4444;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans",
    "Noto Sans JP", Meiryo, sans-serif;
  font-size: 15px;
}

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

/* Sidebar */
#sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.2s ease;
}
#sidebar.hidden { margin-left: -280px; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-header h1 { font-size: 18px; margin: 0; }

#new-chat {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
}
#new-chat:hover { background: var(--accent); border-color: var(--accent); }

#conversation-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}
#conversation-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  margin-bottom: 2px;
}
#conversation-list li:hover { background: var(--panel-2); color: var(--text); }
#conversation-list li.active { background: var(--panel-2); color: var(--text); }
#conversation-list .title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#conversation-list .del {
  opacity: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 15px;
}
#conversation-list li:hover .del { opacity: 1; }
#conversation-list .del:hover { color: var(--danger); }

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

#chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
#toggle-sidebar {
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
}
#chat-title {
  font-weight: 600;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.controls { display: flex; gap: 12px; }
.controls label {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.controls select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
}

.msg-row {
  display: flex;
  padding: 6px 20px;
}
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.bubble {
  max-width: min(760px, 85%);
  padding: 12px 16px;
  border-radius: 14px;
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg-row.user .bubble {
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-row.assistant .bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.bubble.error { border-color: var(--danger); color: #fca5a5; }

/* Markdown */
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre {
  background: #0b0d12;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 10px 0;
}
.bubble code {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
}
.bubble :not(pre) > code {
  background: #0b0d12;
  padding: 2px 5px;
  border-radius: 4px;
}
.bubble ul, .bubble ol { margin: 8px 0; padding-left: 22px; }
.bubble h1, .bubble h2, .bubble h3 { margin: 12px 0 8px; }
.bubble a { color: #93c5fd; }
.bubble blockquote {
  border-left: 3px solid var(--border);
  margin: 8px 0;
  padding-left: 12px;
  color: var(--muted);
}
.cursor::after {
  content: "▍";
  animation: blink 1s steps(2, start) infinite;
  color: var(--muted);
}
@keyframes blink { to { visibility: hidden; } }

.empty-state {
  text-align: center;
  color: var(--muted);
  margin-top: 20vh;
  padding: 0 20px;
}

/* Composer */
#composer {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
#input {
  flex: 1;
  resize: none;
  max-height: 200px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font: inherit;
  line-height: 1.5;
}
#input:focus { outline: none; border-color: var(--accent); }
#send {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 0 20px;
  height: 44px;
  font-weight: 600;
  cursor: pointer;
}
#send:hover { background: var(--accent-2); }
#send:disabled { opacity: 0.5; cursor: not-allowed; }

/* Settings button */
.settings-btn {
  margin: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
}
.settings-btn:hover { color: var(--text); border-color: var(--accent); }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  width: min(440px, 90vw);
}
.modal-card h2 { margin: 0 0 16px; font-size: 18px; }
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}
.field input {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font: inherit;
}
.field input:focus { outline: none; border-color: var(--accent); }
.hint { font-size: 12px; color: var(--muted); line-height: 1.5; margin: 12px 0 0; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}
.btn-primary, .btn-secondary {
  border-radius: 8px;
  padding: 8px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
}
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-2); }
.btn-secondary { background: transparent; color: var(--text); }
.btn-secondary:hover { background: var(--panel-2); }

@media (max-width: 720px) {
  #sidebar {
    position: absolute;
    z-index: 10;
    height: 100%;
  }
  .controls label { font-size: 11px; }
}
