:root {
  /* HSL Color System for easier theming */
  --hue: 220;
  --sat: 90%;
  
  --bg-gradient-start: hsl(var(--hue), var(--sat), 10%);
  --bg-gradient-end: hsl(260, var(--sat), 15%);
  
  --glass-bg: rgba(255, 255, 255, 0.05);
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-highlight: rgba(255, 255, 255, 0.15);
  
  --text-main: #ffffff;
  --text-muted: rgba(255, 255, 255, 0.6);
  
  --primary: hsl(var(--hue), var(--sat), 60%);
  --primary-glow: hsla(var(--hue), var(--sat), 60%, 0.4);
  
  --success: #10b981;
  --danger: #ef4444;
  
  --radius-lg: 24px;
  --radius-md: 16px;
  --radius-sm: 8px;
  
  --anim-speed: 0.3s;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  color: var(--text-main);
  background: #0f0f11; /* Fallback */
  height: 100vh;
  overflow: hidden;
}

/* Dynamic Background */
.bg-gradient {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at top left, var(--bg-gradient-start), transparent 70%),
              radial-gradient(circle at bottom right, var(--bg-gradient-end), transparent 70%);
  z-index: -1;
  animation: bgPulse 20s ease-in-out infinite alternate;
}

@keyframes bgPulse {
  0% { opacity: 0.8; }
  100% { opacity: 1; filter: hue-rotate(15deg); }
}

/* Layout */
.app {
  height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.chat-shell {
  position: relative;
  width: min(800px, 100%);
  height: min(90vh, 1000px);
  display: grid;
  grid-template-rows: auto 1fr auto;
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg);
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  transition: transform var(--anim-speed);
}

/* Glass Utility */
.glass {
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
}

.glass-panel {
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
}

/* Auth Overlay */
.auth-splash {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(10px);
  transition: opacity var(--anim-speed);
}

.auth-splash.hidden {
  opacity: 0;
  pointer-events: none;
}

.auth-card {
  width: min(400px, 90%);
  padding: 32px;
  background: rgba(30, 30, 35, 0.8);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg);
  box-shadow: 0 20px 40px rgba(0,0,0,0.4);
  transform: translateY(0);
  transition: transform var(--anim-speed);
}

.auth-splash.hidden .auth-card {
  transform: translateY(20px);
}

.auth-header h2 {
  margin: 0 0 8px;
  font-weight: 600;
  font-size: 1.5rem;
  background: linear-gradient(to right, #fff, #ccc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.auth-header p {
  margin: 0 0 24px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* Forms */
.input-group {
  margin-bottom: 16px;
}

.input-group label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

input, textarea {
  width: 100%;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-sm);
  padding: 12px;
  color: var(--text-main);
  font-family: inherit;
  font-size: 1rem;
  transition: all 0.2s;
}

input:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-glow);
  background: rgba(0, 0, 0, 0.4);
}

/* Buttons */
button {
  cursor: pointer;
  border: none;
  font-family: inherit;
  transition: all 0.2s;
}

.btn-primary {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 12px var(--primary-glow);
}

.btn-primary:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
}

.btn-primary:active {
  transform: translateY(1px);
}

/* Topbar */
.topbar {
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--glass-border);
  background: rgba(0,0,0,0.1);
}

.title-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-dot-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-dot {
  width: 10px;
  height: 10px;
  background: var(--danger);
  border-radius: 50%;
  box-shadow: 0 0 8px var(--danger);
  transition: all 0.3s;
}

.status-dot.on {
  background: var(--success);
  box-shadow: 0 0 12px var(--success);
}

.title-info h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.title-info p {
  margin: 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Chat Area */
.chat-area {
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

/* Scrollbar */
.messages-container::-webkit-scrollbar {
  width: 6px;
}
.messages-container::-webkit-scrollbar-track {
  background: transparent;
}
.messages-container::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
}
.messages-container::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.2);
}

.msg {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 0.95rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: slideUp 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
  position: relative;
}

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

.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--primary), #4f46e5);
  color: white;
  border-bottom-right-radius: 4px;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
}

.msg.assistant {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.05);
  color: var(--text-main);
  border-bottom-left-radius: 4px;
}

/* Composer */
.composer-area {
  padding: 20px;
  /* background: linear-gradient(to top, rgba(0,0,0,0.4), transparent); */
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 8px;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
}

.composer textarea {
  flex: 1;
  background: transparent;
  border: none;
  padding: 12px;
  min-height: 44px;
  max-height: 200px;
  resize: none;
  font-size: 0.95rem;
}

.composer textarea:focus {
  box-shadow: none;
  background: transparent;
}

.composer-controls {
    display: flex;
    gap: 8px;
    padding-bottom: 4px;
}

.btn-icon {
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: transparent;
    color: var(--text-muted);
}

.btn-icon:hover {
    background: rgba(255,255,255,0.1);
    color: var(--text-main);
}

.btn-send {
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    border-radius: 12px;
    background: var(--primary);
    color: white;
    box-shadow: 0 4px 12px var(--primary-glow);
}

.btn-send:hover {
    filter: brightness(1.1);
    transform: scale(1.05);
}

/* Debug Panel */
.debug-panel {
  border-top: 1px solid var(--glass-border);
  background: rgba(0, 0, 0, 0.2);
}

.debug-panel summary {
  padding: 12px 24px;
  cursor: pointer;
  list-style: none;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.debug-panel summary:hover {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.02);
}

.debug-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 200px;
  border-top: 1px solid var(--glass-border);
}

.debug-column {
  padding: 12px;
  border-right: 1px solid var(--glass-border);
  display: flex;
  flex-direction: column;
}

.debug-column:last-child {
  border-right: none;
}

.debug-column h3 {
  margin: 0 0 8px;
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

.log-container {
  flex: 1;
  overflow: auto;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.8);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.row {
  padding: 4px 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.05);
}

.row.warn {
  color: #fca5a5;
  background: rgba(239, 68, 68, 0.1);
}

.btn-text {
    background: transparent;
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 4px 8px;
}
.btn-text:hover {
    color: var(--danger);
    background: rgba(239, 68, 68, 0.1);
    border-radius: 4px;
}

/* Responsive */
@media (max-width: 768px) {
  .app { padding: 0; }
  .chat-shell { width: 100%; height: 100%; border-radius: 0; border: none; }
  .debug-content { grid-template-columns: 1fr; }
  .debug-column { border-right: none; border-bottom: 1px solid var(--glass-border); }
  .msg { max-width: 85%; }
}

/* Typing Indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 16px 20px;
  width: fit-content;
  min-height: 44px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out both;
}

.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }
.typing-dot:nth-child(3) { animation-delay: 0s; }

@keyframes typingBounce {
  0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* Tool Messages */
.msg.tool {
  align-self: center;
  width: 90%;
  max-width: 600px;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid var(--glass-border);
  border-left: 3px solid #8b5cf6; /* Violet accent */
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.85rem;
  color: var(--text-muted);
  padding: 10px 14px;
  margin: 4px 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.msg.tool.completed {
  border-left-color: var(--success);
  opacity: 0.8;
}

.msg.tool.error {
  border-left-color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

.tool-icon {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  flex-shrink: 0;
}

.tool-content {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tool-name {
  color: var(--text-main);
  font-weight: 600;
  margin-right: 6px;
}

.msg.tool.completed {
  border-left-color: var(--success);
  opacity: 0.8;
}

.msg.tool.error {
  border-left-color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

.tool-icon {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  flex-shrink: 0;
}

.tool-content {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tool-name {
  color: var(--text-main);
  font-weight: 600;
  margin-right: 6px;
}
