:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel: #fff;
  --panel-soft: #f8fafc;
  --line: #dbe1e8;
  --text: #172033;
  --muted: #697386;
  --primary: #1769e0;
  --primary-soft: #eaf2ff;
  --danger: #c93636;
  --shadow: 0 8px 28px rgba(21, 33, 51, .08);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111418;
  --panel: #191e24;
  --panel-soft: #20262d;
  --line: #343b45;
  --text: #edf2f7;
  --muted: #9aa6b5;
  --primary: #65a3ff;
  --primary-soft: #202e43;
  --danger: #ff7373;
  --shadow: none;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

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

button {
  cursor: pointer;
}

input,
select {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
}

/* Application shell */
.topbar {
  display: grid;
  grid-template-columns: 280px 1fr auto;
  align-items: center;
  height: 58px;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 17px;
}

.brand-mark,
.detail-empty span {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  background: var(--primary);
  color: white;
  font-weight: 800;
}

.current-account {
  overflow: hidden;
  color: var(--muted);
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  gap: 6px;
}

.icon-button {
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
}

.text-icon {
  width: 42px;
}

.mobile-tabs {
  display: none;
}

.workspace {
  display: grid;
  grid-template-columns: 190px 330px minmax(0, 1fr);
  height: calc(100vh - 58px);
  overflow: hidden;
}

.groups-panel,
.accounts-panel,
.mail-panel {
  min-width: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
}

.panel-title,
.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  font-weight: 700;
}

.panel-heading > span {
  display: flex;
  align-items: center;
  gap: 7px;
}

.panel-heading span:last-child {
  min-width: 24px;
  padding: 2px 7px;
  border-radius: 10px;
  background: var(--panel-soft);
  color: var(--muted);
  text-align: center;
}

.panel-heading em {
  min-width: 24px;
  padding: 2px 7px;
  border-radius: 10px;
  background: var(--panel-soft);
  color: var(--muted);
  font-style: normal;
  text-align: center;
}

/* Provider groups */
.group-list {
  padding: 10px;
}

.group-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 52px;
  padding: 8px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
}

.group-button:hover,
.group-button.active {
  background: var(--primary-soft);
}

.group-button span {
  display: flex;
  align-items: center;
  gap: 8px;
}

.group-button b {
  font-weight: 600;
}

.group-button small {
  color: var(--muted);
}

.group-button em {
  font-style: normal;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #8491a3;
}

.dot.blue {
  background: #2879ed;
}

.dot.black {
  background: #20252b;
}

.dot.cyan {
  background: #52b9d0;
}

/* Mailbox management */
.accounts-panel {
  display: flex;
  flex-direction: column;
}

.mini-add-button {
  height: 30px;
  padding: 0 9px;
  border: 1px solid var(--primary);
  border-radius: 5px;
  background: var(--primary);
  color: #fff;
  font-size: 12px;
  font-weight: 650;
}

.batch-import {
  border-bottom: 1px solid var(--line);
}

.batch-import summary {
  padding: 12px 14px;
  font-weight: 650;
  cursor: pointer;
  list-style-position: inside;
}

.batch-import[open] summary {
  border-bottom: 1px solid var(--line);
  background: var(--panel-soft);
}

.import-section {
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.import-section label {
  display: block;
  margin-bottom: 7px;
  font-weight: 650;
}

.import-section textarea {
  width: 100%;
  min-height: 112px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
  line-height: 1.5;
  resize: vertical;
}

.format-help {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

.batch-status {
  min-height: 18px;
  margin-top: 7px;
  color: var(--muted);
  font-size: 12px;
}

.button-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 9px;
}

.primary-button,
.secondary-button {
  height: 36px;
  padding: 0 14px;
  border-radius: 6px;
  font-weight: 650;
}

.primary-button {
  border: 1px solid var(--primary);
  background: var(--primary);
  color: white;
}

.primary-button:disabled {
  opacity: .45;
  cursor: not-allowed;
}

.secondary-button {
  border: 1px solid var(--line);
  background: var(--panel);
}

.privacy-disclosure {
  position: fixed;
  right: 12px;
  bottom: 8px;
  z-index: 20;
  max-width: min(560px, calc(100vw - 24px));
  padding: 5px 8px;
  border: 1px solid var(--border-color, #d8dde7);
  border-radius: 6px;
  background: color-mix(in srgb, var(--panel, #fff) 94%, transparent);
  color: var(--muted, #5d6678);
  font-size: 12px;
  line-height: 1.4;
  box-shadow: 0 2px 8px rgb(23 26 35 / 8%);
}

@media (max-width: 720px) {
  .privacy-disclosure {
    position: static;
    max-width: none;
    margin: 8px 12px;
  }
}

.account-tools {
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.account-tools input {
  width: 100%;
  margin-bottom: 9px;
}

.segmented {
  display: flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
}

.segmented button {
  flex: 1;
  height: 29px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
}

.segmented button.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .09);
}

.account-list {
  min-height: 0;
  padding: 8px;
  overflow: auto;
}

.account-item {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) 28px 28px 28px;
  align-items: center;
  gap: 5px;
  width: 100%;
  height: 60px;
  padding: 7px 9px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.account-item:hover,
.account-item.active {
  border-color: #c9dcfa;
  background: var(--primary-soft);
}

.provider-icon {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background: #243447;
  color: white;
  font-weight: 700;
}

.account-item strong,
.account-item small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-item small {
  margin-top: 3px;
  color: var(--muted);
}

.copy-account {
  display: grid;
  place-items: center;
  height: 28px;
  color: var(--muted);
}

.account-action {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
}

.account-action:hover {
  background: var(--panel);
  color: var(--primary);
}

.account-action.delete:hover {
  color: var(--danger);
}

/* Mail list and detail */
.mail-panel {
  display: flex;
  flex-direction: column;
  border-right: 0;
}

.mail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 66px;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
}

.mail-header h1 {
  display: inline;
  margin: 0 10px 0 0;
  font-size: 20px;
}

.mail-header span {
  color: var(--muted);
}

.fetch-button {
  min-width: 104px;
}

.mail-tools {
  display: grid;
  grid-template-columns: auto minmax(280px, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
}

.wide {
  width: 280px;
}

.filter-row {
  display: grid;
  grid-template-columns: 120px 92px minmax(130px, 1fr);
  gap: 8px;
}

.compact {
  width: 150px;
}

.mail-content {
  display: grid;
  grid-template-columns: minmax(280px, 34%) minmax(0, 1fr);
  flex: 1;
  min-height: 0;
}

.mail-list {
  min-height: 0;
  overflow: auto;
  border-right: 1px solid var(--line);
  background: var(--panel-soft);
}

.mail-item {
  width: 100%;
  min-height: 128px;
  padding: 14px 15px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  text-align: left;
}

.mail-item:hover,
.mail-item.active {
  background: var(--primary-soft);
}

.mail-item.active {
  box-shadow: inset 3px 0 var(--primary);
}

.mail-line {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.mail-line strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mail-line time {
  flex: none;
  color: var(--muted);
  font-size: 12px;
}

.mail-item > b {
  display: block;
  margin-top: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mail-item p {
  height: 36px;
  margin: 6px 0;
  overflow: hidden;
  color: var(--muted);
  line-height: 18px;
}

.tags {
  display: flex;
  gap: 5px;
}

.tags i {
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--panel-soft);
  color: var(--muted);
  font-size: 11px;
  font-style: normal;
}

.tags .code-tag {
  background: #dcfce7;
  color: #166534;
}

.mail-detail {
  min-width: 0;
  overflow: auto;
  background: var(--panel);
}

.detail-empty,
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 100%;
  min-height: 180px;
  color: var(--muted);
}

.detail-empty span {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  background: var(--panel-soft);
  color: var(--muted);
}

.detail-empty h2 {
  font-size: 16px;
}

.detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  padding: 22px 24px;
  border-bottom: 1px solid var(--line);
}

.detail-header h2 {
  margin: 0 0 16px;
  font-size: 20px;
}

.detail-header dl {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  gap: 5px 10px;
  margin: 0;
}

.detail-header dt {
  color: var(--muted);
}

.detail-header dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.code-button {
  min-width: 150px;
  padding: 10px 14px;
  border: 1px solid #87d5a3;
  border-radius: 6px;
  background: #edfff3;
  color: #166534;
  text-align: left;
}

.code-button small,
.code-button strong,
.code-button span {
  display: block;
}

.code-button strong {
  margin: 3px 0;
  font-size: 24px;
}

.code-button span {
  font-size: 11px;
}

.message-body {
  padding: 20px 24px;
}

.message-body pre {
  margin: 0;
  overflow-wrap: anywhere;
  font: 14px/1.65 ui-monospace, Consolas, monospace;
  white-space: pre-wrap;
}

.message-frame {
  width: 100%;
  height: 680px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
}

/* Mailbox dialog */
.account-dialog {
  width: min(520px, calc(100vw - 28px));
  max-height: min(720px, calc(100vh - 28px));
  padding: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 18px 60px rgba(0, 0, 0, .24);
}

.account-dialog::backdrop {
  background: rgba(15, 23, 42, .55);
}

.account-dialog form {
  padding: 18px;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.dialog-header h2 {
  margin: 0;
  font-size: 20px;
}

.account-dialog label {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
  font-weight: 600;
}

.account-dialog input,
.account-dialog select,
.account-dialog textarea {
  width: 100%;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
}

.account-dialog textarea {
  resize: vertical;
}

.dialog-note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.connection-status {
  min-height: 34px;
  padding: 8px 10px;
  border-radius: 5px;
  background: var(--panel-soft);
  color: var(--muted);
}

.connection-status.success {
  background: #dcfce7;
  color: #166534;
}

.connection-status.error {
  background: #fee2e2;
  color: #991b1b;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.dialog-actions button {
  min-width: 110px;
}

/* Feedback */
.toast-area {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 10;
  display: grid;
  gap: 8px;
}

.toast {
  max-width: 420px;
  padding: 11px 14px;
  border-radius: 6px;
  background: #1f2937;
  color: white;
  box-shadow: var(--shadow);
}

.toast.success {
  background: #18794e;
}

.toast.error {
  background: var(--danger);
}

.loading span {
  width: 24px;
  height: 24px;
  border: 3px solid var(--line);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 1100px) {
  .workspace {
    grid-template-columns: 160px 290px minmax(0, 1fr);
  }

  .mail-tools {
    grid-template-columns: 1fr;
  }

  .wide,
  .compact {
    width: 100%;
  }

  .mail-content {
    grid-template-columns: 320px minmax(0, 1fr);
  }
}

@media (max-width: 760px) {
  .topbar {
    grid-template-columns: 1fr auto;
    height: 90px;
  }

  .current-account {
    grid-row: 2;
    grid-column: 1 / -1;
    height: 32px;
    padding-top: 7px;
  }

  .mobile-tabs {
    position: sticky;
    top: 0;
    z-index: 5;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    height: 44px;
    padding: 4px;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
  }

  .mobile-tabs button {
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: var(--muted);
  }

  .mobile-tabs button.active {
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 650;
  }

  .workspace {
    display: block;
    height: auto;
    min-height: calc(100vh - 134px);
    overflow: visible;
  }

  .groups-panel {
    border-bottom: 1px solid var(--line);
  }

  .panel-title {
    display: none;
  }

  .group-list {
    display: flex;
    overflow-x: auto;
  }

  .group-button {
    min-width: 140px;
  }

  .accounts-panel {
    height: auto;
  }

  .account-list {
    max-height: 240px;
  }

  .mail-panel {
    min-height: 700px;
  }

  .mail-header {
    position: sticky;
    top: 0;
    z-index: 3;
    background: var(--panel);
  }

  .mail-content {
    display: block;
  }

  .mail-list {
    max-height: 420px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .mail-detail {
    min-height: 500px;
  }

  .filter-row {
    grid-template-columns: 1fr 1fr;
  }

  .filter-row input {
    grid-column: 1 / -1;
  }

  .detail-header {
    display: block;
  }

  .code-button {
    margin-top: 16px;
  }

  body[data-mobile-view="accounts"] .mail-panel {
    display: none;
  }

  body[data-mobile-view="mails"] .groups-panel,
  body[data-mobile-view="mails"] .accounts-panel,
  body[data-mobile-view="detail"] .groups-panel,
  body[data-mobile-view="detail"] .accounts-panel {
    display: none;
  }

  body[data-mobile-view="mails"] .mail-panel,
  body[data-mobile-view="detail"] .mail-panel {
    display: flex;
  }

  body[data-mobile-view="mails"] .mail-detail {
    display: none;
  }

  body[data-mobile-view="detail"] .mail-list {
    display: none;
  }

  body[data-mobile-view="detail"] .mail-detail {
    display: block;
  }

  .account-dialog {
    max-height: calc(100vh - 16px);
  }

  .dialog-actions button {
    flex: 1;
    min-width: 90px;
  }
}
