:root {
  /* Palette: Indigo / Slate */
  --primary: #4F46E5;
  /* Indigo 600 */
  --primary-dark: #4338ca;
  /* Indigo 700 */
  --primary-light: #e0e7ff;
  /* Indigo 100 */
  --on-primary: #ffffff;

  --bg: #f8fafc;
  /* Slate 50 */
  --card: #ffffff;
  --text: #1e293b;
  /* Slate 800 */
  --text-muted: #64748b;
  /* Slate 500 */
  --border: #e2e8f0;
  /* Slate 200 */

  --danger: #ef4444;
  /* Red 500 */
  --success: #22c55e;
  /* Green 500 */
  --warning: #f59e0b;
  /* Amber 500 */

  /* Material Icons Global Styles */
  .material-icons {
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    word-wrap: normal;
    direction: ltr;
    vertical-align: middle;
  }

  .logo-icon {
    font-size: 1.5rem;
    margin-right: 8px;
  }

  .dropdown-arrow-icon {
    font-size: 1.2rem;
    opacity: 0.6;
  }

  .check-icon {
    font-size: 20px;
    color: var(--primary);
  }

  /* Model Dropdown Specific */
  .model-dropdown-divider {
    height: 1px;
    background: var(--border);
    margin: 4px 0;
  }

  .model-toggle {
    color: var(--primary);
    font-weight: 500;
  }

  .model-toggle:hover {
    background: var(--primary-light) !important;
  }

  /* Spacing & Radius */
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1),
  0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1),
  0 4px 6px -4px rgb(0 0 0 / 0.1);

  --font-sans: 'Inter',
  system-ui,
  -apple-system,
  sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  -webkit-font-smoothing: antialiased;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  /* Prevent body scroll */
  height: 100vh;
  /* Full viewport height */
}

#app {
  height: 100vh;
  overflow: hidden;
  /* Prevent app scroll */
  display: flex;
  flex-direction: column;
}

.container {
  height: 100%;
  max-width: 800px;
  /* Slightly narrower for better reading */
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
}

.page {
  flex: 1;
  min-height: 0;
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(5px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Typography */
h1,
h2,
h3,
h4 {
  margin: 0 0 16px 0;
  font-weight: 600;
  letter-spacing: -0.025em;
}

h2 {
  font-size: 1.5rem;
}

h3 {
  font-size: 1.25rem;
}

/* Cards */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  margin-bottom: 16px;
  transition: transform 0.2s, box-shadow 0.2s;
}

/* Forms */
.form label {
  display: block;
  margin-bottom: 6px;
  color: var(--text-muted);
  font-size: 0.875rem;
  font-weight: 500;
}

.form input:not([type="checkbox"]):not([type="radio"]),
.form textarea,
.form select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  transition: border-color 0.2s, box-shadow 0.2s;
  margin-bottom: 16px;
}

.form .row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.form .row input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.form .row span {
  font-weight: 500;
  color: var(--text);
}

.form input:focus,
.form textarea:focus,
.form select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

/* Utilities */
.hidden {
  display: none !important;
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 80px 24px 24px;
  color: var(--text-muted);
  font-size: 1rem;
  min-height: 300px;
}

.empty-icon {
  font-size: 64px;
  color: var(--border);
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}

.empty-message {
  font-size: 0.95rem;
  color: var(--text-muted);
  max-width: 400px;
}

/* Buttons */
.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-weight: 500;
  font-size: 0.95rem;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

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

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

.btn.primary {
  background: var(--primary);
  color: var(--on-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

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

.btn.secondary {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn.secondary:hover {
  background: var(--bg);
  border-color: #cbd5e1;
}

.btn.link {
  background: transparent;
  color: var(--primary);
  padding: 8px 12px;
}

.btn.link:hover {
  background: var(--primary-light);
}

.btn.danger {
  background: #fee2e2;
  color: var(--danger);
}

.btn.danger:hover {
  background: #fecaca;
}

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

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

/* TopBar */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 12px;
  z-index: 100;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.topbar .logo {
  font-size: 1.5rem;
  margin-right: 8px;
}

.topbar .title {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
}

.tb-left {
  display: flex;
  gap: 8px;
  align-items: center;
}

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

.tb-right {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* Mobile responsive: hide text labels, keep only icons */
@media (max-width: 600px) {
  .selector-label {
    display: none !important;
  }

  .selector-btn {
    min-width: unset !important;
    padding: 8px 12px !important;
    gap: 4px !important;
  }

  .dropdown-arrow-icon {
    margin-left: 0 !important;
  }

  .tb-left,
  .tb-right {
    gap: 4px;
  }
}

/* Connection & Thread Selectors */
.connection-selector,
.thread-selector {
  position: relative;
}

.selector-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 180px;
}

.selector-btn:hover {
  background: var(--bg);
  border-color: var(--primary);
}

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

.selector-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.selector-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  min-width: 250px;
  max-width: 350px;
  z-index: 1000;
  overflow: hidden;
  max-height: 400px;
  overflow-y: auto;
}

.selector-dropdown.hidden {
  display: none;
}

.selector-option {
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  gap: 12px;
}

.selector-option:hover {
  background: var(--bg);
}

.selector-option.active {
  background: var(--primary-light);
  color: var(--primary-dark);
  font-weight: 500;
}

.selector-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.selector-option.create {
  color: var(--primary);
  font-weight: 500;
  border-top: 1px solid var(--border);
}

.selector-option.create:hover {
  background: var(--primary-light);
}

/* Model Selector */
.model-selector {
  position: relative;
}

.model-selector-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--primary-light);
  border: 1px solid var(--primary);
  border-radius: var(--radius-sm);
  color: var(--primary-dark);
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.model-selector-btn:hover {
  background: var(--primary);
  color: #fff;
}

.model-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  z-index: 1000;
  overflow: hidden;
}

.model-dropdown.hidden {
  display: none;
}

.model-option {
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  gap: 8px;
}

.model-option:hover {
  background: var(--bg);
}

.model-option.active {
  background: var(--primary-light);
  color: var(--primary-dark);
  font-weight: 500;
}

/* User Menu */
.user-menu {
  position: relative;
}

.user-menu-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.user-menu-btn:hover {
  background: var(--bg);
  border-color: var(--primary);
}

.user-name {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dropdown-arrow {
  font-size: 0.7rem;
  opacity: 0.6;
}

.user-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  min-width: 240px;
  z-index: 1000;
  overflow: hidden;
}

.user-dropdown.hidden {
  display: none;
}

.user-info {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.user-info-name {
  font-weight: 600;
  margin-bottom: 4px;
}

.user-info-email {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.user-credits {
  padding: 12px 16px;
  background: var(--bg);
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  color: var(--primary-dark);
}

.menu-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.menu-item {
  width: 100%;
  padding: 12px 16px;
  border: none;
  background: transparent;
  text-align: left;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.95rem;
  color: var(--text);
}

.menu-item:hover {
  background: var(--bg);
}

/* Mobile Responsive */
@media (max-width: 768px) {
  .topbar {
    flex-wrap: wrap;
  }

  .tb-center {
    order: 3;
    flex-basis: 100%;
    justify-content: flex-start;
    margin-top: 8px;
  }

  .user-name {
    display: none;
  }

  .model-selector-btn span:nth-child(2) {
    display: none;
  }

  .model-selector-btn span:first-child {
    margin: 0;
  }
}


/* Lists */
.list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.list .row.card {
  margin: 0;
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.list .row.card:hover {
  border-color: var(--primary-light);
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s;
}

.pill:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.pill.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* Thread List (Vertical) */
.thread-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
}

.thread-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.thread-item:hover {
  border-color: var(--primary);
  background: var(--primary-light);
  transform: translateX(4px);
}

.thread-item.active {
  border-color: var(--primary);
  background: var(--primary-light);
}

.thread-item-main {
  flex: 1;
}

.thread-item-name {
  font-weight: 600;
  margin-bottom: 4px;
}

.thread-item-model {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.thread-item-arrow {
  color: var(--text-muted);
  transition: transform 0.2s;
}

.thread-item:hover .thread-item-arrow {
  transform: translateX(4px);
  color: var(--primary);
}

/* Chat */
.chatwrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

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

.msg {
  max-width: min(85%, 800px);
  /* Adapts to viewport, max 800px on wide screens */
  width: 100%;
  /* Full width within container */
  display: flex;
  flex-direction: column;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

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

.msg.bot {
  align-self: flex-start;
  align-items: flex-start;
}

.msg .who {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 4px;
  padding: 0 4px;
}

.msg .bubble {
  padding: 12px 16px;
  border-radius: 18px;
  box-shadow: var(--shadow-sm);
  white-space: pre-wrap;
  line-height: 1.5;
  font-size: 0.95rem;
  position: relative;
  max-width: 100%;
  /* Fit within parent */
  box-sizing: border-box;
  overflow-x: auto;
  /* Allow horizontal scroll if needed */
}

.msg.user .bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.bot .bubble {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

/* Credit warning banner */
.credit-warning {
  background: #fee;
  border: 1px solid #f88;
  border-radius: 8px;
  padding: 8px 16px;
  margin: 12px 0;
  display: flex;
  align-items: center;
  gap: 12px;
  color: #c00;
  font-weight: 500;
}

.credit-warning .material-icons {
  font-size: 24px;
  color: #c00;
}

/* Markdown content in bubbles */
.msg.bot .bubble p {
  margin: 0.5em 0;
}

.msg.bot .bubble p:first-child {
  margin-top: 0;
}

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

/* Table wrapper for horizontal scroll on mobile */
.msg.bot .bubble .table-scroll-wrapper {
  width: 100%;
  overflow-x: auto;
  overflow-y: visible;
  -webkit-overflow-scrolling: touch;
  /* Smooth scrolling on iOS */
  margin: 12px 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
}

/* Table export button - positioned below the table */
.table-export-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 8px;
  margin-left: 0;
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 12px;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.2s;
  font-size: 0.875rem;
  gap: 6px;
}

.table-export-btn:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.table-export-btn .material-icons {
  font-size: 18px;
}

/* Tables */
.msg.bot .bubble table {
  width: 100%;
  min-width: 500px;
  /* Ensure table doesn't shrink too much */
  border-collapse: collapse;
  margin: 0;
  /* Margin is on wrapper now */
  font-size: 0.9em;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  overflow: hidden;
}

@media (max-width: 600px) {
  .msg.bot .bubble table {
    min-width: 600px;
    /* Force table to be wider than viewport on mobile */
    font-size: 0.85em;
    /* Slightly smaller text on mobile */
  }
}

.msg.bot .bubble table thead {
  background: #e9ecef;
  color: var(--text);
}

.msg.bot .bubble table th {
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  font-size: 0.85em;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  white-space: nowrap;
}

.msg.bot .bubble table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: normal;
  /* Allow text wrapping in cells if needed */
}

.msg.bot .bubble table tbody tr:nth-child(even) {
  background: #f8f9fa;
}

.msg.bot .bubble table tbody tr:hover {
  background: #f0f1f3;
}

.msg.bot .bubble table tbody tr:last-child td {
  border-bottom: none;
}

/* Lists */
.msg.bot .bubble ul,
.msg.bot .bubble ol {
  margin: 8px 0;
  padding-left: 24px;
}

.msg.bot .bubble li {
  margin: 4px 0;
  line-height: 1.6;
}

/* Code blocks */
.msg.bot .bubble code {
  background: #f5f5f5;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 0.9em;
  color: #d63384;
}

.msg.bot .bubble pre {
  background: #f8f9fa;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
}

.msg.bot .bubble pre code {
  background: none;
  padding: 0;
  color: inherit;
}

/* Blockquotes */
.msg.bot .bubble blockquote {
  border-left: 4px solid var(--primary);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--text-muted);
  font-style: italic;
}

/* Headings */
.msg.bot .bubble h1,
.msg.bot .bubble h2,
.msg.bot .bubble h3,
.msg.bot .bubble h4 {
  margin: 12px 0 8px 0;
  font-weight: 600;
  line-height: 1.3;
}

.msg.bot .bubble h1 {
  font-size: 1.5em;
}

.msg.bot .bubble h2 {
  font-size: 1.3em;
}

.msg.bot .bubble h3 {
  font-size: 1.15em;
}

.msg.bot .bubble h4 {
  font-size: 1.05em;
}

/* Links */
.msg.bot .bubble a {
  color: var(--primary);
  text-decoration: underline;
}

.msg.bot .bubble a:hover {
  color: var(--primary-dark);
}

/* Horizontal rule */
.msg.bot .bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}


/* Composer */
/* Chat wrapper - full viewport with flex layout, accounting for fixed TopBar */
.chatwrap {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding-top: 60px;
  /* Space for fixed TopBar */
  overflow: hidden;
}

/* Messages container - scrollable area */
.messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Composer - fixed at bottom of chatwrap */
.composer {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 12px;
  padding: 16px 24px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
}

@media (max-width: 600px) {
  .composer {
    padding: 12px 16px;
    padding-bottom: 80px;
    /* Fixed large padding for Android/iOS nav bars */
  }
}

.composer textarea {
  flex: 1;
  min-height: 48px;
  max-height: 150px;
  resize: none;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;
  overflow-y: auto;
  transition: border-color 0.2s;
}

.composer textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.send-btn {
  width: 48px;
  height: 48px;
  min-width: 48px;
  min-height: 48px;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
  background: var(--primary-dark);
  transform: scale(1.05);
}

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

.send-btn svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}


/* Utilities */
.text-center {
  text-align: center;
}

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

.mt-4 {
  margin-top: 16px;
}

.mb-4 {
  margin-bottom: 16px;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.w-full {
  width: 100%;
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: #333;
  color: #fff;
  padding: 12px 20px;
  border-radius: 50px;
  box-shadow: var(--shadow-lg);
  font-size: 0.9rem;
  opacity: 0;
  transform: translateY(20px);
  animation: slideUp 0.3s forwards;
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast.error {
  background: var(--danger);
}

.toast.success {
  background: var(--success);
}

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

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(2px);
  animation: fadeIn 0.2s;
}

.modal {
  background: #fff;
  border-radius: var(--radius);
  padding: 24px;
  width: 90%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
  animation: scaleIn 0.2s;
}

.modal h3 {
  margin-top: 0;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

@keyframes scaleIn {
  from {
    transform: scale(0.95);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

/* Thinking Dots Animation */
.bubble.thinking {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 12px 20px;
  min-width: 60px;
}

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

.bubble.thinking .dot:nth-child(1) {
  animation-delay: -0.32s;
}

.bubble.thinking .dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes thinkingBounce {

  0%,
  80%,
  100% {
    transform: scale(0.8);
    opacity: 0.5;
  }

  40% {
    transform: scale(1.2);
    opacity: 1;
  }
}

/* Spinner */
.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ==================== */
/* Modal Styles         */
/* ==================== */

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
  animation: fadeIn 0.2s ease-out;
}

.modal-container {
  background: white;
  border-radius: 12px;
  max-width: 500px;
  width: 90%;
  max-height: 85vh;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: modalSlideIn 0.3s ease-out;
  display: flex;
  flex-direction: column;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

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

.modal-header h2 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
}

.modal-close {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.modal-close:hover {
  background: var(--bg);
  color: var(--text);
}

.modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  flex-shrink: 0;
}

/* Form styles within modal */
.form-group {
  margin-bottom: 20px;
}

.form-group:last-child {
  margin-bottom: 0;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  font-size: 0.9rem;
  color: var(--text);
}

.form-group label .required {
  color: var(--danger);
  margin-left: 4px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.95rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.form-group input.error {
  border-color: var(--danger);
}

.form-error {
  margin-top: 6px;
  font-size: 0.85rem;
  color: var(--danger);
  display: none;
}

.form-error.visible {
  display: block;
}

.form-group small {
  display: block;
  margin-top: 6px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 16px;
}

.modal {
  background: white;
  border-radius: var(--radius);
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  max-width: 500px;
  width: 100%;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

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

.modal-header h3 {
  margin: 0;
  font-size: 1.25rem;
}

.modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.modal-footer .btn {
  min-width: 100px;
}

/* =========================
   BETA Badge
   ========================= */
.beta-badge-inline {
  display: inline-flex;
  align-items: center;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

/* =========================
   Required Field Validation
   ========================= */
input:required:invalid,
textarea:required:invalid,
select:required:invalid {
  border-color: var(--danger, #dc3545);
}

input:required:valid,
textarea:required:valid,
select:required:valid {
  border-color: var(--border, #ddd);
}

input:required:focus:invalid,
textarea:required:focus:invalid,
select:required:focus:invalid {
  border-color: var(--danger, #dc3545);
  box-shadow: 0 0 0 3px rgba(220, 53, 69, 0.1);
}