:root {
  --bg: #0a0a0c;
  --card: #141418;
  --card-hover: #1a1a20;
  --border: rgba(255,255,255,0.07);
  --text: #f2f2f7;
  --muted: #8e8e93;
  --accent: #ffffff;
  --accent-soft: rgba(255,255,255,0.12);
  --success: #32d74b;
  --warn: #ff9f0a;
  --danger: #ff453a;
  --nav-h: 58px;
  --nav-offset: calc(var(--nav-h) + max(16px, env(safe-area-inset-bottom)) + 8px);
  --top-h: 48px;
  --radius: 18px;
  --radius-sm: 12px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

.topbar {
  height: var(--top-h);
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: center;
  justify-content: center;
  padding-left: 12px;
  padding-right: 12px;
  flex-shrink: 0;
  z-index: 20;
}

.topbar-title {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.15px;
  opacity: 0.92;
}

.icon-btn {
  position: absolute;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .18s var(--ease), transform .18s var(--ease);
}
.icon-btn:active { transform: scale(0.96); }
#btn-back { left: 12px; }
#btn-more { right: 12px; }
#btn-settings { right: 12px; }

.ico, .nav-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
}
.ico svg, .nav-icon svg { width: 100%; height: 100%; display: block; }

.hidden { display: none !important; }

.main-scroll {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.view {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 16px var(--nav-offset);
  opacity: 0;
  pointer-events: none;
  transform: translateY(6px);
  transition: opacity .24s var(--ease), transform .24s var(--ease);
  -webkit-overflow-scrolling: touch;
  will-change: opacity, transform;
}
.view.no-nav { padding-bottom: max(20px, env(safe-area-inset-bottom)); }

.view.active {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 2px 0 14px;
}

.page-title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 10px;
}

.card-plus {
  background: linear-gradient(145deg, #1e1812 0%, #121014 100%);
  border-color: rgba(255,180,100,0.12);
  display: flex;
  gap: 14px;
  align-items: center;
}
.card-plus .ico { width: 28px; height: 28px; color: #d4a574; }
.card-plus .title { font-weight: 600; font-size: 15px; }
.card-plus .sub { color: var(--muted); font-size: 12px; margin-top: 3px; line-height: 1.4; }

.stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  min-height: 88px;
}

.stat-value {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.8px;
  line-height: 1;
}

.stat-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.5px;
  margin-top: 8px;
  text-transform: uppercase;
}

.stat-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 5px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 12px;
}
.section-head h3 { font-size: 14px; font-weight: 600; }
.section-head span { font-size: 12px; color: var(--muted); }

.chart-wrap {
  height: 108px;
  display: flex;
  align-items: flex-end;
  gap: 4px;
}

.chart-bar-group {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.chart-bars {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 2px;
}

.bar-in, .bar-out {
  width: 44%;
  border-radius: 3px 3px 0 0;
  min-height: 3px;
  transition: height .45s var(--ease);
}
.bar-in { background: rgba(255,255,255,0.92); }
.bar-out { background: rgba(255,255,255,0.28); }

.chart-label { font-size: 9px; color: var(--muted); }

.legend {
  display: flex;
  gap: 16px;
  margin-top: 10px;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
}
.dot { width: 7px; height: 7px; border-radius: 50%; }

.row-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  transition: background .18s var(--ease);
}
.row-link:active { opacity: 0.85; }
.row-link .left { display: flex; gap: 12px; align-items: center; }
.row-link .ico { color: var(--text); opacity: 0.9; }
.row-link .title { font-weight: 600; font-size: 14px; }
.row-link .sub { color: var(--muted); font-size: 12px; margin-top: 3px; line-height: 1.35; }
.chevron { color: var(--muted); opacity: 0.7; }

.bottom-nav {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(10px, env(safe-area-inset-bottom));
  width: min(420px, calc(100% - 20px));
  height: var(--nav-h);
  background: rgba(18,18,22,0.94);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: 999px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 6px;
  gap: 4px;
  z-index: 30;
}

.nav-item {
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: all .22s var(--ease);
  padding: 0 8px;
  min-width: 0;
  height: 100%;
}
.nav-item .nav-icon { width: 20px; height: 20px; flex-shrink: 0; }
.nav-item .nav-label {
  font-size: 12px;
  font-weight: 600;
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  white-space: nowrap;
  transition: max-width .22s var(--ease), opacity .18s var(--ease);
}
.nav-item.active {
  background: #fff;
  color: #111;
  padding: 0 14px;
}
.nav-item.active .nav-label {
  max-width: 80px;
  opacity: 1;
}

.ach-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.ach-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-height: 138px;
  display: flex;
  flex-direction: column;
}
.ach-card .ico { width: 24px; height: 24px; margin-bottom: 10px; color: var(--muted); }
.ach-title { font-weight: 600; font-size: 14px; }
.ach-sub { color: var(--muted); font-size: 11px; margin-top: 3px; line-height: 1.35; flex: 1; }

.progress-track {
  height: 3px;
  background: rgba(255,255,255,0.08);
  border-radius: 99px;
  margin: 10px 0 5px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: #fff;
  border-radius: 99px;
  transition: width .5s var(--ease);
}
.progress-text { font-size: 10px; color: var(--muted); }

.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item .name { font-weight: 600; font-size: 14px; }
.list-item .meta { color: var(--muted); font-size: 12px; margin-top: 3px; }

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 5px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  flex-shrink: 0;
}
.badge.wait { color: var(--warn); background: rgba(255,159,10,0.12); }
.badge.risk { color: var(--danger); background: rgba(255,69,58,0.12); }

.group-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.6px;
  padding: 10px 0 4px;
  text-transform: uppercase;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.setting-row:last-child { border-bottom: none; }
.setting-row .label { font-weight: 600; font-size: 14px; }
.setting-row .desc { color: var(--muted); font-size: 11px; margin-top: 3px; line-height: 1.35; max-width: 240px; }

.toggle {
  width: 48px;
  height: 28px;
  border-radius: 999px;
  background: #3a3a3e;
  border: none;
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .2s var(--ease);
}
.toggle.on { background: #fff; }
.toggle::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  transition: transform .22s var(--ease), background .22s var(--ease);
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.toggle.on::after { transform: translateX(20px); background: #111; }

.pill {
  padding: 5px 9px;
  border-radius: 999px;
  background: rgba(255,255,255,0.06);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  flex-shrink: 0;
}
.pill.ok { color: var(--success); }

.input, .textarea {
  width: 100%;
  margin-top: 6px;
  background: #0d0d10;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 11px 12px;
  font: inherit;
  font-size: 13px;
  transition: border-color .18s var(--ease);
}
.input:focus, .textarea:focus { outline: none; border-color: rgba(255,255,255,0.18); }
.textarea { min-height: 76px; resize: none; }

.feature-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 8px;
}
.feature-item h4 { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.feature-item p { color: var(--muted); font-size: 12px; line-height: 1.45; }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 36px 12px;
  font-size: 13px;
  line-height: 1.5;
}

.retry-btn {
  display: block;
  margin: 16px auto 0;
  padding: 12px 24px;
  border: none;
  border-radius: 999px;
  background: #fff;
  color: #111;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.retry-btn:active { opacity: 0.85; }

.toast {
  position: fixed;
  left: 50%;
  bottom: var(--nav-offset);
  transform: translateX(-50%) translateY(8px);
  background: rgba(24,24,28,0.96);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 12px;
  z-index: 100;
  opacity: 0;
  transition: opacity .2s var(--ease), transform .2s var(--ease);
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.skeleton {
  background: linear-gradient(90deg, #141418 25%, #1c1c22 50%, #141418 75%);
  background-size: 200% 100%;
  animation: shimmer 1s infinite linear;
  border-radius: var(--radius-sm);
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.app-loading .view-home-skeleton { display: block; }

/* Avatars */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex-shrink: 0;
  background: hsl(var(--av-hue, 220), 55%, 42%);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.12);
  overflow: hidden;
}
.avatar-letter {
  font-weight: 700;
  color: #fff;
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
}
.avatar-md { width: 44px; height: 44px; }
.avatar-md .avatar-letter { font-size: 17px; }
.avatar-lg { width: 48px; height: 48px; }
.avatar-lg .avatar-letter { font-size: 19px; }
.avatar-xl { width: 72px; height: 72px; margin: 0 auto 10px; }
.avatar-xl .avatar-letter { font-size: 28px; }

/* Chat list */
.card-flush { padding: 4px 0; }
.chat-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 11px 16px;
  border: none;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.chat-row:last-child { border-bottom: none; }
.chat-row:active { background: var(--card-hover); }
.chat-row-body { flex: 1; min-width: 0; }
.chat-row-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.chat-row-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row-name .muted { color: var(--muted); font-weight: 500; font-size: 12px; }
.chat-row-time { color: var(--muted); font-size: 11px; flex-shrink: 0; }
.chat-row-preview { color: var(--muted); font-size: 12px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row-tag { color: var(--danger); font-size: 10px; margin-top: 4px; font-weight: 600; }

/* Streak list (horizontal rows) */
.streak-list { padding: 4px 0; }
.streak-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 11px 16px;
  border: none;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.streak-row:last-child { border-bottom: none; }
.streak-row:active { background: var(--card-hover); }
.streak-row.risk { background: rgba(255,69,58,0.04); }
.streak-row-body { flex: 1; min-width: 0; }
.streak-row-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.streak-row-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.streak-row-name .muted { color: var(--muted); font-weight: 500; font-size: 12px; }
.streak-row-meta { color: var(--muted); font-size: 11px; margin-top: 3px; }
.streak-row-days {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.5px;
  line-height: 1;
  flex-shrink: 0;
  min-width: 44px;
  text-align: right;
}
.streak-row-days span { font-size: 11px; font-weight: 600; opacity: 0.55; margin-left: 2px; }

.streak-hero { text-align: center; padding: 20px 16px; }
.streak-hero-name { font-size: 18px; font-weight: 700; margin-top: 4px; }
.streak-hero-days { font-size: 36px; font-weight: 800; margin-top: 6px; letter-spacing: -1px; }
.streak-hero-days span { font-size: 14px; font-weight: 600; opacity: 0.6; }
.streak-hero-sub { color: var(--muted); font-size: 12px; margin-top: 6px; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 8px;
}
.detail-val { font-size: 20px; font-weight: 700; }
.detail-lbl { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* Chat thread (Telegram-like) */
#view-chat-thread.active {
  display: flex;
  flex-direction: column;
  padding: 0 0 max(8px, env(safe-area-inset-bottom));
  overflow: hidden;
}
.thread-loading { padding: 24px; text-align: center; color: var(--muted); }
.thread-stats { margin: 8px 12px 0; padding: 12px 14px; flex-shrink: 0; }
.thread-stats-row { display: flex; justify-content: space-around; text-align: center; }
.thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}
.thread-compose {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 8px 12px 10px;
  border-top: 1px solid var(--border);
  background: rgba(10,10,12,0.96);
  flex-shrink: 0;
}
.thread-input {
  flex: 1;
  resize: none;
  max-height: 100px;
  background: #141418;
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 10px 14px;
  color: var(--text);
  font: inherit;
  font-size: 14px;
}
.thread-input:focus { outline: none; border-color: rgba(255,255,255,0.15); }
.thread-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: #fff;
  color: #111;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.msg-row { display: flex; margin-bottom: 8px; }
.msg-row.in { justify-content: flex-start; }
.msg-row.out { justify-content: flex-end; }
.msg-bubble {
  max-width: 82%;
  padding: 8px 11px 6px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.4;
  word-break: break-word;
}
.msg-row.in .msg-bubble { background: #1c1c22; border-bottom-left-radius: 4px; }
.msg-row.out .msg-bubble { background: linear-gradient(135deg, #3d3d8a, #2a2a62); border-bottom-right-radius: 4px; }
.msg-bubble.has-media { padding: 6px 6px 4px; }
.msg-bubble.has-media .msg-text { padding: 4px 6px 0; }
.msg-reply {
  margin-bottom: 6px;
  padding: 6px 8px 6px 10px;
  border-left: 3px solid rgba(255,255,255,0.35);
  border-radius: 8px;
  background: rgba(0,0,0,0.18);
}
.msg-row.out .msg-reply { border-left-color: rgba(255,255,255,0.55); }
.msg-reply-text {
  font-size: 12px;
  line-height: 1.35;
  color: rgba(255,255,255,0.78);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-media {
  display: block;
  max-width: 100%;
  border-radius: 10px;
}
.msg-media-img { max-height: 280px; object-fit: contain; }
.msg-media-video { max-height: 240px; width: 100%; background: #000; }
.msg-media-round {
  width: 180px;
  height: 180px;
  border-radius: 50%;
  object-fit: cover;
  margin: 0 auto;
}
.msg-media-audio { width: min(260px, 100%); height: 36px; margin: 2px 0; }
.msg-media-file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  font-size: 13px;
  color: rgba(255,255,255,0.85);
}
.msg-text.muted { color: rgba(255,255,255,0.55); font-style: italic; }
.msg-bubble.deleted { border: 1px dashed rgba(255,69,58,0.45); background: rgba(255,69,58,0.08) !important; }
.msg-bubble.edited { border: 1px solid rgba(255,159,10,0.25); }
.msg-bubble.reserve { border: 1px solid rgba(50,215,75,0.25); background: rgba(50,215,75,0.08) !important; }
.msg-tag {
  display: block;
  font-size: 10px;
  font-weight: 600;
  margin-bottom: 4px;
  opacity: 0.85;
}
.msg-tag.deleted { color: var(--danger); }
.msg-tag.edited { color: var(--warn); }
.msg-tag.reserve { color: var(--success); }
.msg-time { font-size: 10px; color: rgba(255,255,255,0.45); text-align: right; margin-top: 4px; }

.chat-menu-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s var(--ease);
}
.chat-menu-backdrop.show { opacity: 1; pointer-events: auto; }
.chat-menu {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  max-width: 448px;
  margin: 0 auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  z-index: 41;
  transform: translateY(110%);
  transition: transform .24s var(--ease);
  overflow: hidden;
}
.chat-menu.show { transform: translateY(0); }
.chat-menu-item {
  display: block;
  width: 100%;
  padding: 14px 16px;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
}
.chat-menu-item:last-child { border-bottom: none; }
.chat-menu-item:active { background: var(--card-hover); }
.chat-menu-item.danger { color: var(--danger); }
.chat-menu-item.warn { color: var(--warn); }
.chat-menu-item.muted { color: var(--muted); }
.thread-audit-head {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.thread-audit-head h4 { font-size: 14px; font-weight: 600; }
.thread-audit-back {
  border: none;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  padding: 4px 0;
}

@media (max-width: 360px) {
  .page-title { font-size: 20px; }
  .stat-value { font-size: 26px; }
}
