:root {
  --bg: var(--tg-theme-bg-color, #f4f5fa);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-secondary-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #101828);
  --muted: var(--tg-theme-hint-color, #667085);
  --accent: var(--tg-theme-button-color, #5b5bd6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #5b5bd6);
  --separator: var(--tg-theme-section-separator-color, rgba(16, 24, 40, 0.09));
  --track: var(--tg-theme-section-separator-color, rgba(16, 24, 40, 0.12));
  --danger: #e5484d;
  --radius: 20px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html,
body {
  margin: 0;
  padding: 0;
  overflow-x: hidden;
}

body.modal-open {
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  min-height: 100vh;
  min-height: 100svh;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  background: none;
  cursor: pointer;
  padding: 0;
}

h1,
h2,
h3,
p {
  margin: 0;
}

.is-hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}

.app {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px
    calc(86px + var(--safe-bottom));
}

/* --------------------------------------------------------------- top bar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 2px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 38px;
  height: 38px;
  flex: none;
  border-radius: 13px;
  background: var(--accent);
  color: var(--accent-text);
  display: grid;
  place-items: center;
  font-size: 18px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.brand-text {
  min-width: 0;
}

.brand h1 {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.2px;
  line-height: 1.2;
}

.brand p {
  font-size: 12px;
  margin-top: 2px;
}

.avatar {
  width: 38px;
  height: 38px;
  flex: none;
  border-radius: 50%;
  background: var(--card);
  border: 1px solid var(--separator);
  display: grid;
  place-items: center;
  font-size: 16px;
  font-weight: 600;
  color: var(--muted);
}

/* ---------------------------------------------------------------- screens */

.screen {
  display: none;
}

.screen.active {
  display: block;
  animation: fade-in 0.22s ease;
}

@keyframes fade-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.card {
  background: var(--card);
  border: 1px solid var(--separator);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.03);
}

.card h3 {
  font-size: 15px;
  font-weight: 650;
}

.card-note {
  font-size: 13px;
  line-height: 1.45;
  margin: 6px 0 14px;
}

.hero {
  position: relative;
  overflow: hidden;
  padding: 18px;
}

.hero-glow {
  position: absolute;
  width: 170px;
  height: 170px;
  right: -48px;
  top: -80px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.16;
  filter: blur(28px);
  pointer-events: none;
}

.hero-top {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.eyebrow {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 4px;
}

.hero h2 {
  font-size: 25px;
  line-height: 1.15;
  letter-spacing: -0.5px;
}

.hero-hint {
  position: relative;
  margin-top: 12px;
  font-size: 13px;
  color: var(--muted);
}

.ring {
  --p: 0;
  width: 60px;
  height: 60px;
  flex: none;
  border-radius: 50%;
  background: conic-gradient(var(--accent) calc(var(--p) * 1%), var(--track) 0);
  display: grid;
  place-items: center;
  transition: background 0.3s ease;
}

.ring-inner {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--card);
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.section-head h3 {
  font-size: 15px;
  font-weight: 650;
}

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin: 8px 2px 14px;
}

.page-head h2 {
  font-size: 24px;
  letter-spacing: -0.5px;
  line-height: 1.15;
}

.page-head .muted {
  font-size: 13px;
  margin-top: 3px;
}

.page-head.section-head {
  margin-top: 22px;
}

.page-head.section-head h3 {
  font-size: 16px;
}

.pill {
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  background: var(--track);
  border-radius: 999px;
  padding: 4px 9px;
  white-space: nowrap;
}

.link-btn {
  color: var(--link);
  font-size: 13px;
  font-weight: 600;
  padding: 4px 0;
}

/* ------------------------------------------------------------------ mood */

.mood-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  margin-top: 4px;
}

.mood {
  height: 50px;
  border-radius: 14px;
  background: var(--bg);
  border: 1px solid transparent;
  font-size: 25px;
  line-height: 1;
  display: grid;
  place-items: center;
  transition: transform 0.12s ease, border-color 0.15s ease,
    background 0.15s ease;
}

.mood:active {
  transform: scale(0.94);
}

.mood.selected {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  transform: translateY(-2px);
}

/* ---------------------------------------------------------------- habits */

.habit-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--separator);
  border-radius: 16px;
  padding: 12px 14px;
  margin-bottom: 8px;
  transition: transform 0.1s ease, opacity 0.2s ease;
}

.habit-row:active {
  transform: scale(0.985);
}

.habit-emoji {
  width: 40px;
  height: 40px;
  flex: none;
  border-radius: 12px;
  background: var(--bg);
  display: grid;
  place-items: center;
  font-size: 20px;
}

.habit-name {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.habit-row.done .habit-name {
  color: var(--muted);
}

.check {
  width: 27px;
  height: 27px;
  flex: none;
  border-radius: 50%;
  border: 2px solid var(--track);
  display: grid;
  place-items: center;
  color: transparent;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.check svg {
  width: 15px;
  height: 15px;
  fill: currentColor;
}

.habit-row.done .check {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.empty {
  text-align: center;
  padding: 26px 18px;
  border: 1px dashed var(--track);
  border-radius: 18px;
  margin-bottom: 10px;
}

.empty-emoji {
  font-size: 32px;
  margin-bottom: 8px;
}

.empty h3 {
  font-size: 15px;
  margin-bottom: 4px;
}

.empty p {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 14px;
}

/* --------------------------------------------------------- manage habits */

.manage-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--separator);
  border-radius: 16px;
  padding: 10px 12px;
  margin-bottom: 8px;
}

.manage-main {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
  flex: 1;
}

.manage-main .habit-emoji {
  width: 38px;
  height: 38px;
}

.row-actions {
  display: flex;
  gap: 6px;
  flex: none;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 11px;
  background: var(--bg);
  border: 1px solid var(--separator);
  display: grid;
  place-items: center;
  color: var(--muted);
  transition: transform 0.1s ease;
}

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.icon-btn.danger {
  color: var(--danger);
}

.archived-block {
  margin-top: 16px;
}

.section-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 2px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.section-toggle .chevron {
  transition: transform 0.2s ease;
}

.section-toggle.open .chevron {
  transform: rotate(180deg);
}

.archived-list {
  margin-top: 2px;
}

.archived-list .manage-row {
  opacity: 0.75;
}

/* --------------------------------------------------------------- buttons */

.primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 14px;
  padding: 14px 18px;
  font-size: 15px;
  font-weight: 650;
  transition: transform 0.1s ease, opacity 0.2s ease;
}

.primary:active {
  transform: scale(0.98);
}

.primary.small {
  padding: 9px 12px;
  border-radius: 12px;
  font-size: 13px;
}

.primary.wide {
  width: 100%;
  margin-top: 4px;
  margin-bottom: 8px;
}

.primary:disabled {
  opacity: 0.6;
  pointer-events: none;
}

.danger {
  width: 100%;
  padding: 13px 16px;
  border-radius: 14px;
  border: 1px solid rgba(229, 72, 77, 0.35);
  color: var(--danger);
  font-weight: 650;
  background: transparent;
}

.danger:active {
  background: rgba(229, 72, 77, 0.08);
}

.footnote {
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
  text-align: center;
  margin: 14px 8px 0;
}

/* --------------------------------------------------------------- reports */

.premium-card {
  margin-top: 16px;
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent), 0 1px 2px rgba(16, 24, 40, 0.03);
}

.premium-card .card-note {
  margin-bottom: 12px;
}

.premium-card .report-card {
  margin-bottom: 10px;
}

.deep-empty {
  font-size: 13px;
  line-height: 1.45;
  margin: 2px 0 10px;
}

.secondary {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 13px 16px;
  margin: 8px 0;
  border-radius: 14px;
  border: 1px solid var(--separator);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  transition: transform 0.1s ease, opacity 0.2s ease;
}

.secondary:active {
  transform: scale(0.98);
}

.secondary:disabled {
  opacity: 0.55;
  pointer-events: none;
}

.report-card.deep {
  border-left-color: var(--link);
}

.report-card {
  background: var(--card);
  border: 1px solid var(--separator);
  border-left: 4px solid var(--accent);
  border-radius: 18px;
  padding: 16px;
  margin-bottom: 12px;
}

.report-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.report-week {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
}

.report-date {
  font-size: 11px;
  color: var(--muted);
}

.report-text {
  font-size: 15px;
  line-height: 1.55;
  white-space: normal;
  overflow-wrap: anywhere;
}

.report-card .footnote {
  text-align: left;
  margin: 10px 0 0;
}

.share-card-btn {
  margin-top: 10px;
  color: var(--link);
  font-size: 13px;
  font-weight: 600;
  padding: 2px 0;
}

.referral-card {
  margin-top: 16px;
}

.referral-card .card-note {
  margin-bottom: 12px;
}

.share-preview {
  display: block;
  width: 100%;
  max-height: 68vh;
  object-fit: contain;
  border-radius: 16px;
  background: #111827;
  margin: 12px 0;
}

.share-note {
  font-size: 12px;
  line-height: 1.45;
  color: var(--muted);
  margin-bottom: 4px;
}

.history-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--separator);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
}

.history-row:active {
  opacity: 0.8;
}

.history-date {
  font-size: 13px;
  font-weight: 650;
  flex: none;
}

.history-kind {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
  flex: none;
}

.history-kind.deep {
  color: var(--link);
}

.history-preview {
  font-size: 12px;
  color: var(--muted);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-arrow {
  color: var(--muted);
  flex: none;
  font-size: 15px;
}

/* -------------------------------------------------------------- settings */

.field-label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 12px 0 6px;
}

.input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--separator);
  border-radius: 12px;
  padding: 12px 13px;
  font-size: 16px; /* iOS не зумит страницу при фокусе */
  outline: none;
  transition: border-color 0.15s ease;
}

.input:focus {
  border-color: var(--accent);
}

.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 36px;
}

#saveSettingsBtn {
  margin-top: 16px;
}

/* ----------------------------------------------------------------- modal */

#modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
}

#modal.open {
  display: block;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(8, 10, 20, 0.52);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 10px 10px calc(10px + var(--safe-bottom));
  animation: fade-in 0.18s ease;
}

.modal-card {
  width: 100%;
  max-width: 520px;
  max-height: 88vh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: 24px;
  padding: 20px;
  box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.2);
  animation: slide-up 0.22s ease;
}

@keyframes slide-up {
  from {
    transform: translateY(24px);
  }
  to {
    transform: none;
  }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}

.modal-head h3 {
  font-size: 19px;
  letter-spacing: -0.3px;
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}

.emoji-option {
  height: 42px;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--separator);
  font-size: 21px;
  display: grid;
  place-items: center;
}

.emoji-option.selected {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.emoji-input {
  margin-top: 8px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 20px;
}

.modal-actions .primary {
  flex: 1;
}

.modal-actions .secondary {
  width: auto;
  flex: none;
  margin: 0;
  padding: 13px 16px;
}

.modal-actions .danger {
  width: auto;
  flex: none;
  padding: 13px 16px;
}

.modal-report-text {
  font-size: 15px;
  line-height: 1.55;
  white-space: normal;
  overflow-wrap: anywhere;
  margin-top: 12px;
}

/* ------------------------------------------------------------ nav / boot */

.tabbar {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  z-index: 20;
  width: 100%;
  max-width: 560px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  padding: 6px 10px calc(6px + var(--safe-bottom));
  background: var(--bg);
  border-top: 1px solid var(--separator);
  box-shadow: 0 -8px 24px rgba(16, 24, 40, 0.05);
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 6px 2px 4px;
  border-radius: 12px;
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 600;
  transition: color 0.15s ease, background 0.15s ease;
}

.tab svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

.tab[aria-selected="true"] {
  color: var(--accent);
  background: var(--card);
}

.boot,
.fatal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
}

.boot p {
  margin-top: 14px;
  font-size: 13px;
}

.spinner {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2.5px solid var(--track);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

.spin {
  display: inline-block;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-top-color: transparent;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.fatal {
  z-index: 90;
}

.fatal-card {
  width: 100%;
  max-width: 380px;
  padding: 26px 20px;
}

.fatal-emoji {
  font-size: 40px;
  margin-bottom: 10px;
}

.fatal-card h2 {
  font-size: 20px;
  margin-bottom: 8px;
}

.fatal-card p {
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 18px;
}

/* ----------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  transform: translate(-50%, 12px);
  z-index: 60;
  max-width: calc(100% - 32px);
  background: #1d2939;
  color: #fff;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  padding: 10px 14px;
  border-radius: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast.error {
  background: #b42318;
}

.toast.success {
  background: #067647;
}

/* ---------------------------------------------------------------- desktop */

@media (min-width: 620px) {
  .app {
    padding-top: 24px;
  }

  .modal-backdrop {
    align-items: center;
    padding: 24px;
  }

  .modal-card {
    border-radius: 24px;
  }
}

@media (max-width: 350px) {
  .app {
    padding-left: 12px;
    padding-right: 12px;
  }

  .brand h1 {
    font-size: 16px;
  }

  .hero h2 {
    font-size: 22px;
  }

  .mood {
    font-size: 23px;
  }

  .page-head h2 {
    font-size: 22px;
  }
}
