:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --accent: var(--tg-theme-button-color, #3a7bf0);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, var(--accent));
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --sep: var(--tg-theme-section-separator-color, rgba(128, 128, 128, 0.18));
  --l0: color-mix(in srgb, var(--hint) 20%, transparent);
  --l1: color-mix(in srgb, var(--accent) 38%, var(--card));
  --l2: color-mix(in srgb, var(--accent) 60%, var(--card));
  --l3: color-mix(in srgb, var(--accent) 80%, var(--card));
  --l4: var(--accent);
  --radius: 14px;
  --cell: 12px;
  --gap: 3px;
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #111214; --card: #1c1c1f; --text: #f2f2f2; --hint: #8b8b92;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
main { max-width: 560px; margin: 0 auto; padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; }
h3 { font-size: 18px; font-weight: 600; }
.hint { color: var(--hint); }
.small { font-size: 13px; }
.center { text-align: center; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* header */
.top { display: flex; align-items: center; gap: 12px; margin: 4px 2px 16px; }
.top p { margin-top: 2px; }

/* tabs */
.tabs { display: flex; background: var(--l0); border-radius: 10px; padding: 2px; margin-bottom: 16px; }
.tabs button { flex: 1; padding: 7px 0; border-radius: 8px; font-size: 14px; font-weight: 500; color: var(--hint); }
.tabs button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08); }

/* cards & lists */
.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 16px; }
.card.list { padding: 0; overflow: hidden; }
.card-title { font-weight: 600; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: baseline; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--hint); margin: 4px 14px 6px; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; width: 100%; text-align: left; position: relative; }
.row + .row::before { content: ""; position: absolute; top: 0; left: 14px; right: 0; border-top: 1px solid var(--sep); }
button.row:active, .row.tap:active { background: var(--l0); }
.row .chev { color: var(--hint); font-size: 18px; }
.row.add { color: var(--link); font-weight: 500; }

.avatar {
  width: 38px; height: 38px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: var(--l1); color: var(--text); font-weight: 600;
}
.emoji { width: 40px; height: 40px; border-radius: 12px; flex: none; display: grid; place-items: center; font-size: 22px; background: var(--l0); }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 99px; background: var(--l0); color: var(--hint); font-weight: 500; }
.badge.accent { background: var(--l1); color: var(--text); }

/* stats */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px; }
.stat b { display: block; font-size: 20px; font-weight: 700; }
.stat span { font-size: 12px; color: var(--hint); }

/* progress */
.bar { height: 6px; border-radius: 3px; background: var(--l0); overflow: hidden; margin-top: 7px; }
.bar i { display: block; height: 100%; border-radius: 3px; background: var(--accent); transition: width 0.3s; }
.bar.met i { background: var(--l4); }
.plus {
  min-width: 44px; height: 36px; padding: 0 10px; border-radius: 10px; background: var(--l1);
  font-weight: 600; font-size: 14px; flex: none;
}
.plus:active { transform: scale(0.95); }
.plus.done { background: var(--accent); color: var(--accent-text); }

/* contribution grid */
.grid-scroll { overflow-x: auto; scrollbar-width: none; }
.grid-scroll::-webkit-scrollbar { display: none; }
.grid { display: flex; gap: var(--gap); width: max-content; }
.gcol { display: flex; flex-direction: column; gap: var(--gap); }
.gcol .m { height: 14px; font-size: 10px; color: var(--hint); white-space: nowrap; width: var(--cell); overflow: visible; }
.c { width: var(--cell); height: var(--cell); border-radius: 3px; background: var(--l0); }
.c.l1 { background: var(--l1); } .c.l2 { background: var(--l2); } .c.l3 { background: var(--l3); } .c.l4 { background: var(--l4); }
.c.x { visibility: hidden; }
.c.today { outline: 1.5px solid var(--text); outline-offset: -1.5px; }
.gdays { position: sticky; left: 0; z-index: 1; background: var(--card); padding-right: 2px; }
.gdays .c { background: none; font-size: 9px; color: var(--hint); line-height: var(--cell); width: 14px; }
.legend { display: flex; align-items: center; justify-content: flex-end; gap: 3px; margin-top: 8px; font-size: 11px; color: var(--hint); }
.legend .c { width: 10px; height: 10px; }
.legend span { margin: 0 4px; }
.mini { display: flex; gap: 3px; }
.mini .c { width: 10px; height: 10px; border-radius: 2px; }

/* forms */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--hint); margin: 0 2px 5px; }
.input, select.input, textarea.input {
  width: 100%; border: 0; outline: 0; border-radius: 10px; padding: 11px 12px;
  background: var(--l0); appearance: none; -webkit-appearance: none;
}
textarea.input { resize: vertical; min-height: 64px; }
.input:focus { box-shadow: 0 0 0 2px var(--l2); }
.split { display: flex; gap: 10px; }
.split > * { flex: 1; min-width: 0; }
.setting { display: flex; align-items: center; gap: 12px; padding: 10px 0; }
.setting + .setting { border-top: 1px solid var(--sep); }
.setting .input { width: auto; padding: 7px 10px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 12px 16px; border-radius: 12px; font-weight: 600; background: var(--l0); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.btn.ghost { background: none; color: var(--link); padding: 8px; }
.btn.block { width: 100%; margin-bottom: 10px; }
.btn.sm { padding: 7px 12px; font-size: 14px; border-radius: 10px; }
.btn:active { opacity: 0.8; }
.btn[disabled] { opacity: 0.5; pointer-events: none; }
.icon-btn { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; color: var(--hint); flex: none; }
.icon-btn:active { background: var(--l0); }

.switch { position: relative; width: 46px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--l0); transition: 0.2s; }
.switch span::after { content: ""; position: absolute; width: 24px; height: 24px; left: 2px; top: 2px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); transition: 0.2s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(18px); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 14px 0; }
.chip { padding: 9px 14px; border-radius: 99px; background: var(--l1); font-weight: 600; }
.chip:active { transform: scale(0.95); }

.empty { text-align: center; padding: 28px 16px; }
.empty .big { font-size: 40px; margin-bottom: 8px; }
.empty p { margin-bottom: 14px; }

.rules { background: var(--l0); border-radius: 10px; padding: 10px 12px; font-size: 14px; white-space: pre-wrap; margin-top: 12px; }
.code { font-family: ui-monospace, Menlo, monospace; background: var(--l0); padding: 2px 6px; border-radius: 6px; }

/* sheet */
.sheet-wrap { position: fixed; inset: 0; z-index: 10; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.4); animation: fade 0.2s; }
.sheet {
  position: relative; width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto;
  background: var(--bg); border-radius: 18px 18px 0 0; padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
  animation: up 0.25s cubic-bezier(0.2, 0.9, 0.3, 1);
}
.sheet::before { content: ""; display: block; width: 36px; height: 5px; border-radius: 3px; background: var(--l0); margin: 0 auto 12px; }
.sheet-head { display: flex; gap: 12px; align-items: center; margin-bottom: 4px; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: rgba(30, 30, 30, 0.92); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 20;
  max-width: calc(100% - 32px); animation: fade 0.2s;
}
.loading { display: grid; place-items: center; height: 60vh; }
.spinner { width: 26px; height: 26px; border-radius: 50%; border: 3px solid var(--l0); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

[hidden] { display: none !important; }
