:root {
  --bg: #f6f7f9; --panel: #ffffff; --side: #eef1f5; --line: #e2e6ec; --text: #1c2129; --muted: #69727f;
  --accent: #2f6fed; --accent-soft: #e4ecfd; --hover: #f1f4f8; --sel: #e4ecfd; --danger: #d23b3b; --star: #e8a400;
  --shadow: 0 10px 40px rgba(20, 30, 50, .18);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181d; --panel: #1d2026; --side: #191c21; --line: #2c3038; --text: #e6e9ee; --muted: #9098a5;
    --accent: #6c9bff; --accent-soft: #24304a; --hover: #252932; --sel: #24304a; --danger: #ff6b6b; --star: #ffc53d;
    --shadow: 0 10px 40px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16181d; --panel: #1d2026; --side: #191c21; --line: #2c3038; --text: #e6e9ee; --muted: #9098a5;
  --accent: #6c9bff; --accent-soft: #24304a; --hover: #252932; --sel: #24304a; --danger: #ff6b6b; --star: #ffc53d;
  --shadow: 0 10px 40px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif; -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 14px; min-height: 1.2em; margin: 0; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 16px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.primary:disabled { opacity: .6; cursor: wait; }
.btn.ghost { background: transparent; }
.icon-btn { border: 0; background: transparent; width: 36px; height: 36px; border-radius: 10px; cursor: pointer; font-size: 18px; color: var(--muted); flex: none; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; }
.link.danger { color: var(--danger); }

/* 登入 */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 32px 24px; display: flex; flex-direction: column; align-items: center; gap: 12px; box-shadow: var(--shadow); }
.login-card h1 { margin: 4px 0 0; font-size: 22px; }
.login-card p.muted { margin: -8px 0 8px; }
.login-card input { width: 100%; padding: 11px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); outline: none; }
.login-card input:focus { border-color: var(--accent); }
.login-card .btn { width: 100%; justify-content: center; padding: 11px; }

/* 版面 */
.app { display: grid; grid-template-columns: 232px minmax(300px, 380px) 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 10px; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 4px 8px 14px; }
.compose-btn { justify-content: center; margin: 0 4px 14px; padding: 10px; }
.folders { display: flex; flex-direction: column; gap: 2px; }
.folders a { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 9px; color: var(--text); text-decoration: none; cursor: pointer; font-size: 14.5px; }
.folders a:hover { background: var(--hover); }
.folders a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folders a .ico { width: 18px; text-align: center; }
.folders a .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folders a .badge { font-size: 12px; font-weight: 700; color: var(--accent); }
.side-label { font-size: 12px; color: var(--muted); padding: 16px 10px 6px; letter-spacing: .04em; }
.side-foot { margin-top: auto; padding: 14px 10px 4px; }

.list-pane { display: flex; flex-direction: column; border-right: 1px solid var(--line); background: var(--panel); min-width: 0; min-height: 0; }
.pane-head { display: flex; align-items: center; gap: 6px; padding: 10px 10px 4px 16px; }
.pane-head h2 { flex: 1; margin: 0; font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search { padding: 6px 12px 10px; }
.search input { width: 100%; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); outline: none; }
.search input:focus { border-color: var(--accent); }
.trash-bar { font-size: 13px; color: var(--muted); padding: 6px 16px 8px; display: flex; justify-content: space-between; gap: 8px; }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.list li { padding: 10px 16px; border-top: 1px solid var(--line); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 1px 10px; position: relative; }
.list li:hover { background: var(--hover); }
.list li.sel { background: var(--sel); }
.list li.unread::before { content: ""; position: absolute; left: 5px; top: 17px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.list li .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li.unread .who, .list li.unread .subj { font-weight: 700; }
.list li .date { font-size: 12.5px; color: var(--muted); }
.list li .subj { grid-column: 1 / -1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.list li .snip { grid-column: 1 / -1; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li .tags { color: var(--star); font-size: 13px; }
.list .none { text-align: center; color: var(--muted); padding: 40px 16px; cursor: default; border: 0; display: block; }
.list .none:hover { background: none; }
.more { margin: 10px auto 14px; }

/* 閱讀 */
.reader-pane { background: var(--panel); min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.reader-pane .empty { margin: auto; color: var(--muted); }
.r-tools { display: flex; align-items: center; gap: 2px; padding: 8px 10px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.r-tools .spacer { flex: 1; }
.r-tools .icon-btn { font-size: 16px; width: auto; padding: 0 10px; height: 34px; }
.r-scroll { overflow-y: auto; flex: 1; padding: 18px 24px 30px; }
.r-subject { font-size: 21px; margin: 0 0 14px; line-height: 1.35; word-break: break-word; }
.r-meta { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 16px; }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; flex: none; }
.r-meta .who { flex: 1; min-width: 0; font-size: 14px; }
.r-meta .who b { font-size: 15px; }
.r-meta .who .addr, .r-meta .who .to { color: var(--muted); word-break: break-all; }
.r-meta .when { color: var(--muted); font-size: 13px; white-space: nowrap; }
.notice { font-size: 13px; background: var(--hover); border-radius: 8px; padding: 7px 12px; margin-bottom: 12px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.body-frame { width: 100%; border: 0; display: block; min-height: 120px; background: #fff; border-radius: 6px; }
.body-text { white-space: pre-wrap; word-break: break-word; font-size: 15px; margin: 0; font-family: inherit; }
.atts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.att { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 7px 12px; text-decoration: none; color: var(--text); font-size: 13.5px; max-width: 100%; }
.att:hover { background: var(--hover); }
.att .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.att .sz { color: var(--muted); font-size: 12px; }

/* 撰寫 */
.compose { width: min(680px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 0; border: 1px solid var(--line); border-radius: 16px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.compose::backdrop { background: rgba(10, 15, 25, .35); }
.compose form { display: flex; flex-direction: column; height: min(640px, calc(100dvh - 34px)); }
.compose-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--line); }
.compose .row { display: flex; align-items: center; gap: 10px; padding: 0 18px; border-bottom: 1px solid var(--line); }
.compose .row label { color: var(--muted); font-size: 14px; width: 46px; flex: none; }
.compose .row input { flex: 1; border: 0; background: none; padding: 10px 0; outline: none; min-width: 0; }
.compose .from-row { flex-wrap: wrap; }
.compose .from-name { max-width: 140px; }
.compose .addr { display: flex; align-items: center; flex: 1; min-width: 180px; color: var(--muted); }
.compose .addr input { text-align: right; color: var(--text); }
.compose textarea { flex: 1; border: 0; padding: 14px 18px; resize: none; outline: none; background: none; min-height: 160px; line-height: 1.6; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 18px; }
.chips:not(:empty) { padding-bottom: 8px; }
.chip { font-size: 13px; background: var(--hover); border-radius: 999px; padding: 4px 6px 4px 12px; display: flex; align-items: center; gap: 4px; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); }
.compose-foot { display: flex; align-items: center; gap: 10px; padding: 12px 18px; border-top: 1px solid var(--line); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%); }
.only-mobile { display: none; }
.scrim { display: none; }

@media (max-width: 1000px) {
  .app { grid-template-columns: 210px 1fr; }
  .reader-pane { position: fixed; inset: 0; z-index: 20; display: none; }
  .app[data-view="read"] .reader-pane { display: flex; }
}
@media (max-width: 700px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); padding-top: max(14px, env(safe-area-inset-top)); }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 25; }
  .pane-head { padding-top: max(10px, env(safe-area-inset-top)); }
  .r-tools { padding-top: max(8px, env(safe-area-inset-top)); }
  .r-scroll { padding: 16px 16px 30px; }
  .compose { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; border-radius: 0; margin: 0; }
  .compose form { height: 100dvh; }
}
@media (min-width: 1001px) { .only-narrow { display: none; } .r-tools [data-act="back"] { display: none; } }
@media (max-width: 700px) { .r-tools .lbl { display: none; } }
