:root {
  --bg: #161616;
  --bg-2: #1f1f1f;
  --bg-3: #292929;
  --border: #333;
  --text: #e8e6e2;
  --text-dim: #999;
  --accent: #d97757;
  --accent-2: #b4603f;
  --danger: #d9534f;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

.hidden { display: none !important; }

.screen {
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

/* Login */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-box {
  width: min(320px, 90vw);
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}
.login-box h1 {
  margin: 0 0 8px;
  font-size: 20px;
  text-align: center;
  color: var(--accent);
}
.login-box input {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 16px;
}
.login-box button {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 10px;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
}
.login-box button:hover { background: var(--accent-2); }
.error { color: var(--danger); font-size: 13px; margin: 0; }

/* App layout */
#app-screen {
  display: flex;
  flex-direction: row;
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 20;
}

.sidebar {
  width: 260px;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 21;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 17px;
}

.conv-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow-y: auto;
  flex: 1;
}
.conv-list li {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 16px;
}
.conv-list li:hover { background: var(--bg-3); }
.conv-list li.active { background: var(--accent); color: #fff; }

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  border-top: 1px solid var(--border);
  font-size: 15px;
}
.voice-toggle { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.conn-status { color: var(--danger); font-size: 10px; }
.conn-status.connected { color: #5cb85c; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}
.icon-btn:hover { background: var(--bg-3); }

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
}
#hamburger-btn { display: none; }
#conv-title {
  flex: 1;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg { max-width: 85%; padding: 10px 14px; border-radius: 14px; line-height: 1.5; font-size: 18px; white-space: pre-wrap; word-wrap: break-word; }
.msg-user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg-assistant { align-self: flex-start; background: var(--bg-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

.msg-thinking {
  align-self: flex-start;
  max-width: 85%;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  color: var(--text-dim);
}
.msg-thinking summary { cursor: pointer; padding: 6px 10px; user-select: none; }
.msg-thinking .thinking-body { padding: 0 10px 8px; white-space: pre-wrap; }

.msg-tool {
  align-self: flex-start;
  max-width: 90%;
  font-size: 14px;
  color: var(--text-dim);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  font-family: ui-monospace, monospace;
}

.msg-tool-result {
  align-self: flex-start;
  max-width: 90%;
  font-size: 14px;
  color: var(--text-dim);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  cursor: pointer;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: ui-monospace, monospace;
}

.msg-voice {
  align-self: flex-start;
  max-width: 85%;
  background: var(--bg-2);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.msg-voice-label {
  font-size: 14px;
  color: var(--accent);
}
.msg-voice audio {
  height: 40px;
  width: 260px;
  max-width: 72vw;
}

.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 10px;
  border-top: 1px solid var(--border);
  background: var(--bg-2);
}
#text-input {
  flex: 1;
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  font-family: inherit;
  resize: none;
  max-height: 140px;
}
.stop-btn {
  background: var(--danger);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
}
#mic-btn.recording {
  color: #fff;
  background: var(--danger);
  border-radius: 50%;
  animation: mic-pulse 1s infinite ease-in-out;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(217, 83, 79, 0.6); }
  50% { box-shadow: 0 0 0 8px rgba(217, 83, 79, 0); }
}

.mic-indicator {
  padding: 4px 12px;
  font-size: 12px;
  color: var(--danger);
}

/* Sidebar settings */
.sidebar-settings {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 15px;
  gap: 8px;
}
.settings-row select {
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 15px;
}
.sidebar-action {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 6px;
}
.sidebar-action:hover { background: var(--bg); }

/* Help modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-box {
  width: min(400px, 90vw);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  max-height: 80vh;
  overflow-y: auto;
}
.modal-box h2 { margin-top: 0; color: var(--accent); font-size: 18px; }
.modal-box p { font-size: 16px; line-height: 1.55; }

@media (max-width: 720px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }
  #hamburger-btn { display: inline-block; }
}
