@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  --bg-deep: #0a0b0f;
  --bg-sidebar: #0d0e13;
  --bg-elevated: #14161c;
  --bg-elevated-2: #1a1c24;
  --surface-hover: rgba(255, 255, 255, 0.05);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --foreground: #ededef;
  --foreground-muted: #8a8f98;
  --foreground-faint: #5c6068;
  --accent: #5e6ad2;
  --accent-hover: #6e7ae2;
  --accent-glow: rgba(94, 106, 210, 0.25);
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #22c55e;
  --success-hover: #16a34a;
  --pending: #eab308;
  --radius: 10px;
  --radius-sm: 6px;
  --sidebar-width: 264px;
  --list-width: 380px;
  --header-height: 52px;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg-deep);
  color: var(--foreground);
  margin: 0;
  height: 100vh;
  overflow: hidden;
  font-size: 14px;
  line-height: 1.5;
}

button {
  font-family: inherit;
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hidden {
  display: none !important;
}

svg.icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Auth screens (setup / unlock) ---------- */

#auth-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-card {
  width: 380px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 2rem;
}

.auth-card h1 {
  margin: 0 0 0.4rem;
  font-size: 1.3rem;
}

.auth-card p.muted {
  margin: 0 0 1.25rem;
}

.muted {
  color: var(--foreground-muted);
}

input,
select,
textarea {
  display: block;
  width: 100%;
  padding: 0.65rem 0.8rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-deep);
  color: var(--foreground);
  font-size: 14px;
  font-family: inherit;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}

input::placeholder,
textarea::placeholder {
  color: var(--foreground-faint);
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

label {
  display: block;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--foreground-muted);
  margin-bottom: 0.35rem;
}

.field {
  margin-bottom: 1rem;
}

.btn-primary,
.btn-secondary,
.btn-danger,
.btn-success,
.btn-ghost {
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.6rem 1rem;
  font-weight: 600;
  font-size: 13.5px;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: background 150ms ease, opacity 150ms ease, transform 100ms ease;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover {
  background: var(--accent-hover);
}
.btn-primary:active {
  transform: scale(0.97);
}
.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--bg-elevated-2);
  color: var(--foreground);
  border: 1px solid var(--border);
}
.btn-secondary:hover {
  background: #20222c;
}

.btn-ghost {
  background: transparent;
  color: var(--foreground-muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover {
  color: var(--foreground);
  border-color: var(--border-strong);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.12);
  color: var(--danger);
}
.btn-danger:hover {
  background: var(--danger);
  color: #fff;
}

.btn-success {
  background: rgba(34, 197, 94, 0.12);
  color: var(--success);
}
.btn-success:hover {
  background: var(--success);
  color: #fff;
}

.error {
  color: var(--danger);
  font-size: 13px;
  min-height: 1.2em;
  margin-top: 0.6rem;
}

/* ---------- App shell (3-pane) ---------- */

#app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) var(--list-width) 1fr;
  height: 100vh;
}

/* Sidebar */

#sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.sidebar-top {
  padding: 1rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 0.85rem;
  letter-spacing: -0.01em;
}

#compose-btn {
  width: 100%;
  justify-content: center;
}

.quarantine-controls {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.quarantine-controls button {
  flex: 1;
  justify-content: center;
  font-size: 12.5px;
  padding: 0.5rem 0.6rem;
}

#quarantine-toggle-btn.paused {
  background: rgba(234, 179, 8, 0.15);
  color: var(--pending);
  border-color: transparent;
}

.account-tree {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0.5rem 0.5rem 1rem;
}

.account-group {
  margin-bottom: 0.25rem;
}

.account-header {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.5rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--foreground);
  font-weight: 600;
  font-size: 12.5px;
  user-select: none;
}

.account-header:hover {
  background: var(--surface-hover);
}

.account-header .chevron {
  transition: transform 150ms ease;
  color: var(--foreground-muted);
}

.account-header.collapsed .chevron {
  transform: rotate(-90deg);
}

.account-header .account-email {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-header .folder-badge {
  flex-shrink: 0;
}

.account-status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--success);
}
.account-status-dot.status-error {
  background: var(--danger);
}
.account-status-dot.status-pending {
  background: var(--pending);
}

.folder-list {
  margin: 0.15rem 0 0.5rem;
}

.folder-list.collapsed {
  display: none;
}

.folder-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.5rem;
  padding-left: calc(0.5rem + var(--depth, 0) * 0.85rem);
  border-radius: var(--radius-sm);
  color: var(--foreground-muted);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.folder-item:hover {
  background: var(--surface-hover);
  color: var(--foreground);
}

.folder-item.active {
  background: var(--accent-glow);
  color: #fff;
}

.folder-item.active svg.icon {
  color: #fff;
}

.folder-item.pending-folder {
  color: var(--pending);
}

.folder-item .folder-name {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.folder-badge {
  background: var(--bg-elevated-2);
  color: var(--foreground-muted);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  padding: 0.05rem 0.4rem;
  min-width: 1.2em;
  text-align: center;
}

.folder-badge.badge-pending {
  background: var(--pending);
  color: #1a1500;
}

.sidebar-bottom {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.4rem;
}
.sidebar-bottom button {
  flex: 1;
  justify-content: center;
}

/* Message list pane */

#message-list-pane {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg-deep);
}

.list-header {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
}

.list-header-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.6rem;
}

.list-header h2 {
  margin: 0;
  font-size: 15px;
}

#approve-all-btn {
  flex-shrink: 0;
}

.filter-label {
  display: block;
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--foreground-faint);
  margin-bottom: 0.35rem;
}

.chip-filter {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.65rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  min-height: 40px;
}

.chip-filter svg.icon {
  color: var(--foreground-faint);
  flex-shrink: 0;
}

.chip-filter:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  background: var(--accent-glow);
  color: #cfd3ff;
  border-radius: 999px;
  padding: 0.15rem 0.3rem 0.15rem 0.6rem;
  font-size: 12.5px;
  font-weight: 500;
}

.chip button {
  background: none;
  border: none;
  color: inherit;
  padding: 0.1rem;
  display: flex;
  border-radius: 50%;
}
.chip button:hover {
  background: rgba(255, 255, 255, 0.15);
}

#chip-input {
  flex: 1;
  min-width: 100px;
  border: none;
  background: transparent;
  padding: 0.2rem;
}
#chip-input:focus {
  box-shadow: none;
}

.message-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.message-row {
  display: flex;
  gap: 0.7rem;
  align-items: flex-start;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  position: relative;
}

.message-row:hover {
  background: var(--surface-hover);
}

.message-row.active {
  background: var(--accent-glow);
}

.message-row.kept-row {
  border-left: 3px solid var(--pending);
  padding-left: calc(1rem - 3px);
}

.message-row.unread .row-from,
.message-row.unread .row-subject {
  color: var(--foreground);
  font-weight: 700;
}

.unread-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  margin-top: 6px;
  flex-shrink: 0;
}
.message-row:not(.unread) .unread-dot {
  visibility: hidden;
}

.row-main {
  flex: 1;
  min-width: 0;
}

.row-from {
  font-size: 13px;
  color: var(--foreground-muted);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-subject {
  font-size: 13.5px;
  color: var(--foreground-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 0.1rem;
}

.row-folder {
  font-size: 11px;
  color: var(--foreground-faint);
  margin-top: 0.15rem;
}

.row-date {
  font-size: 11.5px;
  color: var(--foreground-faint);
  flex-shrink: 0;
  white-space: nowrap;
}

.row-actions {
  display: flex;
  gap: 0.3rem;
  flex-shrink: 0;
}

.icon-btn {
  border: none;
  background: var(--bg-elevated-2);
  color: var(--foreground-muted);
  border-radius: var(--radius-sm);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover {
  color: var(--foreground);
  background: #262933;
}
.icon-btn.approve:hover {
  background: var(--success);
  color: #fff;
}
.icon-btn.reject:hover {
  background: var(--danger);
  color: #fff;
}

.icon-btn.starred,
.icon-btn.kept {
  color: var(--pending);
}

.attachment-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.25rem 0.4rem 0.25rem 0.65rem;
  font-size: 12px;
  color: var(--foreground-muted);
}

.attachment-chip button {
  background: none;
  border: none;
  color: inherit;
  display: flex;
  border-radius: 50%;
  padding: 0.1rem;
}
.attachment-chip button:hover {
  background: rgba(255, 255, 255, 0.15);
  color: var(--foreground);
}

.list-empty,
.list-loading {
  padding: 2.5rem 1rem;
  text-align: center;
  color: var(--foreground-muted);
}

#list-footer {
  padding: 0.6rem 1rem;
  border-top: 1px solid var(--border);
}
#load-more-btn {
  width: 100%;
  justify-content: center;
}

/* Reading pane */

#reading-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg-deep);
}

.reading-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--foreground-faint);
}

.reading-content {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.reading-header {
  padding: 1.25rem 1.5rem 1rem;
  border-bottom: 1px solid var(--border);
}

.reading-header h3 {
  margin: 0 0 0.5rem;
  font-size: 17px;
  line-height: 1.4;
}

.reading-meta {
  color: var(--foreground-muted);
  font-size: 13px;
  margin-bottom: 0.85rem;
}

.meta-row {
  display: flex;
  gap: 0.5rem;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.meta-label {
  flex-shrink: 0;
  width: 38px;
  color: var(--foreground-faint);
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  padding-top: 0.15rem;
}

.meta-date {
  margin-top: 0.35rem;
  font-size: 12px;
  color: var(--foreground-faint);
}

.reading-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.85rem;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.7rem;
  font-size: 12.5px;
  color: var(--foreground-muted);
  cursor: pointer;
  transition: border-color 150ms ease, color 150ms ease;
}

.attachment-chip:hover {
  border-color: var(--accent);
  color: var(--foreground);
}

.attachment-chip .attachment-size {
  color: var(--foreground-faint);
}

.reading-actions {
  display: flex;
  gap: 0.5rem;
}

.reading-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1.5rem;
  white-space: pre-wrap;
  font-size: 13.5px;
  line-height: 1.65;
  color: #d4d6db;
}

/* ---------- Modals ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 560px;
  max-width: calc(100vw - 2rem);
  max-height: calc(100vh - 4rem);
  overflow-y: auto;
  padding: 1.5rem;
}

.modal-wide {
  width: 900px;
}

#attachment-viewer-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  max-height: 70vh;
  overflow: auto;
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
}

#attachment-viewer-body img {
  max-width: 100%;
  max-height: 68vh;
  display: block;
}

#attachment-viewer-body iframe {
  width: 100%;
  height: 68vh;
  border: none;
}

#attachment-viewer-body .no-preview {
  padding: 3rem;
  color: var(--foreground-muted);
  text-align: center;
}

#attachment-download-link {
  display: inline-flex;
  width: fit-content;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
}

.modal-close {
  background: none;
  border: none;
  color: var(--foreground-muted);
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-close:hover {
  background: var(--surface-hover);
  color: var(--foreground);
}

#dropzone {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 2rem;
  text-align: center;
  color: var(--foreground-muted);
  cursor: pointer;
  margin-bottom: 1rem;
  transition: border-color 150ms ease, background 150ms ease;
}

#dropzone.drag-over {
  border-color: var(--accent);
  background: var(--accent-glow);
  color: var(--foreground);
}

#import-result {
  margin-bottom: 0.75rem;
  font-size: 13px;
  color: var(--foreground-muted);
}

.account-card {
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  margin-bottom: 0.6rem;
}

.account-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.account-card-title {
  font-weight: 600;
  font-size: 13.5px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.1rem 0.55rem;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
  margin-left: 0.5rem;
}
.status-connected {
  background: rgba(34, 197, 94, 0.15);
  color: var(--success);
}
.status-pending {
  background: rgba(234, 179, 8, 0.15);
  color: var(--pending);
}
.status-error {
  background: rgba(239, 68, 68, 0.15);
  color: var(--danger);
}

.account-card-meta {
  font-size: 12px;
  color: var(--foreground-muted);
  margin-top: 0.3rem;
}

.account-card-error {
  font-size: 12px;
  color: var(--danger);
  margin-top: 0.3rem;
}

.account-card-actions {
  margin-top: 0.7rem;
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.manual-host-form {
  margin-top: 0.7rem;
  display: none;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.manual-host-form.open {
  display: flex;
}
.manual-host-form input {
  flex: 1;
  min-width: 140px;
}
.manual-host-form button {
  flex-basis: 100%;
}

/* Scrollbars */

::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--bg-elevated-2);
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover {
  background: #2a2d38;
  background-clip: padding-box;
}

/* ---------- Mobile: one pane at a time, driven by data-mobile-view on #app-shell ---------- */

.mobile-back-btn {
  display: none;
}

.reading-header .mobile-back-btn {
  margin-bottom: 0.75rem;
}

.mobile-nav-only {
  display: none;
}

@media (max-width: 860px) {
  #app-shell {
    display: block;
    position: relative;
    height: 100vh;
    overflow: hidden;
  }

  #sidebar,
  #message-list-pane,
  #reading-pane {
    position: absolute;
    inset: 0;
    width: 100%;
    max-width: 100%;
  }

  /* Default: only the sidebar (account/folder picker) is visible. */
  #message-list-pane,
  #reading-pane {
    display: none;
  }

  #app-shell[data-mobile-view="list"] #sidebar {
    display: none;
  }
  #app-shell[data-mobile-view="list"] #message-list-pane {
    display: flex;
  }

  #app-shell[data-mobile-view="reading"] #sidebar,
  #app-shell[data-mobile-view="reading"] #message-list-pane {
    display: none;
  }
  #app-shell[data-mobile-view="reading"] #reading-pane {
    display: flex;
  }

  .mobile-back-btn {
    display: inline-flex;
  }

  .mobile-nav-only {
    display: block;
  }

  .list-header-title-row {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  #approve-all-btn {
    font-size: 12.5px;
    padding: 0.45rem 0.7rem;
  }

  /* Touch targets need real room; mouse-sized 28px icon buttons don't cut it. */
  .icon-btn {
    width: 40px;
    height: 40px;
  }

  .row-actions {
    gap: 0.15rem;
  }

  /* 16px prevents iOS Safari's auto-zoom-on-focus. */
  input,
  select,
  textarea {
    font-size: 16px;
  }

  .modal {
    width: calc(100vw - 1.5rem);
  }

  .modal-wide {
    width: calc(100vw - 1.5rem);
  }

  .reading-actions {
    flex-wrap: wrap;
  }

  #toast-container {
    left: 0.75rem;
    right: 0.75rem;
    bottom: 0.75rem;
  }

  .toast {
    width: 100%;
  }
}

/* ---------- Loading spinner (in-flight IMAP actions) ---------- */

.btn-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: currentColor;
  border-radius: 50%;
  display: inline-block;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

button:disabled {
  opacity: 0.6;
  cursor: default;
}

/* ---------- Toasts (action confirmation) ---------- */

#toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 200;
}

.toast {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0.65rem 1rem;
  font-size: 13px;
  color: var(--foreground);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  animation: toast-in 200ms ease-out;
}

.toast.toast-success {
  border-left: 3px solid var(--success);
}
.toast.toast-error {
  border-left: 3px solid var(--danger);
}

.toast.toast-leaving {
  animation: toast-out 150ms ease-in forwards;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes toast-out {
  to {
    opacity: 0;
    transform: translateY(8px);
  }
}
