/* ==========================================================================
   StoryForge — 櫻霧文庫 × 暮色月臺
   Pure CSS / Zero Dependencies / Light Default + Dark Twilight Theme
   ========================================================================== */

/* ---------- 1. 色彩與排版 Token（預設：淺色暖白粉霧） ---------- */
:root {
  color-scheme: light;

  /* 核心色票 */
  --bg: #FCF7F5;
  --panel: #FFFDFC;
  --panel2: #F6EAE8;
  --input-bg: #FFFFFF;
  --line: #E8D4D2;
  --line-strong: #AA8785;
  --text: #35282C;
  --muted: #725C63;
  --accent: #B04564;
  --accent-hover: #993754;
  --accent-text: #FFFFFF;
  --accent2: #6B5E99;
  --danger: #B53D49;
  --ok: #34735B;

  /* 語意輔助色 */
  --accent-soft: rgba(176, 69, 100, 0.09);
  --accent-softer: rgba(176, 69, 100, 0.045);
  --accent2-soft: rgba(107, 94, 153, 0.10);
  --danger-soft: rgba(181, 61, 73, 0.08);
  --notice-bg: #FDF2F4;
  --notice-line: #E6BCC6;
  --notice-text: #6E3847;
  --error-bg: #FDF0F1;
  --error-line: #E5B0B6;
  --error-text: #9E2A36;
  --header-bg: rgba(252, 247, 245, 0.84);
  --modal-backdrop: rgba(46, 28, 38, 0.48);
  --toast-bg: rgba(53, 40, 44, 0.94);
  --toast-text: #FFFDFC;

  /* 漸層 */
  --grad-bg:
    radial-gradient(960px 540px at 8% -6%, rgba(240, 196, 208, 0.34), transparent 62%),
    radial-gradient(880px 520px at 94% 4%, rgba(206, 198, 236, 0.28), transparent 60%),
    linear-gradient(180deg, #FDF8F6 0%, #FAF3F2 100%);
  --grad-accent: linear-gradient(135deg, #BD4F6E 0%, #9F4368 100%);
  --grad-accent-hover: linear-gradient(135deg, #A93E5E 0%, #8B3559 100%);
  --grad-ribbon: linear-gradient(90deg, #D9859B 0%, #A995D1 100%);
  --grad-dir: linear-gradient(90deg, rgba(176, 69, 100, 0.07) 0%, rgba(107, 94, 153, 0.04) 100%);

  /* 陰影 */
  --shadow-sm: 0 2px 10px rgba(92, 46, 62, 0.05), 0 1px 2px rgba(92, 46, 62, 0.04);
  --shadow-md: 0 10px 28px rgba(92, 46, 62, 0.08), 0 2px 6px rgba(92, 46, 62, 0.04);
  --shadow-lg: 0 22px 54px rgba(62, 32, 44, 0.16), 0 4px 12px rgba(62, 32, 44, 0.06);
  --shadow-accent: 0 6px 18px rgba(176, 69, 100, 0.26);
  --ring: 0 0 0 3px rgba(176, 69, 100, 0.20);

  /* 字體 */
  --font-ui: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  --font-serif: "Noto Serif TC", "Songti TC", "PMingLiU", serif;
  --font-display: "LXGW WenKai TC", "Noto Serif TC", "Kaiti TC", serif;
  --font-brand: "Cormorant Garamond", "Noto Serif TC", Georgia, serif;

  /* 圓角與節奏 */
  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 22px;
  --radius-pill: 999px;
}

/* ---------- 1b. 深色主題（夜晚藍紫） ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;

  --bg: #13111E;
  --panel: #1B182A;
  --panel2: #252038;
  --input-bg: #161324;
  --line: #37304F;
  --line-strong: #6F6494;
  --text: #F0EAF4;
  --muted: #ADA4C4;
  --accent: #EA95AB;
  --accent-hover: #F3ABC0;
  --accent-text: #210F18;
  --accent2: #B4A7E8;
  --danger: #F28590;
  --ok: #7EC9AA;

  --accent-soft: rgba(234, 149, 171, 0.14);
  --accent-softer: rgba(234, 149, 171, 0.07);
  --accent2-soft: rgba(180, 167, 232, 0.14);
  --danger-soft: rgba(242, 133, 144, 0.13);
  --notice-bg: #241C34;
  --notice-line: #52416E;
  --notice-text: #E8D5EB;
  --error-bg: #321B27;
  --error-line: #783A4E;
  --error-text: #F7B8C0;
  --header-bg: rgba(19, 17, 30, 0.82);
  --modal-backdrop: rgba(8, 6, 16, 0.72);
  --toast-bg: rgba(37, 32, 56, 0.96);
  --toast-text: #F5EFF9;

  --grad-bg:
    radial-gradient(900px 520px at 10% -8%, rgba(164, 96, 148, 0.20), transparent 62%),
    radial-gradient(820px 500px at 92% 4%, rgba(112, 98, 192, 0.22), transparent 60%),
    linear-gradient(180deg, #13111E 0%, #100E1A 100%);
  --grad-accent: linear-gradient(135deg, #EE9CB2 0%, #C99CE2 100%);
  --grad-accent-hover: linear-gradient(135deg, #F6AEC2 0%, #D7B0EC 100%);
  --grad-ribbon: linear-gradient(90deg, #EA95AB 0%, #B4A7E8 100%);
  --grad-dir: linear-gradient(90deg, rgba(234, 149, 171, 0.10) 0%, rgba(180, 167, 232, 0.05) 100%);

  --shadow-sm: 0 2px 12px rgba(4, 3, 12, 0.32);
  --shadow-md: 0 12px 32px rgba(4, 3, 12, 0.44);
  --shadow-lg: 0 24px 60px rgba(2, 2, 10, 0.62);
  --shadow-accent: 0 6px 20px rgba(234, 149, 171, 0.22);
  --ring: 0 0 0 3px rgba(234, 149, 171, 0.28);
}

/* 可選：當 <html data-theme="auto"> 時自動跟隨系統深色偏好 */
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    color-scheme: dark;
    --bg: #13111E;
    --panel: #1B182A;
    --panel2: #252038;
    --input-bg: #161324;
    --line: #37304F;
    --line-strong: #6F6494;
    --text: #F0EAF4;
    --muted: #ADA4C4;
    --accent: #EA95AB;
    --accent-hover: #F3ABC0;
    --accent-text: #210F18;
    --accent2: #B4A7E8;
    --danger: #F28590;
    --ok: #7EC9AA;
    --accent-soft: rgba(234, 149, 171, 0.14);
    --accent-softer: rgba(234, 149, 171, 0.07);
    --accent2-soft: rgba(180, 167, 232, 0.14);
    --danger-soft: rgba(242, 133, 144, 0.13);
    --notice-bg: #241C34;
    --notice-line: #52416E;
    --notice-text: #E8D5EB;
    --error-bg: #321B27;
    --error-line: #783A4E;
    --error-text: #F7B8C0;
    --header-bg: rgba(19, 17, 30, 0.82);
    --modal-backdrop: rgba(8, 6, 16, 0.72);
    --toast-bg: rgba(37, 32, 56, 0.96);
    --toast-text: #F5EFF9;
    --grad-bg:
      radial-gradient(900px 520px at 10% -8%, rgba(164, 96, 148, 0.20), transparent 62%),
      radial-gradient(820px 500px at 92% 4%, rgba(112, 98, 192, 0.22), transparent 60%),
      linear-gradient(180deg, #13111E 0%, #100E1A 100%);
    --grad-accent: linear-gradient(135deg, #EE9CB2 0%, #C99CE2 100%);
    --grad-accent-hover: linear-gradient(135deg, #F6AEC2 0%, #D7B0EC 100%);
    --grad-ribbon: linear-gradient(90deg, #EA95AB 0%, #B4A7E8 100%);
    --grad-dir: linear-gradient(90deg, rgba(234, 149, 171, 0.10) 0%, rgba(180, 167, 232, 0.05) 100%);
    --shadow-sm: 0 2px 12px rgba(4, 3, 12, 0.32);
    --shadow-md: 0 12px 32px rgba(4, 3, 12, 0.44);
    --shadow-lg: 0 24px 60px rgba(2, 2, 10, 0.62);
    --shadow-accent: 0 6px 20px rgba(234, 149, 171, 0.22);
    --ring: 0 0 0 3px rgba(234, 149, 171, 0.28);
  }
}

/* ---------- 2. 基礎排版與全局樣式 ---------- */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background-color: var(--bg);
  background-image: var(--grad-bg);
  background-attachment: fixed;
  color: var(--text);
  font: 15px/1.68 var(--font-ui);
  accent-color: var(--accent);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

::selection {
  background: var(--accent-soft);
  color: var(--text);
}

a {
  color: var(--accent);
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
  transition: color 0.18s ease, opacity 0.18s ease;
}

a:hover {
  color: var(--accent-hover);
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 1px 6px;
  color: var(--text);
}

h1, h2, h3 {
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.03em;
  margin: 0.15em 0 0.55em;
  line-height: 1.35;
}

h1 { font-size: clamp(1.55rem, 2.4vw, 1.95rem); }
h2 { font-size: clamp(1.25rem, 1.8vw, 1.48rem); }
h3 { font-size: 1.08rem; }

p { margin: 0.55em 0; }

/* 跳至主內容連結 */
.skip-link {
  position: fixed;
  top: -100px;
  left: 16px;
  z-index: 120;
  padding: 10px 16px;
  background: var(--panel);
  color: var(--accent);
  border: 1.5px solid var(--accent);
  border-radius: var(--radius-sm);
  font-weight: 600;
  text-decoration: none;
  box-shadow: var(--shadow-md);
}
.skip-link:focus {
  top: 14px;
}

/* 全域鍵盤焦點指示（WCAG 2.4.7 / 1.4.11） */
:where(a, button, input, textarea, select, [tabindex]):focus-visible {
  outline: 2.5px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- 3. 背景微動畫裝飾層（花瓣／夜星） ---------- */
.sf-ambient {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
  display: none;
}

:root[data-ambient="off"] .sf-ambient {
  display: none !important;
}

@media (prefers-reduced-motion: no-preference) {
  .sf-ambient {
    display: block;
  }

  .sf-ambient span {
    position: absolute;
    top: -8%;
    width: 11px;
    height: 15px;
    border-radius: 80% 10% 80% 10%;
    background: linear-gradient(135deg, rgba(235, 165, 184, 0.42), rgba(206, 178, 226, 0.22));
    opacity: 0;
    will-change: transform, opacity;
    animation: sf-petal-drift 22s linear infinite;
  }

  .sf-ambient span:nth-child(1) { left: 10%; animation-duration: 24s; animation-delay: -3s; width: 10px; height: 14px; }
  .sf-ambient span:nth-child(2) { left: 26%; animation-duration: 28s; animation-delay: -11s; width: 8px; height: 12px; }
  .sf-ambient span:nth-child(3) { left: 46%; animation-duration: 21s; animation-delay: -6s; width: 12px; height: 16px; }
  .sf-ambient span:nth-child(4) { left: 66%; animation-duration: 26s; animation-delay: -15s; width: 9px; height: 13px; }
  .sf-ambient span:nth-child(5) { left: 82%; animation-duration: 23s; animation-delay: -9s; width: 11px; height: 14px; }
  .sf-ambient span:nth-child(6) { left: 92%; animation-duration: 29s; animation-delay: -18s; width: 8px; height: 11px; }

  /* 深色主題：花瓣自動轉為靜謐浮動星光 */
  :root[data-theme="dark"] .sf-ambient span {
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: rgba(236, 214, 248, 0.65);
    box-shadow: 0 0 10px rgba(234, 149, 171, 0.55);
    animation-name: sf-star-float;
  }
  :root[data-theme="dark"] .sf-ambient span:nth-child(1) { top: 18%; left: 12%; animation-duration: 9s; }
  :root[data-theme="dark"] .sf-ambient span:nth-child(2) { top: 32%; left: 78%; animation-duration: 11s; }
  :root[data-theme="dark"] .sf-ambient span:nth-child(3) { top: 64%; left: 18%; animation-duration: 13s; }
  :root[data-theme="dark"] .sf-ambient span:nth-child(4) { top: 24%; left: 52%; animation-duration: 10s; }
  :root[data-theme="dark"] .sf-ambient span:nth-child(5) { top: 72%; left: 86%; animation-duration: 12s; }
  :root[data-theme="dark"] .sf-ambient span:nth-child(6) { top: 48%; left: 92%; animation-duration: 14s; }
}

@keyframes sf-petal-drift {
  0%   { transform: translate3d(0, 0, 0) rotate(0deg); opacity: 0; }
  12%  { opacity: 0.34; }
  55%  { transform: translate3d(38px, 54vh, 0) rotate(130deg); opacity: 0.26; }
  88%  { opacity: 0.20; }
  100% { transform: translate3d(-18px, 108vh, 0) rotate(260deg); opacity: 0; }
}

@keyframes sf-star-float {
  0%, 100% { transform: translate3d(0, 0, 0) scale(0.85); opacity: 0.14; }
  50%      { transform: translate3d(0, -10px, 0) scale(1.25); opacity: 0.42; }
}

/* ---------- 4. 頂部導覽列 ---------- */
header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 24px;
  min-height: 62px;
  padding: 10px 28px;
  background: var(--header-bg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}

header::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -1px;
  height: 1px;
  background: var(--grad-ribbon);
  opacity: 0.38;
  pointer-events: none;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-family: var(--font-brand);
  font-weight: 600;
  font-size: 24px;
  line-height: 1;
  letter-spacing: 0.04em;
  text-decoration: none;
  color: var(--accent);
}

.brand-mark {
  width: 22px;
  height: 22px;
  color: var(--accent);
  flex: none;
}

nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

nav a {
  position: relative;
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 6px 14px;
  margin-right: 0;
  border-radius: var(--radius-pill);
  text-decoration: none;
  font-size: 14.5px;
  font-weight: 500;
  color: var(--muted);
  transition: color 0.18s ease, background-color 0.18s ease;
}

nav a:hover {
  color: var(--text);
  background: var(--accent-softer);
}

nav a.active {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
  border-bottom: none;
}

nav a.active::after {
  content: "";
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 4px;
  height: 2px;
  border-radius: 2px;
  background: var(--grad-ribbon);
}

.header-tools {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 5px 12px;
  border-radius: var(--radius-pill);
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.icon-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-softer);
}

.theme-icon-dark { display: none; }
:root[data-theme="dark"] .theme-icon-light { display: none; }
:root[data-theme="dark"] .theme-icon-dark { display: inline; }

/* ---------- 5. 主容器與頁尾 ---------- */
main {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: 28px 24px 56px;
  flex: 1 0 auto;
}

.site-footer {
  position: relative;
  z-index: 1;
  margin-top: auto;
  padding: 18px 24px 26px;
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
  border-top: 1px solid var(--line);
}

.site-footer p {
  margin: 0;
}

/* ---------- 6. 按鈕與表單元件 ---------- */
button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 38px;
  padding: 7px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong);
  background: var(--panel);
  color: var(--text);
  font: 500 14.5px/1.3 var(--font-ui);
  text-decoration: none;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition:
    transform 0.18s ease,
    border-color 0.18s ease,
    background-color 0.18s ease,
    color 0.18s ease,
    box-shadow 0.18s ease;
}

button:hover:not(:disabled),
.btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--panel2);
  box-shadow: var(--shadow-md);
}

@media (prefers-reduced-motion: no-preference) {
  button:hover:not(:disabled),
  .btn:hover {
    transform: translateY(-1px);
  }
  button:active:not(:disabled),
  .btn:active {
    transform: translateY(0);
  }
}

button.primary,
.btn.primary {
  background: var(--grad-accent);
  color: var(--accent-text);
  border-color: transparent;
  font-weight: 600;
  box-shadow: var(--shadow-accent);
}

button.primary:hover:not(:disabled),
.btn.primary:hover {
  background: var(--grad-accent-hover);
  color: var(--accent-text);
  border-color: transparent;
  box-shadow: 0 8px 22px rgba(176, 69, 100, 0.32);
}

button.danger {
  color: var(--danger);
  background: var(--panel2);
  border-color: var(--line);
  box-shadow: none;
}

button.danger:hover:not(:disabled) {
  color: var(--danger);
  background: var(--danger-soft);
  border-color: var(--danger);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

input, textarea, select {
  width: 100%;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 10px 13px;
  font: 15px/1.6 var(--font-ui);
  transition: border-color 0.18s ease, box-shadow 0.18s ease, background-color 0.18s ease;
}

input::placeholder,
textarea::placeholder {
  color: var(--muted);
  opacity: 0.78;
}

input:hover:not(:disabled),
textarea:hover:not(:disabled),
select:hover:not(:disabled) {
  border-color: var(--accent);
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: var(--ring);
  outline: none;
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

textarea {
  resize: vertical;
  line-height: 1.72;
}

select {
  cursor: pointer;
}

/* 檔案上傳按鈕美化（消除原生灰按鈕） */
input[type="file"] {
  padding: 6px 8px;
  font-size: 13.5px;
  color: var(--muted);
  background: var(--panel2);
  border: 1px dashed var(--line-strong);
  cursor: pointer;
}

input[type="file"]::file-selector-button {
  margin-right: 10px;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: var(--panel);
  color: var(--text);
  font: 500 13.5px/1.2 var(--font-ui);
  cursor: pointer;
  transition: border-color 0.18s ease, color 0.18s ease, background-color 0.18s ease;
}

input[type="file"]::file-selector-button:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-softer);
}

input[type="checkbox"] {
  width: 17px;
  height: 17px;
  margin: 0;
  padding: 0;
  flex: none;
  cursor: pointer;
  accent-color: var(--accent);
}

label {
  display: block;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--muted);
  margin: 14px 0 6px;
}

label.inline {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  color: var(--text);
  font-weight: 400;
  margin: 8px 0;
  cursor: pointer;
}

label.inline input {
  width: 17px;
  margin-top: 3px;
}

/* ---------- 7. 佈局工具與卡片系統 ---------- */
.row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(276px, 1fr));
  gap: 20px;
  margin-top: 18px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 20px 22px;
  box-shadow: var(--shadow-sm);
  transition: transform 0.22s ease, box-shadow 0.22s ease, border-color 0.22s ease;
}

.card h3 {
  margin-top: 0;
  margin-bottom: 6px;
}

/* 故事列表與角色卡頁的卡片進階質感 */
.grid .card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.grid .card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--grad-ribbon);
  opacity: 0.45;
  transition: opacity 0.22s ease;
}

.grid .card:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow-md);
}

.grid .card:hover::before {
  opacity: 0.95;
}

@media (prefers-reduced-motion: no-preference) {
  .grid .card:hover {
    transform: translateY(-3px);
  }
}

.grid .card > .row:last-child {
  margin-top: auto;
  padding-top: 10px;
}

.muted {
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.62;
}

/* 提示與錯誤區塊（非僅靠色彩，加入左邊條） */
.notice {
  background: var(--notice-bg);
  border: 1px solid var(--notice-line);
  border-left: 3.5px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 14px;
  line-height: 1.68;
  color: var(--notice-text);
  margin: 12px 0;
}

.error {
  background: var(--error-bg);
  border: 1px solid var(--error-line);
  border-left: 3.5px solid var(--danger);
  color: var(--error-text);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 14px;
  line-height: 1.65;
  margin: 12px 0;
  white-space: pre-wrap;
}

.hidden {
  display: none !important;
}

/* ---------- 8. 角色頭像與定型圖展示 ---------- */
.char-head {
  display: flex;
  gap: 14px;
  align-items: center;
  margin-bottom: 6px;
}

.avatar {
  width: 68px;
  height: 88px;
  border-radius: 13px;
  object-fit: cover;
  object-position: 50% 18%;
  background: linear-gradient(155deg, var(--panel2) 0%, var(--accent-soft) 100%);
  border: 1.5px solid var(--line);
  box-shadow: var(--shadow-sm);
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--accent);
  font-size: 24px;
  transition: transform 0.25s ease, border-color 0.2s ease;
}

.grid .card:hover .avatar {
  border-color: var(--accent);
}

@media (prefers-reduced-motion: no-preference) {
  .grid .card:hover img.avatar {
    transform: scale(1.03);
  }
}

.preview {
  width: 160px;
  height: 214px;
  object-fit: cover;
  object-position: 50% 18%;
  border-radius: 14px;
  border: 1.5px solid var(--line);
  box-shadow: var(--shadow-md);
  margin-top: 10px;
  display: block;
}

/* ---------- 9. 故事閱讀頁與側欄 ---------- */
.story-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 28px;
  align-items: start;
}

/* 故事標題輸入欄（文庫書名感） */
#title {
  font-family: var(--font-display);
  font-size: clamp(18px, 3.2vw, 22px) !important;
  font-weight: 700 !important;
  letter-spacing: 0.04em;
  background: var(--panel);
  border-color: var(--line);
  padding: 10px 16px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

#title:focus {
  border-color: var(--accent);
}

/* 小說場景正文（長篇舒適閱讀核心） */
.scene {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 28px 36px;
  margin: 16px 0 20px;
  font-family: var(--font-serif);
  font-size: 17.5px;
  line-height: 2.05;
  letter-spacing: 0.04em;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: var(--shadow-sm);
}

.scene::before {
  content: "";
  position: absolute;
  top: 0;
  left: 28px;
  right: 28px;
  height: 2px;
  border-radius: 0 0 2px 2px;
  background: var(--grad-ribbon);
  opacity: 0.32;
}

.scene .t {
  text-wrap: pretty;
}

/* 場景配圖（單張置中、雙張並排） */
.scene .imgs {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin: 22px 0 4px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.scene .imgs img {
  max-width: 100%;
  width: calc(50% - 7px);
  height: auto;
  border-radius: 12px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow-md);
  display: block;
  transition: transform 0.28s ease, box-shadow 0.28s ease;
}

.scene .imgs img:only-child {
  width: min(100%, 480px);
  margin: 0 auto;
}

@media (prefers-reduced-motion: no-preference) {
  .scene .imgs img:hover {
    transform: scale(1.015);
    box-shadow: var(--shadow-lg);
  }
}

/* 使用者劇情指示（戀愛視覺小說便箋條） */
.dir {
  position: relative;
  border-left: 3.5px solid var(--accent);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--grad-dir);
  padding: 12px 18px;
  margin: 22px 0 10px;
  font-family: var(--font-display);
  font-size: 15.5px;
  line-height: 1.78;
  letter-spacing: 0.03em;
  color: var(--text);
  white-space: pre-wrap;
}

.dir::before {
  content: "劇情指示" / "";
  display: block;
  font-family: var(--font-ui);
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--accent);
  margin-bottom: 4px;
}

/* 新幕進場與串流書寫游標 */
@media (prefers-reduced-motion: no-preference) {
  .scene,
  .dir {
    animation: sf-scene-in 0.42s cubic-bezier(0.22, 1, 0.36, 1) both;
  }

  /* 串流中（#stop 顯示時）自動於最後一幕結尾呈現柔和閃爍游標 */
  .story-layout > div:first-child:has(#stop:not(.hidden)) #scenes .scene:last-child .t::after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 1.12em;
    margin-left: 4px;
    vertical-align: -0.18em;
    border-radius: 2px;
    background: var(--accent);
    animation: sf-caret-blink 1.05s ease-in-out infinite;
  }
}

@keyframes sf-scene-in {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes sf-caret-blink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.15; }
}

/* 狀態列 */
.status {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13.5px;
  min-height: 24px;
  margin-top: 8px;
}

.status:not(:empty)::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  flex: none;
}

@media (prefers-reduced-motion: no-preference) {
  .status:not(:empty)::before {
    animation: sf-pulse 1.5s ease-in-out infinite;
  }
}

@keyframes sf-pulse {
  0%, 100% { transform: scale(1); opacity: 0.9; }
  50%      { transform: scale(1.35); opacity: 0.35; }
}

/* 側欄卡片（刻意保持安靜沉穩，不搶正文視覺） */
.side .card {
  padding: 16px 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-sm);
}

.side .card h3 {
  font-size: 15px;
  padding-bottom: 8px;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.charpick {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 7px 9px;
  margin: 4px 0;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color 0.18s ease, border-color 0.18s ease;
}

.charpick:hover {
  background: var(--accent-softer);
}

.charpick:has(input:checked) {
  background: var(--accent-soft);
  border-color: var(--notice-line);
}

.charpick .avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 14px;
  box-shadow: none;
}

/* ---------- 10. 對話框（18+ 確認視窗與角色卡編輯） ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: auto;
  background: var(--modal-backdrop);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.modal-box {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 28px 30px;
  max-width: 640px;
  width: 100%;
  max-height: 92vh;
  overflow: auto;
  box-shadow: var(--shadow-lg);
}

.modal-box::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3.5px;
  background: var(--grad-ribbon);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

@media (prefers-reduced-motion: no-preference) {
  .modal-box {
    animation: sf-modal-in 0.26s cubic-bezier(0.22, 1, 0.36, 1) both;
  }
}

@keyframes sf-modal-in {
  from { opacity: 0; transform: translateY(12px) scale(0.985); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.gate-eyebrow {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.14em;
  color: var(--accent);
}

#gate .modal-box {
  max-width: 540px;
}

#gate ul {
  margin: 14px 0 22px;
  padding-left: 20px;
  line-height: 1.8;
  color: var(--text);
}

#gate li {
  margin-bottom: 8px;
}

#gate li::marker {
  color: var(--accent);
}

/* ---------- 11. Toast 通知 ---------- */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 99;
  display: none;
  padding: 10px 20px;
  border-radius: var(--radius-pill);
  background: var(--toast-bg);
  color: var(--toast-text);
  border: 1px solid var(--line);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  max-width: calc(100vw - 32px);
  text-align: center;
}

@media (prefers-reduced-motion: no-preference) {
  #toast {
    animation: sf-toast-in 0.24s cubic-bezier(0.22, 1, 0.36, 1) both;
  }
}

@keyframes sf-toast-in {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------- 12. 手機版側欄抽屜按鈕（桌機隱藏） ---------- */
.side-drawer-btn,
.side-backdrop {
  display: none;
}

/* ---------- 13. 響應式斷點（平板 <= 920px ／ 手機 <= 640px） ---------- */
@media (max-width: 920px) {
  .story-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 20px;
  }

  .story-layout > div:first-child {
    min-width: 0;
  }

  main {
    padding: 20px 18px 48px;
  }

  .scene {
    padding: 22px 24px;
  }

  /* 若有掛載 7.6 抽屜腳本，側欄於平板與手機收合為滑入抽屜 */
  .story-layout:has(.side-drawer-btn) .side {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 45;
    width: min(88vw, 360px);
    padding: 20px 16px 88px;
    background: var(--bg);
    border-left: 1px solid var(--line);
    box-shadow: var(--shadow-lg);
    overflow-y: auto;
    overscroll-behavior: contain;
    transform: translateX(100%);
    transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
  }

  .story-layout:has(.side-drawer-btn) .side.is-open {
    transform: translateX(0);
  }

  .side-backdrop.is-open {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 40;
    background: var(--modal-backdrop);
    backdrop-filter: blur(4px);
  }

  .side-drawer-btn {
    display: inline-flex;
    align-items: center;
    flex: none;
    min-height: 44px;
    padding: 9px 14px;
    border-radius: var(--radius-pill);
    background: var(--panel);
    color: var(--accent);
    border: 1.5px solid var(--accent);
    font-weight: 600;
    font-size: 14px;
  }
}

@media (max-width: 640px) {
  header {
    padding: 8px 14px;
    gap: 10px;
    flex-wrap: wrap;
  }

  .brand {
    font-size: 20px;
    order: 1;
  }

  .header-tools {
    order: 2;
  }

  nav {
    gap: 2px;
    order: 3;
    flex: 1 0 100%;
  }

  nav a {
    min-height: 44px;
    padding: 6px 11px;
    font-size: 14px;
  }

  main {
    padding: 16px 14px 40px;
  }

  .grid {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .card {
    padding: 16px;
  }

  .scene {
    padding: 18px 18px;
    font-size: 16.5px;
    line-height: 1.95;
    letter-spacing: 0.03em;
  }

  .scene .imgs img,
  .scene .imgs img:only-child {
    width: 100%;
  }

  .modal {
    padding: 12px;
    align-items: flex-end;
  }

  .modal-box {
    padding: 22px 18px;
    border-radius: var(--radius-lg) var(--radius-lg) 12px 12px;
    max-height: 90dvh;
  }

  /* 手機版固定底部「繼續」操作列（零改 app.js） */
  .story-layout > div:first-child > .row:has(#go) {
    position: sticky;
    bottom: 0;
    z-index: 15;
    margin: 12px -14px 0;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    background: var(--header-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid var(--line);
  }

  .story-layout > div:first-child > .row:has(#go) {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .story-layout > div:first-child > .row:has(#go) > button {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  #go,
  #stop {
    flex: 1 0 110px !important;
  }

  /* 避免 iOS Safari 聚焦自動放大 + 達標 44px 觸控尺寸 */
  input, textarea, select {
    font-size: 16px;
  }

  button, .btn, .charpick {
    min-height: 44px;
  }
}

/* 觸控裝置通用目標尺寸保護（WCAG 2.5.8） */
@media (pointer: coarse) {
  button, .btn, nav a, .charpick {
    min-height: 44px;
  }
  input[type="checkbox"] {
    width: 20px;
    height: 20px;
  }
}

/* ---------- 14. 系統高對比模式（Forced Colors Mode） ---------- */
@media (forced-colors: active) {
  .card,
  .scene,
  .modal-box,
  button,
  .btn,
  input,
  textarea,
  select,
  .notice,
  .error,
  #toast {
    border: 1px solid CanvasText;
    box-shadow: none;
  }

  button.primary {
    background: Highlight;
    color: HighlightText;
    border-color: Highlight;
  }

  nav a.active {
    border-bottom: 2px solid Highlight;
  }

  :where(a, button, input, textarea, select, [tabindex]):focus-visible {
    outline: 3px solid Highlight;
    outline-offset: 2px;
  }

  .sf-ambient {
    display: none;
  }
}

/* 桌機：側欄跟著閱讀位置，改記憶／角色不用捲回頁頂 */
@media (min-width: 921px) {
  .story-layout > .side {
    position: sticky;
    top: 82px;
    max-height: calc(100vh - 102px);
    max-height: calc(100dvh - 102px);
    overflow-y: auto;
    padding-right: 2px;
  }
}
