:root {
  --bg: var(--tg-theme-bg-color, #17212b);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #232e3c);
  --text: var(--tg-theme-text-color, #f5f5f5);
  --hint: var(--tg-theme-hint-color, #708499);
  --link: var(--tg-theme-link-color, #6ab7ff);
  --button: var(--tg-theme-button-color, #5288c1);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e0564f;
  --ok: #4dab6d;
  --nav-h: 58px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
body {
  margin: 0; font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px;
}

/* Header */
header { padding: 12px 16px 8px; position: sticky; top: 0; background: var(--bg); z-index: 5; }
.htitle { font-weight: 700; font-size: 19px; }
.hsub { color: var(--hint); font-size: 12.5px; margin-top: 2px; min-height: 15px; }

/* Bottom navigation */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  display: flex; background: var(--secondary-bg);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  border-top: 1px solid rgba(255,255,255,.06);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 4px 0; border-radius: 10px; color: var(--hint); cursor: pointer;
  transition: transform .1s;
}
.tab:active { transform: scale(.92); }
.tab .ticon { font-size: 19px; line-height: 1; position: relative; }
.tab .tlabel { font-size: 9.5px; font-weight: 600; }
.tab.active { color: var(--link); }
.tabbadge {
  display: none; position: absolute; top: -4px; right: -12px; min-width: 15px; height: 15px;
  padding: 0 4px; border-radius: 8px; background: var(--danger); color: #fff;
  font-size: 9.5px; line-height: 15px; font-weight: 700; text-align: center;
}

main { padding: 2px 12px calc(var(--nav-h) + 30px + env(safe-area-inset-bottom)); }

/* Search */
input.search {
  margin-bottom: 10px; background: var(--secondary-bg);
  background-image: none; padding-left: 14px;
}

/* Stats */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stat { background: var(--secondary-bg); border-radius: 12px; padding: 12px 8px; text-align: center; }
.stat .n { font-size: 22px; font-weight: 700; }
.stat .l { font-size: 11px; color: var(--hint); margin-top: 2px; }

.progress { height: 6px; border-radius: 6px; background: rgba(255,255,255,.1); overflow: hidden; margin-top: 6px; }
.progress > i { display: block; height: 100%; background: var(--ok); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip { padding: 6px 12px; border-radius: 16px; background: var(--secondary-bg); color: var(--hint); font-size: 13px; cursor: pointer; }
.chip.on { background: var(--button); color: var(--button-text); }

.tgt { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--secondary-bg); font-size: 14px; }
.tgt .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
details { background: var(--secondary-bg); border-radius: 10px; margin-bottom: 8px; padding: 4px 12px; }
details > summary { cursor: pointer; padding: 8px 0; font-weight: 600; display: flex; align-items: center; gap: 8px; }
details > summary .dot { width: 9px; height: 9px; border-radius: 50%; }
.ctl { display: flex; gap: 8px; margin: 12px 0; }
.ctl button { flex: 1; padding: 10px; border: none; border-radius: 10px; font-weight: 600; cursor: pointer; background: var(--secondary-bg); color: var(--text); }
.ctl button.cancel { background: rgba(224,86,79,.18); color: var(--danger); }

/* Cards */
.card {
  background: var(--secondary-bg); border-radius: 13px; padding: 10px 12px; margin-bottom: 9px;
  display: flex; align-items: center; gap: 11px; transition: transform .08s;
}
.card.tappable { cursor: pointer; }
.card.tappable:active { transform: scale(.985); }
.card.unread { border-left: 3px solid var(--link); }
.card .body { flex: 1; min-width: 0; }
.card .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .sub { color: var(--hint); font-size: 13px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ntext { white-space: normal; line-height: 1.35; }

.avatar { width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 16px; overflow: hidden; position: relative; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar .type { position: absolute; right: -2px; bottom: -2px; font-size: 12px; background: var(--bg); border-radius: 50%; width: 18px; height: 18px; display: flex; align-items: center; justify-content: center; }
.navatar { width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 18px; background: var(--bg); }

.badge { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 20px; background: rgba(255,255,255,.08); color: var(--hint); margin-right: 4px; }
.badge.warn { background: rgba(224,86,79,.18); color: var(--danger); }
.badge.ok { background: rgba(77,171,109,.18); color: var(--ok); }
.badge.sim { background: rgba(106,183,255,.15); color: var(--link); font-weight: 600; }

.iconbtn { background: none; border: none; color: var(--hint); font-size: 20px; cursor: pointer; padding: 6px; }
.iconbtn.danger { color: var(--danger); }
.iconbtn.send { color: var(--link); }
.addbtn { border: none; border-radius: 18px; padding: 7px 15px; font-weight: 600; font-size: 14px; cursor: pointer; background: var(--button); color: var(--button-text); white-space: nowrap; }
.addbtn.added { background: rgba(77,171,109,.18); color: var(--ok); }

button.primary { width: 100%; padding: 13px; border: none; border-radius: 12px; background: var(--button); color: var(--button-text); font-size: 16px; font-weight: 600; cursor: pointer; }
button.primary:active { opacity: .85; }
button.primary:disabled { opacity: .5; }
button.ghost { width: 100%; padding: 12px; border: 1px dashed var(--hint); border-radius: 12px; background: none; color: var(--hint); font-size: 15px; cursor: pointer; margin-bottom: 12px; }
button.secondary { width: 100%; padding: 12px; border: none; border-radius: 12px; background: var(--secondary-bg); color: var(--text); font-size: 15px; font-weight: 600; cursor: pointer; margin-bottom: 8px; }
button.dangerbtn { width: 100%; padding: 12px; border: none; border-radius: 12px; background: rgba(224,86,79,.16); color: var(--danger); font-size: 15px; font-weight: 600; cursor: pointer; }
.row2 { display: flex; gap: 8px; }
.row2 > button { flex: 1; margin-bottom: 0; }

input, textarea, select {
  width: 100%; padding: 12px; border-radius: 10px; border: none; margin-bottom: 10px;
  background: var(--secondary-bg); color: var(--text); font-size: 15px; font-family: inherit;
}
.modal input, .modal textarea { background: var(--bg); }
textarea { min-height: 120px; resize: vertical; }
input[type="file"] { padding: 10px; }

.empty { text-align: center; color: var(--hint); padding: 40px 0; }
.seg { display: flex; gap: 6px; margin-bottom: 12px; }
.seg div { flex: 1; text-align: center; padding: 9px; border-radius: 10px; background: var(--secondary-bg); color: var(--hint); cursor: pointer; font-weight: 500; }
.modal .seg div { background: var(--bg); }
.seg div.on { background: var(--button) !important; color: var(--button-text); }

.check { display: flex; align-items: center; gap: 11px; padding: 9px 0; border-bottom: 1px solid var(--secondary-bg); }
.check input { width: auto; margin: 0; transform: scale(1.3); flex-shrink: 0; }
.check .body { flex: 1; min-width: 0; }

.kv { display: flex; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--secondary-bg); font-size: 14px; }
.kv .k { color: var(--hint); flex-shrink: 0; width: 108px; }
.kv .v { flex: 1; min-width: 0; word-break: break-word; }
.fulltext { white-space: pre-wrap; background: var(--secondary-bg); border-radius: 10px; padding: 12px; font-size: 13.5px; line-height: 1.4; max-height: 45vh; overflow-y: auto; margin-bottom: 12px; }
.modal .fulltext { background: var(--bg); }

.switch { position: relative; display: inline-block; width: 50px; height: 28px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; margin: 0; }
.slider { position: absolute; inset: 0; background: var(--bg); border-radius: 28px; transition: .2s; cursor: pointer; }
.slider:before { content: ""; position: absolute; height: 22px; width: 22px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: var(--ok); }
.switch input:checked + .slider:before { transform: translateX(22px); }

/* Bottom-sheet modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: none; align-items: flex-end; z-index: 20; }
.modal-bg.open { display: flex; }
.modal {
  width: 100%; background: var(--bg); border-radius: 16px 16px 0 0;
  padding: 8px 16px calc(22px + env(safe-area-inset-bottom));
  max-height: 90vh; overflow-y: auto; animation: sheetUp .18s ease-out;
}
@keyframes sheetUp { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.modal .handle { width: 38px; height: 4px; border-radius: 4px; background: var(--hint); opacity: .45; margin: 0 auto 12px; }
.modal h3 { margin: 0 0 14px; }

.toast { position: fixed; bottom: calc(var(--nav-h) + 18px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); background: var(--secondary-bg); color: var(--text); padding: 10px 16px; border-radius: 10px; opacity: 0; transition: opacity .2s; z-index: 30; font-size: 14px; max-width: 90%; text-align: center; box-shadow: 0 4px 18px rgba(0,0,0,.35); }
.toast.show { opacity: 1; }
.spin { text-align: center; color: var(--hint); padding: 20px 0; }

/* Session-expired overlay (covers the whole app; above nav/toast/modal). */
.expired-overlay { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 24px; }
.expired-box { text-align: center; max-width: 320px; }
.expired-box .expired-emoji { font-size: 48px; margin-bottom: 12px; }
.expired-box .expired-title { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.expired-box .expired-sub { color: var(--hint); line-height: 1.45; margin-bottom: 20px; }