:root {
  --bg-page: #0a0a0a;
  --bg-panel: #121212;
  --bg-hover: #1a1a1a;
  --bg-selected: #202020;
  --border-color: #262626;
  --text-primary: #ededed;
  --text-secondary: #8e8e93;
  --accent-primary: #2563eb;
  --accent-hover: #1d4ed8;
  --status-success: #16a34a;
  --status-warning: #d97706;
  --status-danger: #dc2626;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

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

body {
  background-color: var(--bg-page);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

a {
  color: var(--accent-primary);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 6px 12px;
  outline: none;
}

button {
  cursor: pointer;
  background-color: var(--bg-hover);
  transition: background-color 0.1s ease;
}
button:hover {
  background-color: var(--border-color);
}
button.primary {
  background-color: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #ffffff;
}
button.primary:hover {
  background-color: var(--accent-hover);
}
button.danger {
  background-color: var(--status-danger);
  border-color: var(--status-danger);
  color: #ffffff;
}
button.danger:hover {
  background-color: #b91c1c;
}
button.sm {
  padding: 3px 8px;
  font-size: 12px;
}

input[type="text"], input[type="email"], input[type="password"], textarea, select {
  width: 100%;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent-primary);
}

/* App Shell Layout */
.app-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Pane 1: Navigation rail */
.nav-pane {
  width: 220px;
  min-width: 220px;
  background-color: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}

.brand-header {
  padding: 16px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.compose-box {
  padding: 12px 16px;
}
.compose-box button {
  width: 100%;
  padding: 8px 12px;
  font-weight: 500;
}

.nav-links {
  list-style: none;
  padding: 8px 0;
  flex: 1;
  overflow-y: auto;
}
.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  color: var(--text-secondary);
  cursor: pointer;
  border-left: 2px solid transparent;
}
.nav-item:hover {
  background-color: var(--bg-hover);
  color: var(--text-primary);
}
.nav-item.active {
  background-color: var(--bg-selected);
  color: var(--text-primary);
  border-left-color: var(--accent-primary);
  font-weight: 500;
}
.badge {
  background-color: var(--bg-hover);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 10px;
}
.badge.accent {
  background-color: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #ffffff;
}

.nav-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  font-size: 12px;
}
.quota-bar-wrapper {
  margin-bottom: 12px;
}
.quota-text {
  display: flex;
  justify-content: space-between;
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.progress-bar-bg {
  height: 4px;
  background-color: var(--border-color);
  border-radius: 2px;
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background-color: var(--accent-primary);
}
.user-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text-secondary);
}

/* Pane 2: Message list */
.list-pane {
  width: 340px;
  min-width: 340px;
  background-color: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}

.search-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  gap: 8px;
}

.message-items {
  flex: 1;
  overflow-y: auto;
  list-style: none;
}
.msg-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  cursor: pointer;
  background-color: var(--bg-panel);
  transition: background-color 0.1s;
}
.msg-item:hover {
  background-color: var(--bg-hover);
}
.msg-item.active {
  background-color: var(--bg-selected);
  border-left: 2px solid var(--accent-primary);
}
.msg-item.unread {
  font-weight: 600;
  color: #ffffff;
}
.msg-row-1 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.msg-sender {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}
.msg-date {
  font-size: 11px;
  color: var(--text-secondary);
}
.msg-row-2 {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}
.msg-row-3 {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
}
.star-btn {
  background: none;
  border: none;
  padding: 0 4px;
  color: var(--text-secondary);
}
.star-btn.starred {
  color: #facc15;
}

/* Pane 3: Detail / Reading pane */
.detail-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-page);
  overflow-y: auto;
}

.detail-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-panel);
}
.detail-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.detail-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
}
.detail-meta {
  font-size: 12px;
  color: var(--text-secondary);
  display: grid;
  grid-template-columns: auto 1fr;
  row-gap: 4px;
  column-gap: 12px;
}
.meta-label {
  color: var(--text-secondary);
  font-weight: 500;
}
.meta-val {
  color: var(--text-primary);
}

.attachments-section {
  padding: 12px 20px;
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
}
.att-chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}
.att-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background-color: var(--bg-hover);
  border: 1px solid var(--border-color);
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
}
.att-chip a {
  color: var(--text-primary);
}

.detail-body {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
  line-height: 1.6;
}
.body-plain-view {
  white-space: pre-wrap;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-primary);
}
.body-html-iframe {
  width: 100%;
  height: 500px;
  border: 1px solid var(--border-color);
  background-color: #ffffff;
  border-radius: 4px;
}

/* Empty states */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-secondary);
  text-align: center;
  padding: 20px;
}

/* Modal styles */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-card {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  width: 90%;
  max-width: 650px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.modal-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}
.modal-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
}
.modal-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Login Card */
.login-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 16px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 24px;
}
.login-header {
  margin-bottom: 20px;
  text-align: center;
}
.login-title {
  font-size: 18px;
  font-weight: 600;
}
.login-subtitle {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}
.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.error-banner {
  background-color: rgba(220, 38, 38, 0.15);
  border: 1px solid var(--status-danger);
  color: #fca5a5;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 13px;
  margin-bottom: 16px;
}

/* Responsive collapse */
@media (max-width: 768px) {
  .app-container {
    flex-direction: column;
  }
  .nav-pane {
    width: 100%;
    min-width: 100%;
    height: auto;
  }
  .list-pane {
    width: 100%;
    min-width: 100%;
    height: 350px;
  }
  .detail-pane {
    height: auto;
  }
}

/* Admin Dashboard Specific Styles */
.admin-container {
  display: flex;
  min-height: 100vh;
}
.admin-sidebar {
  width: 240px;
  min-width: 240px;
  background-color: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}
.admin-brand {
  padding: 16px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border-color);
}
.admin-nav {
  list-style: none;
  padding: 8px;
  flex: 1;
}
.admin-nav-item {
  display: block;
  padding: 8px 12px;
  border-radius: 4px;
  color: var(--text-secondary);
  font-size: 13px;
  margin-bottom: 2px;
  text-decoration: none;
}
.admin-nav-item:hover {
  background-color: var(--bg-hover);
  color: var(--text-primary);
  text-decoration: none;
}
.admin-nav-item.active {
  background-color: var(--bg-selected);
  color: var(--text-primary);
  font-weight: 600;
}
.admin-content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  background-color: var(--bg-page);
}
.admin-header {
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.admin-title {
  font-size: 20px;
  font-weight: 600;
}
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stat-card {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 16px;
}
.stat-label {
  font-size: 11px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}
.stat-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
}
.stat-subtext {
  font-size: 11px;
  color: var(--text-secondary);
}
.meter-bar {
  height: 4px;
  background-color: var(--border-color);
  border-radius: 2px;
  margin-top: 8px;
  overflow: hidden;
}
.meter-fill {
  height: 100%;
  background-color: var(--accent-primary);
}
.meter-fill.warning {
  background-color: var(--status-warning);
}
.meter-fill.danger {
  background-color: var(--status-danger);
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 4px;
}
.data-table th, .data-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}
.data-table th {
  background-color: var(--bg-selected);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.data-table tr:last-child td {
  border-bottom: none;
}
.data-table tr:hover td {
  background-color: var(--bg-hover);
}
.badge {
  display: inline-block;
  padding: 2px 6px;
  font-size: 11px;
  border-radius: 3px;
  font-weight: 500;
}
.badge-success {
  background-color: rgba(22, 163, 74, 0.15);
  color: #4ade80;
  border: 1px solid rgba(22, 163, 74, 0.4);
}
.badge-warning {
  background-color: rgba(217, 119, 6, 0.15);
  color: #fbbf24;
  border: 1px solid rgba(217, 119, 6, 0.4);
}
.badge-danger {
  background-color: rgba(220, 38, 38, 0.15);
  color: #f87171;
  border: 1px solid rgba(220, 38, 38, 0.4);
}
.badge-neutral {
  background-color: var(--border-color);
  color: var(--text-secondary);
}
.tab-bar {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 16px;
}
.tab-link {
  padding: 8px 16px;
  font-size: 13px;
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
  cursor: pointer;
  text-decoration: none;
}
.tab-link:hover {
  color: var(--text-primary);
  text-decoration: none;
}
.tab-link.active {
  color: var(--accent-primary);
  border-bottom-color: var(--accent-primary);
  font-weight: 600;
}
.filter-bar {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  align-items: center;
  flex-wrap: wrap;
}
.code-box {
  background-color: var(--bg-page);
  border: 1px solid var(--border-color);
  border-radius: 3px;
  padding: 8px 12px;
  font-family: var(--font-mono);
  font-size: 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

/* Progress & Loading Indicators */
#global-loading-indicator {
  position: fixed;
  top: 0;
  left: 0;
  height: 2px;
  width: 0;
  background-color: var(--accent-primary);
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, width 0.3s ease;
}
#global-loading-indicator.active {
  opacity: 1;
  animation: loading-bar-indeterminate 1.1s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}
@keyframes loading-bar-indeterminate {
  0% { left: 0%; width: 0%; }
  50% { left: 25%; width: 55%; }
  100% { left: 100%; width: 0%; }
}
.htmx-indicator {
  display: none;
}
.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  display: inline-block;
}

/* Skeleton Loading States */
.skeleton-container {
  padding: 24px 28px;
  animation: skeleton-fade-in 0.15s ease;
}
.skeleton-header {
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 20px;
}
.skeleton-line {
  background-color: var(--border-color);
  border-radius: 3px;
  animation: skeleton-shimmer 1.4s ease-in-out infinite;
}
.skeleton-line.title {
  width: 50%;
  height: 20px;
  margin-bottom: 14px;
}
.skeleton-line.meta {
  width: 35%;
  height: 12px;
  margin-bottom: 8px;
}
.skeleton-line.meta-short {
  width: 25%;
  height: 12px;
  margin-bottom: 8px;
}
.skeleton-line.body-line {
  width: 100%;
  height: 13px;
  margin-bottom: 10px;
}
@keyframes skeleton-shimmer {
  0% { opacity: 0.35; }
  50% { opacity: 0.75; }
  100% { opacity: 0.35; }
}
@keyframes skeleton-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* List Pane Loading State */
.list-pane.is-loading #message-items-list {
  opacity: 0.55;
  transition: opacity 0.15s ease;
}

/* Toast Notifications */
#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 400px;
  pointer-events: none;
}
.toast-banner {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  pointer-events: auto;
}
.toast-banner.danger {
  border-color: var(--status-danger);
  border-left: 4px solid var(--status-danger);
}
.toast-banner.success {
  border-color: var(--status-success);
  border-left: 4px solid var(--status-success);
}
.toast-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  padding: 0 4px;
  cursor: pointer;
}
.toast-close:hover {
  color: var(--text-primary);
  background: none;
}

/* Responsive and Mobile Navigation */
.mobile-back-btn {
  display: none;
}

@media (max-width: 768px) {
  .app-container {
    flex-direction: column;
    height: 100vh;
    position: relative;
  }
  .nav-pane {
    width: 100%;
    min-width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }
  .nav-links {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    padding: 0;
  }
  .nav-item {
    padding: 8px 12px;
    white-space: nowrap;
    border-left: none;
    border-bottom: 2px solid transparent;
  }
  .nav-item.active {
    border-left: none;
    border-bottom-color: var(--accent-primary);
  }
  .nav-footer {
    display: none;
  }
  .list-pane {
    width: 100%;
    min-width: 100%;
    flex: 1;
    border-right: none;
  }
  .detail-pane {
    display: none;
  }
  body.mobile-detail-open .detail-pane {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
  }
  body.mobile-detail-open .list-pane,
  body.mobile-detail-open .nav-pane {
    display: none;
  }
  .mobile-back-btn {
    display: inline-block;
  }
}
