:root {
  --bg: #f4f5f7; --pane: #ffffff; --side: #eceef2; --line: #e3e6ec; --text: #1c1f26; --muted: #6b7280; --faint: #9aa1ad;
  --hover: rgba(20,30,50,.05); --active: #e3ebfb; --accent: #3b6fe0; --accent-ink: #fff; --unread: #3b6fe0;
  --star: #e5a50a; --danger: #d14343; --ok: #2f9e6a; --shadow: 0 10px 40px rgba(15,20,30,.2);
  --archive: #2f9e6a; --readc: #3b6fe0;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #16181d; --pane: #1f2229; --side: #1b1e24; --line: #2c3039; --text: #e7e9ee; --muted: #9aa1ad; --faint: #6c7380;
  --hover: rgba(255,255,255,.05); --active: #26324a; --accent: #6b9bff; --accent-ink: #0d1424; --unread: #6b9bff;
  --star: #f2b531; --shadow: 0 10px 40px rgba(0,0,0,.55); color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font: 16px/1.45 "Inter", "Roboto", system-ui, sans-serif; overscroll-behavior-y: contain; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, select, textarea { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; flex: none; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

#root { height: 100dvh; display: flex; flex-direction: column; }
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }

/* ---------- верхня панель ---------- */
.topbar {
  display: flex; align-items: center; gap: 4px; padding: calc(6px + var(--safe-t)) 6px 6px; background: var(--side);
  border-bottom: 1px solid var(--line); min-height: 56px; position: relative; z-index: 2;
}
.topbar h1 { flex: 1; font-size: 19px; margin: 0 4px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar h1 small { display: block; font-size: 12.5px; font-weight: 450; color: var(--muted); }
.ib { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: var(--muted); flex: none; }
.ib:active { background: var(--hover); }
.ib.on { color: var(--star); }
.topbar .search-input { flex: 1; border: 0; background: var(--pane); border-radius: 22px; padding: 10px 16px; outline: none; }

/* ---------- список ---------- */
.list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: calc(90px + var(--safe-b)); }
.filters { display: flex; gap: 8px; padding: 10px 14px 6px; overflow-x: auto; }
.chip-f { padding: 6px 14px; border-radius: 999px; border: 1px solid var(--line); font-size: 14px; color: var(--muted); white-space: nowrap; background: var(--pane); }
.chip-f.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.row-wrap { position: relative; overflow: hidden; border-bottom: 1px solid var(--line); }
.row-bg { position: absolute; inset: 0; display: flex; align-items: center; justify-content: space-between; padding: 0 22px; color: #fff; font-weight: 600; font-size: 14px; }
.row-bg .l, .row-bg .r { display: flex; align-items: center; gap: 8px; opacity: .25; transition: opacity .1s; }
.row-bg.go-l .l, .row-bg.go-r .r { opacity: 1; }
.row-bg.right { background: var(--readc); }
.row-bg.left { background: var(--archive); }
.row-bg.left.del { background: var(--danger); }
.row {
  position: relative; display: grid; grid-template-columns: 44px 1fr auto; column-gap: 12px; padding: 12px 14px;
  background: var(--bg); touch-action: pan-y; user-select: none;
}
.row.sel { background: var(--active); }
.av { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 650; font-size: 16px; grid-row: 1 / 4; position: relative; }
.av .accdot { position: absolute; right: -2px; bottom: -2px; width: 14px; height: 14px; border-radius: 50%; border: 2.5px solid var(--bg); }
.row.sel .av .accdot { border-color: var(--active); }
.av.check { background: var(--accent) !important; }
.row .from { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .date { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 4px; white-space: nowrap; }
.row .date svg { width: 15px; height: 15px; }
.row .subj { grid-column: 2 / 4; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.row .snip { grid-column: 2 / 4; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row.unread .from, .row.unread .subj { font-weight: 700; }
.row.unread .date { color: var(--accent); font-weight: 600; }
.row .star { color: var(--star); }
.list-end { padding: 24px; text-align: center; color: var(--muted); font-size: 14px; }
.empty { padding: 60px 24px; text-align: center; color: var(--muted); }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-b)); height: 56px; padding: 0 22px 0 18px; border-radius: 18px;
  background: var(--accent); color: var(--accent-ink); display: flex; align-items: center; gap: 10px; font-weight: 650;
  box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 5;
}
.selbar {
  position: fixed; left: 0; right: 0; bottom: 0; padding: 8px 8px calc(8px + var(--safe-b)); background: var(--side);
  border-top: 1px solid var(--line); display: flex; justify-content: space-around; z-index: 6;
}
.selbar button { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11.5px; color: var(--muted); padding: 4px 8px; min-width: 60px; }

/* ---------- бічне меню ---------- */
.drawer-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 20; animation: fade .15s; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(330px, 86vw); background: var(--side); z-index: 21; overflow-y: auto;
  padding: calc(14px + var(--safe-t)) 10px calc(14px + var(--safe-b)); animation: slide .18s ease-out;
}
@keyframes slide { from { transform: translateX(-100%); } }
@keyframes fade { from { opacity: 0; } }
.drawer .brand { display: flex; align-items: center; gap: 10px; padding: 6px 10px 14px; font-weight: 700; font-size: 18px; }
.drawer .brand img { width: 30px; height: 30px; }
.dr-item { width: 100%; display: flex; align-items: center; gap: 14px; padding: 11px 12px; border-radius: 12px; text-align: left; }
.dr-item.on { background: var(--active); font-weight: 650; }
.dr-item span { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dr-item .n { flex: none; font-size: 13.5px; color: var(--muted); font-weight: 600; }
.dr-item svg { color: var(--muted); }
.dr-sep { height: 1px; background: var(--line); margin: 8px 6px; }
.dr-acc { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 12px; width: 100%; text-align: left; }
.dr-acc .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.dr-acc .nm { flex: 1; min-width: 0; }
.dr-acc .nm b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.dr-acc .nm small { display: block; color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dr-acc .chev svg { width: 18px; height: 18px; transition: transform .15s; }
.dr-acc.open .chev svg { transform: rotate(90deg); }
.badge { min-width: 24px; padding: 1px 8px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: 12.5px; font-weight: 700; text-align: center; }
.dr-folders { padding-left: 22px; }
.state-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.state-dot.error { background: var(--danger); }

/* ---------- лист ---------- */
.msg { flex: 1; overflow-y: auto; }
.msg-head { padding: 16px 16px 12px; background: var(--pane); border-bottom: 1px solid var(--line); }
.msg-head h2 { margin: 0 0 12px; font-size: 20px; line-height: 1.3; overflow-wrap: anywhere; }
.who { display: grid; grid-template-columns: 40px 1fr; gap: 12px; align-items: center; }
.who .av { width: 40px; height: 40px; grid-row: auto; }
.who b { display: block; }
.who small { display: block; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.banner { margin: 10px 12px 0; padding: 10px 12px; border-radius: 10px; background: var(--pane); border: 1px solid var(--line); font-size: 14px; }
.banner button { color: var(--accent); font-weight: 600; margin-top: 6px; margin-right: 14px; }
.banner.err { color: var(--danger); border-color: var(--danger); }
.atts { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 12px 0; }
.att { display: inline-flex; align-items: center; gap: 8px; padding: 9px 12px; border-radius: 10px; background: var(--pane); border: 1px solid var(--line); font-size: 14px; max-width: 100%; color: var(--text); text-decoration: none; }
.att span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.att small { color: var(--muted); }
.att svg { width: 18px; height: 18px; }
.body { padding: 12px; }
.body iframe { display: block; width: 100%; border: 0; min-height: 120px; border-radius: 12px; background: #fff; }
:root[data-theme="dark"] .body iframe { background: var(--pane); }
.msgbar {
  display: flex; justify-content: space-around; padding: 6px 4px calc(6px + var(--safe-b)); background: var(--side); border-top: 1px solid var(--line);
}
.msgbar button { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11.5px; color: var(--muted); padding: 4px 6px; min-width: 56px; }

/* ---------- написання ---------- */
.cmp { flex: 1; overflow-y: auto; background: var(--pane); display: flex; flex-direction: column; }
.cmp-row { display: flex; align-items: flex-start; gap: 10px; padding: 8px 14px; border-bottom: 1px solid var(--line); min-height: 50px; }
.cmp-row > label { width: 54px; color: var(--muted); font-size: 14px; padding-top: 7px; flex: none; }
.cmp-row select, .cmp-row input.plain { flex: 1; border: 0; background: none; outline: none; padding: 6px 0; min-width: 0; }
.cmp-row input.subj { font-weight: 600; }
.chips { flex: 1; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-width: 0; position: relative; }
.chips input { flex: 1; min-width: 120px; border: 0; background: none; outline: none; padding: 6px 0; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 4px 6px 4px 12px; border-radius: 999px; background: var(--active); font-size: 14px; max-width: 100%; }
.chip.bad { color: var(--danger); background: rgba(209,67,67,.15); }
.chip button { width: 22px; height: 22px; display: grid; place-items: center; color: var(--muted); }
.chip button svg { width: 13px; height: 13px; }
.ac { position: absolute; left: 0; right: 0; top: 100%; background: var(--pane); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); z-index: 10; overflow: hidden; }
.ac button { display: block; width: 100%; text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.ac button small { display: block; color: var(--muted); font-size: 13px; }
.cmp-tools { display: flex; gap: 2px; padding: 4px 8px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.cmp-tools button { height: 40px; min-width: 40px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; color: var(--muted); padding: 0 8px; font-size: 14px; white-space: nowrap; }
.cmp-tools svg { width: 19px; height: 19px; }
.editor { flex: 1; min-height: 220px; padding: 14px 16px; outline: none; overflow-wrap: anywhere; }
.editor blockquote { margin: 6px 0 6px .8ex; border-left: 2px solid var(--line); padding-left: 1ex; color: var(--muted); }
.editor img { max-width: 100%; height: auto; }
.editor .lyst-quote { color: var(--muted); font-size: 14px; }
.editor.dark .lyst-quote { filter: invert(1) hue-rotate(180deg); color: #3a3f4a; }
.editor.dark .lyst-quote img { filter: invert(1) hue-rotate(180deg); }
.cmp-atts { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 14px; }
.cmp-atts:empty { display: none; }
.cmp-banner { margin: 10px 14px 0; padding: 10px 12px; border-radius: 10px; background: var(--active); font-size: 14px; }
.cmp-status { font-size: 13px; color: var(--muted); padding: 4px 14px; min-height: 22px; }
.cmp-status.err { color: var(--danger); }
.send-btn { height: 40px; padding: 0 16px; border-radius: 20px; background: var(--accent); color: var(--accent-ink); font-weight: 650; display: flex; align-items: center; gap: 8px; }
.send-btn svg { width: 18px; height: 18px; }

/* ---------- нижні листи (sheets) ---------- */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; animation: fade .15s; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; background: var(--pane); border-radius: 20px 20px 0 0; z-index: 31;
  padding: 10px 0 calc(14px + var(--safe-b)); max-height: 85dvh; overflow-y: auto; animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(100%); } }
.sheet .grab { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 8px; }
.sheet h3 { margin: 4px 18px 10px; font-size: 17px; }
.sheet .opt { width: 100%; display: flex; align-items: center; gap: 14px; padding: 13px 18px; text-align: left; }
.sheet .opt:active { background: var(--hover); }
.sheet .opt span { flex: 1; }
.sheet .opt small { color: var(--muted); font-size: 13.5px; }
.sheet .opt svg { color: var(--muted); }
.sheet .opt.danger { color: var(--danger); }
.sheet .field { padding: 6px 18px; }
.sheet .sep { height: 1px; background: var(--line); margin: 6px 0; }
.preview { padding: 2px 18px 6px; font-size: 14px; color: var(--ok); min-height: 22px; }
.preview.err { color: var(--danger); }
.note { padding: 4px 18px 8px; font-size: 12.5px; color: var(--faint); }

/* ---------- форми ---------- */
.page { flex: 1; overflow-y: auto; padding: 16px 16px calc(30px + var(--safe-b)); }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13.5px; color: var(--muted); margin-bottom: 6px; }
.field input, .field select, .field textarea, .inp {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--pane); outline: none;
}
.field textarea { min-height: 110px; resize: vertical; }
.field input:focus, .field textarea:focus, .inp:focus { border-color: var(--accent); }
.btn { padding: 12px 18px; border-radius: 12px; border: 1px solid var(--line); background: var(--pane); font-weight: 600; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn[disabled] { opacity: .55; }
.form-err { color: var(--danger); font-size: 14px; margin: 4px 0 10px; }
.card { background: var(--pane); border: 1px solid var(--line); border-radius: 14px; margin-bottom: 14px; overflow: hidden; }
.card h4 { margin: 0; padding: 12px 14px 4px; font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.card .item { display: flex; align-items: center; gap: 12px; padding: 13px 14px; border-top: 1px solid var(--line); width: 100%; text-align: left; }
.card .item:first-of-type { border-top: 0; }
.card .item span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.card .item small { display: block; color: var(--muted); font-size: 13px; }
.card .item .dot { width: 12px; height: 12px; border-radius: 50%; }
.steps { margin: 0 0 16px; padding: 12px 14px 12px 32px; background: var(--pane); border: 1px solid var(--line); border-radius: 12px; font-size: 14.5px; }
.steps a { color: var(--accent); }
.colors { display: flex; gap: 10px; flex-wrap: wrap; }
.colors button { width: 32px; height: 32px; border-radius: 50%; }
.colors button.on { outline: 3px solid var(--text); outline-offset: 2px; }

.login { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 30px 24px; max-width: 420px; margin: 0 auto; width: 100%; }
.login img { width: 84px; height: 84px; margin: 0 auto 18px; display: block; }
.login h1 { text-align: center; margin: 0 0 6px; }
.login p { text-align: center; color: var(--muted); margin: 0 0 24px; }

#toast {
  position: fixed; left: 12px; right: 12px; bottom: calc(86px + var(--safe-b)); background: #23272f; color: #f1f3f6; padding: 13px 16px;
  border-radius: 12px; box-shadow: var(--shadow); font-size: 14.5px; z-index: 50; display: flex; align-items: center; gap: 14px;
}
#toast span { flex: 1; }
#toast button { color: #8fb4ff; font-weight: 700; }
.spin { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
