:root {
  --bg: #0b0b0f;
  --bg2: #15151c;
  --tile: #1b1b24;
  --tile2: #22222e;
  --text: #f4f4f6;
  --muted: #8b8b97;
  --line: #2a2a36;
  --accent: #ff9f0a;
  --radius: 22px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior-y: contain;
}

body {
  min-height: 100dvh;
  padding: calc(var(--safe-top) + 8px) 14px calc(var(--safe-bottom) + 24px);
  max-width: 640px;
  margin: 0 auto;
  user-select: none;
}

/* ---------- Top bar ---------- */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin-bottom: 16px;
}
.date {
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: .3px;
  display: flex;
  align-items: center;
}
.sync-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  margin-left: 7px;
  background: var(--muted);
  transition: background .3s ease;
  cursor: pointer;
  /* enlarge tap target without changing visual size */
  padding: 8px;
  background-clip: content-box;
  box-sizing: content-box;
  margin: -8px -2px -8px -1px;
}
.sync-dot.ok { background: #30d158; }
.sync-dot.busy { background: var(--accent); animation: pulse 1s ease infinite; }
.sync-dot.off { background: #ff453a; }
@keyframes pulse { 50% { opacity: .35; } }
.brand {
  margin: 0;
  font-size: 20px;
  font-weight: 800;
  letter-spacing: .5px;
  text-align: center;
}
.icon-btn {
  justify-self: end;
  width: 38px; height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--tile);
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  display: grid; place-items: center;
  cursor: pointer;
  transition: transform .1s ease, background .2s ease;
}
.icon-btn.small { width: 32px; height: 32px; font-size: 16px; }
.icon-btn:active { transform: scale(.9); background: var(--tile2); }

/* ---------- Grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
@media (min-width: 460px) { .grid { grid-template-columns: repeat(3, 1fr); } }

.tile {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: var(--radius);
  background:
    radial-gradient(120% 120% at 50% 0%, var(--tile2), var(--tile));
  border: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  overflow: hidden;
  transition: transform .12s ease;
  touch-action: manipulation;
}
.tile:active { transform: scale(.96); }
.tile.done { border-color: transparent; }
.tile.done::before {
  content: "";
  position: absolute; inset: 0;
  background: var(--c);
  opacity: .16;
}
.tile.negative.done::before { opacity: .14; }

.ring {
  position: absolute;
  inset: 8px;
  pointer-events: none;
}
.ring circle { fill: none; stroke-width: 5; stroke-linecap: round; }
.ring .track { stroke: rgba(255,255,255,.07); }
.ring .prog {
  stroke: var(--c);
  transition: stroke-dashoffset .45s cubic-bezier(.4,0,.2,1);
  transform: rotate(-90deg);
  transform-origin: center;
}

.tile .emoji {
  font-size: clamp(30px, 9vw, 42px);
  line-height: 1;
  filter: drop-shadow(0 2px 6px rgba(0,0,0,.35));
  z-index: 1;
}
.tile .name {
  font-size: 13px;
  font-weight: 650;
  text-align: center;
  padding: 0 8px;
  z-index: 1;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tile .streak {
  position: absolute;
  top: 9px; right: 11px;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  display: flex; align-items: center; gap: 2px;
  z-index: 1;
}
.tile.has-streak .streak { color: var(--c); }
.tile .badge {
  position: absolute;
  top: 9px; left: 11px;
  font-size: 11px;
  z-index: 1;
  opacity: .7;
}
.tile .count {
  position: absolute;
  bottom: 9px;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  z-index: 1;
}

/* pop animation on completion */
@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); }
  100% { transform: scale(1); }
}
.tile.justdone .emoji { animation: pop .4s ease; }

.wiggle .tile { animation: wiggle .25s ease infinite alternate; }
@keyframes wiggle {
  from { transform: rotate(-1deg); }
  to { transform: rotate(1deg); }
}

/* ---------- Empty state ---------- */
.empty {
  text-align: center;
  margin-top: 22vh;
  color: var(--muted);
}
.empty-mark {
  width: 76px; height: 76px;
  margin: 0 auto 14px;
  border-radius: 24px;
  background: var(--tile);
  display: grid; place-items: center;
  font-size: 34px; color: var(--accent);
}
.empty p { margin: 0 0 18px; }

/* ---------- Buttons ---------- */
.primary {
  background: var(--accent);
  color: #1a1200;
  border: none;
  font-weight: 750;
  font-size: 15px;
  padding: 12px 20px;
  border-radius: 14px;
  cursor: pointer;
}
.primary:active { filter: brightness(.92); }
.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  font-weight: 650;
  font-size: 15px;
  padding: 12px 18px;
  border-radius: 14px;
  cursor: pointer;
}
.ghost.danger { color: #ff5d57; border-color: transparent; }

/* ---------- Sheets ---------- */
.sheet-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  backdrop-filter: blur(4px);
  display: flex; align-items: flex-end;
  z-index: 50;
  animation: fade .2s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  background: var(--bg2);
  border-radius: 26px 26px 0 0;
  padding: 10px 18px calc(var(--safe-bottom) + 18px);
  max-height: 92dvh;
  overflow-y: auto;
  animation: slideup .28s cubic-bezier(.2,.8,.2,1);
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle {
  width: 40px; height: 5px;
  background: var(--line);
  border-radius: 3px;
  margin: 4px auto 14px;
}
.sheet h2 { margin: 0 0 16px; font-size: 19px; }
.sheet-note { margin: -8px 0 18px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.key-status {
  margin: -8px 0 16px;
  color: #ff5d57; font-size: 13px; font-weight: 600;
}
input[type=password] {
  width: 100%;
  background: var(--tile);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 16px;
  padding: 12px 14px;
  border-radius: 12px;
  outline: none;
}
input[type=password]:focus { border-color: var(--accent); }

.field { display: block; margin-bottom: 18px; }
.field > span {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .6px;
  margin-bottom: 8px;
}
input[type=text] {
  width: 100%;
  background: var(--tile);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 16px;
  padding: 12px 14px;
  border-radius: 12px;
  outline: none;
}
input[type=text]:focus { border-color: var(--accent); }

.seg {
  display: grid; grid-template-columns: 1fr 1fr;
  background: var(--tile);
  border-radius: 12px;
  padding: 4px;
  gap: 4px;
}
.seg-item {
  border: none; background: transparent; color: var(--muted);
  font-size: 14px; font-weight: 700; padding: 9px;
  border-radius: 9px; cursor: pointer;
}
.seg-item.active { background: var(--accent); color: #1a1200; }

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
}
.emoji-grid button {
  aspect-ratio: 1; border: 1px solid transparent;
  background: var(--tile); border-radius: 11px;
  font-size: 21px; cursor: pointer; padding: 0;
}
.emoji-grid button.active { border-color: var(--accent); background: var(--tile2); }

.color-row { display: flex; flex-wrap: wrap; gap: 10px; }
.color-row button {
  width: 32px; height: 32px; border-radius: 50%;
  border: 2px solid transparent; cursor: pointer; padding: 0;
}
.color-row button.active { border-color: #fff; transform: scale(1.1); }

.stepper {
  display: inline-flex; align-items: center; gap: 18px;
  background: var(--tile); border-radius: 12px; padding: 6px 14px;
}
.stepper button {
  width: 30px; height: 30px; border-radius: 8px; border: none;
  background: var(--tile2); color: var(--text); font-size: 20px; cursor: pointer;
}
.stepper span { font-size: 17px; font-weight: 700; min-width: 20px; text-align: center; }

.sheet-actions { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.sheet-actions .spacer { flex: 1; }

/* ---------- Detail ---------- */
.detail-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.detail-icon {
  width: 52px; height: 52px; border-radius: 15px;
  display: grid; place-items: center; font-size: 28px;
  background: var(--tile);
}
.detail-head h2 { margin: 0; font-size: 19px; }
.detail-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.detail-head .icon-btn { margin-left: auto; }

.stats {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 8px; margin-bottom: 22px;
}
.stat {
  background: var(--tile); border-radius: 14px; padding: 12px 6px;
  text-align: center;
}
.stat b { display: block; font-size: 20px; font-weight: 800; }
.stat span { font-size: 11px; color: var(--muted); }

.cal-head { display: flex; align-items: center; justify-content: center; gap: 16px; margin-bottom: 10px; }
.cal-head span { font-weight: 700; min-width: 130px; text-align: center; }
.cal-dow {
  display: grid; grid-template-columns: repeat(7, 1fr);
  gap: 6px; margin-bottom: 6px;
}
.cal-dow span { text-align: center; font-size: 11px; color: var(--muted); font-weight: 700; }
.calendar {
  display: grid; grid-template-columns: repeat(7, 1fr);
  gap: 6px; margin-bottom: 18px;
}
.cell {
  aspect-ratio: 1; border-radius: 9px;
  display: grid; place-items: center;
  font-size: 12px; color: var(--muted);
  background: var(--tile);
}
.cell.empty { background: transparent; }
.cell.done { color: #fff; font-weight: 700; }
.cell.today { outline: 2px solid var(--muted); outline-offset: -2px; }
.cell.future { opacity: .35; }
