:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1c2330;
  --muted: #667085;
  --accent: #2458d6;
  --accent-soft: #e6edfc;
  --danger: #c4312b;
  --ok: #1d7a46;
  --warn: #9a6200;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151b;
    --surface: #1b1f27;
    --surface-2: #232834;
    --border: #2f3542;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --accent: #6f9bff;
    --accent-soft: #22304f;
    --danger: #ff6b63;
    --ok: #4cc987;
    --warn: #e0a743;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0 0 12px; font-size: 18px; }
h1 { font-size: 22px; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

input, select, button { font: inherit; color: inherit; }
input, select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
label input, label select { color: var(--text); }

button, .btn { cursor: pointer; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); padding: 7px 14px; }
.btn { color: var(--text); text-decoration: none; display: inline-block; }
button:hover, .btn:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: var(--surface-2); }
button.small, .btn.small { padding: 3px 8px; font-size: 13px; }
button.danger { color: var(--danger); }
.error { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.card-head select { width: auto; }

/* 登入 */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 14px; }

/* 頂部列 */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 2;
}
.brand { white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.tab { border-color: transparent; background: transparent; white-space: nowrap; }
.tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* 管理頁 */
.page { max-width: 1040px; margin: 0 auto; padding: 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
td.count-cell { white-space: nowrap; }
.address-row { display: flex; align-items: center; gap: 8px; }
.address-row input { flex: 1.3; }
.address-row select { flex: 1; }
.address-row .at { color: var(--muted); font-weight: 600; }
#create-form, #domain-form { display: grid; gap: 14px; }
.type-choice { border: 0; padding: 0; margin: 0; min-inline-size: 0; display: grid; gap: 6px; }
.type-choice label { display: flex; gap: 8px; align-items: baseline; color: var(--text); }
.type-choice input { width: auto; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.help { font-size: 14px; color: var(--muted); }
.help summary { cursor: pointer; color: var(--accent); }
.help ol { padding-left: 20px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; white-space: nowrap; }
td.actions { white-space: nowrap; text-align: right; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.badge.ok { color: var(--ok); }
.badge.off { color: var(--danger); }
.badge.warn { color: var(--warn); }
.sub { display: block; color: var(--muted); font-size: 12px; }

.badge.cf { color: var(--accent); background: var(--accent-soft); }
.notice { margin: 0 0 12px; padding: 8px 12px; border-radius: 8px; background: var(--surface-2); color: var(--warn); font-size: 14px; }
dialog { width: min(440px, calc(100% - 32px)); color: var(--text); padding: 0; }
dialog::backdrop { background: rgb(0 0 0 / .4); }
.dialog-body { display: grid; gap: 12px; padding: 18px; }
.dialog-body p { margin: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.domain-list { list-style: none; margin: 0; padding: 0; }
.domain-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }

/* 收件匣：三欄 */
.inbox { display: grid; grid-template-columns: 250px 340px 1fr; height: calc(100vh - 53px); }
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; border-right: 1px solid var(--border); background: var(--surface); }
.pane-reader { border-right: 0; background: var(--bg); }
.pane-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); min-height: 45px; font-size: 14px; color: var(--muted); }
.pane-head > span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; }
.box-list, .message-list { overflow-y: auto; flex: 1; }
.group-label { padding: 10px 12px 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.box-item, .msg-item {
  display: block; width: 100%; text-align: left; border: 0; border-radius: 0;
  border-bottom: 1px solid var(--border); background: transparent; padding: 10px 12px;
}
.box-item { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.box-item .mono { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.box-item.selected, .msg-item.selected { background: var(--accent-soft); }
.count { background: var(--accent); color: #fff; border-radius: 99px; font-size: 12px; padding: 0 7px; }
.msg-item .row1 { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); }
.msg-item .from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-item .subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-item .snippet { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-item.unread .subject { font-weight: 700; }
.msg-item.unread .from { color: var(--text); font-weight: 600; }
.empty { color: var(--muted); padding: 24px 16px; text-align: center; }

.reader { flex: 1; display: flex; flex-direction: column; min-height: 0; overflow-y: auto; }
.reader-head { padding: 16px 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
.reader-head h2 { font-size: 20px; margin-bottom: 8px; overflow-wrap: anywhere; }
.meta { font-size: 13px; color: var(--muted); display: grid; gap: 2px; overflow-wrap: anywhere; }
.reader-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.attachments a { font-size: 13px; padding: 4px 10px; border: 1px solid var(--border); border-radius: 8px; color: var(--accent); text-decoration: none; }
.mail-frame { flex: 1; min-height: 60vh; width: 100%; border: 0; background: #fff; }
.mail-text { margin: 0; padding: 20px; white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 8px; font-size: 14px;
  max-width: calc(100% - 32px); z-index: 10;
}

/* 手機：一次只顯示一欄 */
@media (max-width: 860px) {
  .inbox { grid-template-columns: 1fr; }
  .inbox .pane { border-right: 0; }
  .inbox[data-pane="list"] .pane-messages,
  .inbox[data-pane="list"] .pane-reader,
  .inbox[data-pane="messages"] .pane-boxes,
  .inbox[data-pane="messages"] .pane-reader,
  .inbox[data-pane="reader"] .pane-boxes,
  .inbox[data-pane="reader"] .pane-messages { display: none; }
  .back { display: inline-block; }
  .grid-2 { grid-template-columns: 1fr; }
  .topbar { gap: 8px; padding: 8px; }
  .brand { display: none; }
  .address-row { flex-wrap: wrap; }
  .address-row input, .address-row select { flex: 1 1 100%; }
  .address-row .at { display: none; }
}
