/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f7f7f8;
  --bg-sidebar: #f7f7f8;
  --bg-hover: #ececec;
  --bg-input: #f4f4f4;
  --bg-user-bubble: #f4f4f4;
  --bg-assistant-bubble: transparent;
  --bg-card: #f4f4f4;
  --bg-card-hover: #e8e8e8;

  --text-primary: #1a1a1a;
  --text-secondary: #6b6b7b;
  --text-muted: #9b9ba4;
  --text-link: #10a37f;

  --border-color: #e5e5e5;
  --border-input: #d1d1d1;

  --accent: #10a37f;
  --accent-hover: #0d8c6d;
  --accent-disabled: #b8e2d4;

  --send-btn-bg: #1a1a1a;
  --send-btn-color: #fff;
  --send-btn-disabled-bg: #d1d1d1;
  --send-btn-disabled-color: #9b9ba4;

  --sidebar-width: 260px;
  --topbar-height: 52px;

  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 18px;

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', Consolas, monospace;

  --transition: 0.15s ease;
  --transition-slow: 0.25s ease;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.6;
}

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

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform var(--transition-slow);
  z-index: 100;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 14px 12px;
  border-bottom: 1px solid var(--border-color);
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--text-primary);
  font-weight: 600;
  font-size: 16px;
  letter-spacing: -0.3px;
}

.logo:hover { color: var(--accent); }

.logo-icon {
  font-size: 20px;
  line-height: 1;
}

.close-sidebar-btn {
  display: none;
  font-size: 14px;
}

/* New Chat Button */
.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 10px 8px;
  padding: 9px 14px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 14px;
  font-family: var(--font);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.new-chat-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-input);
}

.new-chat-icon {
  font-size: 18px;
  line-height: 1;
  color: var(--text-secondary);
}

/* Recents */
.recents-section {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.recents-section::-webkit-scrollbar { width: 4px; }
.recents-section::-webkit-scrollbar-track { background: transparent; }
.recents-section::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 2px; }

.recents-label {
  padding: 8px 14px 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
}

.recents-list {
  list-style: none;
  padding: 0 6px;
}

.recents-list li {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background var(--transition), color var(--transition);
  user-select: none;
}

.recents-list li:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.recents-list li.active {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* ===== Main Area ===== */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-primary);
  min-width: 0;
}

/* Topbar (mobile only by default) */
.topbar {
  display: none;
  align-items: center;
  height: var(--topbar-height);
  padding: 0 12px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-primary);
  gap: 8px;
  flex-shrink: 0;
}

.topbar-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Icon button base */
.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  font-size: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: color var(--transition), background var(--transition);
  line-height: 1;
}

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

.hamburger-btn span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text-secondary);
  border-radius: 1px;
  transition: background var(--transition);
}

.hamburger-btn:hover span { background: var(--text-primary); }

/* Messages Container */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 8px;
  scroll-behavior: smooth;
}

.messages-container::-webkit-scrollbar { width: 6px; }
.messages-container::-webkit-scrollbar-track { background: transparent; }
.messages-container::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }
.messages-container::-webkit-scrollbar-thumb:hover { background: var(--border-input); }

/* Welcome Screen */
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  text-align: center;
  padding: 40px 24px;
  animation: fadeIn 0.4s ease;
}

.welcome-icon { font-size: 48px; margin-bottom: 16px; }

.welcome-title {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-bottom: 10px;
  color: var(--text-primary);
}

.welcome-subtitle {
  font-size: 15px;
  color: var(--text-secondary);
  max-width: 420px;
  line-height: 1.6;
}

/* Message Bubbles */
.message-row {
  display: flex;
  padding: 4px 0;
  animation: fadeInUp 0.2s ease;
}

.message-row.user {
  justify-content: flex-end;
  padding-right: 20px;
  padding-left: 60px;
}

.message-row.assistant {
  justify-content: flex-start;
  padding-left: 20px;
  padding-right: 60px;
}

.message-bubble {
  max-width: 100%;
  padding: 10px 16px;
  border-radius: var(--radius-lg);
  font-size: 15px;
  line-height: 1.65;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.message-row.user .message-bubble {
  background: var(--bg-user-bubble);
  color: var(--text-primary);
  border-bottom-right-radius: 4px;
  max-width: 75%;
}

.message-row.assistant .message-bubble {
  background: var(--bg-assistant-bubble);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
  max-width: 85%;
  padding-left: 0;
}

/* Typing cursor animation */
.typing-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--text-primary);
  margin-left: 1px;
  vertical-align: text-bottom;
  animation: blink 0.7s step-end infinite;
}

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

/* Error message */
.message-error {
  color: #f87171;
  font-size: 13px;
  padding: 8px 16px;
  margin: 4px 20px;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: var(--radius-md);
}

/* ===== Video Grid ===== */
.video-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-top: 14px;
  width: 100%;
}

.video-card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  overflow: hidden;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
  text-decoration: none;
  color: var(--text-primary);
  display: block;
}

.video-card:hover {
  background: var(--bg-card-hover);
  transform: translateY(-2px);
}

.video-thumb-wrap {
  position: relative;
  aspect-ratio: 16/9;
  overflow: hidden;
  background: #e0e0e0;
}

.video-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: opacity 0.2s ease;
}

.video-thumb[data-loading="true"] { opacity: 0; }

.video-duration {
  position: absolute;
  bottom: 6px;
  right: 6px;
  background: rgba(0,0,0,0.8);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 3px;
  line-height: 1.4;
  letter-spacing: 0.3px;
}

.video-info {
  padding: 7px 9px 9px;
}

.video-title {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.video-site {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== Input Area ===== */
.input-area {
  padding: 12px 20px 16px;
  background: var(--bg-primary);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 10px 12px 10px 16px;
  transition: border-color var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--border-input);
}

.message-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  max-height: 200px;
  overflow-y: auto;
  scrollbar-width: thin;
}

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

.message-input::-webkit-scrollbar { width: 3px; }
.message-input::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 2px; }

.send-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--send-btn-bg);
  color: var(--send-btn-color);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), opacity var(--transition), transform 0.1s ease;
}

.send-btn:hover:not(:disabled) {
  background: #e0e0e0;
  transform: scale(1.05);
}

.send-btn:disabled {
  background: var(--send-btn-disabled-bg);
  color: var(--send-btn-disabled-color);
  cursor: not-allowed;
}

.input-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
  text-align: center;
}

/* ===== Mobile Overlay ===== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
  backdrop-filter: blur(2px);
}

/* ===== Animations ===== */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

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

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    transform: translateX(-100%);
    box-shadow: 4px 0 20px rgba(0,0,0,0.4);
  }

  .sidebar.open {
    transform: translateX(0);
  }

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

  .close-sidebar-btn {
    display: flex;
  }

  .topbar {
    display: flex;
  }

  .message-row.user { padding-right: 12px; padding-left: 40px; }
  .message-row.assistant { padding-left: 12px; padding-right: 40px; }
  .message-row.user .message-bubble { max-width: 85%; }
  .message-row.assistant .message-bubble { max-width: 95%; }

  .video-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .input-area { padding: 10px 12px 14px; }
  .welcome-title { font-size: 22px; }
}

@media (max-width: 480px) {
  .video-grid { gap: 6px; }
  .video-title { font-size: 11.5px; }
}

/* ===== Prose within assistant bubbles ===== */
.message-bubble p { margin-bottom: 0.75em; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble strong { font-weight: 600; }
.message-bubble em { font-style: italic; }
.message-bubble code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: rgba(0,0,0,0.06);
  padding: 1px 5px;
  border-radius: 4px;
}
.message-bubble ul, .message-bubble ol {
  padding-left: 1.4em;
  margin-bottom: 0.75em;
}
.message-bubble li { margin-bottom: 0.25em; }
