:root {
  --bg: #F3F5F8;
  --surface: #FFFFFF;
  --ink: #17202E;
  --muted: #5E6879;
  --line: #DDE2E9;
  --accent: #1F5F8B;
  --accent-ink: #FFFFFF;
  --accent-soft: #E3EDF5;
  --danger: #B42318;
  --av-l: 88%;
  --av-ink-l: 28%;
  --radius: 14px;
  --bar-h: 56px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F141B;
    --surface: #171E28;
    --ink: #E6EAF0;
    --muted: #97A1B2;
    --line: #273142;
    --accent: #7FB2DB;
    --accent-ink: #0B1520;
    --accent-soft: #1C2B3A;
    --danger: #F97066;
    --av-l: 24%;
    --av-ink-l: 82%;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- Каркас экранов ---------- */

.view {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.bar {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: calc(var(--bar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) 0 max(8px, env(safe-area-inset-left, 0px));
  background: var(--bg);
  border-bottom: 1px solid transparent;
  flex: none;
}
.bar.scrolled { border-bottom-color: var(--line); }
.bar-title {
  flex: 1;
  margin: 0 4px;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar-spacer { flex: 1; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink);
}
.icon-btn:active { background: var(--accent-soft); }
.icon-btn.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.offline-bar {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(88px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  white-space: nowrap;
}

/* ---------- Вход ---------- */

.view-login { justify-content: center; overflow-y: auto; }
.login {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  padding: calc(32px + env(safe-area-inset-top, 0px)) 24px calc(32px + env(safe-area-inset-bottom, 0px));
}
.login-mark {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-ink);
  margin-bottom: 24px;
}
.login-mark svg { width: 30px; height: 30px; }
.login-title { margin: 0 0 8px; font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.login-hint { margin: 0 0 28px; color: var(--muted); }

.field { display: block; margin-bottom: 16px; }
.field span { display: block; margin-bottom: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }
.field input {
  width: 100%;
  height: 50px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 17px;
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.form-error { margin: 0 0 16px; color: var(--danger); font-size: 15px; }

.btn-primary {
  width: 100%;
  height: 52px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 17px;
  font-weight: 650;
}
.btn-primary:disabled { opacity: .6; }

/* ---------- Список писем ---------- */

.search { flex: 1; margin: 0 4px; }
.search input {
  width: 100%;
  height: 40px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px;
  background: var(--surface);
  font-size: 16px;
}
.search input:focus { outline: 2px solid var(--accent); }

.msg-list { list-style: none; margin: 0; padding: 0 0 96px; }
.msg a {
  display: flex;
  gap: 12px;
  padding: 12px max(16px, env(safe-area-inset-right, 0px)) 12px max(16px, env(safe-area-inset-left, 0px));
  color: inherit;
  text-decoration: none;
}
.msg a:active { background: var(--accent-soft); }

.avatar {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: none;
  border-radius: 13px;
  background: hsl(var(--h, 210) 42% var(--av-l));
  color: hsl(var(--h, 210) 45% var(--av-ink-l));
  font-weight: 700;
  font-size: 17px;
}

.msg-main { flex: 1; min-width: 0; padding-top: 1px; }
.msg-top { display: flex; align-items: baseline; gap: 8px; }
.msg-from {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--muted);
}
.msg-date { flex: none; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.msg-subject {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  color: var(--muted);
}
.msg-subject span { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.msg-subject svg { width: 16px; height: 16px; color: var(--muted); }

.msg.unread .msg-from { color: var(--ink); font-weight: 700; }
.msg.unread .msg-subject { color: var(--ink); font-weight: 600; }
.msg.unread .msg-date { color: var(--accent); font-weight: 650; }
.msg.unread .avatar { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

.list-foot { padding: 8px 16px 120px; text-align: center; color: var(--muted); font-size: 14px; }

.empty { padding: 72px 32px; text-align: center; color: var(--muted); }
.empty strong { display: block; margin-bottom: 6px; font-size: 18px; color: var(--ink); }
.empty button { margin-top: 16px; padding: 10px 18px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }

.skel { display: flex; gap: 12px; padding: 14px 16px; }
.skel i { display: block; border-radius: 8px; background: var(--line); animation: pulse 1.2s ease-in-out infinite alternate; }
.skel .sa { width: 42px; height: 42px; border-radius: 13px; }
.skel .sb { flex: 1; display: flex; flex-direction: column; gap: 8px; padding-top: 4px; }
.skel .sb i:first-child { width: 45%; height: 12px; }
.skel .sb i:last-child { width: 80%; height: 12px; }
@keyframes pulse { from { opacity: .5; } to { opacity: 1; } }

.fab {
  position: absolute;
  right: max(16px, env(safe-area-inset-right, 0px));
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  display: flex;
  align-items: center;
  gap: 10px;
  height: 56px;
  padding: 0 22px 0 18px;
  border: 0;
  border-radius: 18px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 650;
  box-shadow: 0 6px 20px -6px rgba(15, 40, 70, .45);
}

/* ---------- Меню папок ---------- */

.scrim { position: fixed; inset: 0; z-index: 20; background: rgba(8, 14, 22, .42); }
.drawer {
  position: fixed;
  z-index: 21;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(320px, 86vw);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  transform: translateX(-102%);
  transition: transform .22s ease;
  padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}
.drawer.open { transform: none; }
@media (prefers-reduced-motion: reduce) { .drawer { transition: none; } }

.drawer-head { padding: 20px 20px 12px; }
.drawer-account { font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.drawer-account b { display: block; margin-bottom: 2px; font-size: 18px; color: var(--ink); }

.folders { flex: 1; overflow-y: auto; padding: 4px 8px; }
.folder {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  padding-left: calc(14px + var(--depth, 0) * 18px);
  border: 0;
  border-radius: 12px;
  background: transparent;
  text-align: left;
}
.folder span { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.folder em { font-style: normal; font-size: 14px; font-weight: 650; color: var(--accent); font-variant-numeric: tabular-nums; }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.folder-sep { height: 1px; margin: 8px 14px; background: var(--line); }

.drawer-foot { padding: 8px; border-top: 1px solid var(--line); }
.drawer-action {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  text-align: left;
}
.drawer-action:active, .folder:active { background: var(--accent-soft); }

/* ---------- Письмо ---------- */

.message { padding: 4px max(16px, env(safe-area-inset-right, 0px)) 24px max(16px, env(safe-area-inset-left, 0px)); }
.message h1 {
  margin: 4px 0 18px;
  font-size: 23px;
  line-height: 1.25;
  font-weight: 700;
  letter-spacing: -0.015em;
  overflow-wrap: anywhere;
}
.sender { display: flex; gap: 12px; align-items: flex-start; }
.sender-info { flex: 1; min-width: 0; }
.sender-name { font-weight: 650; overflow-wrap: anywhere; }
.sender-addr, .sender-meta { font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.sender-meta { margin-top: 4px; }

.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 0 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--accent-soft);
  font-size: 14px;
}
.notice span { flex: 1; }
.notice button { border: 0; background: transparent; color: var(--accent); font-weight: 650; padding: 6px 4px; }

.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.att {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: inherit;
  text-decoration: none;
  font-size: 14px;
}
.att span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.att small { color: var(--muted); flex: none; }
.att svg { width: 18px; height: 18px; color: var(--accent); }

.body-frame {
  display: block;
  width: 100%;
  min-height: 80px;
  margin-top: 18px;
  border: 0;
  border-radius: var(--radius);
  background: #fff;
}
.body-frame.plain { background: transparent; }

.actions {
  display: flex;
  gap: 8px;
  padding: 8px max(12px, env(safe-area-inset-right, 0px)) calc(8px + env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
  border-top: 1px solid var(--line);
  background: var(--bg);
  flex: none;
}
.action {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 4px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  font-size: 13px;
  color: var(--ink);
}
.action:active { background: var(--accent-soft); }
.action:disabled { opacity: .4; }

/* ---------- Новое письмо ---------- */

.send-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  margin-left: 4px;
  padding: 0 16px 0 12px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 650;
}
.send-btn svg { width: 18px; height: 18px; }
.send-btn:disabled { opacity: .55; }
label.icon-btn { cursor: pointer; }

.compose { display: flex; flex-direction: column; min-height: 100%; }
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
}
.row span { width: 64px; flex: none; color: var(--muted); font-size: 15px; }
.row input { flex: 1; min-width: 0; height: 50px; border: 0; background: transparent; font-size: 17px; }
.row input:focus { outline: none; }
.row-link {
  align-self: flex-start;
  margin: 4px 8px;
  padding: 8px;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 16px 0; }
.chips:empty { display: none; }
.chip {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 6px 6px 6px 12px;
  border-radius: 10px;
  background: var(--accent-soft);
  font-size: 14px;
}
.chip span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chip button { display: grid; place-items: center; width: 28px; height: 28px; border: 0; border-radius: 8px; background: transparent; }
.chip svg { width: 16px; height: 16px; }
.body {
  flex: 1;
  min-height: 50vh;
  width: 100%;
  padding: 16px;
  border: 0;
  background: transparent;
  font-size: 17px;
  line-height: 1.5;
  resize: none;
}
.body:focus { outline: none; }

/* ---------- Уведомления ---------- */

.toast {
  position: fixed;
  z-index: 60;
  left: 16px;
  right: 16px;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  max-width: 480px;
  margin: 0 auto;
  padding: 14px 16px;
  border-radius: 14px;
  background: var(--ink);
  color: var(--bg);
  font-size: 15px;
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, .5);
}
.toast.error { background: var(--danger); color: #fff; }

@media (min-width: 720px) {
  .msg-list, .message, .compose, .list-foot { max-width: 760px; margin-left: auto; margin-right: auto; }
}
