/* ============================================================================
   Kotoba · 言葉  —  "the Signed Scroll"
   A quote is presented as a one-of-a-kind sumi-e hanging scroll: the character
   rendered as ink-graded art, the words brushed into the open ma, and the whole
   composition "signed" by a cinnabar hanko carrying the character's own initial.
   ========================================================================== */

/* per-quote accent is a registered custom property so it can TUNE smoothly */
@property --accent { syntax: '<color>'; inherits: true; initial-value: #6b5b8a; }

:root {
  --ground:      #13161d;
  --ground-deep: #0d0f14;
  --surface:     #1a1e27;
  --surface-2:   #20242f;

  --ivory:   #f1ead9;
  --ivory-2: #e7dece;
  --muted:   #8f8a7d;
  --faint:   #6a675f;
  --hairline: rgba(241, 234, 217, 0.12);

  --hanko:      #d2452d;   /* cinnabar — fixed, never derived */
  --hanko-edge: #b23a26;

  --accent: #6b5b8a;

  --ease-ink:   cubic-bezier(0.16, 1, 0.30, 1);   /* ink bleeding into paper */
  --ease-out:   cubic-bezier(0.4, 0, 0.2, 1);
  --ease-press: cubic-bezier(0.34, 1.56, 0.64, 1);

  --font-mincho: "Shippori Mincho", "Songti SC", "Yu Mincho", Georgia, serif;
  --font-gothic: "Zen Kaku Gothic New", "Hiragino Sans", system-ui, sans-serif;

  transition: --accent 900ms ease;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  height: 100svh; height: 100dvh;     /* fill the viewport exactly … */
  display: flex; flex-direction: column;
  overflow: hidden;                    /* … and never scroll the page itself */
  font-family: var(--font-mincho);
  color: var(--ivory);
  background:
    radial-gradient(135% 105% at 50% 24%, #161a22 0%, var(--ground) 42%, var(--ground-deep) 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  opacity: 0;
  transition: opacity 700ms ease;
}
body.is-ready { opacity: 1; }

/* ---- washi paper grain, static ---------------------------------------- */
.grain {
  position: fixed; inset: 0; z-index: 60; pointer-events: none;
  opacity: 0.05; mix-blend-mode: soft-light;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ---- giant 言 watermark, bleeding off the corner ---------------------- */
.watermark {
  position: fixed; right: -0.14em; bottom: -0.30em; z-index: 1; pointer-events: none;
  font-family: var(--font-mincho); font-weight: 500;
  font-size: 52vh; line-height: 0.8; color: rgba(241, 234, 217, 0.030);
  user-select: none; will-change: transform;
  animation: wm-drift 46s ease-in-out infinite;
}
@keyframes wm-drift {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  50%      { transform: translate(-1.6vmax, -1.1vmax) rotate(-1.1deg); }
}


/* ---- masthead (top bar) ----------------------------------------------- */
.topbar {
  position: relative; z-index: 40; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px clamp(18px, 4vw, 44px);
}
.topbar-right { display: flex; align-items: center; gap: clamp(12px, 2vw, 18px); }

.brand { display: flex; align-items: baseline; gap: 10px; text-decoration: none; }
.brand-mark { font-family: var(--font-mincho); font-weight: 600; font-size: 20px; color: var(--ivory); letter-spacing: 0.04em; }
.brand-name { font-family: var(--font-gothic); font-weight: 500; font-size: 10px; letter-spacing: 0.34em; color: var(--muted); }

.folio {
  font-family: var(--font-gothic); font-weight: 500;
  font-size: 11px; letter-spacing: 0.2em; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.folio .num { color: color-mix(in srgb, var(--accent) 62%, var(--ivory)); }

/* saved-quotes opener (heart + count) */
.saved-open {
  appearance: none; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  background: transparent; border: 1px solid var(--hairline); border-radius: 999px;
  padding: 6px 12px; color: var(--muted);
  font-family: var(--font-gothic); font-weight: 500; font-size: 11px; letter-spacing: 0.1em;
  font-variant-numeric: tabular-nums;
  transition: color 200ms, border-color 200ms, background 200ms, transform 130ms var(--ease-out);
}
.saved-open svg { fill: none; stroke: currentColor; stroke-width: 1.6; }
.saved-open:hover { color: var(--ivory-2); border-color: rgba(241, 234, 217, 0.24); }
.saved-open.has-saved svg { fill: var(--hanko); stroke: var(--hanko); }
.saved-open.has-saved { color: var(--ivory-2); }
.saved-open:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- the stage: fills the space between masthead and controls --------- */
.stage {
  position: relative; z-index: 10; flex: 1 1 auto; min-height: 0;
  display: flex; flex-direction: column;
  padding: 4px clamp(20px, 5vw, 56px) 18px;
}
.scene-wrap {
  flex: 1 1 auto; min-height: 0;
  display: grid; place-items: center; overflow: hidden;
}

.scene {
  display: grid;
  grid-template-columns: minmax(0, 33rem) 2px 260px;
  gap: clamp(32px, 5vw, 64px);
  align-items: center;
  width: 100%; max-width: 980px;
  transform-origin: center center;      /* JS scales this to guarantee it fits */
  transition: opacity 230ms var(--ease-out);
}

/* -- LEFT · the words --------------------------------------------------- */
.words { min-width: 0; }

.kicker { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.tick { width: 22px; height: 2px; background: var(--accent); border-radius: 2px; box-shadow: 0 0 12px color-mix(in srgb, var(--accent) 55%, transparent); }
.anime {
  font-family: var(--font-gothic); font-weight: 500;
  font-size: 12px; letter-spacing: 0.22em; text-transform: uppercase; color: var(--muted);
}

.quote {
  margin: 0;
  font-family: var(--font-mincho); font-weight: 500; color: var(--ivory);
  letter-spacing: 0.004em; line-height: 1.5;
  --qs: clamp(1.5rem, 1.05rem + 1.6vw, 2.1rem);
  font-size: var(--qs);
  text-wrap: pretty;
  overflow-wrap: break-word;
}
.quote[data-len="short"] { --qs: clamp(1.9rem, 1.2rem + 2.6vw, 2.9rem); font-weight: 600; line-height: 1.4; }
.quote[data-len="mid"]   { --qs: clamp(1.6rem, 1.05rem + 1.9vw, 2.3rem); }
.quote[data-len="long"]  { --qs: clamp(1.25rem, 1rem + 1.05vw, 1.6rem); line-height: 1.62; }

.byline {
  margin: 26px 0 0; color: var(--ivory-2);
  font-family: var(--font-mincho); font-weight: 600; font-size: clamp(15px, 1.1vw, 18px);
  letter-spacing: 0.01em;
}
.byline .char { color: var(--ivory-2); }

/* -- SPINE · tapered brush stroke --------------------------------------- */
.spine { position: relative; align-self: stretch; min-height: 46vh; display: flex; justify-content: center; }
.brush {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 12px; height: 100%;
  background: linear-gradient(to bottom,
    transparent 0%,
    rgba(241, 234, 217, 0.03) 8%,
    rgba(241, 234, 217, 0.28) 24%,
    rgba(241, 234, 217, 0.40) 50%,
    rgba(241, 234, 217, 0.26) 76%,
    rgba(241, 234, 217, 0.03) 92%,
    transparent 100%);
  clip-path: polygon(38% 0, 62% 0, 74% 50%, 58% 100%, 42% 100%, 26% 50%);
  animation: brush-breathe 7s ease-in-out infinite;
}
@keyframes brush-breathe { 0%, 100% { opacity: 0.72; } 50% { opacity: 1; } }

/* -- RIGHT · the kakemono scroll ---------------------------------------- */
.frame { position: relative; margin: 0; justify-self: center; width: 260px; }

.backdrop {
  position: absolute; inset: -14% -18%; z-index: 0; border-radius: 40%;
  background-size: cover; background-position: 50% 30%;
  filter: blur(38px) saturate(0.7) brightness(0.42);
  opacity: 0.55; transform: scale(1);
}
.backdrop::after { content: ""; position: absolute; inset: 0; background: radial-gradient(60% 60% at 50% 45%, transparent, var(--ground) 100%); }

.cord { position: absolute; top: -22px; left: 50%; transform: translateX(-50%); z-index: 3; display: flex; flex-direction: column; align-items: center; }
.cord i { width: 1px; height: 20px; background: linear-gradient(var(--hanko), transparent); }
.cord::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: var(--hanko); box-shadow: 0 0 8px color-mix(in srgb, var(--hanko) 60%, transparent); }

.panel {
  position: relative; z-index: 2;
  background: var(--surface);
  padding: 10px 10px 18px;                    /* scroll proportion: more at foot */
  border: 1px solid rgba(241, 234, 217, 0.10);
  box-shadow:
    0 30px 60px -20px rgba(0, 0, 0, 0.72),
    0 0 44px -8px color-mix(in srgb, var(--accent) 22%, transparent),
    inset 0 0 0 1px rgba(0, 0, 0, 0.25);
}

.portrait-wrap {
  position: relative; overflow: hidden;
  aspect-ratio: 3 / 4;
  background: linear-gradient(160deg, #191d26, #10131a);
  outline: 1px solid rgba(241, 234, 217, 0.14); outline-offset: -1px;
}

.portrait {
  position: absolute; inset: 0; width: 100%; height: 100%; z-index: 2;
  object-fit: cover; object-position: top center;
  opacity: 0;
  /* the unifying ink grade: makes 7,900 mismatched portraits feel one hand */
  filter: contrast(1.05) saturate(0.88) brightness(0.97);
  transition: opacity 620ms var(--ease-out);
}
.portrait.is-shown { opacity: 1; }
/* indigo multiply + faint grain seat the image into the sumi world */
.portrait-wrap::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 2;
  background:
    linear-gradient(180deg, transparent 55%, rgba(16, 19, 26, 0.55) 100%),
    linear-gradient(0deg, rgba(19, 22, 29, 0.14), rgba(19, 22, 29, 0.14));
  mix-blend-mode: multiply;
}

/* warm "picture light" washing down from the top of the scroll */
.picture-light {
  position: absolute; left: 0; right: 0; top: -18%; height: 62%; z-index: 3; pointer-events: none;
  background: radial-gradient(70% 100% at 50% 0%, rgba(255, 244, 224, 0.16), transparent 70%);
}

/* monogram fallback = a faint ensō + the character's initial(s) */
.monogram {
  position: absolute; inset: 0; z-index: 1;
  display: grid; place-items: center;
  font-family: var(--font-mincho); font-weight: 500; font-size: 5.2rem; color: rgba(241, 234, 217, 0.9);
}
.monogram::before {
  content: ""; position: absolute; width: 62%; aspect-ratio: 1; border-radius: 50%;
  border: 2px solid rgba(241, 234, 217, 0.14);
  border-right-color: transparent; transform: rotate(-28deg);
}
.monogram::after {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(58% 58% at 50% 46%, color-mix(in srgb, var(--accent) 12%, transparent), transparent 70%);
}

/* ---- crossfade out + staged reveal ------------------------------------ */
.scene.is-out { opacity: 0; }

/* meta + portrait wait, hidden (".pre"), until their beat in the reveal.
   Reveals are opacity-only (no positional slide) so nothing shifts; the
   portrait simply fades up out of the background via a whisper of scale+blur. */
.kicker, .byline { transition: opacity 640ms var(--ease-ink), filter 640ms var(--ease-ink); }
.frame { transition: opacity 760ms var(--ease-out), transform 760ms var(--ease-out), filter 760ms var(--ease-out); }
.kicker.pre { opacity: 0; filter: blur(2px); }
.byline.pre { opacity: 0; filter: blur(2px); }
.frame.pre  { opacity: 0; transform: scale(0.955); filter: blur(9px); }

/* typewriter caret while the quote types itself in, letter by letter */
.quote.is-typing::after {
  content: ""; display: inline-block; width: 2px; height: 0.9em; margin-left: 3px;
  vertical-align: -0.06em; background: var(--accent); border-radius: 1px;
  box-shadow: 0 0 10px color-mix(in srgb, var(--accent) 65%, transparent);
  animation: caret-blink 850ms steps(1, end) infinite;
}
@keyframes caret-blink { 0%, 49% { opacity: 1; } 50%, 100% { opacity: 0; } }

/* ---- controls: heart + two quiet pills -------------------------------- */
.controls { flex: 0 0 auto; display: flex; justify-content: center; align-items: center; gap: 12px; padding-top: 8px; }

/* heart + pills share one height so they line up exactly */
.icon-btn {
  appearance: none; cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  width: 42px; height: 42px; padding: 0; border-radius: 2px;
  background: rgba(241, 234, 217, 0.015); border: 1px solid var(--hairline); color: var(--muted);
  transition: color 200ms var(--ease-out), border-color 200ms var(--ease-out), background 200ms var(--ease-out), transform 130ms var(--ease-out);
}
.icon-btn svg { fill: none; stroke: currentColor; stroke-width: 1.6; }
.icon-btn:hover { color: var(--ivory-2); border-color: rgba(241, 234, 217, 0.24); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn[aria-pressed="true"] { color: var(--hanko); border-color: color-mix(in srgb, var(--hanko) 45%, transparent); }
.icon-btn[aria-pressed="true"] svg { fill: var(--hanko); stroke: var(--hanko); }

.btn {
  appearance: none; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  height: 42px; padding: 0 22px; border-radius: 2px;
  font-family: var(--font-gothic); font-weight: 500;
  font-size: 11px; letter-spacing: 0.22em; text-transform: uppercase;
  color: var(--muted);
  background: rgba(241, 234, 217, 0.015);
  border: 1px solid var(--hairline);
  transition: color 200ms var(--ease-out), border-color 200ms var(--ease-out), background 200ms var(--ease-out), transform 130ms var(--ease-out);
}
.btn--primary { color: var(--ivory-2); }
.btn:hover {
  color: var(--ivory);
  border-color: color-mix(in srgb, var(--accent) 55%, var(--hairline));
  background: color-mix(in srgb, var(--accent) 9%, transparent);
}
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ONE unified press animation for every button */
.icon-btn:active, .btn:active, .saved-open:active, .fav-close:active, .fav-card__rm:active { transform: scale(0.93); }

/* ---- copy feedback shown on the button itself ------------------------- */
.btn.is-copied {
  color: var(--ivory);
  border-color: color-mix(in srgb, var(--accent) 60%, var(--hairline));
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

/* ---- favorites · full-page selector ----------------------------------- */
.fav-overlay {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(8, 10, 14, 0.72);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  opacity: 0; transition: opacity 360ms var(--ease-out);
}
.fav-overlay[hidden] { display: none; }
.fav-overlay.is-open { opacity: 1; }

.fav-panel {
  display: flex; flex-direction: column;
  width: min(920px, 100%); margin: 0 auto; height: 100%;
  padding: clamp(18px, 5vh, 52px) clamp(20px, 5vw, 60px) clamp(16px, 4vh, 40px);
  transform: translateY(16px); opacity: 0;
  transition: transform 460ms var(--ease-ink), opacity 460ms var(--ease-ink);
}
.fav-overlay.is-open .fav-panel { transform: none; opacity: 1; }

.fav-head { display: flex; align-items: baseline; justify-content: space-between; padding-bottom: 18px; border-bottom: 1px solid var(--hairline); }
.fav-title { margin: 0; font-family: var(--font-mincho); font-weight: 600; font-size: clamp(22px, 3.4vw, 30px); color: var(--ivory); letter-spacing: 0.02em; }
.fav-title-count { color: var(--muted); font-family: var(--font-gothic); font-weight: 500; font-size: 0.46em; letter-spacing: 0.16em; margin-left: 8px; }
.fav-close {
  appearance: none; cursor: pointer; background: transparent; border: 1px solid var(--hairline);
  color: var(--muted); width: 38px; height: 38px; border-radius: 2px; font-size: 14px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
  transition: color 200ms, border-color 200ms, transform 130ms var(--ease-out);
}
.fav-close:hover { color: var(--ivory); border-color: rgba(241, 234, 217, 0.24); }
.fav-close:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.fav-empty { color: var(--muted); font-family: var(--font-mincho); font-size: 16px; margin: 44px 2px; }

.fav-list {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden; margin-top: 22px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(236px, 1fr)); gap: 12px; align-content: start;
  scrollbar-width: none; -ms-overflow-style: none;      /* hidden scrollbar, still scrolls */
}
.fav-list::-webkit-scrollbar { width: 0; height: 0; }

.fav-card {
  position: relative; cursor: pointer; text-align: left; appearance: none; font: inherit; color: inherit;
  display: flex; gap: 13px; align-items: stretch; min-width: 0; overflow: hidden;
  height: 116px;                       /* fixed height → a uniform, scannable list */
  background: rgba(241, 234, 217, 0.02); border: 1px solid var(--hairline); border-radius: 4px; padding: 15px;
  transition: border-color 220ms, background 220ms, transform 130ms var(--ease-out);
}
.fav-card:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--hairline)); background: color-mix(in srgb, var(--accent) 7%, transparent); }
.fav-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* a slow, quiet dissolve when a saved quote is removed */
.fav-card.removing {
  opacity: 0; transform: scale(0.94); filter: blur(2px); pointer-events: none;
  transition: opacity 560ms var(--ease-out), transform 560ms var(--ease-out), filter 560ms var(--ease-out);
}
.fav-card__thumb { flex: 0 0 auto; align-self: flex-start; width: 44px; height: 58px; border-radius: 2px; overflow: hidden; background: linear-gradient(160deg, #191d26, #10131a); display: grid; place-items: center; }
.fav-card__thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top center; filter: contrast(1.05) saturate(0.88) brightness(0.97); }
.fav-card__mono { font-family: var(--font-mincho); font-size: 19px; color: rgba(241,234,217,0.85); }
.fav-card__body { min-width: 0; flex: 1 1 auto; display: flex; flex-direction: column; padding-right: 30px; }  /* room for the × + ellipsis */
.fav-card__q { font-family: var(--font-mincho); font-size: 14.5px; line-height: 1.42; color: var(--ivory-2);
  overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.fav-card__meta { margin-top: auto; padding-top: 8px; min-width: 0; font-family: var(--font-gothic); font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fav-card__rm {
  position: absolute; top: 8px; right: 8px; width: 26px; height: 26px; border-radius: 2px;
  display: inline-flex; align-items: center; justify-content: center;
  background: rgba(13,15,20,0.55); border: 1px solid var(--hairline); color: var(--muted); cursor: pointer; font-size: 12px; line-height: 1;
  transition: color 180ms, border-color 180ms, background 180ms, transform 130ms var(--ease-out);
}
.fav-card__rm:hover, .fav-card__rm:focus-visible { color: var(--hanko); border-color: color-mix(in srgb, var(--hanko) 45%, transparent); background: rgba(13,15,20,0.82); }

/* ---- loader ----------------------------------------------------------- */
.loader {
  position: fixed; inset: 0; z-index: 90; display: grid; place-items: center;
  background: var(--ground-deep);
  transition: opacity 600ms ease;
}
body.is-ready .loader { opacity: 0; pointer-events: none; }
body.is-ready .loader-kanji { animation: none; }
.loader-kanji { font-family: var(--font-mincho); font-size: 44px; color: var(--ivory); opacity: 0.5; animation: breathe 2.4s ease-in-out infinite; }
@keyframes breathe { 0%, 100% { opacity: 0.24; } 50% { opacity: 0.6; } }

/* ===========================================================================
   Responsive · single-column scroll on narrow screens
   =========================================================================== */
@media (max-width: 760px) {
  .stage { padding: 2px 20px 14px; }
  .scene {
    grid-template-columns: minmax(0, 1fr); gap: 0; max-width: min(30rem, 100%);
    justify-items: center; text-align: center;
  }
  .spine { display: none; }
  .frame { order: -1; width: 148px; margin-bottom: 22px; }
  .kicker { justify-content: center; margin-bottom: 14px; }
  .words { width: 100%; }
  .byline { margin-top: 16px; }
  .quote[data-len="short"] { --qs: clamp(1.6rem, 1.1rem + 3vw, 2.2rem); }
  .quote[data-len="mid"]   { --qs: clamp(1.35rem, 1rem + 2.4vw, 1.85rem); }
  .quote[data-len="long"]  { --qs: clamp(1.1rem, 0.95rem + 1.5vw, 1.35rem); }
  .watermark { font-size: 40vh; }
  .fav-list { grid-template-columns: 1fr; }
}

/* on smaller screens, drop the folio so brand + saved never crowd */
@media (max-width: 480px) {
  .folio { display: none; }
}

/* ===========================================================================
   Reduced motion · collapse to a single quiet crossfade
   =========================================================================== */
@media (prefers-reduced-motion: reduce) {
  :root { transition: none; }
  .scene, .quote, .byline, .kicker, .frame, .portrait { transition: opacity 220ms ease !important; transition-delay: 0ms !important; }
  .kicker.pre, .byline.pre, .frame.pre { transform: none; filter: none; }
  /* stop the ambient drift + the typewriter caret */
  .watermark, .brush, .loader-kanji { animation: none !important; }
  .brush { opacity: 1; }
  .quote.is-typing::after { display: none; }
  .fav-panel { transform: none !important; transition: opacity 200ms ease !important; }
}
