/* ===== 信纸整体容器 ===== */
.modal.letter-modal {
  width: 95%;
  max-width: 680px;
  margin: 0 auto;
  padding: 0;
  border: none;
  border-radius: 16px;
  box-shadow:
    0 25px 60px rgba(0, 0, 0, 0.25),
    0 0 0 1px rgba(255, 255, 255, 0.08);
  overflow: hidden;
  animation: none;
  transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.25s ease;
  opacity: 1;
  transform: translateY(0);
}

.modal.letter-modal:not(.open) {
  opacity: 0;
  transform: translateY(20px) scale(0.97);
}

/* ===== 信纸顶部装饰条 ===== */
.letter-paper-top {
  height: 6px;
  background: linear-gradient(90deg,
    var(--brand-strong, #0b4fa8) 0%,
    #4fb3ff 40%,
    var(--brand-strong, #0b4fa8) 100%);
  flex-shrink: 0;
}

/* ===== 信纸头部 ===== */
.letter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  position: relative;
  z-index: 2;
}

.letter-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.letter-icon {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--brand-strong, #0b4fa8), #4fb3ff);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: #fff;
  box-shadow: 0 2px 8px rgba(11, 79, 168, 0.25);
  flex-shrink: 0;
}

.letter-title-group {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.letter-title {
  margin: 0;
  font-family: 'Cormorant Garamond', serif;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text, #1f343e);
  letter-spacing: 0.06em;
}

.letter-subtitle {
  font-size: 0.68rem;
  color: var(--muted, #5d7079);
  letter-spacing: 0.04em;
}

.letter-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.letter-saved {
  font-size: 0.72rem;
  color: var(--muted, #5d7079);
  padding-right: 8px;
  white-space: nowrap;
  opacity: 0.8;
}

/* ===== 信纸工具栏 ===== */
.letter-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 20px;
  background: rgba(255, 255, 255, 0.6);
  border-bottom: 1px solid rgba(0, 0, 0, 0.04);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  position: relative;
  z-index: 2;
}

.letter-toolbar::-webkit-scrollbar {
  display: none;
}

.letter-style-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.06);
  background: rgba(255, 255, 255, 0.7);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text, #1f343e);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s ease;
}

.letter-style-btn:hover {
  background: rgba(11, 79, 168, 0.08);
  border-color: rgba(11, 79, 168, 0.2);
}

.letter-style-btn.active {
  background: var(--brand-strong, #0b4fa8);
  color: #fff;
  border-color: var(--brand-strong, #0b4fa8);
  box-shadow: 0 2px 8px rgba(11, 79, 168, 0.25);
}

.letter-style-icon {
  font-size: 0.85rem;
  line-height: 1;
}

.letter-toolbar-divider {
  width: 1px;
  height: 20px;
  background: rgba(0, 0, 0, 0.08);
  margin: 0 4px;
  flex-shrink: 0;
}

/* ===== 信纸主体区域（模拟纸张） ===== */
.letter-paper-wrap {
  position: relative;
  background: #faf8f0;
  overflow: hidden;
}

/* 纸张纹理层 */
.letter-paper-wrap::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    url("data:image/svg+xml,%3Csvg width='100' height='100' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100' height='100' filter='url(%23n)' opacity='0.025'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 1;
}

/* 纸张阴影（左右内凹） */
.letter-paper-wrap::after {
  content: '';
  position: absolute;
  inset: 0;
  box-shadow:
    inset 8px 0 16px -8px rgba(0, 0, 0, 0.06),
    inset -8px 0 16px -8px rgba(0, 0, 0, 0.06),
    inset 0 6px 12px -6px rgba(0, 0, 0, 0.04);
  pointer-events: none;
  z-index: 2;
}

/* ===== 横线样式（lines） ===== */
.letter-paper-wrap.style-lines {
  background: #faf8f0;
}

.letter-area {
  width: 100%;
  min-height: 280px;
  height: 52vh;
  max-height: 70vh;
  border: none;
  outline: none;
  padding: 28px 32px 28px 68px;
  font-size: 15px;
  line-height: 28px;
  resize: vertical;
  color: #2c2c2c;
  background-color: transparent;
  background-image: none;
  border-radius: 0;
  box-sizing: border-box;
  font-family: 'Cormorant Garamond', 'Georgia', serif;
  font-weight: 500;
  overscroll-behavior: contain;
  position: relative;
  z-index: 1;
  transition: background 0.3s ease;
  tab-size: 4;
}

/* 横线效果通过 wrapper 上的 ::after 模拟 */
.letter-paper-wrap.style-lines .letter-area {
  background:
    repeating-linear-gradient(
      to bottom,
      transparent 0px,
      transparent 27px,
      rgba(180, 160, 130, 0.2) 27px,
      rgba(180, 160, 130, 0.2) 28px
    );
}

/* 红色竖线（装订线） */
.letter-paper-wrap.style-lines .letter-paper-wrap-inner::after {
  content: '';
  position: absolute;
  left: 56px;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(200, 80, 80, 0.2);
  z-index: 1;
  pointer-events: none;
}

.letter-paper-wrap-inner {
  position: relative;
}

/* ===== 网格样式 ===== */
.letter-paper-wrap.style-grid .letter-area {
  background:
    repeating-linear-gradient(
      to right,
      transparent 0px,
      transparent 27px,
      rgba(180, 160, 130, 0.12) 27px,
      rgba(180, 160, 130, 0.12) 28px
    ),
    repeating-linear-gradient(
      to bottom,
      transparent 0px,
      transparent 27px,
      rgba(180, 160, 130, 0.12) 27px,
      rgba(180, 160, 130, 0.12) 28px
    );
  padding-left: 32px;
}

/* ===== 空白样式 ===== */
.letter-paper-wrap.style-blank .letter-area {
  background: none;
  padding-left: 32px;
}

/* ===== 牛皮纸样式 ===== */
.letter-paper-wrap.style-kraft {
  background: #e8dcc8;
}

.letter-paper-wrap.style-kraft .letter-area {
  background:
    repeating-linear-gradient(
      to bottom,
      transparent 0px,
      transparent 27px,
      rgba(140, 115, 85, 0.18) 27px,
      rgba(140, 115, 85, 0.18) 28px
    );
  color: #3d3225;
}

.letter-paper-wrap.style-kraft::before {
  background-image:
    url("data:image/svg+xml,%3Csvg width='200' height='200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='5' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.06'/%3E%3C/svg%3E");
}

/* ===== 焦点状态 ===== */
.letter-area:focus {
  box-shadow: none;
  outline: none;
}

/* ===== 字数统计栏 ===== */
.letter-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.7);
  border-top: 1px solid rgba(0, 0, 0, 0.04);
  font-size: 0.7rem;
  color: var(--muted, #5d7079);
  position: relative;
  z-index: 2;
}

.letter-word-count {
  font-weight: 500;
  letter-spacing: 0.02em;
}

.letter-footer-hint {
  opacity: 0.6;
  font-size: 0.68rem;
}

/* ===== 弹窗背景遮罩 ===== */
.modal-backdrop#letterModal {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1200;
  padding: 1rem;
  animation: letterBackdropIn 0.25s ease;
}

@keyframes letterBackdropIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ===== 悬浮按钮 - 右上角，远离BGM播放器 ===== */
.floating-letter-btn {
  position: fixed;
  right: calc(16px + env(safe-area-inset-right, 0px));
  top: calc(72px + env(safe-area-inset-top, 0px));
  z-index: 1140;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  padding: 0 14px 0 10px;
  border-radius: 999px;
  background: var(--paper, rgba(255, 255, 255, 0.92));
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--brand-strong, #0b4fa8);
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow:
    0 4px 16px rgba(12, 25, 31, 0.1),
    0 1px 4px rgba(0, 0, 0, 0.06);
  cursor: pointer;
  font-size: 15px;
  font-weight: 700;
  touch-action: manipulation;
  transition: all 0.25s cubic-bezier(0.22, 1, 0.36, 1);
  animation: letterBtnIn 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0.3s both;
}

.floating-letter-btn .letter-btn-label {
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  line-height: 1;
}

@keyframes letterBtnIn {
  from {
    opacity: 0;
    transform: scale(0.8) translateX(10px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateX(0);
  }
}

.floating-letter-btn:hover {
  transform: translateY(-2px);
  box-shadow:
    0 8px 24px rgba(12, 25, 31, 0.14),
    0 2px 6px rgba(0, 0, 0, 0.08);
  background: #fff;
}

.floating-letter-btn:active {
  transform: scale(0.97);
}

/* ===== 锁定 body ===== */
body.letter-open {
  overflow: hidden;
}

/* ===== 移动端响应 ===== */
@media (max-width: 640px) {
  .modal-backdrop#letterModal {
    align-items: flex-end;
    padding: 0;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }

  .modal.letter-modal {
    width: 100%;
    max-width: none;
    height: 85vh;
    max-height: calc(100vh - 0px);
    border-radius: 20px 20px 0 0;
    margin: 0;
    transform: translateY(100%);
    transition: transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
  }

  .modal.letter-modal.open {
    transform: translateY(0);
    opacity: 1;
  }

  /* 移动端拖拽指示条 */
  .letter-mobile-handle {
    display: block;
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: rgba(0, 0, 0, 0.15);
    margin: 10px auto 0;
    flex-shrink: 0;
  }

  .letter-header {
    padding: 12px 16px;
  }

  .letter-toolbar {
    padding: 6px 16px;
  }

  .letter-area {
    height: calc(85vh - 130px);
    padding: 20px 20px 20px 56px;
    font-size: 15px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  }

  .letter-paper-wrap.style-grid .letter-area,
  .letter-paper-wrap.style-blank .letter-area {
    padding-left: 20px;
  }

  .letter-paper-wrap.style-lines .letter-paper-wrap-inner::after {
    left: 44px;
  }

  .letter-saved {
    display: none;
  }

  .floating-letter-btn {
    right: calc(12px + env(safe-area-inset-right, 0px));
    top: calc(66px + env(safe-area-inset-top, 0px));
    height: 36px;
    padding: 0 12px 0 9px;
    font-size: 14px;
  }

  .floating-letter-btn .letter-btn-label {
    font-size: 0.68rem;
  }

  .letter-footer {
    padding: 8px 16px;
  }

  .letter-footer-hint {
    display: none;
  }
}

@media (max-width: 380px) {
  .letter-area {
    padding: 16px 16px 16px 48px;
    font-size: 14px;
    line-height: 26px;
  }

  .letter-paper-wrap.style-lines .letter-area {
    background:
      repeating-linear-gradient(
        to bottom,
        transparent 0px,
        transparent 25px,
        rgba(180, 160, 130, 0.2) 25px,
        rgba(180, 160, 130, 0.2) 26px
      );
  }

  .letter-paper-wrap.style-lines .letter-paper-wrap-inner::after {
    left: 36px;
  }

  .letter-style-btn {
    padding: 5px 9px;
    font-size: 0.7rem;
  }
}

/* 桌面端隐藏拖拽条 */
.letter-mobile-handle {
  display: none;
}

/* ===== 暗色模式适配 ===== */
@media (prefers-color-scheme: dark) {
  .letter-paper-wrap {
    background: #f5f2e8;
  }

  .letter-area {
    color: #2c2c2c;
  }

  .modal.letter-modal {
    box-shadow:
      0 25px 60px rgba(0, 0, 0, 0.5),
      0 0 0 1px rgba(255, 255, 255, 0.06);
  }
}
