:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-soft: #fafafa;
  --border: #e7e7e8;
  --border-strong: #d7d9dd;
  --text: #18181b;
  --muted: #73737a;
  --faint: #a1a1aa;
  --accent: #006ffe;
  --accent-hover: #005fd9;
  --accent-soft: #eef5ff;
  --danger: #f43f5e;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 12px 28px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.status { min-height: 1.2em; color: var(--muted); font-size: 0.85rem; }

.btn-primary, .btn-secondary, .icon-btn {
  min-height: 36px;
  border-radius: 8px;
  padding: 0 13px;
  font-weight: 650;
  font-size: 0.88rem;
}

.btn-primary {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary, .icon-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
}
.btn-secondary:hover, .icon-btn:hover { background: var(--panel-soft); }

.btn-link {
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 0.86rem;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 10px 12px;
  font-size: 0.92rem;
  outline: none;
}
input:focus, select:focus, textarea:focus {
  border-color: rgba(0, 111, 254, 0.45);
  box-shadow: 0 0 0 3px rgba(0, 111, 254, 0.1);
}
textarea { resize: vertical; line-height: 1.5; }

.login {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  padding: 20px;
}
.login-card {
  width: 380px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  padding: 30px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin-bottom: 16px; font-size: 1.55rem; }
.login-card input { margin: 14px 0; }
.login-card .btn-primary { width: 100%; }
.login-card .btn-link { margin-top: 12px; }

.app {
  display: grid;
  grid-template-columns: 260px minmax(340px, 430px) minmax(0, 1fr);
  gap: 4px;
  height: 100vh;
  padding: 4px;
}

.sidebar, .threads, .reader {
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel);
}

.sidebar {
  display: flex;
  flex-direction: column;
  border-radius: 16px 8px 8px 16px;
  padding: 14px;
}
.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 14px;
}
.logo { display: block; font-size: 1.15rem; font-weight: 800; letter-spacing: 0; }
.sidebar-user {
  max-width: 160px;
  margin-top: 2px;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.76rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 18px;
}
.sidebar-label {
  margin: 0 4px 8px;
  color: var(--faint);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.mailbox-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
}
.mailbox-item {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  width: 100%;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  padding: 8px;
  text-align: left;
}
.mailbox-item:hover { background: var(--panel-soft); }
.mailbox-item.active {
  border-color: rgba(0, 111, 254, 0.18);
  background: var(--accent-soft);
}
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}
.mailbox-copy, .thread-copy {
  display: flex;
  min-width: 0;
  flex-direction: column;
}
.mailbox-address {
  overflow: hidden;
  font-size: 0.86rem;
  font-weight: 650;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.mailbox-state, .sidebar-empty {
  color: var(--muted);
  font-size: 0.76rem;
}

.threads {
  display: flex;
  flex-direction: column;
  border-radius: 8px;
}
.threads-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: grid;
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
}
.threads-head h1 {
  overflow: hidden;
  font-size: 1.1rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#mailbox-subtitle {
  overflow: hidden;
  font-size: 0.78rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}
.thread-item {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 10px;
  width: 100%;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  padding: 10px;
  color: var(--text);
  text-align: left;
}
.thread-item:hover { background: var(--panel-soft); }
.thread-item.active {
  border-color: rgba(0, 111, 254, 0.14);
  background: var(--accent-soft);
}
.thread-item.outbound { border-left: 3px solid var(--accent); }
.thread-item.inbound { border-right: 3px solid #22733c; }
.thread-item.unread .thread-participant,
.thread-item.unread .thread-subject {
  font-weight: 800;
}
.thread-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}
.thread-participant,
.thread-subject,
.thread-snippet {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thread-participant { font-size: 0.9rem; font-weight: 650; }
.thread-date { flex: 0 0 auto; color: var(--muted); font-size: 0.72rem; }
.thread-subject { margin-top: 2px; font-size: 0.86rem; }
.thread-snippet { margin-top: 2px; color: var(--muted); font-size: 0.8rem; }
.thread-meta { margin-top: 5px; color: var(--faint); font-size: 0.72rem; }
.list-empty {
  padding: 24px 14px;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}

.reader {
  border-radius: 8px 16px 16px 8px;
  overflow-y: auto;
}
.reader-empty {
  display: flex;
  height: 100%;
  min-height: 320px;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 6px;
  text-align: center;
}
.empty-title { font-size: 1.05rem; font-weight: 750; }
.reader-content {
  min-height: 100%;
  padding: 20px;
}
.reader-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin: -20px -20px 18px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(12px);
}
.reader-head h2 {
  max-width: 70ch;
  overflow-wrap: anywhere;
  font-size: 1.15rem;
  line-height: 1.3;
}
#reader-meta { margin-top: 3px; font-size: 0.78rem; }
.reader-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}
.messages {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 980px;
}
.message {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fff;
  overflow: hidden;
}
.message.outbound { border-left: 3px solid var(--accent); }
.message.inbound { border-right: 3px solid #22733c; }
.message-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 13px 15px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-soft);
}
.message-person {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 10px;
}
.message-from {
  display: block;
  overflow: hidden;
  font-size: 0.92rem;
  font-weight: 750;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.message-address {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.76rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.message-date {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.78rem;
}
.message-side {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}
.direction-badge {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  border-radius: 999px;
  padding: 0 8px;
  font-size: 0.72rem;
  font-weight: 750;
}
.direction-badge.inbound {
  background: #eef7f0;
  color: #22733c;
}
.direction-badge.outbound {
  background: var(--accent-soft);
  color: var(--accent);
}
.message-body {
  padding: 18px;
  overflow-wrap: anywhere;
  font-size: 0.92rem;
  line-height: 1.58;
}
.message-body img { max-width: 100%; height: auto; }
.message-body blockquote {
  margin: 12px 0;
  padding-left: 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--muted);
}
.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 18px 18px;
}
.attachment {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--panel-soft);
  color: var(--text);
  padding: 0 9px;
  font-size: 0.8rem;
  text-decoration: none;
}
.attachment:hover { border-color: var(--border-strong); }

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(24, 24, 27, 0.38);
  padding: 20px;
}
.modal-box {
  position: relative;
  width: 560px;
  max-width: 94vw;
  max-height: 90vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  padding: 22px;
  box-shadow: var(--shadow);
}
.compose-box { width: 680px; }
.modal-box h2 { margin-bottom: 14px; font-size: 1.12rem; }
.modal-box label {
  display: block;
  margin: 12px 0 5px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}
.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
}
.modal-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 16px;
}

.settings-box { width: 780px; }
.key-form {
  display: grid;
  grid-template-columns: 170px minmax(0, 1fr) auto;
  gap: 8px;
  margin: 14px 0 8px;
}
.account-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 14px;
}
.account {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}
.account-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}
.account-title { font-weight: 750; }
.account-meta, .address-meta {
  color: var(--muted);
  font-size: 0.78rem;
}
.domain-list, .address-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}
.address-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 9px 0;
  border-top: 1px solid var(--border);
}

[hidden] { display: none !important; }

@media (max-width: 980px) {
  body { overflow: auto; }
  .app {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100vh;
  }
  .sidebar, .threads, .reader {
    border-radius: 12px;
    min-height: auto;
  }
  .mailbox-list { max-height: 220px; }
  .threads { min-height: 360px; }
  .reader { min-height: 460px; }
  .key-form { grid-template-columns: 1fr; }
}
