:root {
  --tg-bg-color: #f4f6f8;
  --tg-secondary-bg-color: #ffffff;
  --tg-text-color: #17212b;
  --tg-hint-color: #6f7b86;
  --tg-link-color: #168acd;
  --tg-button-color: #168acd;
  --tg-button-text-color: #ffffff;
  --tg-header-bg-color: #ffffff;
  --tg-section-bg-color: #ffffff;
  --border-color: rgba(23, 33, 43, 0.11);
  --pressed-color: rgba(23, 33, 43, 0.06);
  --danger-color: #d84a4a;
  --danger-soft: rgba(216, 74, 74, 0.12);
  --success-color: #26845b;
  --success-soft: rgba(38, 132, 91, 0.12);
  --warning-color: #b87915;
  --warning-soft: rgba(184, 121, 21, 0.13);
  --tomorrow-color: #7267c9;
  --tomorrow-soft: rgba(114, 103, 201, 0.13);
  --nav-height: 68px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--tg-bg-color);
  color-scheme: light dark;
  text-size-adjust: 100%;
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  background: var(--tg-bg-color);
  color: var(--tg-text-color);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  letter-spacing: 0;
}

button,
input,
textarea,
select {
  color: inherit;
  font: inherit;
  letter-spacing: 0;
}

button {
  min-height: 44px;
  border: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--tg-link-color) 46%, transparent);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.standalone-state {
  width: min(100%, 520px);
  min-height: 100vh;
  margin: 0 auto;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 10px;
  padding: 32px 24px;
  text-align: center;
}

.standalone-state h1,
.standalone-state p {
  margin: 0;
}

.standalone-state p {
  max-width: 340px;
  color: var(--tg-hint-color);
}

.gate-mark,
.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  background: var(--tg-button-color);
  color: var(--tg-button-text-color);
  font-weight: 800;
}

.gate-mark {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  font-size: 25px;
}

.app-shell {
  width: min(100%, 840px);
  min-height: 100vh;
  margin: 0 auto;
  background: var(--tg-bg-color);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  min-height: calc(66px + var(--safe-top));
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  background: color-mix(in srgb, var(--tg-header-bg-color) 94%, transparent);
  border-bottom: 1px solid var(--border-color);
  backdrop-filter: blur(14px);
}

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

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 7px;
  font-size: 18px;
}

.brand-block h1,
.brand-block p {
  margin: 0;
}

.brand-block h1 {
  font-size: 19px;
  line-height: 1.1;
}

.brand-block p {
  max-width: 250px;
  margin-top: 3px;
  overflow: hidden;
  color: var(--tg-hint-color);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: 7px;
  background: var(--tg-secondary-bg-color);
  color: var(--tg-text-color);
  font-size: 22px;
}

.icon-button:active,
.nav-button:active,
.task-card:active,
.memory-card:active,
.stat-tile:active {
  background-color: var(--pressed-color);
}

.main-content {
  min-height: calc(100vh - var(--nav-height));
  padding: 16px 14px calc(var(--nav-height) + var(--safe-bottom) + 22px);
}

.screen {
  display: grid;
  gap: 18px;
  animation: screen-in 150ms ease-out;
}

.screen-title {
  display: grid;
  gap: 2px;
}

.screen-title h2,
.section-heading h2,
.section-heading h3 {
  margin: 0;
}

.screen-title h2 {
  font-size: 25px;
}

.eyebrow {
  color: var(--tg-hint-color);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.stat-tile {
  min-width: 0;
  height: 94px;
  display: grid;
  align-content: center;
  justify-items: start;
  gap: 2px;
  padding: 10px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--tg-section-bg-color);
  color: var(--tg-text-color);
  text-align: left;
}

.stat-tile strong {
  font-size: 23px;
  line-height: 1;
}

.stat-tile > span:last-child {
  width: 100%;
  overflow: hidden;
  color: var(--tg-hint-color);
  font-size: 10px;
  text-overflow: ellipsis;
}

.stat-icon {
  color: var(--tg-hint-color);
  font-size: 14px;
  font-weight: 800;
}

.stat-danger .stat-icon,
.stat-danger strong {
  color: var(--danger-color);
}

.stat-today .stat-icon,
.stat-today strong {
  color: var(--warning-color);
}

.stat-tomorrow .stat-icon,
.stat-tomorrow strong {
  color: var(--tomorrow-color);
}

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

.primary-command,
.secondary-command,
.load-more,
.text-button,
.row-command {
  min-height: 46px;
  border-radius: 7px;
  font-weight: 650;
}

.primary-command {
  background: var(--tg-button-color);
  color: var(--tg-button-text-color);
}

.secondary-command,
.load-more {
  border: 1px solid var(--border-color);
  background: var(--tg-secondary-bg-color);
  color: var(--tg-text-color);
}

.content-section {
  display: grid;
  gap: 10px;
}

.section-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}

.section-heading h2 {
  font-size: 18px;
}

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

.section-heading p {
  margin: 2px 0 0;
  color: var(--tg-hint-color);
  font-size: 12px;
}

.link-button,
.text-button {
  min-height: 40px;
  padding: 0 6px;
  background: transparent;
  color: var(--tg-link-color);
}

.item-list,
.memory-grid {
  display: grid;
  gap: 8px;
}

.task-card,
.memory-card,
.deadline-card,
.settings-section {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--tg-section-bg-color);
}

.task-card {
  width: 100%;
  min-height: 64px;
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) 24px;
  align-items: center;
  gap: 9px;
  padding: 10px 11px;
  text-align: left;
}

.task-toggle {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 2px solid var(--tg-hint-color);
  border-radius: 50%;
  background: transparent;
  color: transparent;
}

.task-toggle.is-done {
  border-color: var(--success-color);
  background: var(--success-color);
  color: white;
}

.task-copy {
  min-width: 0;
}

.task-open {
  min-width: 0;
  min-height: 44px;
  display: grid;
  align-content: center;
  justify-items: start;
  padding: 0;
  background: transparent;
  text-align: left;
}

.task-open .task-meta,
.deadline-card .task-meta {
  display: block;
}

.task-title,
.memory-title {
  overflow-wrap: anywhere;
  font-weight: 650;
}

.task-card.is-done .task-title {
  color: var(--tg-hint-color);
  text-decoration: line-through;
}

.task-meta,
.memory-meta,
.memory-summary {
  margin-top: 3px;
  color: var(--tg-hint-color);
  font-size: 12px;
}

.task-chevron {
  color: var(--tg-hint-color);
  font-size: 18px;
  text-align: center;
}

.deadline-card {
  display: grid;
  grid-template-columns: 4px minmax(0, 1fr);
  gap: 11px;
  padding: 11px;
  color: var(--tg-text-color);
  text-align: left;
}

.deadline-bar {
  border-radius: 2px;
  background: var(--warning-color);
}

.deadline-card.is-overdue .deadline-bar {
  background: var(--danger-color);
}

.memory-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.memory-card {
  min-width: 0;
  min-height: 118px;
  padding: 12px;
  text-align: left;
}

.memory-card .category-mark {
  display: inline-block;
  margin-bottom: 8px;
  color: var(--tg-link-color);
  font-size: 11px;
  font-weight: 750;
  text-transform: uppercase;
}

.memory-summary {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.search-field {
  min-height: 46px;
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  padding: 0 12px;
  border: 1px solid var(--border-color);
  border-radius: 7px;
  background: var(--tg-secondary-bg-color);
  color: var(--tg-hint-color);
}

.search-field input {
  width: 100%;
  height: 44px;
  padding: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--tg-text-color);
}

.search-field input::placeholder,
textarea::placeholder,
input::placeholder {
  color: var(--tg-hint-color);
}

.chip-row {
  display: flex;
  gap: 7px;
  margin-inline: -14px;
  padding: 0 14px 4px;
  overflow-x: auto;
  scrollbar-width: none;
}

.chip-row::-webkit-scrollbar {
  display: none;
}

.chip {
  flex: 0 0 auto;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--border-color);
  border-radius: 999px;
  background: var(--tg-secondary-bg-color);
  color: var(--tg-hint-color);
  font-size: 13px;
  font-weight: 650;
}

.chip.is-active {
  border-color: var(--tg-button-color);
  background: var(--tg-button-color);
  color: var(--tg-button-text-color);
}

.task-list,
.memory-list {
  min-height: 116px;
}

.load-more {
  width: 100%;
}

.fab {
  position: fixed;
  right: max(18px, calc((100vw - 840px) / 2 + 18px));
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 14px);
  z-index: 25;
  width: 52px;
  height: 52px;
  min-height: 52px;
  padding: 0;
  border-radius: 50%;
  background: var(--tg-button-color);
  color: var(--tg-button-text-color);
  font-size: 29px;
  line-height: 1;
  box-shadow: 0 8px 22px rgba(23, 33, 43, 0.18);
}

.settings-section {
  display: grid;
  gap: 14px;
  padding: 14px;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--border-color);
  border-radius: 7px;
  background: var(--tg-bg-color);
}

.segmented-control button {
  min-height: 40px;
  border-radius: 5px;
  background: transparent;
  color: var(--tg-hint-color);
  font-size: 12px;
  font-weight: 750;
}

.segmented-control button.is-active {
  background: var(--tg-button-color);
  color: var(--tg-button-text-color);
}

.mode-legend {
  display: grid;
  gap: 8px;
}

.mode-legend p {
  display: grid;
  grid-template-columns: 58px minmax(0, 1fr);
  gap: 8px;
  margin: 0;
  font-size: 12px;
}

.mode-legend span {
  overflow-wrap: anywhere;
  color: var(--tg-hint-color);
}

.status-list {
  display: grid;
  gap: 2px;
}

.status-row {
  min-height: 44px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--border-color);
}

.status-row:last-child {
  border-bottom: 0;
}

.status-value {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--tg-hint-color);
  font-size: 12px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--tg-hint-color);
}

.status-dot.is-good {
  background: var(--success-color);
}

.status-dot.is-bad {
  background: var(--danger-color);
}

.full-width {
  width: 100%;
}

.settings-actions {
  gap: 0;
  padding-block: 0;
}

.row-command {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0;
  border-bottom: 1px solid var(--border-color);
  background: transparent;
  text-align: left;
}

.row-command:last-child {
  border-bottom: 0;
}

.bottom-nav {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 40;
  height: calc(var(--nav-height) + var(--safe-bottom));
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 5px max(8px, calc((100vw - 840px) / 2 + 8px)) var(--safe-bottom);
  border-top: 1px solid var(--border-color);
  background: color-mix(in srgb, var(--tg-header-bg-color) 96%, transparent);
  backdrop-filter: blur(16px);
}

.nav-button {
  min-width: 0;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 1px;
  padding: 3px 2px;
  background: transparent;
  color: var(--tg-hint-color);
}

.nav-button > span {
  font-size: 21px;
  line-height: 1;
}

.nav-button small {
  max-width: 100%;
  overflow: hidden;
  font-size: 10px;
  font-weight: 650;
  text-overflow: ellipsis;
}

.nav-button.is-active {
  color: var(--tg-button-color);
}

.inline-error {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  padding: 11px 12px;
  border-left: 4px solid var(--danger-color);
  background: var(--danger-soft);
}

.inline-error p {
  margin: 3px 0 0;
  color: var(--tg-hint-color);
  font-size: 12px;
}

.empty-state,
.skeleton-card {
  min-height: 82px;
  display: grid;
  align-content: center;
  justify-items: center;
  padding: 16px;
  border: 1px dashed var(--border-color);
  border-radius: 8px;
  color: var(--tg-hint-color);
  text-align: center;
}

.skeleton-card {
  border-style: solid;
  background: var(--tg-section-bg-color);
  overflow: hidden;
}

.skeleton-line {
  width: 100%;
  height: 10px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--tg-hint-color) 18%, transparent);
  animation: skeleton 1.2s ease-in-out infinite alternate;
}

.skeleton-line + .skeleton-line {
  width: 62%;
  margin-top: 9px;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.38);
}

.bottom-sheet {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 70;
  width: min(100%, 640px);
  max-height: min(88vh, 760px);
  margin: 0 auto;
  overflow: auto;
  padding: 7px 16px calc(18px + var(--safe-bottom));
  border: 1px solid var(--border-color);
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  background: var(--tg-section-bg-color);
  animation: sheet-in 180ms ease-out;
}

.sheet-handle {
  width: 38px;
  height: 4px;
  margin: 0 auto 7px;
  border-radius: 2px;
  background: var(--border-color);
}

.sheet-header {
  position: sticky;
  top: -7px;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0 10px;
  background: var(--tg-section-bg-color);
}

.sheet-header h2 {
  margin: 0;
  font-size: 20px;
}

.sheet-content,
.form-stack,
.detail-stack {
  display: grid;
  gap: 14px;
}

.field {
  display: grid;
  gap: 6px;
  color: var(--tg-hint-color);
  font-size: 12px;
  font-weight: 650;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  min-height: 46px;
  padding: 10px 11px;
  border: 1px solid var(--border-color);
  border-radius: 7px;
  outline: 0;
  background: var(--tg-bg-color);
  color: var(--tg-text-color);
  font-weight: 400;
}

.field textarea {
  min-height: 130px;
  resize: vertical;
}

.field-error {
  color: var(--danger-color);
  font-size: 12px;
}

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

.detail-row {
  display: grid;
  grid-template-columns: 104px minmax(0, 1fr);
  gap: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}

.detail-row dt {
  color: var(--tg-hint-color);
}

.detail-row dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  padding: 3px 7px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  color: var(--tg-hint-color);
  font-size: 11px;
}

.toast-region {
  position: fixed;
  right: 12px;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 12px);
  left: 12px;
  z-index: 90;
  display: grid;
  justify-items: center;
  pointer-events: none;
}

.toast {
  max-width: 420px;
  padding: 10px 14px;
  border-radius: 7px;
  background: var(--tg-text-color);
  color: var(--tg-bg-color);
  font-size: 13px;
  animation: toast-in 160ms ease-out;
}

.busy-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 10px;
  background: color-mix(in srgb, var(--tg-bg-color) 78%, transparent);
  backdrop-filter: blur(2px);
}

.spinner {
  width: 30px;
  height: 30px;
  border: 3px solid var(--border-color);
  border-top-color: var(--tg-button-color);
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

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

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

@keyframes sheet-in {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes skeleton {
  from { opacity: 0.42; }
  to { opacity: 0.9; }
}

@media (max-width: 380px) {
  .main-content {
    padding-inline: 10px;
  }

  .stats-grid {
    gap: 5px;
  }

  .stat-tile {
    height: 88px;
    padding: 8px 6px;
  }

  .memory-grid {
    grid-template-columns: 1fr;
  }

  .chip-row {
    margin-inline: -10px;
    padding-inline: 10px;
  }
}

@media (min-width: 680px) {
  .main-content {
    padding-inline: 22px;
  }

  .stats-grid {
    gap: 12px;
  }

  .memory-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .screen[data-screen="settings"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
  }

  .screen[data-screen="settings"] .screen-title {
    grid-column: 1 / -1;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
}
