:root {
  --paper: #f5f0e8;
  --paper-dark: #ede7d9;
  --ink: #2c2416;
  --ink-light: #6b5f4a;
  --ink-faint: #b5a88e;
  --grid: #c8bda8;
  --red-ink: #8b2e1a;
  --blue-ink: #1a3a6b;
  --spine: #d4c9b4;
  --shadow: rgba(44,36,22,0.15);
  --font-hand: 'Caveat', cursive;
  --font-type: 'Special Elite', cursive;
  --font-mono: 'DM Mono', monospace;
  --cell: 20px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  background: #e8e0d0;
  background-image:
    radial-gradient(ellipse at 20% 50%, #d4c9b4 0%, transparent 60%),
    radial-gradient(ellipse at 80% 20%, #e0d8c8 0%, transparent 50%);
  min-height: 100vh;
  font-family: var(--font-hand);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 12px 40px;
}

/* ── LOGIN ── */
#login-screen {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
}
.login-book {
  background: var(--paper);
  border-radius: 4px 12px 12px 4px;
  padding: 48px 56px;
  box-shadow: -6px 0 0 var(--spine), 0 8px 32px var(--shadow);
  max-width: 380px; width: 100%; text-align: center; position: relative;
  background-image: repeating-linear-gradient(transparent,transparent 27px,var(--grid) 27px,var(--grid) 28px);
}
.login-book::before {
  content:''; position:absolute; left:0; top:0; bottom:0; width:6px;
  background: linear-gradient(to right,#b8a88a,var(--spine)); border-radius:4px 0 0 4px;
}
.login-title { font-family:var(--font-type); font-size:28px; color:var(--ink); margin-bottom:6px; }
.login-sub   { font-size:16px; color:var(--ink-light); margin-bottom:36px; }
.google-btn {
  display:flex; align-items:center; justify-content:center; gap:10px;
  background:white; border:1.5px solid var(--grid); border-radius:6px;
  padding:12px 24px; font-family:var(--font-mono); font-size:13px;
  color:var(--ink); cursor:pointer; width:100%;
  box-shadow:0 2px 4px var(--shadow); transition:background .15s;
}
.google-btn:hover { background:var(--paper-dark); }
.login-error { font-family:var(--font-mono); font-size:11px; color:var(--red-ink); margin-top:12px; min-height:16px; }

/* ── LOADING ── */
#loading {
  display:flex; flex-direction:column; align-items:center;
  justify-content:center; min-height:80vh; gap:12px;
  font-family:var(--font-type); font-size:18px; color:var(--ink-light);
}
.spinner {
  width:32px; height:32px; border:2px solid var(--grid);
  border-top-color:var(--ink); border-radius:50%;
  animation:spin .8s linear infinite;
}
@keyframes spin { to { transform:rotate(360deg); } }

/* ── APP ── */
#app { display:none; width:100%; max-width:1020px; }
.top-bar {
  display:flex; align-items:center; justify-content:space-between;
  margin-bottom:16px; padding:0 4px;
}
.app-title { font-family:var(--font-type); font-size:22px; color:var(--ink); letter-spacing:1px; }
.save-dot {
  width:6px; height:6px; border-radius:50%; background:#4a7c59;
  opacity:0; transition:opacity .3s; display:inline-block; margin-left:6px;
}
.save-dot.saving { opacity:1; }
.user-info { display:flex; align-items:center; gap:10px; font-family:var(--font-mono); font-size:13px; color:var(--ink-light); }
.avatar { width:30px; height:30px; border-radius:50%; border:2px solid var(--grid); }
.sign-out-btn {
  background:none; border:1px solid var(--grid); border-radius:4px;
  padding:3px 8px; font-family:var(--font-mono); font-size:11px;
  color:var(--ink-light); cursor:pointer;
}
.sign-out-btn:hover { background:var(--paper-dark); }

/* ── NOTEBOOK ── */
.notebook {
  display:grid; grid-template-columns:minmax(0,1fr) minmax(0,1fr);
  background:var(--paper);
  border-radius:4px 12px 12px 4px;
  box-shadow:-8px 0 0 var(--spine), 4px 0 0 #ccc5b0,
    0 12px 40px var(--shadow), 0 4px 12px var(--shadow);
  position:relative; overflow:hidden;
}
/* Allow 3D page to extend beyond notebook bounds during flip */
.notebook.turn-fwd,
.notebook.turn-bwd {
  overflow:visible;
}
.page {
  padding:20px; position:relative; overflow:hidden;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size:20px 20px;
  background-color:var(--paper);
}
.page-left  { border-right:2px solid var(--spine); transform-origin:right center; }
.page-right { transform-origin:left center; }
.notebook::after {
  content:''; position:absolute; left:50%; top:0; bottom:0; width:12px;
  transform:translateX(-50%);
  background:linear-gradient(to right,rgba(0,0,0,.07),rgba(0,0,0,.02) 30%,transparent 50%,rgba(0,0,0,.02) 70%,rgba(0,0,0,.07));
  pointer-events:none; z-index:10;
}
.notebook::before {
  content:''; position:absolute; left:0; top:0; bottom:0; width:8px;
  background:linear-gradient(to right,#a89878,var(--spine)); z-index:5;
}
.page-right::after {
  content:''; position:absolute; bottom:0; right:0;
  width:0; height:0; border-style:solid;
  border-width:0 0 20px 20px;
  border-color:transparent transparent var(--paper-dark) transparent;
}

/* ── MONTH NAV ── */
.month-nav { display:flex; align-items:center; gap:8px; height:20px; margin-bottom:0; }
.nav-btn { background:none; border:none; font-family:var(--font-hand); font-size:20px; color:var(--ink-light); cursor:pointer; padding:0 4px; line-height:20px; }
.nav-btn:hover { color:var(--ink); }
.month-title { font-family:var(--font-type); font-size:16px; color:var(--ink); flex:1; letter-spacing:1px; line-height:20px; }
.section-label {
  font-family:var(--font-mono); font-size:9px; letter-spacing:2px;
  text-transform:uppercase; color:var(--ink-faint);
  margin-bottom:8px; border-bottom:1px solid var(--grid); padding-bottom:4px;
}

/* ── MOMENTS HEADING ── */
.moments-heading {
  font-family:var(--font-type);
  font-size:11px;
  letter-spacing:2px;
  text-transform:uppercase;
  color:var(--ink);
  border-bottom:1.5px solid var(--ink-faint);
  height:60px;
  line-height:60px;
  padding:0;
  margin:0;
}

/* ── LEFT PAGE: days + moments ── */
.days-moments { width:100%; border-collapse:collapse; }
.days-moments td { padding:0; vertical-align:top; }
.days-moments tr { border:none; }

/* day number cell — fixed width, centred */
.dm-day {
  width:20px; min-width:20px;
  font-family:var(--font-mono); font-size:11px;
  color:var(--ink-light); text-align:center;
  padding:0; vertical-align:middle;
  line-height:20px; height:20px;
}
.dm-day.today-num { color:var(--red-ink); font-weight:600; }

/* moment input (single line) */
.moment-ta {
  border:none; background:transparent;
  font-family:var(--font-hand); font-size:16px; color:var(--ink);
  width:100%; outline:none;
  padding:0 4px; line-height:20px; height:20px;
  display:block;
}
.moment-ta::placeholder { color:var(--ink-faint); font-size:13px; }

/* ── HISTORY ── */
.history-strip { display:flex; gap:5px; margin-top:12px; flex-wrap:wrap; align-items:center; }
.hist-label { font-family:var(--font-mono); font-size:10px; color:var(--ink-light); letter-spacing:1px; }
.hist-chip {
  font-family:var(--font-mono); font-size:10px; padding:2px 7px;
  border:1px solid var(--grid); border-radius:3px;
  color:var(--ink-light); cursor:pointer; background:transparent; transition:all .1s;
}
.hist-chip:hover, .hist-chip.active { background:var(--ink); color:var(--paper); border-color:var(--ink); }

/* ── RIGHT PAGE: habit grid ── */
.right-top { display:flex; justify-content:space-between; align-items:center; height:20px; margin-bottom:0; }
.add-habit-row { display:flex; gap:4px; align-items:center; }
.add-habit-input {
  border:none; border-bottom:1.5px solid var(--grid); background:transparent;
  font-family:var(--font-hand); font-size:14px; color:var(--ink);
  width:90px; outline:none; padding:2px 4px;
}
.add-habit-input::placeholder { color:var(--ink-faint); font-size:12px; }
.add-btn {
  background:none; border:1px solid var(--grid); border-radius:4px;
  width:22px; height:22px; font-size:16px; color:var(--ink-light);
  cursor:pointer; display:flex; align-items:center; justify-content:center;
  font-family:var(--font-hand);
}
.add-btn:hover { background:var(--paper-dark); }

.habit-wrap { overflow-x:auto; display:flex; align-items:flex-start; }
#sleep-graph { flex-shrink:0; display:block; }
.sleep-graph-mobile-wrap { display:none; }
#sleep-graph-mobile { display:block; width:100%; }

/* The grid table */
table.hg {
  border-collapse:collapse;
}

/* header row */
table.hg thead th {
  font-family:var(--font-mono); font-weight:400;
  color:var(--ink); text-align:center;
  border-bottom:1.5px solid var(--ink-faint);
  padding:0; overflow:hidden; white-space:nowrap;
}
th.gh-kg    { width:20px; min-width:20px; max-width:20px; font-size:9px; font-weight:400; }
th.gh-spacer, td.gd-spacer {
  width:20px; min-width:20px; max-width:20px;
  border:none !important; padding:0;
}
th.gh-sleep { width:20px; min-width:20px; max-width:20px; font-size:9px; font-weight:400; color:var(--blue-ink); }
th.gh-habit {
  writing-mode:vertical-rl; transform:rotate(180deg);
  height:60px; font-size:10px; font-weight:400; vertical-align:bottom;
  cursor:pointer; padding-bottom:2px;
  width:20px; min-width:20px; max-width:20px;
  color:var(--ink);
}
th.gh-habit.red-h { color:var(--red-ink); }
.streak-count {
  display:block; font-family:var(--font-mono); font-size:7px;
  color:var(--blue-ink); font-weight:400;
  writing-mode:horizontal-tb; transform:none;
}
th.gh-habit:hover { opacity:.6; }

/* body rows */
table.hg tbody td {
  padding:0; border:none;
  width:20px; min-width:20px; max-width:20px;
  height:var(--cell); text-align:center; vertical-align:middle;
}

/* kg / sleep inline inputs */
.cell-input {
  border:none; background:transparent;
  font-family:var(--font-mono); font-size:10px;
  text-align:center; width:20px; height:var(--cell);
  outline:none; padding:0; display:block; color:var(--ink);
}
.cell-input.sleep-inp { color:var(--blue-ink); }

/* tick cell — fills entire td */
.tick-wrap {
  width:20px; height:var(--cell);
  display:flex; align-items:center; justify-content:center;
  cursor:pointer; transition:background .1s;
}
.tick-wrap:hover { background:rgba(200,189,168,0.5); }
.tick-wrap:focus { outline:1.5px solid var(--ink-faint); outline-offset:-1px; }

/* the × mark itself */
.tick-mark {
  font-family:var(--font-hand); font-weight:700;
  font-size:16px; line-height:1;
  color:transparent;
  width:var(--cell); height:var(--cell);
  display:flex; align-items:center; justify-content:center;
}
.tick-wrap.on .tick-mark     { color:var(--ink); }
.tick-wrap.on.red .tick-mark { color:var(--red-ink); }

/* day number in grid */
td.gd-day {
  font-family:var(--font-mono); font-size:9px;
  color:var(--ink-faint); width:20px;
}
td.gd-day.today { color:var(--red-ink); font-weight:600; }


/* ── TODAY HIGHLIGHT ── */
tr.today-row { background:rgba(139,46,26,0.06); }

/* ── NEXT MONTH ── */
.next-note { margin-top:12px; border-top:1px dashed var(--grid); padding-top:8px; }
.next-label { font-family:var(--font-mono); font-size:9px; color:var(--ink-faint); letter-spacing:1px; text-transform:uppercase; margin-bottom:4px; }
.next-input {
  border:none; background:transparent;
  font-family:var(--font-hand); font-size:14px; color:var(--red-ink);
  width:100%; outline:none;
}
.next-input::placeholder { color:var(--ink-faint); font-size:12px; }

/* ── PAGE TURN ANIMATION ── */
.notebook {
  perspective:2200px;
  transform-style:preserve-3d;
}
.page {
  transition:none;
  transform-style:preserve-3d;
  backface-visibility:hidden;
}
.page-left  { transform-origin:right center; }
.page-right { transform-origin:left center; }

/*
 * Forward turn (next month): the right page lifts, folds over the spine,
 * and lands on the left side — like turning a real notebook page.
 *
 * A shadow overlay on the page simulates light bending across the paper.
 * The opposing page fades in gently underneath.
 */

/* ── FORWARD (next month) ── right page flips left over the spine ── */
.notebook.turn-fwd .page-right {
  animation: flipRight 0.6s cubic-bezier(0.645, 0.045, 0.355, 1.0) forwards;
  z-index: 20;
}
.notebook.turn-fwd .page-left {
  animation: revealUnder 0.6s ease-out;
  z-index: 1;
}

@keyframes flipRight {
  0%   {
    transform: rotateY(0deg);
    box-shadow: none;
  }
  15%  {
    transform: rotateY(-5deg) scale(1.01);
    box-shadow: -2px 4px 12px rgba(44,36,22,0.15);
  }
  50%  {
    transform: rotateY(-90deg) scale(1.02);
    box-shadow: -10px 8px 30px rgba(44,36,22,0.25);
  }
  85%  {
    transform: rotateY(-170deg) scale(1.01);
    box-shadow: -2px 4px 12px rgba(44,36,22,0.1);
  }
  100% {
    transform: rotateY(-180deg);
    box-shadow: none;
  }
}

/* ── BACKWARD (prev month) ── left page flips right over the spine ── */
.notebook.turn-bwd .page-left {
  animation: flipLeft 0.6s cubic-bezier(0.645, 0.045, 0.355, 1.0) forwards;
  z-index: 20;
}
.notebook.turn-bwd .page-right {
  animation: revealUnder 0.6s ease-out;
  z-index: 1;
}

@keyframes flipLeft {
  0%   {
    transform: rotateY(0deg);
    box-shadow: none;
  }
  15%  {
    transform: rotateY(5deg) scale(1.01);
    box-shadow: 2px 4px 12px rgba(44,36,22,0.15);
  }
  50%  {
    transform: rotateY(90deg) scale(1.02);
    box-shadow: 10px 8px 30px rgba(44,36,22,0.25);
  }
  85%  {
    transform: rotateY(170deg) scale(1.01);
    box-shadow: 2px 4px 12px rgba(44,36,22,0.1);
  }
  100% {
    transform: rotateY(180deg);
    box-shadow: none;
  }
}

/* The page underneath fades in as the flipping page reveals it */
@keyframes revealUnder {
  0%   { opacity: 0.4; transform: scale(0.98); }
  40%  { opacity: 0.5; }
  100% { opacity: 1;   transform: scale(1); }
}

/* Moving shadow across the spine during turn — simulates paper curling */
.notebook.turn-fwd::after,
.notebook.turn-bwd::after {
  animation: spineShadowPulse 0.6s ease-in-out;
}
@keyframes spineShadowPulse {
  0%   { opacity: 1; }
  40%  { opacity: 0.3; box-shadow: 0 0 20px rgba(44,36,22,0.2); }
  60%  { opacity: 0.3; box-shadow: 0 0 20px rgba(44,36,22,0.2); }
  100% { opacity: 1; }
}

/* ── RESPONSIVE ── */
@media (max-width:660px) {
  :root { --cell: 32px; }
  body { padding:10px 6px 40px; }

  /* Layout */
  .notebook { grid-template-columns:1fr; border-radius:4px; max-width:100vw; }
  .page-left { border-right:none; border-bottom:2px solid var(--spine); }
  .page { padding:12px; background-image:none; }
  .days-moments, table.hg {
    background-image:
      linear-gradient(var(--grid) 1px, transparent 1px),
      linear-gradient(90deg, var(--grid) 1px, transparent 1px);
    background-size:var(--cell) var(--cell);
  }
  table.hg thead th { border-bottom:1.5px solid var(--ink-faint); }
  .notebook::after { display:none; }
  .habit-wrap { max-width:100%; }

  /* Top bar */
  .app-title { font-size:18px; }
  #user-name { display:none; }

  /* Moments — larger touch targets */
  .dm-day { height:var(--cell); line-height:var(--cell); width:var(--cell); min-width:var(--cell); }
  .moment-ta { height:var(--cell); line-height:var(--cell); font-size:15px; }
  .moments-heading { height:48px; line-height:48px; }

  /* Habit grid — larger cells */
  table.hg tbody td {
    width:var(--cell); min-width:var(--cell); max-width:var(--cell);
    height:var(--cell);
  }
  th.gh-kg, th.gh-sleep {
    width:var(--cell); min-width:var(--cell); max-width:var(--cell);
    font-size:10px;
  }
  th.gh-habit {
    width:var(--cell); min-width:var(--cell); max-width:var(--cell);
    height:48px; font-size:11px;
  }
  th.gh-spacer, td.gd-spacer {
    width:var(--cell); min-width:var(--cell); max-width:var(--cell);
  }
  .tick-wrap { width:var(--cell); height:var(--cell); }
  .tick-mark { width:var(--cell); height:var(--cell); font-size:18px; }
  .cell-input { width:var(--cell); height:var(--cell); font-size:11px; }

  /* Sticky day column */
  th.gh-day {
    position:sticky; left:0; z-index:2;
    background:var(--paper); width:var(--cell); min-width:var(--cell); max-width:var(--cell);
    font-family:var(--font-mono); font-size:9px; font-weight:400;
    border-bottom:1.5px solid var(--ink-faint);
  }
  td.gd-day {
    position:sticky; left:0; z-index:2;
    background:var(--paper);
    font-family:var(--font-mono); font-size:11px;
    color:var(--ink-faint); text-align:center;
    width:var(--cell); min-width:var(--cell); max-width:var(--cell);
  }
  td.gd-day.today { color:var(--red-ink); font-weight:600; }

  /* Add habit row */
  .add-habit-input { width:80px; font-size:13px; }
  .add-btn { width:32px; height:32px; font-size:18px; }

  /* Sleep graph: hide vertical, show horizontal */
  #sleep-graph { display:none; }
  .sleep-graph-mobile-wrap { display:block; margin-top:12px; border-top:1px dashed var(--grid); padding-top:8px; }

  /* History strip */
  .hist-chip { padding:4px 9px; font-size:11px; }
}

/* Hide number input spinners */
.cell-input::-webkit-outer-spin-button,
.cell-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.cell-input[type=number] {
  -moz-appearance: textfield;
}

/* ── PRODUCT TOUR ── */
.tour-overlay {
  position:fixed; top:0; left:0; right:0; bottom:0;
  z-index:9999;
}
.tour-overlay::before {
  content:''; position:absolute; top:0; left:0; right:0; bottom:0;
  background:rgba(44,36,22,0.55);
  pointer-events:none;
}
.tour-spotlight {
  position:absolute;
  border-radius:6px;
  box-shadow:0 0 0 9999px rgba(44,36,22,0.55);
  background:transparent;
  transition:all .35s ease;
  z-index:10000;
  pointer-events:none;
}
.tour-tooltip {
  position:absolute;
  background:var(--paper);
  border:1.5px solid var(--grid);
  border-radius:8px;
  padding:16px 20px 12px;
  max-width:280px;
  box-shadow:0 8px 30px rgba(44,36,22,0.25);
  z-index:10001;
  transition:all .35s ease;
}
.tour-tooltip::before {
  content:'';
  position:absolute;
  width:12px; height:12px;
  background:var(--paper);
  border-left:1.5px solid var(--grid);
  border-top:1.5px solid var(--grid);
  z-index:-1;
}
.tour-tooltip.arrow-top::before {
  top:-7px; left:24px;
  transform:rotate(45deg);
}
.tour-tooltip.arrow-bottom::before {
  bottom:-7px; left:24px;
  transform:rotate(225deg);
}
.tour-tooltip.arrow-left::before {
  left:-7px; top:20px;
  transform:rotate(-45deg);
}
.tour-tooltip.arrow-right::before {
  right:-7px; top:20px;
  transform:rotate(135deg);
}
.tour-text {
  font-family:var(--font-hand); font-size:17px;
  color:var(--ink); line-height:1.4;
  margin-bottom:12px;
}
.tour-footer {
  display:flex; align-items:center; justify-content:space-between;
}
.tour-step-count {
  font-family:var(--font-mono); font-size:10px;
  color:var(--ink-faint);
}
.tour-btns { display:flex; gap:8px; }
.tour-skip {
  background:none; border:none;
  font-family:var(--font-mono); font-size:11px;
  color:var(--ink-faint); cursor:pointer;
  padding:4px 8px;
}
.tour-skip:hover { color:var(--ink); }
.tour-next {
  background:var(--ink); color:var(--paper);
  border:none; border-radius:4px;
  font-family:var(--font-mono); font-size:11px;
  padding:5px 14px; cursor:pointer;
  transition:opacity .15s;
}
.tour-next:hover { opacity:0.85; }
