/* ==========================================================================
   Cloudflare Edge AI Chat - Modern Design System
   ========================================================================== */

:root {
  /* Cloudflare Brand Colors & Theme Palette */
  --cf-orange: #F38020;
  --cf-orange-hover: #E66E00;
  --cf-orange-glow: rgba(243, 128, 32, 0.25);
  
  /* Fallback Light/Dark design tokens */
  --bg-main-light: #f8fafc;
  --bg-main-dark: #0f172a;
  
  --bg-sidebar-light: #ffffff;
  --bg-sidebar-dark: #1e293b;

  --bg-card-light: #ffffff;
  --bg-card-dark: #1e293b;

  --text-main-light: #0f172a;
  --text-main-dark: #f8fafc;

  --text-muted-light: #64748b;
  --text-muted-dark: #94a3b8;

  --border-color-light: #e2e8f0;
  --border-color-dark: #334155;

  --user-msg-bg: var(--cf-orange);
  --user-msg-text: #ffffff;

  --bot-msg-bg-light: #f1f5f9;
  --bot-msg-bg-dark: #1e293b;

  --input-bg-light: #ffffff;
  --input-bg-dark: #0f172a;

  /* Default theme values (overridden dynamically or via media query) */
  --bg-main: var(--bg-main-light);
  --bg-sidebar: var(--bg-sidebar-light);
  --bg-card: var(--bg-card-light);
  --text-main: var(--text-main-light);
  --text-muted: var(--text-muted-light);
  --border-color: var(--border-color-light);
  --bot-msg-bg: var(--bot-msg-bg-light);
  --input-bg: var(--input-bg-light);

  @media (prefers-color-scheme: dark) {
    --bg-main: var(--bg-main-dark);
    --bg-sidebar: var(--bg-sidebar-dark);
    --bg-card: var(--bg-card-dark);
    --text-main: var(--text-main-dark);
    --text-muted: var(--text-muted-dark);
    --border-color: var(--border-color-dark);
    --bot-msg-bg: var(--bot-msg-bg-dark);
    --input-bg: var(--input-bg-dark);
  }

  /* Modern light-dark() CSS enhancement */
  @supports (color: light-dark(white, black)) {
    --bg-main: light-dark(var(--bg-main-light), var(--bg-main-dark));
    --bg-sidebar: light-dark(var(--bg-sidebar-light), var(--bg-sidebar-dark));
    --bg-card: light-dark(var(--bg-card-light), var(--bg-card-dark));
    --text-main: light-dark(var(--text-main-light), var(--text-main-dark));
    --text-muted: light-dark(var(--text-muted-light), var(--text-muted-dark));
    --border-color: light-dark(var(--border-color-light), var(--border-color-dark));
    --bot-msg-bg: light-dark(var(--bot-msg-bg-light), var(--bot-msg-bg-dark));
    --input-bg: light-dark(var(--input-bg-light), var(--input-bg-dark));
  }

  color-scheme: light dark;

  --sidebar-width: 280px;
  --header-height: 64px;
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.12);
  --transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

/* Explicit Dark Mode Attribute Override */
[data-theme="dark"] {
  --bg-main: var(--bg-main-dark);
  --bg-sidebar: var(--bg-sidebar-dark);
  --bg-card: var(--bg-card-dark);
  --text-main: var(--text-main-dark);
  --text-muted: var(--text-muted-dark);
  --border-color: var(--border-color-dark);
  --bot-msg-bg: var(--bot-msg-bg-dark);
  --input-bg: var(--input-bg-dark);
  color-scheme: dark;
}

[data-theme="light"] {
  --bg-main: var(--bg-main-light);
  --bg-sidebar: var(--bg-sidebar-light);
  --bg-card: var(--bg-card-light);
  --text-main: var(--text-main-light);
  --text-muted: var(--text-muted-light);
  --border-color: var(--border-color-light);
  --bot-msg-bg: var(--bot-msg-bg-light);
  --input-bg: var(--input-bg-light);
  color-scheme: light;
}

/* Base Styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  line-height: 1.6;
  height: 100vh;
  overflow: hidden;
}

h1, h2, h3, h4 {
  font-family: 'Outfit', 'Inter', sans-serif;
}

/* Scrollbar Customization */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 99px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Layout */
.app-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  padding: 1.25rem;
  gap: 1.25rem;
  z-index: 100;
  transition: var(--transition);
}

.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

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

.brand-logo {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, var(--cf-orange), #FAAE40);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 1.25rem;
  box-shadow: 0 4px 12px var(--cf-orange-glow);
}

.brand-text h2 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.badge {
  font-size: 0.7rem;
  background: rgba(243, 128, 32, 0.15);
  color: var(--cf-orange);
  padding: 2px 8px;
  border-radius: 99px;
  font-weight: 600;
}

.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.section-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.history-section {
  flex: 1;
  overflow: hidden;
}

.history-list {
  list-style: none;
  overflow-y: auto;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.history-item {
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: var(--transition);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.history-item:hover, .history-item.active {
  background-color: var(--bg-main);
  color: var(--text-main);
}

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  border-top: 1px solid var(--border-color);
  padding-top: 1rem;
}

/* Main Content Area */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
  background-color: var(--bg-main);
}

/* Main Header */
.main-header {
  height: var(--header-height);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  background: var(--bg-main);
  backdrop-filter: blur(8px);
}

.current-chat-title {
  display: flex;
  flex-direction: column;
}

#chat-title-text {
  font-weight: 600;
  font-size: 1rem;
}

.status-indicator {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  background-color: #10b981;
  border-radius: 50%;
  display: inline-block;
  box-shadow: 0 0 8px rgba(16, 185, 129, 0.6);
}

/* Messages Container */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 2rem 15%;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

@media (max-width: 992px) {
  .messages-container {
    padding: 1.5rem 5%;
  }
}

/* Welcome Card */
.welcome-card {
  margin: auto;
  text-align: center;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  padding: 2.5rem 1.5rem;
}

.welcome-icon {
  width: 64px;
  height: 64px;
  background: linear-gradient(135deg, var(--cf-orange), #FAAE40);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  color: #fff;
  box-shadow: 0 8px 24px var(--cf-orange-glow);
}

.welcome-card h1 {
  font-size: 1.75rem;
  font-weight: 700;
}

.welcome-card p {
  color: var(--text-muted);
  font-size: 0.95rem;
}

.sample-prompts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 0.75rem;
  width: 100%;
  margin-top: 1rem;
}

.prompt-chip {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 0.85rem 1rem;
  border-radius: var(--radius-md);
  color: var(--text-main);
  font-size: 0.875rem;
  text-align: left;
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  box-shadow: var(--shadow-sm);
}

.prompt-chip:hover {
  border-color: var(--cf-orange);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.prompt-chip i {
  color: var(--cf-orange);
}

/* Message Item */
.message-row {
  display: flex;
  gap: 1rem;
  max-width: 100%;
  animation: fadeIn 0.3s ease;
}

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

.message-row.user {
  flex-direction: row-reverse;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}

.message-row.user .avatar {
  background: #475569;
  color: #ffffff;
}

.message-row.assistant .avatar {
  background: linear-gradient(135deg, var(--cf-orange), #FAAE40);
  color: #ffffff;
}

.message-bubble {
  max-width: 80%;
  padding: 0.85rem 1.15rem;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  line-height: 1.6;
  position: relative;
  word-break: break-word;
}

.message-row.user .message-bubble {
  background-color: var(--user-msg-bg);
  color: var(--user-msg-text);
  border-bottom-right-radius: 4px;
}

.message-row.assistant .message-bubble {
  background-color: var(--bot-msg-bg);
  border: 1px solid var(--border-color);
  border-bottom-left-radius: 4px;
}

/* Code Blocks & Markdown */
.message-bubble p {
  margin-bottom: 0.75rem;
}

.message-bubble p:last-child {
  margin-bottom: 0;
}

.message-bubble pre {
  background-color: #090d16;
  color: #f8fafc;
  padding: 1rem;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 0.75rem 0;
  position: relative;
  font-size: 0.875rem;
}

.message-bubble code {
  font-family: 'Fira Code', monospace, Consolas;
}

.copy-code-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #94a3b8;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: var(--transition);
}

.copy-code-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #ffffff;
}

/* Typing Dots Animation */
.typing-dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 4px 0;
}

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

.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1.0); }
}

/* Input Area */
.input-area {
  padding: 1rem 15% 1.5rem 15%;
  background: var(--bg-main);
}

@media (max-width: 992px) {
  .input-area {
    padding: 1rem 5% 1.5rem 5%;
  }
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 0.6rem 0.8rem;
  box-shadow: var(--shadow-md);
  transition: var(--transition);
}

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

#user-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-main);
  font-size: 0.95rem;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 160px;
  padding: 0.4rem 0.5rem;
}

.btn-send {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.input-footer {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
  padding: 0 0.5rem;
}

/* Forms & Buttons */
.btn {
  border: none;
  padding: 0.6rem 1.2rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.btn-primary {
  background-color: var(--cf-orange);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--cf-orange-hover);
  box-shadow: 0 4px 12px var(--cf-orange-glow);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-ghost {
  background: transparent;
  color: var(--text-main);
}

.btn-ghost:hover {
  background: rgba(148, 163, 184, 0.15);
}

.btn-block {
  width: 100%;
  justify-content: flex-start;
}

.btn-sm {
  padding: 0.35rem 0.7rem;
  font-size: 0.8rem;
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
}

.form-select, .form-control {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background-color: var(--input-bg);
  color: var(--text-main);
  font-size: 0.875rem;
  outline: none;
  transition: var(--transition);
}

.form-select:focus, .form-control:focus {
  border-color: var(--cf-orange);
}

/* Modal Dialog */
.settings-modal {
  margin: auto;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  background: var(--bg-card);
  color: var(--text-main);
  padding: 1.5rem;
  width: 90%;
  max-width: 500px;
  box-shadow: var(--shadow-md);
}

.settings-modal::backdrop {
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

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

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  font-size: 0.875rem;
  font-weight: 500;
}

.form-range {
  accent-color: var(--cf-orange);
}

.range-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.modal-footer {
  margin-top: 1.5rem;
  display: flex;
  justify-content: flex-end;
}

/* Mobile Responsiveness */
.mobile-only {
  display: none;
}

@media (max-width: 768px) {
  .mobile-only {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: -100%;
    height: 100vh;
    box-shadow: var(--shadow-md);
  }

  .sidebar.open {
    left: 0;
  }

  .sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    z-index: 90;
    display: none;
  }

  .sidebar-overlay.show {
    display: block;
  }
}
