/* ==================== CSS Variables ==================== */
:root {
  /* Colors - WhatsApp-like theme */
  --primary-color: #075e54;
  --primary-dark: #054c44;
  --secondary-color: #25d366;
  --background-color: #e5ddd5;
  --chat-bg: #efeae2;
  --header-bg: #075e54;
  --input-bg: #f0f0f0;
  --message-out: #dcf8c6;
  --message-in: #ffffff;
  --text-primary: #111b21;
  --text-secondary: #667781;
  --border-color: #e9edef;
  --error-color: #dc3545;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

  /* Spacing */
  --spacing-xs: 4px;
  --spacing-sm: 8px;
  --spacing-md: 16px;
  --spacing-lg: 24px;
  --spacing-xl: 32px;

  /* Border radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 16px;
  --radius-full: 9999px;

  /* Safe areas for mobile */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text-primary);
  background-color: var(--background-color);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

.screen {
  display: none;
  height: 100%;
  width: 100%;
}

.screen.active {
  display: flex;
  flex-direction: column;
}

/* ==================== LOGIN SCREEN ==================== */
#login-screen {
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
  padding: var(--spacing-md);
}

.login-container {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: var(--radius-lg);
  padding: var(--spacing-xl);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.login-logo {
  text-align: center;
  margin-bottom: var(--spacing-xl);
}

.logo-icon {
  width: 80px;
  height: 80px;
  background: var(--primary-color);
  color: white;
  font-size: 40px;
  font-weight: bold;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto var(--spacing-md);
}

.login-logo h1 {
  font-size: 28px;
  color: var(--text-primary);
  margin-bottom: var(--spacing-xs);
}

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

.login-form {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-md);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
}

.form-group label {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
}

.form-group input {
  padding: 12px 16px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: 16px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-group input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(7, 94, 84, 0.1);
}

.error-message {
  background: #fee2e2;
  color: var(--error-color);
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: var(--radius-md);
  font-size: 14px;
  text-align: center;
}

.btn-primary {
  background: var(--primary-color);
  color: white;
  border: none;
  padding: 14px 24px;
  border-radius: var(--radius-md);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-sm);
}

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

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

.btn-secondary {
  background: var(--input-bg);
  color: var(--text-primary);
  border: none;
  padding: 12px 20px;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-secondary:hover {
  background: #e0e0e0;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ==================== CHAT SCREEN ==================== */
#chat-screen {
  background: var(--chat-bg);
}

/* Header */
.chat-header {
  background: var(--header-bg);
  color: white;
  padding: var(--spacing-sm) var(--spacing-md);
  padding-top: calc(var(--spacing-sm) + var(--safe-top));
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.avatar {
  width: 40px;
  height: 40px;
  background: var(--secondary-color);
  color: white;
  font-size: 18px;
  font-weight: bold;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-info {
  display: flex;
  flex-direction: column;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
}

.header-status {
  font-size: 12px;
  opacity: 0.8;
}

.header-status.connected {
  color: var(--secondary-color);
  opacity: 1;
}

.icon-btn {
  background: transparent;
  border: none;
  color: inherit;
  padding: var(--spacing-sm);
  border-radius: var(--radius-full);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* Messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--spacing-md);
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23d0c8b8' fill-opacity='0.2'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.message {
  max-width: 85%;
  display: flex;
  flex-direction: column;
  animation: messageIn 0.2s ease-out;
}

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

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

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

.message-content {
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  word-wrap: break-word;
}

.message.user .message-content {
  background: var(--message-out);
  border-bottom-right-radius: var(--spacing-xs);
}

.message.assistant .message-content {
  background: var(--message-in);
  border-bottom-left-radius: var(--spacing-xs);
}

.message-content p {
  margin: 0;
  white-space: pre-wrap;
}

.message-time {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
  padding: 0 var(--spacing-xs);
}

.message.user .message-time {
  text-align: right;
}

/* File attachment in message */
.message-file {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm);
  background: rgba(0, 0, 0, 0.05);
  border-radius: var(--radius-sm);
  margin-top: var(--spacing-sm);
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}

.message-file:hover {
  background: rgba(0, 0, 0, 0.1);
}

.message-file svg {
  flex-shrink: 0;
}

.message-file-name {
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Links in messages */
.message-link {
  color: #0066cc;
  text-decoration: underline;
  word-break: break-all;
  cursor: pointer;
}

.message-link:hover {
  color: #0052a3;
  text-decoration: none;
}

.message-link:visited {
  color: #551a8b;
}

/* Strong/bold text in messages */
.message-content strong {
  font-weight: 600;
}

/* Processing indicator */
.processing-indicator {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) var(--spacing-md);
  color: var(--text-secondary);
  font-size: 13px;
}

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

.typing-dots span {
  width: 8px;
  height: 8px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: typing 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* Input area */
.chat-input {
  display: flex;
  align-items: flex-end;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) var(--spacing-md);
  padding-bottom: calc(var(--spacing-sm) + var(--safe-bottom));
  background: var(--input-bg);
  border-top: 1px solid var(--border-color);
}

.chat-input .icon-btn {
  color: var(--text-secondary);
  flex-shrink: 0;
}

.chat-input .icon-btn:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--primary-color);
}

.input-wrapper {
  flex: 1;
  background: white;
  border-radius: var(--radius-lg);
  padding: var(--spacing-sm) var(--spacing-md);
  box-shadow: var(--shadow);
}

.input-wrapper textarea {
  width: 100%;
  border: none;
  outline: none;
  font-size: 16px;
  line-height: 1.4;
  resize: none;
  max-height: 120px;
  font-family: inherit;
}

.send-btn {
  background: var(--primary-color) !important;
  color: white !important;
  border-radius: var(--radius-full);
}

.send-btn:disabled {
  background: var(--text-secondary) !important;
  opacity: 0.5;
}

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

/* ==================== MODAL ==================== */
.modal {
  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: 100;
  padding: var(--spacing-md);
}

.modal-content {
  background: white;
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 400px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.modal-header h3 {
  font-size: 18px;
  font-weight: 600;
}

.modal-body {
  padding: var(--spacing-md);
  overflow-y: auto;
  text-align: center;
}

#file-preview {
  max-width: 100%;
  margin-bottom: var(--spacing-md);
}

#file-preview img {
  max-width: 100%;
  max-height: 300px;
  border-radius: var(--radius-md);
}

#file-name {
  font-size: 14px;
  color: var(--text-secondary);
  word-break: break-all;
}

.modal-footer {
  display: flex;
  gap: var(--spacing-sm);
  padding: var(--spacing-md);
  border-top: 1px solid var(--border-color);
  justify-content: flex-end;
}

/* ==================== Responsive ==================== */
@media (min-width: 768px) {
  .login-container {
    padding: var(--spacing-xl) 48px;
  }

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

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  :root {
    --background-color: #111b21;
    --chat-bg: #0b141a;
    --input-bg: #1f2c34;
    --message-in: #202c33;
    --message-out: #005c4b;
    --text-primary: #e9edef;
    --text-secondary: #8696a0;
    --border-color: #2a3942;
  }

  .login-container {
    background: #1f2c34;
  }

  .form-group input {
    background: #2a3942;
    color: var(--text-primary);
    border-color: var(--border-color);
  }

  .input-wrapper {
    background: #2a3942;
  }

  .input-wrapper textarea {
    background: transparent;
    color: var(--text-primary);
  }

  .modal-content {
    background: #1f2c34;
  }

  .error-message {
    background: #2a1f1f;
  }
}
