:root {
  --bg: #f3f4f7;
  --panel: #ffffff;
  --panel-2: #f8f9fb;
  --text: #1c2230;
  --muted: #687082;
  --border: #e3e6ec;
  --hover: #f0f2f6;
  --accent: #2459d6;
  --accent-text: #ffffff;
  --accent-soft: #e7eeff;
  --danger: #c4322b;
  --star: #e3a008;
  --shadow: 0 10px 30px rgba(20, 30, 60, .14);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1015;
    --panel: #161920;
    --panel-2: #1b1f27;
    --text: #e5e8ee;
    --muted: #949cab;
    --border: #282d37;
    --hover: #20242d;
    --accent: #6d98ff;
    --accent-text: #0b1020;
    --accent-soft: #1d2a47;
    --danger: #ff7a70;
    --star: #f5c04a;
    --shadow: 0 10px 30px rgba(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, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.ico { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[data-icon] { display: inline-flex; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.block { width: 100%; padding: 10px; }
.btn:disabled { opacity: .6; cursor: default; }
.btn.danger { color: var(--danger); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border: 0; border-radius: 8px; background: transparent;
  color: var(--muted); cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn:disabled { opacity: .35; cursor: default; background: transparent; }
.icon-btn.on { color: var(--star); }

input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], select, textarea {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; outline: none; min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.login-card .muted { margin: -6px 0 4px; }
.stack { display: flex; flex-direction: column; gap: 6px; font-weight: 500; }
.stack input { font-weight: 400; }
details { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; }
details[open] { display: flex; flex-direction: column; gap: 12px; padding-bottom: 14px; }
summary { cursor: pointer; color: var(--muted); }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 500; margin-bottom: 6px; }
.server-row { display: grid; grid-template-columns: 1fr 76px 104px; gap: 6px; }
.error { color: var(--danger); margin: 0; background: color-mix(in srgb, var(--danger) 10%, transparent); padding: 8px 10px; border-radius: 8px; }
.hint { font-size: 12px; color: var(--muted); margin: 0; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: var(--accent); }
.brand span:last-child { color: var(--text); }
.brand.big { font-size: 22px; }
.brand.big .ico { width: 26px; height: 26px; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 236px minmax(300px, 380px) 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--panel-2); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 16px 10px; gap: 14px; min-height: 0; }
.sidebar .brand { padding: 0 8px; }
.compose-btn { margin: 0 4px; }
.folders { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.folder {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 7px 10px; border: 0; border-radius: 8px;
  background: transparent; cursor: pointer; text-align: left; color: var(--text);
}
.folder:hover { background: var(--hover); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folder .ico { color: var(--muted); }
.folder.active .ico { color: var(--accent); }
.folder .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder .count { font-size: 12px; font-weight: 600; color: var(--accent); }
.folder.group { cursor: default; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; padding-top: 12px; }
.folder.group:hover { background: transparent; }
.account { display: flex; align-items: center; gap: 6px; padding: 8px 6px 0; border-top: 1px solid var(--border); }
.account-email { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 13px; }
.scrim { display: none; }
.mobile-only { display: none; }

/* ---------- list ---------- */
.list-pane { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.list-toolbar { display: flex; align-items: center; gap: 6px; padding: 12px 12px 8px; }
.search { flex: 1; display: flex; align-items: center; gap: 8px; background: var(--hover); border-radius: 8px; padding: 0 10px; color: var(--muted); }
.search input { flex: 1; border: 0; background: transparent; padding: 8px 0; box-shadow: none !important; }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 4px 14px 10px; border-bottom: 1px solid var(--border); }
.list-head h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-head select { padding: 4px 8px; font-size: 13px; }

.messages { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.msg { display: grid; grid-template-columns: 10px 1fr auto; gap: 2px 8px; padding: 10px 14px 10px 10px; border-bottom: 1px solid var(--border); cursor: pointer; position: relative; }
.msg:hover { background: var(--hover); }
.msg.active { background: var(--accent-soft); }
.msg .dot { grid-row: 1 / span 2; width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; }
.msg.unread .dot { background: var(--accent); }
.msg .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .when { font-size: 12px; color: var(--muted); white-space: nowrap; }
.msg .subj { grid-column: 2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.msg .marks { grid-column: 3; display: flex; gap: 4px; justify-content: flex-end; color: var(--muted); }
.msg .marks .ico { width: 14px; height: 14px; }
.msg .marks .star { color: var(--star); fill: var(--star); }
.msg.unread .who, .msg.unread .subj { font-weight: 650; color: var(--text); }
.list-empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.pager { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px 6px 14px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; }

/* ---------- reader ---------- */
.reader { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--panel); }
.reader-empty { margin: auto; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 10px; }
.reader-empty .ico { width: 48px; height: 48px; stroke-width: 1.3; opacity: .5; }
.reader-toolbar { display: flex; align-items: center; gap: 2px; padding: 8px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.reader-toolbar .sep { flex: 1; }
.reader-toolbar select { padding: 5px 8px; font-size: 13px; max-width: 180px; }
.reader-head { padding: 16px 20px 12px; border-bottom: 1px solid var(--border); }
.reader-head h1 { font-size: 20px; margin: 0 0 12px; line-height: 1.3; overflow-wrap: anywhere; }
.sender { display: flex; gap: 12px; align-items: flex-start; }
.avatar { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; flex: none; }
.sender-info { flex: 1; min-width: 0; }
.sender-line { display: flex; gap: 8px; flex-wrap: wrap; align-items: baseline; }
.sender-line strong { overflow-wrap: anywhere; }
.recips { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.date { color: var(--muted); font-size: 13px; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.reader-head .chips { margin-top: 12px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel-2); text-decoration: none; color: var(--text); font-size: 13px; max-width: 100%;
}
a.chip:hover { border-color: var(--accent); }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 240px; }
.chip .size { color: var(--muted); font-size: 12px; }
.chip .ico { width: 15px; height: 15px; }
.chip button { border: 0; background: none; padding: 0; cursor: pointer; color: var(--muted); display: inline-flex; }
.chip input { margin: 0; }
.banner { display: flex; align-items: center; gap: 10px; padding: 8px 20px; background: var(--panel-2); border-bottom: 1px solid var(--border); font-size: 13px; color: var(--muted); }
.banner button { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-weight: 500; }
.reader-body { flex: 1; min-height: 0; display: flex; }
.reader-body iframe { flex: 1; border: 0; width: 100%; background: #fff; }
.plain { flex: 1; overflow: auto; padding: 18px 20px; white-space: pre-wrap; overflow-wrap: anywhere; font-family: inherit; margin: 0; }
.loading { margin: auto; color: var(--muted); }

/* ---------- compose ---------- */
.modal { position: fixed; inset: 0; background: rgba(10, 14, 25, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.compose-card {
  width: 100%; max-width: 760px; height: min(720px, 100%); background: var(--panel); border-radius: 14px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--border);
}
.compose-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--border); }
.compose-head h3 { margin: 0; font-size: 16px; }
.compose-fields { display: flex; flex-direction: column; }
.field { display: flex; align-items: center; border-bottom: 1px solid var(--border); padding: 0 18px; }
.field span { width: 56px; color: var(--muted); flex: none; }
.field input { flex: 1; border: 0 !important; box-shadow: none !important; padding: 10px 0; background: transparent; }
#cBody { flex: 1; border: 0; border-radius: 0; padding: 14px 18px; resize: none; box-shadow: none !important; line-height: 1.5; }
#cAttachments { padding: 0 18px; }
#cAttachments:not(:empty) { padding: 10px 18px; border-top: 1px solid var(--border); }
.compose-foot { display: flex; align-items: center; gap: 8px; padding: 10px 18px; border-top: 1px solid var(--border); }

.settings-card {
  width: 100%; max-width: 480px; background: var(--panel); border-radius: 14px; box-shadow: var(--shadow);
  border: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden;
}
.settings-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.settings-body textarea { resize: vertical; font-weight: 400; }

.suggestions {
  position: fixed; z-index: 60; list-style: none; margin: 0; padding: 4px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); max-height: 280px; overflow-y: auto;
}
.suggestions li { display: flex; flex-direction: column; padding: 6px 10px; border-radius: 6px; cursor: pointer; overflow: hidden; }
.suggestions li span, .suggestions li strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggestions li.active, .suggestions li:hover { background: var(--accent-soft); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel);
  padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); z-index: 100; max-width: calc(100% - 32px);
}
.toast.err { background: var(--danger); color: #fff; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .app { grid-template-columns: 210px minmax(260px, 320px) 1fr; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .mobile-only { display: inline-flex; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 270px; max-width: 85vw; z-index: 30;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  body.show-sidebar .sidebar { transform: none; box-shadow: var(--shadow); }
  body.show-sidebar .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
  .list-pane { border-right: 0; }
  .reader { position: fixed; inset: 0; z-index: 10; transform: translateX(100%); transition: transform .2s ease; }
  body.show-reader .reader { transform: none; }
  .list-toolbar { padding: 10px 16px 8px 8px; }
  .reader-head { padding: 14px 16px 10px; }
  .plain { padding: 16px; }
  .modal { padding: 0; }
  .compose-card { height: 100%; max-width: none; border-radius: 0; border: 0; }
  .field { padding: 0 16px; }
  .server-row { grid-template-columns: 1fr 70px; }
  .server-row select { grid-column: 1 / -1; }
}
@media (min-width: 821px) {
  .back-btn { display: none !important; }
}
