-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
556 lines (505 loc) · 25.5 KB
/
Copy pathindex.html
File metadata and controls
556 lines (505 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Night Ritual – Sebastian</title>
<link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@300;400;600&family=DM+Mono:wght@300;400&display=swap" rel="stylesheet"/>
<style>
:root {
--bg: #0d1117; --bg2: #161b22; --bg3: #1a1f2e;
--text: #e8e0d5; --muted: #6b7280;
--faint: rgba(255,255,255,0.04); --border: rgba(255,255,255,0.08);
--gold: #c4977a; --gold2: #e8b88a;
--blue: #4a6fa5; --green: #7b9e87; --rust: #b07d62; --purple: #8e7bb5;
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
body {
min-height: 100vh;
background: linear-gradient(160deg, var(--bg) 0%, var(--bg2) 50%, var(--bg3) 100%);
font-family: 'Cormorant Garamond', Georgia, serif;
color: var(--text); padding-bottom: 5rem;
}
/* TABS */
.tabs {
display: flex; border-bottom: 1px solid var(--border);
background: rgba(0,0,0,0.5); position: sticky; top: 0; z-index: 100;
backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.tab {
flex: 1; padding: 1rem 0.3rem; text-align: center;
font-family: 'DM Mono', monospace; font-size: 0.58rem; letter-spacing: 0.12em;
color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent;
transition: all 0.2s; user-select: none;
}
.tab.active { color: var(--gold); border-bottom-color: var(--gold); }
/* PAGES */
.page { display: none; padding: 1.5rem 1rem 2rem; max-width: 560px; margin: 0 auto; }
.page.active { display: block; }
/* HEADER */
.page-header { text-align: center; margin-bottom: 1.5rem; }
.eyebrow { font-family: 'DM Mono', monospace; font-size: 0.6rem; letter-spacing: 0.2em; color: var(--muted); margin-bottom: 0.35rem; }
.page-title { font-size: clamp(1.5rem, 5vw, 2.2rem); font-weight: 300; line-height: 1.1; }
.page-sub { font-family: 'DM Mono', monospace; font-size: 0.65rem; color: var(--muted); margin-top: 0.3rem; }
/* PROGRESS */
.prog-wrap { margin-bottom: 1.4rem; }
.prog-labels { display: flex; justify-content: space-between; margin-bottom: 0.4rem; }
.prog-labels span { font-family: 'DM Mono', monospace; font-size: 0.63rem; color: var(--muted); letter-spacing: 0.1em; }
.prog-count { color: var(--gold) !important; font-size: 0.78rem !important; }
.prog-bar { height: 5px; background: var(--border); border-radius: 4px; overflow: hidden; }
.prog-fill { height: 100%; border-radius: 4px; background: linear-gradient(90deg, var(--blue), var(--gold)); transition: width 0.45s cubic-bezier(.4,0,.2,1); }
/* CARDS */
.card { border-radius: 14px; padding: 1.15rem 1.15rem 0.85rem; background: var(--faint); border: 1px solid var(--border); margin-bottom: 0.75rem; transition: border-color 0.3s; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.card-left { display: flex; align-items: center; gap: 0.5rem; }
.card-left h2 { font-size: 1rem; font-weight: 400; color: #d4ccc5; transition: color 0.3s; }
.card-badge { font-family: 'DM Mono', monospace; font-size: 0.6rem; color: #4b5563; letter-spacing: 0.08em; transition: color 0.3s; }
/* ITEMS */
.item { display: flex; align-items: center; gap: 0.65rem; padding: 0.6rem 0.5rem; border-radius: 8px; cursor: pointer; transition: background 0.15s; user-select: none; }
.item:active { background: rgba(255,255,255,0.06); }
.dot { width: 20px; height: 20px; border-radius: 50%; border: 1.5px solid #374151; flex-shrink: 0; display: flex; align-items: center; justify-content: center; transition: all 0.2s; }
.dot svg { display: none; }
.item.checked .dot svg { display: block; }
.item-label { font-size: 0.9rem; color: #c9c3bc; line-height: 1.35; transition: all 0.2s; }
.item.checked .item-label { color: #3a404d; text-decoration: line-through; }
/* SAVE BTN */
.save-btn {
width: 100%; padding: 1rem;
background: linear-gradient(135deg, var(--blue), var(--gold));
border: none; border-radius: 12px; color: white;
font-family: 'DM Mono', monospace; font-size: 0.75rem; letter-spacing: 0.1em;
cursor: pointer; margin-top: 0.85rem; transition: opacity 0.2s;
}
.save-btn:active { opacity: 0.8; }
/* TOAST */
.toast {
position: fixed; bottom: 2rem; left: 50%;
transform: translateX(-50%) translateY(100px);
background: #1a2a1a; border: 1px solid var(--green);
color: var(--green); font-family: 'DM Mono', monospace;
font-size: 0.7rem; padding: 0.7rem 1.4rem;
border-radius: 50px; letter-spacing: 0.08em;
transition: transform 0.3s cubic-bezier(.4,0,.2,1);
z-index: 9999; white-space: nowrap; pointer-events: none;
}
.toast.show { transform: translateX(-50%) translateY(0); }
/* STATS */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.65rem; margin-bottom: 1.15rem; }
.stat-box { background: var(--faint); border: 1px solid var(--border); border-radius: 12px; padding: 1rem; text-align: center; }
.stat-num { font-size: 1.85rem; font-weight: 300; color: var(--gold); display: block; line-height: 1; }
.stat-label { font-family: 'DM Mono', monospace; font-size: 0.56rem; color: var(--muted); letter-spacing: 0.1em; margin-top: 0.3rem; display: block; }
.section-stat { margin-bottom: 0.55rem; }
.section-stat-head { display: flex; justify-content: space-between; font-size: 0.8rem; margin-bottom: 0.28rem; color: #c9c3bc; }
.section-stat-head span:last-child { font-family: 'DM Mono', monospace; font-size: 0.63rem; color: var(--muted); }
.mini-bar { height: 4px; background: var(--border); border-radius: 3px; overflow: hidden; }
.mini-fill { height: 100%; border-radius: 3px; transition: width 0.6s ease; }
/* PERIOD TOGGLE */
.period-toggle { display: flex; background: var(--faint); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 1.15rem; overflow: hidden; }
.period-btn { flex: 1; padding: 0.6rem; font-family: 'DM Mono', monospace; font-size: 0.6rem; letter-spacing: 0.08em; color: var(--muted); cursor: pointer; border: none; background: none; transition: all 0.2s; }
.period-btn.active { background: rgba(196,151,122,0.15); color: var(--gold); }
.section-label { font-family: 'DM Mono', monospace; font-size: 0.6rem; letter-spacing: 0.15em; color: var(--muted); margin: 1.15rem 0 0.65rem; }
/* HISTORY */
.history-entry { background: var(--faint); border: 1px solid var(--border); border-radius: 12px; padding: 1rem 1.15rem; margin-bottom: 0.65rem; }
.history-date { font-family: 'DM Mono', monospace; font-size: 0.62rem; color: var(--muted); letter-spacing: 0.1em; margin-bottom: 0.45rem; }
.history-score { display: flex; align-items: baseline; gap: 0.5rem; margin-bottom: 0.4rem; }
.history-score .big { font-size: 1.5rem; font-weight: 300; }
.history-score .small { font-family: 'DM Mono', monospace; font-size: 0.65rem; color: var(--muted); }
.history-pills { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.pill { font-family: 'DM Mono', monospace; font-size: 0.56rem; letter-spacing: 0.04em; padding: 0.2rem 0.5rem; border-radius: 20px; border: 1px solid; }
/* BACKUP */
.backup-card { background: var(--faint); border: 1px solid var(--border); border-radius: 14px; padding: 1.2rem; margin-bottom: 0.85rem; }
.backup-title { font-family: 'DM Mono', monospace; font-size: 0.6rem; letter-spacing: 0.15em; color: var(--muted); margin-bottom: 0.55rem; }
.backup-desc { font-size: 0.85rem; color: #a0998f; line-height: 1.55; margin-bottom: 0.85rem; }
.outline-btn { width: 100%; padding: 0.75rem; background: none; border: 1px solid var(--border); border-radius: 10px; color: var(--muted); font-family: 'DM Mono', monospace; font-size: 0.63rem; letter-spacing: 0.08em; cursor: pointer; transition: all 0.2s; }
.outline-btn:active { border-color: rgba(255,255,255,0.3); color: var(--text); }
/* INSTRUCTION BOX */
.info-box { background: rgba(74,111,165,0.1); border: 1px solid rgba(74,111,165,0.3); border-radius: 12px; padding: 1rem 1.1rem; margin-bottom: 1.2rem; }
.info-box p { font-family: 'DM Mono', monospace; font-size: 0.65rem; color: #8ba3c7; line-height: 1.8; letter-spacing: 0.03em; }
.info-box p b { color: #adc0e0; }
/* EMPTY */
.empty-state { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }
.empty-state .emoji { font-size: 2rem; margin-bottom: 0.65rem; }
.empty-state p { font-family: 'DM Mono', monospace; font-size: 0.68rem; line-height: 1.7; }
@keyframes glow {
from { text-shadow: 0 0 8px var(--gold); }
to { text-shadow: 0 0 20px var(--gold2), 0 0 40px rgba(232,184,138,0.4); }
}
.glow { animation: glow 2s ease-in-out infinite alternate; color: var(--gold2) !important; }
</style>
</head>
<body>
<div class="tabs">
<div class="tab active" onclick="showPage('tonight')">TONIGHT</div>
<div class="tab" onclick="showPage('stats')">PROGRESS</div>
<div class="tab" onclick="showPage('history')">HISTORY</div>
<div class="tab" onclick="showPage('backup')">BACKUP</div>
</div>
<!-- TONIGHT -->
<div class="page active" id="page-tonight">
<div class="page-header">
<p class="eyebrow">NIGHTLY RITUAL</p>
<h1 class="page-title" id="tonight-title">Good Night, Sebastian</h1>
<p class="page-sub" id="tonight-date"></p>
</div>
<div class="prog-wrap">
<div class="prog-labels">
<span>TONIGHT</span>
<span class="prog-count" id="tonight-count">0/15</span>
</div>
<div class="prog-bar"><div class="prog-fill" id="tonight-fill" style="width:0%"></div></div>
</div>
<div id="tonight-sections"></div>
<button class="save-btn" onclick="saveTonight()">✦ Save Tonight's Progress</button>
</div>
<!-- STATS -->
<div class="page" id="page-stats">
<div class="page-header">
<p class="eyebrow">YOUR CONSISTENCY</p>
<h1 class="page-title">Progress Report</h1>
</div>
<div class="period-toggle">
<button class="period-btn active" onclick="setPeriod('week')">THIS WEEK</button>
<button class="period-btn" onclick="setPeriod('month')">THIS MONTH</button>
<button class="period-btn" onclick="setPeriod('all')">ALL TIME</button>
</div>
<div id="stats-content"></div>
</div>
<!-- HISTORY -->
<div class="page" id="page-history">
<div class="page-header">
<p class="eyebrow">PAST NIGHTS</p>
<h1 class="page-title">History</h1>
</div>
<div id="history-content"></div>
</div>
<!-- BACKUP -->
<div class="page" id="page-backup">
<div class="page-header">
<p class="eyebrow">DATA SAFETY</p>
<h1 class="page-title">Backup & Restore</h1>
</div>
<div class="info-box">
<p>
<b>📱 To keep data saved on your phone:</b><br>
1. Open this file in <b>Safari</b><br>
2. Tap Share → <b>"Add to Home Screen"</b><br>
3. Always open the app from your <b>Home Screen icon</b><br><br>
Data saves to your phone's browser storage and stays there permanently as long as you always use the same browser.
</p>
</div>
<div class="backup-card">
<p class="backup-title">⬇ EXPORT BACKUP</p>
<p class="backup-desc">Download all your history as a file. Save it to iCloud, Google Drive, or your Files app as a safety net.</p>
<button class="outline-btn" onclick="exportData()">Download Backup File</button>
</div>
<div class="backup-card">
<p class="backup-title">⬆ IMPORT BACKUP</p>
<p class="backup-desc">Restore from a previously exported file. Your existing data will be merged, nothing is overwritten.</p>
<input type="file" id="import-file" accept=".json" style="display:none" onchange="importData(event)"/>
<button class="outline-btn" onclick="document.getElementById('import-file').click()">Import Backup File</button>
</div>
</div>
<div class="toast" id="toast"></div>
<script>
// ── SECTIONS ──────────────────────────────────────────────
const SECTIONS = [
{ id:"oral", title:"Oral Care", emoji:"🦷", color:"#4a6fa5",
items:["Water pick floss","Brush teeth","Floss","Mouth wash","Mouth probiotics"] },
{ id:"body", title:"Body & Movement", emoji:"💪", color:"#7b9e87",
items:["2 minute sit-ups","3 minute plank"] },
{ id:"health", title:"Health & Supplements", emoji:"💊", color:"#b07d62",
items:["Glass of water","Finasteride"] },
{ id:"mindfulness", title:"Mindfulness & Spirit", emoji:"🧘", color:"#8e7bb5",
items:["3 minute meditation","Pray & give thanks"] },
{ id:"sleep", title:"Sleep Prep", emoji:"🌙", color:"#c4977a",
items:["Dim lights & warm lighting","Phone on Do Not Disturb","No screens 30 min before bed","Set tomorrow's alarm"] }
];
const TOTAL = SECTIONS.reduce((s,sec) => s + sec.items.length, 0);
const KEY = 'sebastian_nr_v3';
let state = {}, history = {}, currentPeriod = 'week';
// ── STORAGE ───────────────────────────────────────────────
function load() {
try {
const raw = localStorage.getItem(KEY);
if (raw) { const d = JSON.parse(raw); history = d.history || {}; }
else history = {};
} catch(e) { history = {}; }
}
function save() {
try { localStorage.setItem(KEY, JSON.stringify({ history, savedAt: Date.now() })); return true; }
catch(e) { return false; }
}
// ── HELPERS ───────────────────────────────────────────────
function todayKey() {
const d = new Date();
return `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;
}
function fmtDate(d) {
return `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;
}
function showToast(msg) {
const t = document.getElementById('toast');
t.textContent = msg; t.classList.add('show');
setTimeout(() => t.classList.remove('show'), 2500);
}
// ── TONIGHT ───────────────────────────────────────────────
function initTonight() {
const d = new Date();
document.getElementById('tonight-date').textContent =
d.toLocaleDateString('en-US', { weekday:'long', month:'long', day:'numeric' });
const today = todayKey();
state = {};
SECTIONS.forEach(sec => sec.items.forEach((_, i) => {
const k = `${sec.id}-${i}`;
state[k] = history[today]?.checks?.[k] || false;
}));
buildSections();
updateProgress();
}
function buildSections() {
const container = document.getElementById('tonight-sections');
container.innerHTML = '';
SECTIONS.forEach(sec => {
const card = document.createElement('div');
card.className = 'card'; card.id = 'card-' + sec.id;
card.innerHTML = `
<div class="card-head">
<div class="card-left">
<span style="font-size:1rem">${sec.emoji}</span>
<h2 id="stitle-${sec.id}">${sec.title}</h2>
</div>
<span class="card-badge" id="sbadge-${sec.id}">0/${sec.items.length}</span>
</div>
<div id="sitems-${sec.id}"></div>
`;
const wrap = card.querySelector('#sitems-' + sec.id);
sec.items.forEach((text, i) => {
const k = `${sec.id}-${i}`;
const el = document.createElement('div');
el.className = 'item' + (state[k] ? ' checked' : '');
el.id = 'ni-' + k;
el.innerHTML = `
<div class="dot" id="nd-${k}"
style="border-color:${state[k] ? sec.color : '#374151'};
background:${state[k] ? sec.color : 'transparent'}">
<svg width="9" height="7" viewBox="0 0 10 8" fill="none">
<path d="M1 4L3.5 6.5L9 1" stroke="white" stroke-width="1.5"
stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
<span class="item-label">${text}</span>
`;
el.onclick = () => toggle(k, sec);
wrap.appendChild(el);
});
container.appendChild(card);
refreshSection(sec);
});
}
function toggle(k, sec) {
state[k] = !state[k];
const el = document.getElementById('ni-' + k);
const dot = document.getElementById('nd-' + k);
el.classList.toggle('checked', state[k]);
dot.style.background = state[k] ? sec.color : 'transparent';
dot.style.borderColor = state[k] ? sec.color : '#374151';
refreshSection(sec);
updateProgress();
}
function refreshSection(sec) {
const checked = sec.items.filter((_, i) => state[`${sec.id}-${i}`]).length;
const done = checked === sec.items.length;
const badge = document.getElementById('sbadge-' + sec.id);
const title = document.getElementById('stitle-' + sec.id);
const card = document.getElementById('card-' + sec.id);
if (!badge) return;
badge.textContent = done ? 'DONE ✓' : `${checked}/${sec.items.length}`;
badge.style.color = done ? sec.color : '#4b5563';
title.style.color = done ? sec.color : '#d4ccc5';
card.style.borderColor = done ? sec.color + '55' : 'rgba(255,255,255,0.08)';
}
function updateProgress() {
const checked = Object.values(state).filter(Boolean).length;
const pct = Math.round(checked / TOTAL * 100);
document.getElementById('tonight-fill').style.width = pct + '%';
document.getElementById('tonight-count').textContent = `${checked}/${TOTAL}`;
const t = document.getElementById('tonight-title');
if (checked === TOTAL) { t.textContent = 'Night Complete ✦'; t.classList.add('glow'); }
else { t.textContent = 'Good Night, Sebastian'; t.classList.remove('glow'); }
}
function saveTonight() {
const today = todayKey();
const checked = Object.values(state).filter(Boolean).length;
const scores = {};
SECTIONS.forEach(sec => {
scores[sec.id] = {
checked: sec.items.filter((_, i) => state[`${sec.id}-${i}`]).length,
total: sec.items.length
};
});
history[today] = { checks: {...state}, total: checked, max: TOTAL, sections: scores, savedAt: Date.now() };
const ok = save();
showToast(ok ? '✓ Saved to your phone' : '✗ Save failed — try again');
}
// ── STATS ─────────────────────────────────────────────────
function setPeriod(p) {
currentPeriod = p;
document.querySelectorAll('.period-btn').forEach((b, i) =>
b.classList.toggle('active', ['week','month','all'][i] === p));
buildStats();
}
function buildStats() {
const keys = Object.keys(history).sort();
if (!keys.length) {
document.getElementById('stats-content').innerHTML =
`<div class="empty-state"><div class="emoji">🌙</div>
<p>No data yet.<br>Complete your first night<br>and hit Save!</p></div>`;
return;
}
const now = new Date();
let filtered;
if (currentPeriod === 'week') {
const start = new Date(now); start.setDate(now.getDate() - now.getDay());
filtered = keys.filter(k => k >= fmtDate(start));
} else if (currentPeriod === 'month') {
const prefix = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}`;
filtered = keys.filter(k => k.startsWith(prefix));
} else { filtered = keys; }
if (!filtered.length) {
document.getElementById('stats-content').innerHTML =
`<div class="empty-state"><p>No data for this period yet.</p></div>`;
return;
}
const days = filtered.length;
const totalC = filtered.reduce((s, k) => s + history[k].total, 0);
const maxP = filtered.reduce((s, k) => s + history[k].max, 0);
const avgPct = Math.round(totalC / maxP * 100);
const perfect = filtered.filter(k => history[k].total === history[k].max).length;
const consist = Math.round(perfect / days * 100);
const streak = calcStreak(keys);
const secAvg = {};
SECTIONS.forEach(sec => {
const c = filtered.reduce((s, k) => s + (history[k].sections?.[sec.id]?.checked || 0), 0);
const t = filtered.reduce((s, k) => s + (history[k].sections?.[sec.id]?.total || sec.items.length), 0);
secAvg[sec.id] = t > 0 ? Math.round(c / t * 100) : 0;
});
document.getElementById('stats-content').innerHTML = `
<div class="stat-grid">
<div class="stat-box"><span class="stat-num">${avgPct}%</span><span class="stat-label">AVG COMPLETION</span></div>
<div class="stat-box"><span class="stat-num">${consist}%</span><span class="stat-label">PERFECT NIGHTS</span></div>
<div class="stat-box"><span class="stat-num">${days}</span><span class="stat-label">DAYS TRACKED</span></div>
<div class="stat-box"><span class="stat-num">${streak}</span><span class="stat-label">DAY STREAK 🔥</span></div>
</div>
<p class="section-label">BY CATEGORY</p>
${SECTIONS.map(sec => `
<div class="section-stat">
<div class="section-stat-head">
<span>${sec.emoji} ${sec.title}</span><span>${secAvg[sec.id]}%</span>
</div>
<div class="mini-bar">
<div class="mini-fill" style="width:${secAvg[sec.id]}%;background:${sec.color}"></div>
</div>
</div>
`).join('')}
<p class="section-label" style="margin-top:1.4rem">INSIGHT</p>
<div class="card" style="padding:1rem 1.15rem">
<p style="font-size:0.88rem;line-height:1.65;color:#c9c3bc">
${generateInsight(secAvg, avgPct, streak, perfect, days)}
</p>
</div>
`;
}
function calcStreak(sortedKeys) {
if (!sortedKeys.length) return 0;
let streak = 0, check = todayKey();
const set = new Set(sortedKeys);
while (set.has(check)) {
streak++;
const d = new Date(check + 'T12:00:00');
d.setDate(d.getDate() - 1);
check = fmtDate(d);
}
return streak;
}
function generateInsight(secAvg, avg, streak, perfect, total) {
const w = SECTIONS.reduce((a, b) => secAvg[a.id] < secAvg[b.id] ? a : b);
const s = SECTIONS.reduce((a, b) => secAvg[a.id] > secAvg[b.id] ? a : b);
if (!total) return "Start tracking to see insights here.";
if (avg >= 90) return `You're crushing it, Sebastian. ${streak > 2 ? `A ${streak}-day streak shows real discipline. ` : ''}Strongest area: ${s.emoji} ${s.title} at ${secAvg[s.id]}%. Keep the momentum.`;
if (avg >= 70) return `Solid at ${avg}% — momentum is building. ${w.emoji} ${w.title} at ${secAvg[w.id]}% is your biggest opportunity this week.`;
if (avg >= 50) return `You're showing up and that's what matters. ${s.emoji} ${s.title} is your anchor at ${secAvg[s.id]}%. Stack ${w.emoji} ${w.title} right after it.`;
return `${total} night${total > 1 ? 's' : ''} tracked — the streak starts now. Lead with ${s.emoji} ${s.title} every night this week and build from there.`;
}
// ── HISTORY ───────────────────────────────────────────────
function buildHistory() {
const keys = Object.keys(history).sort().reverse();
const c = document.getElementById('history-content');
if (!keys.length) {
c.innerHTML = `<div class="empty-state"><div class="emoji">📅</div>
<p>No history yet.<br>Save tonight's routine to start.</p></div>`;
return;
}
c.innerHTML = keys.map(k => {
const e = history[k];
const pct = Math.round(e.total / e.max * 100);
const d = new Date(k + 'T12:00:00');
const ds = d.toLocaleDateString('en-US', { weekday:'long', month:'short', day:'numeric' });
const col = pct >= 90 ? '#7b9e87' : pct >= 60 ? '#c4977a' : '#b07d62';
const pills = SECTIONS.map(sec => {
const s = e.sections?.[sec.id]; if (!s) return '';
const done = s.checked === s.total;
return `<span class="pill" style="color:${done ? sec.color : '#4b5563'};border-color:${done ? sec.color+'55' : 'rgba(255,255,255,0.07)'}">${sec.emoji} ${sec.title}</span>`;
}).join('');
return `
<div class="history-entry">
<div class="history-date">${ds.toUpperCase()}</div>
<div class="history-score">
<span class="big" style="color:${col}">${pct}%</span>
<span class="small">${e.total}/${e.max} completed</span>
</div>
<div class="history-pills">${pills}</div>
</div>`;
}).join('');
}
// ── BACKUP ────────────────────────────────────────────────
function exportData() {
const data = { history, exportedAt: new Date().toISOString(), version: 3 };
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url; a.download = `night-routine-backup-${todayKey()}.json`;
a.click(); URL.revokeObjectURL(url);
showToast('✓ Backup downloaded');
}
function importData(event) {
const file = event.target.files[0]; if (!file) return;
const reader = new FileReader();
reader.onload = e => {
try {
const data = JSON.parse(e.target.result);
if (!data.history) throw new Error('Invalid');
Object.assign(history, data.history);
save();
initTonight();
showToast(`✓ Imported ${Object.keys(data.history).length} days`);
} catch { showToast('✗ Invalid backup file'); }
};
reader.readAsText(file);
event.target.value = '';
}
// ── NAV ───────────────────────────────────────────────────
function showPage(id) {
document.querySelectorAll('.page').forEach(p => p.classList.remove('active'));
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.getElementById('page-' + id).classList.add('active');
const idx = ['tonight','stats','history','backup'].indexOf(id);
document.querySelectorAll('.tab')[idx].classList.add('active');
if (id === 'stats') buildStats();
if (id === 'history') buildHistory();
}
// ── START ─────────────────────────────────────────────────
load();
initTonight();
</script>
</body>
</html>