:root {
  --bg: #1a1d21;
  --bg-elevated: #25282e;
  --card: #2c3038;
  --card-hover: #343842;
  --text: #f2f2f7;
  --text-muted: #8e8e93;
  --text-dim: #636366;
  --accent: #d4a017;
  --accent-soft: #c9a227;
  --flame: #ff9500;
  --flame-dim: #5a4a3a;
  --nav-bg: #2a2d33;
  --nav-active: #3a3d45;
  --border: rgba(255, 255, 255, 0.06);
  --danger: #ff453a;
  --ok: #30d158;
  --radius: 14px;
  --radius-sm: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --nav-h: 72px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
  user-select: none;
}

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

.tab {
  display: none;
  flex: 1;
  flex-direction: column;
  min-height: 0;
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 8px);
}

.tab.active {
  display: flex;
}

/* ===== Top bar ===== */
.top-bar {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: 8px;
  padding: 12px 14px 8px;
  position: relative;
  z-index: 20;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.15s;
}

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

.icon-btn.active {
  background: rgba(212, 160, 23, 0.2);
  color: var(--accent);
}

.day-selector {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
}

.day-selector .chevron {
  opacity: 0.7;
  transition: transform 0.2s;
}

.day-selector.open .chevron {
  transform: rotate(180deg);
}

.day-menu {
  position: absolute;
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
  width: min(240px, calc(100% - 48px));
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  z-index: 30;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  animation: pop 0.18s ease;
}

.day-menu.hidden {
  display: none;
}

.day-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 1rem;
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
}

.day-menu button:hover,
.day-menu button:focus-visible {
  background: rgba(255, 255, 255, 0.06);
}

.day-menu button.selected {
  color: var(--accent);
  font-weight: 600;
}

@keyframes pop {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-6px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0) scale(1);
  }
}

.edit-toolbar {
  display: flex;
  gap: 8px;
  padding: 0 14px 10px;
  animation: fade 0.2s ease;
}

.edit-toolbar.hidden {
  display: none;
}

.toolbar-btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 0.9rem;
  font-weight: 600;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}

.toolbar-btn.primary {
  background: var(--accent);
  color: #1a1a1a;
}

@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ===== Task list ===== */
.task-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 14px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.task-list:empty::after {
  content: "Нет задач на этот день.\AНажмите «Код задач» в режиме редактирования.";
  white-space: pre-line;
  color: var(--text-dim);
  text-align: center;
  margin-top: 48px;
  font-size: 0.95rem;
  line-height: 1.5;
}

.task-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 4px 0;
  transition: background 0.15s, box-shadow 0.15s, transform 0.15s;
  touch-action: pan-y;
}

.task-card.dragging {
  opacity: 0.85;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.4);
  transform: scale(1.02);
  z-index: 50;
}

.task-card.drop-target {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.task-card.drop-before {
  box-shadow: 0 -3px 0 0 var(--accent);
}

.task-card.drop-after {
  box-shadow: 0 3px 0 0 var(--accent);
}

.task-row {
  display: grid;
  grid-template-columns: 44px 1fr 40px;
  align-items: center;
  min-height: 52px;
  padding: 4px 8px 4px 4px;
  gap: 2px;
}

.task-row.sub {
  grid-template-columns: 44px 1fr;
  min-height: 44px;
  padding-left: 28px;
  opacity: 0.92;
}

.check {
  width: 28px;
  height: 28px;
  margin: 0 auto;
  border-radius: 50%;
  border: 2px solid var(--text-dim);
  background: transparent;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: border-color 0.15s, background 0.15s;
  flex-shrink: 0;
}

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

.check.done::after {
  content: "";
  width: 7px;
  height: 12px;
  border: solid #1a1a1a;
  border-width: 0 2.5px 2.5px 0;
  transform: rotate(45deg) translate(-1px, -1px);
}

.task-row.sub .check {
  width: 22px;
  height: 22px;
  border-width: 1.5px;
}

.task-row.sub .check.done::after {
  width: 5px;
  height: 9px;
}

.task-title {
  font-size: 1.05rem;
  font-weight: 500;
  color: var(--text-muted);
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-title.done {
  color: var(--accent-soft);
  text-decoration: line-through;
  text-decoration-color: var(--accent);
  text-decoration-thickness: 2px;
}

.expand-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  border-radius: 8px;
}

.expand-btn svg {
  transition: transform 0.25s ease;
}

.task-card.expanded .expand-btn svg {
  transform: rotate(180deg);
}

.subtasks {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.28s ease;
  overflow: hidden;
}

.task-card.expanded .subtasks {
  grid-template-rows: 1fr;
}

.subtasks-inner {
  min-height: 0;
  padding-bottom: 6px;
}

body.edit-mode .task-card {
  cursor: grab;
}

body.edit-mode .task-card:active {
  cursor: grabbing;
}

body.edit-mode .check,
body.edit-mode .expand-btn {
  pointer-events: none;
  opacity: 0.45;
}

.ghost-slot {
  height: 56px;
  border-radius: var(--radius);
  border: 2px dashed rgba(212, 160, 23, 0.45);
  background: rgba(212, 160, 23, 0.08);
  margin: 0;
  transition: height 0.15s;
}

/* ===== Streak tab ===== */
#tab-streak {
  align-items: center;
  justify-content: flex-start;
  padding-top: 36px;
  gap: 28px;
}

.streak-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.flame-wrap {
  filter: drop-shadow(0 8px 24px rgba(255, 149, 0, 0.35));
  animation: flicker 2.8s ease-in-out infinite;
}

@keyframes flicker {
  0%, 100% { transform: scale(1) translateY(0); filter: drop-shadow(0 8px 24px rgba(255, 149, 0, 0.35)); }
  50% { transform: scale(1.04) translateY(-3px); filter: drop-shadow(0 12px 28px rgba(255, 149, 0, 0.5)); }
}

.streak-count {
  font-size: 2rem;
  font-weight: 800;
  color: var(--flame);
  letter-spacing: -0.03em;
}

.week-calendar {
  width: min(340px, calc(100% - 40px));
  background: #fff;
  border-radius: 12px;
  padding: 14px 12px 16px;
  color: #1c1c1e;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
}

.week-calendar-head {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 10px;
  text-align: center;
  font-size: 0.7rem;
  font-weight: 700;
  color: #8e8e93;
  letter-spacing: 0.04em;
}

.week-calendar-row {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  align-items: center;
  justify-items: center;
}

.day-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #d1d1d6;
  display: grid;
  place-items: center;
  font-size: 0.65rem;
  font-weight: 700;
  color: transparent;
  transition: background 0.2s, transform 0.2s;
}

.day-dot.done {
  background: var(--flame);
  box-shadow: 0 0 0 3px rgba(255, 149, 0, 0.25);
}

.day-dot.today {
  outline: 2px solid #1c1c1e;
  outline-offset: 2px;
}

.day-dot.missed {
  background: #aeaeb2;
}

/* ===== Bottom nav ===== */
.bottom-nav {
  position: fixed;
  left: 50%;
  bottom: calc(12px + var(--safe-bottom));
  transform: translateX(-50%);
  width: min(280px, calc(100% - 48px));
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 6px;
  background: var(--nav-bg);
  border-radius: 999px;
  z-index: 40;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.4);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 8px 10px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.nav-item.active {
  background: var(--nav-active);
  color: var(--text);
}

.nav-label {
  font-size: 0.72rem;
  font-weight: 600;
}

.calendar-icon {
  width: 28px;
  height: 28px;
  border-radius: 5px;
  background: #fff;
  color: #1c1c1e;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1;
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.cal-month {
  font-size: 0.45rem;
  font-weight: 800;
  background: #ff3b30;
  color: #fff;
  width: 100%;
  text-align: center;
  padding: 1px 0;
  letter-spacing: 0.02em;
}

.cal-day {
  font-size: 0.85rem;
  font-weight: 800;
  padding-top: 1px;
}

.flame-nav {
  font-size: 1.25rem;
  line-height: 1.2;
}

/* ===== Modals ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
}

.modal-sheet {
  position: relative;
  width: min(400px, 100%);
  background: var(--bg-elevated);
  border-radius: 18px;
  padding: 20px 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  animation: sheet-up 0.22s ease;
  border: 1px solid var(--border);
}

.modal-sheet.wide {
  max-height: min(80vh, 560px);
}

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

.modal-sheet h2 {
  font-size: 1.15rem;
  font-weight: 700;
  text-align: center;
}

.modal-hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.4;
  margin-top: -6px;
}

.modal-input {
  width: 100%;
  border: none;
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  font-size: 1.05rem;
  padding: 14px 14px;
  outline: none;
}

.modal-input:focus {
  box-shadow: 0 0 0 2px rgba(212, 160, 23, 0.45);
}

.code-area {
  width: 100%;
  min-height: 220px;
  flex: 1;
  border: none;
  border-radius: 12px;
  background: #121417;
  color: #e8e8ed;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.8rem;
  line-height: 1.45;
  padding: 12px;
  resize: vertical;
  outline: none;
  user-select: text;
}

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

.modal-btn {
  border: none;
  border-radius: 12px;
  padding: 13px;
  font-size: 1rem;
  font-weight: 650;
  cursor: pointer;
}

.modal-btn.ghost {
  background: var(--card);
  color: var(--text);
}

.modal-btn.accent {
  background: var(--accent);
  color: #1a1a1a;
}

/* Telegram MainButton space hint */
body.tg-expand #app {
  min-height: 100dvh;
}
