:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #f1f3f8;
  --line: #e3e6ee;
  --text: #1c1f26;
  --muted: #6b7180;
  --accent: #4f6bff;
  --accent-2: #3d55e0;
  --accent-soft: #e8ecff;
  --danger: #d93a3a;
  --star: #f5a524;
  --shadow: 0 10px 40px rgb(20 24 40 / 0.14);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1116;
    --panel: #171a21;
    --panel-2: #1e222b;
    --line: #2a2f3a;
    --text: #e7e9ee;
    --muted: #9198a8;
    --accent: #7088ff;
    --accent-2: #8a9dff;
    --accent-soft: #232a48;
    --shadow: 0 10px 40px rgb(0 0 0 / 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 20px; height: 20px; fill: currentColor; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.spacer { flex: 1; }
.error { color: var(--danger); margin: 0; }

.primary {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 10px 18px;
  font-weight: 600;
  transition: background 0.15s;
}
.primary:hover { background: var(--accent-2); }
.primary:disabled { opacity: 0.6; cursor: progress; }

.ghost {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
}
.ghost:hover { background: var(--panel-2); }
.ghost.small { padding: 4px 10px; font-size: 12.5px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  text-decoration: none;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.on { color: var(--star); }

/* ---------- Sign in ---------- */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgb(79 107 255 / 0.18), transparent 60%),
    radial-gradient(900px 500px at 110% 110%, rgb(124 77 255 / 0.16), transparent 60%),
    var(--bg);
}
.login-card {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 32px 28px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 24px; }
.login-card p { margin: 0; }
.login-logo { width: 48px; height: 48px; }
.login-card label { display: grid; gap: 6px; font-weight: 500; }
.login-card input {
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 10px;
  padding: 11px 12px;
  outline: none;
}
.login-card input:focus { border-color: var(--accent); background: var(--panel); }
.fine { font-size: 12px; }

/* ---------- Layout ---------- */
.app {
  height: 100%;
  display: grid;
  grid-template-columns: 240px minmax(320px, 420px) 1fr;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 12px;
  border-right: 1px solid var(--line);
  background: var(--panel);
  min-height: 0;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 2px 8px; }
.brand img { width: 28px; height: 28px; }
.compose-btn { display: flex; align-items: center; gap: 10px; justify-content: center; padding: 12px; }
.compose-btn svg { width: 18px; height: 18px; }

.folders { flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 2px; }
.folder {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  text-align: left;
  width: 100%;
  color: var(--text);
}
.folder:hover { background: var(--panel-2); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folder svg { width: 18px; height: 18px; color: var(--muted); }
.folder.active svg { color: var(--accent); }
.folder .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder .count { font-size: 12px; font-weight: 600; color: var(--accent); }
.folder.depth-1 { padding-left: 26px; }
.folder.depth-2 { padding-left: 42px; }
.folder.depth-3 { padding-left: 58px; }
.folder-divider { height: 1px; background: var(--line); margin: 8px 6px; }

.account { display: flex; align-items: center; gap: 8px; padding: 8px; border-top: 1px solid var(--line); }
.account span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12.5px; }

/* ---------- Message list ---------- */
.list-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
}
.list-head { display: flex; align-items: center; gap: 6px; padding: 12px 12px 6px; }
.search {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0 10px;
}
.search:focus-within { border-color: var(--accent); background: var(--panel); }
.search svg { color: var(--muted); width: 18px; height: 18px; }
.search input { flex: 1; border: 0; background: transparent; padding: 9px 0; outline: none; min-width: 0; }
.list-title { display: flex; align-items: baseline; gap: 10px; padding: 6px 18px 10px; }
.list-title h2 { margin: 0; font-size: 18px; }

.messages { list-style: none; margin: 0; padding: 0 8px 8px; overflow-y: auto; flex: 1; }
.msg {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  column-gap: 10px;
  row-gap: 2px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
}
.msg:hover { background: var(--panel-2); }
.msg.selected { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }
.msg .dot { grid-row: 1; align-self: center; width: 8px; height: 8px; border-radius: 50%; }
.msg.unread .dot { background: var(--accent); }
.msg .who { grid-column: 2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg.unread .who, .msg.unread .subj { font-weight: 650; }
.msg .when { grid-column: 3; font-size: 12px; color: var(--muted); white-space: nowrap; }
.msg .subj { grid-column: 2 / 4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; gap: 6px; align-items: center; }
.msg .subj span { overflow: hidden; text-overflow: ellipsis; }
.msg .subj svg { width: 14px; height: 14px; color: var(--muted); }
.msg .subj .star { color: var(--star); }
.msg.unread .when { color: var(--accent); font-weight: 600; }
.more { margin: 4px 16px 16px; }
.empty { text-align: center; padding: 40px 20px; }

.skeleton { height: 54px; margin: 4px 0; border-radius: 10px; background: linear-gradient(90deg, var(--panel-2), var(--line), var(--panel-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Reader ---------- */
.reader { min-width: 0; min-height: 0; overflow-y: auto; background: var(--bg); }
.reader-empty { height: 100%; display: grid; place-content: center; justify-items: center; gap: 10px; }
.reader-empty svg { width: 56px; height: 56px; opacity: 0.35; }
#reader-view { max-width: 980px; margin: 0 auto; padding: 0 24px 32px; }
.toolbar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 0;
  background: var(--bg);
}
.tool { border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 7px 14px; font-weight: 500; }
.tool:hover { border-color: var(--accent); color: var(--accent); }
.move { border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 7px 8px; max-width: 150px; }
.r-subject { font-size: 22px; line-height: 1.3; margin: 8px 0 16px; overflow-wrap: anywhere; }
.r-head { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700; color: #fff;
  background: var(--accent);
}
.r-people { flex: 1; min-width: 0; }
.r-people div { overflow: hidden; text-overflow: ellipsis; }
.images-bar {
  display: flex; align-items: center; gap: 12px; justify-content: space-between;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 12px; margin-bottom: 12px; font-size: 13px;
}
.attachments { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-wrap: wrap; gap: 8px; }
.attachments:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 7px 10px; max-width: 280px; text-decoration: none; color: inherit;
}
.chip:hover { border-color: var(--accent); }
.chip svg { width: 18px; height: 18px; color: var(--accent); }
.chip .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .size { color: var(--muted); font-size: 12px; white-space: nowrap; }
.chip button { border: 0; background: transparent; color: var(--muted); padding: 0 2px; }
.r-body {
  display: block;
  width: 100%;
  min-height: 200px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

/* ---------- Compose ---------- */
.compose {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}
.compose::backdrop { background: rgb(10 12 20 / 0.45); backdrop-filter: blur(2px); }
.compose form { display: flex; flex-direction: column; height: min(680px, calc(100vh - 48px)); }
.compose-head { display: flex; align-items: center; padding: 12px 12px 8px 20px; }
.compose-head h2 { flex: 1; margin: 0; font-size: 16px; }
.field { display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--line); padding: 0 20px; }
.field label { width: 56px; color: var(--muted); }
.field input { flex: 1; border: 0; background: transparent; padding: 11px 0; outline: none; min-width: 0; }
#c-body {
  flex: 1;
  border: 0;
  resize: none;
  padding: 16px 20px;
  background: transparent;
  outline: none;
  font: 14.5px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
#c-files { padding: 0 20px; }
.compose-foot { display: flex; align-items: center; gap: 10px; padding: 12px 20px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.attach { cursor: pointer; border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; }
.from-name { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 5px 8px; width: 140px; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: max-content; max-width: calc(100vw - 32px); }
.toast {
  background: #1c1f26; color: #fff; border-radius: 10px; padding: 10px 16px;
  box-shadow: var(--shadow); animation: rise 0.2s ease-out;
  display: flex; gap: 14px; align-items: center;
}
.toast.err { background: var(--danger); }
.toast button { background: transparent; border: 0; color: #9db0ff; font-weight: 600; }
@keyframes rise { from { transform: translateY(8px); opacity: 0; } }

.only-mobile { display: none; }
.fab {
  position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 18px; padding: 0;
  place-items: center; box-shadow: var(--shadow); z-index: 10;
}

/* ---------- Narrow screens: one pane at a time ---------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(280px, 85vw); z-index: 20;
    transform: translateX(-100%); transition: transform 0.2s; box-shadow: var(--shadow);
  }
  .app.menu-open .sidebar { transform: none; }
  .reader { display: none; }
  .app.reading .list-pane { display: none; }
  .app.reading .reader { display: block; }
  #reader-view { padding: 0 16px 24px; }
  .toolbar { flex-wrap: wrap; row-gap: 6px; }
  .tool { padding: 6px 10px; white-space: nowrap; }
  .move { max-width: 120px; }
  .r-head { flex-wrap: wrap; }
  .r-people div { white-space: normal; overflow-wrap: anywhere; }
  .r-head time { flex-basis: 100%; padding-left: 52px; }
  .compose { width: 100vw; max-width: 100vw; max-height: 100dvh; height: 100dvh; border-radius: 0; margin: 0; }
  .compose form { height: 100dvh; }
}
