:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #f1f3f9;
  --line: #e4e7ef;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #4f46e5;
  --accent-2: #6366f1;
  --accent-soft: #eef0ff;
  --danger: #dc2626;
  --unread: #0f172a;
  --shadow: 0 10px 30px rgba(15, 23, 42, .08);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1020; --panel: #111729; --panel-2: #172036; --line: #232c44;
    --text: #e6e9f2; --muted: #8b95ad; --accent: #818cf8; --accent-2: #a5b4fc;
    --accent-soft: #1d2445; --unread: #ffffff; --shadow: 0 10px 30px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1020; --panel: #111729; --panel-2: #172036; --line: #232c44;
  --text: #e6e9f2; --muted: #8b95ad; --accent: #818cf8; --accent-2: #a5b4fc;
  --accent-soft: #1d2445; --unread: #ffffff; --shadow: 0 10px 30px rgba(0,0,0,.4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button { font: inherit; color: inherit; }
input { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Buttons */
.btn { border: 0; border-radius: 10px; padding: 9px 16px; cursor: pointer; font-weight: 600; display: inline-flex; align-items: center; gap: 8px; justify-content: center; transition: transform .05s, background .15s; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; box-shadow: 0 4px 14px rgba(79, 70, 229, .3); }
.btn-primary:disabled { opacity: .6; cursor: default; }
.btn-ghost { background: transparent; color: var(--accent); }
.btn-block { width: 100%; }
.icon-btn { border: 0; background: transparent; width: 36px; height: 36px; border-radius: 10px; display: inline-grid; place-items: center; cursor: pointer; color: var(--muted); flex: none; }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; font-size: 12px; padding: 0 4px; }

/* Brand */
.brand { display: flex; align-items: center; gap: 10px; font-size: 18px; letter-spacing: -.02em; padding: 4px 6px 18px; }
.brand b { color: var(--accent); font-weight: 700; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: linear-gradient(135deg, var(--accent), #22d3ee); position: relative; }
.brand-mark::after { content: ""; position: absolute; inset: 8px 6px; border: 2px solid #fff; border-top-color: transparent; border-radius: 2px; }
.brand-lg { font-size: 24px; justify-content: center; padding-bottom: 4px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(1200px 600px at 10% -10%, rgba(99,102,241,.18), transparent), radial-gradient(900px 500px at 110% 110%, rgba(34,211,238,.14), transparent), var(--bg); }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.login-card p { margin: 0 0 6px; text-align: center; }
.login-card label { display: grid; gap: 6px; font-weight: 600; font-size: 13px; }
.login-card input[type=email], .login-card input[type=password] { border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px; padding: 11px 12px; outline: none; }
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.check { display: flex !important; align-items: center; gap: 8px; font-weight: 500 !important; color: var(--muted); }
.error { color: var(--danger); text-align: center; margin: 0; }

/* Layout */
.app { display: grid; grid-template-columns: 248px minmax(320px, 400px) 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 16px 12px; display: flex; flex-direction: column; min-height: 0; }
.btn-compose { width: 100%; padding: 12px; margin-bottom: 14px; }
.folders { flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 2px; }
.folder { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; cursor: pointer; color: var(--text); border: 0; background: none; text-align: left; width: 100%; }
.folder:hover { background: var(--panel-2); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folder .name { flex: 1; }
.folder .count { font-size: 12px; font-weight: 700; background: var(--accent); color: #fff; border-radius: 999px; padding: 0 7px; min-width: 20px; text-align: center; }
.account { display: flex; align-items: center; gap: 8px; padding-top: 12px; border-top: 1px solid var(--line); }
.account-info { flex: 1; min-width: 0; font-weight: 600; }
.avatar { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; flex: none; font-size: 13px; }

.list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.list-head { display: flex; align-items: center; gap: 6px; padding: 12px; border-bottom: 1px solid var(--line); }
.search { flex: 1; display: flex; align-items: center; gap: 8px; background: var(--panel-2); border-radius: 10px; padding: 0 10px; color: var(--muted); }
.search input { border: 0; background: transparent; outline: none; padding: 9px 0; width: 100%; color: var(--text); }
.list-title { display: flex; align-items: baseline; justify-content: space-between; padding: 14px 16px 6px; }
.list-title h1 { font-size: 18px; margin: 0; letter-spacing: -.01em; }
.message-list { flex: 1; overflow-y: auto; }
.msg { display: grid; grid-template-columns: 38px 1fr auto; gap: 4px 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; position: relative; }
.msg:hover { background: var(--panel-2); }
.msg.active { background: var(--accent-soft); }
.msg .avatar { grid-row: span 3; width: 36px; height: 36px; }
.msg .from { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.msg .subject { grid-column: 2 / 4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .preview { grid-column: 2 / 4; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg.unread .from, .msg.unread .subject { font-weight: 700; color: var(--unread); }
.msg.unread::before { content: ""; position: absolute; left: 5px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--accent); }
.msg .flags { display: flex; gap: 4px; align-items: center; color: var(--muted); }
.msg .flags svg { width: 14px; height: 14px; }
.star-on { color: #f59e0b !important; fill: #f59e0b; }
.list-empty { padding: 48px 16px; text-align: center; color: var(--muted); }

.reader { background: var(--bg); overflow-y: auto; min-width: 0; }
.empty { height: 100%; display: grid; place-content: center; justify-items: center; color: var(--muted); gap: 8px; }
.empty svg { width: 48px; height: 48px; stroke-width: 1.3; opacity: .6; }
#readerContent { max-width: 920px; margin: 0 auto; padding: 18px 24px 40px; }
.reader-actions { display: flex; gap: 4px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; position: sticky; top: 0; background: var(--bg); padding: 6px 0; z-index: 1; }
.reader-actions .spacer { flex: 1; }
.reader h2 { font-size: 22px; line-height: 1.3; margin: 4px 0 16px; letter-spacing: -.01em; overflow-wrap: anywhere; }
.meta { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 18px; }
.meta .who { flex: 1; min-width: 0; }
.meta .who .name { font-weight: 600; }
.meta .who .addr, .meta .who .rcpt { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.meta .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.mail-frame { width: 100%; border: 0; display: block; min-height: 120px; background: #fff; border-radius: 8px; }
.text-body { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; font: inherit; }
.images-bar { display: flex; align-items: center; gap: 8px; background: var(--accent-soft); border-radius: 10px; padding: 8px 12px; margin-bottom: 12px; font-size: 13px; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.attachment { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); cursor: pointer; max-width: 260px; }
.attachment:hover { border-color: var(--accent); }
.attachment .ellipsis { max-width: 160px; }
.btn-back { display: none; }

/* Compose */
.compose { position: fixed; inset: 0; background: rgba(15, 23, 42, .35); display: flex; align-items: flex-end; justify-content: flex-end; padding: 16px; z-index: 50; }
.compose-card { width: min(640px, 100%); height: min(640px, calc(100dvh - 32px)); background: var(--panel); border-radius: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--line); }
.compose-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 10px 18px; background: var(--panel-2); font-weight: 600; }
.field { display: flex; align-items: center; gap: 10px; padding: 0 16px; border-bottom: 1px solid var(--line); }
.field span { color: var(--muted); width: 44px; font-size: 13px; }
.field select { flex: 1; border: 0; outline: none; background: transparent; padding: 11px 0; font: inherit; color: inherit; }
.field input { flex: 1; border: 0; outline: none; background: transparent; padding: 11px 0; min-width: 0; }
.toolbar { display: flex; gap: 2px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
.toolbar button { border: 0; background: none; width: 30px; height: 28px; border-radius: 6px; cursor: pointer; }
.toolbar button:hover { background: var(--panel-2); }
.compose-body { flex: 1; overflow-y: auto; padding: 14px 16px; outline: none; overflow-wrap: anywhere; }
.compose-body blockquote { margin: 8px 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.attach-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--panel-2); border-radius: 999px; padding: 4px 6px 4px 12px; font-size: 12px; margin-bottom: 8px; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); }
.compose-foot { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); z-index: 60; max-width: calc(100% - 32px); }
.spinner { width: 22px; height: 22px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 40px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.only-mobile { display: none; }
.scrim { display: none; }

/* Responsive */
@media (max-width: 1100px) {
  .app { grid-template-columns: 220px minmax(280px, 340px) 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-grid; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 272px; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .scrim { display: block; position: fixed; inset: 0; background: rgba(15,23,42,.4); z-index: 30; }
  .reader { position: fixed; inset: 0; z-index: 20; transform: translateX(100%); transition: transform .2s; }
  .app.reading .reader { transform: none; }
  .btn-back { display: inline-grid; }
  #readerContent { padding: 8px 16px 32px; }
  .compose { padding: 0; }
  .compose-card { height: 100dvh; width: 100%; border-radius: 0; }
}
