html {
  font-size: 14px;
}

@media (min-width: 768px) {
  html {
    font-size: 16px;
  }
}

.btn:focus, .btn:active:focus, .btn-link.nav-link:focus, .form-control:focus, .form-check-input:focus {
  box-shadow: 0 0 0 0.25rem rgba(var(--mt-brand-rgb), 0.3);
}

html {
  position: relative;
  min-height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.form-floating > .form-control-plaintext::placeholder, .form-floating > .form-control::placeholder {
  color: var(--bs-secondary-color);
  text-align: end;
}

.form-floating > .form-control-plaintext:focus::placeholder, .form-floating > .form-control:focus::placeholder {
  text-align: start;
}

/* ---- Mailbox browser (3-pane reader) ---- */
.mailbox-browser {
  overflow: hidden;
}

.mb-grid {
  display: grid;
  /* Two fixed panes + two drag gutters + a flexible reading pane. The first/third
     widths are overridden inline (and persisted per user) once the page loads. */
  grid-template-columns: 240px 6px minmax(260px, 360px) 6px 1fr;
  /* Pin the single row to the grid's height so each pane is bounded and its
     body scrolls internally instead of overflowing (and being clipped). */
  grid-template-rows: minmax(0, 1fr);
  height: calc(100vh - 240px);
  min-height: 420px;
}

/* Draggable divider between panes. */
.mb-gutter {
  cursor: col-resize;
  background: var(--bs-border-color);
  transition: background-color 0.15s ease;
}

.mb-gutter:hover,
.mb-gutter.mb-gutter-active {
  background: var(--bs-primary);
}

/* While dragging, keep the col-resize cursor and stop the panes selecting text. */
body.mb-resizing {
  cursor: col-resize;
  user-select: none;
}

.mb-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;   /* allow the scrollable body to shrink below its content */
  overflow: hidden;
  border-right: 1px solid var(--bs-border-color);
}

.mb-pane:last-child {
  border-right: 0;
}

.mb-pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.625rem 0.75rem;
  font-weight: 600;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--bs-border-color);
  background: var(--bs-tertiary-bg);
  min-width: 0;
}

.mb-pane-body {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
}

/* Folder tree */
.mb-folder-list,
.mb-message-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.mb-folder {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: inherit;
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mb-folder:hover {
  background: var(--bs-secondary-bg);
}

.mb-folder.active {
  background: rgba(var(--mt-brand-rgb), 0.15);
  font-weight: 600;
}

/* Folders the scanner examines. The solid green reads well on white but goes muddy against the
   dark pane, so dark mode uses Bootstrap's lighter emphasis tint instead. */
.mb-folder-icon-scanned {
  color: var(--bs-success);
}

[data-bs-theme="dark"] .mb-folder-icon-scanned {
  color: var(--bs-success-text-emphasis, #75b798);
}

/* Sort / group toolbar above the message list */
.mb-toolbar {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid var(--bs-border-color);
  background: var(--bs-body-bg);
}

.mb-toolbar-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--bs-secondary-color);
  text-transform: uppercase;
  letter-spacing: 0.02em;
  margin: 0;
}

.mb-toolbar-select {
  width: auto;
  min-width: 0;
  flex: 0 0 auto;
}

.mb-toolbar-dir {
  flex: 0 0 auto;
  line-height: 1;
}

/* Conversation / sender group header rows in the message list */
.mb-group-header {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 0.35rem 0.75rem;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--bs-secondary-color);
  background: var(--bs-tertiary-bg);
  border-bottom: 1px solid var(--bs-border-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mb-group-header .mb-group-count {
  font-weight: 600;
  color: var(--bs-secondary-color);
  opacity: 0.75;
}

/* Infinite-scroll sentinel + "loading more" row */
.mb-sentinel {
  height: 1px;
}

.mb-list-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  color: var(--bs-secondary-color);
  font-size: 0.85rem;
}

/* Message list */
.mb-message {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--bs-border-color);
  cursor: pointer;
}

.mb-message:hover {
  background: var(--bs-secondary-bg);
}

.mb-message.active {
  background: rgba(var(--mt-brand-rgb), 0.15);
}

.mb-message.unread .mb-message-from,
.mb-message.unread .mb-message-subject {
  font-weight: 700;
}

/* Live "a scan job is currently working on this message" highlight. */
.mb-message.mb-scanning {
  box-shadow: inset 3px 0 0 0 var(--bs-primary);
  animation: mb-scan-pulse 1.4s ease-in-out infinite;
}

@keyframes mb-scan-pulse {
  0%, 100% { background: rgba(var(--mt-brand-rgb), 0.06); }
  50% { background: rgba(var(--mt-brand-rgb), 0.18); }
}

/* While a scan job is processing this row, swap its status square for a spinning ring so the
   user can see exactly which message the job is on. */
.mb-message.mb-scanning .mb-status { position: relative; }
.mb-message.mb-scanning .mb-status > i { visibility: hidden; }
.mb-message.mb-scanning .mb-status::after {
  content: "";
  position: absolute;
  inset: 3px;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: mb-status-spin 0.7s linear infinite;
}

@keyframes mb-status-spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .mb-message.mb-scanning .mb-status::after { animation: none; }
}

/* Safety scorecard checklist. */
.mb-scorecard-list > li {
  border-bottom: 1px solid var(--bs-border-color-translucent);
}
.mb-scorecard-list > li:last-child {
  border-bottom: 0;
}

/* Activity → "What we examined". The check details are full sentences and the block now carries the
   rules and audit outcome too, so the dialog takes most of the viewport rather than a slim column
   that wrapped every detail to four or five lines. */
.act-scorecard-dialog {
  max-width: min(1140px, 96vw);
}
.act-scorecard-dialog .modal-content {
  min-height: min(85vh, 900px);
}
.act-scorecard-dialog .act-check-detail {
  min-width: 22rem;
}

.mb-message-row {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  align-items: baseline;
}

.mb-message-from {
  font-size: 0.9rem;
  min-width: 0;
  flex: 1 1 auto;
}

/* Per-row safety status: a small square icon (no text) with a native hover tooltip. */
.mb-status {
  flex: 0 0 auto;
  align-self: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  font-size: 0.7rem;
  line-height: 1;
  color: #fff;
}

.mb-status-safe { background-color: var(--bs-success); }
/* Red: a blacklist rule or an explicit block decided it. */
.mb-status-blocked { background-color: var(--bs-danger); }
/* Amber: only inferred — the AI, or a match against mail the user flagged before. Dark glyph,
   because white on amber falls short of a readable contrast at this size. */
.mb-status-flagged { background-color: var(--bs-warning); color: var(--bs-emphasis-color, #000); }
/* Grey hourglass: unscanned or undecided — needs review. */
.mb-status-pending { background-color: var(--bs-secondary); }

/* Review-only mode (the mask button). The server already leaves settled mail out of the page, so
   this only has to collapse a row the moment it stops needing review — a message decided in the
   reader, or one a rule turns out to cover — without re-querying the list under the user. */
.mb-message.mb-settled,
.mb-group-header.mb-settled {
  display: none;
}

/* The mask has to read as on or off at a glance, and the other header buttons are already the link
   colour — so amber, which nothing else up there uses, rather than a shade of the same indigo. */
#mb-review-only.active,
#mb-review-only.active:hover {
  color: var(--bs-warning);
}

.mb-message-date {
  font-size: 0.75rem;
  color: var(--bs-secondary-color);
  white-space: nowrap;
  flex: 0 0 auto;
}

.mb-message-subject {
  font-size: 0.85rem;
  color: var(--bs-secondary-color);
}

/* Reading pane */
.mb-reader-head {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--bs-border-color);
}

.mb-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.mb-attachments-label {
  flex-basis: 100%;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--bs-secondary-color);
}

/* "Flag as…" menu: a bold category name with a muted one-line explanation.
   Roughly double Bootstrap's default 10rem menu width so descriptions read on one line. */
.mb-flag-menu {
  min-width: 18rem;
  max-width: 21rem;
}

.mb-flag-item {
  white-space: normal;
  padding-top: 0.3rem;
  padding-bottom: 0.3rem;
}

.mb-flag-name {
  display: block;
  font-weight: 600;
}

.mb-flag-desc {
  display: block;
  font-size: 0.75rem;
  line-height: 1.2;
  color: var(--bs-secondary-color);
}

.dropdown-item:hover .mb-flag-desc,
.dropdown-item:focus .mb-flag-desc {
  color: inherit;
}

/* The per-message safety actions (Mark safe / Block / Flag as…) while they are unavailable —
   the message is still downloading, a scan is working on it, or its verdict is already settled.
   Bootstrap only dims a disabled outline button, and a dimmed green or red still reads as
   "press me" on the dark surface, so drop the colour entirely: grey means grey. */
#mb-mark-safe:disabled,
#mb-blacklist:disabled,
#mb-flag:disabled,
.mb-quick-flag:disabled {
  color: var(--bs-secondary-color);
  border-color: var(--mt-border);
  background-color: transparent;
  opacity: 0.65;
}

.mb-attachment {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--bs-border-color);
  border-radius: 0.375rem;
  font-size: 0.8rem;
  text-decoration: none;
  background: var(--bs-tertiary-bg);
}

.mb-attachment:hover {
  background: var(--bs-secondary-bg);
}

.mb-body-frame {
  width: 100%;
  border: 0;
  min-height: 200px;
  display: block;
}

/* ---- Sender-domain group view (fills the reading pane) ---- */
.mb-groups {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.mb-groups-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid var(--bs-border-color);
  background: var(--bs-tertiary-bg);
}

.mb-groups-body {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
}

/* One sender domain: clickable summary + score + actions menu. */
.mb-domain-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--bs-border-color);
}

.mb-domain-row:hover {
  background: var(--bs-secondary-bg);
}

.mb-domain-row.active {
  background: rgba(var(--mt-brand-rgb), 0.12);
}

/* Sender favicon: a fixed square at the left edge of the row, spanning name, meta and bar.
   The real icon is layered over a generic globe, so a domain with no favicon of its own shows
   the placeholder rather than a broken image. */
.mb-domain-icon {
  position: relative;
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 0.375rem;
  overflow: hidden;
  background: var(--bs-tertiary-bg);
}

/* A light backing behind the icon itself: most favicons are drawn for a white page, and
   black-on-transparent marks (github, nytimes) vanish entirely on the dark theme without it. */
.mb-domain-icon img {
  position: relative;   /* above the placeholder */
  width: 22px;
  height: 22px;
  object-fit: contain;
  background: #fff;
  border-radius: 0.25rem;
  padding: 1px;
}

.mb-domain-icon-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--bs-secondary-color);
  font-size: 0.95rem;
}

.mb-domain-main {
  flex: 1 1 auto;
  min-width: 0;
  cursor: pointer;
}

.mb-domain-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.mb-domain-meta {
  font-size: 0.75rem;
  color: var(--bs-secondary-color);
}

/* Flagged / safe / unscanned split of the domain's mail. */
.mb-domain-bar {
  display: flex;
  height: 4px;
  margin-top: 0.35rem;
  border-radius: 2px;
  overflow: hidden;
  background: var(--bs-border-color);
}

.mb-domain-seg { display: block; height: 100%; }
.mb-domain-seg-bad { background-color: var(--bs-danger); }
.mb-domain-seg-review { background-color: var(--bs-secondary); }
.mb-domain-seg-good { background-color: var(--bs-success); }
.mb-domain-seg-none { background-color: var(--bs-secondary-bg); }

/* Average safety score of the domain's scanned mail — the slot an AI-derived
   domain verdict will take over once we score domains as a whole. */
.mb-domain-score {
  flex: 0 0 auto;
  min-width: 2.25rem;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 0.15rem 0.35rem;
  border-radius: 0.375rem;
  color: #fff;
}

.mb-domain-score-bad { background-color: var(--bs-danger); }
.mb-domain-score-warn { background-color: var(--bs-warning); color: #000; }
.mb-domain-score-good { background-color: var(--bs-success); }
.mb-domain-score-none {
  background: transparent;
  color: var(--bs-secondary-color);
}

.mb-domain-actions { flex: 0 0 auto; }

/* Active state for the header's group toggle. */
#mb-groups-btn.active { color: var(--bs-primary); }

/* The message list's search bar, revealed by the header's magnifier. Sits between the pane header
   and the sort/group toolbar, so the field appears directly under the button that opened it. */
.mb-search {
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid var(--bs-border-color);
  background: var(--bs-body-bg);
}

.mb-search .input-group-text {
  background: var(--bs-tertiary-bg);
  color: var(--bs-secondary-color);
}

/* The pane is narrow, so the one clear button next to the field is the only one worth the room:
   drop WebKit's own search-clear cross rather than showing two of them side by side. */
.mb-search input[type="search"]::-webkit-search-cancel-button {
  display: none;
}

/* Lit while a term is actually filtering the list. Every button up here is already the link
   indigo, so recolouring alone wouldn't read as "on" — a brand-tinted chip behind the magnifier
   does, and the box-shadow draws it without nudging the row's layout. */
#mb-search-btn.active,
#mb-search-btn.active:hover {
  color: var(--bs-primary);
  background: rgba(var(--mt-brand-rgb), 0.18);
  border-radius: 0.25rem;
  box-shadow: 0 0 0 0.25rem rgba(var(--mt-brand-rgb), 0.18);
}

/* "Showing only <domain>" chip above the message list. */
.mb-domain-filter {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  border-bottom: 1px solid var(--bs-border-color);
  background: rgba(var(--mt-brand-rgb), 0.1);
}

/* Stack the panes on small screens */
@media (max-width: 767.98px) {
  .mb-grid {
    grid-template-columns: 1fr;
    grid-template-rows: none;   /* stacked panes size to content; page scrolls */
    height: auto;
  }
  /* No horizontal resizing when the panes are stacked. */
  .mb-gutter {
    display: none;
  }
  .mb-pane {
    border-right: 0;
    border-bottom: 1px solid var(--bs-border-color);
  }
  .mb-folders .mb-pane-body,
  .mb-messages .mb-pane-body {
    max-height: 240px;
  }
}
/* Dashboard token gauge (radial-tick style) */
.token-gauge {
  width: 100%;
  max-width: 240px;
  height: auto;
  --tick-empty: #d7dbe2;
  --tick-fill: #7cb342;
}

[data-bs-theme="dark"] .token-gauge {
  --tick-empty: #3d4350;
}

.token-gauge.level-warn  { --tick-fill: #f0ad4e; }
.token-gauge.level-critical { --tick-fill: #e15241; }

.gauge-tick {
  stroke: var(--tick-empty);
  stroke-width: 2.6;
  stroke-linecap: round;
  transition: stroke 0.3s ease;
}

.gauge-tick.filled {
  stroke: var(--tick-fill);
}

.gauge-value {
  fill: var(--tick-fill);
  font-size: 30px;
  font-weight: 700;
}

/* Admin token-usage bar chart (hand-rolled; no charting dependency). */
.usage-bars {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  overflow-x: auto;
  padding-bottom: 0.25rem;
}

.usage-bar-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1 1 0;
  min-width: 24px;
}

.usage-bar-track {
  height: 220px;
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.usage-bar {
  width: 70%;
  min-width: 8px;
  background: var(--bs-primary);
  border-radius: 3px 3px 0 0;
  transition: height 0.2s ease, opacity 0.2s ease;
}

.usage-bar-col:hover .usage-bar {
  opacity: 0.8;
}

.usage-bar-label {
  margin-top: 0.35rem;
  font-size: 0.65rem;
  line-height: 1;
  color: var(--bs-secondary-color);
  white-space: nowrap;
  text-align: center;
  height: 1em;
}

/* Category-action score legend: 0 (left, dangerous) -> 100 (right, safe). */
.score-legend-bar {
  height: 14px;
  border-radius: 7px;
  background: linear-gradient(to right,
    #dc3545 0%, #fd7e14 30%, #ffc107 55%, #28a745 100%);
}

/* Help popovers (e.g. "User history match" on Category actions): wider than Bootstrap's default,
   which wraps prose into an unreadable column. */
.help-popover {
  --bs-popover-max-width: 24rem;
}

.help-popover .popover-body {
  font-size: 0.875rem;
}

/* Scorecard badges that carry their own dismiss X (the user's flag, and their mark-safe /
   blacklist decision). The badge names the thing, so the X replaces a separate "Remove ..."
   button that repeated the name. */
.mb-badge-dismissable {
  display: inline-flex;
  align-items: center;
}

.mb-badge-x {
  border: 0;
  background: none;
  line-height: 1;
  font-size: 0.8em;
  color: inherit;
  opacity: 0.7;
  /* Padding grows the hit target to a comfortable size; the negative margins keep the badge
     itself the size it would be without it. */
  padding: 0.35rem;
  margin: -0.35rem -0.25rem -0.35rem 0.15rem;
}

.mb-badge-x:hover:not(:disabled),
.mb-badge-x:focus-visible {
  opacity: 1;
}

.mb-badge-x:disabled {
  opacity: 0.35;
  cursor: default;
}
