:root {
  --bg: #0f0b16;
  --panel: #1b1426;
  --panel-2: #251c33;
  --border: #33284a;
  --text: #f4eefc;
  --muted: #a99bbd;
  --act: #c9b5ee;
  --accent: #e0457b;
  --accent-2: #9b5cf6;
  --good: #34d399;
  --bad: #f87171;
  --radius: 18px;
  --c: #8b5cf6; /* character colour, set per character */
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "PingFang HK", "Noto Sans TC", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: var(--panel-2); border-radius: 12px; padding: 10px 14px; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; font-weight: 600; }
button.danger { background: transparent; color: var(--bad); border: 1px solid #5b2a3a; }
button.icon { background: transparent; font-size: 26px; line-height: 1; padding: 6px 10px; color: var(--muted); }
button.link { background: none; padding: 0; color: var(--act); text-decoration: underline; }
a { color: var(--act); }

#app { height: 100dvh; max-width: 760px; margin: 0 auto; }
.screen { height: 100%; display: flex; flex-direction: column; }
.screen[hidden] { display: none; }

/* ---------- Home ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 8px;
}
.topbar h1 { margin: 0; font-size: 26px; letter-spacing: -.02em; }
.topbar h1 span { display: block; font-size: 13px; font-weight: 500; color: var(--muted); letter-spacing: 0; }
.banner {
  margin: 8px 16px; padding: 14px 16px; border-radius: var(--radius);
  background: linear-gradient(135deg, #3b1d3a, #241b45); border: 1px solid var(--border);
  display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap;
}
.banner p { margin: 0; }
.banner[hidden] { display: none; }
.grid {
  flex: 1; overflow-y: auto; padding: 8px 16px calc(env(safe-area-inset-bottom) + 24px);
  display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); grid-auto-rows: 250px; gap: 12px; align-content: start;
}
.card {
  position: relative; height: 100%; width: 100%; padding: 0; overflow: hidden; text-align: left;
  border-radius: var(--radius); border: 1px solid var(--border);
  background: radial-gradient(circle at 30% 20%, var(--c), #1a1226 75%);
  transition: transform .15s;
}
.card:active { transform: scale(.97); }
.card img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.card .initial {
  position: absolute; inset: 0 0 35% 0; display: grid; place-items: center;
  font-size: 72px; font-weight: 700; color: rgba(255,255,255,.85);
}
.card-info {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 40px 12px 12px; display: flex; flex-direction: column; gap: 2px;
  background: linear-gradient(transparent, rgba(10, 6, 16, .92) 45%);
}
.card-info strong { font-size: 16px; line-height: 1.2; }
.card .genre { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--act); font-weight: 600; }
.card .tag { font-size: 12.5px; line-height: 1.35; color: #d6cbe6; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.mini-bar { height: 4px; border-radius: 4px; background: rgba(255,255,255,.15); margin-top: 6px; overflow: hidden; }
.mini-bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #ff8fb1); }
.mini-label { font-size: 11px; color: #ffb3c9; }
.card.add { background: var(--panel); border-style: dashed; }
.card.add .plus { position: absolute; inset: 0 0 30% 0; display: grid; place-items: center; font-size: 64px; color: var(--muted); }
.card.add .card-info { background: none; }

/* ---------- Chat ---------- */
.chat-top {
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px;
  border-bottom: 1px solid var(--border); background: rgba(15, 11, 22, .9);
}
.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex: none; overflow: hidden;
  display: grid; place-items: center; font-weight: 700; font-size: 18px;
  background: var(--c); border: 2px solid color-mix(in srgb, var(--c) 60%, #fff 40%);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.loading { animation: glow 1.4s ease-in-out infinite; }
.who { flex: 1; min-width: 0; }
#chat-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-mood { font-size: 13px; color: var(--muted); }

.affection {
  display: flex; align-items: center; gap: 8px; padding: 8px 16px;
  font-size: 13px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.affection .heart { color: var(--accent); font-size: 18px; }
.affection .bar { flex: 1; height: 8px; border-radius: 8px; background: #2c2240; overflow: hidden; }
#aff-fill { height: 100%; width: 0; border-radius: 8px; background: linear-gradient(90deg, var(--accent-2), var(--accent), #ff8fb1); transition: width .8s cubic-bezier(.2,.8,.2,1); }
#aff-label { font-weight: 700; color: #ffb3c9; }
#aff-num { color: var(--muted); font-variant-numeric: tabular-nums; }
.affection.pulse-up .heart { animation: beat .9s ease 2; }
.affection.pulse-down .heart { animation: shake .5s ease 1; color: var(--muted); }

#messages { flex: 1; overflow-y: auto; padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }
.msg { display: flex; flex-direction: column; max-width: 88%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.char { align-self: flex-start; }
.msg.system { align-self: center; max-width: 92%; text-align: center; font-size: 13.5px; color: #fca5a5; background: #2a1520; padding: 8px 12px; border-radius: 12px; display: block; }
.bubble { padding: 10px 14px; border-radius: var(--radius); overflow-wrap: anywhere; }
.bubble p { margin: 0 0 .6em; }
.bubble p:last-child { margin-bottom: 0; }
.msg.char .bubble { background: var(--panel); border: 1px solid var(--border); border-top-left-radius: 6px; }
.msg.user .bubble { background: linear-gradient(135deg, #b8336a, #7a3fd0); border-top-right-radius: 6px; }
em.act { color: var(--act); }
.msg.user em.act { color: #f3e6ff; }

.pic { margin-bottom: 8px; border-radius: var(--radius); overflow: hidden; width: min(340px, 78vw); aspect-ratio: 3 / 4; background: var(--panel-2); }
.pic img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.pic.loading { display: grid; place-items: center; color: var(--muted); font-size: 14px;
  background: linear-gradient(110deg, #1d1529 30%, #2d2241 50%, #1d1529 70%); background-size: 250% 100%; animation: shimmer 1.6s linear infinite; }

.tools { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; align-items: center; }
.tools button { font-size: 12.5px; padding: 4px 10px; border-radius: 999px; background: transparent; border: 1px solid var(--border); color: var(--muted); }
.delta { font-size: 12.5px; font-weight: 700; padding: 3px 10px; border-radius: 999px; }
.delta.up { color: #ff9cbc; background: #3a1730; }
.delta.down { color: #a5b4fc; background: #1d2140; }
.delta .why { font-weight: 400; opacity: .85; }

.typing { display: flex; gap: 5px; padding: 14px 16px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }

#suggestions { display: flex; gap: 8px; overflow-x: auto; padding: 0 14px 8px; scrollbar-width: none; }
#suggestions:empty { display: none; }
#suggestions button { flex: none; max-width: 80%; font-size: 13.5px; padding: 8px 12px; border-radius: 14px; text-align: left;
  background: var(--panel); border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--border)); white-space: normal; }

#composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px 12px calc(env(safe-area-inset-bottom) + 10px); border-top: 1px solid var(--border); background: var(--bg); }
#input { flex: 1; resize: none; max-height: 140px; padding: 11px 14px; border-radius: 20px; border: 1px solid var(--border); background: var(--panel); outline: none; font-size: 16px; }
#input:focus { border-color: var(--accent-2); }
#send { width: 46px; height: 46px; border-radius: 50%; padding: 0; font-size: 18px; flex: none; }

/* ---------- Pop-ups ---------- */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 40px); padding: 0;
  border: 1px solid var(--border); border-radius: 22px; background: var(--panel); color: var(--text);
}
dialog::backdrop { background: rgba(5, 2, 10, .7); backdrop-filter: blur(3px); }
#modal-form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0 0 4px; font-size: 20px; }
dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
dialog input, dialog textarea, dialog select {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); font-size: 16px; color: var(--text);
}
dialog input[type="color"] { height: 44px; padding: 4px; }
dialog textarea { resize: vertical; }
dialog details { border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
dialog details[open] > * + * { margin-top: 10px; }
dialog summary { cursor: pointer; color: var(--muted); font-size: 14px; }
.hint { margin: -4px 0 0; font-size: 13px; color: var(--muted); }
.note { margin: 0; padding: 10px 12px; border-radius: 12px; background: #3a1726; color: #fecdd3; font-size: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row .hint { margin: 0; }
.actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 6px; flex-wrap: wrap; }
.actions .spacer { flex: 1; }
.menu { display: flex; flex-direction: column; gap: 8px; }
.menu button { text-align: left; padding: 12px 14px; }
dialog.small { width: min(380px, calc(100vw - 24px)); }
dialog.image { background: transparent; border: 0; width: auto; max-width: 96vw; }
dialog.image #modal-form { padding: 0; }
.zoomed { max-width: 96vw; max-height: 90dvh; border-radius: 16px; display: block; cursor: zoom-out; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; left: 50%; transform: translateX(-50%); top: calc(env(safe-area-inset-top) + 108px); z-index: 50;
  display: flex; flex-direction: column; gap: 8px; width: min(440px, calc(100vw - 24px)); pointer-events: none; }
.toast { padding: 10px 14px; border-radius: 14px; background: #2a2038; border: 1px solid var(--border); box-shadow: 0 8px 30px rgba(0,0,0,.4);
  font-size: 14px; transition: opacity .5s, transform .5s; animation: drop .3s ease; }
.toast.good { border-color: #6b2b4c; background: #3a1730; }
.toast.bad { border-color: #2d3366; background: #1d2140; }
.toast.error { border-color: #7f1d1d; background: #3b1219; color: #fecaca; }
.toast.hide { opacity: 0; transform: translateY(-8px); }

@keyframes shimmer { to { background-position: -250% 0; } }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }
@keyframes beat { 0%, 100% { transform: scale(1); } 30% { transform: scale(1.45); } 60% { transform: scale(.95); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
@keyframes glow { 50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--c) 40%, transparent); } }
@keyframes drop { from { opacity: 0; transform: translateY(-10px); } }

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

/* ---------- Gallery, motion effect and viewer ---------- */
button.icon.small { font-size: 20px; }
#gallery-count { font-size: 13px; color: var(--muted); }
#gallery-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#gallery-body { flex: 1; overflow-y: auto; padding: 8px 16px calc(env(safe-area-inset-bottom) + 24px); }
#gallery-body h3 { margin: 18px 0 10px; font-size: 15px; color: var(--act); letter-spacing: .02em; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.tile { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.tile .pic { width: 100%; margin: 0; cursor: pointer; position: relative; }
.tile-title { font-size: 13.5px; font-weight: 600; }
.tile-sub { font-size: 12px; color: var(--muted); margin-top: -4px; }
.tile.locked .pic { cursor: default; display: grid; place-items: center; background: #160f20; }
.tile.locked img.blur { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; filter: blur(14px) grayscale(.6) brightness(.45); transform: scale(1.15); animation: none; }
.tile.locked .lock { position: relative; font-size: 32px; }
.tile.locked .tile-title { color: var(--muted); }
.pic.empty { display: grid; place-items: center; text-align: center; color: var(--muted); font-size: 13px; border: 1px dashed var(--border); background: var(--panel); border-radius: var(--radius); }

.pic { position: relative; }
.pic video { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.pic.has-video::before { content: '▶︎ ' ; position: absolute; z-index: 2; top: 8px; left: 8px; font-size: 11px; padding: 2px 7px; border-radius: 999px; background: rgba(0,0,0,.55); color: #fff; }

/* Free "live picture" effect: slow zoom and drift, plus a soft light sweep */
.pic.live:not(.has-video) img { animation: drift 18s ease-in-out infinite alternate; transform-origin: 50% 35%; }
.pic.live:not(.has-video)::after {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(115deg, transparent 35%, rgba(255, 240, 255, .14) 50%, transparent 65%);
  background-size: 300% 100%; animation: sweep 9s ease-in-out infinite; mix-blend-mode: screen;
}
@keyframes drift {
  0% { transform: scale(1.02) translate(0, 0); }
  50% { transform: scale(1.07) translate(-1.2%, -1%); }
  100% { transform: scale(1.04) translate(1%, .6%); }
}
@keyframes sweep { 0%, 55% { background-position: 120% 0; } 100% { background-position: -120% 0; } }

.tools .status { font-size: 12.5px; color: var(--act); padding: 4px 4px; animation: glowtext 1.6s ease-in-out infinite; }
@keyframes glowtext { 50% { opacity: .45; } }

dialog.viewer { width: min(560px, calc(100vw - 16px)); }
.viewer-media img, .viewer-media video { width: 100%; max-height: 70dvh; object-fit: contain; display: block; border-radius: 14px; background: #000; }
.caption { margin: 0; font-size: 14px; color: var(--muted); }
a.btn { display: inline-block; text-decoration: none; color: var(--text); background: var(--panel-2); border-radius: 12px; padding: 10px 14px; }
.banner p { flex: 1; min-width: 200px; }
.moment-badge { align-self: flex-start; margin-bottom: 6px; font-size: 12.5px; font-weight: 700; color: #ffd27a; background: #3a2a12; border: 1px solid #6b4a1a; padding: 3px 10px; border-radius: 999px; }
