:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e3e6eb;
  --text: #1b1f27;
  --muted: #697386;
  --accent: #2f5bea;
  --accent-soft: #e8eefe;
  --danger: #c93a3a;
  --star: #e3a008;
  --unread-bg: #ffffff;
  --read-bg: #f9fafb;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #181b21;
    --surface-2: #20242c;
    --border: #2b3039;
    --text: #e6e8ec;
    --muted: #8b93a3;
    --accent: #6d8dff;
    --accent-soft: #1f2a4d;
    --danger: #ef6b6b;
    --unread-bg: #1c2027;
    --read-bg: #16191e;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); margin: 8px 0 0; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface);
  padding: 7px 12px; border-radius: 8px; cursor: pointer;
  text-decoration: none; color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.icon-btn { border: 0; background: transparent; font-size: 20px; padding: 4px 8px; cursor: pointer; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; }
.logo {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M5 8l7 5 7-5' stroke='white' stroke-width='2' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-size: cover; flex: none;
}

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 10px;
}
.login-card .brand { margin-bottom: 10px; }
.login-card label { font-size: 13px; color: var(--muted); }
.login-card input {
  padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font: inherit;
}
.login-card .btn { justify-content: center; padding: 10px; }

/* layout */
.app {
  height: 100dvh; display: grid;
  grid-template-columns: 220px minmax(300px, 420px) 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: 12px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.search { flex: 1; max-width: 560px; margin-left: 24px; }
.search input {
  width: 100%; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--surface); }

.sidebar { grid-area: side; padding: 12px 8px; overflow-y: auto; border-right: 1px solid var(--border); }
.nav-group { display: grid; gap: 2px; }
.nav-title { margin: 18px 12px 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.nav-item {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  border: 0; background: transparent; text-align: left; padding: 7px 12px; border-radius: 8px; cursor: pointer;
  overflow: hidden;
}
.nav-item span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.count { font-size: 12px; font-weight: 600; }
.count.muted { font-weight: 400; }

.list-pane {
  grid-area: list; display: flex; flex-direction: column; min-height: 0;
  border-right: 1px solid var(--border); background: var(--surface);
}
.toolbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-bottom: 1px solid var(--border); min-height: 48px; flex-wrap: wrap;
}
.toolbar-title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bulk { display: flex; gap: 6px; flex-wrap: wrap; }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.row {
  display: grid; grid-template-columns: auto auto 1fr auto; gap: 4px 10px; align-items: center;
  padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; background: var(--read-bg);
}
.row.unread { background: var(--unread-bg); }
.row.unread .from, .row.unread .subj { font-weight: 650; }
.row:hover { filter: brightness(.98); }
.row.selected { background: var(--accent-soft); }
.row .from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .time { font-size: 12px; color: var(--muted); }
.row .subj, .row .to {
  grid-column: 3 / 5; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.row .to { font-size: 12px; color: var(--muted); }
.row .dot { width: 8px; height: 8px; border-radius: 50%; background: transparent; }
.row.unread .dot { background: var(--accent); }
.star-btn { border: 0; background: transparent; cursor: pointer; padding: 0 2px; font-size: 16px; color: var(--muted); }
.star-btn.on { color: var(--star); }
.list-footer { padding: 10px; text-align: center; }
.empty { color: var(--muted); }

.reader-pane { grid-area: reader; overflow-y: auto; min-width: 0; }
.reader-empty { height: 100%; display: grid; place-items: center; color: var(--muted); }
.reader { padding: 16px 24px 40px; max-width: 980px; }
.reader-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.subject { font-size: 21px; font-weight: 650; margin: 0 0 14px; word-break: break-word; }
.meta { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 14px; }
.meta-lines { flex: 1; min-width: 0; word-break: break-word; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--accent-soft); color: var(--accent); font-weight: 700;
}
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 8px 12px; border-radius: 8px; background: var(--surface-2); margin-bottom: 12px; font-size: 13px;
}
.body-wrap { background: #fff; border-radius: var(--radius); border: 1px solid var(--border); overflow: hidden; }
#r-body { width: 100%; border: 0; display: block; min-height: 120px; background: #fff; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.attachment {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; max-width: 280px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); text-decoration: none;
}
.attachment:hover { background: var(--surface-2); }
.attachment .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow);
  z-index: 50; max-width: calc(100vw - 32px);
}
.scrim { display: none; }
.only-mobile { display: none !important; }

@media (max-width: 1100px) {
  .app { grid-template-columns: 200px 340px 1fr; }
}
@media (max-width: 820px) {
  .only-mobile { display: inline-flex !important; }
  .brand-text { display: none; }
  .search { margin-left: 0; }
  #logout-btn { padding: 7px 8px; }
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "list"; }
  .app.reading { grid-template-areas: "top" "reader"; }
  .app.reading .list-pane { display: none; }
  .app:not(.reading) .reader-pane { display: none; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: 260px; z-index: 30;
    background: var(--surface); transform: translateX(-100%); transition: transform .2s;
  }
  .app.drawer .sidebar { transform: none; }
  .app.drawer .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
  .reader { padding: 12px 16px 32px; }
  .list-pane { border-right: 0; }
}
