:root {
  --bg-main: #090d16;
  --bg-surface: #111827;
  --bg-surface-elevated: #1e293b;
  --border-subtle: #27354a;
  --text-main: #f1f5f9;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --accent-cyan: #38bdf8;
  --accent-blue: #3b82f6;
  --accent-purple: #818cf8;
  --accent-emerald: #10b981;
  --accent-amber: #f59e0b;
  --user-bubble: linear-gradient(135deg, #2563eb, #1d4ed8);
  --ai-bubble: #131c2e;
  --shadow-glow: 0 4px 20px rgba(56, 189, 248, 0.15);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* App Container */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  position: relative;
  border-left: 1px solid var(--border-subtle);
  border-right: 1px solid var(--border-subtle);
  background: var(--bg-main);
}

/* Header */
header {
  padding: calc(12px + var(--safe-top)) 16px 12px 16px;
  background: rgba(17, 24, 39, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 20;
}

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

.app-logo {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: linear-gradient(135deg, #38bdf8, #818cf8);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  box-shadow: var(--shadow-glow);
}

.app-title-group h1 {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.engine-status-row {
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 11px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(30, 41, 59, 0.8);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-emerald);
}

.header-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: rgba(30, 41, 59, 0.6);
  border: 1px solid var(--border-subtle);
  color: var(--text-main);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}

.icon-btn:active {
  transform: scale(0.95);
  background: var(--bg-surface-elevated);
}

/* Chat Thread */
#chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 88%;
  animation: fadeIn 0.2s ease-out;
}

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

.msg-row.user {
  align-self: flex-end;
}

.msg-row.ai {
  align-self: flex-start;
}

.msg-header {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.msg-row.user .msg-header {
  justify-content: flex-end;
}

.bubble {
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
}

.msg-row.user .bubble {
  background: var(--user-bubble);
  color: #ffffff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 10px rgba(37, 99, 235, 0.25);
}

.msg-row.ai .bubble {
  background: var(--ai-bubble);
  color: var(--text-main);
  border: 1px solid var(--border-subtle);
  border-bottom-left-radius: 4px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
}

/* Markdown Rendering in AI Bubble */
.bubble p { margin-bottom: 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 12px 0 6px 0; font-size: 1.1em; color: var(--accent-cyan); }
.bubble ul, .bubble ol { margin-left: 18px; margin-bottom: 8px; }
.bubble li { margin-bottom: 4px; }
.bubble code {
  background: rgba(15, 23, 42, 0.8);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.9em;
  color: #38bdf8;
}
.bubble pre {
  background: #090e17;
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 10px 0;
  border: 1px solid var(--border-subtle);
}
.bubble pre code { background: none; padding: 0; color: #e2e8f0; }
.bubble blockquote {
  border-left: 3px solid var(--accent-cyan);
  padding-left: 10px;
  margin: 8px 0;
  color: #cbd5e1;
  background: rgba(56, 189, 248, 0.05);
}

/* Action Tag Card */
.action-card {
  margin-top: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(16, 185, 129, 0.1);
  border: 1px solid rgba(16, 185, 129, 0.3);
  color: #34d399;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Bottom Bar */
footer {
  padding: 10px 14px calc(12px + var(--safe-bottom)) 14px;
  background: rgba(17, 24, 39, 0.95);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 20;
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#chat-input {
  flex: 1;
  background: #090d16;
  border: 1px solid var(--border-subtle);
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 15px;
  color: var(--text-main);
  resize: none;
  max-height: 120px;
  min-height: 44px;
  line-height: 1.4;
  outline: none;
  transition: border-color 0.15s;
}

#chat-input:focus {
  border-color: var(--accent-cyan);
}

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #38bdf8, #2563eb);
  color: white;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: var(--shadow-glow);
  transition: transform 0.15s;
}

.send-btn:active {
  transform: scale(0.92);
}

.mic-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--bg-surface-elevated);
  color: var(--text-muted);
  border: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}

.attach-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--bg-surface-elevated);
  color: var(--text-muted);
  border: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}

.attach-btn:active {
  transform: scale(0.92);
  background: rgba(56, 189, 248, 0.2);
  border-color: var(--accent-cyan);
}

.mic-btn.listening {
  background: #ef4444;
  color: white;
  animation: pulse 1.2s infinite;
  border-color: #f87171;
}

/* Attachment Bar & Chips */
.attachment-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 2px 8px 2px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: rgba(30, 41, 59, 0.85);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 4px 8px 4px 4px;
  font-size: 12px;
  color: var(--text-main);
  flex-shrink: 0;
  max-width: 200px;
  animation: fadeIn 0.2s ease-out;
}

.attachment-thumb {
  width: 38px;
  height: 38px;
  border-radius: 6px;
  object-fit: cover;
  background: #000;
}

.attachment-file-icon {
  width: 38px;
  height: 38px;
  border-radius: 6px;
  background: rgba(56, 189, 248, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.attachment-info {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  line-height: 1.3;
}

.attachment-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}

.attachment-size {
  font-size: 10px;
  color: var(--text-dim);
}

.attachment-remove-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  margin-left: 2px;
}

.attachment-remove-btn:hover {
  color: #ef4444;
}

/* Chat Attachments inside Bubbles */
.chat-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.chat-attachment-img {
  max-width: 220px;
  max-height: 200px;
  border-radius: 10px;
  object-fit: cover;
  border: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  display: block;
}

.chat-attachment-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.15);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 12px;
  color: var(--accent-cyan);
  text-decoration: none;
  word-break: break-all;
}

@keyframes pulse {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); }
  70% { transform: scale(1.06); box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal-overlay.active {
  display: flex;
}

.modal-box {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 16px;
  width: 100%;
  max-width: 440px;
  padding: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-title {
  font-size: 17px;
  font-weight: 700;
}

.close-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 20px;
  cursor: pointer;
}

/* Toast */
#toast {
  position: fixed;
  top: calc(20px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%) translateY(-50px);
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid var(--accent-cyan);
  color: var(--text-main);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: var(--shadow-glow);
  z-index: 200;
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: none;
}

#toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* Loading indicator */
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
}

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

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

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1.1); opacity: 1; }
}
