:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --bg-subtle: #f6f7f9;
  --bg-hover: #eef0f3;
  --border: #e2e5ea;
  --text: #16181d;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --focus-ring: #2563eb;
  --unread-dot: #2563eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181c;
    --bg-subtle: #1e1f24;
    --bg-hover: #26282e;
    --border: #303239;
    --text: #f2f3f5;
    --text-muted: #9aa0aa;
    --accent: #4f8cff;
    --accent-text: #0b0d10;
    --danger: #f87171;
    --focus-ring: #4f8cff;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
  background: var(--bg);
  color: var(--text);
  touch-action: manipulation;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  font-weight: 500;
}

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

.btn-primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: transparent;
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-block {
  width: 100%;
}

.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  padding: 0.25rem 0;
}

.btn-link:hover {
  text-decoration: underline;
}

.btn-icon {
  background: none;
  border: none;
  font-size: 1.25rem;
  line-height: 1;
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
  color: var(--text-muted);
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text);
}

/* Login */
.login-screen {
  height: 100%;
  display: grid;
  place-items: center;
  background: var(--bg-subtle);
}

.login-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
  width: min(320px, 90vw);
  text-align: center;
}

.login-card h1 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
}

.login-card p {
  color: var(--text-muted);
  margin: 0 0 1.25rem;
}

.login-error {
  color: var(--danger);
  margin-top: 1rem;
  font-size: 0.875rem;
}

/* App shell */
.app {
  height: 100%;
  display: grid;
  grid-template-columns: 220px 320px 1fr;
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
  }
  .thread-list-pane,
  .reading-pane {
    display: none;
  }
  .app.view-threads .thread-list-pane {
    display: flex;
  }
  .app.view-reading .reading-pane {
    display: flex;
  }
}

.sidebar {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem;
  gap: 1rem;
  background: var(--bg-subtle);
  min-width: 0;
}

.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.brand {
  font-weight: 700;
  font-size: 1.05rem;
}

.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.sidebar-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

#mailbox-select {
  width: 100%;
  padding: 0.4rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background-color: var(--bg);
  color: var(--text);
}

.folder-nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  flex: 1;
}

.folder-item {
  text-align: left;
  background: none;
  border: none;
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  color: var(--text);
}

.folder-item:hover {
  background: var(--bg-hover);
}

.folder-item.active {
  background: var(--bg-hover);
  font-weight: 600;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

.thread-list-pane {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.pane-header {
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
}

#search-input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
}

.thread-list {
  overflow-y: auto;
  flex: 1;
}

.thread-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
  background: none;
  border-left: 3px solid transparent;
  min-width: 0;
}

.thread-item:hover {
  background: var(--bg-hover);
}

.thread-item.selected {
  background: var(--bg-hover);
  border-left-color: var(--accent);
}

.thread-item.unread .thread-subject {
  font-weight: 700;
}

.thread-item-top {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  min-width: 0;
}

.thread-from {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.thread-time {
  color: var(--text-muted);
  font-size: 0.75rem;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.thread-subject {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.thread-snippet {
  color: var(--text-muted);
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.thread-empty,
.empty-state {
  color: var(--text-muted);
  padding: 2rem 1rem;
  text-align: center;
}

.reading-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-y: auto;
}

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

.back-to-list {
  display: none;
  flex-shrink: 0;
}

@media (max-width: 900px) {
  .back-to-list {
    display: inline-flex;
  }
}

.reading-header h2 {
  margin: 0;
  font-size: 1.1rem;
  text-wrap: balance;
  overflow-wrap: break-word;
  flex: 1;
  min-width: 0;
}

.reading-actions {
  display: flex;
  gap: 0.35rem;
  flex-shrink: 0;
}

.message {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.message-meta {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.5rem;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.message-meta strong {
  color: var(--text);
}

.message-body {
  white-space: pre-wrap;
  overflow-wrap: break-word;
  line-height: 1.5;
}

.message-attachments {
  margin-top: 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.attachment-chip {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.25rem 0.75rem;
  font-size: 0.8rem;
  background: var(--bg-subtle);
  text-decoration: none;
  color: var(--text);
}

.attachment-chip:hover {
  background: var(--bg-hover);
}

.message-reply-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

/* Compose overlay */
.compose-overlay {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.35);
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 1.5rem;
  z-index: 20;
}

.compose-window {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: min(560px, 100%);
  max-height: min(640px, 90vh);
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
}

.compose-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.compose-body {
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow-y: auto;
  flex: 1;
}

.compose-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.4rem;
}

.compose-row span {
  width: 3.5rem;
  flex-shrink: 0;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.compose-row input,
#compose-from {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  padding: 0.3rem 0;
}

#compose-from {
  background-color: var(--bg);
}

.compose-textarea {
  flex: 1;
  min-height: 200px;
  resize: vertical;
  border: none;
  padding: 0.5rem 0;
  background: none;
}

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

.compose-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
}

.compose-footer-actions {
  display: flex;
  gap: 0.5rem;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
