@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* ── Update banner ────────────────────────── */
#update-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: #10A37F;
  color: #fff;
  font-family: Inter, system-ui, sans-serif;
  font-size: 14px;
  font-weight: 500;
  animation: slideDown 0.3s ease;
}
#update-banner button {
  padding: 4px 14px;
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 6px;
  background: rgba(255,255,255,0.15);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}
#update-banner button:hover {
  background: rgba(255,255,255,0.3);
}
#update-banner button.dismiss {
  margin-left: auto;
  border: none;
  background: none;
  font-size: 16px;
  padding: 4px 8px;
}
@keyframes slideDown {
  from { transform: translateY(-100%); }
  to { transform: translateY(0); }
}

/* ── Activity indicator ────────────────────── */
.activity-indicator {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 0;
  color: var(--color-text-secondary, #A0A0A0);
  font-size: 13px;
}
.activity-dots {
  display: flex;
  gap: 4px;
}
.activity-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-accent, #10A37F);
  animation: dotPulse 1.4s ease-in-out infinite;
}
.activity-dots span:nth-child(2) { animation-delay: 0.2s; }
.activity-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dotPulse {
  0%, 80%, 100% { opacity: 0.2; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.2); }
}
.activity-label {
  color: var(--color-text-secondary, #A0A0A0);
}
.activity-time {
  color: var(--color-text-tertiary, #666);
  font-size: 11px;
}

:root {
  /* ── Backgrounds ─────────────────────────── */
  --color-bg-base:            #0D0D0D;
  --color-bg-primary:         #1A1A1A;
  --color-bg-secondary:       #141414;
  --color-bg-tertiary:        #242424;
  --color-bg-hover:           #2A2A2A;
  --color-bg-active:          #333333;

  /* ── Chat Surfaces ──────────────────────── */
  --color-surface-user:       #2A2D35;
  --color-surface-ai:         transparent;
  --color-surface-code:       #0D1117;
  --color-surface-input:      #242424;
  --color-surface-input-focus:#2A2A2A;
  --color-surface-tool:       #1E2328;

  /* ── Text Hierarchy ─────────────────────── */
  --color-text-primary:       #ECECEC;
  --color-text-secondary:     #A0A0A0;
  --color-text-tertiary:      #6B6B6B;
  --color-text-inverse:       #1A1A1A;
  --color-text-link:          #60A5FA;
  --color-text-code:          #E6DB74;

  /* ── Accent ─────────────────────────────── */
  --color-accent-primary:     #10A37F;
  --color-accent-primary-hover:#0E8C6B;
  --color-accent-primary-active:#0C7A5E;
  --color-accent-secondary:   #7C6AEF;

  /* ── Borders ────────────────────────────── */
  --color-border-subtle:      #2A2A2A;
  --color-border-default:     #333333;
  --color-border-strong:      #444444;
  --color-border-focus:       #10A37F;

  /* ── Semantic ───────────────────────────── */
  --color-success:            #34D399;
  --color-error:              #F87171;
  --color-warning:            #FBBF24;
  --color-info:               #60A5FA;

  /* ── Scrollbar ──────────────────────────── */
  --color-scrollbar-thumb:    #333333;
  --color-scrollbar-thumb-hover:#444444;

  /* ── Radius ─────────────────────────────── */
  --radius:     12px;
  --radius-sm:  8px;
  --radius-pill: 24px;

  /* ── Safe Areas ─────────────────────────── */
  --sat: env(safe-area-inset-top);
  --sar: env(safe-area-inset-right);
  --sab: env(safe-area-inset-bottom);
  --sal: env(safe-area-inset-left);

  /* ── Keyboard ───────────────────────────── */
  --keyboard-height: 0px;
}

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

html, body {
  height: 100%;
  background: var(--color-bg-base);
  color: var(--color-text-primary);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ── Layout ───────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  background: var(--color-bg-primary);
}

/* ── Top Bar ──────────────────────────────────────────── */
#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--sat)) 16px 10px;
  background: var(--color-bg-secondary);
  border-bottom: 1px solid var(--color-border-subtle);
  min-height: 52px;
}

#topbar h1 {
  font-size: 16px;
  font-weight: 700;
  color: var(--color-accent-primary);
  flex: 1;
}

#status-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--color-text-tertiary);
  padding: 4px 10px;
  background: var(--color-bg-tertiary);
  border-radius: 20px;
  border: 1px solid var(--color-border-subtle);
  transition: all 0.3s;
}

#status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--color-text-tertiary);
  transition: background 0.3s;
  flex-shrink: 0;
}

#status-indicator.connected { border-color: rgba(52,211,153,0.3); }
#status-indicator.connected #status-dot { background: var(--color-success); box-shadow: 0 0 6px var(--color-success); animation: pulse 2s infinite; }
#status-indicator.streaming { border-color: rgba(16,163,127,0.4); color: var(--color-accent-primary); }
#status-indicator.streaming #status-dot { background: var(--color-accent-primary); box-shadow: 0 0 8px var(--color-accent-primary); animation: pulse 0.8s infinite; }
#status-indicator.disconnected { border-color: rgba(248,113,113,0.3); color: var(--color-error); }
#status-indicator.disconnected #status-dot { background: var(--color-error); }
#status-indicator.error { border-color: rgba(251,191,36,0.3); color: var(--color-warning); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

#btn-settings {
  background: none;
  border: 1px solid var(--color-border-subtle);
  color: var(--color-text-tertiary);
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, color 0.15s;
}
#btn-settings:hover { border-color: var(--color-accent-primary); color: var(--color-accent-primary); }

/* ── Chat Area ────────────────────────────────────────── */
#chat-wrap {
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#chat {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

#chat::-webkit-scrollbar { width: 5px; }
#chat::-webkit-scrollbar-track { background: transparent; }
#chat::-webkit-scrollbar-thumb { background: var(--color-scrollbar-thumb); border-radius: 3px; }
#chat::-webkit-scrollbar-thumb:hover { background: var(--color-scrollbar-thumb-hover); }

/* ── Messages ─────────────────────────────────────────── */
.msg {
  position: relative;
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.7;
  animation: msgIn 0.15s ease-out;
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── User Messages ────────────────────────────────────── */
.msg-user {
  align-self: flex-end;
  background: var(--color-surface-user);
  color: var(--color-text-primary);
  max-width: 85%;
  border-radius: 18px 18px 4px 18px;
  border: 1px solid var(--color-border-subtle);
}

/* ── AI Messages (flat card — no bubble) ──────────────── */
.msg-ai {
  align-self: flex-start;
  background: var(--color-surface-ai);
  border: none;
  color: var(--color-text-primary);
  width: 100%;
  max-width: 100%;
  padding: 12px 16px;
}

/* ── Message Labels ───────────────────────────────────── */
.msg-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
  color: var(--color-text-tertiary);
}

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

/* ── Message Content ──────────────────────────────────── */
.msg-content {
  word-break: break-word;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.7;
  color: var(--color-text-primary);
}

.msg-user .msg-content {
  white-space: pre-wrap;
}

/* ── Message Copy Button ──────────────────────────────── */
.msg-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  opacity: 0;
  background: var(--color-bg-tertiary);
  border: 1px solid var(--color-border-subtle);
  color: var(--color-text-secondary);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 11px;
  cursor: pointer;
  transition: opacity 0.15s;
  font-family: inherit;
}
.msg:hover .msg-copy,
.msg-copy:focus { opacity: 1; }

/* ── AI Markdown Content ──────────────────────────────── */
.msg-ai .msg-content h1,
.msg-ai .msg-content h2,
.msg-ai .msg-content h3,
.msg-ai .msg-content h4 {
  color: var(--color-text-primary);
  margin: 16px 0 8px;
  font-weight: 700;
}
.msg-ai .msg-content h1 { font-size: 20px; }
.msg-ai .msg-content h2 { font-size: 17px; }
.msg-ai .msg-content h3 { font-size: 15px; }
.msg-ai .msg-content h4 { font-size: 14px; }

.msg-ai .msg-content p { margin: 8px 0; }
.msg-ai .msg-content p:first-child { margin-top: 0; }
.msg-ai .msg-content p:last-child { margin-bottom: 0; }

.msg-ai .msg-content ul,
.msg-ai .msg-content ol { margin: 8px 0; padding-left: 20px; }
.msg-ai .msg-content li { margin: 4px 0; }

.msg-ai .msg-content a { color: var(--color-text-link); text-decoration: none; }
.msg-ai .msg-content a:hover { text-decoration: underline; }

.msg-ai .msg-content strong { color: var(--color-text-primary); font-weight: 600; }
.msg-ai .msg-content em { font-style: italic; }

.msg-ai .msg-content blockquote {
  border-left: 3px solid var(--color-accent-primary);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--color-text-secondary);
}

.msg-ai .msg-content hr {
  border: none;
  border-top: 1px solid var(--color-border-subtle);
  margin: 16px 0;
}

.msg-ai .msg-content table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
  font-size: 14px;
}
.msg-ai .msg-content th,
.msg-ai .msg-content td {
  border: 1px solid var(--color-border-default);
  padding: 6px 10px;
  text-align: left;
}
.msg-ai .msg-content th {
  background: var(--color-bg-tertiary);
  font-weight: 600;
}

/* ── Inline Code ──────────────────────────────────────── */
.msg-ai .msg-content code:not(.hljs):not([class*="language-"]) {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--color-border-subtle);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: 'JetBrains Mono', 'Consolas', monospace;
  font-size: 13px;
  color: var(--color-text-code);
}

/* ── Code Blocks ──────────────────────────────────────── */
.code-block {
  position: relative;
  margin: 12px 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--color-surface-code);
  border: 1px solid var(--color-border-subtle);
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: rgba(255,255,255,0.03);
  border-bottom: 1px solid var(--color-border-subtle);
  font-size: 12px;
}

.code-lang {
  color: var(--color-text-tertiary);
  text-transform: lowercase;
}

.copy-btn {
  background: none;
  border: 1px solid var(--color-border-subtle);
  color: var(--color-text-secondary);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 11px;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}
.copy-btn:hover { border-color: var(--color-accent-primary); color: var(--color-accent-primary); }
.copy-btn.copied { border-color: var(--color-success); color: var(--color-success); }

.code-block pre {
  margin: 0;
  padding: 12px;
  overflow-x: auto;
}

.code-block code {
  font-family: 'JetBrains Mono', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.5;
  color: var(--color-text-primary);
}

/* ── Tool Steps (collapsible) ─────────────────────────── */
.tool-step {
  margin: 8px 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--color-border-subtle);
  background: var(--color-surface-tool);
}

.tool-step-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: none;
  border: none;
  width: 100%;
  color: var(--color-text-secondary);
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
}
.tool-step-header:hover { background: rgba(255,255,255,0.02); }

.tool-icon { font-size: 14px; flex-shrink: 0; }
.tool-label { flex: 1; }

.tool-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.tool-status.running { background: var(--color-accent-primary); animation: pulse 0.8s infinite; }
.tool-status.done { background: var(--color-success); }
.tool-status.error { background: var(--color-error); }

.tool-chevron {
  font-size: 10px;
  transition: transform 0.15s;
  color: var(--color-text-tertiary);
}
.tool-step.expanded .tool-chevron { transform: rotate(90deg); }

.tool-step-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease;
}
.tool-step.expanded .tool-step-body {
  max-height: 300px;
  overflow-y: auto;
}

.tool-step-body pre {
  padding: 8px 12px;
  font-family: 'JetBrains Mono', 'Consolas', monospace;
  font-size: 12px;
  color: var(--color-text-tertiary);
  white-space: pre-wrap;
  word-break: break-all;
  margin: 0;
}

/* ── Blinking Cursor (streaming) ──────────────────────── */
.cursor {
  display: inline-block;
  width: 2px;
  height: 14px;
  background: var(--color-accent-primary);
  vertical-align: middle;
  margin-left: 2px;
  animation: blink 1s step-end infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ── System / Info Messages ───────────────────────────── */
.msg-system {
  align-self: center;
  background: rgba(248,113,113,0.08);
  border: 1px solid rgba(248,113,113,0.2);
  color: var(--color-error);
  font-size: 12px;
  padding: 6px 14px;
  border-radius: 20px;
}

.msg-info {
  align-self: center;
  color: var(--color-text-tertiary);
  font-size: 12px;
  padding: 4px 0;
}

/* ── Scroll-to-bottom Badge ───────────────────────────── */
#scroll-badge {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-accent-primary);
  color: white;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 16px;
  border-radius: 20px;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  white-space: nowrap;
  box-shadow: 0 4px 16px rgba(16,163,127,0.4);
  z-index: 10;
}
#scroll-badge.visible { opacity: 1; pointer-events: auto; }

/* ── Input Area ───────────────────────────────────────── */
#input-area {
  background: var(--color-bg-secondary);
  border-top: 1px solid var(--color-border-subtle);
  padding: 12px 16px calc(12px + var(--sab));
}

#input-row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

#prompt-input {
  flex: 1;
  background: var(--color-surface-input);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-pill);
  padding: 10px 18px;
  color: var(--color-text-primary);
  font-size: 15px;
  font-family: inherit;
  resize: none;
  min-height: 44px;
  max-height: 200px;
  outline: none;
  transition: border-color 0.15s, background-color 0.15s;
  line-height: 1.5;
}

#prompt-input::placeholder { color: var(--color-text-tertiary); }
#prompt-input:focus {
  border-color: var(--color-border-focus);
  background: var(--color-surface-input-focus);
}

#btn-send {
  width: 44px;
  height: 44px;
  background: var(--color-accent-primary);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s, transform 0.1s, background-color 0.15s;
}
#btn-send:hover { background: var(--color-accent-primary-hover); }
#btn-send:active { transform: scale(0.95); }
#btn-send:disabled { opacity: 0.3; cursor: not-allowed; }

#btn-cancel {
  width: 44px;
  height: 44px;
  background: rgba(248,113,113,0.12);
  border: 1px solid rgba(248,113,113,0.3);
  border-radius: 50%;
  color: var(--color-error);
  font-size: 18px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#btn-cancel.visible { display: flex; }
#btn-cancel:hover { background: rgba(248,113,113,0.2); }

#input-hint {
  font-size: 11px;
  color: var(--color-text-tertiary);
  margin-top: 6px;
  text-align: right;
}

/* ── Settings Panel ───────────────────────────────────── */
#settings-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
#settings-overlay.open { display: flex; }

#settings-panel {
  background: var(--color-bg-secondary);
  border: 1px solid var(--color-border-default);
  border-radius: 16px;
  padding: 28px;
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

#settings-panel h2 {
  font-size: 18px;
  font-weight: 700;
  color: var(--color-accent-primary);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--color-text-tertiary);
}

.field input {
  background: var(--color-surface-input);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--color-text-primary);
  font-size: 14px;
  font-family: 'JetBrains Mono', 'Consolas', monospace;
  outline: none;
  transition: border-color 0.15s;
}
.field input:focus { border-color: var(--color-border-focus); }
.field small { font-size: 11px; color: var(--color-text-tertiary); }

.settings-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 4px;
}

.btn-primary {
  background: var(--color-accent-primary);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s;
  font-family: inherit;
}
.btn-primary:hover { background: var(--color-accent-primary-hover); }

.btn-ghost {
  background: none;
  border: 1px solid var(--color-border-default);
  color: var(--color-text-secondary);
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: border-color 0.15s;
  font-family: inherit;
}
.btn-ghost:hover { border-color: var(--color-text-tertiary); }

/* ── Empty State ──────────────────────────────────────── */
#empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 40px 20px;
  color: var(--color-text-tertiary);
  text-align: center;
  flex: 1;
}

#empty-state .icon { font-size: 48px; opacity: 0.5; }
#empty-state h3 { font-size: 18px; color: var(--color-text-secondary); }
#empty-state p { font-size: 13px; max-width: 280px; }

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 480px) {
  #topbar { padding: calc(8px + var(--sat)) 12px 8px; }
  #chat { padding: 12px; }
  #input-area { padding: 10px 12px calc(10px + var(--sab)); }
  .msg { font-size: 14px; }
  #prompt-input { font-size: 14px; padding: 10px 14px; }
}
