:root {
  --bg: #f6f8fc;
  --surface: #ffffff;
  --surface-2: #eef2f8;
  --hover: #e8edf5;
  --selected: #d3e3fd;
  --text: #1f1f1f;
  --muted: #5f6368;
  --border: #e0e3e8;
  --accent: #0b57d0;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --ok: #146c2e;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1b1f24;
    --surface-2: #232830;
    --hover: #2a3039;
    --selected: #24395c;
    --text: #e6e8eb;
    --muted: #9aa0a6;
    --border: #2f353d;
    --accent: #a8c7fa;
    --accent-text: #0b1d3a;
    --danger: #f2b8b5;
    --ok: #6dd58c;
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
}
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 8px 16px;
  cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.primary.danger-fill { background: var(--danger); color: var(--surface); }
.btn:disabled { opacity: 0.6; cursor: default; }
.icon-btn {
  border: 0; background: transparent; border-radius: 50%;
  width: 36px; height: 36px; cursor: pointer; display: inline-grid; place-items: center;
  color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn svg, .btn svg { width: 20px; height: 20px; fill: currentColor; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--muted); }
.input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); outline: none;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 25%, transparent); }
.error { color: var(--danger); min-height: 20px; margin: 4px 0 10px; }
.brand { font-weight: 600; font-size: 20px; letter-spacing: -0.2px; display: flex; align-items: center; gap: 8px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-weight: 700; font-size: 15px;
}

/* ---------- Login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 400px; background: var(--surface); border-radius: 20px;
  padding: 36px 32px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 22px; font-weight: 500; margin: 24px 0 4px; }
.login-card p { color: var(--muted); margin: 0 0 24px; }
.login-card .btn { width: 100%; padding: 11px; }

/* ---------- App shell ---------- */
.app { display: grid; grid-template-rows: 64px 1fr; height: 100%; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 0 16px; }
.topbar .search { flex: 1; max-width: 720px; position: relative; }
.topbar .search input {
  width: 100%; border: 0; border-radius: 999px; padding: 12px 16px 12px 44px; background: var(--surface-2); outline: none;
}
.topbar .search input:focus { background: var(--surface); box-shadow: var(--shadow); }
.topbar .search svg { position: absolute; left: 14px; top: 11px; width: 20px; fill: var(--muted); }
.topbar .spacer { flex: 1; }
.account { position: relative; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
.menu {
  position: absolute; right: 0; top: 44px; background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); min-width: 240px; padding: 8px; z-index: 20;
}
.menu .who { padding: 10px 12px; color: var(--muted); border-bottom: 1px solid var(--border); margin-bottom: 6px; word-break: break-all; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.menu button:hover { background: var(--hover); }

.body { display: grid; grid-template-columns: 240px 1fr; min-height: 0; }
.sidebar { padding: 8px 12px 8px 8px; overflow-y: auto; }
.compose-btn {
  display: inline-flex; align-items: center; gap: 12px; border: 0; cursor: pointer;
  background: var(--selected); color: var(--text); padding: 16px 24px 16px 18px; border-radius: 16px;
  font-weight: 500; margin: 0 0 16px 4px;
}
.compose-btn:hover { box-shadow: var(--shadow); }
.compose-btn svg { width: 22px; fill: currentColor; }
.folder {
  display: flex; align-items: center; gap: 16px; width: 100%; border: 0; background: none; cursor: pointer;
  padding: 0 12px 0 24px; height: 34px; border-radius: 0 999px 999px 0; text-align: left;
}
.folder svg { width: 20px; fill: var(--muted); }
.folder:hover { background: var(--hover); }
.folder.active { background: var(--selected); font-weight: 600; }
.folder .count { margin-left: auto; font-size: 12px; font-weight: 600; }

.main { background: var(--surface); border-radius: 16px; margin: 0 16px 16px 0; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.toolbar { display: flex; align-items: center; gap: 4px; padding: 8px 12px; border-bottom: 1px solid var(--border); min-height: 52px; }
.toolbar .pager { margin-left: auto; color: var(--muted); display: flex; align-items: center; gap: 4px; }
.list { overflow-y: auto; flex: 1; }
.row {
  display: grid; grid-template-columns: 32px 200px 1fr auto; align-items: center; gap: 8px;
  padding: 0 16px 0 12px; height: 42px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.row:hover { box-shadow: inset 1px 0 0 var(--border), inset -1px 0 0 var(--border), 0 1px 2px rgba(0,0,0,.15); z-index: 1; position: relative; }
.row.unread { font-weight: 700; background: var(--surface); }
.row:not(.unread) { background: var(--surface-2); }
.row.checked { background: var(--selected); }
.row .who, .row .subj { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .date { font-size: 12px; color: var(--muted); white-space: nowrap; display: flex; gap: 6px; align-items: center; }
.row.unread .date { color: var(--text); }
.row .date svg { width: 16px; fill: var(--muted); }
.empty { padding: 48px; text-align: center; color: var(--muted); }

.reader { overflow-y: auto; flex: 1; padding: 20px 28px 40px; }
.reader h2 { font-size: 22px; font-weight: 400; margin: 4px 0 20px; word-break: break-word; }
.meta { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 20px; }
.meta .avatar { flex: none; cursor: default; }
.meta .lines { flex: 1; min-width: 0; }
.meta .from { font-weight: 600; }
.meta .from span { font-weight: 400; color: var(--muted); }
.meta .sub { color: var(--muted); font-size: 12px; word-break: break-all; }
.meta .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.msg-frame { width: 100%; border: 0; min-height: 200px; background: #fff; border-radius: 8px; }
.msg-text { white-space: pre-wrap; word-break: break-word; line-height: 1.5; }
.attachments { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); }
.attachment {
  display: flex; gap: 10px; align-items: center; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 14px; text-decoration: none; color: var(--text); max-width: 260px;
}
.attachment:hover { background: var(--hover); }
.attachment .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment .size { color: var(--muted); font-size: 12px; }
.reply-bar { display: flex; gap: 8px; margin-top: 28px; }

/* ---------- Compose ---------- */
.compose {
  position: fixed; right: 24px; bottom: 0; width: 560px; max-width: 100vw; height: 560px; max-height: calc(100vh - 40px);
  background: var(--surface); border-radius: 12px 12px 0 0; box-shadow: var(--shadow); display: flex; flex-direction: column; z-index: 30;
}
.compose header { display: flex; align-items: center; padding: 8px 8px 8px 16px; background: var(--surface-2); border-radius: 12px 12px 0 0; font-weight: 500; }
.compose header .icon-btn { margin-left: auto; }
.compose .line { display: flex; align-items: center; border-bottom: 1px solid var(--border); margin: 0 16px; }
.compose .line label { color: var(--muted); width: 56px; }
.compose .line input { flex: 1; border: 0; outline: none; padding: 10px 0; background: transparent; }
.compose .line .link { border: 0; background: none; color: var(--muted); cursor: pointer; }
.compose textarea { flex: 1; border: 0; outline: none; resize: none; padding: 12px 16px; background: transparent; line-height: 1.5; }
.compose footer { display: flex; align-items: center; gap: 8px; padding: 12px 16px; flex-wrap: wrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 8px; }
.chip { background: var(--surface-2); border-radius: 6px; padding: 4px 8px; font-size: 12px; display: flex; gap: 6px; align-items: center; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0; }

/* ---------- Dialog / toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 40; padding: 16px; }
.dialog { background: var(--surface); border-radius: 20px; padding: 24px; width: 100%; max-width: 420px; box-shadow: var(--shadow); }
.dialog h3 { margin: 0 0 16px; font-weight: 500; font-size: 20px; }
.dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.toast {
  position: fixed; left: 24px; bottom: 24px; background: #323232; color: #fff; padding: 12px 20px;
  border-radius: 8px; z-index: 50; box-shadow: var(--shadow); max-width: calc(100vw - 48px);
}

/* ---------- Admin ---------- */
.admin { max-width: 1000px; margin: 0 auto; padding: 0 16px 40px; }
.card { background: var(--surface); border-radius: 16px; padding: 20px 24px; margin-bottom: 16px; }
.card h2 { font-size: 16px; font-weight: 600; margin: 0 0 16px; }
.create-form { display: grid; grid-template-columns: 1fr 1fr auto; gap: 12px; align-items: end; }
.create-form .field { margin: 0; }
.addr { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.addr input { border: 0; flex: 1; min-width: 0; padding: 10px 12px; outline: none; background: transparent; }
.addr span { color: var(--muted); padding-right: 12px; }
.stats { display: flex; gap: 32px; flex-wrap: wrap; }
.stat .num { font-size: 26px; font-weight: 600; }
.stat .lbl { color: var(--muted); font-size: 13px; }
.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin-top: 16px; }
.bar div { height: 100%; background: var(--accent); }
.bar.warn div { background: var(--danger); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: 13px; }
td.actions { text-align: right; white-space: nowrap; }
td.actions .btn { padding: 5px 12px; font-size: 13px; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); }
.badge.on { color: var(--ok); }
.badge.off { color: var(--danger); }
.table-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.table-head .input { max-width: 260px; }

/* ---------- Mobile ---------- */
.only-mobile { display: none; }
@media (max-width: 760px) {
  .only-mobile { display: inline-grid; }
  .topbar .brand-text { display: none; }
  .body { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 64px; left: 0; bottom: 0; width: 260px; background: var(--bg);
    z-index: 25; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .main { margin: 0 8px 8px; }
  .row { grid-template-columns: 28px 1fr auto; grid-template-rows: auto auto; height: auto; padding: 8px 12px 8px 8px; row-gap: 2px; }
  .row .check { grid-row: span 2; }
  .row .subj { grid-column: 2 / 4; font-size: 13px; }
  .reader { padding: 16px; }
  .compose { right: 0; width: 100vw; height: 100%; max-height: 100%; border-radius: 0; }
  .compose header { border-radius: 0; }
  .create-form { grid-template-columns: 1fr; }
  .hide-mobile { display: none; }
}
