// =============================================================================
// Email App Styles
// =============================================================================

// -----------------------------------------------------------------------------
// Container
// -----------------------------------------------------------------------------

.email-container {
  display: flex;
  min-height: calc(100vh - 200px);
  background: var(--surface-color);
  border-radius: var(--bs-border-radius-lg);
  border: 1px solid var(--border-color);
  overflow: hidden;
}

// -----------------------------------------------------------------------------
// Sidebar
// -----------------------------------------------------------------------------

.email-sidebar {
  width: 240px;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.email-sidebar-header {
  padding: 1.25rem;
  border-bottom: 1px solid var(--border-color);
}

// -----------------------------------------------------------------------------
// Navigation
// -----------------------------------------------------------------------------

.email-nav {
  padding: 0.75rem 0;
}

.email-nav-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.email-nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 1.25rem;
  color: var(--default-color);
  font-size: 0.875rem;
  cursor: pointer;
  transition: all 0.2s;
  border-left: 3px solid transparent;

  i {
    font-size: 1.125rem;
    width: 20px;
    text-align: center;
    color: var(--muted-color);
  }

  span:first-of-type {
    flex: 1;
  }

  &:hover {
    background: var(--background-color);
  }

  &.active {
    background: color-mix(in srgb, var(--accent-color), transparent 92%);
    border-left-color: var(--accent-color);
    color: var(--accent-color);
    font-weight: 500;

    i {
      color: var(--accent-color);
    }
  }
}

.email-nav-badge {
  font-size: 0.75rem;
  font-weight: 500;
  padding: 0.125rem 0.5rem;
  border-radius: 10px;
  background: var(--accent-color);
  color: var(--contrast-color);

  &.bg-warning {
    background: var(--warning-color) !important;
  }
}

// -----------------------------------------------------------------------------
// Labels
// -----------------------------------------------------------------------------

.email-labels {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border-color);
}

.email-labels-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;

  span {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted-color);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
}

.email-labels-add {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--background-color);
    color: var(--default-color);
  }
}

.email-labels-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.email-label-item {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0;
  font-size: 0.8125rem;
  color: var(--default-color);
  cursor: pointer;
  border-radius: var(--bs-border-radius);

  &:hover {
    color: var(--accent-color);
  }

  &.active {
    color: var(--accent-color);
    font-weight: 500;
  }
}

.email-label-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

// -----------------------------------------------------------------------------
// Storage
// -----------------------------------------------------------------------------

.email-storage {
  padding: 1rem 1.25rem;
  margin-top: auto;
  border-top: 1px solid var(--border-color);
}

.email-storage-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--muted-color);
  margin-bottom: 0.5rem;
}

// -----------------------------------------------------------------------------
// Main Area
// -----------------------------------------------------------------------------

.email-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

// -----------------------------------------------------------------------------
// List View
// -----------------------------------------------------------------------------

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

.email-list-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
}

.email-list-actions {
  display: flex;
  align-items: center;
  gap: 0.25rem;

  .form-check {
    margin: 0;
    padding-left: 0;

    .form-check-input {
      margin: 0;
    }
  }
}

.email-action-btn {
  width: 44px;
  height: 44px;
  border-radius: var(--bs-border-radius);
  border: none;
  background: none;
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--background-color);
    color: var(--default-color);
  }
}

.email-search {
  flex: 1;
  position: relative;
  max-width: 400px;

  .form-control {
    padding-left: 2.5rem;
    background: var(--background-color);
    border-color: transparent;

    &:focus {
      background: var(--surface-color);
      border-color: var(--border-color);
    }
  }
}

.email-search-icon {
  position: absolute;
  left: 0.875rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted-color);
}

.email-list-pagination {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
}

.email-pagination-info {
  font-size: 0.8125rem;
  color: var(--muted-color);
}

.email-pagination-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--bs-border-radius);
  border: 1px solid var(--border-color);
  background: var(--surface-color);
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover:not(:disabled) {
    background: var(--background-color);
    color: var(--default-color);
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}

// -----------------------------------------------------------------------------
// Email List
// -----------------------------------------------------------------------------

.email-list {
  flex: 1;
  overflow-y: auto;

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
  }
}

.email-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1rem;
  border-bottom: 1px solid var(--border-color);
  cursor: pointer;
  transition: background 0.2s;

  &:hover {
    background: var(--background-color);
  }

  &.unread {
    background: color-mix(in srgb, var(--accent-color), transparent 97%);

    .email-item-sender,
    .email-item-subject {
      font-weight: 600;
    }

    &:hover {
      background: color-mix(in srgb, var(--accent-color), transparent 94%);
    }
  }
}

.email-item-select {
  flex-shrink: 0;

  .form-check-input {
    margin: 0;
  }
}

.email-item-star {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--muted-color);
  cursor: pointer;
  padding: 0;
  font-size: 1rem;
  transition: color 0.2s;

  &:hover {
    color: var(--warning-color);
  }

  &.starred {
    color: var(--warning-color);
  }
}

.email-item-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  font-weight: 600;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
  }
}

.email-item-content {
  flex: 1;
  min-width: 0;
}

.email-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.125rem;
}

.email-item-sender {
  font-size: 0.875rem;
  color: var(--heading-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.email-item-time {
  font-size: 0.75rem;
  color: var(--muted-color);
  flex-shrink: 0;
  margin-left: 0.5rem;
}

.email-item-subject {
  font-size: 0.8125rem;
  color: var(--default-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 0.125rem;
}

.email-item-preview {
  font-size: 0.8125rem;
  color: var(--muted-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.email-item-labels {
  display: flex;
  gap: 0.25rem;
  flex-shrink: 0;
}

.email-item-label {
  font-size: 0.6875rem;
  padding: 0.125rem 0.5rem;
  border-radius: 10px;
  color: var(--contrast-color);
  font-weight: 500;
}

.email-item-attachment {
  color: var(--muted-color);
  flex-shrink: 0;
}

// -----------------------------------------------------------------------------
// Detail View
// -----------------------------------------------------------------------------

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

.email-detail-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
}

.email-back-btn {
  width: 44px;
  height: 44px;
  border-radius: var(--bs-border-radius);
  border: none;
  background: none;
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--background-color);
    color: var(--default-color);
  }
}

.email-detail-actions {
  display: flex;
  gap: 0.25rem;
}

.email-detail-nav {
  display: flex;
  gap: 0.25rem;
  margin-left: auto;
}

.email-detail-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
  }
}

.email-detail-subject {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;

  h4 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--heading-color);
    margin: 0;
    line-height: 1.4;
  }
}

.email-detail-labels {
  display: flex;
  gap: 0.25rem;
  flex-shrink: 0;
}

// -----------------------------------------------------------------------------
// Message
// -----------------------------------------------------------------------------

.email-message {
  background: var(--background-color);
  border-radius: var(--bs-border-radius-lg);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.email-message-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-color);
}

.email-message-sender {
  display: flex;
  gap: 0.75rem;

  img {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
  }
}

.email-message-sender-info {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.email-message-sender-name {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--heading-color);
}

.email-message-sender-email {
  font-size: 0.8125rem;
  font-weight: 400;
  color: var(--muted-color);
  margin-left: 0.25rem;
}

.email-message-recipients {
  font-size: 0.8125rem;
  color: var(--muted-color);
}

.email-message-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.email-message-date {
  font-size: 0.8125rem;
  color: var(--muted-color);
}

.email-star-btn {
  &.starred {
    color: var(--warning-color);
  }
}

.email-message-body {
  font-size: 0.9375rem;
  line-height: 1.7;
  color: var(--default-color);

  p {
    margin-bottom: 1rem;

    &:last-child {
      margin-bottom: 0;
    }
  }

  ul, ol {
    margin-bottom: 1rem;
    padding-left: 1.5rem;

    li {
      margin-bottom: 0.5rem;
    }
  }
}

// -----------------------------------------------------------------------------
// Attachments
// -----------------------------------------------------------------------------

.email-attachments {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border-color);
}

.email-attachments-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;

  i {
    color: var(--muted-color);
  }

  span {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--heading-color);
    flex: 1;
  }
}

.email-attachments-download {
  font-size: 0.8125rem;
  color: var(--accent-color);
  text-decoration: none;

  &:hover {
    text-decoration: underline;
  }
}

.email-attachments-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.email-attachment-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: var(--bs-border-radius);
}

.email-attachment-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--bs-border-radius);
  background: color-mix(in srgb, var(--accent-color), transparent 90%);
  color: var(--accent-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}

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

.email-attachment-name {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--heading-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.email-attachment-size {
  font-size: 0.75rem;
  color: var(--muted-color);
}

.email-attachment-actions {
  display: flex;
  gap: 0.25rem;
}

.email-attachment-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--bs-border-radius);
  border: none;
  background: none;
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--background-color);
    color: var(--accent-color);
  }
}

// -----------------------------------------------------------------------------
// Quick Reply
// -----------------------------------------------------------------------------

.email-quick-reply {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 1rem;
  background: var(--background-color);
  border-radius: var(--bs-border-radius-lg);

  img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
  }

  .btn {
    flex-shrink: 0;
  }
}

.email-quick-reply-input {
  flex: 1;

  textarea {
    width: 100%;
    border: 1px solid var(--border-color);
    background: var(--surface-color);
    border-radius: var(--bs-border-radius);
    padding: 0.75rem;
    font-size: 0.875rem;
    color: var(--default-color);
    resize: none;
    min-height: 80px;

    &:focus {
      outline: none;
      border-color: var(--accent-color);
    }

    &::placeholder {
      color: var(--muted-color);
    }
  }
}

// -----------------------------------------------------------------------------
// Compose Modal
// -----------------------------------------------------------------------------

.email-compose-form {
  .form-label {
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--heading-color);
  }

  .form-control {
    font-size: 0.875rem;
  }

  textarea.form-control {
    resize: none;
  }
}

.email-compose-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.email-compose-attachment {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.75rem;
  background: var(--background-color);
  border-radius: var(--bs-border-radius);
  font-size: 0.8125rem;
  color: var(--default-color);

  i {
    color: var(--muted-color);
  }
}

.email-compose-attachment-remove {
  background: none;
  border: none;
  padding: 0;
  color: var(--muted-color);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;

  &:hover {
    color: var(--danger-color);
  }
}

.email-compose-tools {
  display: flex;
  gap: 0.25rem;
}

.email-compose-tool {
  width: 44px;
  height: 44px;
  border-radius: var(--bs-border-radius);
  border: none;
  background: none;
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--background-color);
    color: var(--default-color);
  }
}

// -----------------------------------------------------------------------------
// Responsive
// -----------------------------------------------------------------------------

@media (max-width: 991.98px) {
  .email-message-meta {
    flex-wrap: wrap;
  }
}

@media (max-width: 767.98px) {
  .email-container {
    min-height: calc(100vh - 180px);
  }

  .email-list-header {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .email-search {
    order: 1;
    flex-basis: 100%;
    max-width: none;
    margin-top: 0.5rem;
  }

  .email-list-pagination {
    margin-left: 0;
  }

  // Keep preview visible but truncate more aggressively
  .email-item-preview {
    display: block;
    -webkit-line-clamp: 1;
  }

  .email-detail-content {
    padding: 1rem;
  }

  .email-message {
    padding: 1rem;
  }

  .email-message-header {
    flex-direction: column;
    gap: 1rem;
  }

  .email-message-meta {
    width: 100%;
    justify-content: flex-start;
  }

  // Touch-friendly quick reply
  .email-quick-reply {
    flex-wrap: wrap;

    img {
      width: 44px;
      height: 44px;
    }

    .btn {
      min-height: 44px;
      padding: 0.5rem 1rem;
    }
  }

  .email-quick-reply-input {
    flex-basis: 100%;
    order: 2;
    margin-top: 0.5rem;

    textarea {
      min-height: 60px;
    }
  }
}

// -----------------------------------------------------------------------------
// Sidebar Toggle Button (hidden by default, shown under 1280px)
// -----------------------------------------------------------------------------

.email-sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  background: var(--background-color);
  border: none;
  border-radius: 50%;
  color: var(--muted-color);
  font-size: 1.125rem;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;

  &:hover {
    background: var(--border-color);
    color: var(--default-color);
  }
}

// -----------------------------------------------------------------------------
// Sidebar Close Button (hidden by default)
// -----------------------------------------------------------------------------

.email-sidebar-close {
  display: none;
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 44px;
  height: 44px;
  padding: 0;
  background: color-mix(in srgb, var(--default-color), transparent 95%);
  border: none;
  border-radius: var(--bs-border-radius);
  color: var(--muted-color);
  cursor: pointer;
  transition: all 0.2s;
  z-index: 10;

  &:hover {
    background: color-mix(in srgb, var(--danger-color), transparent 85%);
    color: var(--danger-color);
  }
}

// -----------------------------------------------------------------------------
// Sidebar Overlay (hidden by default)
// -----------------------------------------------------------------------------

.email-sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: color-mix(in srgb, var(--default-color), transparent 50%);
  z-index: 1040;
  opacity: 0;
  transition: opacity 0.3s ease;

  &.show {
    opacity: 1;
  }

  [data-theme="dark"] & {
    background: color-mix(in srgb, var(--default-color), transparent 30%);
  }
}

// -----------------------------------------------------------------------------
// Responsive - Under 1280px: Show toggle button, sidebar becomes slide-in panel
// -----------------------------------------------------------------------------

@media (max-width: 1279.98px) {
  // Show the toggle button in header
  .email-sidebar-toggle {
    display: flex;
  }

  // Show the overlay when sidebar is open
  .email-sidebar-overlay {
    display: block;
    pointer-events: none;

    &.show {
      pointer-events: auto;
    }
  }

  // Show the close button inside sidebar
  .email-sidebar-close {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  // Sidebar becomes slide-in panel
  .email-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 90vw;
    height: 100vh;
    height: 100dvh; // Dynamic viewport height for mobile browsers
    background: var(--surface-color);
    z-index: 1050;
    display: flex;
    flex-direction: column;
    overflow: hidden; // Container doesn't scroll, children do
    padding-top: 3.5rem; // Space for close button
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.15);
    transform: translateX(-100%);
    transition: transform 0.3s ease;

    &.show {
      transform: translateX(0);
    }
  }

  // Sidebar header - fixed at top
  .email-sidebar-header {
    flex-shrink: 0;
  }

  // Email nav - fixed
  .email-nav {
    flex-shrink: 0;
  }

  // Email labels should take remaining space and scroll
  .email-labels {
    display: block; // Show labels in mobile sidebar
    flex: 1;
    min-height: 0; // Important for flex child to allow shrinking
    overflow-y: auto;
    max-height: none; // Remove any max-height restrictions
    scrollbar-width: thin;

    &::-webkit-scrollbar {
      width: 4px;
    }

    &::-webkit-scrollbar-track {
      background: var(--background-color);
    }

    &::-webkit-scrollbar-thumb {
      background: var(--border-color);
      border-radius: 2px;

      &:hover {
        background: var(--muted-color);
      }
    }
  }

  // Show storage in mobile sidebar
  .email-storage {
    display: block;
    flex-shrink: 0;
  }

  // Prevent body scroll when sidebar is open
  body.email-sidebar-open {
    overflow: hidden;
  }
}

// Small phone breakpoint
@media (max-width: 575.98px) {
  .email-container {
    border-radius: 0;
    border-left: none;
    border-right: none;
    min-height: calc(100vh - 160px);
  }

  .email-list-header {
    padding: 0.5rem 0.75rem;
  }

  .email-list-actions {
    gap: 0;
  }

  // Ensure touch targets remain 44px
  .email-action-btn {
    width: 44px;
    height: 44px;
  }

  .email-pagination-btn {
    width: 44px;
    height: 44px;
  }

  .email-pagination-info {
    font-size: 0.75rem;
  }

  .email-item {
    padding: 0.75rem;
    gap: 0.5rem;
  }

  .email-item-avatar {
    width: 36px;
    height: 36px;
    font-size: 0.75rem;
  }

  .email-item-sender {
    font-size: 0.8125rem;
  }

  .email-item-subject {
    font-size: 0.75rem;
  }

  .email-item-preview {
    font-size: 0.75rem;
  }

  .email-item-time {
    font-size: 0.6875rem;
  }

  .email-item-labels {
    display: none;
  }

  // Detail view on small screens
  .email-detail-header {
    padding: 0.5rem 0.75rem;
    gap: 0.25rem;
  }

  .email-detail-content {
    padding: 0.75rem;
  }

  .email-detail-subject {
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;

    h4 {
      font-size: 1rem;
    }
  }

  .email-message {
    padding: 0.75rem;
    margin-bottom: 1rem;
  }

  .email-message-header {
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
  }

  .email-message-sender img {
    width: 40px;
    height: 40px;
  }

  .email-message-sender-name {
    font-size: 0.875rem;
  }

  .email-message-sender-email {
    font-size: 0.75rem;
  }

  .email-message-body {
    font-size: 0.875rem;
  }

  // Attachments
  .email-attachments {
    margin-top: 1rem;
    padding-top: 1rem;
  }

  .email-attachment-item {
    padding: 0.5rem;
  }

  .email-attachment-icon {
    width: 36px;
    height: 36px;
    font-size: 1rem;
  }

  .email-attachment-btn {
    width: 44px;
    height: 44px;
  }

  // Compose modal adjustments
  .email-compose-tools {
    flex-wrap: wrap;
  }

  .email-compose-tool {
    width: 44px;
    height: 44px;
  }
}
