:root {
  --color-primary: #2e7d32;
  --color-primary-light: #4caf50;
  --color-primary-dark: #1b5e20;
  --color-primary-bg: #e8f5e9;
  --color-amber: #ff8f00;
  --color-amber-bg: #fff3e0;
  --color-red: #c62828;
  --color-red-bg: #fbe9e7;
  --color-text: #212121;
  --color-text-muted: #757575;
  --color-border: #e0e0e0;
  --color-bg: #ffffff;
  --color-sidebar-bg: #fafafa;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.1);
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --transition: 150ms ease;
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --header-height: 56px;
  --sidebar-width: 280px;
}

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

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

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.screen {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Login Screen */
.login-header {
  padding: 24px;
  text-align: center;
  border-bottom: 1px solid var(--color-border);
}

.login-header h1 {
  font-size: 1.75rem;
  font-weight: 600;
  color: var(--color-primary);
}

.login-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 24px 16px;
}

#login-form {
  width: 100%;
  max-width: 360px;
}

.form-group {
  margin-bottom: 16px;
}

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

.form-group input {
  width: 100%;
  padding: 12px 14px;
  font-size: 1rem;
  font-family: inherit;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.form-group input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-bg);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  font-size: 0.9375rem;
  font-weight: 500;
  font-family: inherit;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background-color var(--transition), opacity var(--transition);
  white-space: nowrap;
}

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

.btn-primary {
  background: var(--color-primary);
  color: white;
  width: 100%;
}

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

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

.btn-secondary:hover:not(:disabled) {
  background: var(--color-border);
}

.btn-danger {
  background: var(--color-red-bg);
  color: var(--color-red);
}

.btn-danger:hover:not(:disabled) {
  background: #fce4ec;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--color-text);
  cursor: pointer;
  transition: background-color var(--transition);
}

.icon-btn:hover:not(:disabled) {
  background: var(--color-border);
}

.icon-btn:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.error-message {
  margin-top: 16px;
  padding: 12px;
  background: var(--color-red-bg);
  color: var(--color-red);
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  text-align: center;
}

/* App Screen */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-height);
  padding: 0 16px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
  position: sticky;
  top: 0;
  z-index: 10;
}

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

.app-title {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--color-primary);
}

.header-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  max-width: 300px;
}

.usage-meter {
  flex: 1;
  height: 6px;
  background: var(--color-border);
  border-radius: 3px;
  overflow: hidden;
}

.usage-bar {
  height: 100%;
  width: 0%;
  background: var(--color-primary);
  border-radius: 3px;
  transition: width 300ms ease, background-color 300ms ease;
}

.usage-bar.warning {
  background: var(--color-amber);
}

.usage-bar.danger {
  background: var(--color-red);
}

.usage-text {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--color-text-muted);
  min-width: 36px;
  text-align: right;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--color-sidebar-bg);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
}

.bots-nav {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bot-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-md);
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  transition: background-color var(--transition);
}

.bot-item:hover {
  background: var(--color-border);
}

.bot-item.active {
  background: var(--color-primary-bg);
}

.bot-item:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.bot-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1rem;
  color: white;
  flex-shrink: 0;
}

.bot-info {
  flex: 1;
  min-width: 0;
}

.bot-name {
  display: block;
  font-weight: 500;
  font-size: 0.9375rem;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bot-title {
  display: block;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

/* Main Content */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.view {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Chat View */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
  flex-shrink: 0;
}

.chat-bot-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.chat-bot-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.875rem;
  color: white;
  flex-shrink: 0;
}

.chat-bot-name {
  display: block;
  font-weight: 500;
  font-size: 0.9375rem;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-bot-title {
  display: block;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.chats-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: var(--color-sidebar-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background-color var(--transition), border-color var(--transition);
}

.chat-item:hover {
  background: var(--color-border);
}

.chat-item.active {
  border-color: var(--color-primary);
  background: var(--color-primary-bg);
}

.chat-item:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.chat-item-title {
  font-weight: 500;
  font-size: 0.9375rem;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  animation: fadeIn 150ms ease;
}

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

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

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

.message-bubble {
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  font-size: 0.9375rem;
  line-height: 1.6;
  word-wrap: break-word;
}

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

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

.message-bubble p {
  margin: 0.5em 0;
}

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

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

.message-bubble ul, .message-bubble ol {
  margin: 0.5em 0;
  padding-left: 1.5em;
}

.message-bubble li {
  margin: 0.25em 0;
}

.message-bubble strong {
  font-weight: 600;
}

.message-time {
  font-size: 0.6875rem;
  color: var(--color-text-muted);
  margin-top: 4px;
  padding: 0 4px;
}

.message.thinking .message-bubble {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
}

.thinking-dots {
  display: flex;
  gap: 4px;
}

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

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

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

/* Meter Banner */
.meter-banner {
  padding: 12px 16px;
  background: var(--color-amber-bg);
  border-bottom: 1px solid var(--color-amber);
  color: var(--color-amber);
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.meter-banner button {
  padding: 6px 12px;
  font-size: 0.8125rem;
  background: var(--color-amber);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.meter-banner button:hover {
  background: #e67e00;
}

/* Chat Input */
.chat-input-form {
  padding: 12px 16px;
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#chat-input {
  flex: 1;
  min-height: 44px;
  max-height: 160px;
  padding: 10px 14px;
  font-size: 0.9375rem;
  font-family: inherit;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-sidebar-bg);
  color: var(--color-text);
  resize: none;
  line-height: 1.5;
  transition: border-color var(--transition), box-shadow var(--transition);
}

#chat-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-bg);
  background: var(--color-bg);
}

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

/* Settings View */
#settings-view {
  padding: 24px;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
}

#settings-view h2 {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 24px;
}

.settings-section {
  margin-bottom: 32px;
}

.settings-section h3 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 12px;
}

.account-info {
  padding: 16px;
  background: var(--color-sidebar-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  margin-bottom: 16px;
}

.account-info p {
  margin: 4px 0;
  font-size: 0.9375rem;
}

.account-info strong {
  color: var(--color-text);
}

.coming-soon {
  color: var(--color-text-muted);
  font-size: 0.9375rem;
}

/* Mobile adjustments */
@media (max-width: 640px) {
  :root {
    --sidebar-width: 100%;
  }

  .header-center {
    display: none;
  }

  .sidebar {
    position: fixed;
    top: var(--header-height);
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 250ms ease;
    box-shadow: var(--shadow-lg);
  }

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

  .chat-header {
    position: sticky;
    top: 0;
    z-index: 5;
  }

  #chat-bots-btn {
    display: inline-flex !important;
  }

  .app-title {
    display: none;
  }
}

@media (min-width: 641px) {
  #chat-bots-btn {
    display: none !important;
  }

  #sidebar-toggle {
    display: none;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* High contrast mode */
@media (prefers-contrast: high) {
  :root {
    --color-border: #000;
    --color-text-muted: #333;
  }
}

/* Focus visible polyfill */
:focus:not(:focus-visible) {
  outline: none;
}