/* ============================================================
   老地方见 —— 深夜书房里的一本旧书
   手机优先。中文手写体走系统楷体栈，不依赖外部字体文件，
   部署后离线也能显示。
   ============================================================ */

:root {
  --ink:      #3a2c1e;
  --ink-soft: #6b5942;
  --paper:    #e8dcc4;
  --paper-hi: #f2e9d6;
  --room:     #1a1410;
  --candle:   #ffb765;
  --seal:     #8c4a3f;

  --hand: "Kaiti SC", "STKaiti", KaiTi, "楷体", "TsangerYuYangT", serif;
  --body: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--room);
  color: var(--ink);
  font-family: var(--body);
  -webkit-text-size-adjust: 100%;
}

body {
  /* 屋子：中间一圈烛光，四周压暗 */
  background:
    radial-gradient(120% 80% at 50% 34%, #2e2318 0%, #1a1410 58%, #0d0a07 100%);
  overflow: hidden;
}

/* ---------------------------------------------------------
   密码门
   ------------------------------------------------------- */
#gate {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  background: radial-gradient(80% 60% at 50% 45%, #241b13 0%, #0d0a07 100%);
  transition: opacity .7s ease, visibility .7s;
}
#gate.done { opacity: 0; visibility: hidden; pointer-events: none; }

.gate-box { text-align: center; padding: 0 32px; }

.gate-seal {
  width: 62px; height: 62px; margin: 0 auto 22px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: radial-gradient(circle at 38% 34%, #b4614f, var(--seal) 62%, #5e2f28);
  color: #f0d9c0; font-size: 24px;
  box-shadow: 0 6px 20px #0009, inset 0 -2px 6px #0006;
  animation: seal-breathe 4.5s ease-in-out infinite;
}
@keyframes seal-breathe {
  0%, 100% { transform: scale(1);    box-shadow: 0 6px 20px #0009, inset 0 -2px 6px #0006; }
  50%      { transform: scale(1.05); box-shadow: 0 8px 30px #000b, inset 0 -2px 6px #0006; }
}

.gate-hint {
  margin: 0 0 26px;
  font-family: var(--hand);
  font-size: 19px; letter-spacing: .16em;
  color: #a3907a;
}

#gate-form { display: flex; gap: 12px; justify-content: center; align-items: stretch; }

#gate-input {
  width: 150px; height: 48px; padding: 0 0 4px 14px;
  border: 0; border-bottom: 1.5px solid #6b5942;
  background: transparent;
  color: #e8dcc4;
  font-family: var(--hand);
  font-size: 22px; letter-spacing: .5em; text-align: center;
  outline: none;
  transition: border-color .3s;
}
#gate-input:focus { border-color: var(--candle); }
#gate-input::placeholder { color: #55483a; letter-spacing: .3em; }

#gate-form button {
  height: 48px; padding: 0 22px;
  border: 1px solid #6b5942; border-radius: 3px;
  background: transparent; color: #c8b294;
  font-family: var(--hand); font-size: 16px; letter-spacing: .1em;
  cursor: pointer;
  transition: background .3s, color .3s, border-color .3s;
}
#gate-form button:hover,
#gate-form button:active { background: #6b594222; color: var(--candle); border-color: var(--candle); }

.gate-err {
  min-height: 20px; margin: 18px 0 0;
  font-family: var(--hand); font-size: 14px; color: #a8635a;
  opacity: 0; transition: opacity .3s;
}
.gate-err.show { opacity: 1; }

.shake { animation: shake .4s; }
@keyframes shake {
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* ---------------------------------------------------------
   书房
   ------------------------------------------------------- */
#room {
  position: relative; z-index: 1;
  height: 100%;
  display: flex; flex-direction: column;
  align-items: center; justify-content: flex-start;
  padding: max(14px, env(safe-area-inset-top)) 16px max(14px, env(safe-area-inset-bottom));
  opacity: 0; transition: opacity 1s ease .2s;
}
#room.in { opacity: 1; }

/* --- 烛火 --- */
.candle {
  position: absolute; top: 16px; left: 50%;
  transform: translateX(-50%);
  pointer-events: none;
}
.candle .flame {
  display: block; width: 13px; height: 22px;
  border-radius: 50% 50% 46% 46% / 62% 62% 38% 38%;
  background: radial-gradient(ellipse at 50% 72%, #fff3d0 0%, var(--candle) 45%, #e0642a 100%);
  filter: blur(.4px);
  animation: flicker 2.6s ease-in-out infinite;
  transform-origin: 50% 100%;
}
.candle .glow {
  position: absolute; top: -46px; left: 50%;
  width: 240px; height: 200px; margin-left: -120px;
  border-radius: 50%;
  background: radial-gradient(circle, #ffb76522 0%, transparent 68%);
  animation: flicker-glow 3.4s ease-in-out infinite;
}
@keyframes flicker {
  0%, 100% { transform: scaleY(1)    rotate(-1deg); opacity: 1; }
  30%      { transform: scaleY(1.13) rotate(1.5deg); opacity: .92; }
  60%      { transform: scaleY(.94)  rotate(-2deg); opacity: .97; }
}
@keyframes flicker-glow {
  0%, 100% { opacity: .85; } 45% { opacity: 1; } 70% { opacity: .7; }
}

/* --- 书 --- */
.book {
  width: min(560px, 100%);
  /* 撑满可用高度：书是主体，不该缩在屏幕中间一小条 */
  flex: 1 1 auto;
  min-height: 0;
  margin-top: 52px;              /* 给烛火留出位置 */
  display: flex;
  filter: drop-shadow(0 26px 46px #000a);
}

.page {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex; flex-direction: column;
  padding: 30px 26px 22px 32px;   /* 左边多留一点，别压在书脊上 */
  overflow: hidden;               /* 书脊的圆角要能裁住 */
  border-radius: 3px 8px 8px 3px;
  background:
    /* 纸的斑驳 */
    radial-gradient(60% 40% at 22% 18%, #fff8e8aa, transparent 62%),
    radial-gradient(50% 40% at 78% 82%, #d9c9a855, transparent 60%),
    linear-gradient(105deg, var(--paper-hi), var(--paper) 42%, #dfd0b4 100%);
  box-shadow:
    inset 14px 0 26px -18px #00000055,   /* 靠书脊那侧的阴影 */
    inset 0 0 60px -18px #8b7a5c55;
}

/* 书脊 */
.page-edge {
  position: absolute; left: 0; top: 0; bottom: 0; width: 9px;
  background: linear-gradient(90deg, #6b5942, #8f7a5b 40%, transparent);
  border-radius: 3px 0 0 3px;
}

.page-head { flex: none; text-align: center; padding-bottom: 16px; }
.page-head h1 {
  margin: 0 0 10px;
  font-family: var(--hand);
  font-size: clamp(21px, 5.6vw, 27px);
  font-weight: 400; letter-spacing: .1em;
  color: var(--ink);
}
.page-head p {
  margin: 0;
  font-family: var(--hand);
  font-size: 14px; line-height: 1.9; letter-spacing: .05em;
  color: var(--ink-soft);
}

/* --- 对话 --- */
.thread {
  flex: 1 1 auto;
  min-height: 84px;
  overflow-y: auto;
  padding: 4px 2px 8px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.thread::-webkit-scrollbar { width: 3px; }
.thread::-webkit-scrollbar-thumb { background: #b0a084; border-radius: 3px; }

.turn { margin: 0 0 18px; }

/* 她写的：偏右，颜色浅一点，像铅笔 */
.turn.me {
  text-align: right;
  font-family: var(--hand);
  font-size: 16px; line-height: 2; letter-spacing: .04em;
  color: #7d6a52;
  white-space: pre-wrap; word-break: break-word;
}
.turn.me::after {
  content: ""; display: block;
  width: 46px; height: 1px; margin: 8px 0 0 auto;
  background: linear-gradient(90deg, transparent, #b3a288);
}

/* 书写的：正文墨色 */
.turn.reply {
  font-family: var(--hand);
  font-size: 17.5px; line-height: 2.15; letter-spacing: .045em;
  color: var(--ink);
  white-space: pre-wrap; word-break: break-word;
}

/* 逐字浮现 + 墨迹晕开 */
.turn.reply .ch {
  opacity: 0;
  animation: ink-in .5s ease forwards;
}
@keyframes ink-in {
  0%   { opacity: 0; filter: blur(3px); }
  55%  { opacity: .85; filter: blur(.7px); }
  100% { opacity: 1; filter: blur(0); }
}

/* 思考中：墨点呼吸 */
.thinking {
  display: flex; gap: 7px; align-items: center;
  padding: 6px 0 2px;
}
.thinking i {
  width: 5px; height: 5px; border-radius: 50%;
  background: #8d7a5e;
  animation: blot 1.5s ease-in-out infinite;
}
.thinking i:nth-child(2) { animation-delay: .22s; }
.thinking i:nth-child(3) { animation-delay: .44s; }
@keyframes blot {
  0%, 100% { opacity: .25; transform: scale(.85); }
  50%      { opacity: .9;  transform: scale(1.25); }
}

.turn.err {
  font-family: var(--hand); font-size: 14.5px;
  color: #9c6055; letter-spacing: .04em;
}

/* --- 写字区 --- */
.ask {
  flex: none;
  display: flex; align-items: flex-end; gap: 10px;
  padding-top: 12px;
  border-top: 1px solid #b9a98c66;
}

#ask-input {
  flex: 1;
  min-height: 44px; max-height: 128px;
  padding: 8px 2px;
  border: 0; background: transparent; resize: none; outline: none;
  font-family: var(--hand);
  font-size: 16.5px; line-height: 1.9; letter-spacing: .04em;
  color: var(--ink);
}
#ask-input::placeholder { color: #a4947a; }

#ask-send {
  flex: none;
  width: 38px; height: 38px; margin-bottom: 3px;
  display: grid; place-items: center;
  border: 1px solid #b0a084; border-radius: 50%;
  background: transparent; color: var(--ink-soft);
  cursor: pointer;
  transition: background .25s, color .25s, border-color .25s, opacity .25s;
}
#ask-send svg { width: 19px; height: 19px; }
#ask-send:hover:not(:disabled) { background: #b0a08422; color: var(--ink); border-color: var(--ink-soft); }
#ask-send:disabled { opacity: .35; cursor: default; }

.ask-note {
  flex: none;
  margin: 8px 0 0; text-align: center;
  font-family: var(--hand); font-size: 12px; letter-spacing: .1em;
  color: #a4947a;
}

/* --- 底部导航 --- */
.shelf {
  flex: none;
  margin-top: 20px;
  font-family: var(--hand); font-size: 14px; letter-spacing: .12em;
}
.shelf a, .shelf .dot { color: #7f6e57; text-decoration: none; }
.shelf a { border-bottom: 1px solid transparent; transition: color .3s, border-color .3s; }
.shelf a:hover { color: var(--candle); border-bottom-color: #ffb76566; }
.shelf .dot { margin: 0 12px; }

/* --- 窄屏 --- */
@media (max-height: 680px) {
  .candle { top: 1.5%; }
  .page { padding: 22px 20px 18px; }
  .page-head { padding-bottom: 10px; }
  .page-head p { font-size: 13px; line-height: 1.7; }
  .ask-note { display: none; }
}
@media (max-width: 380px) {
  .page { padding: 24px 18px 18px; }
}

/* 不喜欢动效就都停掉 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .turn.book .ch { opacity: 1; }
}
