:root {
  --bg: #E8EDEF;
  --surface: #FFFFFF;
  --ink: #16282C;
  --muted: #667A80;
  --line: #D3DCDF;
  --pine: #2C5F54;
  --pine-ink: #F4FBF8;
  --sun: #E8B84A;
  --danger: #B3372F;
  --font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F1A1D;
    --surface: #1B2A2E;
    --ink: #E6EEF0;
    --muted: #8DA0A6;
    --line: #2B3C41;
    --pine: #3A7A6B;
    --pine-ink: #F4FBF8;
    --danger: #E36A61;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-left, 0px) 0 env(safe-area-inset-right, 0px);
}

:focus-visible { outline: 3px solid var(--sun); outline-offset: 2px; }

.muted { color: var(--muted); }

/* ---------- 공통 버튼 ---------- */
.btn {
  font: inherit; font-weight: 600;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 12px; padding: 12px 20px; min-height: 48px; cursor: pointer;
}
.btn.primary { background: var(--pine); border-color: var(--pine); color: var(--pine-ink); }
.btn:disabled { opacity: .55; cursor: default; }

.link-btn {
  font: inherit; font-size: 14px; color: var(--muted);
  background: none; border: 0; padding: 8px; cursor: pointer; text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---------- 로딩 / 로그인 / 미등록 ---------- */
.center-view {
  min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 24px; text-align: center;
}
.wordmark { font-size: 56px; line-height: 1; font-weight: 800; letter-spacing: -0.04em; margin: 0 0 4px; color: var(--pine); }
@media (prefers-color-scheme: dark) { .wordmark { color: #7FC4B2; } }

.login-form { width: min(100%, 340px); display: flex; flex-direction: column; gap: 8px; text-align: left; margin-top: 12px; }
.login-form label { font-size: 14px; font-weight: 600; margin-top: 6px; }
.login-form input {
  font: inherit; font-size: 16px; /* 16px 미만이면 iOS가 확대함 */
  width: 100%; min-height: 48px; padding: 10px 14px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink);
}
.error { color: var(--danger); font-size: 14px; min-height: 1.4em; margin: 4px 0; }
.uid { display: block; max-width: 100%; word-break: break-all; padding: 10px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; font-size: 14px; user-select: all; }

/* ---------- 채팅 레이아웃 ---------- */
.chat { height: 100%; display: flex; flex-direction: column; }

.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.chat-header h1 { margin: 0; font-size: 22px; font-weight: 800; letter-spacing: -0.03em; color: var(--pine); }
@media (prefers-color-scheme: dark) { .chat-header h1 { color: #7FC4B2; } }
.who-am-i { display: flex; align-items: center; gap: 4px; font-size: 14px; color: var(--muted); }

.messages {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 12px 12px 8px; display: flex; flex-direction: column;
}

.empty { margin: auto; text-align: center; color: var(--muted); padding: 24px; }
.more { align-self: center; margin-bottom: 8px; }

.day {
  align-self: center; margin: 14px 0 8px; padding: 3px 12px;
  font-size: 13px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
}

/* ---------- 메시지 ---------- */
.msg { display: flex; flex-direction: column; max-width: 82%; margin-top: 10px; }
.msg.grouped { margin-top: 3px; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }

.who { font-size: 13px; font-weight: 700; color: var(--muted); margin: 0 4px 3px; }
.msg.mine .who, .msg.grouped .who { display: none; }

.row { display: flex; align-items: flex-end; gap: 6px; }
.msg.mine .row { flex-direction: row-reverse; }

.bubble {
  background: var(--surface); color: var(--ink);
  border-radius: 18px 18px 18px 6px; padding: 9px 13px; overflow: hidden;
  word-break: break-word; overflow-wrap: anywhere;
}
.msg.mine .bubble { background: var(--pine); color: var(--pine-ink); border-radius: 18px 18px 6px 18px; }
.msg.grouped .bubble { border-radius: 18px; }
.bubble p { margin: 0; white-space: pre-wrap; }
.bubble img { display: block; max-width: 100%; max-height: 320px; border-radius: 12px; cursor: zoom-in; }
.bubble.has-image { padding: 4px; }
.bubble.has-image p { padding: 6px 9px 4px; }

time { font-size: 12px; color: var(--muted); white-space: nowrap; flex: none; }

.msg.pending { opacity: .6; }

.actions { display: none; margin-top: 2px; }
.msg.open .actions { display: block; }
.del { font: inherit; font-size: 14px; color: var(--danger); background: none; border: 0; padding: 6px 4px; cursor: pointer; }

/* ---------- 입력창 ---------- */
.preview {
  display: flex; align-items: center; gap: 12px; padding: 8px 14px;
  background: var(--surface); border-top: 1px solid var(--line);
}
.preview img { height: 56px; width: 56px; object-fit: cover; border-radius: 10px; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  background: var(--surface); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; min-width: 0; resize: none; max-height: 120px;
  font: inherit; font-size: 16px; line-height: 1.4;
  padding: 10px 14px; border: 1px solid var(--line); border-radius: 20px;
  background: var(--bg); color: var(--ink);
}
.icon-btn {
  flex: none; width: 44px; height: 44px; display: grid; place-items: center;
  background: none; border: 0; border-radius: 50%; color: var(--muted); cursor: pointer;
}
.send-btn {
  flex: none; height: 44px; padding: 0 16px; font: inherit; font-weight: 700;
  border: 0; border-radius: 22px; background: var(--pine); color: var(--pine-ink); cursor: pointer;
}
.send-btn:disabled { background: var(--line); color: var(--muted); cursor: default; }

/* ---------- 확대 보기 / 토스트 ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.88);
  display: grid; place-items: center; padding: 16px; cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  z-index: 11; max-width: 90%; padding: 10px 16px; border-radius: 12px;
  background: var(--ink); color: var(--bg); font-size: 14px; text-align: center;
}

@media (min-width: 720px) {
  .chat { max-width: 640px; margin: 0 auto; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
}

@media (prefers-reduced-motion: no-preference) {
  .msg { animation: pop .14s ease-out; }
  @keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
}
