/* =========================================================================
   Habits — stylesheet
   Dark mode only. iOS / SwiftUI inspired. Mobile-first, capped for desktop.
   ========================================================================= */

/* ---- Reset ---- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
}

h1,
h2,
p {
  margin: 0;
}

/* Anything JS marks [hidden] must truly disappear, even if another rule
   later sets display on the same element. */
[hidden] {
  display: none !important;
}

/* ---- Design tokens ---- */
:root {
  --bg: #161618;
  --card: #232326;
  --fill: #3A3A3C;
  --text: #F2F2F7;
  --text-dim: #98989F;
  --separator: rgba(255, 255, 255, 0.08);
  --accent: #0A84FF; /* default only — JS overrides this per .habit-card */

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}

/* ---- Base / body ---- */
html,
body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.35;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

/* ---- Buttons (base) ---- */
button {
  touch-action: manipulation;
  -webkit-appearance: none;
  appearance: none;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  margin: 0;
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* =========================================================================
   Header
   ========================================================================= */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px 14px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--separator);
}

.app-title {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.4px;
  line-height: 1.1;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--card);
  color: var(--text-dim);
  font-size: 20px;
  transition: transform 150ms ease, opacity 150ms ease;
}

.icon-btn:active {
  transform: scale(0.92);
  opacity: 0.8;
}

.add-btn {
  color: var(--accent);
  font-size: 26px;
  font-weight: 400;
}

#menu-btn {
  color: var(--text-dim);
}

/* =========================================================================
   Habit list / empty state
   ========================================================================= */

.habit-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 560px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: calc(32px + env(safe-area-inset-bottom));
}

.empty-state {
  max-width: 560px;
  margin: 30vh auto 0;
  padding: 0 32px;
  text-align: center;
}

.empty-title {
  font-size: 20px;
  font-weight: 600;
}

.empty-sub {
  margin-top: 6px;
  font-size: 15px;
  color: var(--text-dim);
}

/* =========================================================================
   Habit card
   ========================================================================= */

.habit-card {
  background: var(--card);
  border: 1px solid var(--separator);
  border-radius: 20px;
  padding: 16px;
  animation: card-in 250ms ease both;
}

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.habit-name {
  min-width: 0;
  font-size: 19px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-top-right {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  gap: 6px;
}

.streak {
  display: flex;
  align-items: center;
  gap: 4px;
  transition: opacity 200ms ease;
}

.streak-count {
  font-size: 17px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.streak-flame {
  font-size: 15px;
  line-height: 1;
}

.habit-card.no-streak .streak {
  opacity: 0.35;
}

.habit-card.no-streak .streak-flame {
  filter: grayscale(1);
}

/* Smaller icon button variant used inside the card header */
.edit-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--fill) 50%, transparent);
  color: var(--text-dim);
  font-size: 17px;
  line-height: 1;
}

/* ---- Weekday row / month grid ---- */

.weekday-row {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 4px;
}

.weekday {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-align: center;
}

.month-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 14px;
}

.day-cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 5px;
  background: color-mix(in srgb, var(--fill) 35%, transparent);
  transition: background-color 250ms ease;
}

.day-cell.done {
  background: var(--accent);
}

.day-cell.today {
  box-shadow: inset 0 0 0 1.5px var(--accent);
}

/* Done + today: an inset ring would vanish against the solid accent fill,
   so switch to an outer ring so the "today" marker still reads. */
.day-cell.today.done {
  box-shadow: 0 0 0 1.5px color-mix(in srgb, var(--accent) 60%, transparent);
}

.day-cell.out {
  visibility: hidden;
}

/* ---- Complete button ---- */

.complete-btn {
  width: 100%;
  height: 50px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  transition: background-color 200ms ease, color 200ms ease, transform 200ms ease;
}

.complete-btn:active {
  transform: scale(0.97);
}

.complete-btn.light-accent {
  color: rgba(0, 0, 0, 0.75);
}

.complete-btn.done {
  background: color-mix(in srgb, var(--accent) 22%, var(--card));
  color: var(--accent);
}

/* =========================================================================
   Backdrops (shared) — sheet / dialog / action menu overlays
   ========================================================================= */

.sheet-backdrop,
.dialog-backdrop,
.menu-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 12, 0.55);
}

/* Sheet backdrop: JS toggles [hidden] + an "open" class, so the fade is
   driven by a transition keyed off that class. */
.sheet-backdrop {
  z-index: 40;
  opacity: 0;
  transition: opacity 300ms ease;
}

.sheet-backdrop.open {
  opacity: 1;
}

/* Dialog / menu backdrops double as their own flex centering container
   (their content is a DOM child, not a sibling). JS toggles [hidden] plus
   the same "open" class as the sheet, so keying the fade off .open animates
   both opening AND closing — the children fade with their parent. */
.dialog-backdrop,
.menu-backdrop {
  z-index: 50;
  display: flex;
  opacity: 0;
  transition: opacity 300ms ease;
}

.dialog-backdrop.open,
.menu-backdrop.open {
  opacity: 1;
}

.dialog-backdrop {
  align-items: center;
  justify-content: center;
}

.menu-backdrop {
  flex-direction: column;
  justify-content: flex-end;
  padding: 12px calc(12px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom))
    calc(12px + env(safe-area-inset-left));
}

/* =========================================================================
   Sheet (add / edit habit)
   ========================================================================= */

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 41;
  max-width: 560px;
  margin: 0 auto;
  background: var(--card);
  border-radius: 22px 22px 0 0;
  padding: 8px 20px calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.4);
  transform: translateY(100%);
  transition: transform 300ms cubic-bezier(0.32, 0.72, 0, 1);
}

.sheet.open {
  transform: translateY(0);
}

.sheet:not(.open) {
  transform: translateY(100%);
}

.sheet-grabber {
  width: 36px;
  height: 5px;
  margin: 4px auto 10px;
  border-radius: 3px;
  background: var(--fill);
}

.sheet-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.sheet-title {
  font-size: 17px;
  font-weight: 600;
  text-align: center;
}

.sheet-btn {
  font-size: 17px;
  color: var(--accent);
  padding: 4px 2px;
  white-space: nowrap;
}

.sheet-header .sheet-btn:first-child {
  justify-self: start;
}

.sheet-header .sheet-btn:last-child {
  justify-self: end;
}

.sheet-btn-primary {
  font-weight: 600;
}

.sheet-btn[disabled] {
  opacity: 0.4;
}

.text-field {
  width: 100%;
  border: none;
  border-radius: 12px;
  background: color-mix(in srgb, var(--fill) 55%, transparent);
  padding: 13px 14px;
  font-family: inherit;
  font-size: 17px;
  color: var(--text);
}

.text-field::placeholder {
  color: var(--text-dim);
}

.text-field:focus {
  outline: none;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 60%, transparent);
}

.swatch-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  justify-items: center;
  gap: 12px;
  margin-top: 16px;
}

.swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  padding: 0;
  transition: transform 150ms ease, box-shadow 150ms ease;
}

.swatch.selected {
  box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--text-dim);
  transform: scale(1.05);
}

.delete-btn {
  width: 100%;
  height: 48px;
  margin-top: 24px;
  border-radius: 12px;
  background: rgba(255, 69, 58, 0.15);
  color: #FF453A;
  font-size: 17px;
  font-weight: 600;
  transition: opacity 150ms ease;
}

.delete-btn:active {
  opacity: 0.6;
}

/* =========================================================================
   Dialog (confirm delete)
   ========================================================================= */

.dialog {
  z-index: 51;
  width: min(280px, 80vw);
  background: #2A2A2D;
  border-radius: 16px;
  padding: 20px 16px 8px;
  text-align: center;
  animation: dialog-in 200ms cubic-bezier(0.32, 0.72, 0, 1) both;
}

.dialog-title {
  font-size: 17px;
  font-weight: 600;
}

.dialog-message {
  margin: 6px 0 14px;
  font-size: 13px;
  color: var(--text-dim);
}

.dialog-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin: 0 -16px;
  border-top: 1px solid var(--separator);
}

.dialog-btn {
  padding: 12px;
  font-size: 17px;
  color: var(--accent);
  transition: opacity 150ms ease;
}

.dialog-btn:active {
  opacity: 0.6;
}

.dialog-actions .dialog-btn:first-child {
  border-right: 1px solid var(--separator);
}

.dialog-btn-destructive {
  color: #FF453A;
  font-weight: 600;
}

/* =========================================================================
   Action menu (export / import / cancel)
   ========================================================================= */

.action-menu {
  z-index: 51;
  display: flex;
  flex-direction: column;
  width: 100%;
  animation: menu-in 200ms ease both;
}

.menu-item {
  width: 100%;
  padding: 16px;
  background: var(--card);
  font-size: 17px;
  color: var(--accent);
  text-align: center;
  transition: opacity 150ms ease;
}

.menu-item:active {
  opacity: 0.6;
}

/* First non-cancel item gets the top rounding */
.menu-item:not(.menu-cancel):first-child {
  border-radius: 14px 14px 0 0;
}

/* The non-cancel item immediately followed by Cancel is the last of the
   group and gets the bottom rounding (works for any number of items). */
.menu-item:not(.menu-cancel):has(+ .menu-cancel) {
  border-radius: 0 0 14px 14px;
}

/* Hairline between consecutive non-cancel items */
.menu-item:not(.menu-cancel) ~ .menu-item:not(.menu-cancel) {
  border-top: 1px solid var(--separator);
}

.menu-cancel {
  margin-top: 8px;
  border-radius: 14px;
  background: #2A2A2D;
  font-weight: 600;
}

/* =========================================================================
   Animations
   ========================================================================= */

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

@keyframes dialog-in {
  from {
    opacity: 0;
    transform: scale(0.92);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

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

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