:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f9fafb;
  --text: #1c2230;
  --muted: #5d6677;
  --border: #e1e4ea;
  --accent: #2456d6;
  --accent-text: #ffffff;
  --accent-soft: #e8eefc;
  --bad: #c2312f;
  --star: #d48806;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131a;
    --panel: #161b24;
    --panel-2: #1b212c;
    --text: #e6e9ef;
    --muted: #9aa3b2;
    --border: #2a3140;
    --accent: #6e9bff;
    --accent-text: #0b1020;
    --accent-soft: #1c2a4a;
    --bad: #ff7b72;
    --star: #f0b44c;
    --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.05rem; margin: 0; }
.muted { color: var(--muted); }
.error { color: var(--bad); margin: 0; }

.brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; font-size: 1.05rem; }
.brand-lg { font-size: 1.4rem; margin-bottom: 1rem; }
.logo { width: 24px; height: 24px; border-radius: 7px; background: var(--accent); flex: none; position: relative; }
.logo::after { content: ""; position: absolute; inset: 7px 5px; border: 2px solid var(--accent-text); border-radius: 2px; }

.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(400px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 2rem; box-shadow: var(--shadow); display: grid; gap: .75rem; }
label { display: grid; gap: .3rem; font-size: .9rem; font-weight: 550; }
input, textarea, select {
  font: inherit; font-weight: 400; color: var(--text); background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: .5rem .7rem; width: 100%;
}
textarea { resize: vertical; line-height: 1.5; }
.btn { font: inherit; font-weight: 550; border: 1px solid var(--border); background: var(--panel); color: var(--text); padding: .45rem .85rem; border-radius: 8px; cursor: pointer; line-height: 1.2; }
.btn:hover { background: var(--panel-2); }
.btn:focus-visible, input:focus-visible, textarea:focus-visible, .msg:focus-visible, .folder:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.btn.block { width: 100%; padding: .6rem; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.mail {
  height: 100vh; display: grid;
  grid-template-columns: 220px minmax(300px, 400px) 1fr;
  grid-template-rows: 56px 1fr;
  grid-template-areas: "top top top" "folders list reader";
}
.topbar { grid-area: top; display: flex; align-items: center; gap: 1rem; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.search { flex: 1; max-width: 560px; }
.search input { background: var(--panel-2); }
.user { margin-left: auto; display: flex; align-items: center; gap: .5rem; font-size: .9rem; white-space: nowrap; }
.folders { grid-area: folders; padding: 12px; border-right: 1px solid var(--border); background: var(--panel); overflow-y: auto; display: grid; align-content: start; gap: 12px; }
.folder { display: flex; justify-content: space-between; align-items: center; width: 100%; text-align: left; background: none; border: 0; color: var(--text); font: inherit; padding: .45rem .7rem; border-radius: 8px; cursor: pointer; }
.folder:hover { background: var(--panel-2); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.count { font-size: .8rem; font-weight: 700; background: var(--accent); color: var(--accent-text); border-radius: 99px; padding: 0 .45rem; min-width: 1.4rem; text-align: center; }

.list { grid-area: list; display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--panel); }
.list-head { display: flex; justify-content: space-between; align-items: baseline; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.messages { overflow-y: auto; flex: 1; }
.list-foot { padding: 8px; text-align: center; }
.msg { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer; }
.msg:hover { background: var(--panel-2); }
.msg.selected { background: var(--accent-soft); }
.msg .from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .date { font-size: .8rem; color: var(--muted); white-space: nowrap; }
.msg .subject { grid-column: 1 / -1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .92rem; }
.msg .preview { grid-column: 1 / -1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; color: var(--muted); }
.msg.unread .from, .msg.unread .subject { font-weight: 700; }
.msg.unread { box-shadow: inset 3px 0 0 var(--accent); }
.star { color: var(--star); }
.empty { padding: 3rem 1rem; text-align: center; color: var(--muted); }

.reader { grid-area: reader; overflow-y: auto; background: var(--bg); min-width: 0; }
.reader-empty { height: 100%; display: grid; place-items: center; }
.reader-inner { max-width: 900px; margin: 0 auto; padding: 20px clamp(16px, 3vw, 32px) 40px; }
.toolbar { display: flex; flex-wrap: wrap; gap: .35rem; margin-bottom: 16px; align-items: center; }
.toolbar .back { display: none; }
.mail-head { background: var(--panel); border: 1px solid var(--border); border-radius: 12px 12px 0 0; padding: 16px 20px; }
.mail-head h1 { font-size: 1.3rem; margin-bottom: .5rem; overflow-wrap: anywhere; }
.meta { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; font-size: .9rem; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; overflow-wrap: anywhere; }
.mail-body { background: var(--panel); border: 1px solid var(--border); border-top: 0; border-radius: 0 0 12px 12px; padding: 20px; }
.mail-body pre { white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; margin: 0; }
.mail-body iframe { width: 100%; border: 0; min-height: 200px; background: #fff; border-radius: 6px; }
.attachments { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.attachment { display: inline-flex; gap: .4rem; align-items: center; }
.note { font-size: .85rem; color: var(--muted); margin: 0 0 12px; }

.composer { border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); width: min(760px, calc(100% - 32px)); box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
.composer::backdrop, #mover::backdrop { background: rgba(10, 14, 22, .45); }
.composer form { padding: 1rem 1.25rem 1.25rem; display: grid; gap: .5rem; }
.composer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .25rem; }
.composer .row { grid-template-columns: 64px 1fr; align-items: center; font-weight: 500; }
.composer-actions { display: flex; justify-content: flex-end; gap: .5rem; }
#mover { border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); width: min(360px, calc(100% - 32px)); }
#mover form { padding: 1.25rem; display: grid; gap: .5rem; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: .5rem; z-index: 10; }
.toast { background: var(--text); color: var(--bg); padding: .65rem 1rem; border-radius: 8px; max-width: 360px; font-size: .9rem; }
.toast.bad { background: var(--bad); color: #fff; }

@media (max-width: 1000px) {
  .mail { grid-template-columns: 180px 1fr; grid-template-areas: "top top" "folders list"; }
  .mail.reading { grid-template-areas: "top top" "folders reader"; }
  .mail.reading .list { display: none; }
  .mail:not(.reading) .reader { display: none; }
  .toolbar .back { display: inline-block; }
}
@media (max-width: 640px) {
  .mail { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto 1fr; grid-template-areas: "top" "folders" "list"; height: auto; min-height: 100vh; }
  .mail.reading { grid-template-areas: "top" "reader"; }
  .mail.reading .folders { display: none; }
  .topbar { flex-wrap: wrap; padding: 8px 16px; gap: .5rem; }
  .search { order: 3; max-width: none; flex-basis: 100%; }
  .user #me { display: none; }
  .folders { border-right: 0; border-bottom: 1px solid var(--border); display: flex; gap: 6px; overflow-x: auto; padding: 8px 16px; }
  .folders #compose { width: auto; flex: none; }
  #folder-list { display: flex; gap: 4px; }
  .folder { white-space: nowrap; width: auto; }
  .list { border-right: 0; }
  .composer .row { grid-template-columns: 1fr; }
}
