:root {
  color-scheme: light dark;
  --bg: #f6f6f4; --panel: #ffffff; --panel-2: #f0f0ec; --line: #dedfd9; --text: #1c1d1a; --muted: #62645c;
  --accent: #2f5d50; --accent-text: #ffffff; --accent-soft: #e2ece8; --danger: #a13a2c; --warn-bg: #f7efd9; --warn-text: #5e4a12; --unread: #17382f;
  --radius: 10px; --shadow: 0 8px 28px rgba(20, 22, 18, 0.14);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171a; --panel: #1d2023; --panel-2: #26292d; --line: #33373b; --text: #eceee9; --muted: #a2a69e;
    --accent: #7fbfa9; --accent-text: #10201b; --accent-soft: #24352f; --danger: #ef8e7f; --warn-bg: #3a3218; --warn-text: #efdba4; --unread: #d7efe6;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; overflow-wrap: anywhere; }
h1, h2, h3 { line-height: 1.2; margin: 0; font-weight: 650; }
h1 { font-size: 22px; } h2 { font-size: 18px; } h3 { font-size: 15px; }
p { margin: 0; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%; min-width: 0; }
textarea { resize: vertical; min-height: 180px; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="checkbox"] { width: 18px; height: 18px; flex: none; accent-color: var(--accent); }

.btn { font: inherit; font-weight: 550; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 13px; cursor: pointer; white-space: nowrap; }
.btn:hover:not(:disabled) { background: var(--panel-2); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-quiet { background: transparent; border-color: transparent; }
.btn-small { padding: 4px 9px; font-size: 13px; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; text-decoration: underline; }

.notice { background: var(--warn-bg); color: var(--warn-text); border-radius: 8px; padding: 9px 12px; font-size: 14px; }
.error { color: var(--danger); font-size: 14px; }

.toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 60; width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: 14px; }
.toast-bad { background: var(--danger); color: #fff; }

/* Sign-in pages */
.auth-page { min-height: 100%; display: flex; flex-direction: column; align-items: center; padding: 40px 16px; gap: 18px; }
.auth-brand { display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; }
.brand { white-space: nowrap; overflow-wrap: normal; font-size: 22px; font-weight: 700; letter-spacing: -0.01em; color: var(--accent); text-decoration: none; }
.auth-card { width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.tabs { display: flex; gap: 4px; background: var(--panel-2); padding: 3px; border-radius: 9px; }
.tabs button { flex: 1; border: 0; background: transparent; font: inherit; font-weight: 550; color: var(--muted); padding: 7px 8px; border-radius: 7px; cursor: pointer; }
.tabs button { overflow-wrap: normal; min-width: 0; }
.tabs button[aria-selected="true"] { background: var(--panel); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }
.address-input { display: flex; align-items: center; gap: 6px; }
.address-input input { flex: 1; }
.address-input select { width: auto; max-width: 55%; }
.address-input .at { color: var(--muted); }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; }
.codes { list-style: none; margin: 0; padding: 12px; display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 6px; background: var(--panel-2); border-radius: 8px; font: 14px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; overflow-wrap: normal; }
.qr { width: 180px; height: 180px; border-radius: 8px; align-self: center; display: block; margin: 0 auto; }
.secret { font: 14px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--panel-2); padding: 8px 10px; border-radius: 8px; }
.auth-foot { display: flex; gap: 14px; align-items: center; font-size: 13px; color: var(--muted); }

/* Mail */
.mail-app { display: grid; grid-template-rows: auto minmax(0, 1fr); grid-template-columns: minmax(0, 1fr); height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); min-width: 0; }
.topbar .search { flex: 1; max-width: 560px; min-width: 0; }
.mail-body { display: grid; grid-template-columns: 240px minmax(300px, 400px) minmax(0, 1fr); min-height: 0; }
.sidebar { border-right: 1px solid var(--line); padding: 12px 10px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; min-height: 0; }
.nav { display: flex; flex-direction: column; gap: 1px; }
.nav button { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; border: 0; background: transparent; font: inherit; color: var(--text); padding: 7px 10px; border-radius: 8px; cursor: pointer; min-width: 0; }
.nav button:hover { background: var(--panel-2); }
.nav button[aria-current="true"] { background: var(--accent-soft); font-weight: 600; }
.nav .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; overflow-wrap: normal; }
.nav .count { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.side-title { font-size: 12px; font-weight: 600; color: var(--muted); padding: 0 10px 4px; text-transform: uppercase; letter-spacing: 0.04em; }
.side-backdrop { display: none; }
.side-actions { display: flex; flex-direction: column; gap: 6px; padding: 0 10px; }
.quota { padding: 0 10px; font-size: 12px; color: var(--muted); margin-top: auto; }
.bar { height: 5px; background: var(--panel-2); border-radius: 3px; overflow: hidden; margin-top: 5px; }
.bar i { display: block; height: 100%; background: var(--accent); }

.list-pane { border-right: 1px solid var(--line); overflow-y: auto; min-height: 0; background: var(--panel); }
.msg { display: grid; grid-template-columns: 1fr auto; gap: 1px 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; width: 100%; background: none; border-left: 3px solid transparent; border-top: 0; border-right: 0; font: inherit; color: inherit; text-align: left; }
.msg:hover { background: var(--panel-2); }
.msg[aria-current="true"] { background: var(--accent-soft); }
.msg.unread { border-left-color: var(--accent); }
.msg.unread .who, .msg.unread .subj { font-weight: 650; color: var(--unread); }
.msg .who, .msg .subj, .msg .snip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; overflow-wrap: normal; min-width: 0; }
.msg .when { font-size: 12px; color: var(--muted); }
.msg .snip { grid-column: 1 / -1; font-size: 13px; color: var(--muted); }
.msg .subj { grid-column: 1 / -1; font-size: 14px; }
.chip { display: inline-block; font-size: 11px; line-height: 1.5; padding: 0 6px; background: var(--panel-2); color: var(--muted); border-radius: 5px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; overflow-wrap: normal; vertical-align: middle; }
.msg .tags { grid-column: 1 / -1; display: flex; gap: 4px; align-items: center; min-width: 0; margin-top: 2px; }
.empty { padding: 40px 18px; text-align: center; color: var(--muted); }
.more { padding: 12px; display: flex; justify-content: center; }

.reader { overflow-y: auto; min-height: 0; display: flex; flex-direction: column; min-width: 0; }
.reader-head { padding: 16px 20px 12px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--line); background: var(--panel); }
.reader-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.reader-meta { font-size: 13px; color: var(--muted); display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 2px 8px; }
.reader-meta b { font-weight: 550; color: var(--text); }
.reader-body { padding: 16px 20px; display: flex; flex-direction: column; gap: 12px; flex: 1; }
.letter-frame { width: 100%; border: 0; background: #fff; border-radius: 8px; min-height: 120px; color-scheme: light; }
.letter-text { white-space: pre-wrap; margin: 0; font: inherit; }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; }
.star { background: none; border: 0; cursor: pointer; font-size: 16px; color: var(--muted); padding: 0 2px; line-height: 1; }
.star[aria-pressed="true"] { color: #c98a12; }

dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); padding: 0; width: min(680px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(10, 12, 10, 0.45); }
.dialog-head { display: flex; align-items: center; gap: 8px; padding: 14px 18px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.dialog-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }
.dialog-foot { display: flex; align-items: center; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.file-list { display: flex; flex-wrap: wrap; gap: 6px; font-size: 13px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--panel-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td input[type="text"] { padding: 5px 8px; font-size: 13px; min-width: 110px; }
.section { display: flex; flex-direction: column; gap: 10px; }
.section + .section { border-top: 1px solid var(--line); padding-top: 16px; }

/* Admin */
.admin-page { max-width: 1040px; margin: 0 auto; padding: 18px 16px 60px; display: flex; flex-direction: column; gap: 18px; }
.admin-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; }
.stat b { display: block; font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.switch-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.status-pill { white-space: nowrap; font-size: 13px; font-weight: 600; padding: 2px 10px; border-radius: 99px; background: var(--panel-2); }
.status-pill.on { background: var(--accent-soft); color: var(--accent); }
.status-pill.off { background: var(--warn-bg); color: var(--warn-text); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 12px; }

/* Legal pages */
.doc { max-width: 720px; margin: 0 auto; padding: 32px 16px 60px; display: flex; flex-direction: column; gap: 12px; }
.doc h2 { margin-top: 12px; }

@media (max-width: 900px) {
  .mail-body { grid-template-columns: minmax(0, 1fr); }
  .topbar { flex-wrap: wrap; padding: 8px 16px; gap: 8px; }
  .topbar .search { order: 5; flex: 1 0 100%; max-width: none; }
  .topbar .btn-wide { display: none; }
  .sidebar { display: none; border: 0; }
  .sidebar.open { display: flex; position: fixed; top: 0; left: 0; bottom: 0; width: min(84vw, 340px); height: 100dvh; background: var(--panel); z-index: 20; box-shadow: var(--shadow); }
  .side-backdrop.show { display: block; position: fixed; inset: 0; background: rgba(10, 12, 10, 0.5); z-index: 19; }
  .side-head { display: flex; align-items: center; justify-content: space-between; padding: 0 2px 0 10px; }
  .side-close { font-size: 20px; line-height: 1; min-width: 44px; min-height: 44px; }
  #menu { font-size: 22px; line-height: 1; min-width: 44px; min-height: 44px; padding: 0; }
  .nav button { padding: 11px 10px; }
  .list-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 16px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 2; }
  .list-where { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; overflow-wrap: normal; }
  input, select, textarea { font-size: 16px; }
  .btn-small { padding: 9px 12px; font-size: 14px; }
  .star { font-size: 22px; padding: 2px 4px; }
  .msg { padding: 12px 16px; }
  dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border: 0; border-radius: 0; }
  dialog > form { min-height: 100%; display: flex; flex-direction: column; }
  dialog > form > .dialog-body { flex: 1; }
  dialog textarea { min-height: 40dvh; }
  .dialog-head { padding: 10px 16px; }
  .dialog-body { padding: 14px 16px; }
  .dialog-foot { padding: 10px 16px; position: sticky; bottom: 0; background: var(--panel); }
  .list-pane { border: 0; }
  .mail-app.reading .list-pane { display: none; }
  .mail-app:not(.reading) .reader { display: none; }
}
@media (min-width: 901px) {
  .only-small { display: none !important; }
}
@media (max-width: 600px) {
  .tabs button { font-size: 14px; padding: 7px 4px; }
  .table-cards thead { display: none; }
  .table-cards tr { display: block; padding: 10px 12px; border-bottom: 1px solid var(--line); }
  .table-cards tr:last-child { border-bottom: 0; }
  .table-cards td { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 5px 0; border: 0; }
  .table-cards td[data-label]::before { content: attr(data-label); font-size: 12px; color: var(--muted); flex: none; max-width: 40%; }
  .table-cards td input[type="text"] { flex: 1; min-width: 0; max-width: 60%; font-size: 16px; }
  .table-cards .cell-title { font-weight: 600; }
  .table-cards td:empty { display: none; }
}
@media (max-width: 480px) {
  .reader-head, .reader-body { padding-left: 16px; padding-right: 16px; }
  .auth-card { padding: 18px 16px; }
}
