:root {
  --bg: #f6f3f1;
  --surface: #ffffff;
  --text: #2a2522;
  --muted: #8a817b;
  --line: #e7e0db;
  --accent: #ff7a90;
  --accent-2: #ffb3c0;
  --me: #ffe3e8;
  --bot: #ffffff;
  --pos: #4fb38a;
  --neg: #e2656b;
  --neu: #c9a227;
  --track: #ece6e2;
  --shadow: 0 2px 12px rgba(0, 0, 0, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1817; --surface: #252120; --text: #f1ece9; --muted: #a39a94; --line: #3a3431;
    --me: #5a2f38; --bot: #2e2927; --track: #3a3431; --shadow: none;
  }
}
* { box-sizing: border-box; }
/* 페이지 자체는 스크롤하지 않고 메시지 목록만 스크롤 (iOS에서 상단바가 밀려 올라가는 문제 방지) */
html, body { height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body {
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", sans-serif;
  font-size: 15px;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button {
  border: 0; border-radius: 10px; padding: 10px 16px; background: var(--accent); color: #fff;
  font-weight: 600; cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
button.danger { background: var(--neg); width: 100%; }
/* iOS Safari는 16px 미만 입력창을 누르면 화면을 확대해 버리므로 16px 유지 */
input, select, textarea {
  font-size: 16px;
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
}
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--neg); min-height: 1em; margin: 0; font-size: 13px; }

/* --app-h: 키보드가 올라온 뒤 실제로 보이는 높이 (app.js에서 visualViewport로 갱신) */
.screen { height: var(--app-h, 100dvh); }
.center { overflow-y: auto; }
.center { display: flex; align-items: center; justify-content: center; padding: 16px; }
.card {
  width: 100%; max-width: 360px; background: var(--surface); border-radius: 16px; padding: 24px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px;
}
.card h1 { font-size: 20px; margin: 0; }
.card label, .label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.radio-row { display: flex; gap: 16px; }
.radio-row label { flex-direction: row; align-items: center; gap: 6px; color: var(--text); font-size: 15px; }
.radio-row input { width: auto; }

/* chat screen */
#chat-screen { position: fixed; top: 0; left: 0; right: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.who { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: #fff; display: grid; place-items: center; font-size: 12px; font-weight: 700; flex: none;
}
.who > div:last-child { min-width: 0; }
.name { font-weight: 700; white-space: nowrap; }
.sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-badge {
  background: var(--track); color: var(--text); font-size: 12px; font-weight: 600; padding: 5px 10px;
  border-radius: 999px; white-space: nowrap; max-width: 40vw; overflow: hidden; text-overflow: ellipsis;
}
.totals { font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; }
.totals b { color: var(--text); }
.actions { display: flex; gap: 6px; }
.icon-btn { background: transparent; color: var(--text); padding: 6px 10px; font-size: 18px; }
#toggle-panel { display: none; }

.layout { position: relative; flex: 1; display: flex; min-height: 0; overflow: hidden; }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.messages { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { display: flex; flex-direction: column; max-width: min(78%, 560px); }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.bubble {
  padding: 10px 14px; border-radius: 16px; white-space: pre-wrap; word-break: break-word; line-height: 1.5;
  box-shadow: var(--shadow);
}
.msg.user .bubble { background: var(--me); border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--bot); border-bottom-left-radius: 4px; }
.msg.proactive .bubble { border-left: 3px solid var(--accent-2); }
.time { font-size: 11px; color: var(--muted); margin-top: 3px; }
.meta { font-size: 11px; color: var(--muted); margin-top: 4px; }
.deltas { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chip { font-size: 11px; padding: 2px 7px; border-radius: 999px; background: var(--track); }
.chip.up { color: var(--pos); }
.chip.down { color: var(--neg); }
.chip.romance { background: var(--me); }

.photo { margin-top: 6px; }
.photo img {
  display: block; width: min(260px, 70vw); aspect-ratio: 2 / 3; object-fit: cover; border-radius: 14px;
  cursor: zoom-in; box-shadow: var(--shadow); background: var(--track);
}
.photo-pending {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--muted);
  padding: 10px 12px; border-radius: 12px; background: var(--track);
}
.photo-pending.error { color: var(--neg); }
.small-btn { padding: 4px 10px; font-size: 12px; }
.spinner {
  width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--muted); border-top-color: transparent;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
label.check input { width: auto; }

.typing { padding: 0 16px 8px; color: var(--muted); font-size: 13px; }
.typing span { animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 100% { opacity: .2 } 50% { opacity: 1 } }

.composer {
  display: flex; gap: 8px; padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--surface);
}
.composer textarea { resize: none; max-height: 140px; }
.composer button { flex: none; white-space: nowrap; }

.banner {
  margin: 12px 16px 0; padding: 10px 14px; border-radius: 12px; background: var(--me); font-size: 13px;
}

/* emotion panel */
.panel {
  width: 300px; flex: none; overflow-y: auto; padding: 16px; background: var(--surface); border-left: 1px solid var(--line);
}
.panel h2 { font-size: 13px; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.panel h2:not(:first-child) { margin-top: 20px; }
.emotion-list { display: flex; flex-direction: column; gap: 9px; }
.emo { display: grid; grid-template-columns: 62px 1fr 30px; align-items: center; gap: 8px; font-size: 13px; border-radius: 6px; }
.emo .val { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.emo.flash-up { animation: flashUp 1.4s; }
.emo.flash-down { animation: flashDown 1.4s; }
@keyframes flashUp { 0% { background: rgba(79, 179, 138, .3) } 100% { background: transparent } }
@keyframes flashDown { 0% { background: rgba(226, 101, 107, .3) } 100% { background: transparent } }

/* slider-like meter */
.meter { position: relative; height: 6px; border-radius: 999px; background: var(--track); }
.meter .fill {
  position: absolute; inset: 0 auto 0 0; width: 0; border-radius: 999px; background: var(--c, var(--accent));
  transition: width var(--dur, .6s) ease-out;
}
.meter .thumb {
  position: absolute; top: 50%; left: 0; width: 14px; height: 14px; margin: -7px 0 0 -7px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--c, var(--accent)); transition: left var(--dur, .6s) ease-out;
}
.meter i { position: absolute; top: -3px; bottom: -3px; width: 1px; background: var(--muted); opacity: .4; }
.romance-meter { height: 10px; --c: var(--accent); }
.romance-meter .thumb { width: 18px; height: 18px; margin: -9px 0 0 -9px; }
.romance-info { display: flex; justify-content: space-between; margin-top: 10px; font-size: 13px; }
.romance-info b { font-size: 18px; color: var(--accent); }

dialog {
  border: 0; border-radius: 16px; padding: 20px; width: min(420px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; font-size: 18px; }
dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
hr { border: 0; border-top: 1px solid var(--line); width: 100%; margin: 4px 0; }

@media (max-width: 760px) {
  #toggle-panel { display: block; }
  .topbar {
    gap: 8px;
    padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
  }
  .composer { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .messages { padding: 12px; }
  .avatar { width: 34px; height: 34px; font-size: 11px; }
  .model-badge { max-width: 28vw; padding: 5px 8px; font-size: 11px; }
  .icon-btn { padding: 6px; }
  .totals { font-size: 11px; }
  .panel {
    position: absolute; top: 0; right: 0; bottom: 0; width: min(320px, 88vw); z-index: 10;
    transform: translateX(100%); transition: transform .25s; box-shadow: -4px 0 16px rgba(0, 0, 0, .15);
  }
  .panel.open { transform: none; }
  .msg { max-width: 86%; }
}
