/* Reset */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --bg: #1a1b26;
  --bg-surface: #24283b;
  --bg-hover: #2f3352;
  --text: #c0caf5;
  --text-dim: #565f89;
  --accent: #7aa2f7;
  --accent-dim: #3d59a1;
  --green: #9ece6a;
  --red: #f7768e;
  --orange: #ff9e64;
  --yellow: #e0af68;
  --border: #3b4261;
  --user-bg: #283457;
  --assistant-bg: #1e2030;
  --tool-bg: #1a1e2e;
  --radius: 8px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

/* === Login Page === */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--bg-surface);
  padding: 2.5rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.login-card h1 {
  color: var(--accent);
  font-size: 1.8rem;
  margin-bottom: 0.25rem;
}

.login-card .subtitle {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-bottom: 1.5rem;
}

.login-card input {
  width: 100%;
  padding: 0.7rem 1rem;
  margin-bottom: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
}

.login-card input:focus {
  border-color: var(--accent);
}

.login-card button {
  width: 100%;
  padding: 0.7rem;
  margin-top: 0.5rem;
  background: var(--accent);
  color: #1a1b26;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

.login-card button:hover { opacity: 0.85; }

.error {
  margin-top: 0.75rem;
  color: var(--red);
  font-size: 0.85rem;
}

/* === Chat Layout === */
body:not(.login-body) {
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header-left h1 {
  font-size: 1.1rem;
  color: var(--accent);
}

.header-right {
  display: flex;
  gap: 0.5rem;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.dot.running { background: var(--green); box-shadow: 0 0 6px var(--green); }
.dot.stopped { background: var(--red); opacity: 0.6; }

#statusText {
  font-size: 0.85rem;
  color: var(--text-dim);
}

/* Buttons */
.btn {
  padding: 0.35rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover:not(:disabled) { background: var(--bg-hover); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-start { border-color: var(--green); color: var(--green); }
.btn-start:hover:not(:disabled) { background: rgba(158, 206, 106, 0.1); }
.btn-stop { border-color: var(--red); color: var(--red); }
.btn-stop:hover:not(:disabled) { background: rgba(247, 118, 142, 0.1); }
.btn-logout { border-color: var(--text-dim); color: var(--text-dim); }

/* Chat area */
#chatArea {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
}

#messages {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Messages */
.msg {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  max-width: 85%;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--user-bg);
  border: 1px solid var(--accent-dim);
}

.msg.assistant {
  align-self: flex-start;
  background: var(--assistant-bg);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 100%;
}

.msg-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.35rem;
  color: var(--text-dim);
}

.msg.user .msg-label { color: var(--accent); }
.msg.assistant .msg-label { color: var(--green); }

/* Markdown content */
.msg-content {
  font-size: 0.9rem;
  line-height: 1.7;
}

.msg-content p { margin-bottom: 0.5rem; }
.msg-content p:last-child { margin-bottom: 0; }

.msg-content code {
  background: rgba(122, 162, 247, 0.1);
  padding: 0.15rem 0.35rem;
  border-radius: 3px;
  font-size: 0.85em;
  font-family: 'Fira Code', 'Consolas', monospace;
}

.msg-content pre {
  background: #16161e;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  overflow-x: auto;
  margin: 0.5rem 0;
}

.msg-content pre code {
  background: none;
  padding: 0;
  font-size: 0.82em;
}

.msg-content ul, .msg-content ol {
  margin: 0.5rem 0;
  padding-left: 1.5rem;
}

.msg-content blockquote {
  border-left: 3px solid var(--accent-dim);
  padding-left: 0.75rem;
  color: var(--text-dim);
  margin: 0.5rem 0;
}

.msg-content a {
  color: var(--accent);
  text-decoration: none;
}

.msg-content a:hover { text-decoration: underline; }

/* Tool blocks */
.tool-block {
  margin: 0.5rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.tool-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  background: var(--tool-bg);
  cursor: pointer;
  font-size: 0.8rem;
  user-select: none;
}

.tool-header:hover { background: var(--bg-hover); }

.tool-icon { font-size: 0.9rem; }
.tool-name { color: var(--orange); font-weight: 600; }
.tool-chevron {
  margin-left: auto;
  transition: transform 0.2s;
  color: var(--text-dim);
}

.tool-block.open .tool-chevron { transform: rotate(90deg); }

.tool-body {
  display: none;
  padding: 0.5rem 0.75rem;
  background: #16161e;
  font-size: 0.78rem;
  font-family: 'Consolas', monospace;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 300px;
  overflow-y: auto;
  color: var(--text-dim);
}

.tool-block.open .tool-body { display: block; }

.tool-result {
  border-top: 1px solid var(--border);
  padding: 0.5rem 0.75rem;
  display: none;
  background: #16161e;
  font-size: 0.78rem;
  font-family: 'Consolas', monospace;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 200px;
  overflow-y: auto;
  color: var(--text-dim);
}

.tool-block.open .tool-result.has-content { display: block; }

/* === AskUserQuestion Buttons === */
.ask-user-block {
  margin: 0.75rem 0;
  padding: 1rem;
  background: var(--bg-surface);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius);
  border-left: 3px solid var(--accent);
}

.ask-question { margin-bottom: 0.5rem; }

.ask-question-text {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.75rem;
}

.ask-options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.ask-option {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 0.6rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
  text-align: left;
  min-width: 140px;
  max-width: 280px;
}

.ask-option:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--bg-hover);
}

.ask-option:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ask-opt-desc {
  display: block;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 0.2rem;
  line-height: 1.3;
}

.ask-other {
  border-style: dashed;
  color: var(--text-dim);
}

/* === TodoWrite / Task List === */
.todo-block {
  margin: 0.5rem 0;
  padding: 0.75rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.todo-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.5rem;
}

.todo-list { display: flex; flex-direction: column; gap: 0.35rem; }

.todo-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.35rem 0.5rem;
  border-radius: 4px;
  font-size: 0.85rem;
}

.todo-item.completed { color: var(--green); }
.todo-item.in_progress { color: var(--yellow); }
.todo-item.pending { color: var(--text-dim); }

.todo-status { flex-shrink: 0; font-size: 0.9rem; }
.todo-content { flex: 1; line-height: 1.4; }
.todo-empty { color: var(--text-dim); font-size: 0.85rem; font-style: italic; }

/* Input area */
#inputArea {
  padding: 0.75rem 1rem;
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.input-wrapper {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  gap: 0.5rem;
}

#msgInput {
  flex: 1;
  padding: 0.6rem 0.85rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  resize: none;
  outline: none;
  min-height: 40px;
  max-height: 200px;
  transition: border-color 0.2s;
}

#msgInput:focus { border-color: var(--accent); }
#msgInput:disabled { opacity: 0.5; }

#btnSend {
  padding: 0.6rem 1.2rem;
  background: var(--accent);
  color: #1a1b26;
  border: none;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: opacity 0.15s;
  align-self: flex-end;
}

#btnSend:hover:not(:disabled) { opacity: 0.85; }
#btnSend:disabled { opacity: 0.4; cursor: not-allowed; }

/* Busy indicator */
.busy {
  text-align: center;
  padding: 0.4rem;
  color: var(--yellow);
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--yellow);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.done-indicator {
  text-align: center;
  padding: 0.4rem;
  color: var(--green);
  font-size: 0.85rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  animation: fadeIn 0.3s ease-out;
}

.tool-summary {
  color: var(--text-dim);
  font-size: 0.78rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 400px;
}

/* Error toast */
.toast {
  position: fixed;
  top: 1rem;
  right: 1rem;
  padding: 0.6rem 1rem;
  background: var(--red);
  color: #1a1b26;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 600;
  z-index: 1000;
  animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* Responsive */
@media (max-width: 600px) {
  header { padding: 0.4rem 0.6rem; }
  .header-left h1 { font-size: 0.95rem; }
  .btn { padding: 0.3rem 0.6rem; font-size: 0.75rem; }
  #chatArea { padding: 0.5rem; }
  .msg { max-width: 95%; }
}
