/* ============================================================
   STREAKS — Tally Board Design System
   Aesthetic: Warm industrial minimalism. Like chalk on slate.
   Fonts: Playfair Display (display) + DM Mono (data/mono)
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,600;0,700;1,400&family=DM+Mono:ital,wght@0,300;0,400;0,500;1,300&display=swap');

/* ── Design tokens ─────────────────────────────────────────── */
:root {
  /* Base palette */
  --ink:        #1a1714;   /* near-black with brown warmth */
  --ink-mid:    #2c2824;   /* card backgrounds */
  --ink-light:  #3d3832;   /* borders, dividers */
  --chalk:      #f0ead8;   /* primary text — aged ivory */
  --chalk-dim:  #a89f8c;   /* secondary text — faded chalk */
  --chalk-faint:#5a5348;   /* placeholder, disabled */

  /* Accent — ember / amber fire */
  --ember:      #e8a033;   /* streak count, active elements */
  --ember-dim:  #c4872a;   /* hover darken */
  --ember-glow: rgba(232, 160, 51, 0.15);

  /* State — sage green for "done today" */
  --done:       #6b9e7a;
  --done-dim:   #4f7a5c;
  --done-glow:  rgba(107, 158, 122, 0.12);

  /* Danger */
  --danger:     #c0544a;
  --danger-dim: #a04038;
  --danger-glow:rgba(192, 84, 74, 0.12);

  /* Layout */
  --radius-sm:  4px;
  --radius:     8px;
  --radius-lg:  14px;
  --max-width:  720px;

  /* Shadows */
  --shadow-card: 0 2px 12px rgba(0,0,0,0.45), 0 1px 3px rgba(0,0,0,0.3);
  --shadow-inset: inset 0 1px 3px rgba(0,0,0,0.4);
}

/* ── Reset & base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; -webkit-font-smoothing: antialiased; }

body {
  background-color: var(--ink);
  color: var(--chalk);
  font-family: 'DM Mono', monospace;
  font-weight: 300;
  min-height: 100vh;
  /* Subtle grain texture via SVG noise */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.03'/%3E%3C/svg%3E");
}

/* ── Typography ────────────────────────────────────────────── */
h1, h2, h3 {
  font-family: 'Playfair Display', serif;
  font-weight: 600;
  line-height: 1.2;
  color: var(--chalk);
}

h1 { font-size: 2rem; }
h2 { font-size: 1.4rem; }

a {
  color: var(--ember);
  text-decoration: none;
  transition: color 0.15s ease;
}
a:hover { color: var(--ember-dim); }

/* ── Site shell ────────────────────────────────────────────── */
.site-header {
  border-bottom: 1px solid var(--ink-light);
  padding: 0 1.5rem;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: var(--ink);
  /* very subtle top glow */
  box-shadow: 0 1px 0 var(--ink-light), 0 4px 20px rgba(0,0,0,0.6);
}

.site-logo {
  font-family: 'Playfair Display', serif;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--chalk);
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
}

.site-logo .logo-fire {
  font-size: 1.1rem;
  /* Subtle flicker animation */
  animation: flicker 3s ease-in-out infinite;
}

@keyframes flicker {
  0%, 100% { opacity: 1; transform: scaleY(1); }
  45%       { opacity: 0.85; transform: scaleY(0.96); }
  50%       { opacity: 1; transform: scaleY(1.04); }
  55%       { opacity: 0.9; transform: scaleY(0.97); }
}

.site-nav {
  display: flex;
  align-items: center;
  gap: 1rem;
}

/* ── Page layout ───────────────────────────────────────────── */
.page-wrapper {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 2.5rem 1.5rem 4rem;
}

.page-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 2rem;
  gap: 1rem;
}

.page-title {
  font-family: 'Playfair Display', serif;
  font-size: 1.75rem;
  font-weight: 600;
}

.page-meta {
  font-size: 0.75rem;
  color: var(--chalk-faint);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  align-self: center;
}

/* ── Buttons ───────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1.1rem;
  border-radius: var(--radius-sm);
  font-family: 'DM Mono', monospace;
  font-size: 0.78rem;
  font-weight: 400;
  letter-spacing: 0.04em;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
  white-space: nowrap;
  text-decoration: none;
}

.btn:active { transform: translateY(1px); }

/* Primary — ember amber, filled */
.btn-primary {
  background: var(--ember);
  color: var(--ink);
  border-color: var(--ember);
  font-weight: 500;
}
.btn-primary:hover {
  background: var(--ember-dim);
  border-color: var(--ember-dim);
  color: var(--ink);
}

/* Ghost — chalk outline */
.btn-ghost {
  background: transparent;
  color: var(--chalk-dim);
  border-color: var(--ink-light);
}
.btn-ghost:hover {
  border-color: var(--chalk-faint);
  color: var(--chalk);
  background: rgba(255,255,255,0.03);
}

/* Danger ghost */
.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
  opacity: 0.7;
}
.btn-danger:hover {
  background: var(--danger-glow);
  opacity: 1;
}

/* Mark Done */
.btn-done {
  background: transparent;
  color: var(--done);
  border-color: var(--done);
  padding: 0.45rem 0.9rem;
}
.btn-done:hover {
  background: var(--done-glow);
}

/* Already done state */
.btn-done-today {
  background: var(--done-glow);
  color: var(--done);
  border-color: var(--done-dim);
  cursor: default;
  opacity: 0.8;
}
.btn-done-today:hover { transform: none; }

/* ── Habit card / row ──────────────────────────────────────── */
.habits-list {
  display: flex;
  flex-direction: column;
  gap: 1px; /* gap becomes the border between rows */
}

.habit-row {
  background: var(--ink-mid);
  border: 1px solid var(--ink-light);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 0.35rem 1rem;
  transition: border-color 0.2s ease, background 0.2s ease;
  /* Staggered entrance animation */
  opacity: 0;
  animation: slideIn 0.35s ease forwards;
}

.habit-row:hover {
  border-color: var(--ink-light);
  background: #302c27;
}

/* Stagger children */
.habit-row:nth-child(1)  { animation-delay: 0.05s; }
.habit-row:nth-child(2)  { animation-delay: 0.10s; }
.habit-row:nth-child(3)  { animation-delay: 0.15s; }
.habit-row:nth-child(4)  { animation-delay: 0.20s; }
.habit-row:nth-child(5)  { animation-delay: 0.25s; }
.habit-row:nth-child(6)  { animation-delay: 0.30s; }
.habit-row:nth-child(7)  { animation-delay: 0.35s; }
.habit-row:nth-child(8)  { animation-delay: 0.40s; }
.habit-row:nth-child(n+9){ animation-delay: 0.45s; }

@keyframes slideIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Done-today variant — subtle green wash */
.habit-row.is-done-today {
  border-color: rgba(107, 158, 122, 0.3);
  background: #252e28;
}

/* ── Habit row internals ───────────────────────────────────── */
.habit-name {
  font-family: 'Playfair Display', serif;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--chalk);
  grid-column: 1;
  grid-row: 1;
  align-self: center;
}

.habit-cue {
  font-size: 0.72rem;
  color: var(--chalk-faint);
  letter-spacing: 0.03em;
  grid-column: 1;
  grid-row: 2;
  /* Show cue as a subtle trigger hint */
}
.habit-cue::before {
  content: "when ";
  color: var(--chalk-faint);
  font-style: italic;
}

.habit-actions {
  grid-column: 2;
  grid-row: 1 / 3;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  justify-content: flex-end;
}

/* ── Streak display ────────────────────────────────────────── */
.streak-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-family: 'DM Mono', monospace;
  font-size: 1.1rem;
  font-weight: 500;
  color: var(--ember);
  min-width: 3.5rem;
  justify-content: flex-end;
}

/* Tally marks — the distinctive visual */
.streak-tally {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 20px;
}

.tally-group {
  display: flex;
  align-items: flex-end;
  gap: 1.5px;
  position: relative;
}

/* Each tally stick */
.tally-mark {
  width: 2px;
  height: 14px;
  background: var(--ember);
  border-radius: 1px;
  opacity: 0.9;
}

/* The crossing diagonal on 5th mark */
.tally-group.complete::after {
  content: '';
  position: absolute;
  width: calc(100% + 2px);
  height: 2px;
  background: var(--ember);
  border-radius: 1px;
  top: 50%;
  left: -1px;
  transform: rotate(-22deg) translateY(-50%);
}

/* Partial tally marks (dimmed future slots) */
.tally-mark.empty {
  background: var(--ink-light);
  opacity: 0.5;
}

/* ── Empty state ───────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  border: 1px dashed var(--ink-light);
  border-radius: var(--radius-lg);
}

.empty-state-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  opacity: 0.4;
}

.empty-state-title {
  font-family: 'Playfair Display', serif;
  font-size: 1.2rem;
  color: var(--chalk-dim);
  margin-bottom: 0.5rem;
}

.empty-state-body {
  font-size: 0.8rem;
  color: var(--chalk-faint);
  margin-bottom: 1.5rem;
}

/* ── Forms ─────────────────────────────────────────────────── */
.form-card {
  background: var(--ink-mid);
  border: 1px solid var(--ink-light);
  border-radius: var(--radius-lg);
  padding: 2rem 2rem 2.25rem;
  box-shadow: var(--shadow-card);
  /* Entrance */
  animation: slideIn 0.3s ease forwards;
}

.form-card h2 {
  font-family: 'Playfair Display', serif;
  font-size: 1.25rem;
  margin-bottom: 1.75rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--ink-light);
  color: var(--chalk-dim);
}

.form-section {
  margin-bottom: 1.75rem;
}

.form-section-label {
  font-size: 0.65rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--chalk-faint);
  margin-bottom: 0.75rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

@media (max-width: 560px) {
  .form-row { grid-template-columns: 1fr; }
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.form-group label {
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--chalk-dim);
}

.form-group input[type="text"],
.form-group input[type="email"],
.form-group textarea {
  background: var(--ink);
  border: 1px solid var(--ink-light);
  border-radius: var(--radius-sm);
  color: var(--chalk);
  font-family: 'DM Mono', monospace;
  font-size: 0.88rem;
  font-weight: 300;
  padding: 0.65rem 0.85rem;
  width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  box-shadow: var(--shadow-inset);
  outline: none;
}

.form-group input[type="text"]:focus,
.form-group input[type="email"]:focus,
.form-group textarea:focus {
  border-color: var(--ember);
  box-shadow: var(--shadow-inset), 0 0 0 3px var(--ember-glow);
}

.form-group input::placeholder,
.form-group textarea::placeholder {
  color: var(--chalk-faint);
  font-style: italic;
}

/* Field hint text */
.form-hint {
  font-size: 0.68rem;
  color: var(--chalk-faint);
  font-style: italic;
}

/* The "habit loop" visual grouping */
.habit-loop-wrapper {
  border: 1px solid var(--ink-light);
  border-radius: var(--radius);
  padding: 1.25rem;
  position: relative;
  margin-bottom: 1.25rem;
}

.habit-loop-connector {
  position: absolute;
  left: 1.25rem;
  top: 3rem;
  bottom: 3rem;
  width: 2px;
  background: linear-gradient(to bottom, var(--ember-dim), var(--done));
  border-radius: 1px;
  opacity: 0.4;
}

.habit-loop-fields {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding-left: 1.5rem;
}

.habit-loop-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.habit-loop-field label {
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--chalk-faint);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.habit-loop-field label .step-icon {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid var(--ink-light);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.6rem;
  color: var(--chalk-faint);
  flex-shrink: 0;
  margin-left: -1.85rem; /* pull into connector zone */
  background: var(--ink-mid);
}

.habit-loop-field input[type="text"] {
  background: var(--ink);
  border: 1px solid var(--ink-light);
  border-radius: var(--radius-sm);
  color: var(--chalk);
  font-family: 'DM Mono', monospace;
  font-size: 0.88rem;
  font-weight: 300;
  padding: 0.6rem 0.85rem;
  width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  box-shadow: var(--shadow-inset);
  outline: none;
}

.habit-loop-field input:focus {
  border-color: var(--ember);
  box-shadow: var(--shadow-inset), 0 0 0 3px var(--ember-glow);
}

/* Form footer */
.form-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 1.25rem;
  border-top: 1px solid var(--ink-light);
  margin-top: 1.5rem;
  gap: 0.75rem;
}

/* ── HTMX swap animations ──────────────────────────────────── */

/* Row fades in fresh after HTMX swap */
.htmx-settling .habit-row {
  animation: swapIn 0.3s ease forwards;
}

@keyframes swapIn {
  from { opacity: 0; transform: scale(0.98); }
  to   { opacity: 1; transform: scale(1); }
}

/* Button shows spinner during request */
.htmx-request .btn-done .btn-text::after,
.htmx-request .btn-done-today .btn-text::after {
  content: '…';
}

/* ── Notification / flash ──────────────────────────────────── */
.flash {
  padding: 0.65rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.78rem;
  margin-bottom: 1.25rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  animation: slideIn 0.25s ease forwards;
}

.flash-success {
  background: var(--done-glow);
  border: 1px solid var(--done-dim);
  color: var(--done);
}

.flash-error {
  background: var(--danger-glow);
  border: 1px solid var(--danger-dim);
  color: var(--danger);
}

/* ── Utility ───────────────────────────────────────────────── */
.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;
}

.text-dim   { color: var(--chalk-dim); }
.text-faint { color: var(--chalk-faint); }
.text-ember { color: var(--ember); }
.text-done  { color: var(--done); }

.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }

/* ── Responsive tweaks ─────────────────────────────────────── */
@media (max-width: 480px) {
  .page-header { flex-direction: column; align-items: flex-start; gap: 0.75rem; }
  .habit-row   { grid-template-columns: 1fr; }
  .habit-actions {
    grid-column: 1;
    grid-row: 3;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  .habit-row { gap: 0.5rem; }
}

/* ── Scrollbar ─────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--ink); }
::-webkit-scrollbar-thumb { background: var(--ink-light); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--chalk-faint); }

/* ── Auth pages ────────────────────────────────────────── */
.auth-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.auth-wrapper { width: 100%; max-width: 420px; padding: 1rem; }
.auth-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 2.5rem 2rem; }
.auth-header { text-align: center; margin-bottom: 2rem; }
.auth-title { font-family: var(--font-display); font-size: 2rem; color: var(--chalk); margin: 0.5rem 0 0.25rem; }
.auth-subtitle { color: var(--chalk-dim); margin: 0; font-size: 0.9rem; }
.auth-switch { text-align: center; margin-top: 1.5rem; color: var(--chalk-dim); font-size: 0.85rem; }
.auth-switch a { color: var(--ember); }
.alert { padding: 0.75rem 1rem; border-radius: 6px; margin-bottom: 1rem; font-size: 0.875rem; }
.alert-error { background: rgba(200,50,50,0.15); color: #e07070; border: 1px solid rgba(200,50,50,0.3); }
.alert-info { background: rgba(100,150,200,0.15); color: #90b8d8; border: 1px solid rgba(100,150,200,0.3); }
.alert-success { background: rgba(80,160,100,0.15); color: #80c090; border: 1px solid rgba(80,160,100,0.3); }
.form-error { display: block; color: #e07070; font-size: 0.8rem; margin-top: 0.25rem; }

/* ── Streak chain calendar ──────────────────────────────── */
.streak-chain-wrapper {
  grid-column: 1 / -1;
}
/* <details> handles open/close natively; we only style the summary toggle */
.streak-chain-wrapper > summary {
  list-style: none;
  display: inline-block;
}
.streak-chain-wrapper > summary::-webkit-details-marker { display: none; }
.streak-chain-wrapper .chain-full {
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.28s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.22s ease;
}
.streak-chain-wrapper[open] .chain-full { max-height: 60px; opacity: 1; }
.btn-chain-toggle {
  background: transparent; border: none;
  color: var(--chalk-faint, #555); padding: 0.3rem 0.4rem;
  cursor: pointer; font-size: 0.6rem; line-height: 1;
  transition: color 0.15s ease, transform 0.2s ease;
}
details[open] .btn-chain-toggle { transform: rotate(180deg); }
.chain-full { padding: 0.5rem 0 0.3rem; }
.chain-track { display: flex; align-items: center; gap: 3px; }
.chain-cell {
  flex: 0 0 auto; width: 14px; height: 14px; border-radius: 50%;
  position: relative; cursor: default;
}
.chain-cell-inner { display: block; width: 100%; height: 100%; border-radius: 50%; }
.chain-cell--accepted .chain-cell-inner {
  background: var(--ember, #e8a033);
  box-shadow: 0 0 4px rgba(232, 160, 51, 0.35);
}
.chain-cell--pending_approval .chain-cell-inner {
  border: 1.5px dashed var(--ember, #e8a033); background: transparent;
  animation: pendingPulse 2s ease-in-out infinite;
}
@keyframes pendingPulse {
  0%,100% { opacity: 1; } 50% { opacity: 0.45; }
}
.chain-cell--rejected .chain-cell-inner {
  background: rgba(192,84,74,0.28); border: 1px solid rgba(192,84,74,0.45);
}
.chain-cell--missed .chain-cell-inner {
  border: 1px solid rgba(255,255,255,0.12); background: transparent; opacity: 0.5;
}
.chain-cell--today::before, .chain-cell--today::after {
  content: ''; position: absolute; top: -3px; bottom: -3px;
  width: 4px; border: 1.5px solid rgba(240,234,216,0.4); border-radius: 1px;
}
.chain-cell--today::before { left: -4px; border-right: none; }
.chain-cell--today::after  { right: -4px; border-left: none; }
