:root {
  --bg: #f6f8f7;
  --surface: #ffffff;
  --surface-2: #f1f4f2;
  --hover: #eef3f0;
  --selected: #e3f1e8;
  --text: #13201a;
  --text-2: #4b5a52;
  --muted: #7a877f;
  --border: #e3e8e5;
  --accent: #15803d;
  --accent-hover: #166534;
  --accent-soft: #dcf2e3;
  --accent-text: #ffffff;
  --danger: #b42318;
  --warn: #b54708;
  --shadow: 0 1px 2px rgba(16, 24, 20, .06), 0 8px 24px rgba(16, 24, 20, .08);
  --shadow-lg: 0 12px 48px rgba(16, 24, 20, .22);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1210;
    --surface: #141a17;
    --surface-2: #1a221e;
    --hover: #1d2722;
    --selected: #1c3326;
    --text: #e6ece8;
    --text-2: #b4c0b9;
    --muted: #84918a;
    --border: #252f2a;
    --accent: #22a352;
    --accent-hover: #2bb85f;
    --accent-soft: #173323;
    --danger: #f97066;
    --warn: #fdb022;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
    --shadow-lg: 0 12px 48px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button, input, textarea { font: inherit; color: inherit; }
h1, h2 { margin: 0; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 13px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid transparent; border-radius: 999px; padding: 8px 16px;
  font-weight: 600; cursor: pointer; text-decoration: none; transition: background .15s, box-shadow .15s;
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: .6; cursor: progress; }
.btn-dark { background: #1f2328; color: #fff; }
.btn-dark:hover { background: #32383f; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text-2); }
.btn-ghost:hover { background: var(--hover); color: var(--text); }
.btn-lg { padding: 12px 22px; font-size: 15px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 999px;
  border: 0; background: transparent; color: var(--text-2); cursor: pointer;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.link { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 13px; padding: 0 4px; }
.link:hover { color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px;
  background: radial-gradient(1200px 600px at 50% -10%, var(--accent-soft), transparent 60%), var(--bg); }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow); padding: 40px 32px; text-align: center;
  display: grid; gap: 12px; justify-items: center;
}
.login-card h1 { font-size: 24px; letter-spacing: -.02em; }
.login-card .muted { margin: 0 0 12px; }
.login-card .btn { width: 100%; }
.login-error { margin: 4px 0 0; color: var(--danger); font-size: 13px; }
.fine { margin: 8px 0 0; color: var(--muted); font-size: 12px; }
.brand-mark { width: 52px; height: 52px; border-radius: 14px; background: var(--accent); color: #fff; display: grid; place-items: center; margin-bottom: 4px; }
.brand-mark svg { width: 28px; height: 28px; }
.brand-mark.sm { width: 30px; height: 30px; border-radius: 8px; margin: 0; }
.brand-mark.sm svg { width: 18px; height: 18px; }

/* Layout */
.app { height: 100%; display: grid; grid-template-columns: 232px minmax(300px, 400px) 1fr; }
.sidebar { display: flex; flex-direction: column; gap: 16px; padding: 16px 12px; background: var(--bg); min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; padding: 4px 8px; letter-spacing: -.01em; }
.compose-btn { justify-content: flex-start; padding: 14px 20px; border-radius: 16px; box-shadow: var(--shadow); width: fit-content; }
.folders { display: grid; gap: 2px; }
.folder {
  display: flex; align-items: center; gap: 14px; width: 100%; padding: 8px 14px; border: 0;
  border-radius: 999px; background: transparent; color: var(--text-2); cursor: pointer; text-align: left;
}
.folder:hover { background: var(--hover); }
.folder.active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.folder span { flex: 1; }
.count { font-size: 12px; font-weight: 700; color: var(--accent); }
.me { margin-top: auto; display: flex; align-items: center; gap: 10px; padding: 8px; border-top: 1px solid var(--border); padding-top: 14px; }
.me img { width: 32px; height: 32px; border-radius: 999px; }
.me .who { flex: 1; min-width: 0; line-height: 1.25; }
.me .who b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me .who span { font-size: 12px; color: var(--muted); }
.scrim { display: none; }

.list-pane { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
.toolbar { display: flex; align-items: center; gap: 6px; padding: 12px 12px 8px; }
.search { flex: 1; display: flex; align-items: center; gap: 8px; background: var(--surface-2); border-radius: 999px; padding: 0 14px; color: var(--muted); border: 1px solid transparent; }
.search:focus-within { background: var(--surface); border-color: var(--border); box-shadow: var(--shadow); }
.search input { flex: 1; border: 0; background: transparent; padding: 9px 0; outline: none; color: var(--text); min-width: 0; }
.list-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 16px 10px; border-bottom: 1px solid var(--border); }
.list-head h2 { font-size: 15px; font-weight: 700; }
.chip-toggle { display: inline-flex; cursor: pointer; }
.chip-toggle input { position: absolute; opacity: 0; }
.chip-toggle span { font-size: 12px; font-weight: 500; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); color: var(--text-2); }
.chip-toggle input:checked + span { background: var(--accent-soft); border-color: transparent; color: var(--text); }
.chip-toggle input:focus-visible + span { outline: 2px solid var(--accent); }

.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.row { display: grid; grid-template-columns: 36px 1fr; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer; position: relative; }
.row:hover { background: var(--hover); }
.row.active { background: var(--selected); }
.row.unread::before { content: ""; position: absolute; left: 5px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 999px; background: var(--accent); }
.row .top { display: flex; align-items: baseline; gap: 8px; }
.row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.row .time { font-size: 12px; color: var(--muted); flex: none; }
.row .subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); display: flex; gap: 6px; align-items: center; }
.row .subject svg { width: 14px; height: 14px; color: var(--muted); }
.row .snippet { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row.unread .name, .row.unread .subject { color: var(--text); font-weight: 600; }
.row.unread .time { color: var(--accent); font-weight: 600; }
.list-empty { padding: 48px 24px; text-align: center; color: var(--muted); }

.avatar { width: 36px; height: 36px; border-radius: 999px; display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 14px; flex: none; }
.avatar.lg { width: 42px; height: 42px; font-size: 16px; }

/* Reader */
.reader { min-height: 0; overflow-y: auto; background: var(--surface); display: flex; flex-direction: column; }
.reader-empty { margin: auto; display: grid; justify-items: center; gap: 8px; color: var(--muted); }
.reader-empty svg { width: 56px; height: 56px; stroke-width: 1.2; opacity: .5; }
.msg { display: flex; flex-direction: column; gap: 16px; padding: 20px 28px 28px; flex: 1; }
.msg-actions { display: flex; align-items: center; gap: 4px; margin: -4px -8px 0; }
.msg-actions .spacer { flex: 1; }
.msg h1 { font-size: 22px; font-weight: 600; letter-spacing: -.015em; line-height: 1.3; }
.sender { display: flex; gap: 12px; align-items: flex-start; }
.sender .info { flex: 1; min-width: 0; }
.sender .line1 { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px; }
.sender .line1 b { font-weight: 600; }
.sender .addr { color: var(--muted); font-size: 13px; word-break: break-all; }
.sender .to { color: var(--muted); font-size: 13px; margin-top: 2px; word-break: break-word; }
.sender time { color: var(--muted); font-size: 13px; flex: none; }
.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 500; padding: 2px 8px; border-radius: 999px; }
.badge svg { width: 14px; height: 14px; }
.badge.ok { background: var(--accent-soft); color: var(--accent); }
.badge.warn { background: color-mix(in srgb, var(--warn) 15%, transparent); color: var(--warn); }
.badge.neutral { background: var(--surface-2); color: var(--muted); }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; }
.file-chip {
  display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); cursor: pointer; max-width: 260px; color: var(--text);
}
.file-chip:hover { background: var(--hover); }
.file-chip .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chip .fsize { color: var(--muted); font-size: 12px; flex: none; }
.body-frame { flex: 1; min-height: 50vh; width: 100%; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.body-text { white-space: pre-wrap; word-break: break-word; margin: 0; font: inherit; line-height: 1.6; }
.reply-bar { display: flex; gap: 8px; flex-wrap: wrap; }

/* Compose */
.compose {
  position: fixed; right: 24px; bottom: 0; width: min(560px, 100vw); height: min(620px, 90vh);
  display: flex; flex-direction: column; background: var(--surface); border-radius: 12px 12px 0 0;
  box-shadow: var(--shadow-lg); border: 1px solid var(--border); border-bottom: 0; z-index: 30;
}
.compose-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 8px 16px; background: var(--surface-2); border-radius: 12px 12px 0 0; font-weight: 600; }
.compose-fields { display: grid; padding: 0 16px; }
.field { display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border); padding: 2px 0; }
.field > span:first-child { color: var(--muted); width: 44px; flex: none; font-size: 13px; }
.field input { flex: 1; border: 0; background: transparent; padding: 8px 0; outline: none; min-width: 0; }
.from-wrap { flex: 1; display: flex; align-items: center; min-width: 0; }
.from-wrap input { flex: 0 1 auto; width: 6ch; min-width: 3ch; max-width: 100%; }
.from-wrap .domain { color: var(--muted); }
#c-subject { font-weight: 500; }
#c-body { flex: 1; border: 0; resize: none; padding: 14px 16px; background: transparent; outline: none; line-height: 1.6; }
.files { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px; }
.files:empty { display: none; }
.files .file-chip { padding: 4px 6px 4px 10px; font-size: 13px; cursor: default; }
.files .file-chip button { border: 0; background: none; color: var(--muted); cursor: pointer; display: grid; place-items: center; padding: 2px; border-radius: 999px; }
.files .file-chip button:hover { background: var(--hover); color: var(--text); }
.files .file-chip svg { width: 14px; height: 14px; }
.compose-foot { display: flex; align-items: center; gap: 8px; padding: 12px 16px; }
.compose-foot .error { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1f2328; color: #fff;
  padding: 10px 18px; border-radius: 8px; box-shadow: var(--shadow-lg); z-index: 40; font-weight: 500;
}

.mobile-only, .mobile-back { display: none; }

/* Tablet: hide sidebar into a drawer */
@media (max-width: 1100px) {
  .app { grid-template-columns: minmax(300px, 380px) 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 20; background: var(--surface); transform: translateX(-100%); transition: transform .2s; visibility: hidden; }
  .app.nav-open .sidebar { transform: none; box-shadow: var(--shadow-lg); visibility: visible; }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 15; }
  .mobile-only { display: inline-grid; }
  .list-pane { border-left: 0; }
}
/* Phone: one pane at a time */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .app.reading .list-pane { display: none; }
  .app:not(.reading) .reader { display: none; }
  .mobile-back { display: inline-grid; }
  .msg { padding: 12px 16px 20px; }
  .msg h1 { font-size: 19px; }
  .sender { flex-wrap: wrap; }
  .sender .info { flex-basis: calc(100% - 54px); }
  .sender .addr { word-break: normal; overflow-wrap: anywhere; }
  .sender time { order: 3; width: 100%; padding-left: 54px; font-size: 12px; }
  .compose { inset: 0; width: 100%; height: 100%; border-radius: 0; border: 0; }
  .compose-head { border-radius: 0; }
}
