:root {
  --bg: #f4f7fa;
  --panel: #ffffff;
  --ink: #14202b;
  --muted: #5d6b78;
  --line: #dde4ea;
  --accent: #0b5f8a;
  --accent-ink: #ffffff;
  --hover: #eaf2f7;
  --unread: #0b5f8a;
  --danger: #b42318;
  --warn-bg: #fff4e5;
  --warn-ink: #8a4b00;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e151b;
    --panel: #151f27;
    --ink: #e6edf2;
    --muted: #93a3b0;
    --line: #26343f;
    --accent: #1f9bd1;
    --accent-ink: #04121a;
    --hover: #1c2a35;
    --unread: #4cc3f0;
    --danger: #ff7b6e;
    --warn-bg: #3a2a12;
    --warn-ink: #ffcf8a;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px; }
button:hover { background: var(--hover); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:disabled { opacity: .6; cursor: default; }
button.danger { color: var(--danger); }
input, textarea { width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 9px 11px; }
textarea { resize: vertical; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0 0; }
.icon-btn { border: none; background: none; font-size: 20px; padding: 4px 8px; }

.screen { height: 100%; }
#login { display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; text-align: center; display: grid; gap: 12px; }
.login-card img { margin: 0 auto; border-radius: 16px; }
.login-card h1 { margin: 0; font-size: 24px; }

#app { display: flex; flex-direction: column; }
.top { display: flex; align-items: center; gap: 10px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--line); }
.top .logo { border-radius: 7px; }
.top .brand { font-size: 17px; }
.top input[type=search] { flex: 1; max-width: 520px; margin-left: auto; }
#menu-btn { display: none; }
.body { flex: 1; display: grid; grid-template-columns: 190px minmax(280px, 380px) 1fr; min-height: 0; }
.folders { display: flex; flex-direction: column; gap: 2px; padding: 12px 8px; border-right: 1px solid var(--line); overflow-y: auto; }
.folders button { text-align: left; border: none; background: none; display: flex; justify-content: space-between; }
.folders button.active { background: var(--hover); font-weight: 600; }
#from-line { overflow-wrap: anywhere; padding: 0 12px; }
.folders hr { border: none; border-top: 1px solid var(--line); width: 100%; }
.count { color: var(--unread); font-weight: 700; }
.list { overflow-y: auto; border-right: 1px solid var(--line); background: var(--panel); }
.row { display: block; width: 100%; text-align: left; border: none; border-bottom: 1px solid var(--line); border-radius: 0; padding: 11px 14px; background: none; }
.row.selected { background: var(--hover); }
.row .r1 { display: flex; gap: 8px; justify-content: space-between; }
.row .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.row .subj, .row .snip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .snip { color: var(--muted); font-size: 13px; }
.row.unread .who, .row.unread .subj { font-weight: 700; }
.row.unread .who::before { content: "\25CF  "; color: var(--unread); font-size: 10px; vertical-align: middle; }
.list .more { margin: 12px auto; display: block; }
.list .empty, .reader .empty { padding: 24px; text-align: center; }

.reader { overflow-y: auto; padding: 16px 20px 40px; }
.reader h2 { margin: 4px 0 12px; font-size: 20px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
#back-btn { display: none; }
.msg { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 12px; overflow: hidden; }
.msg.out { border-left: 4px solid var(--accent); }
.msg-head { padding: 12px 14px; display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.msg-head .addr { font-size: 13px; color: var(--muted); word-break: break-all; }
.msg-body { padding: 12px 14px; }
.msg-body pre { white-space: pre-wrap; word-wrap: break-word; font: inherit; margin: 0; }
.msg-body iframe { width: 100%; border: none; min-height: 120px; background: #fff; border-radius: 6px; }
.msg-tools { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 12px; }
.warn { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 12px; border-radius: 8px; margin: 10px 14px 0; font-size: 13px; }
.attach { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 12px; }
.attach a { border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; text-decoration: none; color: inherit; font-size: 13px; }

dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(680px, calc(100% - 24px)); background: var(--panel); color: var(--ink); }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog form { display: grid; gap: 10px; padding: 16px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.dlg-head, .dlg-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.dlg-foot { justify-content: flex-end; }
.files { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.file-btn { border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; color: var(--ink); }

#toast { position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 10; max-width: calc(100% - 32px); }

@media (max-width: 860px) {
  .top { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
  .top .brand { display: none; }
  #menu-btn { display: block; }
  .body { grid-template-columns: 1fr; }
  .folders { position: fixed; inset: 0 auto 0 0; width: 240px; z-index: 5; background: var(--panel); padding-top: max(16px, env(safe-area-inset-top)); transform: translateX(-100%); transition: transform .2s; }
  body.menu-open .folders { transform: none; box-shadow: 0 0 30px rgba(0,0,0,.25); }
  .list { border-right: none; }
  .reader { display: none; padding: 12px 16px 40px; }
  body.reading .list { display: none; }
  body.reading .reader { display: block; }
  #back-btn { display: inline-block; }
}
