/* =========================================================
   Webmail - Giao diện giống Gmail, responsive + dark/light
   ========================================================= */
*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg: #f6f8fc;
  --surface: #ffffff;
  --surface2: #f2f6fc;
  --surface3: #eef2f7;
  --text: #1f1f1f;
  --muted: #5f6368;
  --border: #e1e4e9;
  --border-strong: #c4c7cc;
  --accent: #0b57d0;
  --accent-soft: #e8f0fe;
  --accent-text: #0b57d0;
  --hover: #f1f3f4;
  --danger: #d93025;
  --success: #188038;
  --warning: #b06000;
  --shadow: 0 1px 6px rgba(32, 33, 36, .15);
  --shadow-lg: 0 6px 28px rgba(32, 33, 36, .28);
  --radius: 12px;
  --sidebar-w: 240px;
  --topbar-h: 56px;
  --font: "Segoe UI", Roboto, system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --unread-w: 500;
}

[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #1a2026;
  --surface2: #232a31;
  --surface3: #161b21;
  --text: #e8eaed;
  --muted: #9aa0a6;
  --border: #3c4043;
  --border-strong: #5f6368;
  --accent: #8ab4f8;
  --accent-soft: #1f3a5f;
  --accent-text: #8ab4f8;
  --hover: #282d33;
  --danger: #f28b82;
  --success: #81c995;
  --warning: #fdd663;
  --shadow: 0 1px 6px rgba(0, 0, 0, .45);
  --shadow-lg: 0 6px 28px rgba(0, 0, 0, .6);
}

html, body { height: 100%; margin: 0; }

body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.hidden { display: none !important; }

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; color: var(--text); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 6px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background-color: var(--muted); }

a { color: var(--accent); }

/* ---------------- chung ------------- */
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 50%;
  border: none; background: transparent; color: var(--text);
  transition: background .15s;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn svg { display: block; }

.theme-icon { display: inline-flex; }
[data-theme="light"] .theme-dark-icon { display: inline-flex; }
[data-theme="light"] .theme-light-icon { display: none; }
[data-theme="dark"] .theme-dark-icon { display: none; }
[data-theme="dark"] .theme-light-icon { display: inline-flex; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text); padding: 9px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 500; transition: background .15s, box-shadow .15s;
}
.btn:hover { background: var(--hover); box-shadow: var(--shadow); }
.btn svg { display: block; }
.btn-primary {
  background: var(--accent); border-color: transparent; color: var(--surface); font-weight: 600;
}
.btn-primary:hover { background: var(--accent-text); color: var(--surface); }
.btn-block { width: 100%; justify-content: center; }
.btn-ghost {
  background: transparent; border-color: transparent; color: var(--accent-text);
  padding: 7px 12px; font-weight: 600;
}
.btn-ghost:hover { background: var(--accent-soft); box-shadow: none; }

.alert { padding: 10px 14px; border-radius: 8px; font-size: 13px; line-height: 1.5; margin: 10px 0; }
.alert-danger { background: rgba(217, 48, 37, .12); color: var(--danger); border: 1px solid rgba(217, 48, 37, .3); }
code { background: var(--surface3); padding: 1px 5px; border-radius: 4px; font-size: 12px; }

/* toasts */
.toasts {
  position: fixed; top: 14px; right: 14px; z-index: 200; display: flex; flex-direction: column; gap: 8px;
}
.toast {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  box-shadow: var(--shadow-lg); border-radius: 10px; padding: 12px 16px; min-width: 220px; max-width: 340px;
  display: flex; gap: 10px; align-items: flex-start; animation: t-in .2s ease;
}
.toast.error { border-left: 4px solid var(--danger); }
.toast.ok { border-left: 4px solid var(--success); }
.toast .t-close { margin-left: auto; border: none; background: none; color: var(--muted); font-size: 16px; }
@keyframes t-in { from { transform: translateY(-8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------------- đăng nhập ------------- */
.login-page {
  height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; overflow: auto;
  background: radial-gradient(1200px 600px at 20% -10%, var(--accent-soft), transparent), var(--bg);
}
.login-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 32px 34px 22px; text-align: center;
}
.login-logo { display: inline-flex; margin-bottom: 8px; }
.login-title { margin: 6px 0 2px; font-size: 22px; font-weight: 600; }
.login-sub { margin: 0 0 8px; color: var(--muted); }
.login-sub b { color: var(--accent-text); }
.field { display: block; text-align: left; margin: 12px 0; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
.field input {
  width: 100%; padding: 11px 12px; border: 1px solid var(--border-strong); border-radius: 8px;
  font-size: 15px; background: var(--surface); outline: none; transition: border-color .15s, box-shadow .15s;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.pw-wrap { position: relative; display: block; }
.pw-wrap input { padding-right: 42px; }
.pw-eye {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 32px; height: 32px; border: none; background: none; color: var(--muted); font-size: 15px; border-radius: 50%;
}
.pw-eye:hover { background: var(--hover); }
#lp-submit { margin-top: 8px; }
.login-hint { font-size: 12.5px; color: var(--muted); margin: 14px 0 0; }
.login-foot { font-size: 11.5px; color: var(--muted); margin: 16px 0 0; opacity: .85; }

/* ---------------- bố cục app ------------- */
.app {
  display: grid; grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh; max-height: 100vh;
}
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0; z-index: 40;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 20px 20px 12px; }
.brand-name { font-size: 17px; font-weight: 600; }
.brand-mark { display: inline-flex; }

.btn-compose {
  margin: 8px 16px 12px; border-radius: 999px; box-shadow: var(--shadow);
  justify-content: flex-start; font-weight: 600; padding: 10px 18px;
}
.btn-compose:hover { box-shadow: var(--shadow-lg); }

.nav { flex: 1; overflow-y: auto; padding: 4px 8px 12px; min-height: 0; }
.nav-group { font-size: 11.5px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); padding: 12px 12px 4px; }
.nav-item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  border: none; background: transparent; color: var(--text);
  padding: 9px 14px; border-radius: 999px; font-size: 13.5px; text-align: left;
  transition: background .12s; position: relative; margin-bottom: 1px;
}
.nav-item:hover { background: var(--hover); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-text); font-weight: 500; }
.nav-item svg { flex: none; }
.nav-item .n-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .n-badge {
  font-size: 12px; font-weight: 600; color: var(--accent-text); background: rgba(11, 87, 208, .1);
  border-radius: 999px; padding: 1px 8px; min-width: 22px; text-align: center;
}
[data-theme="dark"] .nav-item .n-badge { background: rgba(138, 180, 248, .16); }

.side-foot { border-top: 1px solid var(--border); padding: 10px 14px; display: flex; align-items: center; gap: 6px; }
.user-chip { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; flex: none; display: inline-flex;
  align-items: center; justify-content: center; background: linear-gradient(135deg, #7c83ff, #4a5cff);
  color: #fff; font-weight: 600; font-size: 14px; text-transform: uppercase;
}
.user-meta { display: flex; flex-direction: column; min-width: 0; }
.user-meta b { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-meta span { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.scrim {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 35; animation: fade-in .2s;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; }
.topbar {
  height: var(--topbar-h); flex: none; display: flex; align-items: center; gap: 6px;
  padding: 0 12px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.topbar-spacer { flex: 1; }
.search {
  display: flex; align-items: center; gap: 8px; flex: 1; max-width: 560px; margin: 0 8px;
  background: var(--bg); border: 1px solid transparent; border-radius: 24px; padding: 7px 12px;
  transition: background .15s, box-shadow .15s;
}
.search:focus-within { background: var(--surface); box-shadow: var(--shadow); border-color: var(--border-strong); }
.search-ic { color: var(--muted); display: inline-flex; }
.search input { flex: 1; border: none; outline: none; background: transparent; font-size: 14px; min-width: 0; }
.search-clear { border: none; background: none; color: var(--muted); display: inline-flex; padding: 2px; }
.avatar-top { width: 34px; height: 34px; margin-left: 4px; }

.workspace { flex: 1; display: flex; min-height: 0; position: relative; }
.pane { min-height: 0; display: flex; flex-direction: column; }
.list-pane {
  flex: 1; width: auto; border-right: 1px solid var(--border); background: var(--surface);
}
.app.reading .list-pane { flex: none; width: 44%; min-width: 320px; max-width: 620px; }
.read-pane { flex: 1; min-width: 0; background: var(--surface); }

.pane-head {
  flex: none; padding: 10px 14px 6px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.pane-title-row { display: flex; align-items: center; gap: 12px; }
.select-all input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.pane-title { margin: 0; font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pane-count { font-size: 12px; color: var(--muted); margin-left: auto; white-space: nowrap; }

.bulk-bar { display: flex; align-items: center; gap: 4px; padding: 6px 0 4px; }
.bulk-move { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); }
.bulk-move select { padding: 5px 8px; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--surface); font-size: 12.5px; }

.msg-list { flex: 1; overflow-y: auto; min-height: 0; }
.msg-row {
  display: grid; grid-template-columns: 34px 1fr auto; gap: 8px; align-items: center;
  padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border);
}
.msg-row:hover { background: var(--hover); }
.msg-row.active { background: var(--accent-soft); }
.msg-row .m-check { display: flex; align-items: center; justify-content: center; }
.msg-row .m-check input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.msg-row .m-clip { color: var(--muted); display: inline-flex; }
.msg-row.unseen .m-main .m-subject { font-weight: 700; }
.msg-row.unseen .m-main .m-from { font-weight: 600; }
.msg-row .m-main { min-width: 0; }
.msg-row .m-main .m-from { font-size: 13px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-row .m-main .m-subject { font-size: 13px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-row .m-main .m-snippet { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.msg-row .m-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; }
.msg-row .m-date { font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.msg-row .m-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }
.msg-row.unseen .m-dot { background: var(--success); }
.grid-form { font-size: 11px; color: var(--muted); }

.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; color: var(--muted); padding: 40px; text-align: center; }
.empty-ic svg { opacity: .4; }

.pager { flex: none; display: flex; align-items: center; justify-content: center; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }
.pager button {
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 6px 14px; font-size: 13px;
}
.pager button:hover:not(:disabled) { background: var(--hover); }
.pager button:disabled { opacity: .4; cursor: default; }
.pager .p-info { font-size: 12.5px; color: var(--muted); margin: 0 6px; }

.loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 10px; background: var(--surface); color: var(--muted); z-index: 10; }
.spinner {
  width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------- đọc thư ------------- */
.read-toolbar {
  flex: none; display: flex; align-items: center; gap: 4px; padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.read-body { flex: 1; overflow-y: auto; padding: 22px 26px 60px; }
.msg-header { margin-bottom: 22px; padding-bottom: 16px; border-bottom: 1px solid var(--border); }
.msg-subject { font-size: 20px; font-weight: 600; margin: 0 0 14px; line-height: 1.35; word-break: break-word; }
.msg-sender { display: flex; align-items: center; gap: 12px; margin-top: 6px; }
.msg-sender .sender-name { font-size: 14px; font-weight: 600; }
.msg-sender .sender-mail { color: var(--muted); font-size: 12.5px; }
.msg-date-line { margin-left: auto; color: var(--muted); font-size: 12px; text-align: right; }
.msg-recipients { font-size: 12.5px; color: var(--muted); margin: 6px 0 0 59px; }
.msg-recipients b { color: var(--text); }

.attachments-bar { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 18px; }
.att-chip {
  display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--border);
  background: var(--surface2); border-radius: 10px; padding: 8px 10px; font-size: 12.5px; max-width: 260px;
}
.att-chip:hover { background: var(--hover); }
.att-chip .att-ic { color: var(--muted); display: inline-flex; flex: none; }
.att-chip .att-info { min-width: 0; }
.att-chip .att-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; color: var(--text); font-weight: 500; }
.att-chip .att-size { color: var(--muted); font-size: 11px; }

.msg-html { line-height: 1.6; word-wrap: break-word; overflow-wrap: anywhere; }
.msg-html img { max-width: 100%; height: auto; }
.msg-html blockquote, .quote blockquote { border-left: 3px solid var(--border-strong); margin: 8px 0 8px 8px; padding-left: 12px; color: var(--muted); }
.msg-html table { max-width: 100%; border-collapse: collapse; }
.msg-html pre, .msg-html code { white-space: pre-wrap; }
.msg-plain { white-space: pre-wrap; font-family: inherit; line-height: 1.6; }

/* ---------------- soạn thư ------------- */
.compose-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 100;
  display: flex; align-items: flex-end; justify-content: flex-end; padding: 0;
}
.compose {
  width: 720px; max-width: 100vw; height: min(680px, 92vh);
  background: var(--surface); border-radius: 20px 20px 0 0; box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; overflow: hidden;
  animation: compose-up .22s ease;
}
@keyframes compose-up { from { transform: translateY(30px); opacity: .5; } to { transform: none; opacity: 1; } }
.compose-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--surface2); border-bottom: 1px solid var(--border);
}
.compose-head > span { font-size: 15px; font-weight: 600; }
.compose-head-actions { display: flex; align-items: center; gap: 6px; }
.compose-fields { flex: none; border-bottom: 1px solid var(--border); }
.cf-row { display: flex; align-items: center; gap: 10px; padding: 8px 16px; border-bottom: 1px solid var(--border); }
.cf-label { width: 56px; flex: none; font-size: 12.5px; color: var(--muted); }
.cf-row input {
  flex: 1; border: none; outline: none; background: transparent; font-size: 14px; min-width: 0; padding: 3px 0;
}
.cf-toggle { border: none; background: none; color: var(--accent-text); font-size: 12px; font-weight: 600; padding: 4px 8px; border-radius: 6px; }
.cf-toggle:hover { background: var(--accent-soft); }
.cf-attach { padding: 8px 16px; border-bottom: 1px solid var(--border); }
.cf-attach-list { display: flex; flex-wrap: wrap; gap: 8px; }
.att-mini {
  display: inline-flex; align-items: center; gap: 8px; background: var(--surface2);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; font-size: 12.5px; max-width: 220px;
}
.att-mini .att-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.att-mini button { border: none; background: none; color: var(--muted); padding: 2px; display: inline-flex; }
.att-mini button:hover { color: var(--danger); }

.compose-body {
  flex: 1; overflow-y: auto; padding: 14px 18px; outline: none; font-size: 14.5px; line-height: 1.6;
}
.compose-body:empty::before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; }
.compose-foot {
  flex: none; display: flex; align-items: center; gap: 8px; padding: 10px 16px;
  border-top: 1px solid var(--border); background: var(--surface);
}
.foot-hint { font-size: 11.5px; color: var(--muted); }
.compose-status { flex: none; padding: 8px 16px; font-size: 13px; color: var(--success); border-top: 1px solid var(--border); }

/* quote trong trả lời/chuyển tiếp */
.quote {
  margin-top: 16px; padding: 10px 0 0 12px; border-top: 1px solid var(--border-strong); border-left: 3px solid var(--border-strong);
  padding-left: 12px; color: var(--muted); font-size: 13.5px;
}

/* ---------------- responsive ------------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: min(78vw, 300px);
    transform: translateX(-105%); transition: transform .22s ease; box-shadow: var(--shadow-lg);
  }
  body.sidebar-open .sidebar { transform: none; }
  body.sidebar-open .scrim { display: block; }
  .list-pane { width: 100%; max-width: none; min-width: 0; border-right: none; flex: none !important; }
  .read-pane {
    position: absolute; inset: 0; z-index: 30; background: var(--surface);
  }
  .workspace { overflow: hidden; }
  .compose { border-radius: 16px 16px 0 0; }
  .brand-name, .search { margin-left: 2px; }
}

@media (max-width: 520px) {
  .read-body { padding: 16px 16px 50px; }
  .msg-subject { font-size: 17px; }
  .foot-hint { display: none; }
  .pane-title-row { gap: 8px; }
  .msg-sender .sender-date { display: none; }
  .avatar-top { display: none; }
}