:root {
  --ink: #10233a;
  --ink-soft: #3d536b;
  --paper: #f3efe6;
  --panel: rgba(255, 252, 246, 0.92);
  --line: rgba(16, 35, 58, 0.12);
  --accent: #d9772c;
  --accent-deep: #b35a16;
  --ok: #1f7a4c;
  --bad: #b42318;
  --shadow: 0 18px 50px rgba(16, 35, 58, 0.12);
  --unread: #1a4a7a;
  --sidebar: rgba(255, 252, 246, 0.85);
}

html[data-theme="dark"] {
  --ink: #e8eef5;
  --ink-soft: #a8b8c9;
  --paper: #121820;
  --panel: rgba(28, 36, 48, 0.94);
  --line: rgba(232, 238, 245, 0.12);
  --accent: #e8954a;
  --accent-deep: #d9772c;
  --ok: #3daf72;
  --bad: #e85a52;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --unread: #7eb6e8;
  --sidebar: rgba(22, 28, 38, 0.92);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; height: 100%; }
body {
  font-family: "Sora", sans-serif;
  color: var(--ink);
  background: var(--paper);
  overflow: hidden;
}

.bg {
  position: fixed; inset: 0; z-index: -2;
  background:
    radial-gradient(1200px 600px at 10% -10%, #dce8f5 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #f2d7bf 0%, transparent 50%),
    linear-gradient(165deg, #eef3f8 0%, #f3efe6 45%, #e7edf4 100%);
  transition: opacity 0.35s ease;
}
html[data-theme="dark"] .bg {
  background:
    radial-gradient(1000px 500px at 10% -10%, #1e3a55 0%, transparent 55%),
    radial-gradient(800px 400px at 100% 0%, #3a2818 0%, transparent 50%),
    linear-gradient(165deg, #101820 0%, #151c26 50%, #121820 100%);
}
.orb {
  position: fixed; border-radius: 50%; filter: blur(40px);
  opacity: 0.4; z-index: -1;
  animation: drift 14s ease-in-out infinite alternate;
  pointer-events: none;
}
.orb-a { width: 260px; height: 260px; background: #9db8d4; top: 10%; left: -50px; }
.orb-b { width: 300px; height: 300px; background: #e8b48a; right: -70px; bottom: 10%; animation-delay: -4s; }
html[data-theme="dark"] .orb-a { background: #2a4a6a; }
html[data-theme="dark"] .orb-b { background: #5a3a22; }
@keyframes drift {
  from { transform: translate(0, 0); }
  to { transform: translate(14px, 20px); }
}

.hidden { display: none !important; }

.gate-shell {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  min-height: 100dvh;
  padding: 2rem 1rem;
  overflow-y: auto;
}
.gate-inner {
  width: min(400px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.75rem;
}
.brand-mark {
  margin: 0;
  font-family: "Fraunces", serif;
  font-size: clamp(2.4rem, 8vw, 3.4rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1;
}
.brand-mark.gate-logo {
  text-align: center;
  font-size: clamp(2.8rem, 10vw, 3.8rem);
  animation: rise 0.65s ease both;
}
.brand-mark.sm { font-size: 1.55rem; }
.panel.gate {
  width: 100%;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1.25rem 1.35rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  animation: rise 0.75s ease both;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.field { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 0.9rem; }
.field span { font-size: 0.82rem; color: var(--ink-soft); font-weight: 500; }
.field input, .field textarea, .search-row input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.65rem 0.8rem;
  font: inherit;
  color: var(--ink);
  background: rgba(255,255,255,0.55);
}
html[data-theme="dark"] .field input,
html[data-theme="dark"] .field textarea,
html[data-theme="dark"] .search-row input {
  background: rgba(0,0,0,0.25);
}
.field textarea { resize: vertical; min-height: 140px; }
.hint { margin: 0.5rem 0 0; font-size: 0.85rem; color: var(--ink-soft); min-height: 1.2em; }
.hint.error { color: var(--bad); }
.hint.ok { color: var(--ok); }
.hint-inline { margin: 0; color: var(--ink-soft); font-size: 0.88rem; }

.btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  border-radius: 10px;
  padding: 0.6rem 1rem;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s, border-color 0.15s;
}
.btn:hover { background: rgba(16,35,58,0.05); }
html[data-theme="dark"] .btn:hover { background: rgba(255,255,255,0.06); }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-deep));
  border-color: transparent;
  color: #fff;
  width: 100%;
}
.btn.primary:hover { filter: brightness(1.05); transform: translateY(-1px); }
.btn.danger { color: var(--bad); border-color: rgba(180,35,24,0.35); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 0.4rem 0.7rem; font-size: 0.85rem; }

.choice { display: inline-flex; align-items: center; gap: 0.4rem; margin-right: 0.9rem; font-size: 0.9rem; cursor: pointer; }
.mode { border: none; margin: 0; padding: 0; }

/* App shell */
.app {
  display: grid;
  grid-template-columns: 218px minmax(280px, 380px) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  gap: 0;
  animation: rise 0.5s ease both;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.25rem 0.95rem;
  border-right: 1px solid var(--line);
  background: var(--sidebar);
  backdrop-filter: blur(12px);
}
.side-brand { min-width: 0; }
.side-email {
  margin: 0.4rem 0 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
  word-break: break-all;
}
.compose-cta { width: 100%; padding: 0.6rem 1rem; }
.side-nav { display: flex; flex-direction: column; gap: 0.25rem; flex: 1; }
.nav-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-weight: 500;
  padding: 0.6rem 0.7rem;
  border-radius: 10px;
  cursor: pointer;
}
.nav-btn:hover { background: rgba(217,119,44,0.1); }
.nav-btn.active {
  background: rgba(217,119,44,0.16);
  color: var(--accent-deep);
}
html[data-theme="dark"] .nav-btn.active { color: var(--accent); }
.nav-count {
  font-size: 0.72rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent-deep);
  border-radius: 999px;
  padding: 0.05rem 0.42rem;
}
.nav-count:empty { display: none; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 0.7rem; }
.quota-box { display: flex; flex-direction: column; gap: 0.35rem; }
.quota-meter {
  height: 5px;
  border-radius: 999px;
  background: rgba(16,35,58,0.12);
  overflow: hidden;
}
html[data-theme="dark"] .quota-meter { background: rgba(255,255,255,0.12); }
.quota-meter i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), var(--accent-deep));
  transition: width 0.35s ease, background 0.2s;
}
.quota-meter i.warn { background: linear-gradient(90deg, #e0a03a, var(--bad)); }
.quota-line { margin: 0; font-size: 0.75rem; color: var(--ink-soft); }

.list-pane {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: var(--panel);
  min-width: 0;
}
.list-head {
  padding: 0.8rem 0.9rem;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: var(--panel);
}
.list-tools { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.tools-spacer { flex: 1; }
.list-count { font-size: 0.76rem; color: var(--ink-soft); }
.search-row { display: flex; gap: 0.4rem; }
.search-row input { flex: 1; min-width: 0; padding: 0.45rem 0.65rem; font-size: 0.88rem; }

.mail-list { flex: 1; overflow-y: auto; }
.mail-item {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: start;
  gap: 0.6rem;
  padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.12s, border-color 0.12s;
}
.mail-item:hover { background: rgba(217,119,44,0.06); }
.mail-item.active {
  background: rgba(217,119,44,0.12);
  border-left-color: var(--accent);
}
.mail-item.unread .mail-subject { font-weight: 600; color: var(--unread); }
.mail-item.unread .mail-from::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 0.35rem;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: middle;
}
.mail-item .row-check { margin-top: 0.25rem; }
.mail-item-body { min-width: 0; }
.mail-row-top {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.15rem;
}
.mail-subject {
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mail-from {
  margin: 0;
  flex: 1;
  min-width: 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mail-date {
  margin: 0;
  flex: none;
  font-size: 0.72rem;
  color: var(--ink-soft);
  white-space: nowrap;
}
.empty-mail { padding: 2rem 1rem; text-align: center; color: var(--ink-soft); font-size: 0.9rem; }

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 0.9rem;
  border-top: 1px solid var(--line);
  font-size: 0.82rem;
  color: var(--ink-soft);
}

.read-pane {
  overflow-y: auto;
  padding: 1.35rem clamp(1rem, 3vw, 2.25rem) 2.5rem;
  background: rgba(255,252,246,0.55);
  min-width: 0;
}
html[data-theme="dark"] .read-pane { background: rgba(18,24,32,0.55); }
.read-empty-state {
  height: 100%;
  min-height: 50vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  color: var(--ink-soft);
  font-size: 0.92rem;
}
.read-empty-state p { margin: 0; }
.read-empty-mark {
  font-size: 2.4rem;
  line-height: 1;
  opacity: 0.35;
}
.read-actions {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  gap: 0.45rem;
  margin: -1.35rem -0.5rem 0.85rem;
  padding: 0.7rem 0.5rem;
  flex-wrap: wrap;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.read-inner { max-width: 68rem; }
.read-pane h3 {
  margin: 0 0 0.75rem;
  font-family: "Fraunces", serif;
  font-weight: 500;
  font-size: clamp(1.15rem, 2.4vw, 1.45rem);
  line-height: 1.3;
  word-break: break-word;
}
.mail-meta-grid {
  display: grid;
  gap: 0.3rem;
  margin-bottom: 1rem;
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255,255,255,0.4);
  font-size: 0.84rem;
  color: var(--ink-soft);
  word-break: break-word;
}
html[data-theme="dark"] .mail-meta-grid { background: rgba(0,0,0,0.18); }
.mail-meta-grid strong { color: var(--ink); margin-right: 0.35rem; }

.code-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  margin: 0 0 1rem;
  padding: 0.65rem 0.75rem;
  border: 1px dashed rgba(217,119,44,0.45);
  border-radius: 12px;
  background: rgba(217,119,44,0.08);
}
.code-bar-label { font-size: 0.8rem; font-weight: 600; color: var(--accent-deep); }
.code-chip {
  border: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-deep));
  color: #fff;
  font-family: "Sora", monospace;
  font-weight: 600;
  letter-spacing: 0.08em;
  padding: 0.4rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s;
}
.code-chip:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(179, 90, 22, 0.25); }
.code-chip.copied { background: var(--ok); letter-spacing: 0; }

.mail-body {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.55;
  font-size: 0.95rem;
}
.mail-body.html { white-space: normal; }
.mail-body img, .mail-body table { max-width: 100%; }
.mail-body a { color: var(--accent-deep); }
.attach-list { margin-top: 1.25rem; font-size: 0.85rem; color: var(--ink-soft); }
.attach-list ul { margin: 0.4rem 0 0; padding-left: 1.1rem; }

/* 写信 / 设置：主区域内嵌页面 */
.work-pane {
  grid-column: 2 / -1;
  display: none;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--panel);
}
.app[data-view="compose"] #compose-pane,
.app[data-view="settings"] #settings-pane {
  display: flex;
  animation: rise 0.3s ease both;
}
.app[data-view="compose"] .list-pane,
.app[data-view="compose"] .read-pane,
.app[data-view="settings"] .list-pane,
.app[data-view="settings"] .read-pane {
  display: none;
}
.work-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  padding: 1.1rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
}
.work-kicker {
  margin: 0;
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
}
.work-head h2 {
  margin: 0.2rem 0 0.25rem;
  font-family: "Fraunces", serif;
  font-weight: 500;
  font-size: 1.3rem;
}
.work-body {
  flex: 1;
  overflow-y: auto;
  padding: 1.2rem clamp(1rem, 3vw, 2rem) 2.5rem;
  max-width: 62rem;
  width: 100%;
}
.field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 0 1rem;
}
.settings-block { margin-bottom: 1.75rem; }
.settings-block h3 {
  margin: 0 0 0.65rem;
  font-size: 0.95rem;
  font-weight: 600;
}
.btn.primary.auto { width: auto; }

.mobile-back {
  display: none;
  margin: 0 0 0.85rem;
}

.req { color: var(--bad); font-style: normal; }
.compose-opts { margin: 0 0 0.85rem; }
.compose-opts legend { font-size: 0.82rem; color: var(--ink-soft); margin-bottom: 0.35rem; }
.compose-actions {
  display: flex;
  gap: 0.6rem;
  align-items: center;
  flex-wrap: wrap;
}
.compose-actions .btn.primary { width: auto; min-width: 8rem; }
.btn:disabled { opacity: 0.55; cursor: wait; }

.toast-host {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  max-width: min(360px, calc(100vw - 2rem));
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  padding: 0.85rem 1rem;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  line-height: 1.4;
  animation: toast-in 0.25s ease both;
}
.toast.ok { border-color: rgba(31, 122, 76, 0.35); color: var(--ok); }
.toast.err { border-color: rgba(180, 35, 24, 0.35); color: var(--bad); }
.toast.info { color: var(--ink); }
.toast.out { animation: toast-out 0.22s ease both; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes toast-out {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(8px); }
}

@media (max-width: 1180px) {
  .app { grid-template-columns: 190px minmax(250px, 320px) minmax(0, 1fr); }
}

@media (max-width: 860px) {
  body { overflow: auto; }
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
    min-height: 100dvh;
  }
  .sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem 0.6rem;
    padding: 0.85rem 0.9rem;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
  .side-brand { margin: 0; flex: 1; min-width: 140px; }
  .compose-cta { width: auto; order: 3; }
  .side-nav { flex-direction: row; flex: none; flex-wrap: wrap; gap: 0.3rem; }
  .side-foot {
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
    margin: 0;
    width: 100%;
    justify-content: space-between;
  }
  .quota-box { flex: 1; max-width: 220px; }
  .work-pane { grid-column: 1 / -1; min-height: 60vh; }
  .list-pane { min-height: 60vh; border-right: none; }
  .read-pane { display: none; min-height: 60vh; }
  .read-actions { position: static; margin: 0 0 0.85rem; padding: 0 0 0.7rem; background: transparent; }
  .app.show-read .list-pane { display: none; }
  .app.show-read .read-pane { display: block; }
  .app.show-read .mobile-back { display: inline-flex; }
}

@media (max-width: 520px) {
  .brand-mark.sm { font-size: 1.3rem; }
  .read-pane { padding: 1rem 0.9rem 2rem; }
  .list-head { padding: 0.7rem 0.75rem; }
  .mail-item { padding: 0.65rem 0.75rem; }
  .work-head { padding: 0.9rem; }
  .work-body { padding: 1rem 0.9rem 2rem; }
}
