:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #12161c;
  --text-dim: #61697a;
  --text-faint: #8b93a4;
  --accent: #1b62d6;
  --accent-soft: #e8f0fd;
  --danger: #c8322c;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1e242c;
    --border: #2a313b;
    --text: #e8ebf0;
    --text-dim: #9aa4b4;
    --text-faint: #6e788a;
    --accent: #5b9bff;
    --accent-soft: #17243a;
    --danger: #ff6b63;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.spacer { flex: 1; }

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

/* ------------------------------------------------------------- buttons */

.btn-primary, .btn-secondary {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-primary:disabled { opacity: .55; cursor: default; }
.btn-secondary { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.btn-secondary:hover { background: var(--border); }

.icon-btn {
  background: transparent;
  border: 0;
  border-radius: 7px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1;
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.icon-btn.on { color: #e0a300; }

.link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  cursor: pointer;
  font-size: 13px;
  text-decoration: none;
}
.link:hover { text-decoration: underline; }

.form-error { color: var(--danger); font-size: 13px; margin: 0; }

/* --------------------------------------------------------------- login */

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1100px 520px at 50% -10%, var(--accent-soft), transparent 70%),
    var(--bg);
}

.login-card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 30px 28px 26px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.brand { display: flex; align-items: center; gap: 13px; margin-bottom: 20px; }
.brand h1 { font-size: 19px; margin: 0; letter-spacing: -.2px; }
.brand-sub { margin: 2px 0 0; color: var(--text-faint); font-size: 12.5px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 40px; height: 40px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 19px;
}
.brand-mark.small { width: 26px; height: 26px; border-radius: 7px; font-size: 13px; }

.login-card label, .modal-body label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-dim);
  margin-top: 8px;
}

.login-card input, .modal-body input, .new-user-grid input, .field input, #c-body {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
  outline: none;
}
.login-card input:focus, .modal-body input:focus, .new-user-grid input:focus,
.field input:focus, #c-body:focus, #search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
.login-card .btn-primary { margin-top: 18px; }

/* ----------------------------------------------------------- app shell */

.app {
  height: 100%;
  display: grid;
  grid-template-columns: 232px 360px 1fr;
}

.sidebar {
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 14px;
  min-width: 0;
}
.sidebar-head { display: flex; align-items: center; gap: 9px; padding: 2px 4px; font-size: 14.5px; }
.compose-btn { width: 100%; }

.folders { display: flex; flex-direction: column; gap: 2px; }
.folder {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 8px 11px;
  cursor: pointer;
  color: var(--text-dim);
  font-weight: 500;
  text-align: left;
}
.folder:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.count { font-size: 11.5px; font-weight: 700; color: var(--text-faint); }
.folder.active .count { color: var(--accent); }
.count:empty { display: none; }

.sidebar-foot { margin-top: auto; border-top: 1px solid var(--border); padding-top: 12px; }
.me { display: flex; align-items: center; gap: 9px; margin-bottom: 10px; min-width: 0; }
.avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  font-size: 12.5px; font-weight: 700;
  flex: 0 0 auto;
}
.me-text { min-width: 0; }
.me-name { font-weight: 600; font-size: 13px; }
.me-name:empty { display: none; }
.me-mail, .me-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me-mail { font-size: 12px; color: var(--text-faint); }
.foot-actions { display: flex; flex-wrap: wrap; gap: 12px; }

/* ------------------------------------------------------------ list pane */

.list-pane {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.list-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
}
#search {
  flex: 1;
  min-width: 0;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 12px;
  outline: none;
}

.list { flex: 1; overflow-y: auto; }

.row {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--border);
  padding: 11px 14px;
  cursor: pointer;
  position: relative;
}
.row:hover { background: var(--surface-2); }
.row.active { background: var(--accent-soft); }
.row.unread::before {
  content: "";
  position: absolute;
  left: 5px; top: 50%;
  width: 5px; height: 5px;
  margin-top: -2.5px;
  border-radius: 50%;
  background: var(--accent);
}
.row-top { display: flex; align-items: baseline; gap: 8px; }
.row-from { font-weight: 500; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row.unread .row-from, .row.unread .row-subject { font-weight: 700; }
.row-date { font-size: 11.5px; color: var(--text-faint); flex: 0 0 auto; }
.row-subject { margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-snippet {
  margin-top: 2px;
  font-size: 12.5px;
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-badges { float: right; margin-left: 6px; color: var(--text-faint); font-size: 12px; }

.list-empty { padding: 44px 20px; text-align: center; color: var(--text-faint); }
.load-more { border: 0; border-top: 1px solid var(--border); background: var(--surface); padding: 12px; cursor: pointer; color: var(--accent); }

/* -------------------------------------------------------------- reader */

.reader { background: var(--surface); display: flex; flex-direction: column; min-width: 0; }
.reader-empty { margin: auto; text-align: center; color: var(--text-faint); }
.empty-mark { font-size: 40px; opacity: .35; display: block; margin-bottom: 8px; }

.reader-body { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.reader-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px 12px;
  border-bottom: 1px solid var(--border);
}
.reader-head h2 { font-size: 17px; margin: 0; flex: 1; min-width: 0; line-height: 1.35; }
.reader-actions { display: flex; gap: 2px; flex: 0 0 auto; }

.meta { padding: 12px 18px; display: flex; flex-direction: column; gap: 3px; border-bottom: 1px solid var(--border); }
.meta-row { display: flex; gap: 10px; font-size: 13px; }
.meta-k { color: var(--text-faint); width: 48px; flex: 0 0 auto; }
.meta-v { color: var(--text-dim); word-break: break-word; min-width: 0; }

.notice {
  margin: 10px 18px 0;
  padding: 8px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--text-dim);
  display: flex;
  gap: 8px;
  align-items: center;
}

.attachments { padding: 12px 18px 0; display: flex; flex-wrap: wrap; gap: 8px; }
.attachment {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 11px;
  font-size: 12.5px;
  text-decoration: none;
  color: var(--text);
  background: var(--surface-2);
  max-width: 260px;
}
.attachment:hover { border-color: var(--accent); }
.attachment span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment em { color: var(--text-faint); font-style: normal; flex: 0 0 auto; }

.body-frame { flex: 1; width: 100%; border: 0; background: #fff; min-height: 260px; }
.reader-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; align-items: center; gap: 16px; }

/* -------------------------------------------------------------- modals */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 20, .5);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 40;
}

.modal {
  width: min(680px, 100%);
  max-height: min(86vh, 760px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal.small { width: min(400px, 100%); }
.modal-head { display: flex; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.modal-head strong { flex: 1; }
.modal-body { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 5px; }
.modal-foot { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--border); }

.compose-fields { padding: 4px 16px 0; }
.field { display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border); }
.field label { width: 52px; flex: 0 0 auto; font-size: 12.5px; color: var(--text-faint); }
.field input { border: 0 !important; box-shadow: none !important; padding: 11px 0; background: transparent; }
#c-body {
  flex: 1;
  min-height: 220px;
  margin: 12px 16px;
  width: auto;
  resize: none;
  border-radius: 8px;
  font: 13.5px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.compose-attach { padding: 0 16px; display: flex; flex-wrap: wrap; gap: 6px; }
.compose-attach:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 20px; padding: 4px 6px 4px 11px; font-size: 12px;
}
.chip button { background: none; border: 0; cursor: pointer; color: var(--text-faint); padding: 0 3px; }
.file-btn { cursor: pointer; }

.user-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.user-table td { padding: 9px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.user-table .u-mail { font-weight: 600; }
.user-table .u-sub { color: var(--text-faint); font-size: 12px; }
.user-table .u-actions { text-align: right; white-space: nowrap; }
.user-table .u-actions .link { margin-left: 10px; }
.tag { font-size: 11px; background: var(--accent-soft); color: var(--accent); border-radius: 5px; padding: 1px 6px; margin-left: 6px; }
.tag.off { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

.new-user { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 16px; display: flex; flex-direction: column; gap: 9px; align-items: flex-start; }
.new-user-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; width: 100%; }
.check { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--text-dim); }
.check input { width: auto; }

/* --------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 60;
  font-size: 13px;
  max-width: 88vw;
}

/* -------------------------------------------------------------- mobile */

.only-mobile { display: none; }

@media (max-width: 1080px) {
  .app { grid-template-columns: 200px 320px 1fr; }
}

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; position: relative; }
  .only-mobile { display: grid; }

  .sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    width: 250px;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }

  .list-pane, .reader { grid-row: 1; grid-column: 1; }
  .reader { display: none; }
  .app.reading .list-pane { display: none; }
  .app.reading .reader { display: flex; }

  .new-user-grid { grid-template-columns: 1fr; }
  .modal { max-height: 92vh; }
}
