:root {
  --bg-black: #060606;
  --bg-card: rgba(18, 16, 12, 0.85);
  --border-gold: rgba(212, 175, 55, 0.2);
  --gold-primary: #ffd700;
  --gold-glow: #d4af37;
  --gold-light: #fef08a;
  --text-white: #fafafa;
  --text-muted: #71717a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* CSS Theme Presets */
[data-theme="cyberpunk"] {
  --gold-primary: #00f3ff;
  --gold-glow: #00a8ff;
  --gold-light: #80f7ff;
  --border-gold: rgba(0, 243, 255, 0.25);
  --bg-card: rgba(10, 16, 24, 0.85);
}

[data-theme="emerald"] {
  --gold-primary: #10b981;
  --gold-glow: #059669;
  --gold-light: #6ee7b7;
  --border-gold: rgba(16, 185, 129, 0.25);
  --bg-card: rgba(8, 18, 14, 0.85);
}

[data-theme="amethyst"] {
  --gold-primary: #c084fc;
  --gold-glow: #9333ea;
  --gold-light: #e9d5ff;
  --border-gold: rgba(192, 132, 252, 0.25);
  --bg-card: rgba(16, 10, 22, 0.85);
}

[data-theme="crimson"] {
  --gold-primary: #f43f5e;
  --gold-glow: #e11d48;
  --gold-light: #fecdd3;
  --border-gold: rgba(244, 63, 94, 0.25);
  --bg-card: rgba(24, 10, 14, 0.85);
}

[data-theme="sunset"] {
  --gold-primary: #f97316;
  --gold-glow: #ea580c;
  --gold-light: #ffedd5;
  --border-gold: rgba(249, 115, 22, 0.25);
  --bg-card: rgba(24, 14, 10, 0.85);
}

[data-theme="midnight"] {
  --gold-primary: #3b82f6;
  --gold-glow: #2563eb;
  --gold-light: #bfdbfe;
  --border-gold: rgba(59, 130, 246, 0.25);
  --bg-card: rgba(10, 14, 24, 0.85);
}

/* Monochrome / Dark Themes */
[data-theme="obsidian"] {
  --gold-primary: #ffffff;
  --gold-glow: #a1a1aa;
  --gold-light: #ffffff;
  --border-gold: rgba(255, 255, 255, 0.25);
  --bg-card: rgba(10, 10, 10, 0.9);
  --bg-black: #000000;
}

[data-theme="slate"] {
  --gold-primary: #94a3b8;
  --gold-glow: #64748b;
  --gold-light: #cbd5e1;
  --border-gold: rgba(148, 163, 184, 0.25);
  --bg-card: rgba(15, 23, 42, 0.85);
}

[data-theme="silver"] {
  --gold-primary: #e2e8f0;
  --gold-glow: #94a3b8;
  --gold-light: #f8fafc;
  --border-gold: rgba(226, 232, 240, 0.25);
  --bg-card: rgba(18, 20, 24, 0.85);
}

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

body {
  font-family: 'Plus Jakarta Sans', sans-serif;
  background-color: var(--bg-black);
  color: var(--text-white);
  height: 100vh;
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Animated Canvas Background */
#bg-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  z-index: 0;
  pointer-events: none;
}

/* Custom Scrollbars */
::-webkit-scrollbar {
  width: 4px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border-gold);
  border-radius: 4px;
}

.glow-aura {
  position: fixed;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: radial-gradient(circle at center, rgba(212, 175, 55, 0.08) 0%, transparent 70%);
  pointer-events: none;
  z-index: 1;
}

.app-viewport {
  position: relative;
  z-index: 10;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: flex;
  background: var(--bg-card);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  overflow: hidden;
}

@media (min-width: 768px) {
  .app-viewport {
    width: 95vw;
    max-width: 1100px;
    height: 88vh;
    border: 1px solid var(--border-gold);
    border-radius: 20px;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.8), 0 0 15px rgba(212, 175, 55, 0.05);
  }
}

/* Mobile Drawer & Overlay Optimization */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 25;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.sidebar {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 82vw;
  max-width: 300px;
  background: rgba(10, 8, 5, 0.96);
  border-right: 1px solid var(--border-gold);
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 1rem) 1rem calc(var(--safe-bottom) + 1rem) 1rem;
  gap: 1rem;
  z-index: 30;
  transform: translateX(-100%);
  transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar.open {
  transform: translateX(0);
  box-shadow: 10px 0 30px rgba(0, 0, 0, 0.8);
}

@media (min-width: 768px) {
  .sidebar {
    position: relative;
    transform: none;
    width: 280px;
    background: rgba(10, 8, 4, 0.6);
    padding: 1rem;
  }
  .sidebar-overlay {
    display: none;
  }
}

.btn-new-chat {
  background: rgba(212, 175, 55, 0.12);
  color: var(--gold-primary);
  border: 1px solid var(--border-gold);
  padding: 0.85rem 1rem;
  border-radius: 14px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 48px; /* Mobile Touch Target Standard */
  transition: all 0.2s ease;
}

.btn-new-chat:active {
  background: rgba(212, 175, 55, 0.25);
  transform: scale(0.98);
}

.history-title {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.7rem;
  color: var(--text-muted);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 0 0.5rem;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  -webkit-overflow-scrolling: touch;
}

.history-item {
  padding: 0.85rem 0.9rem;
  border-radius: 12px;
  font-size: 0.88rem;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 44px;
  transition: all 0.2s ease;
}

.history-item:active {
  background: rgba(212, 175, 55, 0.1);
  color: var(--text-white);
}

.history-item.active {
  background: rgba(212, 175, 55, 0.14);
  color: var(--gold-light);
  border: 1px solid rgba(212, 175, 55, 0.15);
}

.history-item .item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.delete-btn {
  background: none;
  border: none;
  color: #ef4444;
  cursor: pointer;
  font-size: 1rem;
  padding: 6px 10px;
  opacity: 0.7;
}

/* Sidebar Footer & Logout Button */
.sidebar-footer {
  padding-top: 0.85rem;
  border-top: 1px solid var(--border-gold);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: auto;
}

.user-info {
  padding: 0 0.25rem;
}

.user-email-text {
  font-size: 0.82rem;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.btn-logout {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
  border: 1px solid rgba(239, 68, 68, 0.25);
  border-radius: 12px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 42px;
  transition: all 0.2s ease;
}

.btn-logout:hover {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(239, 68, 68, 0.45);
}

.btn-logout:active {
  transform: scale(0.98);
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  width: 100%;
  height: 100%;
}

header {
  padding: calc(var(--safe-top) + 0.6rem) 0.9rem 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border-gold);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(10, 8, 4, 0.75);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 15;
}

@media (min-width: 768px) {
  header {
    padding: 0.8rem 1rem;
  }
}

.left-nav {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.theme-dropdown {
  background: rgba(20, 18, 12, 0.85);
  color: var(--gold-light);
  border: 1px solid var(--border-gold);
  padding: 0.45rem 0.5rem;
  border-radius: 10px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.75rem;
  outline: none;
  cursor: pointer;
  max-width: 110px;
  text-overflow: ellipsis;
  height: 38px;
}

@media (min-width: 480px) {
  .theme-dropdown {
    max-width: none;
    padding: 0.4rem 0.6rem;
  }
}

.theme-dropdown option {
  background: #0f0f0f;
  color: #fafafa;
}

.menu-btn {
  background: rgba(212, 175, 55, 0.08);
  border: 1px solid var(--border-gold);
  color: var(--gold-primary);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  cursor: pointer;
  flex-shrink: 0;
}

.menu-btn:active {
  background: rgba(212, 175, 55, 0.2);
}

@media (min-width: 768px) {
  .menu-btn {
    display: none;
  }
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

#avatar-canvas {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: rgba(212, 175, 55, 0.05);
  border: 1px solid var(--border-gold);
  flex-shrink: 0;
}

.logo-group {
  display: flex;
  flex-direction: column;
}

.brand-subtitle {
  font-size: 0.5rem;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-muted);
  letter-spacing: 0.1em;
  display: none;
}

@media (min-width: 380px) {
  .brand-subtitle {
    display: block;
  }
}

.logo {
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.logo span {
  color: var(--gold-primary);
}

.status-badge {
  background: rgba(20, 18, 12, 0.85);
  color: var(--gold-light);
  border: 1px solid var(--border-gold);
  padding: 0.4rem 0.6rem;
  border-radius: 10px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.7rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  height: 38px;
}

.status-text {
  display: none;
}

@media (min-width: 480px) {
  .status-text {
    display: inline;
  }
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #22c55e;
  box-shadow: 0 0 8px #22c55e;
  flex-shrink: 0;
}

.chat-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.chat-box {
  flex: 1;
  overflow-y: auto;
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

@media (min-width: 768px) {
  .chat-box {
    padding: 1.5rem 2rem;
    gap: 1.2rem;
  }
}

/* Base Message Styling */
.message {
  width: 100%;
  max-width: 100%;
  padding: 0.85rem 1rem;
  border-radius: 14px;
  line-height: 1.6;
  font-size: 0.92rem;
  word-break: break-word;
  animation: fadeIn 0.2s ease;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* Strictly Top-to-Bottom Paragraph Form for AI Responses */
.message.assistant {
  align-self: stretch;
  width: 100%;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 14px;
  color: var(--text-white);
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.message.user {
  align-self: flex-end;
  max-width: 88%;
  background: rgba(212, 175, 55, 0.18);
  border: 1px solid var(--border-gold);
  color: var(--gold-light);
  border-bottom-right-radius: 4px;
}

/* Full Width Paragraph Flow for AI Output */
.message p {
  display: block;
  width: 100%;
  margin: 0;
  line-height: 1.65;
}

.message ul, 
.message ol {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin: 0;
  padding-left: 1.25rem;
  width: 100%;
}

.message li {
  display: list-item;
  width: 100%;
  line-height: 1.6;
}

.message pre {
  display: block;
  width: 100%;
  background: #18181b;
  padding: 0.85rem;
  border-radius: 10px;
  overflow-x: auto;
  margin: 0;
  border: 1px solid rgba(255, 255, 255, 0.1);
  -webkit-overflow-scrolling: touch;
}

.message code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.82rem;
}

.message :not(pre) > code {
  background: rgba(255, 255, 255, 0.12);
  padding: 2px 5px;
  border-radius: 4px;
  color: var(--gold-light);
  display: inline;
  width: auto;
}

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

.typing-cursor {
  display: inline-block;
  width: 6px;
  height: 14px;
  background-color: var(--gold-primary);
  margin-left: 4px;
  vertical-align: middle;
  animation: blink 0.7s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.thinking-notice {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.78rem;
}

.thinking-spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--border-gold);
  border-top: 2px solid var(--gold-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Mobile Sticky Input Deck & Keyboard Optimization */
.input-deck {
  padding: 0.6rem 0.8rem calc(var(--safe-bottom) + 0.6rem) 0.8rem;
  background: rgba(10, 8, 4, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border-gold);
}

@media (min-width: 768px) {
  .input-deck {
    padding: 1rem 1.5rem;
  }
}

.input-wrapper {
  display: flex;
  align-items: center;
  background: rgba(22, 19, 14, 0.95);
  border: 1px solid var(--border-gold);
  border-radius: 16px;
  padding: 0.35rem 0.4rem 0.35rem 0.8rem;
  gap: 0.4rem;
}

textarea {
  flex: 1;
  background: transparent;
  color: var(--text-white);
  border: none;
  outline: none;
  resize: none;
  height: 24px;
  max-height: 120px;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.4;
}

.btn-gold {
  background: var(--gold-primary);
  color: #050505;
  border: none;
  padding: 0.6rem 1rem;
  border-radius: 12px;
  font-weight: 700;
  font-size: 0.85rem;
  cursor: pointer;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.2s, transform 0.1s;
}

.btn-gold:active:not(:disabled) {
  transform: scale(0.96);
}

.btn-gold:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* =========================================================
   MOBILE MARKDOWN & TABLE RESPONSIVENESS FIXES
   ========================================================= */

/* 1. Prevent Table Squeezing with Touch Scrolling */
.message table {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  white-space: normal;
  -webkit-overflow-scrolling: touch;
  border-collapse: collapse;
  margin: 0.75rem 0;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.message th, 
.message td {
  padding: 0.65rem 0.8rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  min-width: 120px; /* Forces minimum width so text doesn't crush vertically */
  text-align: left;
  vertical-align: top;
  font-size: 0.85rem;
  line-height: 1.45;
}

.message th {
  background: rgba(212, 175, 55, 0.18);
  color: var(--gold-light);
  font-weight: 600;
  white-space: nowrap;
}

.message td {
  background: rgba(0, 0, 0, 0.25);
}

/* 2. Responsive Heading Sizes on Mobile */
.message h1 { 
  font-size: 1.15rem; 
  font-weight: 700; 
  margin: 0.8rem 0 0.4rem; 
  line-height: 1.35; 
  color: var(--gold-light);
}

.message h2 { 
  font-size: 1.05rem; 
  font-weight: 700; 
  margin: 0.7rem 0 0.35rem; 
  line-height: 1.35; 
}

.message h3 { 
  font-size: 0.95rem; 
  font-weight: 600; 
  margin: 0.6rem 0 0.3rem; 
}

/* 3. Auto-Wrap Long URLs */
.message a {
  color: #60a5fa;
  word-break: break-all;
  overflow-wrap: anywhere;
  text-decoration: underline;
}

/* 4. Compact Message Padding for Mobile Containers */
@media (max-width: 767px) {
  .message.assistant {
    padding: 0.75rem 0.85rem;
  }
}