:root {
  --bg: #0a0a0a;
  --bg-elevated: #0f0f0f;
  --card: #151515;
  --card-hover: #1c1c1c;
  --border: #1e1e1e;
  --border-subtle: #181818;
  --text: #e8e8e8;
  --text-secondary: #b0b0b0;
  --dim: #707070;
  --accent: #6b8afd;
  --accent-hover: #5a7bf0;
  --accent-dim: rgba(107,138,253,0.10);
  --accent-glow: rgba(107,138,253,0.06);
  --user-bg: #162d4a;
  --asst-bg: #151515;
  --red: #e74c3c;
  --green: #5cb85c;
  --yellow: #f0ad4e;
  --radius: 16px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  line-height: 1.5;
}

/* Toast */
#toast {
  position: fixed; top: 20px; left: 50%; transform: translateX(-50%) translateY(-12px);
  background: var(--card); color: var(--text); padding: 12px 24px;
  border-radius: var(--radius-sm); font-size: 13px; font-weight: 500;
  z-index: 999; opacity: 0; transition: opacity 0.3s, transform 0.3s;
  pointer-events: none; border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: var(--red); color: #f5a5a0; }

/* Login */
#login-view { display: flex; align-items: center; justify-content: center; height: 100vh; height: 100dvh; animation: fadeIn 0.6s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: translateY(0); } }
.login-box { text-align: center; padding: 24px; }
.login-box h1 { font-size: 52px; font-weight: 200; color: var(--text); margin-bottom: 8px; letter-spacing: 8px; text-transform: lowercase; text-shadow: 0 0 40px rgba(107,138,253,0.3), 0 0 80px rgba(107,138,253,0.1); }
.login-tagline { color: var(--dim); font-size: 13px; letter-spacing: 4px; text-transform: uppercase; margin-bottom: 40px; font-weight: 300; }
.login-row { display: flex; align-items: center; justify-content: center; gap: 0; }
.login-box input { background: var(--card); border: 1px solid var(--border); border-right: none; color: var(--text); padding: 14px 20px; border-radius: var(--radius) 0 0 var(--radius); font-size: 15px; width: 240px; outline: none; text-align: center; letter-spacing: 2px; transition: border-color var(--transition), box-shadow var(--transition); }
.login-box input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-dim); }
.login-box input::placeholder { color: #444; letter-spacing: 1px; }
.login-box button { background: var(--accent); border: 1px solid var(--accent); color: white; padding: 14px 20px; border-radius: 0 var(--radius) var(--radius) 0; font-size: 18px; cursor: pointer; transition: background var(--transition), transform 0.1s; line-height: 1; }
.login-box button:hover { background: var(--accent-hover); }
.login-box button:active { transform: scale(0.96); }

/* Header */
.header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--bg-elevated); min-height: 56px;
  position: relative; z-index: 10;
  backdrop-filter: blur(8px);
}
.header-title { font-size: 16px; font-weight: 600; letter-spacing: 0.3px; }
.header-right { display: flex; gap: 2px; }
.header button {
  background: none; border: none; color: var(--dim); font-size: 18px;
  cursor: pointer; padding: 8px 10px; border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
  min-width: 44px; min-height: 44px;
  display: flex; align-items: center; justify-content: center;
}
.header button:hover { background: var(--card-hover); color: var(--text); }
.header button:active { transform: scale(0.94); }

/* View transitions */
#conv-view, #chat-view, #voice-view, #goal-view {
  animation: viewIn 0.25s ease-out;
}
@keyframes viewIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

/* Conversations */
#conv-view { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
#conv-list { flex: 1; overflow-y: auto; padding: 8px 10px; -webkit-overflow-scrolling: touch; }
.conv-card {
  padding: 16px 18px; border-radius: var(--radius-sm); cursor: pointer;
  transition: background var(--transition), transform 0.1s;
  display: flex; align-items: center; margin-bottom: 2px;
  min-height: 60px;
}
.conv-card:hover { background: var(--card-hover); }
.conv-card:active { background: var(--card); transform: scale(0.985); }
.conv-info { flex: 1; min-width: 0; }
.conv-title { font-weight: 500; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.5; }
.conv-meta { color: var(--dim); font-size: 12px; margin-top: 4px; letter-spacing: 0.2px; }

/* Chat */
#chat-view { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
#chat-messages {
  flex: 1; overflow-y: auto; padding: 20px 16px; padding-bottom: 8px;
  display: flex; flex-direction: column; gap: 8px;
  -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: 88%; padding: 12px 16px; border-radius: 20px;
  font-size: 15px; line-height: 1.6; word-wrap: break-word;
  animation: msgIn 0.25s ease-out;
}
@keyframes msgIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.msg.user {
  align-self: flex-end; background: var(--user-bg);
  border-bottom-right-radius: 6px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}
.msg.assistant {
  align-self: flex-start; background: var(--asst-bg);
  border: 1px solid var(--border); border-bottom-left-radius: 6px;
}
.msg.thinking { color: var(--dim); font-style: italic; }
.msg-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.msg-role {
  font-size: 11px; color: var(--dim);
  font-weight: 600; letter-spacing: 0.5px; text-transform: uppercase;
}
.msg-copy { background: none; border: none; color: var(--dim); cursor: pointer; font-size: 14px; padding: 0 2px; opacity: 0.4; transition: opacity 0.15s; }
.msg-copy:hover { opacity: 1; }
@keyframes blink { 0%,80%,100% { opacity: 0; } 40% { opacity: 1; } }
.msg a { color: var(--accent); text-decoration: none; }
.msg a:hover { text-decoration: underline; }
.msg code { background: #1a1a1a; padding: 2px 7px; border-radius: 5px; font-size: 13px; font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace; border: 1px solid var(--border-subtle); }
.msg pre { background: #0d0d0d; padding: 14px 16px; border-radius: var(--radius-sm); overflow-x: auto; margin: 10px 0; border: 1px solid var(--border); }
.msg pre code { background: none; padding: 0; font-size: 13px; border: none; }
.md-table-wrap { overflow-x: auto; margin: 10px 0; }
.md-table-wrap table { border-collapse: collapse; font-size: 13px; width: 100%; }
.md-table-wrap th, .md-table-wrap td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; white-space: nowrap; }
.md-table-wrap th { background: #1a1a1a; font-weight: 600; }
.md-table-wrap tr:nth-child(even) { background: rgba(255,255,255,0.02); }
.msg h2, .msg h3, .msg h4 { margin: 14px 0 6px; color: #f0f0f0; font-weight: 600; }
.msg h2 { font-size: 1.2em; }
.msg h3 { font-size: 1.1em; }
.msg h4 { font-size: 1em; }
.msg hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.msg ul, .msg ol { padding-left: 20px; margin: 6px 0; }
.msg li { margin: 3px 0; }
.msg strong { color: #f0f0f0; }

/* Input */
.chat-input {
  display: flex; align-items: flex-end; padding: 12px 12px;
  border-top: 1px solid var(--border); background: var(--bg-elevated);
  padding-bottom: max(12px, env(safe-area-inset-bottom)); gap: 8px;
}
.chat-input textarea {
  flex: 1; background: var(--card); border: 1px solid var(--border);
  color: var(--text); padding: 12px 18px; border-radius: 24px;
  font-size: 16px; font-family: inherit; resize: none;
  max-height: 120px; outline: none; line-height: 1.45;
  overflow-y: auto; scrollbar-width: none; -ms-overflow-style: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.chat-input textarea::-webkit-scrollbar { display: none; }
.chat-input textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-dim); }
.chat-input textarea::placeholder { color: #4a4a4a; }
.chat-input button {
  background: var(--card); border: 1px solid var(--border); color: var(--dim);
  border-radius: 50%; width: 44px; height: 44px; font-size: 18px;
  cursor: pointer; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  transition: background var(--transition), transform 0.1s, border-color var(--transition), color var(--transition);
}
.chat-input button:hover { background: var(--card-hover); color: var(--text); }
.chat-input button:active { transform: scale(0.92); }
#chat-send { background: var(--accent); color: white; border: none; }
#chat-send:hover { background: var(--accent-hover); }

/* Pending files */
#pending-files { padding: 8px 16px; font-size: 12px; color: var(--dim); display: flex; flex-wrap: wrap; gap: 8px; }
.file-chip { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-xs); padding: 6px 12px; display: inline-flex; align-items: center; gap: 8px; font-size: 12px; }
.file-chip button { background: none; border: none; color: var(--red); cursor: pointer; font-size: 14px; padding: 0 2px; }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 2px; }

/* Work toggle (after done) */
.work-toggle { margin-bottom: 10px; border-radius: var(--radius-sm); background: #0c0c0c; border: 1px solid var(--border); font-size: 12px; }
.work-toggle summary { padding: 8px 14px; cursor: pointer; color: var(--dim); list-style: none; font-style: italic; transition: color var(--transition); }
.work-toggle summary:hover { color: var(--text-secondary); }
.work-toggle summary::-webkit-details-marker { display: none; }
.work-toggle summary::before { content: '▶ '; font-size: 9px; }
.work-toggle[open] summary::before { content: '▼ '; }
.work-content { padding: 10px 14px; border-top: 1px solid var(--border); max-height: 400px; overflow-y: auto; color: var(--dim); font-size: 12px; line-height: 1.6; }

/* Voice chat */
#voice-view { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
#voice-messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 10px; -webkit-overflow-scrolling: touch; }
.voice-controls { display: flex; flex-direction: column; align-items: center; padding: 28px 16px; padding-bottom: max(28px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--bg-elevated); }
#voice-ptt {
  width: 80px; height: 80px; border-radius: 50%; background: var(--card);
  border: 2px solid var(--border); color: var(--text); font-size: 32px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all var(--transition); user-select: none; -webkit-user-select: none;
}
#voice-ptt:active, #voice-ptt.recording { background: var(--red); border-color: var(--red); transform: scale(1.1); box-shadow: 0 0 24px rgba(231,76,60,0.3); }
#voice-ptt-label { color: var(--dim); font-size: 12px; margin-top: 10px; letter-spacing: 0.3px; }
.voice-msg { max-width: 85%; padding: 12px 16px; border-radius: 20px; font-size: 15px; line-height: 1.6; word-wrap: break-word; }
.voice-msg.user { align-self: flex-end; background: var(--user-bg); border-bottom-right-radius: 6px; }
.voice-msg.assistant { align-self: flex-start; background: var(--asst-bg); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.voice-msg.assistant.speaking { border-color: var(--accent); box-shadow: 0 0 16px rgba(107,138,253,0.2); }

/* Notification dot */
.notif-dot { display: inline-block; width: 8px; height: 8px; background: var(--accent); border-radius: 50%; margin-right: 8px; vertical-align: middle; box-shadow: 0 0 8px rgba(107,138,253,0.5); }
.working-dot { display: inline-block; width: 8px; height: 8px; background: var(--accent); border-radius: 50%; margin-right: 8px; vertical-align: middle; animation: blink 1.4s infinite both; }
.goal-dot { display: inline-block; width: 8px; height: 8px; background: #e63946; border-radius: 50%; margin-right: 8px; vertical-align: middle; animation: blink 1.4s infinite both; }

/* File panel */
#files-panel { border-bottom: 1px solid var(--border); max-height: 150px; overflow-y: auto; }
.file-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; text-decoration: none; color: var(--text); font-size: 13px; border-bottom: 1px solid var(--border-subtle); transition: background var(--transition); min-height: 44px; }
.file-row:hover { background: var(--card); }
.file-row:last-child { border-bottom: none; }

/* Queue indicator */
#queue-indicator { padding: 8px 16px; font-size: 11px; color: var(--accent); border-top: 1px solid var(--border); font-weight: 600; letter-spacing: 0.3px; }

/* Queued message */
.msg.user.queued { opacity: 0.5; border: 1px dashed var(--dim); }

/* Goals */
.list-section { padding: 18px 18px 8px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: var(--dim); }
.list-section.goals { color: var(--accent); }
.goal-card {
  padding: 14px 16px; border-radius: var(--radius-sm); cursor: pointer;
  transition: background var(--transition); display: flex; align-items: center;
  gap: 14px; margin: 3px 10px; min-height: 56px;
}
.goal-card:hover { background: var(--card-hover); }
.goal-card:active { background: var(--card); }
.goal-badge {
  font-size: 16px; flex-shrink: 0; width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: var(--card); border-radius: 50%; border: 1px solid var(--border);
}
.goal-info { flex: 1; min-width: 0; }
.goal-task { color: var(--text); font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.4; }
.goal-meta { color: var(--dim); font-size: 11px; margin-top: 4px; display: flex; align-items: center; gap: 6px; }
.goal-status-running { color: var(--accent); font-weight: 600; }
.goal-status-waiting { color: var(--yellow); font-weight: 600; }
.goal-status-completed { color: var(--green); font-weight: 600; }
.goal-status-cancelled { color: var(--red); font-weight: 600; }
.goal-question { margin: 10px 10px 6px 16px; padding: 10px 14px; background: var(--accent-dim); border-radius: var(--radius-sm); font-size: 13px; color: var(--text-secondary); border-left: 3px solid var(--accent); line-height: 1.5; }
.goal-answer-row { display: flex; gap: 8px; margin: 8px 10px 10px 16px; }
.goal-answer-row input { flex: 1; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 14px; color: var(--text); font-size: 13px; outline: none; transition: border-color var(--transition); min-height: 44px; }
.goal-answer-row input:focus { border-color: var(--accent); }
.goal-answer-row button { background: var(--accent); color: #fff; border: none; border-radius: var(--radius-sm); padding: 10px 16px; cursor: pointer; font-weight: 600; transition: background var(--transition); min-height: 44px; }
.goal-answer-row button:hover { background: var(--accent-hover); }

/* In-chat goals bar */
#chat-goals-bar { border-bottom: 1px solid var(--border); }
#chat-goals-bar .goals-header { display: flex; align-items: center; gap: 8px; padding: 10px 16px; cursor: pointer; font-size: 12px; color: var(--accent); font-weight: 600; transition: background var(--transition); border-radius: 0; min-height: 44px; }
#chat-goals-bar .goals-header:hover { background: var(--card-hover); }
#chat-goals-bar .goals-header .chevron { transition: transform var(--transition); font-size: 10px; }
#chat-goals-bar .goals-header .chevron.open { transform: rotate(90deg); }
#chat-goals-bar .goals-body { display: none; }
#chat-goals-bar .goals-body.open { display: block; }

/* Goal detail view */
#goal-view { display: none; flex-direction: column; height: 100vh; height: 100dvh; }
#goal-transcript {
  flex: 1; overflow-y: auto; padding: 20px 16px;
  font-family: 'SF Mono', 'Fira Code', monospace; font-size: 12px;
  line-height: 1.8; color: var(--dim); white-space: pre-wrap;
  word-break: break-word; -webkit-overflow-scrolling: touch;
}
.goal-action-bar { padding: 14px 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.goal-action-bar button { background: var(--card); border: 1px solid var(--border); color: var(--dim); border-radius: var(--radius-sm); padding: 12px 18px; font-size: 13px; cursor: pointer; transition: background var(--transition), color var(--transition); min-height: 44px; }
.goal-action-bar button:hover { background: var(--card-hover); color: var(--text); }
.goal-action-bar button.danger { color: var(--red); border-color: rgba(231,76,60,0.3); }
.goal-action-bar button.danger:hover { background: rgba(231,76,60,0.08); }

/* Reconnect banner */
.reconnect-banner {
  position: fixed; top: 0; left: 0; right: 0; padding: 10px 0;
  background: rgba(15, 15, 15, 0.97); backdrop-filter: blur(12px);
  color: var(--dim); text-align: center; font-size: 12px;
  font-weight: 500; letter-spacing: 0.3px; z-index: 9999;
  opacity: 0; transform: translateY(-100%);
  transition: opacity 0.4s ease, transform 0.4s ease;
  border-bottom: 1px solid var(--border);
}
.reconnect-banner.visible { opacity: 1; transform: translateY(0); }
.reconnect-banner.reconnected { background: rgba(15, 30, 20, 0.97); color: var(--green); border-bottom-color: rgba(92,184,92,0.2); }

/* Context indicator */
#ctx-indicator { padding: 6px 16px; font-size: 11px; color: var(--dim); border-bottom: 1px solid var(--border-subtle); letter-spacing: 0.2px; }

/* App shell */
#app-shell { height: 100vh; height: 100dvh; }

/* Empty state for right panel */
#empty-state { display: none; flex: 1; align-items: center; justify-content: center; height: 100%; }
.empty-state-content { text-align: center; color: var(--dim); }
.empty-state-icon { font-size: 36px; display: block; margin-bottom: 12px; opacity: 0.5; }
.empty-state-content p { font-size: 14px; letter-spacing: 0.5px; }

/* Mobile: keep existing stacked behavior */
#right-panel { display: contents; }

/* Desktop layout — split pane */
@media (min-width: 768px) {
  #app-shell { display: flex; flex-direction: row; }
  #conv-view { width: 320px; min-width: 320px; max-width: 320px; border-right: 1px solid var(--border); height: 100vh; height: 100dvh; display: flex !important; flex-direction: column; }
  #right-panel { display: flex; flex-direction: column; flex: 1; min-width: 0; height: 100vh; height: 100dvh; }
  #chat-view { flex: 1; flex-direction: column; height: 100%; border: none; max-width: none; margin: 0; }
  #voice-view { flex: 1; height: 100%; max-width: none; margin: 0; }
  #goal-view { flex: 1; height: 100%; max-width: none; margin: 0; }
  #empty-state { display: flex; }
  #chat-back-btn { display: none; }
  .msg { max-width: 75%; }
  .voice-msg { max-width: 75%; }
  #chat-messages { padding: 24px 28px; gap: 10px; }
  .chat-input { padding: 14px 20px; }
  .conv-card { padding: 14px 18px; }
  #conv-list { padding: 6px 8px; }
  .conv-card .conv-title { font-size: 14px; }
  .conv-card .conv-meta { font-size: 11px; }
  /* Active conversation highlight */
  .conv-card.active { background: var(--card); border-left: 3px solid var(--accent); }
}

@media (min-width: 1200px) {
  #conv-view { width: 360px; min-width: 360px; max-width: 360px; }
  .msg { max-width: 70%; }
}
