/* CSS Variables */
:root {
  --primary-color: #25d366;
  --primary-dark: #128c7e;
  --secondary-color: #075e54;
  --background: #f0f2f5;
  --surface: #ffffff;
  --text-primary: #111b21;
  --text-secondary: #667781;
  --border-color: #e9edef;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  --radius: 8px;
}

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

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background-color: var(--background);
  color: var(--text-primary);
  line-height: 1.5;
  height: 100dvh;
  overflow: hidden;
}

/* App Container */
.app-container {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1000px;
  margin: 0 auto;
  background: var(--surface);
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

/* App Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--secondary-color);
  color: white;
}

.app-header h1 {
  font-size: 1.25rem;
  font-weight: 600;
}

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

.message-count {
  font-size: 0.8rem;
  opacity: 0.8;
}

.header-btn {
  padding: 6px 14px;
  background: rgba(255, 255, 255, 0.15);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background-color 0.2s;
}

.header-btn:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* Messages Container */
.messages-container {
  flex: 1;
  overflow-y: auto;
  background: var(--background);
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.messages .placeholder {
  text-align: center;
  color: var(--text-secondary);
  padding: 60px 20px;
}

.load-more {
  padding: 10px;
  text-align: center;
}

.load-more.hidden {
  display: none;
}

.load-more button {
  padding: 8px 20px;
  background: var(--surface);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s;
}

.load-more button:hover {
  border-color: var(--primary-color);
  color: var(--primary-dark);
}

/* Chat Message */
.chat-message {
  padding: 10px 14px;
  background: var(--surface);
  border-radius: var(--radius);
  border-left: 3px solid var(--border-color);
  max-width: 85%;
}

.chat-message .message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
  gap: 12px;
}

.chat-message .message-sender {
  font-weight: 600;
  color: var(--primary-dark);
  font-size: 0.85rem;
}

.chat-message .message-time {
  font-size: 0.7rem;
  color: var(--text-secondary);
}

.chat-message .message-content {
  color: var(--text-primary);
  font-size: 0.9rem;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}

.chat-message .message-content mark {
  background-color: #fff3cd;
  padding: 1px 4px;
  border-radius: 3px;
  font-weight: 500;
}

.chat-message .message-content .message-link {
  color: var(--primary-dark);
  text-decoration: underline;
  word-break: break-all;
}

.chat-message .message-content .message-link:hover {
  color: var(--primary-color);
}

/* Highlighted search result */
.chat-message.search-match {
  background: rgba(37, 211, 102, 0.15);
  border-left-color: var(--primary-color);
}

.chat-message.current-match {
  background: rgba(37, 211, 102, 0.3);
  border-left-color: var(--primary-dark);
  box-shadow: 0 0 0 2px var(--primary-color);
}

/* Date separator */
.date-separator {
  text-align: center;
  padding: 15px 0;
}

.date-separator span {
  background: var(--surface);
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  box-shadow: var(--shadow);
}

/* Scroll to Bottom Button */
.scroll-to-bottom {
  position: absolute;
  bottom: 75px;
  right: 20px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary-color);
  color: white;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  transition: background-color 0.2s, transform 0.2s;
  z-index: 99;
}

.scroll-to-bottom:hover {
  background: var(--primary-dark);
  transform: scale(1.1);
}

.scroll-to-bottom.hidden {
  display: none;
}

/* Search Bar at Bottom */
.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border-color);
}

.search-bar input {
  flex: 1;
  padding: 10px 16px;
  border: 1px solid var(--border-color);
  border-radius: 20px;
  font-size: 0.95rem;
  transition: border-color 0.2s;
}

.search-bar input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.search-bar > button {
  padding: 10px 20px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background-color 0.2s;
}

.search-bar > button:hover {
  background-color: var(--primary-dark);
}

/* Floating Search Navigation */
.search-nav {
  display: none;
  position: absolute;
  bottom: 70px;
  left: 50%;
  transform: translateX(-50%);
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--surface);
  border-radius: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  z-index: 100;
}

.search-nav.active {
  display: flex;
}

.search-nav .nav-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  background: var(--background);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  color: var(--text-primary);
  transition: all 0.2s;
}

@media (hover: hover) {
  .search-nav .nav-btn:hover:not(:disabled) {
    background: var(--primary-color);
    color: white;
  }
}

.search-nav .nav-btn:active:not(:disabled) {
  background: var(--primary-color);
  color: white;
}

.search-nav .nav-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.search-nav .clear-btn {
  background: transparent;
  color: var(--text-secondary);
}

@media (hover: hover) {
  .search-nav .clear-btn:hover {
    background: #ffebee;
    color: #dc3545;
  }
}

.search-nav .clear-btn:active {
  background: #ffebee;
  color: #dc3545;
}

#match-info {
  font-size: 0.9rem;
  color: var(--text-primary);
  font-weight: 500;
  min-width: 50px;
  text-align: center;
}

/* 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: 1000;
  padding: 20px;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  max-width: 500px;
  width: 100%;
  max-height: 90vh;
  overflow: auto;
}

.modal-content.modal-large {
  max-width: 700px;
}

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

.modal-header h2 {
  font-size: 1.1rem;
  color: var(--secondary-color);
}

.modal-close {
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  font-size: 1.5rem;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
}

.modal-close:hover {
  background: var(--background);
  color: var(--text-primary);
}

.modal-body {
  padding: 20px;
}

/* Upload Section */
.upload-instructions {
  margin-bottom: 20px;
}

.upload-instructions p {
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.upload-instructions ol {
  margin-left: 20px;
  color: var(--text-secondary);
}

.upload-instructions li {
  margin-bottom: 6px;
}

.upload-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.drop-zone {
  border: 2px dashed var(--border-color);
  border-radius: var(--radius);
  padding: 30px;
  text-align: center;
  transition: all 0.2s;
}

.drop-zone.dragover {
  border-color: var(--primary-color);
  background-color: rgba(37, 211, 102, 0.05);
}

.drop-zone p {
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.drop-zone label {
  display: inline-block;
  margin-top: 8px;
}

.drop-zone.has-file {
  border-color: var(--primary-color);
  background-color: rgba(37, 211, 102, 0.05);
}

.drop-zone .file-name {
  color: var(--primary-dark);
  font-weight: 600;
}

.btn-primary {
  padding: 12px 24px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.2s;
}

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

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

.password-field {
  width: 100%;
}

.password-field.hidden {
  display: none;
}

.password-field input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 1rem;
}

.password-field input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.btn-secondary {
  padding: 8px 16px;
  background-color: var(--border-color);
  color: var(--text-primary);
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-secondary:hover {
  background-color: #d1d7db;
}

.upload-status {
  padding: 12px;
  border-radius: var(--radius);
  display: none;
  text-align: center;
}

.upload-status.success {
  display: block;
  background-color: rgba(37, 211, 102, 0.1);
  color: var(--primary-dark);
  border: 1px solid var(--primary-color);
}

.upload-status.error {
  display: block;
  background-color: rgba(220, 53, 69, 0.1);
  color: #dc3545;
  border: 1px solid #dc3545;
}

.upload-status.loading {
  display: block;
  background-color: rgba(0, 123, 255, 0.1);
  color: #007bff;
  border: 1px solid #007bff;
}

/* Stats Section */
.stats-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--background);
  padding: 16px;
  border-radius: var(--radius);
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary-dark);
  margin-bottom: 4px;
}

.stat-label {
  color: var(--text-secondary);
  font-size: 0.8rem;
}

.stats-section h3 {
  color: var(--secondary-color);
  margin-bottom: 12px;
  font-size: 1rem;
}

.stats-list {
  /* No max-height - show all items */
}

.stats-item {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-color);
}

.stats-item:last-child {
  border-bottom: none;
}

.stats-item-name {
  color: var(--text-primary);
  font-size: 0.9rem;
}

.stats-item-count {
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 0.9rem;
}

/* Media Attachments */
.message-attachments {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.attachment-image {
  width: 250px;
  max-width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--background);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.message-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--radius);
  cursor: pointer;
  transition: opacity 0.2s;
}

.message-image:hover {
  opacity: 0.9;
}

.attachment-video {
  width: 300px;
  max-width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
}

.message-video {
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  background: #000;
}

.attachment-document {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--background);
  border-radius: var(--radius);
  max-width: 300px;
}

.document-icon {
  font-size: 1.5rem;
}

.document-link {
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: var(--text-primary);
  overflow: hidden;
}

.document-link:hover {
  color: var(--primary-dark);
}

.document-name {
  font-weight: 500;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.document-size {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

/* Lightbox */
.lightbox {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.95);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.lightbox.hidden {
  display: none;
}

.lightbox-content {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: calc(100% - 60px);
  padding: 20px;
}

#lightbox-image {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
  border-radius: 4px;
}

.lightbox-close {
  position: absolute;
  top: 15px;
  right: 20px;
  width: 44px;
  height: 44px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  font-size: 2rem;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
  z-index: 10;
}

.lightbox-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

.lightbox-prev,
.lightbox-next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  height: 50px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
}

.lightbox-prev {
  left: 20px;
}

.lightbox-next {
  right: 20px;
}

.lightbox-prev:hover:not(:disabled),
.lightbox-next:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.2);
}

.lightbox-prev:disabled,
.lightbox-next:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.lightbox-counter {
  color: white;
  font-size: 0.9rem;
  padding: 15px;
  text-align: center;
}

/* Responsive */
@media (max-width: 768px) {
  .app-container {
    height: 100dvh; /* Use dynamic viewport height for mobile */
  }

  .app-header {
    flex-direction: row;
    gap: 10px;
    padding: 10px 12px;
    flex-shrink: 0;
  }

  .app-header h1 {
    font-size: 1rem;
  }

  .header-actions {
    gap: 8px;
  }

  .header-btn {
    padding: 5px 10px;
    font-size: 0.8rem;
  }

  .message-count {
    display: none;
  }

  .messages-container {
    flex: 1;
    min-height: 0; /* Important for flex child scrolling */
  }

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

  .search-bar {
    padding: 12px;
    gap: 10px;
    flex-shrink: 0;
  }

  .search-bar input {
    flex: 1;
    min-width: 0;
    padding: 14px 18px;
    font-size: 16px; /* Prevent iOS zoom on focus */
    border-radius: 25px;
  }

  .search-bar > button {
    padding: 14px 20px;
    font-size: 1rem;
    white-space: nowrap;
    border-radius: 25px;
  }

  .search-nav {
    bottom: 80px;
    padding: 10px 16px;
    gap: 10px;
  }

  .search-nav .nav-btn {
    width: 44px;
    height: 44px;
    font-size: 1.2rem;
  }

  #match-info {
    font-size: 1rem;
    min-width: 55px;
  }

  .scroll-to-bottom {
    bottom: 70px;
    right: 12px;
    width: 40px;
    height: 40px;
  }

  /* Modal fixes for mobile */
  .modal {
    padding: 10px;
    align-items: flex-start;
    overflow-y: auto;
  }

  .modal-content {
    margin: 0;
    max-height: none;
    width: 100%;
  }

  .modal-content.modal-large {
    max-width: 100%;
  }

  .modal-body {
    padding: 15px;
    max-height: calc(100dvh - 120px);
    overflow-y: auto;
  }

  .stats-summary {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .stat-card {
    padding: 12px;
  }

  .stat-value {
    font-size: 1.2rem;
  }

  .attachment-image {
    width: 200px;
  }

  .attachment-video {
    max-width: 100%;
  }

  .attachment-document {
    max-width: 100%;
  }

  .lightbox-prev,
  .lightbox-next {
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
  }

  .lightbox-prev {
    left: 10px;
  }

  .lightbox-next {
    right: 10px;
  }
}
