:root {
  --bg: #f6f3ef;
  --surface: #ffffff;
  --surface-2: #f1ece6;
  --hover: #ebe4dc;
  --selected: #f8e6d3;
  --border: #e3dbd1;
  --text: #22201d;
  --text-2: #5f5a53;
  --text-3: #8b847b;
  --accent: #b45f1c;
  --accent-text: #ffffff;
  --accent-soft: #f6dfc8;
  --star: #e0a417;
  --danger: #b3261e;
  --unread-bg: #ffffff;
  --read-bg: #faf8f5;
  --shadow: 0 8px 30px rgb(40 25 10 / 0.18), 0 2px 6px rgb(40 25 10 / 0.08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171513;
    --surface: #201d1a;
    --surface-2: #2a2622;
    --hover: #322d28;
    --selected: #3d2c1d;
    --border: #38322c;
    --text: #ece7e1;
    --text-2: #b8b0a6;
    --text-3: #8a8279;
    --accent: #e89a55;
    --accent-text: #1c120a;
    --accent-soft: #4a321d;
    --star: #f2c14e;
    --danger: #f2b8b5;
    --unread-bg: #26221e;
    --read-bg: #1d1a18;
    --shadow: 0 8px 30px rgb(0 0 0 / 0.5), 0 2px 6px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171513;
  --surface: #201d1a;
  --surface-2: #2a2622;
  --hover: #322d28;
  --selected: #3d2c1d;
  --border: #38322c;
  --text: #ece7e1;
  --text-2: #b8b0a6;
  --text-3: #8a8279;
  --accent: #e89a55;
  --accent-text: #1c120a;
  --accent-soft: #4a321d;
  --star: #f2c14e;
  --danger: #f2b8b5;
  --unread-bg: #26221e;
  --read-bg: #1d1a18;
  --shadow: 0 8px 30px rgb(0 0 0 / 0.5), 0 2px 6px rgb(0 0 0 / 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.hidden { display: none !important; }
svg.i { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- auth ---------- */
.auth {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px 24px;
}
.auth-brand { display: flex; align-items: center; gap: 10px; font-size: 20px; font-weight: 650; margin-bottom: 4px; }
.auth-brand img { width: 32px; height: 32px; }
.auth-sub { color: var(--text-2); margin: 0 0 20px; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; margin-bottom: 18px; }
.tabs button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 7px; color: var(--text-2); font-weight: 550; }
.tabs button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 0.08); }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 13px; color: var(--text-2); margin-bottom: 5px; font-weight: 550; }
.field input, .input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  outline: none;
}
.field input:focus, .input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.addr-input { display: flex; align-items: stretch; }
.addr-input input { border-radius: 9px 0 0 9px; }
.addr-input em { font-style: normal; display: flex; align-items: center; padding: 0 10px; border: 1px solid var(--border); border-left: 0; border-radius: 0 9px 9px 0; background: var(--surface-2); color: var(--text-2); white-space: nowrap; font-size: 13px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 9px; padding: 10px 16px; font-weight: 600;
  background: var(--accent); color: var(--accent-text);
}
.btn:disabled { opacity: 0.6; cursor: default; }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--hover); }
.err { color: var(--danger); font-size: 13px; margin: 0 0 12px; min-height: 1em; }
.ts { margin-bottom: 14px; min-height: 0; }
.auth-note { color: var(--text-3); font-size: 12.5px; margin: 16px 0 0; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 240px 1fr; grid-template-rows: 60px 1fr; height: 100vh; height: 100dvh; }
.top { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; padding: 0 16px 0 10px; }
.brand { display: flex; align-items: center; gap: 9px; font-size: 18px; font-weight: 650; min-width: 196px; text-decoration: none; color: var(--text); }
.brand img { width: 30px; height: 30px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: none; color: var(--text-2);
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.on { color: var(--star); }
.menu-btn { display: none; }
.search {
  flex: 1; max-width: 720px; display: flex; align-items: center; gap: 8px;
  background: var(--surface-2); border-radius: 24px; padding: 0 6px 0 14px; height: 46px; color: var(--text-2);
}
.search:focus-within { background: var(--surface); box-shadow: var(--shadow); }
.search input { flex: 1; border: 0; background: none; outline: none; height: 100%; min-width: 0; color: var(--text); }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  font-weight: 650; font-size: 14px; color: #fff; flex: none; border: 0;
}
.me { margin-left: auto; position: relative; }
button.avatar { cursor: pointer; }
.pop {
  position: absolute; right: 0; top: 44px; z-index: 30; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow); padding: 16px; width: 280px;
}
.pop .who { font-weight: 600; }
.pop .addr { color: var(--text-2); font-size: 13px; margin-bottom: 12px; word-break: break-all; }
.meter { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin: 6px 0 4px; }
.meter i { display: block; height: 100%; background: var(--accent); }
.pop small { color: var(--text-3); }
.pop-actions { display: flex; gap: 8px; margin-top: 14px; }

.side { padding: 6px 12px 16px 8px; overflow-y: auto; }
.compose-btn {
  display: inline-flex; align-items: center; gap: 12px; padding: 0 22px 0 16px; height: 54px; border-radius: 16px;
  border: 0; background: var(--accent-soft); color: var(--text); font-weight: 600; margin: 4px 0 14px; font-size: 14.5px;
}
.compose-btn:hover { box-shadow: 0 1px 3px rgb(0 0 0 / 0.15); }
.nav a {
  display: flex; align-items: center; gap: 14px; height: 34px; padding: 0 12px 0 14px; border-radius: 0 17px 17px 0;
  color: var(--text); text-decoration: none; font-size: 14px;
}
.nav a:hover { background: var(--hover); }
.nav a.on { background: var(--selected); font-weight: 650; }
.nav a .n { margin-left: auto; font-size: 12.5px; font-weight: 650; }
.nav a span.t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side h3 { display: flex; align-items: center; justify-content: space-between; font-size: 14px; font-weight: 600; margin: 18px 0 4px 14px; }
.side h3 .icon-btn { width: 32px; height: 32px; }

.main {
  background: var(--surface); border-radius: 16px 16px 0 0; margin-right: 16px; overflow: hidden; display: flex; flex-direction: column; min-width: 0;
}
.toolbar { display: flex; align-items: center; gap: 2px; padding: 6px 10px; border-bottom: 1px solid var(--border); min-height: 52px; }
.toolbar .grow { flex: 1; }
.toolbar .range { color: var(--text-2); font-size: 12.5px; margin-right: 4px; white-space: nowrap; }
.check { width: 18px; height: 18px; accent-color: var(--accent); margin: 0 11px; flex: none; }
.scroll { flex: 1; overflow-y: auto; }

/* ---------- list ---------- */
.row {
  display: flex; align-items: center; gap: 4px; padding: 0 16px 0 0; height: 42px; border-bottom: 1px solid var(--border);
  background: var(--read-bg); cursor: pointer; position: relative;
}
.row.unread { background: var(--unread-bg); }
.row.unread .from, .row.unread .subj { font-weight: 700; color: var(--text); }
.row:hover { box-shadow: inset 1px 0 0 var(--border), inset -1px 0 0 var(--border), 0 1px 3px rgb(0 0 0 / 0.12); z-index: 1; }
.row.sel { background: var(--selected); }
.row .icon-btn { width: 32px; height: 32px; }
.row .icon-btn svg { width: 18px; height: 18px; }
.row .from { width: 190px; flex: none; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--text-2); padding-left: 4px; }
.row .from .cnt { color: var(--text-3); font-weight: 400; font-size: 12px; margin-left: 4px; }
.row .body { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--text-3); }
.row .subj { color: var(--text-2); }
.row .att { color: var(--text-3); display: inline-flex; }
.row .date { width: 72px; text-align: right; flex: none; font-size: 12.5px; color: var(--text-2); }
.row.unread .date { font-weight: 700; color: var(--text); }
.chip {
  display: inline-block; font-size: 11.5px; padding: 1px 7px; border-radius: 5px; background: var(--surface-2); color: var(--text-2);
  margin-right: 5px; font-weight: 550; vertical-align: 1px;
}
.empty { padding: 60px 24px; text-align: center; color: var(--text-3); }
.empty b { display: block; color: var(--text-2); font-size: 16px; margin-bottom: 6px; }

/* ---------- thread ---------- */
.thread { padding: 8px 24px 40px 24px; max-width: 980px; }
.thread h1 { font-size: 22px; font-weight: 500; margin: 12px 0 18px 48px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; word-break: break-word; }
.msg { display: grid; grid-template-columns: 40px 1fr; gap: 0 12px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.msg:last-of-type { border-bottom: 0; }
.msg .avatar { width: 40px; height: 40px; }
.msg-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; min-width: 0; }
.msg-head b { font-weight: 650; }
.msg-head .ad { color: var(--text-3); font-size: 12.5px; }
.msg-head .when { margin-left: auto; color: var(--text-3); font-size: 12.5px; white-space: nowrap; }
.msg-to { color: var(--text-3); font-size: 12.5px; margin-top: 1px; overflow-wrap: anywhere; }
.msg-body { grid-column: 2; margin-top: 12px; min-width: 0; }
.msg-body pre { white-space: pre-wrap; word-wrap: break-word; font: inherit; margin: 0; }
.msg-body iframe { width: 100%; border: 0; min-height: 60px; display: block; background: #fff; border-radius: 6px; }
.img-bar { background: var(--surface-2); border-radius: 8px; padding: 8px 12px; margin-bottom: 10px; font-size: 13px; color: var(--text-2); }
.img-bar button { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 0 4px; }
.atts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.att-card {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px;
  text-decoration: none; color: var(--text); max-width: 260px; font-size: 13px;
}
.att-card:hover { background: var(--hover); }
.att-card span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-card small { color: var(--text-3); flex: none; }
.reply-row { display: flex; gap: 10px; margin: 20px 0 0 52px; }
.pill {
  display: inline-flex; align-items: center; gap: 8px; padding: 8px 18px; border-radius: 18px; border: 1px solid var(--border);
  background: none; color: var(--text-2); font-weight: 550;
}
.pill:hover { background: var(--hover); }

/* ---------- compose ---------- */
.compose {
  position: fixed; right: 24px; bottom: 0; width: 540px; max-width: calc(100vw - 32px); height: 560px; max-height: calc(100dvh - 40px);
  background: var(--surface); border-radius: 12px 12px 0 0; box-shadow: var(--shadow); display: flex; flex-direction: column; z-index: 40;
}
.compose.min { height: auto; }
.compose.min .compose-body { display: none; }
.compose-head {
  display: flex; align-items: center; padding: 0 6px 0 16px; height: 42px; background: var(--surface-2); border-radius: 12px 12px 0 0;
  font-weight: 600; font-size: 14px; cursor: pointer;
}
.compose-head .icon-btn { width: 32px; height: 32px; }
.compose-head span { flex: 1; }
.compose-body { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.cline { display: flex; align-items: center; border-bottom: 1px solid var(--border); margin: 0 16px; }
.cline label { color: var(--text-3); width: 44px; font-size: 13.5px; }
.cline input { flex: 1; border: 0; outline: none; background: none; padding: 9px 0; min-width: 0; }
.cline button.link { border: 0; background: none; color: var(--text-2); font-size: 13px; }
.compose textarea { flex: 1; border: 0; outline: none; resize: none; padding: 12px 16px; background: none; line-height: 1.5; }
.compose .files { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px; }
.compose .files .chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 4px 3px 9px; }
.compose .files .chip button { border: 0; background: none; padding: 0 3px; color: var(--text-3); }
.compose-foot { display: flex; align-items: center; gap: 6px; padding: 10px 12px 12px 16px; }
.compose-foot .grow { flex: 1; }
.compose .note { font-size: 12px; color: var(--text-3); padding: 0 16px 6px; }

/* ---------- misc ---------- */
.toast {
  position: fixed; left: 24px; bottom: 24px; background: #2b2724; color: #f3eee8; padding: 12px 18px; border-radius: 8px;
  box-shadow: var(--shadow); z-index: 60; max-width: calc(100vw - 48px); font-size: 14px;
}
.menu {
  position: absolute; z-index: 50; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 6px 0; min-width: 200px; max-height: 320px; overflow-y: auto;
}
.menu .mh { padding: 6px 14px; font-size: 12.5px; color: var(--text-3); }
.menu button { display: flex; align-items: center; gap: 10px; width: 100%; border: 0; background: none; padding: 8px 14px; text-align: left; }
.menu button:hover { background: var(--hover); }
.scrim { position: fixed; inset: 0; z-index: 20; }
.loading { padding: 40px; text-align: center; color: var(--text-3); }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 60px 1fr; }
  .menu-btn { display: inline-flex; }
  .brand { min-width: 0; }
  .brand span { display: none; }
  .side {
    position: fixed; left: 0; top: 0; bottom: 0; width: 270px; background: var(--bg); z-index: 35; padding-top: 16px;
    transform: translateX(-100%); transition: transform 0.2s; box-shadow: var(--shadow);
  }
  .side.open { transform: none; }
  .main { margin: 0; border-radius: 0; }
  .row { height: auto; padding: 8px 12px 8px 0; flex-wrap: wrap; row-gap: 0; }
  .row .from { flex: 1; width: auto; }
  .row .body { flex-basis: 100%; padding-left: 70px; order: 2; }
  .row .att { display: none; }
  .thread { padding: 4px 16px 32px; }
  .thread h1 { margin-left: 0; font-size: 19px; }
  .msg { grid-template-columns: 34px 1fr; }
  .msg .avatar { width: 34px; height: 34px; }
  .reply-row { margin-left: 0; }
  .compose { right: 0; width: 100%; max-width: 100%; height: 100dvh; max-height: 100dvh; border-radius: 0; }
  .compose-head { border-radius: 0; }
  .toolbar .range { display: none; }
}
@media (max-width: 480px) {
  .search { height: 42px; }
  .row .date { width: 56px; }
}
