:root {
  color-scheme: light dark;
  --bg: #eef2f8;
  --bg-glow-1: rgb(99 102 241 / 14%);
  --bg-glow-2: rgb(14 165 233 / 12%);
  --panel: #ffffff;
  --panel-2: #f6f8fc;
  --hover: #f1f4fa;
  --line: #e3e8f1;
  --text: #121a2b;
  --muted: #5f6c82;
  --faint: #95a0b4;
  --accent: #4f46e5;
  --accent-2: #0ea5e9;
  --accent-ink: #ffffff;
  --accent-soft: #eef0ff;
  --code-bg: #eef0ff;
  --code-ink: #3f37c9;
  --live: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --radius: 18px;
  --shadow: 0 1px 2px rgb(16 24 40 / 4%), 0 8px 28px rgb(30 41 80 / 8%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --bg-glow-1: rgb(99 102 241 / 18%);
    --bg-glow-2: rgb(14 165 233 / 10%);
    --panel: #131a2c;
    --panel-2: #172036;
    --hover: #1a2338;
    --line: #243049;
    --text: #e8ecf5;
    --muted: #9aa6bd;
    --faint: #6d7990;
    --accent: #818cf8;
    --accent-2: #38bdf8;
    --accent-ink: #0b1020;
    --accent-soft: #1e2550;
    --code-bg: #1e2550;
    --code-ink: #b4bcff;
    --live: #4ade80;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 10px 30px rgb(0 0 0 / 25%);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  color: var(--text);
  font: 14.5px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(900px 500px at 8% -10%, var(--bg-glow-1), transparent 60%),
    radial-gradient(800px 500px at 100% 110%, var(--bg-glow-2), transparent 60%),
    var(--bg);
  background-attachment: fixed;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr-only { position: absolute; top: 0; left: 0; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
svg.icon { width: 18px; height: 18px; flex: none; }

/* ---------- Khung: 3 thẻ nổi ---------- */
.layout { display: grid; grid-template-columns: 340px minmax(330px, 420px) 1fr; gap: 16px; padding: 16px; height: 100vh; height: 100dvh; }
.layout > * { min-width: 0; min-height: 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.rail { display: flex; flex-direction: column; gap: 14px; padding: 16px; overflow-y: auto; }

.brand { display: flex; align-items: center; gap: 10px; padding: 2px 4px 2px; }
.brand-mark { width: 34px; height: 34px; border-radius: 11px; color: #fff; display: grid; place-items: center; background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: 0 6px 16px rgb(79 70 229 / 30%); }
.brand-mark svg { width: 18px; height: 18px; }
.brand-name { font-weight: 800; font-size: 18px; letter-spacing: -0.02em; line-height: 1.1; }
.brand-name small { display: block; font-weight: 500; font-size: 12px; color: var(--faint); letter-spacing: 0; }

/* Thẻ địa chỉ nổi bật */
.hero { position: relative; overflow: hidden; border-radius: 16px; padding: 16px; color: #fff; display: grid; gap: 12px;
  background: linear-gradient(135deg, #4f46e5 0%, #6d28d9 55%, #0ea5e9 130%); box-shadow: 0 10px 28px rgb(79 70 229 / 28%); }
.hero::after { content: ""; position: absolute; width: 180px; height: 180px; right: -60px; top: -70px; border-radius: 50%; background: rgb(255 255 255 / 10%); pointer-events: none; }
.hero-label { display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; color: rgb(255 255 255 / 80%); }
.hero-label svg { width: 15px; height: 15px; }
.address { font-family: var(--mono); line-height: 1.35; overflow-wrap: anywhere; cursor: copy; border-radius: 8px; position: relative; z-index: 1; }
.address .local { display: block; font-size: 22px; font-weight: 750; letter-spacing: -0.01em; }
.address .domain { display: block; font-size: 14px; color: rgb(255 255 255 / 78%); }
.address.loading .local { opacity: 0.6; }
.hero-actions { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; position: relative; z-index: 1; }
.hero .btn { border-color: rgb(255 255 255 / 22%); background: rgb(255 255 255 / 14%); color: #fff; }
.hero .btn:hover { background: rgb(255 255 255 / 24%); }
.hero .btn.primary { background: #fff; color: #3f37c9; border-color: #fff; }
.hero .btn.primary:hover { filter: none; background: #f4f4ff; }
.hero .btn.done { background: #16a34a; border-color: #16a34a; color: #fff; }
.hero-foot { display: flex; justify-content: space-between; align-items: center; position: relative; z-index: 1; }
.pill { display: inline-flex; align-items: center; gap: 7px; padding: 4px 10px; border-radius: 999px; background: rgb(255 255 255 / 16%); font-size: 12.5px; font-weight: 600; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: #4ade80; box-shadow: 0 0 0 0 rgb(74 222 128 / 60%); animation: pulse 2s infinite; }
.pill.off .dot { background: #fbbf24; animation: none; }
@keyframes pulse { 70% { box-shadow: 0 0 0 7px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }
.link-btn { all: unset; display: inline-flex; align-items: center; gap: 5px; cursor: pointer; font-size: 12.5px; font-weight: 600; color: rgb(255 255 255 / 85%); padding: 4px 6px; border-radius: 8px; }
.link-btn:hover { background: rgb(255 255 255 / 14%); color: #fff; }
.link-btn svg { width: 15px; height: 15px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 7px; border: 1px solid var(--line); background: var(--panel); border-radius: 11px; padding: 9px 12px; font-weight: 650; white-space: nowrap; transition: background 0.12s, border-color 0.12s, transform 0.08s; }
.btn:hover { background: var(--hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.icon-only { padding: 9px 10px; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--hover); color: var(--text); }
.btn.small { padding: 6px 9px; border-radius: 9px; }
.spin svg { animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .pill .dot, .spin svg { animation: none; } }

/* Thẻ phụ trong cột trái */
.box { border: 1px solid var(--line); border-radius: 14px; background: var(--panel-2); padding: 12px; display: grid; gap: 10px; }
.box-title { display: flex; align-items: center; gap: 7px; font-size: 13px; font-weight: 700; color: var(--muted); margin: 0; }
.box-title svg { width: 16px; height: 16px; color: var(--accent); }
.field-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.input { border: 1px solid var(--line); border-radius: 11px; background: var(--panel); padding: 9px 11px; min-width: 0; width: 100%; }
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.input.mono { font-family: var(--mono); font-size: 13.5px; }
.at-row { display: grid; grid-template-columns: 1fr auto; align-items: center; border: 1px solid var(--line); border-radius: 11px; background: var(--panel); overflow: hidden; }
.at-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.at-row input { border: 0; background: transparent; padding: 9px 11px; min-width: 0; font-family: var(--mono); }
.at-row input:focus { outline: none; }
.at-row span { color: var(--faint); padding-right: 11px; font-family: var(--mono); }

/* Hộp gần đây */
.section-title { display: flex; align-items: center; justify-content: space-between; font-size: 12px; font-weight: 700; color: var(--faint); text-transform: uppercase; letter-spacing: 0.06em; padding: 0 4px; margin: 2px 0 -6px; background: none; border: 0; text-align: left; cursor: default; width: 100%; }
.recent { display: grid; gap: 4px; margin: 0; padding: 0; list-style: none; }
.recent li { display: flex; align-items: center; gap: 4px; border-radius: 12px; border: 1px solid transparent; }
.recent li:hover { background: var(--hover); }
.recent li.current { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 25%, transparent); }
.recent .open { all: unset; flex: 1; min-width: 0; display: grid; grid-template-columns: auto 1fr; gap: 0 10px; align-items: center; padding: 8px 4px 8px 8px; cursor: pointer; border-radius: 12px; }
.recent .open:focus-visible { outline: 2px solid var(--accent); }
.recent .ico { grid-row: span 2; width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; background: var(--panel); border: 1px solid var(--line); color: var(--accent); }
.recent .ico svg { width: 15px; height: 15px; }
.recent .addr { font-family: var(--mono); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.recent li.current .addr { color: var(--accent); font-weight: 650; }
.recent .meta { display: flex; align-items: center; gap: 10px; font-size: 11.5px; color: var(--faint); }
.recent .meta span { display: inline-flex; align-items: center; gap: 3px; }
.recent .meta svg { width: 12px; height: 12px; }
.recent .meta .has { color: var(--accent); font-weight: 700; }
.recent .forget { all: unset; opacity: 0; padding: 7px; color: var(--faint); cursor: pointer; border-radius: 9px; display: grid; place-items: center; }
.recent .forget svg { width: 15px; height: 15px; }
.recent li:hover .forget, .recent .forget:focus-visible { opacity: 1; }
.recent .forget:hover { color: var(--danger); background: var(--panel); }
.rail-foot { margin-top: auto; display: flex; gap: 14px; padding: 4px 4px 0; font-size: 12.5px; }
.rail-foot a { display: inline-flex; align-items: center; gap: 5px; color: var(--faint); text-decoration: none; }
.rail-foot a svg { width: 14px; height: 14px; }
.rail-foot a:hover { color: var(--accent); }

/* ---------- Danh sách thư ---------- */
.inbox { display: flex; flex-direction: column; overflow: hidden; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 14px 12px 16px; border-bottom: 1px solid var(--line); }
.panel-head h1 { display: flex; align-items: center; gap: 10px; font-size: 16px; margin: 0; letter-spacing: -0.01em; }
.head-ico { width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.head-ico svg { width: 17px; height: 17px; }
.count { display: inline-grid; place-items: center; min-width: 22px; height: 22px; padding: 0 7px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 700; }
.list { flex: 1; overflow-y: auto; padding: 8px; }
.item { all: unset; box-sizing: border-box; display: grid; grid-template-columns: auto 1fr auto; gap: 1px 12px; width: 100%; padding: 11px 12px; border-radius: 14px; cursor: pointer; position: relative; border: 1px solid transparent; }
.item + .item { margin-top: 4px; }
.item:hover { background: var(--hover); }
.item[aria-current="true"] { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 22%, transparent); }
.item:focus-visible { outline: 2px solid var(--accent); }
.avatar { grid-row: span 2; width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 15px; align-self: center; text-transform: uppercase; box-shadow: inset 0 0 0 1px rgb(255 255 255 / 12%); }
.item .who { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .when { color: var(--faint); font-size: 12px; white-space: nowrap; justify-self: end; }
.item .subject { grid-column: 2; color: var(--muted); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .code-cell { grid-column: 3; grid-row: 2; justify-self: end; }
.item.unread .who::after { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); margin-left: 7px; vertical-align: 2px; }
.item.fresh { animation: fresh 1.8s ease-out; }
@keyframes fresh { from { background: color-mix(in srgb, var(--accent) 20%, transparent); } }
.code { all: unset; display: inline-flex; align-items: center; gap: 5px; font-family: var(--mono); font-weight: 800; font-size: 14px; letter-spacing: 0.06em; background: var(--code-bg); color: var(--code-ink); padding: 3px 9px; border-radius: 8px; cursor: copy; white-space: nowrap; }
.code svg { width: 13px; height: 13px; opacity: 0.75; }
.code:hover { filter: brightness(0.96); }
.code:focus-visible { outline: 2px solid var(--accent); }

.empty { flex: 1; display: grid; place-content: center; justify-items: center; gap: 4px; text-align: center; color: var(--muted); padding: 32px; }
.empty .art { width: 76px; height: 76px; border-radius: 24px; display: grid; place-items: center; margin-bottom: 10px; color: var(--accent); background: linear-gradient(135deg, var(--accent-soft), color-mix(in srgb, var(--accent-2) 14%, transparent)); }
.empty .art svg { width: 34px; height: 34px; }
.empty b { color: var(--text); font-size: 15.5px; }

/* ---------- Đọc thư ---------- */
.reader { display: flex; flex-direction: column; overflow: hidden; }
.reader-head { padding: 16px 20px 14px; border-bottom: 1px solid var(--line); display: grid; gap: 12px; }
.reader-top { display: flex; align-items: flex-start; gap: 12px; }
.reader-top .avatar { width: 42px; height: 42px; grid-row: auto; flex: none; align-self: flex-start; }
.reader-top h2 { margin: 0; font-size: 18px; letter-spacing: -0.015em; line-height: 1.3; overflow-wrap: anywhere; }
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 12px; color: var(--muted); font-size: 13px; margin-top: 3px; }
.meta b { color: var(--text); font-weight: 650; }
.meta span { display: inline-flex; align-items: center; gap: 4px; }
.meta svg { width: 13px; height: 13px; }
.code-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 12px 14px; border-radius: 14px; border: 1px dashed color-mix(in srgb, var(--accent) 45%, transparent); background: linear-gradient(135deg, var(--accent-soft), color-mix(in srgb, var(--accent-2) 10%, transparent)); }
.code-card .label { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.code-card .label svg { width: 15px; height: 15px; color: var(--accent); }
.code-card .value { font-family: var(--mono); font-size: 28px; font-weight: 800; letter-spacing: 0.08em; color: var(--code-ink); cursor: copy; line-height: 1.2; }
.tabs { display: inline-flex; gap: 2px; padding: 3px; border-radius: 11px; background: var(--panel-2); border: 1px solid var(--line); justify-self: start; }
.tab { all: unset; cursor: pointer; padding: 5px 12px; border-radius: 8px; color: var(--muted); font-size: 13px; font-weight: 650; }
.tab[aria-selected="true"] { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / 8%); }
.tab:focus-visible { outline: 2px solid var(--accent); }
.reader-body { flex: 1; min-height: 0; overflow: auto; background: #fff; }
.reader-body iframe { display: block; width: 100%; height: 100%; border: 0; background: #fff; }
.reader-body pre { margin: 0; padding: 18px 22px; white-space: pre-wrap; overflow-wrap: anywhere; font: 14px/1.6 var(--font); color: #1a2232; background: #fff; min-height: 100%; }
.back { display: none; }

.toasts { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; pointer-events: none; }
.toast { display: flex; align-items: center; gap: 8px; background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 999px; font-weight: 650; box-shadow: var(--shadow); animation: rise 0.18s ease-out; }
.toast svg { width: 16px; height: 16px; }
.toast.bad { background: var(--danger); color: #fff; }
@keyframes rise { from { transform: translateY(6px); opacity: 0; } }

/* ---------- Màn hình vừa (2 cột) ---------- */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 320px 1fr; }
  .reader { position: fixed; inset: 16px 16px 16px calc(320px + 32px); z-index: 20; }
  .layout:not(.reading) .reader { display: none; }
  .back { display: inline-flex; }
}

/* ---------- Điện thoại (1 cột) ---------- */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; height: auto; min-height: 100dvh; padding: 10px; gap: 10px; }
  .rail { overflow: visible; padding: 12px; gap: 12px; }
  .address .local { font-size: 20px; }
  .rail .recent, .rail-foot { display: none; }
  .rail .section-title { cursor: pointer; margin-bottom: 0; }
  .rail .section-title::after { content: ""; width: 7px; height: 7px; border: solid currentColor; border-width: 0 2px 2px 0; transform: rotate(45deg); margin: -4px 6px 0 0; transition: transform .2s; }
  .rail.show-recent .section-title::after { transform: rotate(-135deg); margin-top: 4px; }
  .rail.show-recent .recent { display: grid; }
  .inbox { min-height: 50vh; }
  .list { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .reader { inset: 0; border-radius: 0; border: 0; }
  .reader-head { padding: 12px 14px; }
  .reader-top h2 { font-size: 16.5px; }
  .code-card .value { font-size: 24px; }
}
