/* 無聲頻率：全白、留白、黑體。
   版面原則：畫布全螢幕；文字與按鈕用統一的間距、字級與安全區域；
   球的位置由 app.js 依螢幕比例計算（直式上下、橫式與桌機左右），按鈕跟著同一套座標。 */

:root {
  --paper: #f3f3f1;
  --ink: #141414;
  --muted: #8a8a86;
  --line: #d6d6d2;
  --fg: var(--ink);

  /* 安全區域（瀏海、底部橫條） */
  --sa-t: env(safe-area-inset-top, 0px);
  --sa-r: env(safe-area-inset-right, 0px);
  --sa-b: env(safe-area-inset-bottom, 0px);
  --sa-l: env(safe-area-inset-left, 0px);

  /* 間距與字級 */
  --gutter: clamp(16px, 4vw, 40px);
  --edge: clamp(14px, 3vw, 28px);
  --fs-xs: 11px;
  --fs-sm: 12px;
  --fs-md: clamp(14px, 0.3vw + 13px, 16px);
  --fs-lg: clamp(17px, 0.6vw + 15px, 20px);
  --fs-xl: clamp(20px, 1.6vw + 15px, 28px);

  /* 可見高度：鍵盤彈出時由 app.js 更新 */
  --vvh: 100dvh;

  font-family: 'Noto Sans TC', 'PingFang TC', 'Heiti TC', 'Microsoft JhengHei', sans-serif;
  font-weight: 400;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation; /* 取消雙擊放大 */
}
body {
  margin: 0;
  position: fixed; inset: 0;
  background: var(--paper);
  color: var(--fg);
  letter-spacing: .04em;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.layer { position: fixed; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }
body::after { content: ''; position: fixed; inset: 0; background-image: var(--grain, none); background-size: 96px 96px; opacity: .045; mix-blend-mode: multiply; pointer-events: none; z-index: 5; }

/* ---------- 畫面容器：全螢幕，內容各自置中與限寬 ---------- */
.screen {
  position: fixed; inset: 0;
  display: none;
  pointer-events: none;
}
.screen > * { pointer-events: auto; }
.screen.active { display: block; animation: fade .8s ease both; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; padding: 0; letter-spacing: inherit; }
:focus-visible { outline: 1px solid currentColor; outline-offset: 4px; }

/* ---------- 共用元件 ---------- */
.brand {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(var(--sa-t) + clamp(20px, 4vh, 36px));
  margin: 0; text-align: center; white-space: nowrap; pointer-events: none;
  font-size: var(--fs-sm); letter-spacing: .6em; text-indent: .6em; color: var(--muted);
}
.close {
  position: absolute;
  top: calc(var(--sa-t) + var(--edge) - 10px); left: calc(var(--sa-l) + var(--edge) - 10px);
  width: 44px; height: 44px;
  display: grid; place-items: center;
  font-size: 22px; font-weight: 300; line-height: 1; color: var(--muted);
}
.corner {
  position: absolute;
  bottom: calc(var(--sa-b) + var(--edge));
  padding: 12px 8px; margin: -12px -8px; /* 觸控範圍至少 44px 高 */
  font-size: var(--fs-sm); color: var(--muted); text-decoration: none; letter-spacing: .2em; white-space: nowrap;
}
.corner-left { left: calc(var(--sa-l) + var(--edge)); }
.corner-right { right: calc(var(--sa-r) + var(--edge)); }
.corner-center { left: 50%; transform: translateX(-50%); }
.corner.top { top: calc(var(--sa-t) + var(--edge)); bottom: auto; }
/* 背景音樂開關：首頁左上角一條小聲波；關掉時拉平成一條線 */
.sound {
  position: absolute;
  top: calc(var(--sa-t) + var(--edge) - 12px); left: calc(var(--sa-l) + var(--edge) - 12px);
  width: 48px; height: 44px; display: grid; place-items: center; color: var(--muted);
}
/* 開著：聲波緩慢流動（兩端淡出）；關掉：淡成一條平線。用透明度切換，iOS Safari 也看得到變化 */
.sound svg { width: 30px; height: 14px; overflow: hidden; -webkit-mask-image: linear-gradient(90deg, transparent, #000 22%, #000 78%, transparent); mask-image: linear-gradient(90deg, transparent, #000 22%, #000 78%, transparent); }
.sound path { fill: none; stroke: currentColor; stroke-width: 1.3; stroke-linecap: round; }
.sound .wave { animation: sound-flow 3.2s linear infinite; transition: opacity .5s ease; }
.sound .flat { opacity: 0; transition: opacity .5s ease; }
.sound[aria-pressed="false"] .wave { opacity: 0; animation-play-state: paused; }
.sound[aria-pressed="false"] .flat { opacity: .6; }
@keyframes sound-flow { to { transform: translateX(-30px); } }
@media (prefers-reduced-motion: reduce) { .sound .wave { animation: none; } } /* 減少動態：只顯示靜止的聲波 */
#screen-home .sound { transition-delay: .7s; }
.corner.tiny { font-size: var(--fs-xs); letter-spacing: .15em; opacity: .75; }

.hint {
  position: absolute; left: var(--gutter); right: var(--gutter);
  bottom: calc(var(--sa-b) + clamp(28px, 6vh, 48px));
  margin: 0; text-align: center; font-size: var(--fs-sm); color: var(--muted); letter-spacing: .3em;
}
.hint.center { top: 50%; bottom: auto; transform: translateY(-50%); }

/* ---------- 首頁：球的位置由 app.js 設定 left／top／width／height ---------- */
/* 進場：腳本啟動前先藏起首頁介面，進場動畫後依序淡入（標題字距由寬收攏） */
#screen-home > * { transition: opacity 1s ease, letter-spacing 1.8s cubic-bezier(.2, .7, .2, 1), text-indent 1.8s cubic-bezier(.2, .7, .2, 1); }
#screen-home .portal { transition-delay: .2s; }
#screen-home .corner { transition-delay: .7s; }
#screen-home .corner.top { bottom: auto; }
html.intro #screen-home > * { opacity: 0; pointer-events: none; transition: none; }
#screen-home.leaving > * { opacity: 0; pointer-events: none; transition: opacity .22s ease; transition-delay: 0s; }
html.intro #screen-home .brand { letter-spacing: 1.2em; text-indent: 1.2em; }
.portal {
  position: absolute; left: 50%; top: 50%;
  width: 34vmin; height: 34vmin;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  font-size: var(--fs-lg); letter-spacing: 0;
}
.portal-gloom { color: #f3f3f1; }
.portal-glow { color: #fff; text-shadow: 0 1px 6px rgba(40, 30, 70, .35), 0 0 2px rgba(40, 30, 70, .25); } /* 白字加淡陰影，不和彩色球搶 */
.access {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(var(--sa-t) + clamp(52px, 9vh, 80px));
  width: min(200px, 60vw);
  font: inherit; font-size: 16px; /* 16px 以上，iOS 才不會自動放大 */
  text-align: center; color: var(--ink); background: transparent;
  border: none; border-bottom: 1px solid var(--muted); border-radius: 0; padding: 8px 6px;
  -webkit-user-select: text; user-select: text;
}
.access:focus { outline: none; border-bottom-color: var(--ink); }

/* ---------- 寫下來：沒有框，只有一條線；鍵盤彈出時跟著可見高度移動 ---------- */
.write-form {
  position: absolute; left: 50%;
  top: calc(var(--vvh) * 0.4);
  width: min(640px, calc(100% - var(--gutter) * 2 - var(--sa-l) - var(--sa-r)));
  transform: translate(-50%, -50%);
  display: flex; align-items: flex-end; gap: 14px;
}
textarea {
  flex: 1; min-width: 0; resize: none;
  font: inherit; font-size: max(16px, var(--fs-lg)); line-height: 1.7; letter-spacing: .06em;
  color: var(--fg); background: transparent;
  border: none; border-bottom: 1px solid var(--muted); border-radius: 0;
  padding: 6px 2px; min-height: calc(1.7em + 14px); max-height: calc(var(--vvh) * 0.4); caret-color: var(--fg);
  overflow-y: auto; scrollbar-width: none; text-align: center; /* 字置中，送出時從中間散開 */
  -webkit-user-select: text; user-select: text;
}
textarea:focus { outline: none; border-bottom-color: var(--fg); }
textarea::-webkit-scrollbar { display: none; }
textarea::placeholder { color: var(--muted); }
.send { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--muted); font-size: 18px; color: var(--fg); }

/* ---------- 念球 ---------- */
.ear {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(var(--sa-t) + clamp(56px, 11vh, 110px));
  display: flex; align-items: center; gap: 22px; /* 小點放大時的光圈不碰到字 */
  min-height: 44px; padding: 0 14px;
  font-size: var(--fs-xs); letter-spacing: .4em; color: var(--muted);
}
.ear i { flex: none; width: 8.5px; height: 8.5px; border-radius: 50%; background: currentColor; animation: ear 2.4s ease-out infinite; }
@keyframes ear { 0% { box-shadow: 0 0 0 0 rgba(140,140,134,.6); } 100% { box-shadow: 0 0 0 14px rgba(140,140,134,0); } }
.whisper {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(var(--sa-t) + clamp(56px, 11vh, 110px));
  width: min(30em, calc(100% - var(--gutter) * 2));
  margin: 0; text-align: center;
  font-size: var(--fs-md); line-height: 1.9; color: var(--fg); opacity: .85;
  animation: fade 1.2s ease both;
  cursor: pointer; letter-spacing: normal;
}
.crisis {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(var(--sa-t) + clamp(120px, 22vh, 200px));
  width: min(28em, calc(100% - var(--gutter) * 2));
  font-size: var(--fs-sm); line-height: 1.8; text-align: center; color: var(--fg);
}
.crisis a { color: inherit; }
.actions {
  position: absolute; left: 0; right: 0;
  bottom: calc(var(--sa-b) + clamp(24px, 6vh, 56px));
  display: flex; justify-content: center; gap: clamp(32px, 8vw, 72px);
}
.word {
  min-height: 44px; padding: 0 8px;
  font-size: var(--fs-md); letter-spacing: .5em; text-indent: .5em;
  border-bottom: 1px solid transparent; transition: border-color .3s;
}
@media (hover: hover) { .word:hover { border-bottom-color: currentColor; } }

/* ---------- 睡前咒語：咒語、念珠、提示垂直置中 ---------- */
#screen-spell.active { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: clamp(20px, 4vh, 36px); padding: calc(var(--sa-t) + var(--vvh) * 0.1) var(--gutter) calc(var(--vvh) * 0.46); } /* 文字在上半部，球在下方 */
#screen-spell > :not(#btn-skip) { pointer-events: none; }
.spell {
  margin: 0; max-width: 18em; text-align: center;
  font-size: var(--fs-xl); font-weight: 400; line-height: 1.8; letter-spacing: .08em;
  text-wrap: balance; transition: opacity .25s;
}
.slots { display: flex; justify-content: center; gap: 10px; }
.slots i { width: 8px; height: 8px; border-radius: 50%; border: 1px solid var(--fg); opacity: .5; transition: all .5s; }
.slots i.lit { background: var(--c, #fff); border-color: transparent; opacity: 1; box-shadow: 0 0 10px 2px var(--c, #fff); }
#screen-spell .hint { position: absolute; }

/* ---------- 回聲（原收集冊）：依日期排列的紀錄 ---------- */
.album {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(var(--sa-t) + clamp(72px, 12vh, 112px));
  bottom: 0;
  width: min(640px, calc(100% - var(--gutter) * 2 - var(--sa-l) - var(--sa-r)));
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: none;
  padding-bottom: calc(var(--sa-b) + 40px);
}
.album::-webkit-scrollbar { display: none; }
.day { margin-bottom: clamp(22px, 4vh, 36px); }
.day-label { margin: 0 0 12px; font-size: var(--fs-xs); color: var(--muted); letter-spacing: .25em; }
.day-orbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 14px 8px; }
.rec { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 6px; min-height: 72px; padding: 4px 0; transition: transform .2s; }
.rec:active { transform: scale(.94); }
.rec span { font-size: var(--fs-xs); color: var(--muted); letter-spacing: .12em; font-variant-numeric: tabular-nums; }
/* 柔邊小球：怨是灰階，善是彩色（--h 色相） */
.orb-dot {
  display: block; width: var(--d, 40px); height: var(--d, 40px); border-radius: 50%; flex: none;
  -webkit-mask-image: radial-gradient(circle closest-side, #000 0%, #000 52%, rgba(0,0,0,.72) 70%, rgba(0,0,0,.28) 86%, transparent 100%); mask-image: radial-gradient(circle closest-side, #000 0%, #000 52%, rgba(0,0,0,.72) 70%, rgba(0,0,0,.28) 86%, transparent 100%); /* 和念球一樣柔和淡出的邊 */
}
.orb-dot.gloom { background: radial-gradient(circle at 36% 32%, hsl(240 3% calc(22% + var(--l, .5) * 18%)), #1d1d20 45%, #0c0c0e 70%); }
.orb-dot.glow {
  background:
    radial-gradient(circle at 30% 68%, hsl(var(--h) 92% 60% / .55), transparent 46%),
    radial-gradient(circle at 70% 32%, hsl(calc(var(--h) + 55) 88% 62% / .5), transparent 46%),
    radial-gradient(circle at 36% 32%, #fff, hsl(calc(var(--h) + 25) 70% 84%) 50%, hsl(calc(var(--h) + 60) 70% 76%));
}
.record {
  position: absolute; inset: 0; z-index: 2;
  background: var(--bg, #f3f3f1);
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: none;
  animation: fade .45s ease both;
}
.record::-webkit-scrollbar { display: none; }
.record-body {
  width: min(30em, calc(100% - var(--gutter) * 2));
  margin: 0 auto;
  padding: calc(var(--sa-t) + clamp(56px, 10vh, 96px)) 0 calc(var(--sa-b) + 48px);
  display: flex; flex-direction: column; align-items: center; text-align: center;
}
.record-orb .orb-dot { animation: breathe 7s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.04); } }
.record-time { margin: 18px 0 0; font-size: var(--fs-xs); color: var(--muted); letter-spacing: .2em; font-variant-numeric: tabular-nums; }
.record-mood { margin: 6px 0 0; font-size: var(--fs-sm); color: var(--muted); letter-spacing: .3em; }
.record-mood:empty { display: none; }
.record-part { margin-top: clamp(28px, 5vh, 40px); width: 100%; }
.record-part h2 { margin: 0 0 10px; font-size: var(--fs-xs); font-weight: 400; color: var(--muted); letter-spacing: .4em; text-indent: .4em; }
.record-part p { margin: 0; font-size: var(--fs-md); line-height: 1.95; white-space: pre-wrap; overflow-wrap: anywhere; }
.record-end { margin: clamp(28px, 5vh, 40px) 0 0; font-size: var(--fs-xs); color: var(--muted); letter-spacing: .3em; }
.record-end:empty { display: none; }
.record-del { margin-top: clamp(36px, 7vh, 56px); min-height: 44px; padding: 0 12px; font-size: var(--fs-xs); color: var(--muted); letter-spacing: .25em; }
.meta {
  position: absolute; left: 0; right: 0;
  bottom: calc(var(--sa-b) + clamp(48px, 9vh, 80px));
  margin: 0; text-align: center; font-size: var(--fs-sm); color: var(--muted); letter-spacing: .3em;
}

/* ---------- 登入：置中一欄，字少 ---------- */
.acc {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(320px, calc(100% - var(--gutter) * 2));
  display: flex; flex-direction: column; align-items: center; gap: clamp(14px, 2.6vh, 22px);
  text-align: center;
}
.acc p { margin: 0; }
.acc-lead { font-size: var(--fs-md); line-height: 2; letter-spacing: .2em; }
.access.inline { position: static; transform: none; }
.acc-main { letter-spacing: .3em; text-indent: .3em; border-bottom-color: var(--line); }
.acc-note { font-size: var(--fs-xs); line-height: 1.9; color: var(--muted); letter-spacing: .15em; }
.acc-note a { color: inherit; }
.acc-merge { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.acc-pic { width: 56px; height: 56px; border-radius: 50%; object-fit: cover; }
.acc-name { font-size: var(--fs-lg); letter-spacing: .2em; }
.acc-links { display: flex; justify-content: center; gap: clamp(24px, 6vw, 48px); }
.acc-links .word { letter-spacing: .3em; text-indent: .3em; }
.acc-links.small .word { font-size: var(--fs-sm); color: var(--muted); }
#go-account { max-width: 9em; overflow: hidden; text-overflow: ellipsis; }

/* ---------- 提示訊息 ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sa-b) + clamp(72px, 14vh, 120px));
  max-width: calc(100% - var(--gutter) * 2);
  background: var(--ink); color: #f3f3f1;
  padding: 8px 18px; border-radius: 999px;
  font-size: var(--fs-sm); line-height: 1.6; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  z-index: 10; pointer-events: none;
}
/* ---------- 確認框 ---------- */
.ask {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center; padding: var(--gutter);
  background: rgba(243, 243, 241, .72);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  animation: fade .3s ease both;
}
.ask-card {
  width: min(300px, 100%);
  display: flex; flex-direction: column; align-items: center; gap: 18px;
  padding: 30px 22px 14px; text-align: center;
  animation: rise .45s cubic-bezier(.2, .8, .2, 1) both;
}
@keyframes rise { from { transform: translateY(10px); opacity: 0; } }
.ask-orb {
  width: 42px; height: 42px; border-radius: 50%;
  background: radial-gradient(circle at 36% 32%, #55555b, #1d1d20 45%, #0c0c0e 70%);
  -webkit-mask-image: radial-gradient(circle closest-side, #000 52%, rgba(0,0,0,.6) 74%, transparent 100%);
  mask-image: radial-gradient(circle closest-side, #000 52%, rgba(0,0,0,.6) 74%, transparent 100%);
  animation: breathe 5s ease-in-out infinite;
}
.ask-text { margin: 0; font-size: var(--fs-md); line-height: 1.9; letter-spacing: .08em; text-wrap: balance; }
.ask-actions { display: flex; justify-content: center; gap: clamp(36px, 12vw, 64px); margin-top: 4px; }
.ask-actions .word { letter-spacing: .4em; text-indent: .4em; color: var(--muted); }
.ask-actions .ask-ok { color: var(--fg); }
[hidden] { display: none !important; }

/* ---------- 矮螢幕（手機橫式） ---------- */
@media (max-height: 480px) {
  :root { --edge: 14px; }
  .brand { top: calc(var(--sa-t) + 14px); }
  .ear, .whisper { top: calc(var(--sa-t) + 12px); }
  .crisis { top: calc(var(--sa-t) + 64px); }
  .actions { bottom: calc(var(--sa-b) + 10px); }
  .meta { bottom: calc(var(--sa-b) + 14px); }
  .hint { bottom: calc(var(--sa-b) + 12px); }
  .album { top: calc(var(--sa-t) + 52px); }
  .write-form { top: calc(var(--vvh) * 0.45); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
