/* =============================================================================
   Reusable UI component library — shadcn/ui-inspired, hand-rolled in plain CSS.
   ========================================================================== */

/* --- Buttons ----------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: 38px;
  padding: 0 var(--space-4);
  font-size: var(--text-sm);
  font-weight: 500;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  white-space: nowrap;
  transition: background-color var(--duration-fast) var(--ease-out), border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out), transform var(--duration-fast) var(--ease-out), box-shadow var(--duration-fast) var(--ease-out);
  user-select: none;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; pointer-events: none; }
.btn svg { width: 16px; height: 16px; flex-shrink: 0; }

.btn-primary {
  background: var(--accent);
  color: var(--text-on-accent);
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover { background: var(--accent-strong); box-shadow: var(--shadow-glow); }

.btn-secondary {
  background: var(--bg-surface-raised);
  border-color: var(--border-default);
  color: var(--text-primary);
}
.btn-secondary:hover { background: var(--bg-hover); border-color: var(--border-strong); }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}
.btn-ghost:hover { background: var(--bg-hover); color: var(--text-primary); }

.btn-danger {
  background: var(--danger-500);
  color: white;
}
.btn-danger:hover { background: var(--danger-600); }

.btn-outline-danger {
  background: transparent;
  border-color: var(--danger-500);
  color: var(--danger-500);
}
.btn-outline-danger:hover { background: rgba(244, 63, 94, 0.1); }

.btn-sm { height: 30px; padding: 0 var(--space-3); font-size: var(--text-xs); border-radius: var(--radius-sm); }
.btn-lg { height: 46px; padding: 0 var(--space-6); font-size: var(--text-md); }
.btn-icon { width: 38px; padding: 0; }
.btn-icon.btn-sm { width: 30px; }
.btn-block { width: 100%; }

/* --- Inputs -------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: var(--space-2); margin-bottom: var(--space-4); }
.field-row { display: flex; gap: var(--space-4); }
.field-row > .field { flex: 1; min-width: 0; }
/* Used for short fields (port numbers, etc.) sharing a row with a wide
   one. The 640px breakpoint (layout.css) drops this back to full width
   once field-rows stack vertically on narrow screens. */
.field-row > .field.field-narrow { flex: 0 0 140px; max-width: 140px; }

.label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-primary);
}
.label-hint { font-weight: 400; color: var(--text-tertiary); }

.input, .textarea, select.input {
  width: 100%;
  height: 40px;
  padding: 0 var(--space-3);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--text-sm);
  transition: border-color var(--duration-fast) var(--ease-out), box-shadow var(--duration-fast) var(--ease-out), background-color var(--duration-fast) var(--ease-out);
}
.input::placeholder, .textarea::placeholder { color: var(--text-tertiary); }
.input:hover, .textarea:hover { border-color: var(--border-strong); }
.input:focus, .textarea:focus, select.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft-bg);
}
.textarea { height: auto; min-height: 96px; padding: var(--space-3); resize: vertical; }
.help-text { font-size: var(--text-xs); color: var(--text-tertiary); }
.field-error { font-size: var(--text-xs); color: var(--danger-500); }
.input.has-error, .textarea.has-error { border-color: var(--danger-500); }

.checkbox-row { display: flex; align-items: center; gap: var(--space-2); }
.checkbox-row input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }

/* Switch ------------------------------------------------------------------- */
.switch { position: relative; display: inline-flex; align-items: center; width: 40px; height: 22px; }
.switch__input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch__track {
  position: absolute; inset: 0;
  background: var(--bg-active);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-full);
  transition: background-color var(--duration-base) var(--ease-out);
}
.switch__thumb {
  position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px;
  background: white;
  border-radius: 50%;
  box-shadow: var(--shadow-sm);
  transition: transform var(--duration-base) var(--ease-out);
}
.switch__input:checked ~ .switch__track { background: var(--accent); border-color: var(--accent); }
.switch__input:checked ~ .switch__track .switch__thumb { transform: translateX(18px); }
.switch__input:checked ~ .switch__track ~ .switch__thumb { transform: translateX(18px); }

/* --- Cards ----------------------------------------------------------------- */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-glass {
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border-subtle);
}
.card-header { padding: var(--space-5) var(--space-6); border-bottom: 1px solid var(--border-subtle); display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); }
.card-body { padding: var(--space-6); }
.card-footer { padding: var(--space-4) var(--space-6); border-top: 1px solid var(--border-subtle); }
.card-title { font-size: var(--text-md); font-weight: 600; }
.card-subtitle { font-size: var(--text-sm); color: var(--text-tertiary); margin-top: 2px; }
.card-interactive { transition: border-color var(--duration-base) var(--ease-out), transform var(--duration-base) var(--ease-out), box-shadow var(--duration-base) var(--ease-out); }
.card-interactive:hover { border-color: var(--border-strong); box-shadow: var(--shadow-md); transform: translateY(-1px); }

/* --- Badges ------------------------------------------------------------ */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  height: 20px;
  padding: 0 var(--space-2);
  font-size: var(--text-xs);
  font-weight: 500;
  border-radius: var(--radius-full);
  border: 1px solid transparent;
  white-space: nowrap;
}
.badge-neutral { background: var(--bg-active); color: var(--text-secondary); }
.badge-accent { background: var(--accent-soft-bg); color: var(--accent-strong); border-color: var(--accent-soft-border); }
.badge-success { background: rgba(34, 197, 94, 0.12); color: var(--success-500); }
.badge-warning { background: rgba(245, 158, 11, 0.12); color: var(--warning-500); }
.badge-danger { background: rgba(244, 63, 94, 0.12); color: var(--danger-500); }
.badge-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

/* --- Alerts / flash messages ---------------------------------------------- */
.alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-default);
  background: var(--bg-surface-raised);
  font-size: var(--text-sm);
  animation: fade-in var(--duration-base) var(--ease-out);
}
.alert svg { width: 18px; height: 18px; flex-shrink: 0; margin-top: 1px; }
.alert-success { background: rgba(34, 197, 94, 0.08); border-color: rgba(34, 197, 94, 0.25); color: #4ade80; }
.alert-error { background: rgba(244, 63, 94, 0.08); border-color: rgba(244, 63, 94, 0.25); color: #fb7185; }
.alert-warning { background: rgba(245, 158, 11, 0.08); border-color: rgba(245, 158, 11, 0.25); color: #fbbf24; }
.alert-info { background: rgba(14, 165, 233, 0.08); border-color: rgba(14, 165, 233, 0.25); color: #38bdf8; }

/* --- Toasts ------------------------------------------------------------ */
.toast-region {
  position: fixed;
  bottom: var(--space-5);
  right: var(--space-5);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: min(360px, calc(100vw - 2 * var(--space-5)));
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--bg-surface-overlay);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  animation: toast-in var(--duration-slow) var(--ease-out);
}
.toast.is-leaving { animation: toast-out var(--duration-base) var(--ease-in-out) forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateX(16px) scale(0.98); } to { opacity: 1; transform: translateX(0) scale(1); } }
@keyframes toast-out { to { opacity: 0; transform: translateX(16px) scale(0.98); } }

/* --- Dropdowns / menus ------------------------------------------------------ */
.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
  position: absolute;
  z-index: var(--z-dropdown);
  min-width: 200px;
  padding: var(--space-2);
  background: var(--bg-surface-overlay);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translateY(-4px) scale(0.98);
  pointer-events: none;
  transition: opacity var(--duration-fast) var(--ease-out), transform var(--duration-fast) var(--ease-out);
}
.dropdown-menu.is-open { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; }
.dropdown-menu[data-align="right"] { right: 0; }
.dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  color: var(--text-primary);
  text-align: left;
}
.dropdown-item:hover { background: var(--bg-hover); }
.dropdown-item.is-danger { color: var(--danger-500); }
.dropdown-item svg { width: 15px; height: 15px; color: var(--text-tertiary); }
.dropdown-divider { height: 1px; background: var(--border-subtle); margin: var(--space-2) 0; }
.dropdown-label { padding: var(--space-1) var(--space-3); font-size: var(--text-xs); color: var(--text-tertiary); text-transform: uppercase; letter-spacing: 0.04em; }

/* --- Modals / dialogs -------------------------------------------------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(4, 4, 8, 0.6);
  backdrop-filter: blur(2px);
  z-index: var(--z-modal-backdrop);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-base) var(--ease-out);
}
.modal-backdrop.is-open { opacity: 1; pointer-events: auto; }
.modal {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -48%) scale(0.98);
  width: min(480px, calc(100vw - 2rem));
  max-height: calc(100vh - 4rem);
  overflow-y: auto;
  background: var(--bg-surface-raised);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-modal);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-base) var(--ease-out), transform var(--duration-base) var(--ease-out);
}
.modal.is-open { opacity: 1; transform: translate(-50%, -50%) scale(1); pointer-events: auto; }
.modal.modal-lg { width: min(680px, calc(100vw - 2rem)); }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: var(--space-5) var(--space-6); border-bottom: 1px solid var(--border-subtle); }
.modal-body { padding: var(--space-6); }
.modal-footer { display: flex; justify-content: flex-end; gap: var(--space-3); padding: var(--space-4) var(--space-6); border-top: 1px solid var(--border-subtle); }

/* --- Tabs --------------------------------------------------------------- */
.tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--border-subtle); }
.tab {
  padding: var(--space-3) var(--space-1);
  margin-right: var(--space-5);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-tertiary);
  border-bottom: 2px solid transparent;
  transition: color var(--duration-fast) var(--ease-out), border-color var(--duration-fast) var(--ease-out);
}
.tab:hover { color: var(--text-primary); }
.tab.is-active { color: var(--text-primary); border-color: var(--accent); }

/* --- Tooltips ------------------------------------------------------------- */
.tooltip { position: relative; }
.tooltip::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%) translateY(2px);
  padding: 5px 9px;
  background: #000;
  color: #fff;
  font-size: var(--text-xs);
  white-space: nowrap;
  border-radius: var(--radius-sm);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-fast) var(--ease-out), transform var(--duration-fast) var(--ease-out);
  z-index: var(--z-tooltip);
}
.tooltip:hover::after { opacity: 1; transform: translateX(-50%) translateY(0); }

/* --- Tables --------------------------------------------------------------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); }
table.table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
table.table th {
  text-align: left;
  font-weight: 500;
  color: var(--text-tertiary);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-surface-raised);
}
table.table td { padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--border-subtle); }
table.table tbody tr:last-child td { border-bottom: none; }
table.table tbody tr:hover { background: var(--bg-hover); }

/* --- Avatars -------------------------------------------------------------- */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 600;
  color: white;
  flex-shrink: 0;
  background: var(--accent);
}
.avatar-sm { width: 26px; height: 26px; font-size: 10px; }
.avatar-lg { width: 48px; height: 48px; font-size: var(--text-sm); }

/* --- Breadcrumbs ------------------------------------------------------------ */
.breadcrumbs { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-sm); color: var(--text-tertiary); }
.breadcrumbs a:hover { color: var(--text-primary); }
.breadcrumbs .sep { color: var(--text-disabled); }

/* --- Empty states ------------------------------------------------------- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-16) var(--space-6);
  color: var(--text-tertiary);
}
.empty-state-icon {
  width: 56px; height: 56px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-lg);
  background: var(--bg-surface-raised);
  border: 1px solid var(--border-subtle);
  margin-bottom: var(--space-4);
  color: var(--text-tertiary);
}
.empty-state-icon svg { width: 24px; height: 24px; }
.empty-state h3 { color: var(--text-primary); font-size: var(--text-md); margin-bottom: var(--space-1); }
.empty-state p { max-width: 360px; font-size: var(--text-sm); margin-bottom: var(--space-4); }

/* --- Pagination ------------------------------------------------------------ */
.pagination { display: flex; align-items: center; gap: var(--space-1); flex-wrap: wrap; row-gap: var(--space-2); }
.pagination-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 32px; height: 32px;
  padding: 0 var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  color: var(--text-secondary);
}
.pagination-btn:hover:not(.is-disabled) { background: var(--bg-hover); color: var(--text-primary); }
.pagination-btn.is-active { background: var(--accent); color: var(--text-on-accent); }
.pagination-btn.is-disabled { opacity: 0.35; pointer-events: none; }
.pagination-ellipsis { color: var(--text-disabled); padding: 0 var(--space-1); }
.pagination-jump { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-sm); color: var(--text-tertiary); }
.pagination-jump input { width: 56px; height: 32px; text-align: center; }

/* --- Command palette -------------------------------------------------------- */
.command-palette-backdrop {
  position: fixed; inset: 0;
  background: rgba(4, 4, 8, 0.55);
  z-index: var(--z-command-palette);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-base) var(--ease-out);
}
.command-palette-backdrop.is-open { opacity: 1; pointer-events: auto; }
.command-palette {
  position: fixed;
  top: 14%;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  width: min(560px, calc(100vw - 2rem));
  background: var(--bg-surface-overlay);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  z-index: calc(var(--z-command-palette) + 1);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-base) var(--ease-out), transform var(--duration-base) var(--ease-out);
  overflow: hidden;
}
.command-palette.is-open { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; }
.command-palette-input {
  width: 100%;
  height: 54px;
  padding: 0 var(--space-5);
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border-subtle);
  font-size: var(--text-md);
  color: var(--text-primary);
}
.command-palette-input:focus { outline: none; }
.command-palette-results { max-height: 360px; overflow-y: auto; padding: var(--space-2); }
.command-palette-item {
  display: flex; align-items: center; gap: var(--space-3);
  width: 100%;
  padding: var(--space-3) var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
}
.command-palette-item svg { width: 16px; height: 16px; color: var(--text-tertiary); }
.command-palette-item.is-selected { background: var(--bg-selected); }
.command-palette-item .kbd { margin-left: auto; }
.command-palette-empty { padding: var(--space-8); text-align: center; color: var(--text-tertiary); font-size: var(--text-sm); }

.kbd {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 20px; height: 20px; padding: 0 6px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-tertiary);
  background: var(--bg-active);
  border: 1px solid var(--border-default);
  border-radius: 4px;
}

/* --- Status pill (account connection state) --------------------------------- */
.status-pill { display: inline-flex; align-items: center; gap: 6px; font-size: var(--text-xs); }
.status-pill .dot { width: 7px; height: 7px; border-radius: 50%; }
.status-connected .dot { background: var(--success-500); box-shadow: 0 0 0 3px rgba(34,197,94,0.18); }
.status-error .dot { background: var(--danger-500); box-shadow: 0 0 0 3px rgba(244,63,94,0.18); }
.status-unverified .dot { background: var(--text-tertiary); }
.status-syncing .dot { background: var(--warning-500); animation: pulse-dot 1.2s ease-in-out infinite; }
@keyframes pulse-dot { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* --- Context menu ------------------------------------------------------- */
.context-menu {
  position: fixed;
  z-index: var(--z-dropdown);
  min-width: 190px;
  padding: var(--space-2);
  background: var(--bg-surface-overlay);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: scale(0.98);
  pointer-events: none;
  transition: opacity var(--duration-fast) var(--ease-out), transform var(--duration-fast) var(--ease-out);
}
.context-menu.is-open { opacity: 1; transform: scale(1); pointer-events: auto; }

/* --- Filter chips (pill-shaped toggle buttons) -------------------------------- */
/* Generic, not mail-specific — used by the mail search filter bar AND the
   dashboard's mail-count widget, and each page only loads its own
   page-specific stylesheet, so this needs to live somewhere always loaded. */
.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  height: 32px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-full);
  border: 1px solid var(--border-default);
  background: var(--bg-surface);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-secondary);
  transition: border-color var(--duration-fast) var(--ease-out), background-color var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.filter-chip:hover { border-color: var(--border-strong); color: var(--text-primary); background: var(--bg-hover); }
.filter-chip.is-active { background: var(--accent-soft-bg); border-color: var(--accent-soft-border); color: var(--accent-strong); }
.filter-chip svg { width: 14px; height: 14px; }

/* --- Loading overlay (HTMX-driven) ------------------------------------------- */
/* Full-cover overlay for any container mid-fetch: give the container
   `position: relative` and an `hx-indicator` pointing at an element with
   this class. HTMX toggles `.htmx-request` on that element for exactly
   the lifetime of the request; we ride that instead of the default
   `.htmx-indicator` behaviour so we can fade a full overlay + spinner,
   not just a bare icon. Used by the mail reading pane and the dashboard's
   mail-count widget — kept here (not in mail.css/dashboard.css) because
   it's genuinely generic and each page only loads its own stylesheet. */
.pane-loader {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  background: color-mix(in srgb, var(--bg-canvas) 72%, transparent);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-fast) var(--ease-out);
}
.pane-loader.htmx-request {
  opacity: 1;
  pointer-events: auto;
}
.pane-loader-text {
  font-size: var(--text-sm);
  color: var(--text-secondary);
}
.spinner-lg {
  width: 30px;
  height: 30px;
  border-width: 3px;
}
