:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --hover: #eef0f4;
  --selected: #e8e8fb;
  --border: #e3e6eb;
  --text: #1c2024;
  --text-2: #60646c;
  --text-3: #8b8d98;
  --accent: #5b5bd6;
  --accent-hover: #4f4fc4;
  --accent-soft: #ededfc;
  --accent-text: #ffffff;
  --danger: #dc3e42;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.06), 0 1px 3px rgb(16 24 40 / 0.08);
  --shadow-lg: 0 12px 32px -8px rgb(16 24 40 / 0.22), 0 4px 12px -4px rgb(16 24 40 / 0.1);
  --radius: 12px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111113;
    --surface: #18191b;
    --surface-2: #212225;
    --hover: #222327;
    --selected: #24244a;
    --border: #2e3035;
    --text: #edeef0;
    --text-2: #b0b4ba;
    --text-3: #777b84;
    --accent: #6e6ade;
    --accent-hover: #7f7be6;
    --accent-soft: #23234a;
    --danger: #ec5d5e;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-lg: 0 16px 40px -8px rgb(0 0 0 / 0.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
svg { display: block; width: 18px; height: 18px; flex: none; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 38px; padding: 0 16px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); cursor: pointer; font-weight: 550; transition: background .15s, transform .05s;
}
.btn:hover { background: var(--hover); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: transparent; color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: .6; cursor: progress; }
.btn-block { width: 100%; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); height: 34px; padding: 0 10px; }
.btn-ghost:hover { background: var(--hover); color: var(--text); }
.btn-danger:hover { color: var(--danger); }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; border: 0; border-radius: 10px;
  background: transparent; color: var(--text-2); cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.link-btn { border: 0; background: none; color: var(--text-2); cursor: pointer; font-size: 13px; padding: 4px 6px; border-radius: 6px; }
.link-btn:hover { color: var(--accent); background: var(--accent-soft); }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: -0.01em; }
.brand-mark {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, #7c7bf0, var(--accent)); color: #fff; box-shadow: var(--shadow);
}
.brand-mark svg { width: 18px; height: 18px; }
.brand-lg { font-size: 22px; justify-content: center; }
.brand-lg .brand-mark { width: 44px; height: 44px; border-radius: 12px; }
.brand-lg .brand-mark svg { width: 24px; height: 24px; }

/* ---------- login ---------- */
.login {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px;
  background:
    radial-gradient(1200px 600px at 10% -10%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 60%),
    radial-gradient(900px 500px at 110% 110%, color-mix(in srgb, #3e9bff 14%, transparent), transparent 60%),
    var(--bg);
}
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 36px 28px 28px; box-shadow: var(--shadow-lg); display: grid; gap: 16px;
}
.login-sub { margin: -6px 0 6px; text-align: center; color: var(--text-2); }
.field { display: grid; gap: 6px; font-weight: 550; font-size: 13px; color: var(--text-2); }
.field input {
  height: 42px; padding: 0 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); color: var(--text); font-size: 15px; outline: none; transition: border-color .15s, box-shadow .15s;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); background: var(--surface); }
.login-error { margin: 0; color: var(--danger); font-size: 13px; }
.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }

/* ---------- app layout ---------- */
.app { display: grid; grid-template-columns: 240px minmax(300px, 400px) 1fr; height: 100dvh; overflow: hidden; }

.sidebar { display: flex; flex-direction: column; gap: 18px; padding: 18px 14px; border-right: 1px solid var(--border); background: var(--bg); min-height: 0; }
.sidebar .brand { padding: 2px 6px; }
.btn-compose { height: 44px; border-radius: 12px; box-shadow: var(--shadow); justify-content: flex-start; padding: 0 18px; }
.folders { display: grid; gap: 2px; }
.folder { display: flex; align-items: center; gap: 12px; height: 38px; padding: 0 12px; border-radius: 10px; color: var(--text-2); font-weight: 550; }
.folder:hover { background: var(--hover); color: var(--text); }
.folder.active { background: var(--accent-soft); color: var(--accent); }
.badge { margin-left: auto; min-width: 22px; height: 20px; padding: 0 7px; border-radius: 10px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 650; display: grid; place-items: center; }
.sidebar-foot { margin-top: auto; display: flex; align-items: center; gap: 8px; padding: 10px 6px 0; border-top: 1px solid var(--border); }
.account { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.account-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); font-size: 13px; }

.list-pane { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--surface); }
.list-head { display: flex; align-items: center; gap: 6px; padding: 14px 12px 8px 20px; }
.list-head h1 { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: -0.01em; flex: 1; }
.search { display: flex; align-items: center; gap: 8px; margin: 0 14px 10px; padding: 0 12px; height: 38px; border-radius: 10px; background: var(--surface-2); color: var(--text-3); border: 1px solid transparent; }
.search:focus-within { border-color: var(--accent); background: var(--surface); box-shadow: 0 0 0 3px var(--accent-soft); }
.search input { flex: 1; border: 0; outline: none; background: transparent; color: var(--text); min-width: 0; }

.message-list { list-style: none; margin: 0; padding: 0 8px 12px; overflow-y: auto; flex: 1; }
.msg {
  display: grid; grid-template-columns: 36px 1fr auto; gap: 2px 12px; padding: 12px; border-radius: 12px;
  cursor: pointer; position: relative; outline: none;
}
.msg:hover { background: var(--hover); }
.msg:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }
.msg.selected { background: var(--selected); }
.msg .avatar { grid-row: span 3; margin-top: 2px; }
.msg-from { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.msg-date { font-size: 12px; color: var(--text-3); white-space: nowrap; align-self: center; }
.msg-subject { grid-column: 2 / 4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.msg-subject svg { width: 14px; height: 14px; color: var(--text-3); }
.msg-subject > span:not(.tag) { overflow: hidden; text-overflow: ellipsis; }
.tag { flex: none; margin-left: auto; padding: 0 7px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); font-size: 11.5px; font-weight: 600; line-height: 18px; }
.msg-snippet { grid-column: 2 / 4; color: var(--text-3); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg.unread .msg-from, .msg.unread .msg-subject { color: var(--text); font-weight: 700; }
.msg.unread::before { content: ""; position: absolute; left: 3px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--accent); }
.list-more { display: flex; justify-content: center; padding: 8px; }
.list-empty { padding: 48px 24px; text-align: center; color: var(--text-3); }
.skeleton { height: 74px; margin: 4px 0; border-radius: 12px; background: linear-gradient(90deg, var(--surface-2), var(--hover), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

.avatar { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-weight: 650; font-size: 14px; color: #fff; flex: none; text-transform: uppercase; }
.avatar-sm { width: 28px; height: 28px; font-size: 12px; }
.avatar-lg { width: 42px; height: 42px; font-size: 16px; }

/* ---------- reader ---------- */
.reader-pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--surface); }
.reader-empty { margin: auto; display: grid; justify-items: center; gap: 12px; color: var(--text-3); }
.empty-mark { display: grid; place-items: center; width: 64px; height: 64px; border-radius: 20px; background: var(--surface-2); color: var(--text-3); }
.empty-mark svg { width: 28px; height: 28px; }
.reader-content { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.reader-toolbar { display: flex; align-items: center; gap: 4px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.reader-toolbar .spacer { flex: 1; }
.reader-scroll { overflow-y: auto; flex: 1; display: flex; flex-direction: column; padding: 24px 28px 28px; gap: 18px; }
.reader-subject { margin: 0; font-size: 22px; line-height: 1.3; font-weight: 700; letter-spacing: -0.015em; overflow-wrap: anywhere; }
.reader-meta { display: grid; grid-template-columns: auto 1fr auto; gap: 0 12px; align-items: center; }
.reader-meta .avatar { grid-row: span 2; }
.meta-from { font-weight: 650; overflow-wrap: anywhere; }
.meta-from small { font-weight: 400; color: var(--text-3); margin-left: 4px; font-size: 13px; }
.meta-to { color: var(--text-3); font-size: 13px; overflow-wrap: anywhere; }
.meta-date { grid-row: 1; grid-column: 3; color: var(--text-3); font-size: 13px; white-space: nowrap; }
.notice { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 10px; background: var(--surface-2); color: var(--text-2); font-size: 13px; }
.notice .link-btn { margin-left: auto; color: var(--accent); font-weight: 600; }
.body-frame { width: 100%; flex: 1; min-height: 420px; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.body-frame.plain { background: var(--surface); border-color: transparent; }

.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 8px; max-width: 280px; height: 36px; padding: 0 10px 0 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); font-size: 13px; }
a.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-size { color: var(--text-3); white-space: nowrap; }
.chip .icon-btn { width: 24px; height: 24px; border-radius: 6px; }
.chip svg { width: 16px; height: 16px; }

/* ---------- composer ---------- */
.composer {
  position: fixed; right: 24px; bottom: 0; width: min(560px, calc(100vw - 32px)); height: min(620px, calc(100dvh - 48px));
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-bottom: 0;
  border-radius: 16px 16px 0 0; box-shadow: var(--shadow-lg); z-index: 40; animation: rise .18s ease-out;
}
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }
.composer-head { display: flex; align-items: center; padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--border); }
.composer-head h2 { margin: 0; font-size: 15px; font-weight: 650; flex: 1; }
.composer-form { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.composer .row { display: flex; align-items: center; gap: 10px; padding: 0 18px; border-bottom: 1px solid var(--border); min-height: 42px; }
.composer .row > span { color: var(--text-3); width: 52px; flex: none; font-size: 13px; }
.composer .row input { flex: 1; border: 0; outline: none; background: transparent; min-width: 0; height: 40px; }
.composer .row input[readonly] { color: var(--text-2); }
.composer textarea { flex: 1; border: 0; outline: none; resize: none; padding: 14px 18px; background: transparent; min-height: 120px; line-height: 1.6; }
.composer .chips { padding: 0 18px 10px; }
.composer-foot { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-top: 1px solid var(--border); }
.composer-hint { margin-left: auto; color: var(--text-3); font-size: 12px; }

/* ---------- toasts ---------- */
.toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 60; pointer-events: none; }
.toast { padding: 10px 16px; border-radius: 10px; background: var(--text); color: var(--bg); box-shadow: var(--shadow-lg); font-weight: 550; animation: rise .18s ease-out; max-width: calc(100vw - 32px); }
.toast.error { background: var(--danger); color: #fff; }

.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

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

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .app { grid-template-columns: 210px minmax(280px, 340px) 1fr; }
  .reader-scroll { padding: 20px; }
}

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-grid !important; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 50; background: var(--surface);
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow-lg);
  }
  .app.drawer-open .sidebar { transform: none; }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 45; }
  .list-pane { border-right: 0; }
  .list-head { padding-left: 8px; }
  .reader-pane { display: none; }
  .app.reading .list-pane { display: none; }
  .app.reading .reader-pane { display: flex; }
  .app.reading .fab { display: none !important; }
  .reader-scroll { padding: 16px; }
  .reader-subject { font-size: 19px; }
  .reader-meta { grid-template-columns: auto 1fr; }
  .reader-meta .avatar { grid-row: span 3; }
  .meta-date { grid-row: auto; grid-column: 2; }
  .meta-from small { display: block; margin-left: 0; }
  .fab {
    position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 18px; border: 0;
    background: var(--accent); color: #fff; box-shadow: var(--shadow-lg); place-items: center; z-index: 30; cursor: pointer;
  }
  .fab svg { width: 22px; height: 22px; }
  .composer { inset: 0; width: auto; height: auto; border-radius: 0; border: 0; }
  .composer-hint { display: none; }
  .body-frame { min-height: 60dvh; }
}
