/* ─── Reset & base ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* VS Code Dark Modern palette */
  --bg:            #1e1e1e;
  --bg-elev-1:     #252526;
  --bg-elev-2:     #2d2d30;
  --bg-input:      #3c3c3c;
  --bg-titlebar:   #3c3c3c;
  --bg-statusbar:  #007acc;
  --bg-sidebar:    #252526;
  --border:        #2d2d30;
  --border-light:  #3c3c3c;
  --text:          #cccccc;
  --text-bright:   #ffffff;
  --text-muted:    #858585;
  --text-faint:    #6b6b6b;
  --accent:        #4ec9b0;     /* claude / cyan */
  --accent-blue:   #007acc;     /* vscode blue */
  --accent-blue-h: #1f8ad2;
  --selection:     #094771;
  --warning:       #dcdcaa;
  --error:         #f48771;
  --success:       #4ec9b0;
  --user-bubble:   #094771;
  --assistant-bg:  #252526;
  --code-bg:       #1e1e1e;
  --tool-bg:       #2d2d30;

  --font-ui:   -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;

  --titlebar-h:    44px;
  --statusbar-h:   24px;
  --inputbar-h:    62px;
  --sidebar-w:     280px;

  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

html, body {
  height: 100%;
  width:  100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  overflow: hidden;
}

button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}

input, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  outline: none;
}

textarea { resize: none; }

.hidden { display: none !important; }

.screen { position: fixed; inset: 0; }

/* ─── Login screen ─────────────────────────────────────────────────────── */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: max(24px, var(--safe-top)) 24px max(24px, var(--safe-bottom));
}

.login-card {
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.login-logo { display: flex; justify-content: center; margin-bottom: 16px; }

.login-card h1 {
  font-size: 28px;
  font-weight: 600;
  color: var(--text-bright);
  letter-spacing: -0.5px;
}

.login-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin: 4px 0 28px;
  font-family: var(--font-mono);
}

.login-card form { display: flex; flex-direction: column; gap: 10px; }

.login-card input {
  width: 100%;
  padding: 14px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  font-size: 15px;
  font-family: var(--font-mono);
  text-align: center;
  letter-spacing: 0.5px;
}

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

.login-card button {
  width: 100%;
  padding: 14px;
  background: var(--accent-blue);
  color: white;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  transition: background 0.15s;
}

.login-card button:hover,
.login-card button:active { background: var(--accent-blue-h); }

.error-text {
  color: var(--error);
  font-size: 13px;
  margin-top: 12px;
  min-height: 18px;
}

.login-hint {
  color: var(--text-faint);
  font-size: 12px;
  margin-top: 18px;
  font-family: var(--font-mono);
}

/* ─── App layout ───────────────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-rows:
    calc(var(--titlebar-h) + var(--safe-top))
    1fr
    var(--inputbar-h)
    calc(var(--statusbar-h) + var(--safe-bottom));
  grid-template-areas:
    "title"
    "main"
    "input"
    "status";
  background: var(--bg);
}

/* ─── Title bar ────────────────────────────────────────────────────────── */
.titlebar {
  grid-area: title;
  display: flex;
  align-items: center;
  background: var(--bg-titlebar);
  border-bottom: 1px solid var(--border);
  padding: var(--safe-top) 8px 0 8px;
  -webkit-user-select: none;
  user-select: none;
  -webkit-app-region: drag;
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--text);
  -webkit-app-region: no-drag;
  flex-shrink: 0;
}

.icon-btn:hover,
.icon-btn:active { background: rgba(255, 255, 255, 0.08); }

.titlebar-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
  font-family: var(--font-mono);
  min-width: 0;
}

.titlebar-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 6px var(--accent);
  flex-shrink: 0;
}

.titlebar-cwd {
  color: var(--text-muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 50%;
}

/* ─── Sidebar ──────────────────────────────────────────────────────────── */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 50;
  animation: fadeIn 0.2s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  max-width: 85vw;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  z-index: 51;
  display: flex;
  flex-direction: column;
  padding: max(16px, var(--safe-top)) 0 max(16px, var(--safe-bottom));
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar:not(.hidden) { transform: translateX(0); }

.sidebar-section { padding: 16px 16px 8px; }

.sidebar-section.sidebar-bottom {
  margin-top: auto;
  border-top: 1px solid var(--border);
  padding-top: 16px;
}

.sidebar-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.6px;
  text-transform: uppercase;
  margin-bottom: 10px;
}

.mini-btn {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 14px;
}
.mini-btn:hover { background: rgba(255, 255, 255, 0.06); color: var(--text); }

.sidebar-input-wrap input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 12px;
}
.sidebar-input-wrap input:focus { border-color: var(--accent-blue); }

.cwd-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.preset-btn {
  padding: 4px 10px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border-light);
  border-radius: 12px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  transition: all 0.15s;
}
.preset-btn:hover { background: var(--bg-input); color: var(--text); }
.preset-btn.active { background: var(--selection); color: var(--text-bright); border-color: var(--accent-blue); }

.model-toggle {
  display: flex;
  background: var(--bg-elev-2);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  padding: 2px;
}

.model-btn {
  flex: 1;
  padding: 6px 8px;
  font-size: 11px;
  font-family: var(--font-mono);
  color: var(--text-muted);
  border-radius: 4px;
  transition: all 0.15s;
}
.model-btn.active { background: var(--accent-blue); color: white; }

.threads-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 40vh;
  overflow-y: auto;
}

.threads-empty {
  color: var(--text-faint);
  font-size: 12px;
  padding: 8px 0;
  font-style: italic;
}

.thread-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
  transition: background 0.1s;
}
.thread-item:hover { background: rgba(255, 255, 255, 0.05); color: var(--text); }
.thread-item.active { background: var(--selection); color: var(--text-bright); }

.thread-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-delete {
  opacity: 0;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  color: var(--text-muted);
  font-size: 14px;
  flex-shrink: 0;
}
.thread-item:hover .thread-delete { opacity: 1; }
.thread-delete:hover { background: rgba(255, 255, 255, 0.1); color: var(--error); }

.logout-btn {
  width: 100%;
  padding: 10px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 12px;
  transition: all 0.15s;
}
.logout-btn:hover { color: var(--error); border-color: var(--error); }

/* ─── Main / messages area ─────────────────────────────────────────────── */
.main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

.messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 14px 24px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 32px 16px;
  color: var(--text-muted);
}

.welcome-icon { margin-bottom: 16px; opacity: 0.8; }

.welcome h2 {
  font-size: 22px;
  font-weight: 500;
  color: var(--text-bright);
  margin-bottom: 8px;
}

.welcome p { max-width: 320px; line-height: 1.6; }

.welcome-suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 28px;
  width: 100%;
  max-width: 320px;
}

.suggestion {
  padding: 12px 16px;
  background: var(--bg-elev-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  text-align: left;
  font-size: 13px;
  color: var(--text);
  transition: all 0.15s;
}
.suggestion:hover {
  background: var(--bg-elev-2);
  border-color: var(--accent-blue);
}

/* ─── Message bubbles ──────────────────────────────────────────────────── */
.message {
  display: flex;
  margin-bottom: 14px;
  animation: slideIn 0.2s ease-out;
}
@keyframes slideIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.message-user { justify-content: flex-end; }
.message-assistant { justify-content: flex-start; }

.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.message-user .bubble {
  background: var(--user-bubble);
  color: var(--text-bright);
  border-bottom-right-radius: 4px;
}

.message-assistant .bubble {
  background: var(--assistant-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  max-width: 92%;
}

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }

.bubble code {
  font-family: var(--font-mono);
  font-size: 12.5px;
  background: rgba(0, 0, 0, 0.3);
  padding: 1px 6px;
  border-radius: 3px;
  color: var(--accent);
}

.bubble pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
  -webkit-overflow-scrolling: touch;
}

.bubble pre code {
  background: transparent;
  padding: 0;
  color: var(--text);
  font-size: 12px;
  line-height: 1.5;
}

.bubble ul, .bubble ol { margin: 8px 0 8px 20px; }
.bubble li { margin: 2px 0; }

.bubble h1, .bubble h2, .bubble h3 {
  margin: 12px 0 6px;
  color: var(--text-bright);
  font-weight: 600;
}
.bubble h1 { font-size: 18px; }
.bubble h2 { font-size: 16px; }
.bubble h3 { font-size: 14px; }

.bubble a { color: var(--accent-blue); text-decoration: underline; }

.bubble blockquote {
  border-left: 3px solid var(--accent-blue);
  padding-left: 10px;
  color: var(--text-muted);
  margin: 8px 0;
}

/* ─── Tool call cards ──────────────────────────────────────────────────── */
.tool-card {
  margin: 8px 0;
  background: var(--tool-bg);
  border: 1px solid var(--border-light);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  overflow: hidden;
}

.tool-card-header {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0, 0, 0, 0.2);
}

.tool-card-icon {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  color: var(--accent);
}

.tool-card-name {
  color: var(--text);
  font-weight: 500;
}

.tool-card-target {
  color: var(--text-muted);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.tool-card-status {
  margin-left: auto;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  background: var(--bg);
  color: var(--text-muted);
}
.tool-card-status.running { color: var(--warning); }
.tool-card-status.success { color: var(--success); }
.tool-card-status.error { color: var(--error); }

.tool-card-body {
  padding: 8px 12px;
  color: var(--text-muted);
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
  display: none;
}

.tool-card.expanded .tool-card-body { display: block; }

/* ─── Streaming indicator ──────────────────────────────────────────────── */
.cursor-blink {
  display: inline-block;
  width: 7px;
  height: 14px;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.thinking-indicator {
  display: inline-flex;
  gap: 3px;
  padding: 4px 0;
}
.thinking-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: thinking 1.4s infinite ease-in-out both;
}
.thinking-indicator span:nth-child(1) { animation-delay: -0.32s; }
.thinking-indicator span:nth-child(2) { animation-delay: -0.16s; }
@keyframes thinking {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* ─── Tool panel (terminal-like) ───────────────────────────────────────── */
.tool-panel {
  border-top: 1px solid var(--border);
  background: var(--bg-elev-1);
  flex-shrink: 0;
  max-height: 40vh;
  display: flex;
  flex-direction: column;
}

.tool-panel.collapsed { max-height: 32px; }

.tool-panel-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.6px;
  cursor: pointer;
  user-select: none;
  flex-shrink: 0;
  height: 32px;
}

.chevron {
  font-size: 8px;
  transition: transform 0.2s;
}
.tool-panel:not(.collapsed) .chevron { transform: rotate(90deg); }

.tool-badge {
  margin-left: auto;
  padding: 1px 7px;
  background: var(--accent-blue);
  color: white;
  border-radius: 9px;
  font-size: 10px;
  min-width: 18px;
  text-align: center;
  display: none;
}
.tool-badge.visible { display: inline-block; }

.tool-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 12px 12px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}

.tool-panel.collapsed .tool-panel-body { display: none; }

.tool-log-entry {
  padding: 4px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}
.tool-log-entry:last-child { border-bottom: none; }
.tool-log-entry .ts { color: var(--text-faint); margin-right: 8px; }
.tool-log-entry .label { color: var(--accent); margin-right: 8px; }

/* ─── Input bar ────────────────────────────────────────────────────────── */
.input-bar {
  grid-area: input;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 10px 10px;
  background: var(--bg-elev-1);
  border-top: 1px solid var(--border);
  padding-left: max(10px, var(--safe-left));
  padding-right: max(10px, var(--safe-right));
}

.input-wrap {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  border-radius: 18px;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  min-height: 42px;
  transition: border-color 0.15s;
}
.input-wrap:focus-within { border-color: var(--accent-blue); }

#input {
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  max-height: 120px;
  overflow-y: auto;
}

#input::placeholder { color: var(--text-faint); }

.action-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text);
  transition: all 0.15s;
}

.mic-btn { background: var(--bg-elev-2); border: 1px solid var(--border-light); }
.mic-btn:hover { background: var(--bg-input); }
.mic-btn.recording {
  background: var(--error);
  border-color: var(--error);
  color: white;
  animation: pulse 1.2s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(244, 135, 113, 0.6); }
  50%      { box-shadow: 0 0 0 10px rgba(244, 135, 113, 0); }
}

.send-btn { background: var(--accent-blue); color: white; }
.send-btn:hover { background: var(--accent-blue-h); }
.send-btn:disabled { background: var(--bg-elev-2); color: var(--text-faint); cursor: not-allowed; }

/* ─── Status bar ───────────────────────────────────────────────────────── */
.statusbar {
  grid-area: status;
  background: var(--bg-statusbar);
  color: white;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
  padding-bottom: var(--safe-bottom);
  font-size: 11px;
  font-family: var(--font-mono);
  height: calc(var(--statusbar-h) + var(--safe-bottom));
  overflow-x: auto;
  white-space: nowrap;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #888;
  display: inline-block;
}
.conn-dot.connected { background: #4ec9b0; box-shadow: 0 0 4px #4ec9b0; }
.conn-dot.error     { background: #f48771; }

/* ─── Cancel button ────────────────────────────────────────────────────── */
.cancel-btn {
  position: fixed;
  bottom: calc(var(--inputbar-h) + var(--statusbar-h) + var(--safe-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border-light);
  border-radius: 18px;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  z-index: 40;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.cancel-btn:hover { border-color: var(--error); color: var(--error); }

.spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--text-faint);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Tablet / desktop ─────────────────────────────────────────────────── */
@media (min-width: 768px) {
  .sidebar { transform: translateX(0); position: relative; }
  .sidebar-overlay { display: none !important; }

  #app {
    grid-template-columns: var(--sidebar-w) 1fr;
    grid-template-rows:
      calc(var(--titlebar-h) + var(--safe-top))
      1fr
      var(--inputbar-h)
      calc(var(--statusbar-h) + var(--safe-bottom));
    grid-template-areas:
      "title  title"
      "side   main"
      "side   input"
      "status status";
  }

  .sidebar { grid-area: side; }

  #menu-btn { display: none; }

  .bubble { max-width: 720px; }
}

/* ─── Highlight.js override for VS Code Dark Modern feel ──────────────── */
.hljs { background: transparent !important; padding: 0 !important; }

/* Scrollbars */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-input); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }
