/* ===== RESET & BASE ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg:         #0a0a0f;
  --bg-2:       #12121a;
  --bg-3:       #1a1a26;
  --bg-4:       #22223a;
  --accent:     #6c63ff;
  --accent-dim: #4e47cc;
  --accent-glow: rgba(108, 99, 255, 0.18);
  --text:       #e8e8f0;
  --text-dim:   #8888aa;
  --text-muted: #55556a;
  --user-bubble: #1e1e3a;
  --ai-bubble:   #14141f;
  --border:     rgba(108, 99, 255, 0.15);
  --border-soft: rgba(255,255,255,0.06);
  --success:    #4ade80;
  --warning:    #facc15;
  --error:      #f87171;
  --radius:     14px;
  --radius-sm:  8px;
  --font:       'Inter', system-ui, -apple-system, sans-serif;
  --mono:       'JetBrains Mono', 'Fira Code', monospace;
  --header-h:   56px;
  --layer-h:    48px;
  --input-h:    72px;
  --hints-h:    24px;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ===== APP LAYOUT ===== */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 860px;
  margin: 0 auto;
  position: relative;
}

/* ===== HEADER ===== */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 16px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  backdrop-filter: blur(12px);
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  user-select: none;
}

.logo-icon {
  font-size: 22px;
  color: var(--accent);
  line-height: 1;
}

.logo-text {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--text);
}

/* Status indicator */
.status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.status.online .status-dot {
  background: var(--success);
  box-shadow: 0 0 6px var(--success);
  animation: pulse-dot 2.5s ease-in-out infinite;
}

.status.offline .status-dot {
  background: var(--error);
}

.status.checking .status-dot {
  background: var(--warning);
  animation: pulse-dot 1s ease-in-out infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* Icon buttons */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--bg-3);
  color: var(--text);
}

/* ===== LAYER BAR ===== */
#layer-bar {
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.layer-scroll {
  display: flex;
  gap: 6px;
  padding: 8px 14px;
  overflow-x: auto;
  scrollbar-width: none;
  align-items: center;
}

.layer-scroll::-webkit-scrollbar { display: none; }

.layer-btn {
  flex-shrink: 0;
  padding: 4px 13px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.05em;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.layer-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-glow);
}

.layer-btn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 12px rgba(108, 99, 255, 0.4);
}

.layer-info {
  padding: 4px 14px 6px;
  font-size: 11px;
  color: var(--text-muted);
}

.layer-info.hidden { display: none; }

/* ===== CHAT WINDOW ===== */
#chat-window {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--bg-4) transparent;
}

#chat-window::-webkit-scrollbar { width: 4px; }
#chat-window::-webkit-scrollbar-thumb { background: var(--bg-4); border-radius: 4px; }

/* ===== MESSAGES ===== */
.message {
  display: flex;
  max-width: 100%;
  animation: msg-in 0.2s ease-out;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

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

.message.ai {
  justify-content: flex-start;
}

.bubble {
  max-width: min(72%, 600px);
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.65;
  word-break: break-word;
  position: relative;
}

.message.user .bubble {
  background: var(--user-bubble);
  border: 1px solid rgba(108, 99, 255, 0.25);
  border-bottom-right-radius: 4px;
  color: var(--text);
}

.message.ai .bubble {
  background: var(--ai-bubble);
  border: 1px solid var(--border-soft);
  border-bottom-left-radius: 4px;
  color: var(--text);
}

/* Bubble meta */
.meta {
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--mono);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.meta .layer-tag {
  color: var(--accent);
  font-weight: 600;
}

.meta .model-tag {
  color: var(--text-dim);
}

.meta .elapsed-tag {
  color: var(--text-muted);
}

/* Typing indicator */
.bubble.typing {
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.bubble.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: typing-bounce 1.2s ease-in-out infinite;
}

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

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* Markdown inside bubbles */
.bubble p { margin-bottom: 0.6em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 {
  margin: 0.8em 0 0.4em;
  color: var(--text);
  font-weight: 600;
}
.bubble h1 { font-size: 1.2em; }
.bubble h2 { font-size: 1.1em; }
.bubble h3 { font-size: 1em; }

.bubble ul, .bubble ol {
  padding-left: 1.4em;
  margin: 0.5em 0;
}

.bubble li { margin: 0.2em 0; }

.bubble code {
  font-family: var(--mono);
  font-size: 0.85em;
  background: var(--bg-3);
  padding: 1px 5px;
  border-radius: 4px;
  color: #b5aaff;
}

.bubble pre {
  background: var(--bg-3);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0.7em 0;
  position: relative;
}

.bubble pre code {
  background: none;
  padding: 0;
  color: var(--text);
  font-size: 0.83em;
  line-height: 1.55;
}

.bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  margin: 0.6em 0;
  color: var(--text-dim);
  font-style: italic;
}

.bubble a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: rgba(108, 99, 255, 0.4);
}

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

.bubble table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.7em 0;
  font-size: 0.9em;
}

.bubble th, .bubble td {
  padding: 7px 12px;
  border: 1px solid var(--border-soft);
  text-align: left;
}

.bubble th {
  background: var(--bg-3);
  color: var(--text);
  font-weight: 600;
}

.bubble td { color: var(--text-dim); }

/* Error message */
.message.error .bubble {
  background: rgba(248, 113, 113, 0.08);
  border-color: rgba(248, 113, 113, 0.25);
  color: var(--error);
}

/* ===== INPUT AREA ===== */
#input-area {
  background: var(--bg-2);
  border-top: 1px solid var(--border);
  padding: 12px 14px 10px;
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  transition: border-color 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

#user-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 14.5px;
  line-height: 1.55;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--bg-4) transparent;
}

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

#btn-send {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.1s;
}

#btn-send:hover { background: var(--accent-dim); }
#btn-send:active { transform: scale(0.93); }
#btn-send:disabled { background: var(--bg-4); cursor: not-allowed; }

.input-hints {
  display: flex;
  gap: 12px;
  margin-top: 5px;
  padding-left: 2px;
}

.hint {
  font-size: 11px;
  color: var(--text-muted);
}

.session-id {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 10px;
  opacity: 0.5;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
  white-space: nowrap;
}

/* ===== MODAL ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal.hidden { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(6px);
}

.modal-box {
  position: relative;
  z-index: 1;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(420px, 92vw);
  padding: 0;
  overflow: hidden;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border-soft);
}

.modal-header h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

.modal-body {
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 14px;
  color: var(--text-dim);
}

.setting-row span { flex-shrink: 0; }

.setting-row input[type="text"],
.setting-row input[type="number"] {
  flex: 1;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  padding: 6px 10px;
  outline: none;
  font-family: var(--mono);
}

.setting-row input:focus { border-color: var(--accent); }

.checkbox-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

.info-row code {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-3);
  padding: 3px 8px;
  border-radius: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
  white-space: nowrap;
  display: inline-block;
}

.modal-footer {
  display: flex;
  gap: 10px;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border-soft);
}

.primary-btn {
  flex: 1;
  padding: 9px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.primary-btn:hover { background: var(--accent-dim); }

.secondary-btn {
  flex: 1;
  padding: 9px 16px;
  background: var(--bg-3);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
}

.secondary-btn:hover { border-color: var(--accent); color: var(--text); }

/* ===== SCROLLBAR GLOBAL ===== */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-4); border-radius: 4px; }

/* ===== RESPONSIVE ===== */
@media (max-width: 480px) {
  .bubble { max-width: 88%; font-size: 14px; }
  #header { padding: 0 12px; }
  .logo-text { font-size: 16px; }
  #input-area { padding: 10px 10px 8px; }
  .input-hints .hint:not(.session-id) { display: none; }
}

@media (prefers-color-scheme: light) {
  /* Force dark regardless */
  :root { color-scheme: dark; }
}
