:root {
  --bg: #f6f6f5;
  --surface: #ffffff;
  --surface-2: #f0f0ef;
  --surface-3: #e7e7e5;
  --line: #e3e3e1;
  --line-strong: #cfcfcc;
  --text: #161616;
  --text-2: #5c5c5a;
  --text-3: #8b8b88;
  --accent: #161616;
  --accent-text: #ffffff;
  --selected: #ebebea;
  --unread-dot: #161616;
  --focus: #2b6cff;
  --ok: #1f7a4a;
  --warn: #a15c00;
  --bad: #b42318;
  --ok-bg: #e6f3eb;
  --warn-bg: #fbf0dc;
  --bad-bg: #fbe8e6;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 28px rgba(0,0,0,.08);
  --radius: 10px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e0e0e;
    --surface: #161616;
    --surface-2: #1d1d1d;
    --surface-3: #262626;
    --line: #262626;
    --line-strong: #383838;
    --text: #ececec;
    --text-2: #a3a3a3;
    --text-3: #747474;
    --accent: #ececec;
    --accent-text: #111111;
    --selected: #232323;
    --unread-dot: #ececec;
    --ok: #5fc48b;
    --warn: #e0a54a;
    --bad: #f07267;
    --ok-bg: #12261a;
    --warn-bg: #2a2111;
    --bad-bg: #2c1513;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--text-2); }
.small { font-size: 12.5px; }
.error { color: var(--bad); font-size: 13px; min-height: 1em; margin: 8px 0 0; }
.spacer { flex: 1; }
kbd {
  font: 11px var(--mono);
  padding: 1px 5px;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--text-2);
  background: var(--surface);
}

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px;
  border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font-weight: 500;
  transition: background .12s, border-color .12s, transform .06s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { opacity: .9; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { height: 28px; padding: 0 10px; font-size: 13px; }
.btn[disabled] { opacity: .55; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; border-radius: 8px;
  border: 0; background: transparent; color: var(--text-2);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.on { color: var(--text); }
.link-btn { border: 0; background: none; color: var(--text-2); font-size: 12.5px; padding: 0 4px; }
.link-btn:hover { color: var(--text); text-decoration: underline; }
input:not([type=checkbox]):not([type=file]), select, textarea {
  width: 100%;
  border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--surface); padding: 8px 10px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--text-2); }
input[type=checkbox] { accent-color: var(--accent); }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; letter-spacing: -.01em; }
.brand-mark {
  width: 18px; height: 18px; border-radius: 5px; background: var(--text);
  box-shadow: inset 0 0 0 4px var(--text), inset 0 0 0 6px var(--bg);
}

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px; padding: 30px 26px 22px; box-shadow: var(--shadow);
}
.login-card .brand { font-size: 18px; }
.login-sub { color: var(--text-2); margin: 8px 0 22px; }
.g-btn { min-height: 44px; display: flex; justify-content: center; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--text-3); font-size: 12px; margin: 18px 0 14px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.stack { display: grid; gap: 10px; }
#login-code { letter-spacing: .35em; font-size: 18px; text-align: center; font-family: var(--mono); }

/* ---------- layout ---------- */
.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 236px minmax(300px, 400px) 1fr;
  grid-template-rows: 56px 1fr;
  grid-template-areas: "top top top" "side list reader";
}
.topbar {
  grid-area: top;
  display: flex; align-items: center; gap: 14px;
  padding: 0 14px 0 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.topbar .brand { width: 204px; flex: none; }
.search {
  flex: 1; max-width: 640px; position: relative; display: flex; align-items: center;
}
.search svg { position: absolute; left: 11px; color: var(--text-3); pointer-events: none; }
.search input { padding-left: 36px !important; padding-right: 36px !important; background: var(--surface-2) !important; border-color: transparent !important; height: 36px; }
.search input:focus { background: var(--surface) !important; border-color: var(--line-strong) !important; }
.search kbd { position: absolute; right: 10px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.sync-btn {
  display: flex; align-items: center; gap: 7px; height: 32px; padding: 0 10px;
  border: 0; border-radius: 8px; background: transparent; color: var(--text-2); font-size: 12.5px;
}
.sync-btn:hover { background: var(--surface-2); color: var(--text); }
.sync-btn.spinning svg { animation: spin .9s linear infinite; }
.sync-btn.bad { color: var(--bad); }
@keyframes spin { to { transform: rotate(360deg); } }
.user-menu { position: relative; }
.avatar-btn {
  width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--line-strong);
  background: var(--surface-2); font-weight: 600; font-size: 13px;
}
.menu {
  position: absolute; right: 0; top: 40px; z-index: 30; min-width: 240px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px;
}
.menu-head { padding: 8px 10px 10px; color: var(--text-2); font-size: 12.5px; border-bottom: 1px solid var(--line); margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; }
.menu-item {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 8px 10px; border: 0; border-radius: 6px; background: none; font-size: 13.5px; cursor: pointer;
}
.menu-item:hover { background: var(--surface-2); }
.menu-item.danger { color: var(--bad); }

/* ---------- sidebar ---------- */
.sidebar {
  grid-area: side; overflow-y: auto; padding: 14px 10px 24px;
  background: var(--bg); border-right: 1px solid var(--line);
}
.compose-btn { width: 100%; height: 40px; margin-bottom: 14px; border-radius: 10px; }
.folders, .domains { list-style: none; margin: 0; padding: 0; }
.nav-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  height: 32px; padding: 0 10px; border: 0; border-radius: 8px;
  background: none; color: var(--text-2); text-align: left; font-size: 13.5px;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--selected); color: var(--text); font-weight: 600; }
.nav-item .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .count { font-size: 12px; color: var(--text-3); font-variant-numeric: tabular-nums; }
.nav-item .count.unread { color: var(--text); font-weight: 650; }
.side-heading { margin: 20px 10px 6px; font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3); }
.domain-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--dot, var(--text-3)); }
.nav-item.sub { height: 28px; padding-left: 28px; font-size: 13px; }
.domains li + li { margin-top: 2px; }
.domain-group { margin-bottom: 6px; }

/* ---------- list ---------- */
.list-pane {
  grid-area: list; display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-right: 1px solid var(--line);
}
.list-head { display: flex; align-items: baseline; gap: 10px; padding: 16px 18px 10px; border-bottom: 1px solid var(--line); }
.list-head h1 { font-size: 17px; margin: 0; letter-spacing: -.01em; font-weight: 650; }
.msg-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.msg {
  position: relative; display: grid; grid-template-columns: 1fr auto; gap: 2px 10px;
  padding: 11px 16px 12px 26px; border-bottom: 1px solid var(--line); cursor: pointer;
}
.msg:hover { background: var(--surface-2); }
.msg.selected { background: var(--selected); box-shadow: inset 3px 0 0 var(--text); }
.msg.unread::before {
  content: ""; position: absolute; left: 11px; top: 17px; width: 7px; height: 7px; border-radius: 50%; background: var(--unread-dot);
}
.msg .who { min-width: 0; display: flex; align-items: center; gap: 6px; font-size: 13.5px; color: var(--text-2); overflow: hidden; white-space: nowrap; }
.msg .who .name { overflow: hidden; text-overflow: ellipsis; }
.msg.unread .who, .msg.unread .subj { color: var(--text); font-weight: 650; }
.msg .n { font-size: 11.5px; color: var(--text-3); font-weight: 500; }
.msg time { font-size: 12px; color: var(--text-3); white-space: nowrap; font-variant-numeric: tabular-nums; }
.msg.unread time { color: var(--text); font-weight: 600; }
.msg .subj { grid-column: 1 / -1; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .snip { grid-column: 1 / -1; font-size: 12.5px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .meta { grid-column: 1 / -1; display: flex; align-items: center; gap: 6px; margin-top: 4px; min-height: 0; }
.msg .meta:empty { display: none; }
.msg .meta svg { width: 14px; height: 14px; color: var(--text-3); }
.msg .star-on { color: var(--warn) !important; fill: currentColor; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; height: 20px; padding: 0 7px; border-radius: 999px;
  font-size: 11.5px; font-weight: 500; background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.chip .domain-dot { width: 6px; height: 6px; }
.chip.ok { background: var(--ok-bg); color: var(--ok); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.bad { background: var(--bad-bg); color: var(--bad); }
.list-empty { padding: 48px 20px; text-align: center; color: var(--text-3); }
.load-more { margin: 10px auto 16px; display: flex; }
.skeleton { height: 74px; border-bottom: 1px solid var(--line); background: linear-gradient(90deg, transparent, var(--surface-2), transparent); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* ---------- reader ---------- */
.reader-pane { grid-area: reader; overflow-y: auto; background: var(--bg); min-width: 0; }
.empty { height: 100%; display: grid; place-content: center; justify-items: center; gap: 8px; color: var(--text-3); }
.empty svg { width: 40px; height: 40px; stroke-width: 1.2; }
.reader-bar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 2px; padding: 8px 14px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.reader-bar .sep { width: 1px; height: 20px; background: var(--line-strong); margin: 0 6px; }
.thread { max-width: 940px; margin: 0 auto; padding: 18px 22px 60px; }
.thread-title { font-size: 21px; line-height: 1.3; font-weight: 650; letter-spacing: -.015em; margin: 4px 0 6px; overflow-wrap: anywhere; }
.thread-sub { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 18px; color: var(--text-2); font-size: 12.5px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  margin-bottom: 10px; overflow: hidden;
}
.card.collapsed { cursor: pointer; }
.card.collapsed:hover { border-color: var(--line-strong); }
.card-head { display: flex; gap: 12px; padding: 14px 16px 10px; align-items: flex-start; }
.card.collapsed .card-head { padding-bottom: 14px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 650; font-size: 13.5px; color: #fff; background: var(--av, #666);
}
.card-who { flex: 1; min-width: 0; }
.card-who .line1 { display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; }
.card-who .name { font-weight: 650; }
.card-who .addr { color: var(--text-3); font-size: 12.5px; overflow-wrap: anywhere; }
.card-who .line2 { color: var(--text-2); font-size: 12.5px; margin-top: 1px; overflow-wrap: anywhere; }
.card.collapsed .line2 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-side { display: flex; align-items: center; gap: 4px; color: var(--text-3); font-size: 12px; white-space: nowrap; }
.card-badges { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 10px 64px; }
.card-body { padding: 4px 16px 14px 64px; }
.card-body iframe { display: block; width: 100%; border: 0; height: 120px; background: #fff; border-radius: 6px; }
.card-body iframe.dark-text { background: transparent; }
.notice {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin: 0 16px 10px 64px; padding: 8px 12px; border-radius: 8px;
  background: var(--surface-2); color: var(--text-2); font-size: 12.5px;
}
.atts { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 16px 14px 64px; }
.att {
  display: flex; align-items: center; gap: 10px; max-width: 280px; padding: 8px 12px 8px 8px;
  border: 1px solid var(--line); border-radius: 8px; text-decoration: none; background: var(--surface);
}
.att:hover { border-color: var(--line-strong); background: var(--surface-2); }
.att .thumb { width: 36px; height: 36px; border-radius: 6px; background: var(--surface-3); display: grid; place-items: center; overflow: hidden; flex: none; }
.att .thumb img { width: 100%; height: 100%; object-fit: cover; }
.att .fname { font-size: 12.5px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att .fsize { font-size: 11.5px; color: var(--text-3); }
.card-actions { display: flex; gap: 8px; padding: 0 16px 14px 64px; }
.reply-box {
  display: flex; gap: 8px; margin-top: 16px;
}
.reply-box .btn { height: 38px; }

/* ---------- compose ---------- */
.compose {
  position: fixed; right: 24px; bottom: 0; z-index: 40;
  width: min(620px, calc(100vw - 32px)); max-height: min(720px, calc(100dvh - 72px));
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line-strong); border-bottom: 0;
  border-radius: 12px 12px 0 0; box-shadow: var(--shadow);
}
.compose.min .compose-body { display: none; }
.compose.min { width: 300px; }
.compose-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 6px 6px 16px; background: var(--surface-2); border-radius: 12px 12px 0 0; font-weight: 600; font-size: 13.5px; cursor: default;
}
.compose-head-actions { display: flex; }
.compose-body { display: flex; flex-direction: column; min-height: 0; overflow-y: auto; padding: 4px 16px 12px; }
.field { display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--line); min-height: 42px; }
.field label { width: 54px; flex: none; color: var(--text-3); font-size: 13px; }
.field input, .field select { border: 0 !important; padding: 8px 0 !important; background: transparent !important; border-radius: 0 !important; }
.from-row { flex: 1; display: flex; gap: 8px; min-width: 0; }
.from-row input { flex: 0 0 38%; min-width: 0; }
.from-row select { flex: 1; min-width: 0; }
#c-body { border: 0; padding: 12px 0; min-height: 220px; resize: vertical; line-height: 1.55; background: transparent; }
.c-quote { padding: 4px 0 8px; color: var(--text-2); }
.c-files { list-style: none; margin: 0; padding: 0 0 8px; display: flex; flex-wrap: wrap; gap: 6px; }
.c-files li { display: flex; align-items: center; gap: 6px; padding: 4px 4px 4px 10px; border-radius: 6px; background: var(--surface-2); font-size: 12.5px; }
.c-files button { border: 0; background: none; color: var(--text-3); width: 22px; height: 22px; border-radius: 4px; }
.c-files button:hover { background: var(--surface-3); color: var(--text); }
.compose-foot { display: flex; align-items: center; gap: 8px; padding-top: 8px; }
.compose-foot .btn { min-width: 92px; }

/* ---------- dialogs & toast ---------- */
.dialog {
  border: 1px solid var(--line); border-radius: 14px; background: var(--surface); color: var(--text);
  padding: 20px 22px; box-shadow: var(--shadow); max-width: min(460px, calc(100vw - 32px));
}
.dialog.wide { max-width: min(900px, calc(100vw - 32px)); width: 100%; }
.dialog::backdrop { background: rgba(0,0,0,.35); }
.dialog h2 { font-size: 16px; margin: 0 0 14px; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-head h2 { margin: 0; }
.keys { display: grid; grid-template-columns: auto 1fr; gap: 8px 18px; margin: 0 0 18px; font-size: 13.5px; }
.keys dd { margin: 0; color: var(--text-2); }
.kv { display: grid; grid-template-columns: 150px 1fr; gap: 6px 14px; font-size: 12.5px; margin: 14px 0; }
.kv dt { color: var(--text-3); }
.kv dd { margin: 0; font-family: var(--mono); overflow-wrap: anywhere; }
.source { margin: 0; max-height: 50vh; overflow: auto; padding: 12px; border-radius: 8px; background: var(--surface-2); font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
  display: flex; align-items: center; gap: 14px; padding: 10px 14px 10px 16px; border-radius: 10px;
  background: var(--text); color: var(--bg); font-size: 13.5px; box-shadow: var(--shadow);
}
.toast button { border: 0; background: none; color: inherit; font-weight: 650; text-decoration: underline; }

.only-mobile { display: none; }
.scrim { display: none; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .app { grid-template-columns: 210px minmax(280px, 340px) 1fr; }
  .topbar .brand { width: 178px; }
}
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
  .topbar { padding: 0 8px 0 6px; gap: 8px; }
  .topbar .brand { width: auto; }
  .brand-name, .hide-mobile { display: none; }
  .only-mobile { display: inline-grid; }
  .sidebar {
    position: fixed; z-index: 50; top: 0; bottom: 0; left: 0; width: 280px; max-width: 85vw;
    transform: translateX(-100%); transition: transform .2s ease; background: var(--surface); box-shadow: var(--shadow);
  }
  .app.nav-open .sidebar { transform: none; }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; z-index: 45; background: rgba(0,0,0,.35); }
  .list-pane, .reader-pane { grid-area: main; border: 0; }
  .app.reading .list-pane { display: none; }
  .app:not(.reading) .reader-pane { display: none; }
  .thread { padding: 14px 16px 60px; }
  .card-head { padding: 12px 12px 8px; flex-wrap: wrap; row-gap: 2px; }
  .card-who { flex-basis: calc(100% - 48px); }
  .card-side { width: 100%; padding-left: 48px; }
  .card-side time { margin-right: auto; }
  .card.collapsed .card-side { display: none; }
  .card-body, .card-badges, .atts, .card-actions { padding-left: 12px; padding-right: 12px; }
  .notice { margin-left: 12px; margin-right: 12px; }
  .compose { right: 0; left: 0; width: 100%; max-height: 100dvh; top: 0; border-radius: 0; }
  .compose-head { border-radius: 0; }
  .compose.min { top: auto; width: 100%; }
  .kv { grid-template-columns: 1fr; gap: 2px; }
  .kv dd { margin-bottom: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
