:root {
  --bg: #f3f5f8; --panel: #ffffff; --panel-2: #f8fafc; --text: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --accent: #0891b2; --accent-soft: #e0f7fb; --accent-text: #ffffff; --hover: #f1f5f9; --sel: #e6f6fa;
  --danger: #dc2626; --danger-soft: #fef2f2; --input: #ffffff; --shadow: 0 1px 2px rgba(15,23,42,.06), 0 4px 16px rgba(15,23,42,.06);
  --grad: linear-gradient(135deg, #22c3e6 0%, #9be22d 100%);
  --side: #0e1116; --side-text: #cbd5e1; --side-muted: #7c8796; --side-hover: #1a1f27; --side-on: #222a35;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0e13; --panel: #12161d; --panel-2: #161b23; --text: #e6e9ef; --muted: #8b95a5; --line: #222935;
    --accent: #4cd7f6; --accent-soft: #10303a; --accent-text: #061016; --hover: #181e27; --sel: #13303a;
    --danger: #f87171; --danger-soft: #2a1515; --input: #0e1218; --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
    --side: #080a0e; --side-hover: #141922; --side-on: #1b222d;
  }
}
* { box-sizing: border-box; }
html, body, #app { margin: 0; height: 100%; }
body { height: 100dvh; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-text-size-adjust: 100%; -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
svg.i { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.btn {
  display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--panel);
  border-radius: 10px; padding: 8px 14px; min-height: 40px; white-space: nowrap; font-weight: 500; transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.grad { background: var(--grad); color: #071016; border: 0; font-weight: 700; box-shadow: 0 4px 14px rgba(34,195,230,.25); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--hover); }
.btn:disabled { opacity: .6; cursor: default; }
.iconbtn { border: 0; background: none; width: 40px; height: 40px; border-radius: 10px; display: inline-grid; place-items: center; color: var(--muted); }
.iconbtn:hover { background: var(--hover); color: var(--text); }

input.field, textarea.field {
  width: 100%; border: 1px solid var(--line); background: var(--input); border-radius: 10px; padding: 10px 12px; transition: border-color .15s, box-shadow .15s;
}
input.field:focus, textarea.field:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ── login ── */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; background: radial-gradient(1200px 600px at 10% -10%, rgba(34,195,230,.18), transparent 60%), radial-gradient(900px 500px at 110% 110%, rgba(155,226,45,.14), transparent 60%), var(--bg); }
.login form { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 32px 28px; display: grid; gap: 14px; box-shadow: var(--shadow); }
.login .logo { width: 56px; height: 56px; border-radius: 14px; }
.login h1 { margin: 4px 0 0; font-size: 22px; letter-spacing: -.01em; }
.login p { margin: 0 0 6px; color: var(--muted); font-size: 14px; }
.login .btn { justify-content: center; min-height: 44px; }
.err { color: var(--danger); font-size: 14px; min-height: 1.2em; }

/* ── shell: sidebar | list | reader ── */
.shell { display: grid; grid-template-columns: 232px minmax(320px, 400px) 1fr; height: 100%; }
.side { background: var(--side); color: var(--side-text); display: flex; flex-direction: column; padding: 16px 12px; gap: 6px; min-height: 0; }
.side .brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; color: #fff; text-decoration: none; }
.side .brand img { width: 34px; height: 34px; border-radius: 9px; }
.side .brand b { font-size: 16px; letter-spacing: -.01em; display: block; line-height: 1.2; }
.side .brand small { color: var(--side-muted); font-size: 12px; }
.side .compose-btn { justify-content: center; margin-bottom: 10px; min-height: 44px; }
.side nav { display: grid; gap: 2px; }
.nav { display: flex; align-items: center; gap: 12px; padding: 9px 12px; border-radius: 10px; border: 0; background: none; color: var(--side-text); text-align: left; width: 100%; }
.nav:hover { background: var(--side-hover); color: #fff; }
.nav.on { background: var(--side-on); color: #fff; font-weight: 600; }
.nav.on svg { color: #4cd7f6; }
.nav .label { flex: 1; }
.nav .count { background: var(--grad); color: #071016; border-radius: 999px; padding: 0 8px; font-size: 12px; font-weight: 700; line-height: 20px; }
.side .spacer { flex: 1; }
.side .foot { color: var(--side-muted); font-size: 12px; padding: 8px 12px 0; overflow: hidden; text-overflow: ellipsis; }

.listpane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.listhead { padding: 16px 16px 12px; border-bottom: 1px solid var(--line); display: grid; gap: 12px; }
.listhead .row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.listhead h2 { margin: 0; font-size: 20px; letter-spacing: -.01em; }
.searchbox { position: relative; }
.searchbox svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 18px; height: 18px; }
.searchbox input { padding-left: 38px; background: var(--panel-2); }
.list { overflow-y: auto; flex: 1; }
.item { display: flex; gap: 12px; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 14px 16px; position: relative; }
.item:hover { background: var(--hover); }
.item.sel { background: var(--sel); }
.item.sel::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent); }
.item .body { flex: 1; min-width: 0; }
.item .row1 { display: flex; gap: 8px; align-items: baseline; }
.item .who { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.item .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.item .subj, .item .snip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .subj { font-size: 14px; }
.item .snip { color: var(--muted); font-size: 13px; }
.item .clip { width: 14px; height: 14px; color: var(--muted); align-self: center; }
.item.unread .who, .item.unread .subj { font-weight: 700; }
.item.unread .when { color: var(--accent); font-weight: 600; }
.item.unread .dot { position: absolute; left: 6px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--accent); }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 15px; color: #fff; flex: none; text-transform: uppercase; }
.avatar.lg { width: 44px; height: 44px; font-size: 17px; }
.empty { padding: 48px 24px; color: var(--muted); text-align: center; display: grid; gap: 8px; justify-items: center; }
.empty svg { width: 40px; height: 40px; opacity: .5; }
.more { margin: 14px auto; display: flex; }

.view { overflow-y: auto; min-width: 0; background: var(--bg); }
.placeholder { color: var(--muted); display: grid; place-items: center; align-content: center; gap: 10px; height: 100%; text-align: center; padding: 24px; }
.placeholder img { width: 64px; height: 64px; border-radius: 16px; opacity: .9; }
.toolbar { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 4px; padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.toolbar .grow { flex: 1; }
.reader { padding: 20px; max-width: 980px; margin: 0 auto; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); }
.reader .card { padding: 22px; }
.reader h1 { margin: 0 0 18px; font-size: 22px; line-height: 1.3; letter-spacing: -.01em; overflow-wrap: anywhere; }
.sender { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 18px; }
.sender .who { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.sender .who b { font-weight: 600; }
.sender .who .addr { color: var(--muted); font-size: 13px; }
.sender .who .to { color: var(--muted); font-size: 13px; }
.sender .date { color: var(--muted); font-size: 13px; white-space: nowrap; }
.mailframe { width: 100%; min-height: 120px; border: 0; display: block; background: #fff; border-radius: 10px; }
.mailtext { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; font: inherit; }
.atts { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.atts a { display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; color: var(--text); text-decoration: none; background: var(--panel-2); max-width: 100%; overflow-wrap: anywhere; font-size: 14px; }
.atts a:hover { border-color: var(--accent); }
.atts small { color: var(--muted); }
.note { background: var(--accent-soft); border-radius: 10px; padding: 8px 12px; font-size: 14px; margin-bottom: 14px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: space-between; }
.reply-bar { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }

.compose { padding: 20px; max-width: 860px; margin: 0 auto; }
.compose .card { overflow: hidden; }
.compose .head { padding: 14px 18px; border-bottom: 1px solid var(--line); font-weight: 600; display: flex; align-items: center; justify-content: space-between; }
.compose .line { display: flex; align-items: center; gap: 10px; padding: 0 18px; border-bottom: 1px solid var(--line); }
.compose .line span { color: var(--muted); font-size: 14px; width: 64px; flex: none; }
.compose .line input { border: 0; background: none; padding: 12px 0; width: 100%; outline: none; }
.compose textarea { border: 0; background: none; width: 100%; min-height: 300px; padding: 16px 18px; resize: vertical; outline: none; line-height: 1.6; }
.compose .sig { padding: 0 18px 16px; color: var(--muted); font-size: 13px; display: flex; gap: 12px; align-items: center; }
.compose .sig img { height: 28px; border-radius: 6px; }
.compose .foot { display: flex; align-items: center; gap: 10px; padding: 12px 18px; border-top: 1px solid var(--line); background: var(--panel-2); flex-wrap: wrap; }
.compose .foot .grow { flex: 1; }
.compose .files { font-size: 13px; color: var(--muted); }
.compose .err { padding: 0 18px 12px; }

.settings { padding: 20px; max-width: 640px; margin: 0 auto; display: grid; gap: 16px; }
.settings section { padding: 18px; display: grid; gap: 10px; }
.settings h3 { margin: 0; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.settings p { margin: 0; color: var(--muted); font-size: 14px; }
.settings .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); background: #0f172a; color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 30; max-width: calc(100% - 32px); box-shadow: var(--shadow); font-size: 14px; }

.mobile-only { display: none !important; }
.tabbar, .fab { display: none; }

/* ── tablet: icon-only sidebar ── */
@media (max-width: 1150px) {
  .shell { grid-template-columns: 72px minmax(300px, 360px) 1fr; }
  .side { padding: 16px 10px; align-items: center; }
  .side .brand { padding: 4px 0 14px; }
  .side .brand div, .nav .label, .side .foot { display: none; }
  .side .compose-btn { width: 48px; height: 48px; padding: 0; border-radius: 14px; }
  .side .compose-btn span { display: none; }
  .nav { justify-content: center; padding: 10px; position: relative; }
  .nav .count { position: absolute; top: 2px; right: 2px; font-size: 10px; padding: 0 5px; line-height: 16px; }
}

/* ── phone: one pane at a time, bottom tab bar ── */
@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .side { display: none; }
  .listpane { border-right: 0; }
  .shell.reading .listpane, .shell.reading .tabbar, .shell.reading .fab { display: none; }
  .shell:not(.reading) .view { display: none; }
  .listhead { padding-top: max(14px, env(safe-area-inset-top)); }
  .toolbar { padding-top: max(10px, env(safe-area-inset-top)); }
  .mobile-only { display: inline-grid !important; }
  .tabbar { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--panel); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
  .tabbar button { border: 0; background: none; display: grid; justify-items: center; gap: 2px; padding: 8px 0 6px; font-size: 11px; color: var(--muted); position: relative; }
  .tabbar button.on { color: var(--accent); font-weight: 600; }
  .tabbar .count { position: absolute; top: 4px; left: calc(50% + 6px); background: var(--danger); color: #fff; border-radius: 999px; font-size: 10px; padding: 0 5px; line-height: 16px; font-weight: 700; }
  .fab { display: grid; place-items: center; position: fixed; right: 18px; bottom: calc(76px + env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 18px; border: 0; background: var(--grad); color: #071016; box-shadow: 0 8px 24px rgba(34,195,230,.35); z-index: 5; }
  .fab svg { width: 24px; height: 24px; stroke-width: 2.2; }
  .reader, .compose, .settings { padding: 12px; }
  .reader .card { padding: 16px; border-radius: 14px; }
  .reader h1 { font-size: 19px; }
  .toolbar .lbl { display: none; }
  .compose textarea { min-height: 240px; }
}
