/* ── Variables ─────────────────────────────────────────────────────────────── */
:root {
  --bg:        #0f1419;
  --bg2:       #1a2332;
  --bg3:       #243044;
  --bg4:       #2e3d55;
  --accent:    #4fc3f7;
  --accent2:   #0288d1;
  --success:   #66bb6a;
  --warning:   #ffa726;
  --danger:    #ef5350;
  --text:      #e8eaf0;
  --text-dim:  #8899aa;
  --border:    #2a3a4e;
  --topbar-h:  48px;
  --sidebar-w: 240px;
  --files-w:   260px;
  --nav-h:     52px;
  --radius:    8px;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  overflow: hidden;
}

button { cursor: pointer; border: none; background: none; color: inherit; font: inherit; }
textarea, input { font: inherit; color: inherit; background: none; border: none; outline: none; }

/* ── Login ─────────────────────────────────────────────────────────────────── */
#login-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}

.login-box {
  width: min(360px, 90vw);
  padding: 40px 32px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  text-align: center;
}

.login-logo { font-size: 40px; margin-bottom: 12px; }
.login-title { font-size: 22px; font-weight: 700; color: var(--accent); }
.login-sub { color: var(--text-dim); font-size: 13px; margin: 4px 0 24px; }

#login-form input {
  width: 100%; padding: 12px 16px;
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 15px;
  margin-bottom: 12px;
}
#login-form input:focus { border-color: var(--accent); }

#login-form button {
  width: 100%; padding: 12px;
  background: var(--accent); color: var(--bg);
  border-radius: var(--radius); font-size: 15px; font-weight: 600;
}
#login-form button:hover { background: #81d4fa; }

.login-error { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 20px; }

/* ── App shell ─────────────────────────────────────────────────────────────── */
#app {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding-top: var(--topbar-h);
  padding-bottom: 0;
}

/* ── Topbar ────────────────────────────────────────────────────────────────── */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
  padding: 0 12px;
  z-index: 100;
}

.thread-title {
  flex: 1; font-weight: 600; font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.topbar-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 34px; height: 34px;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; transition: background .15s;
}
.icon-btn:hover { background: var(--bg3); }

.ws-dot {
  width: 8px; height: 8px; border-radius: 50%;
  display: inline-block; flex-shrink: 0;
}
.ws-dot.connected    { background: var(--success); }
.ws-dot.connecting   { background: var(--warning); animation: pulse 1s infinite; }
.ws-dot.disconnected { background: var(--danger); }
.icon-btn.active { background: var(--bg3); color: var(--accent); }
.icon-btn.danger:hover { background: rgba(239,83,80,.15); color: var(--danger); }

/* ── Main layout ───────────────────────────────────────────────────────────── */
#main {
  flex: 1; display: flex; overflow: hidden;
  height: calc(100vh - var(--topbar-h));
}

/* ── Sidebar ───────────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0; overflow: hidden;
  transition: width .2s;
}
#sidebar.collapsed { width: 0; }

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

.thread-item {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 10px;
  border-radius: var(--radius);
  cursor: pointer; transition: background .12s;
  margin-bottom: 2px; position: relative;
}
.thread-item:hover { background: var(--bg3); }
.thread-item.active { background: var(--bg4); }

.thread-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.thread-dot.idle    { background: var(--success); }
.thread-dot.running { background: var(--warning); animation: pulse 1s infinite; }
.thread-dot.error   { background: var(--danger); }

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

.thread-info { flex: 1; min-width: 0; }
.thread-name {
  font-size: 13px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.thread-meta { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

.thread-delete {
  opacity: 0; font-size: 16px; color: var(--text-dim);
  padding: 0 4px; border-radius: 4px;
}
.thread-item:hover .thread-delete { opacity: 1; }
.thread-delete:hover { color: var(--danger); }

.new-thread-btn {
  margin: 8px; padding: 10px;
  background: var(--bg3); border: 1px dashed var(--border);
  border-radius: var(--radius); font-size: 13px;
  color: var(--accent); transition: background .12s;
}
.new-thread-btn:hover { background: var(--bg4); }

/* ── Chat area ─────────────────────────────────────────────────────────────── */
#chat-area {
  flex: 1; display: flex; flex-direction: column; overflow: hidden;
  min-width: 0;
}

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

/* Welcome state */
.chat-empty {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--text-dim); text-align: center; gap: 8px;
}
.chat-empty .empty-icon { font-size: 40px; }
.chat-empty p { font-size: 13px; }

/* Message bubbles */
.msg {
  display: flex; flex-direction: column;
  max-width: min(680px, 90%);
}
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.55;
  word-break: break-word;
}
.msg.user .msg-bubble {
  background: var(--accent2);
  border-bottom-right-radius: 4px;
}
.msg.assistant .msg-bubble {
  background: var(--bg3);
  border-bottom-left-radius: 4px;
}

.msg-bubble.streaming { opacity: .85; }
.msg-bubble.streaming::after {
  content: '▋';
  animation: blink .7s step-start infinite;
  color: var(--accent);
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

.msg-footer {
  display: flex; align-items: center; gap: 8px;
  margin-top: 4px; padding: 0 4px;
}
.msg-time { font-size: 11px; color: var(--text-dim); }
.msg-cost { font-size: 11px; color: var(--text-dim); }
.msg-play {
  font-size: 12px; color: var(--text-dim);
  padding: 2px 6px; border-radius: 4px;
  transition: background .12s;
}
.msg-play:hover { background: var(--bg3); color: var(--accent); }

/* Inline markdown */
.msg-bubble code {
  background: rgba(0,0,0,.3); padding: 1px 5px; border-radius: 3px;
  font-family: 'SF Mono', 'Fira Code', monospace; font-size: 12px;
}
.msg-bubble pre {
  background: rgba(0,0,0,.3); padding: 10px 12px; border-radius: 6px;
  overflow-x: auto; margin: 6px 0;
}
.msg-bubble pre code { background: none; padding: 0; }
.msg-bubble strong { font-weight: 600; color: #fff; }
.msg-bubble em { color: #c8d8f0; }
.msg-bubble a { color: var(--accent); }

/* Tool cards */
.tool-card {
  margin-top: 8px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
  font-size: 12px;
}
.tool-header {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; cursor: pointer;
  background: var(--bg3); user-select: none;
}
.tool-header:hover { background: var(--bg4); }
.tool-icon { font-size: 14px; }
.tool-name { font-weight: 600; color: var(--accent); }
.tool-chevron { margin-left: auto; color: var(--text-dim); transition: transform .2s; }
.tool-card.open .tool-chevron { transform: rotate(90deg); }
.tool-body { display: none; padding: 8px 10px; }
.tool-card.open .tool-body { display: block; }
.tool-section { margin-bottom: 6px; }
.tool-label { color: var(--text-dim); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; margin-bottom: 3px; }
.tool-content {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 11px; white-space: pre-wrap; word-break: break-all;
  background: rgba(0,0,0,.25); padding: 6px 8px; border-radius: 4px;
  max-height: 180px; overflow-y: auto; color: #c8e6c9;
}
.tool-content.output { color: var(--text); }

/* ── Input bar ─────────────────────────────────────────────────────────────── */
#input-bar {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.mic-btn { flex-shrink: 0; }
.mic-btn.listening { color: var(--danger); background: rgba(239,83,80,.1); animation: pulse 1s infinite; }

#msg-input {
  flex: 1; resize: none; line-height: 1.5;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 12px;
  max-height: 160px; overflow-y: auto;
  font-size: 14px;
}
#msg-input:focus { border-color: var(--accent); }
#msg-input::placeholder { color: var(--text-dim); }

.send-btn {
  width: 36px; height: 36px; flex-shrink: 0;
  background: var(--accent); color: var(--bg);
  border-radius: 8px; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  transition: background .12s;
}
.send-btn:hover { background: #81d4fa; }
.send-btn:disabled { background: var(--bg3); color: var(--text-dim); cursor: default; }

/* ── Files panel ───────────────────────────────────────────────────────────── */
#files-panel {
  width: var(--files-w);
  background: var(--bg2);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0; transition: width .2s; overflow: hidden;
}
#files-panel.collapsed { width: 0; }

.files-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 12px 8px;
  font-weight: 600; font-size: 13px; color: var(--text-dim);
}

.upload-btn {
  font-size: 20px; cursor: pointer; color: var(--accent);
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 4px;
}
.upload-btn:hover { background: var(--bg3); }

#files-drop-zone {
  flex: 1; overflow-y: auto;
  padding: 8px;
  position: relative;
}
#files-drop-zone.drag-over { outline: 2px dashed var(--accent); outline-offset: -4px; }

#drop-hint {
  text-align: center; color: var(--text-dim); font-size: 12px;
  padding: 20px; display: none;
}

#file-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px;
}

.file-item {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 6px; overflow: hidden;
  cursor: pointer; transition: border-color .12s;
  position: relative; font-size: 11px;
}
.file-item:hover { border-color: var(--accent); }

.file-thumb {
  width: 100%; aspect-ratio: 1;
  object-fit: cover; display: block;
}
.file-icon-wrap {
  width: 100%; aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; background: var(--bg4);
}
.file-name {
  padding: 4px 6px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  color: var(--text-dim);
}
.file-del {
  position: absolute; top: 3px; right: 3px;
  background: rgba(15,20,25,.8); color: var(--danger);
  border-radius: 4px; width: 18px; height: 18px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; opacity: 0; transition: opacity .12s;
}
.file-item:hover .file-del { opacity: 1; }

/* ── Mobile bottom nav ─────────────────────────────────────────────────────── */
#mobile-nav {
  display: none;
  height: var(--nav-h);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.nav-btn {
  flex: 1; font-size: 13px; font-weight: 500;
  color: var(--text-dim); transition: color .12s;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.nav-btn.active { color: var(--accent); }

/* ── Driving overlay ───────────────────────────────────────────────────────── */
#driving-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  z-index: 200;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 20px; padding: 40px 24px;
  text-align: center;
}

.driving-thread-name {
  font-size: 16px; color: var(--text-dim); font-weight: 500;
}

.driving-status {
  font-size: 20px; font-weight: 600; color: var(--accent);
}

.driving-wave {
  display: flex; align-items: center; gap: 5px; height: 48px;
}
.wave-bar {
  width: 5px; border-radius: 3px; background: var(--accent);
  animation: wave 1.2s ease-in-out infinite;
}
.wave-bar:nth-child(1) { animation-delay: 0s;    height: 20px; }
.wave-bar:nth-child(2) { animation-delay: .1s;   height: 36px; }
.wave-bar:nth-child(3) { animation-delay: .2s;   height: 48px; }
.wave-bar:nth-child(4) { animation-delay: .3s;   height: 36px; }
.wave-bar:nth-child(5) { animation-delay: .4s;   height: 20px; }
@keyframes wave {
  0%, 100% { transform: scaleY(1); opacity: .8; }
  50% { transform: scaleY(.4); opacity: .4; }
}
.driving-wave.idle .wave-bar { animation: none; height: 8px; opacity: .3; }
.driving-wave.speaking .wave-bar { background: var(--warning); }

.driving-last-exchange {
  font-size: 18px; line-height: 1.5;
  max-width: 600px; color: var(--text);
  max-height: 30vh; overflow-y: auto;
}

.driving-stop {
  padding: 16px 48px;
  background: var(--danger); color: #fff;
  border-radius: 40px; font-size: 18px; font-weight: 700;
}
.driving-stop:hover { background: #e53935; }

.driving-exit {
  font-size: 13px; color: var(--text-dim); text-decoration: underline;
}

/* ── Scrollbars ────────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Mobile ────────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  #mobile-nav { display: flex; }
  #app { padding-bottom: var(--nav-h); }

  #sidebar {
    position: fixed; left: 0; top: var(--topbar-h); bottom: var(--nav-h);
    z-index: 50; display: none;
    width: 100%;
  }
  #sidebar.mobile-visible { display: flex; }

  #files-panel {
    position: fixed; right: 0; top: var(--topbar-h); bottom: var(--nav-h);
    z-index: 50; display: none;
    width: 100%;
  }
  #files-panel.mobile-visible { display: flex; width: 100%; }

  #btn-files-toggle { display: none; }

  #main { flex-direction: column; }
  #chat-area { display: flex; }
}

@media (min-width: 769px) {
  #mobile-nav { display: none; }
  #sidebar { display: flex !important; }
}
