:root {
  color-scheme: dark;
  --bg: #0b0d12;
  --surface: #11141b;
  --raised: #171b24;
  --hover: #1a1f2a;
  --line: #242a36;
  --line-strong: #323a49;
  --text: #e8ecf3;
  --muted: #9aa4b5;
  --faint: #6f7a8c;
  --accent: #4d8dff;
  --accent-strong: #2f6fec;
  --accent-soft: #4d8dff1f;
  --accent-text: #9cc0ff;
  --ok: #43c08f;
  --danger: #f0717e;
  --danger-soft: #f0717e1f;
  --shadow: 0 24px 70px #0009;
  --avatar-s: 42%;
  --avatar-l: 24%;
  --avatar-t: 78%;
  font-family: Inter, 'Segoe UI', system-ui, -apple-system, Roboto, Arial, sans-serif;
  font-synthesis: none;
}
:root[data-theme=light] {
  color-scheme: light;
  --bg: #f3f4f7;
  --surface: #fff;
  --raised: #f5f6f9;
  --hover: #eef1f6;
  --line: #e2e5eb;
  --line-strong: #cdd2dc;
  --text: #171a21;
  --muted: #566070;
  --faint: #79828f;
  --accent: #2563eb;
  --accent-strong: #2563eb;
  --accent-soft: #2563eb14;
  --accent-text: #1d4fd0;
  --ok: #128a5e;
  --danger: #c9303f;
  --danger-soft: #c9303f12;
  --shadow: 0 24px 70px #1a223a33;
  --avatar-s: 60%;
  --avatar-l: 90%;
  --avatar-t: 34%;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; touch-action: manipulation; }
button:disabled { opacity: .45; cursor: not-allowed; }
a { color: inherit; text-decoration: none; }
h1, h2, h3, p, dl, dd { margin: 0; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr-only { position: fixed; width: 1px; height: 1px; opacity: 0; overflow: hidden; }
.mono, code { font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; }
.muted { color: var(--muted); }
.grow { flex: 1; }

.brand { display: inline-flex; align-items: center; gap: 10px; font-size: 16px; font-weight: 650; letter-spacing: -.2px; white-space: nowrap; }
.brand b { font-weight: 450; color: var(--muted); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 38px; padding: 0 14px; border-radius: 8px; background: var(--raised); border: 1px solid var(--line-strong); font-weight: 550; white-space: nowrap; transition: background .12s, border-color .12s; }
.btn:hover:not(:disabled) { background: var(--hover); }
.btn.primary { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.wide { width: 100%; }
.btn.square { width: 38px; padding: 0; flex: none; }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13px; }

.icon-btn { display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 8px; color: var(--muted); flex: none; transition: background .12s, color .12s; }
.icon-btn:hover:not(:disabled) { background: var(--hover); color: var(--text); }
.icon-btn.danger:hover:not(:disabled), .icon-btn.armed { background: var(--danger-soft); color: var(--danger); }
.icon-btn.spinning svg, #refresh.spinning svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Каркас */
.app { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr); height: 100dvh; overflow: hidden; }
.topbar { display: flex; align-items: center; gap: 24px; min-height: 60px; padding: 0 16px 0 20px; background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-actions { display: flex; align-items: center; gap: 4px; margin-left: auto; }
.topbar-actions form { display: contents; }
.topbar-note { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag { margin-right: 8px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--line-strong); color: var(--muted); font-size: 12px; white-space: nowrap; cursor: help; }
.stats { display: flex; gap: 28px; min-width: 0; }
.stats div { display: flex; flex-direction: column; }
.stats dt { color: var(--faint); font-size: 12px; line-height: 1.3; white-space: nowrap; }
.stats dd { font-size: 15px; font-weight: 620; font-variant-numeric: tabular-nums; white-space: nowrap; }
.address-pill { display: inline-flex; align-items: center; gap: 8px; min-width: 0; max-width: 46vw; height: 36px; padding: 0 12px; border-radius: 8px; background: var(--raised); border: 1px solid var(--line); font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 13.5px; }
.address-pill span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.address-pill svg { width: 15px; height: 15px; color: var(--faint); }
.address-pill:hover { border-color: var(--line-strong); }
.load-error { padding: 10px 20px; background: var(--danger-soft); color: var(--danger); }

.panes { display: grid; min-height: 0; }
.panes.three { grid-template-columns: 300px 370px minmax(0, 1fr); grid-row: 3; }
.panes.two { grid-template-columns: 400px minmax(0, 1fr); grid-row: 3; }
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface); }
.pane + .pane { border-left: 1px solid var(--line); }
.reader-pane { background: var(--bg); }
.scroll { flex: 1; min-height: 0; overflow: auto; scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }
.pane-head { display: flex; align-items: center; gap: 8px; min-height: 56px; padding: 8px 12px 8px 16px; border-bottom: 1px solid var(--line); flex: none; }
.pane-head h2 { font-size: 15px; font-weight: 620; }
.counter { color: var(--faint); font-weight: 500; margin-left: 2px; }
.live { display: inline-flex; align-items: center; gap: 7px; margin-left: auto; color: var(--faint); font-size: 12.5px; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.back { display: none; }
.reader-head { min-height: 52px; }
.pane-tools { display: flex; gap: 8px; padding: 12px 12px 8px; }
.search { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; height: 38px; padding: 0 11px; border-radius: 8px; background: var(--raised); border: 1px solid var(--line); color: var(--faint); }
.search:focus-within { border-color: var(--accent); }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: none; color: var(--text); }
.search input::placeholder { color: var(--faint); }

.tabs { display: flex; gap: 2px; padding: 0 12px; border-bottom: 1px solid var(--line); flex: none; }
.tabs button { padding: 9px 8px 10px; color: var(--muted); font-size: 13px; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 550; }
.tabs button span { color: var(--faint); font-weight: 400; font-size: 12px; }

.avatar { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 10px; background: hsl(var(--h) var(--avatar-s) var(--avatar-l)); color: hsl(var(--h) 70% var(--avatar-t)); font-size: 15px; font-weight: 650; flex: none; }

.address-row, .message-row { display: flex; gap: 12px; width: 100%; text-align: left; border-bottom: 1px solid var(--line); transition: background .1s; }
.address-row { align-items: center; padding: 11px 14px; }
.address-row:hover, .message-row:hover { background: var(--hover); }
.address-row.active, .message-row.active { background: var(--accent-soft); box-shadow: inset 3px 0 var(--accent); }
.address-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.address-main strong { font-weight: 570; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.address-main span { color: var(--faint); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { min-width: 24px; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-text); font-size: 12.5px; font-weight: 600; text-align: center; font-variant-numeric: tabular-nums; }

.message-row { padding: 13px 14px; }
.message-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.message-top { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.message-top strong { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-top time { color: var(--faint); font-size: 12.5px; white-space: nowrap; flex: none; }
.message-subject, .message-excerpt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-excerpt { color: var(--faint); font-size: 13px; }
.message-excerpt:empty { display: none; }
.code-chip { align-self: flex-start; margin-top: 6px; padding: 1px 8px; border-radius: 6px; background: var(--accent-soft); color: var(--accent-text); font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 13px; letter-spacing: .6px; }

.mailbox-head { padding-left: 16px; }
.mailbox-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.mailbox-title strong { font-weight: 620; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailbox-title span { color: var(--faint); font-size: 12.5px; }
.grant-bar { padding: 10px 12px; border-bottom: 1px solid var(--line); flex: none; }

.code-banner { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 12px; padding: 12px 14px; border-radius: 10px; background: var(--accent-soft); border: 1px solid var(--line-strong); flex: none; }
.code-banner span { display: block; color: var(--muted); font-size: 12.5px; }
.code-banner strong { font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 24px; font-weight: 650; letter-spacing: 2px; }

/* Письмо */
.reader-body { display: flex; flex-direction: column; }
.letter { display: flex; flex-direction: column; gap: 16px; width: 100%; max-width: 920px; min-height: 100%; margin: 0 auto; padding: 22px 28px 28px; }
.letter-subject { font-size: 21px; line-height: 1.3; font-weight: 640; letter-spacing: -.2px; overflow-wrap: anywhere; }
.letter-meta { display: flex; gap: 12px; align-items: flex-start; }
.letter-meta > div { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.letter-meta strong { font-weight: 600; }
.letter-meta span { color: var(--faint); font-size: 13px; overflow-wrap: anywhere; }
.letter-meta time { color: var(--faint); font-size: 13px; white-space: nowrap; }
.letter-codes { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 12px 14px; border-radius: 10px; background: var(--accent-soft); border: 1px solid var(--line-strong); }
.letter-codes > span { color: var(--muted); font-size: 13px; }
.letter-codes > div { display: flex; flex-wrap: wrap; gap: 8px; }
.code-copy { display: inline-flex; align-items: center; gap: 8px; padding: 5px 10px; border-radius: 8px; background: var(--surface); border: 1px solid var(--line-strong); font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 17px; font-weight: 600; letter-spacing: 1px; }
.code-copy svg { width: 15px; height: 15px; color: var(--faint); }
.code-copy:hover { border-color: var(--accent); }
.view-tabs { padding: 0; }
.letter-frame { flex: 1; width: 100%; min-height: 460px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.letter-text { flex: 1; margin: 0; padding: 18px; min-height: 200px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); font: inherit; line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere; }
.letter-text a { color: var(--accent-text); text-decoration: underline; }
.letter-headers { color: var(--faint); font-size: 13px; }
.letter-headers summary { cursor: pointer; }
.letter-headers dl { display: grid; grid-template-columns: max-content 1fr; gap: 3px 12px; margin-top: 8px; }
.letter-headers dd { overflow-wrap: anywhere; }

.empty { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; min-height: 100%; padding: 48px 24px; text-align: center; color: var(--faint); }
.empty h3 { color: var(--text); font-size: 15px; font-weight: 600; margin-top: 10px; }
.empty p { max-width: 280px; font-size: 13.5px; }
.empty-mark { display: grid; place-items: center; width: 52px; height: 52px; border-radius: 14px; background: var(--raised); border: 1px solid var(--line); color: var(--faint); }
.empty-mark svg { width: 24px; height: 24px; }
.skeleton { padding: 14px; }
.skeleton span { display: block; height: 58px; margin-bottom: 10px; border-radius: 8px; background: var(--raised); animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .5; } }

/* Диалоги */
dialog { width: 440px; max-width: calc(100vw - 24px); max-height: calc(100dvh - 24px); padding: 22px; color: var(--text); background: var(--surface); border: 1px solid var(--line-strong); border-radius: 14px; box-shadow: var(--shadow); }
dialog.wide-dialog { width: 560px; }
dialog::backdrop { background: #0008; backdrop-filter: blur(3px); }
dialog h2 { font-size: 18px; font-weight: 640; }
dialog label { display: block; margin: 14px 0 6px; color: var(--muted); font-size: 13px; }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 4px; }
.dialog-head .mono { overflow-wrap: anywhere; font-size: 13.5px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.field, .address-input, .link-form input, .link-form select, .login-card input { width: 100%; height: 40px; border-radius: 8px; border: 1px solid var(--line-strong); background: var(--bg); }
.field, .link-form input, .link-form select, .login-card input { padding: 0 12px; outline: 0; }
.field:focus, .address-input:focus-within, .link-form input:focus, .link-form select:focus, .login-card input:focus { border-color: var(--accent); }
.address-input { display: flex; align-items: center; overflow: hidden; }
.address-input input { flex: 1; min-width: 0; height: 100%; padding: 0 12px; border: 0; outline: 0; background: none; }
.address-input span { padding-right: 12px; color: var(--faint); white-space: nowrap; }
.hint { margin-top: 7px; color: var(--faint); font-size: 13px; overflow-wrap: anywhere; }
.form-error, .err { margin-top: 12px; padding: 10px 12px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); font-size: 13.5px; }
.segmented { display: flex; gap: 4px; margin: 14px 0 0; padding: 4px; border: 0; border-radius: 9px; background: var(--raised); }
.segmented label { flex: 1; margin: 0; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; }
.segmented span { display: block; padding: 6px; border-radius: 6px; color: var(--muted); text-align: center; }
.segmented input:checked + span { background: var(--surface); color: var(--text); font-weight: 550; box-shadow: 0 1px 2px #0003; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

.access-block { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.access-block h3 { font-size: 15px; font-weight: 620; }
.access-block > .muted { margin-top: 2px; font-size: 13.5px; }
.credential { display: flex; align-items: center; gap: 2px; min-height: 44px; margin-top: 8px; padding: 2px 4px 2px 12px; border-radius: 8px; background: var(--raised); }
.credential:first-of-type { margin-top: 12px; }
.credential span { width: 64px; flex: none; color: var(--faint); font-size: 13px; }
.credential code { flex: 1; min-width: 0; font-size: 13.5px; overflow-wrap: anywhere; }
.access-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.link-form { display: grid; grid-template-columns: minmax(0, 1fr) 118px auto; gap: 8px; margin-top: 12px; }
.link-list:not(:empty) { margin-top: 10px; }
.link-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.link-row > div { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.link-row strong { font-weight: 570; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-row > div span { color: var(--faint); font-size: 12.5px; }
.link-row.expired { opacity: .6; }
.link-icon { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; background: var(--accent-soft); color: var(--accent-text); flex: none; }
.link-icon svg { width: 16px; height: 16px; }

.toasts { position: fixed; right: 20px; bottom: 20px; z-index: 20; display: flex; flex-direction: column; gap: 8px; }
.toast { max-width: 340px; padding: 10px 14px; border-radius: 9px; background: var(--raised); border: 1px solid var(--line-strong); box-shadow: 0 10px 30px #0005; }
.toast.error { color: var(--danger); }

.dead-link { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 20px; background: var(--bg); }
.dead-card { display: flex; flex-direction: column; align-items: center; gap: 10px; max-width: 380px; text-align: center; }
.dead-card h1 { margin-top: 8px; font-size: 21px; font-weight: 640; }
.dead-card p { color: var(--muted); margin-bottom: 8px; }

/* Вход */
.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 20px; background: radial-gradient(ellipse 70% 50% at 50% 0, var(--accent-soft), transparent), var(--bg); }
.login-card { width: 100%; max-width: 400px; padding: 30px; border-radius: 16px; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); }
.login-card h1 { margin-top: 24px; font-size: 24px; font-weight: 650; letter-spacing: -.3px; }
.login-lead { margin-top: 2px; color: var(--muted); }
.login-card form { margin-top: 18px; }
.login-card label { display: block; margin: 14px 0 6px; color: var(--muted); font-size: 13px; }
.login-card .err { margin: 0 0 4px; }
.login-card .btn { margin-top: 20px; min-height: 42px; }
.login-note { margin-top: 18px; color: var(--faint); font-size: 13px; text-align: center; }
.login-theme { position: fixed; top: 14px; right: 14px; }

@media (max-width: 1240px) {
  .panes.three { grid-template-columns: 250px 310px minmax(0, 1fr); }
  .panes.two { grid-template-columns: 330px minmax(0, 1fr); }
  .stats { gap: 18px; }
  .letter { padding: 18px 20px 22px; }
}
@media (max-width: 980px) {
  .stats div:nth-child(n+3) { display: none; }
}
@media (max-width: 860px) {
  .topbar { gap: 10px; min-height: 54px; padding: 0 8px 0 12px; }
  .brand span { display: none; }
  .tag, .topbar-note, .stats div:nth-child(n+2) { display: none; }
  .panes.three, .panes.two { grid-template-columns: minmax(0, 1fr); }
  .pane { display: none; }
  .pane + .pane { border-left: 0; }
  .panes[data-pane=addresses] .addresses-pane, .panes[data-pane=messages] .messages-pane, .panes[data-pane=reader] .reader-pane { display: flex; }
  .back { display: inline-grid; }
  .mailbox-head { padding-left: 6px; }
  .reader-head { padding-left: 6px; }
  .letter { padding: 16px; }
  .letter-subject { font-size: 19px; }
  .letter-meta { flex-wrap: wrap; }
  .letter-meta > div { flex-basis: calc(100% - 50px); }
  .letter-meta time { margin-left: 50px; }
  .letter-frame { min-height: 62dvh; }
  .link-form { grid-template-columns: minmax(0, 1fr) 118px; }
  .link-form input { grid-column: 1 / -1; }
  .address-pill { max-width: none; min-width: 0; flex: 1; }
  .toasts { left: 12px; right: 12px; bottom: 12px; }
  .toast { max-width: none; }
  dialog { padding: 18px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
