/* KiloPlayground UI Styles
   Theme: Dark with Purple Accent (#8A2BE2)
   Layout: Qwen-style generous spacing + ChatGPT-style sidebar and bubbles
   Mobile-first, responsive
*/

:root {
  --bg: #202123;
  --bg-elev: #2a2b32;
  --bg-elev-2: #32333b;
  --text: #f5f6f7;
  --text-dim: #c8cbd0;
  --muted: #8a8f98;
  --accent: #8A2BE2; /* Kilo purple */
  --accent-2: #a869ee;
  --danger: #ff5e5e;
  --ok: #2ecc71;

  --bubble-user-bg: linear-gradient(135deg, rgba(138,43,226,0.18), rgba(138,43,226,0.08));
  --bubble-assistant-bg: #2a2b32;

  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;

  --header-h: 56px;
  --sidebar-w: 260px;
  --gap: 16px;
  --shadow-1: 0 4px 16px rgba(0,0,0,0.25);
  --shadow-2: 0 6px 24px rgba(0,0,0,0.35);
  --ring: 0 0 0 2px rgba(138,43,226,0.45);

  --font: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
}

*,
*::before,
*::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  background: radial-gradient(1200px 800px at 20% -10%, rgba(138,43,226,0.08), transparent 60%) ,
              radial-gradient(1200px 800px at 110% 10%, rgba(138,43,226,0.06), transparent 60%) ,
              var(--bg);
  color: var(--text);
}

.dark { background: inherit; color: inherit; }

.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 14px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.00));
  -webkit-backdrop-filter: saturate(120%) blur(6px);
  backdrop-filter: saturate(120%) blur(6px);
  position: sticky;
  top: 0;
  z-index: 5;
}

.app-header .logo {
  font-weight: 700;
  letter-spacing: 0.2px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-size: 18px;
}

.app-header .spacer { flex: 1; }

.app-shell {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
  height: calc(100vh - var(--header-h));
}

/* Sidebar (hidden on small screens, visible >= 960px) */
.sidebar {
  display: none;
  padding: 14px;
  background: linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0.00));
  border-right: 1px solid rgba(255,255,255,0.06);
}

.new-chat {
  width: 100%;
  padding: 10px 12px;
  background: linear-gradient(180deg, rgba(138,43,226,0.22), rgba(138,43,226,0.12));
  color: var(--text);
  border: 1px solid rgba(138,43,226,0.35);
  border-radius: var(--radius-md);
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}
.new-chat:hover { box-shadow: var(--ring); }
.new-chat:active { transform: translateY(1px) scale(0.995); }

.chat-list {
  list-style: none;
  margin: 12px 0 0 0;
  padding: 0;
  display: grid;
  gap: 8px;
  max-height: calc(100% - 60px);
  overflow-y: auto;
  scrollbar-width: thin;
}
.chat-item {
  padding: 10px 12px;
  border-radius: var(--radius-md);
  background: rgba(255,255,255,0.03);
  color: var(--text-dim);
  border: 1px solid rgba(255,255,255,0.06);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.chat-item:hover {
  background: rgba(138,43,226,0.12);
  color: var(--text);
  border-color: rgba(138,43,226,0.35);
}

/* Chat list actions */
.chat-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.chat-item-actions button {
  padding: 4px 6px;
  font-size: 12px;
  line-height: 1;
  border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.10);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.08s ease;
}
.chat-item-actions button:hover {
  border-color: rgba(138,43,226,0.45);
  background: rgba(138,43,226,0.10);
}
.chat-item-actions button:active {
  transform: translateY(1px) scale(0.995);
}

/* Icon-specific accents */
.chat-rename { }
.chat-delete { color: var(--danger); border-color: rgba(255,94,94,0.35); }
.chat-delete:hover { background: rgba(255,94,94,0.10); border-color: rgba(255,94,94,0.55); }

/* Chat area */
.chat-area {
  display: grid;
  grid-template-rows: 1fr auto auto;
  min-height: 100%;
}

.messages {
  padding: 22px;
  overflow-y: auto;
  scroll-behavior: smooth;
  display: grid;
  align-content: start;
  gap: 16px;
}

.bubble {
  max-width: 820px;
  line-height: 1.65;
  padding: 12px 40px 12px 14px; /* extra right space for copy button */
  border-radius: var(--radius-lg);
  word-wrap: break-word;
  word-break: break-word;
  border: 1px solid rgba(255,255,255,0.08);
  box-shadow: var(--shadow-1);
  position: relative;
}
.bubble.user {
  margin-left: auto;
  background: var(--bubble-user-bg);
  border-color: rgba(138,43,226,0.35);
}
.bubble.assistant {
  margin-right: auto;
  background: var(--bubble-assistant-bg);
  border-color: rgba(255,255,255,0.10);
}
.bubble code, .bubble pre {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  background: rgba(255,255,255,0.06);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
}

/* Copy button inside bubbles */
.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 6px;
  font-size: 12px;
  line-height: 1;
  border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.06);
  color: var(--text-dim);
  border: 1px solid rgba(255,255,255,0.10);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.08s ease, color 0.15s ease;
}
.copy-btn:hover {
  border-color: rgba(138,43,226,0.45);
  background: rgba(138,43,226,0.10);
  color: var(--text);
}
.copy-btn.copied {
  background: rgba(46, 204, 113, 0.20);
  border-color: rgba(46, 204, 113, 0.45);
  color: #caffd7;
}

/* Composer */
.composer {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid rgba(255,255,255,0.06);
  background: linear-gradient(180deg, rgba(255,255,255,0.00), rgba(255,255,255,0.03));
}

.composer input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius-md);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.composer input[type="text"]:focus {
  border-color: rgba(138,43,226,0.50);
  box-shadow: var(--ring);
}

.toggle-settings {
  padding: 10px 12px;
  background: rgba(255,255,255,0.06);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: var(--radius-md);
  cursor: pointer;
}
.toggle-settings:hover {
  border-color: rgba(138,43,226,0.45);
}

.send {
  padding: 10px 16px;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.15s ease;
}
.send:hover { filter: brightness(1.08); }
.send:active { transform: translateY(1px) scale(0.995); }

/* Settings panel */
.settings-panel {
  border-top: 1px solid rgba(255,255,255,0.06);
  background: rgba(255,255,255,0.03);
  padding: 12px;
  display: grid;
  gap: 10px;
}

.settings-panel.hidden { display: none; }

.settings-row {
  display: grid;
  gap: 6px;
}

.settings-row label {
  font-size: 12px;
  color: var(--muted);
}

.settings-row select,
.settings-row input[type="range"],
.settings-row input[type="text"],
.settings-row input[type="number"],
.settings-row textarea {
  width: 100%;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.settings-row select:focus,
.settings-row input[type="range"]:focus,
.settings-row input[type="text"]:focus,
.settings-row input[type="number"]:focus,
.settings-row textarea:focus {
  border-color: rgba(138,43,226,0.50);
  box-shadow: var(--ring);
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--text-dim);
}

/* Scrollbar tweaks */
.messages::-webkit-scrollbar,
.chat-list::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
.messages::-webkit-scrollbar-thumb,
.chat-list::-webkit-scrollbar-thumb {
  background-color: rgba(255,255,255,0.12);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: padding-box;
}

/* Responsive layout */
@media (min-width: 960px) {
  .app-shell {
    grid-template-columns: var(--sidebar-w) 1fr;
  }
  .sidebar { display: block; }
  .messages {
    padding: 26px 32px;
  }
  .composer {
    padding: 16px 18px;
    grid-template-columns: auto 1fr auto;
  }
  .settings-panel {
    padding: 16px 18px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .settings-row { min-width: 240px; }
}

/* Mobile spacing refinements */
@media (max-width: 359px) {
  .composer input[type="text"] { font-size: 14px; }
  .send, .toggle-settings { padding: 8px 10px; }
}

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

/* Help tooltip */
.help {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-left: 6px;
  border-radius: 50%;
  border: 1px solid rgba(138,43,226,0.65);
  color: var(--accent);
  font-weight: 700;
  font-size: 10px;
  line-height: 1;
  cursor: help;
  position: relative;
}
.help::after {
  content: attr(data-tip);
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(100% + 8px);
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-2);
  padding: 8px 10px;
  font-size: 12px;
  max-width: 320px;
  white-space: normal;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease-in-out;
  z-index: 20;
}
.help::before {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(100% + 6px);
  border: 6px solid transparent;
  border-top-color: var(--bg-elev-2);
  opacity: 0;
  transition: opacity 120ms ease-in-out;
  z-index: 21;
}
.help:hover::after,
.help:hover::before {
  opacity: 1;
}

/* Align label contents (icon + text) */
.settings-row label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* Ensure text/number inputs in settings rows match theming */
.settings-row input[type="text"],
.settings-row input[type="number"] {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.08);
}

/* Message bubble monospace tweak for code blocks */
.bubble pre {
  white-space: pre-wrap;
  overflow-x: auto;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.10);
  padding: 10px 12px;
  border-radius: var(--radius-md);
  margin: 8px 0;
}

/* Accessibility Styles */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.keyboard-focus {
  outline: 2px solid var(--purple-500);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(147, 51, 234, 0.3);
}

.keyboard-focus[data-focused="true"] {
  outline-color: var(--purple-600);
  box-shadow: 0 0 0 6px rgba(147, 51, 234, 0.4);
}

/* Skip Links */
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  background: var(--gradient-primary);
  color: white;
  padding: 8px 16px;
  text-decoration: none;
  border-radius: 0 0 8px 8px;
  z-index: 1000;
  transition: top 0.3s ease;
}

.skip-link:focus {
  top: 0;
}

/* High Contrast Mode */
.high-contrast {
  --bg: #000000;
  --bg-elev: #1a1a1a;
  --bg-elev-2: #2d2d2d;
  --text: #ffffff;
  --text-dim: #e0e0e0;
  --muted: #a0a0a0;
  --accent: #b366ff;
  --accent-2: #c77dff;
  --bubble-user-bg: linear-gradient(135deg, rgba(179, 102, 255, 0.18), rgba(179, 102, 255, 0.08));
  --bubble-assistant-bg: #1a1a1a;
}

/* Reduced Motion */
.reduced-motion * {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}

.reduced-motion *,
.reduced-motion::before,
.reduced-motion::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}

/* Focus Indicators */
.bubble.selected {
  border: 2px solid var(--purple-500);
  box-shadow: 0 0 0 4px rgba(147, 51, 234, 0.3);
}

/* Shortcuts Help Modal */
.shortcuts-help {
  max-height: 80vh;
  overflow-y: auto;
}

.shortcuts-section {
  margin-bottom: 24px;
}

.shortcuts-section h3 {
  margin: 0 0 16px 0;
  color: var(--text-primary);
  font-size: 18px;
  border-bottom: 1px solid rgba(147, 51, 234, 0.2);
  padding-bottom: 8px;
}

.shortcut-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 12px;
}

.shortcut-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--bg-surface);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  transition: all 0.2s ease;
}

.shortcut-item:hover {
  background: rgba(147, 51, 234, 0.1);
  border-color: rgba(147, 51, 234, 0.3);
}

kbd {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 4px;
  padding: 4px 8px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.shortcut-item span {
  color: var(--text-secondary);
  font-size: 14px;
}

.shortcuts-actions {
  margin-top: 24px;
  text-align: center;
}

/* Enhanced Button Focus */
button:focus,
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--purple-500);
  outline-offset: 2px;
}

/* Enhanced ARIA Support */
[role="button"]:not(:disabled):hover {
  cursor: pointer;
}

[role="button"]:not(:disabled):focus {
  outline: 2px solid var(--purple-500);
}

/* Toast Notifications for Screen Readers */
.toast[role="alert"] {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1001;
}

/* Responsive Accessibility */
@media (max-width: 768px) {
  .shortcuts-help {
    max-height: 90vh;
    width: 95%;
  }
  
  .shortcut-list {
    grid-template-columns: 1fr;
  }
  
  .skip-link {
    font-size: 14px;
    padding: 6px 12px;
  }
}

/* Print Styles */
@media print {
  .modal,
  .search-suggestions,
  .toolbar {
    display: none !important;
  }
  
  .chat-area {
    display: block !important;
  }
  
  .bubble {
    break-inside: avoid;
    orphans: 2;
    widows: 2;
  }
}

/* Search and Filter Styles */
.search-container {
  position: relative;
  flex: 1;
  max-width: 400px;
}

.global-search {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  font-size: 14px;
  transition: all 0.2s ease;
}

.global-search:focus {
  border-color: var(--purple-500);
  box-shadow: 0 0 0 2px rgba(147, 51, 234, 0.2);
  outline: none;
}

.search-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.search-btn:hover {
  color: var(--text-primary);
  background: rgba(147, 51, 234, 0.1);
}

/* Search Suggestions */
.search-suggestions {
  background: var(--bg-elevated);
  border: 1px solid rgba(147, 51, 234, 0.3);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  max-height: 300px;
  overflow-y: auto;
  z-index: 1000;
}

.search-suggestion {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  border-bottom: 1px solid transparent;
}

.search-suggestion:hover {
  background: rgba(147, 51, 234, 0.1);
}

.search-suggestion.selected {
  background: rgba(147, 51, 234, 0.2);
  border-bottom-color: rgba(147, 51, 234, 0.5);
}

.suggestion-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.suggestion-content {
  flex: 1;
  min-width: 0;
}

.suggestion-text {
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.suggestion-type {
  color: var(--text-muted);
  font-size: 12px;
  background: rgba(147, 51, 234, 0.2);
  padding: 2px 6px;
  border-radius: 10px;
  margin-left: 8px;
}

/* Enhanced Sidebar Sections */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.section-actions {
  display: flex;
  gap: 6px;
}

.filter-buttons {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}

.filter-btn {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-secondary);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 4px;
}

.filter-btn:hover {
  background: rgba(147, 51, 234, 0.1);
  border-color: rgba(147, 51, 234, 0.3);
  color: var(--text-primary);
}

.filter-btn.active {
  background: var(--gradient-primary);
  border-color: var(--purple-500);
  color: white;
}

.clear-filters-btn {
  background: rgba(239, 68, 68, 0.1);
  border-color: rgba(239, 68, 68, 0.3);
  color: var(--error);
}

.clear-filters-btn:hover {
  background: rgba(239, 68, 68, 0.2);
}

.sort-btn {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-secondary);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 4px;
}

.sort-btn:hover {
  background: rgba(147, 51, 234, 0.1);
  border-color: rgba(147, 51, 234, 0.3);
  color: var(--text-primary);
}

.sort-btn.active {
  background: var(--gradient-primary);
  border-color: var(--purple-500);
  color: white;
}

.sort-order {
  font-size: 10px;
  font-weight: bold;
}

/* Enhanced Chat Items */
.chat-item {
  position: relative;
}

.chat-item.active {
  background: rgba(147, 51, 234, 0.15);
  border-color: rgba(147, 51, 234, 0.4);
}

.bookmark-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.bookmark-btn:hover {
  color: var(--purple-400);
  background: rgba(147, 51, 234, 0.1);
}

/* Search Highlight */
.search-highlight {
  background: rgba(255, 215, 0, 0.3);
  color: var(--text-primary);
  padding: 2px 4px;
  border-radius: 4px;
  font-weight: 600;
}

/* Empty State */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.6;
}

.empty-state h3 {
  margin: 0 0 8px 0;
  color: var(--text-secondary);
  font-size: 18px;
}

.empty-state p {
  margin: 0;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.5;
}

/* Responsive Search */
@media (max-width: 768px) {
  .search-container {
    max-width: 200px;
  }
  
  .filter-buttons {
    grid-template-columns: 1fr;
  }
  
  .section-actions {
    flex-wrap: wrap;
  }
  
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
}