/* ============================================================
   Little folded paper slips at lavender dusk: soft, warm, matte
   ============================================================ */

:root {
  /* Surface: soft lilac grey with a faint warm glow, like dusk */
  --surface: #e7e2ee;
  --surface-deep: #dbd4e5;
  --glow: rgba(248, 222, 222, 0.6);
  --on-surface: #3d3850;
  --on-surface-muted: #6e6884;
  --thread-color: rgba(61, 56, 80, 0.28);

  /* Paper tones: warm and soft against the cool lilac */
  --paper-0: #fdf8f0; /* cream */
  --paper-1: #fbe7d9; /* peach */
  --paper-2: #f8e1e6; /* pink */
  --paper-3: #fbf2da; /* butter */
  --ink: #3d3850;
  --ink-muted: #7a7489;
  --rule: rgba(61, 56, 80, 0.1);

  --accent: #c46d7e;       /* warm rose: seal, hearts, buttons */
  --accent-ink: #fff7f6;

  --shadow: 0 1px 2px rgba(45, 38, 64, 0.18), 0 6px 16px rgba(45, 38, 64, 0.14);

  /* Crumpled-paper relief: SVG noise lit from the top-left, multiplied onto each slip */
  --wrinkle: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='320' height='320'%3E%3Cfilter id='w' x='0' y='0' width='100%' height='100%'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.011 0.016' numOctaves='4' seed='11' stitchTiles='stitch' result='n'/%3E%3CfeDiffuseLighting in='n' surfaceScale='2.6' lighting-color='%23fff' diffuseConstant='1.15'%3E%3CfeDistantLight azimuth='225' elevation='58'/%3E%3C/feDiffuseLighting%3E%3C/filter%3E%3Crect width='100%' height='100%' filter='url(%23w)'/%3E%3C/svg%3E");
  --wrinkle-strength: 0.55;
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='g' x='0' y='0' width='100%' height='100%'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85 0.6' numOctaves='2' seed='4' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.32  0 0 0 0 0.3  0 0 0 0 0.36  0.45 0 0 0 -0.18'/%3E%3C/filter%3E%3Crect width='100%' height='100%' filter='url(%23g)'/%3E%3C/svg%3E");
  --pen: #3d3850;          /* deep violet-grey ink */

  --hand: 'Caveat', 'Segoe Print', 'Bradley Hand', cursive;
  --serif: 'Fraunces', Georgia, 'Times New Roman', serif;
  --ui: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Dusky violet, after sunset */
    --surface: #353046;
    --surface-deep: #2b2739;
    --glow: rgba(214, 140, 160, 0.14);
    --on-surface: #ece8f4;
    --on-surface-muted: #b8b1c8;
    --thread-color: rgba(236, 232, 244, 0.24);
    /* Paper dims a little at night */
    --paper-0: #f0ebe3;
    --paper-1: #eeddd1;
    --paper-2: #ecd7dc;
    --paper-3: #eee6d0;
    --accent: #d68596;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 20px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { background: var(--surface); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--on-surface);
  font: 16px/1.5 var(--ui);
  /* A faint warm glow from the top over a soft vignette */
  background:
    radial-gradient(ellipse 70% 45% at 50% 0%, var(--glow), transparent 70%),
    radial-gradient(ellipse at 50% 20%, var(--surface) 0%, var(--surface-deep) 100%);
  background-attachment: fixed;
  overflow-x: hidden;
}

button, input, textarea { font: inherit; color: inherit; }

.title {
  margin: 0;
  font: italic 400 1.9rem/1.1 var(--serif);
  letter-spacing: -0.01em;
  color: var(--on-surface);
}

/* ---- Buttons & fields ---- */
button {
  background: var(--accent);
  color: var(--accent-ink);
  border: 0;
  border-radius: 999px;
  padding: 0.5rem 1.2rem;
  cursor: pointer;
  font-size: 0.95rem;
}
button:hover { filter: brightness(1.06); }
button:disabled { opacity: 0.6; cursor: default; }

.link-btn {
  background: none;
  color: var(--on-surface-muted);
  padding: 0.2rem 0.4rem;
  text-decoration: underline;
  text-underline-offset: 3px;
}

input, textarea {
  width: 100%;
  background: rgba(255, 255, 255, 0.45);
  border: 1px solid rgba(61, 56, 80, 0.18);
  border-radius: 6px;
  padding: 0.5rem 0.7rem;
  color: var(--ink);
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.error { color: #b23a52; min-height: 1.2em; margin: 0; font-size: 0.9rem; }

.row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.row button { margin-left: auto; }

/* ---- A slip of paper (shared look) ---- */
.slip {
  position: relative;
  color: var(--ink);
  background: var(--paper-0);
  border-radius: 2px;
  box-shadow: var(--shadow);
  padding: 1.2rem 1.2rem 1rem;
}
.slip label { color: var(--ink-muted); font-size: 0.9rem; }

/* ---- Login ---- */
.login-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.6rem;
  padding: 24px 16px;
}
.login-page .title { font-size: 2.4rem; }
.login-card { width: 100%; max-width: 330px; transform: rotate(-0.8deg); }
.login-card form { display: grid; gap: 0.6rem; }
#login-form:not([hidden]) + #setup { margin-top: 1.4rem; padding-top: 1.2rem; border-top: 1px dashed rgba(61, 56, 80, 0.25); }
.setup-intro { margin: 0 0 0.6rem; color: var(--ink-muted); font-size: 0.95rem; }

/* ---- Main layout ---- */
.top {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem 1rem;
  justify-content: space-between;
  align-items: center;
  max-width: 900px;
  margin: 0 auto;
  padding: 1.6rem 16px 0.4rem;
}
.top-right { display: flex; gap: 0.7rem; align-items: center; font-size: 0.9rem; color: var(--on-surface-muted); }
.unread-count {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 999px;
  padding: 0.1rem 0.7rem;
  font-size: 0.8rem;
}

.wrap { max-width: 900px; margin: 0 auto; padding: 0 16px 4rem; }

/* Writing slip */
.compose {
  max-width: 520px;
  margin: 1.4rem auto 2.6rem;
  display: grid;
  gap: 0.3rem;
}
.compose label { font: 600 1.35rem var(--hand); color: var(--ink-muted); }
.compose textarea {
  color: var(--pen);
  /* Ruled lines that sit exactly under each line of handwriting */
  background: repeating-linear-gradient(180deg, transparent 0 31px, var(--rule) 31px 32px) local;
  border: 0;
  border-radius: 0;
  padding: 0;
  font: 500 1.55rem/32px var(--hand);
  min-height: calc(32px * 4);
  resize: vertical;
}
.compose textarea:focus { outline: none; }
.compose .wait { margin: 0.2rem 0 0; font: 500 1.3rem/1.3 var(--hand); color: var(--ink-muted); }
.compose.locked textarea { display: none; }
.compose.locked button { display: none; }
.compose textarea::placeholder { color: #aaa2b6; }

.panel { max-width: 520px; margin: 1.8rem auto 0; background: var(--paper-2); }
.panel h2 { font: italic 400 1.2rem var(--serif); margin: 0; }
.panel .hint { margin: 0.2rem 0 0.6rem; color: var(--ink-muted); font-size: 0.9rem; }
.panel form { display: grid; gap: 0.5rem; }

/* ---- Timeline: a stitched thread down the page, slips hang either side ---- */
.timeline {
  --thread: var(--thread-color);
  --axis: 72px;
  position: relative;
  display: grid;
  gap: 1.1rem;
  padding: 0.4rem 0 2rem;
}
.timeline::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  margin-left: -1px;
  background: repeating-linear-gradient(180deg, var(--thread) 0 7px, transparent 7px 12px);
}

.day {
  position: relative;
  justify-self: center;
  margin: 0.8rem 0 0.2rem;
  padding: 0.15rem 0.9rem;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: 0 0 0 1px var(--thread);
  font: italic 400 0.95rem var(--serif);
  color: var(--on-surface-muted);
}

.entry {
  display: grid;
  grid-template-columns: 1fr var(--axis) 1fr;
  align-items: start;
}
.entry > .note { grid-row: 1; }
.entry.theirs > .note { grid-column: 1; justify-self: end; }
.entry.mine > .note { grid-column: 3; justify-self: start; }

.entry-axis {
  grid-column: 2;
  grid-row: 1;
  position: relative;
  display: grid;
  justify-items: center;
  gap: 3px;
  padding-top: 1.1rem;
}
.entry-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--on-surface-muted);
  box-shadow: 0 0 0 3px var(--surface);
}
.entry.theirs .entry-dot { background: var(--accent); }
.entry-time {
  padding: 0 3px;
  border-radius: 4px;
  background: var(--surface);
  font-size: 0.68rem;
  line-height: 1.2;
  text-align: center;
  color: var(--on-surface-muted);
}

/* Phones: one thread on the left, all slips to its right */
@media (max-width: 620px) {
  .timeline { --axis: 46px; }
  .timeline::before { left: 23px; }
  .day { justify-self: start; }
  .entry { grid-template-columns: var(--axis) 1fr; }
  .entry-axis { grid-column: 1; }
  .entry.theirs > .note, .entry.mine > .note { grid-column: 2; }
  .entry.theirs > .note { justify-self: start; }
  .entry.mine > .note { justify-self: end; }
  .entry-time { font-size: 0.6rem; }
}

.note {
  --paper: var(--paper-0);
  --tilt: 0deg;
  position: relative;
  width: min(var(--w, 220px), 100%);
  transform: rotate(var(--tilt));
  transition: transform 0.2s ease, filter 0.2s ease;
  /* drop-shadow follows the uneven clipped edges (box-shadow would be clipped away) */
  filter: drop-shadow(0 1px 1px rgba(45, 38, 64, 0.22)) drop-shadow(0 5px 9px rgba(45, 38, 64, 0.18));
}
.note:hover {
  transform: rotate(0deg) translateY(-2px);
  filter: drop-shadow(0 2px 2px rgba(45, 38, 64, 0.22)) drop-shadow(0 12px 18px rgba(45, 38, 64, 0.22));
}
@media (prefers-color-scheme: dark) {
  .note { filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.4)) drop-shadow(0 6px 12px rgba(0, 0, 0, 0.32)); }
  .note:hover { filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.5)) drop-shadow(0 14px 22px rgba(0, 0, 0, 0.5)); }
}

/* Unfolded slip: small, uneven edges (clip-path set per note in JS),
   one crease across the middle where it was folded in half. */
.note-inner {
  color: var(--ink);
  padding: 0.9rem 0.95rem 0.45rem;
  clip-path: var(--edge, none);
  background:
    /* the crease: a dark line with a highlight just below it */
    linear-gradient(180deg, transparent calc(50% - 2px), rgba(0, 0, 0, 0.07) calc(50% - 0.5px), rgba(255, 255, 255, 0.45) calc(50% + 0.5px), transparent calc(50% + 3px)),
    /* the halves catch light slightly differently */
    linear-gradient(180deg, rgba(255, 255, 255, 0.12) 0 50%, rgba(0, 0, 0, 0.02) 50% 100%),
    /* uneven light across the crumpled sheet */
    radial-gradient(ellipse at var(--lx, 30%) var(--ly, 20%), rgba(255, 255, 255, 0.28), transparent 60%),
    var(--paper);
}

.note-body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 0 0 0.3rem;
  font: 500 1.35rem/1.2 var(--hand);
  color: var(--pen);
  text-shadow: 0 0 0.6px rgba(61, 56, 80, 0.4);
}

.note-foot { display: flex; align-items: baseline; gap: 0.6rem; }
.note-sign { font: 600 1.15rem var(--hand); color: var(--ink-muted); }

.note-actions { display: flex; justify-content: space-between; align-items: center; min-height: 1.7rem; }
.note-status { color: var(--ink-muted); font-size: 0.78rem; font-style: italic; }
.note-status.hearted { color: var(--accent); }

.heart-btn {
  background: none;
  color: var(--accent);
  font-size: 1.25rem;
  line-height: 1;
  padding: 0.1rem 0.25rem;
  margin-left: auto;
}
.heart-btn.on { transform: scale(1.12); }

/* ---- Folded sheet (a note you haven't opened yet) ----------------------
   The sheet is folded in half: its bottom half is folded up over the top
   half, so the crease is along the bottom edge. JS (setupFold) measures the
   note and builds:
     .fold-stage
       .fold-base   -> top half of the inside (hidden under the flap)
       .fold-flap   -> bottom half, hinged at its top edge, rotated 180deg
         .flap-inside   -> its inside (the lower half of the writing)
         .note-front    -> its outside ("For Alice"), shown while folded
   Opening rotates the flap back down to 0deg. */

.note.folded, .note.opening { transform: rotate(var(--tilt)); }
.note.folded > .note-inner, .note.opening > .note-inner { visibility: hidden; }

.fold-stage {
  position: absolute;
  inset: 0 0 auto 0;
  height: calc(var(--h) * 2);
  perspective: 1100px;
  perspective-origin: 50% var(--h);
  transform-style: preserve-3d;
  pointer-events: none;
}

.half {
  position: absolute;
  left: 0;
  right: 0;
  height: var(--h);
  overflow: hidden;
}
.half-content { position: absolute; left: 0; right: 0; }

.fold-base {
  top: 0;
  transform: translateZ(-1px);
}

.fold-flap {
  position: absolute;
  left: 0;
  right: 0;
  top: var(--h);
  height: var(--h);
  transform-origin: 50% 0;
  transform: rotateX(180deg);
  transform-style: preserve-3d;
}
.flap-inside { top: 0; backface-visibility: hidden; }
/* While folded, the hidden top half shows only blank paper (its text could
   otherwise leak through the crease); the writing appears once opening starts. */
.note.folded .fold-base .half-content > * { visibility: hidden; }

/* Shading overlays that change while the flap turns */
.shade {
  position: absolute;
  inset: 0;
  background: #000;
  opacity: 0;
  pointer-events: none;
}

/* The outside of the folded sheet */
.note-front {
  position: absolute;
  inset: 0;
  top: 4px;               /* the edges don't line up perfectly: the inside peeks out at the top */
  bottom: -2px;           /* wrap slightly past the crease so nothing underneath shows through */
  display: grid;
  align-content: start;
  gap: 0.1rem;
  padding: 0.85rem 1rem;
  text-align: left;
  color: var(--ink);
  border-radius: 0;        /* it's a <button>; don't inherit the pill shape */
  clip-path: var(--front-edge, none);
  transform: rotateX(180deg);
  backface-visibility: hidden;
  pointer-events: auto;
  cursor: pointer;
  background:
    /* rounded crease along the bottom: light catches the bend, then it falls into shadow */
    linear-gradient(180deg, transparent calc(100% - 14px), rgba(255, 255, 255, 0.35) calc(100% - 9px), rgba(0, 0, 0, 0.1) 100%),
    /* the open top edge sits slightly lifted */
    linear-gradient(180deg, rgba(0, 0, 0, 0.06), transparent 10px),
    var(--paper);
}
.note-front:hover { filter: none; }
.note-front:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.front-to { font: 600 1.6rem/1.1 var(--hand); color: var(--pen); }
.front-hint { margin-top: auto; font-size: 0.72rem; font-style: italic; color: var(--ink-muted); opacity: 0.8; }

/* Little wax seal holding the open edge closed */
.seal {
  position: absolute;
  left: 50%;
  top: -8px;               /* straddles the open top edge of the folded slip */
  width: 26px;
  height: 26px;
  margin-left: -13px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 0.85rem;
  /* matte pressed wax: a soft rim, no shine */
  box-shadow: 0 1px 2px rgba(50, 40, 70, 0.3), inset 0 0 0 2px rgba(0, 0, 0, 0.08);
}

/* Gentle breathing so folded sheets invite a tap */
.note.folded { animation: breathe 4s ease-in-out infinite; }
@keyframes breathe {
  0%, 100% { transform: rotate(var(--tilt)); }
  50% { transform: rotate(calc(var(--tilt) + 1deg)) translateY(-3px); }
}

/* While opening: straighten up and lift off the table */
.note.opening { transform: rotate(0deg) translateY(-4px); transition: transform 0.3s ease; z-index: 2; }
.note.opening .seal { animation: seal-pop 0.35s ease-in forwards; }
@keyframes seal-pop {
  to { transform: translateY(-14px) scale(0.6) rotate(-25deg); opacity: 0; }
}

/* After opening, the writing settles in */
.note.just-opened > .note-inner .note-body { animation: ink 0.6s ease-out; }
@keyframes ink { from { opacity: 0.4; filter: blur(0.6px); } to { opacity: 1; filter: none; } }

/* ---- Slight wrinkles on every piece of paper ---- */
.note-inner { position: relative; }
.slip::after,
.note-inner::after,
.note-front::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 1;
  border-radius: inherit;
  background:
    var(--grain) var(--wy, 0) var(--wx, 0) / 180px 180px,
    var(--wrinkle) var(--wx, 0) var(--wy, 0) / 300px 300px,
    var(--wrinkle) calc(var(--wy, 0) * 2) calc(var(--wx, 0) * 2) / 820px 820px;
  mix-blend-mode: multiply;
  opacity: var(--wrinkle-strength);
  pointer-events: none;
}
.seal { z-index: 3; }
.heart-btn { position: relative; z-index: 2; }

.empty { text-align: center; color: var(--on-surface-muted); font: 1.5rem var(--hand); }

@media (prefers-reduced-motion: reduce) {
  .note, .note.folded, .note.opening { animation: none; transition: none; }
  .note.just-opened > .note-inner .note-body, .note.opening .seal { animation: none; }
  .note:hover { transform: rotate(var(--tilt)); }
}
