:root {
  --bg: var(--tg-theme-bg-color, #0f1115);
  --secbg: var(--tg-theme-secondary-bg-color, #1a1d24);
  --text: var(--tg-theme-text-color, #f2f3f5);
  --hint: var(--tg-theme-hint-color, #8a909b);
  --link: var(--tg-theme-link-color, #5eb5f7);
  --btn: var(--tg-theme-button-color, #3b82f6);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --accent: #ff6b6b;
  --good: #2ecc71;
  --card: var(--tg-theme-section-bg-color, #1e222b);
  --border: rgba(255,255,255,0.07);
  --radius: 16px;
}

body[data-theme="dark"] {
  --bg: #0f1115;
  --secbg: #1a1d24;
  --text: #f2f3f5;
  --hint: #8a909b;
  --link: #5eb5f7;
  --btn: #3b82f6;
  --btn-text: #ffffff;
  --card: #1e222b;
  --border: rgba(255,255,255,0.07);
}

body[data-theme="light"] {
  --bg: #eef0f3;
  --secbg: #ffffff;
  --text: #16191f;
  --hint: #6c7480;
  --link: #2f7bd6;
  --btn: #3b82f6;
  --btn-text: #ffffff;
  --card: #ffffff;
  --border: rgba(0,0,0,0.08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, "Vazirmatn", sans-serif;
  font-size: 15px;
}
body.rtl { direction: rtl; }
#app { padding: 14px 14px 90px; max-width: 640px; margin: 0 auto; }

.hidden { display: none !important; }
.center { display: flex; align-items: center; justify-content: center; min-height: 70vh; }
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.15); border-top-color: var(--btn);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px;
  box-shadow: 0 1px 0 rgba(255,255,255,0.03);
}
h1 { font-size: 20px; margin: 4px 0 14px; }
h2 { font-size: 16px; margin: 0 0 12px; }
.hint { color: var(--hint); font-size: 13px; }
.row { display: flex; align-items: center; gap: 10px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }

.btn {
  background: var(--btn); color: var(--btn-text); border: none;
  border-radius: 12px; padding: 12px 16px; font-size: 15px; font-weight: 600;
  cursor: pointer; width: 100%;
}
.btn:active { opacity: 0.8; }
.btn.sec { background: var(--secbg); color: var(--text); }
.btn.ghost { background: transparent; color: var(--link); border: 1px solid var(--hint); }
.btn.danger { background: #e74c3c; color: #fff; }
.btn.small { width: auto; padding: 8px 12px; font-size: 13px; }
.btn.tiny { width: auto; padding: 6px 10px; font-size: 13px; border-radius: 10px; }

input, textarea {
  width: 100%; background: var(--secbg); color: var(--text);
  border: 1px solid transparent; border-radius: 12px; padding: 12px;
  font-size: 15px; font-family: inherit;
}
textarea { min-height: 80px; resize: vertical; }

.profile-head { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; }
.avatar {
  width: 58px; height: 58px; border-radius: 50%;
  background: var(--secbg); display: flex; align-items: center; justify-content: center;
  font-size: 32px; flex-shrink: 0;
}
.tag { font-weight: 700; font-size: 17px; }
.subtag { color: var(--hint); font-size: 12px; }

.habit {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.habit:last-child { border-bottom: none; }
.habit-name { font-weight: 600; }
.streak-badge { color: var(--good); font-size: 12px; }
.counter { display: flex; align-items: center; gap: 8px; }
.counter .val { min-width: 28px; text-align: center; font-weight: 700; font-size: 16px; }
.cbtn {
  width: 34px; height: 34px; border-radius: 50%; border: none;
  background: var(--secbg); color: var(--text); font-size: 18px; font-weight: 700; cursor: pointer;
}
.cbtn.plus { background: var(--accent); color: #fff; }

.fruit-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.fruit-opt {
  aspect-ratio: 1; border-radius: 12px; background: var(--secbg);
  display: flex; align-items: center; justify-content: center; font-size: 26px; cursor: pointer;
  border: 2px solid transparent;
}
.fruit-opt.sel { border-color: var(--btn); }
.name-grid { display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }
.name-opt {
  padding: 12px; border-radius: 12px; background: var(--secbg); cursor: pointer;
  border: 2px solid transparent; font-weight: 600; text-align: center;
}
.name-opt.sel { border-color: var(--btn); }

.seg { display: flex; background: var(--secbg); border-radius: 12px; padding: 4px; margin-bottom: 12px; }
.seg button { flex: 1; background: transparent; color: var(--text); border: none; padding: 9px; border-radius: 9px; font-weight: 600; cursor: pointer; }
.seg button.active { background: var(--btn); color: var(--btn-text); }

.rank-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.rank-row:last-child { border-bottom: none; }
.rank-pos { width: 28px; font-weight: 700; color: var(--hint); text-align: center; }
.rank-row.me { background: rgba(59,130,246,0.12); border-radius: 10px; padding-left: 6px; padding-right: 6px; }
.rank-emoji { font-size: 22px; }
.rank-streak { color: var(--good); font-weight: 700; }

.list-item { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 20px; background: var(--secbg); color: var(--hint); }
.badge.open { background: rgba(46,204,113,0.18); color: var(--good); }
.badge.closed { background: rgba(231,76,60,0.18); color: #e74c3c; }
.badge.admin { background: rgba(59,130,246,0.18); color: var(--link); }

.msg { padding: 10px 12px; border-radius: 12px; margin-bottom: 8px; max-width: 85%; }
.msg.them { background: var(--secbg); align-self: flex-start; }
.msg.me { background: var(--btn); color: var(--btn-text); margin-left: auto; }
.msg .meta { font-size: 10px; opacity: 0.7; margin-top: 4px; }
.chat { display: flex; flex-direction: column; }

.nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--secbg); display: flex; justify-content: space-around;
  padding: 8px 4px calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border);
  z-index: 50;
}
.nav-btn { background: none; border: none; color: var(--hint); display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 10px; cursor: pointer; flex: 1; }
.nav-btn span { font-size: 20px; }
.nav-btn.active { color: var(--btn); }
.nav-btn label { pointer-events: none; }

.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: #000; color: #fff; padding: 10px 18px; border-radius: 20px; font-size: 14px;
  opacity: 0; transition: opacity 0.25s; z-index: 100; max-width: 90%;
}
.toast.show { opacity: 0.92; }
.spacer { height: 8px; }
.link { color: var(--link); cursor: pointer; word-break: break-all; }
.empty { text-align: center; color: var(--hint); padding: 30px 10px; }
