/* ─── CloviTek Founder Chat — Tokens & Reset ─────────────── */
:root {
  --brand: #4F46E5;
  --brand-dark: #3730A3;
  --brand-light: #EEF2FF;
  --bg: #F8FAFC;
  --surface: #FFFFFF;
  --surface-hover: #F1F5F9;
  --text: #1E293B;
  --text-muted: #64748B;
  --border: #E2E8F0;
  --danger: #EF4444;
  --success: #22C55E;
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --bg: #0F172A;
  --surface: #1E293B;
  --surface-hover: #334155;
  --text: #F8FAFC;
  --text-muted: #94A3B8;
  --border: #334155;
  --brand-light: #312E81;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.4);
}

/* ─── Global Reset ───────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: var(--font-family);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

body.loading #app {
  display: none;
}

body.loading .loading-overlay {
  display: flex;
}

.loading-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  gap: 1rem;
}

.loading-overlay p {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ─── Cap Banner ─────────────────────────────────────────── */
.cap-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--danger);
  color: white;
  text-align: center;
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
  z-index: 1000;
}

.cap-banner.hidden {
  display: none;
}

/* ─── App Layout ─────────────────────────────────────────── */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ─── Sidebar ────────────────────────────────────────────── */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform var(--transition);
  z-index: 100;
  overflow: hidden;
}

.sidebar-overlay {
  display: none;
}

.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.sidebar-header h2 {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--brand);
  color: white;
  border: none;
  padding: 0.6rem 1rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
}

.new-chat-btn:hover {
  background: var(--brand-dark);
  transform: translateY(-1px);
}

.new-chat-btn i {
  width: 16px;
  height: 16px;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background var(--transition);
  position: relative;
}

.session-item:hover {
  background: var(--surface-hover);
}

.session-item.active {
  background: var(--brand-light);
  border-left: 3px solid var(--brand);
}

.session-item .preview {
  flex: 1;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}

.session-item .timestamp {
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.session-item .delete-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.2rem;
  border-radius: 4px;
  opacity: 0;
  transition: opacity var(--transition), color var(--transition);
  flex-shrink: 0;
}

.session-item:hover .delete-btn {
  opacity: 1;
}

.session-item .delete-btn:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

.session-item-placeholder {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* ─── Main ───────────────────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ─── Top Bar ────────────────────────────────────────────── */
.top-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.hamburger-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 0.3rem;
  border-radius: var(--radius);
}

.hamburger-btn:hover {
  background: var(--surface-hover);
}

.brand {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  flex-shrink: 0;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-left: auto;
}

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

.user-email {
  font-weight: 500;
  color: var(--text);
}

.model-select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  font-family: inherit;
  color: var(--text);
  cursor: pointer;
  outline: none;
  transition: border-color var(--transition);
  max-width: 160px;
}

.model-select:focus {
  border-color: var(--brand);
}

.theme-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.3rem;
  border-radius: var(--radius);
  transition: color var(--transition), background var(--transition);
}

.theme-toggle:hover {
  color: var(--text);
  background: var(--surface-hover);
}

.signout-link {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-decoration: none;
  transition: color var(--transition);
}

.signout-link:hover {
  color: var(--danger);
}

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

.chat-thread {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.empty-state {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.empty-state.hidden {
  display: none;
}

.empty-icon {
  width: 48px;
  height: 48px;
  stroke-width: 1.5;
  color: var(--border);
}

.empty-state h3 {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--text);
}

.empty-state p {
  font-size: 0.9rem;
  max-width: 300px;
}

/* ─── Messages ───────────────────────────────────────────── */
.message {
  display: flex;
  gap: 0.75rem;
  max-width: 80%;
  animation: fadeIn 0.2s ease;
}

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

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

.message.user .avatar {
  background: var(--brand);
  color: white;
}

.message.assistant {
  align-self: flex-start;
}

.message.assistant .avatar {
  background: var(--surface-hover);
  border: 1px solid var(--border);
  color: var(--text);
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  flex-shrink: 0;
  text-transform: uppercase;
}

.content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
  line-height: 1.6;
  overflow-wrap: break-word;
  box-shadow: var(--shadow);
}

.message.user .content {
  background: var(--brand);
  color: white;
  border-color: var(--brand);
}

.content p {
  margin-bottom: 0.5rem;
}

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

.content pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  overflow-x: auto;
  font-size: 0.8rem;
  line-height: 1.4;
  margin: 0.5rem 0;
}

.content code {
  background: var(--bg);
  padding: 0.15rem 0.4rem;
  border-radius: 3px;
  font-size: 0.85em;
}

.content pre code {
  background: transparent;
  padding: 0;
}

/* ─── Input Area ─────────────────────────────────────────── */
.input-area {
  flex-shrink: 0;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

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

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

.chat-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  resize: none;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  padding: 0.25rem 0.5rem;
  min-height: 24px;
  max-height: 120px;
  line-height: 1.5;
}

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

.send-btn {
  background: var(--brand);
  border: none;
  color: white;
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
  flex-shrink: 0;
}

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

.send-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.send-btn i {
  width: 18px;
  height: 18px;
}

.token-counter {
  text-align: right;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  padding-right: 0.25rem;
}

/* ─── Responsive ─────────────────────────────────────────── */
@media (max-width: 768px) {
  .hamburger-btn {
    display: block;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
    z-index: 200;
  }

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

  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 199;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition);
  }

  .sidebar-overlay.open {
    opacity: 1;
    pointer-events: all;
  }

  .top-bar-right .user-info {
    display: none;
  }

  .message {
    max-width: 90%;
  }

  .model-select {
    max-width: 120px;
    font-size: 0.75rem;
  }
}

@media (max-width: 480px) {
  .top-bar {
    padding: 0.4rem 0.75rem;
  }

  .brand {
    font-size: 0.9rem;
  }

  .sidebar {
    width: 260px;
  }

  .message {
    max-width: 95%;
  }
}