:root {
  --bg: #080c16;
  --bg-light: #0e1525;
  --fg: #d4d8e0;
  --muted: #5a6070;
  --gold: #d4a843;
  --gold-bright: #f0c850;
  --silver: #9eaab8;
  --blue: #d4a843;
  --green: #6abf7b;
  --red: #c44040;
  --yellow: #d4a843;
  --purple: #8878a8;
  --cyan: #9eaab8;
  --msg-user: #1a1830;
  --msg-bot: #121828;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ── Login ── */
#login-screen, #totp-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  height: 100dvh;
}

.login-box {
  background: var(--bg-light);
  border: 1px solid rgba(92, 107, 122, 0.3);
  border-radius: 16px;
  padding: 40px;
  width: 340px;
  text-align: center;
}

.login-logo {
  font-size: 36px;
  font-weight: bold;
  color: var(--gold);
  margin-bottom: 4px;
}

.login-subtitle {
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 28px;
}

#login-form, #totp-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#login-form input, #totp-form input {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(92, 107, 122, 0.3);
  border-radius: 10px;
  padding: 14px 16px;
  color: var(--fg);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

#login-form input:focus, #totp-form input:focus {
  border-color: var(--gold);
}

#login-form input::placeholder, #totp-form input::placeholder {
  color: var(--muted);
}

#totp-form input {
  text-align: center;
  letter-spacing: 8px;
  font-size: 20px;
}

#login-btn, #totp-btn {
  background: var(--gold);
  color: var(--bg);
  border: none;
  border-radius: 10px;
  padding: 14px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
}

#login-btn:active, #totp-btn:active { opacity: 0.85; }

.error {
  color: var(--red);
  font-size: 13px;
  min-height: 20px;
}

/* ── Chat ── */
#chat-screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

#chat-header {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  background: var(--bg-light);
  border-bottom: 1px solid rgba(92, 107, 122, 0.15);
  flex-shrink: 0;
  gap: 10px;
}

#sessions-btn, #tasks-btn, #new-session-btn, #logout-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
}

#sessions-btn:active, #tasks-btn:active, #new-session-btn:active, #logout-btn:active {
  color: var(--gold);
}

.header-title {
  flex: 1;
  color: var(--gold);
  font-size: 16px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  margin-right: 4px;
}

.status-dot.disconnected { background: var(--red); }

/* ── Messages ── */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.message {
  max-width: 88%;
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user {
  align-self: flex-end;
  background: var(--msg-user);
  border-bottom-right-radius: 4px;
}

.message.assistant {
  align-self: flex-start;
  background: var(--msg-bot);
  border-bottom-left-radius: 4px;
}

.message pre {
  background: rgba(0, 0, 0, 0.3);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  font-family: 'Cascadia Code', 'Fira Code', monospace;
  font-size: 13px;
  margin: 8px 0;
}

.message code {
  background: rgba(0, 0, 0, 0.3);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Cascadia Code', 'Fira Code', monospace;
  font-size: 13px;
}

.message pre code {
  background: none;
  padding: 0;
}

.message p { margin: 4px 0; }
.message ul, .message ol { padding-left: 20px; margin: 4px 0; }
.message h1, .message h2, .message h3 { margin: 8px 0 4px; color: var(--gold); }
.message strong { color: var(--gold-bright); }
.message a { color: var(--gold); }
.message table { border-collapse: collapse; margin: 8px 0; width: 100%; font-size: 13px; }
.message th, .message td { border: 1px solid var(--muted); padding: 6px 8px; text-align: left; }
.message th { background: rgba(212, 168, 67, 0.1); }

.message-time {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

/* ── Typing ── */
#typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  color: var(--muted);
  font-size: 13px;
}

.typing-dots span {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--gold);
  animation: typing 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* ── Input bar ── */
#input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--bg-light);
  border-top: 1px solid rgba(92, 107, 122, 0.15);
  flex-shrink: 0;
}

#message-input {
  flex: 1;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(92, 107, 122, 0.3);
  border-radius: 20px;
  padding: 10px 16px;
  color: var(--fg);
  font-family: inherit;
  font-size: 16px;
  line-height: 1.4;
  outline: none;
  resize: none;
  max-height: 120px;
  overflow-y: auto;
}

#message-input:focus {
  border-color: var(--gold);
}

#message-input::placeholder {
  color: var(--muted);
}

#send-btn {
  background: var(--gold);
  border: none;
  border-radius: 50%;
  width: 42px;
  height: 42px;
  font-size: 20px;
  color: var(--bg);
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#send-btn:active { opacity: 0.85; }

/* ── Sessions panel ── */
#sessions-panel {
  position: absolute;
  top: 0;
  left: 0;
  width: 280px;
  height: 100%;
  background: var(--bg-light);
  border-right: 1px solid rgba(92, 107, 122, 0.2);
  z-index: 100;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sessions-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(92, 107, 122, 0.15);
  font-weight: 600;
  color: var(--gold);
}

#close-sessions {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
}

#sessions-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.session-item {
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.15s;
}

.session-item:active, .session-item.active {
  background: rgba(212, 168, 67, 0.15);
}

.session-item .session-preview {
  font-size: 14px;
  color: var(--fg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item .session-meta {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

/* ── Streaming ── */
.message.streaming {
  border-left: 2px solid var(--gold);
}

/* ── Activity zone (tools en cours) ── */
.activity-zone {
  background: rgba(212, 168, 67, 0.05);
  border: 1px solid rgba(212, 168, 67, 0.15);
  border-radius: 12px;
  padding: 10px 14px;
  margin: 4px 0;
  max-width: 88%;
  align-self: flex-start;
  transition: opacity 0.3s;
}

.activity-zone.collapsed {
  opacity: 0.5;
}

.activity-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--gold);
  margin-bottom: 6px;
}

/* Spinner rotatif */
.activity-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(212, 168, 67, 0.3);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.activity-tools {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tool-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.tool-item.running {
  color: var(--fg);
}

.tool-item.done {
  opacity: 0.6;
}

.tool-item .tool-icon {
  font-size: 13px;
  flex-shrink: 0;
}

.tool-item strong {
  color: var(--gold-bright);
  font-size: 12px;
  flex-shrink: 0;
}

.tool-item code {
  background: rgba(0, 0, 0, 0.3);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 55vw;
}

/* Spinner dans chaque tool */
.tool-spinner {
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 1.5px solid rgba(212, 168, 67, 0.3);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

.tool-check {
  color: var(--green);
  font-size: 12px;
  flex-shrink: 0;
}

/* ── Tasks panel ── */
#tasks-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 320px;
  height: 100%;
  background: var(--bg-light);
  border-left: 1px solid rgba(92, 107, 122, 0.2);
  z-index: 100;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.task-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.task-delete {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
  opacity: 0.5;
}

.task-delete:active { opacity: 1; }

.hidden { display: none !important; }

/* ── Mobile ── */
@media (max-width: 768px) {
  .login-box { width: 88%; padding: 32px 24px; }
  #messages { padding: 12px; }
  .message { max-width: 92%; font-size: 14px; }
  #sessions-panel { width: 100%; }
}
