/* ========== 기본 ========== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:root {
  /* JS가 동적으로 갱신 — visualViewport.height/offsetTop 기준 */
  --app-height: 100vh;
  --vv-top: 0px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  --bg: #0f1115;
  --bg-elev: #161922;
  --bg-side: #0b0d12;
  --border: #1f2229;
  --text: #e6e6e6;
  --muted: #888;
  --accent: #4f8cff;
  --user-bubble: #1a2233;
}

html, body {
  margin: 0; padding: 0;
  width: 100%; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo",
    "Malgun Gothic", "Noto Sans KR", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}
body {
  overscroll-behavior: none;
  /* iOS에서 바운스/스크롤로 화면이 밀리는 것 방지 */
  position: fixed;
  inset: 0;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ========== 폼 컨트롤 ========== */
button {
  cursor: pointer; border: none; border-radius: 10px;
  padding: 12px 16px; font-size: 15px;
  font-family: inherit; touch-action: manipulation;
  color: inherit;
}
button.primary { background: var(--accent); color: #fff; font-weight: 500; }
button.primary:active { background: #3b78ec; transform: scale(0.97); }
button.ghost {
  background: transparent; color: var(--muted);
  border: 1px solid #333; padding: 8px 12px; font-size: 13px;
}
.icon-btn {
  background: transparent; color: #ddd;
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  padding: 0; border-radius: 10px;
}
.icon-btn:active { background: var(--bg-elev); }

input, textarea {
  background: var(--bg-elev); color: var(--text);
  border: 1px solid #2a2f38;
  border-radius: 10px; padding: 12px 14px;
  /* iOS 자동 확대 방지: 16px 이상 필수 */
  font-size: 16px; font-family: inherit;
  width: 100%;
  -webkit-appearance: none; appearance: none;
}
input:focus, textarea:focus { outline: none; border-color: var(--accent); }

/* ========== 인증 ========== */
#auth {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  padding-top: max(20px, var(--safe-top));
  padding-bottom: max(20px, var(--safe-bottom));
  overflow-y: auto;
}
.auth-card {
  background: var(--bg-elev); border: 1px solid #2a2f38;
  border-radius: 16px; padding: 28px 22px;
  width: 100%; max-width: 380px;
  display: flex; flex-direction: column; gap: 12px;
}
.auth-card h1 { margin: 0 0 8px; text-align: center; font-size: 22px; }
.tabs { display: flex; gap: 8px; }
.tab {
  flex: 1; background: var(--bg-elev); color: #aaa;
  padding: 10px; border-radius: 10px; font-size: 14px;
  border: 1px solid #2a2f38;
}
.tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.error { color: #ff6b6b; font-size: 13px; min-height: 18px; text-align: center; }

.divider {
  display: flex; align-items: center; gap: 10px;
  margin: 4px 0; color: var(--muted); font-size: 12px;
}
.divider::before, .divider::after {
  content: ""; flex: 1; height: 1px; background: #2a2f38;
}
.google-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: #fff; color: #333;
  padding: 11px 14px; border-radius: 10px;
  text-decoration: none; font-weight: 500; font-size: 14px;
}
.google-btn:active { opacity: 0.85; }

.footer-btns { display: flex; gap: 6px; }

/* 관리자 모달 */
.admin-modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.admin-card {
  background: var(--bg-elev); border: 1px solid #2a2f38;
  border-radius: 12px; width: 100%; max-width: 900px;
  max-height: 85vh; display: flex; flex-direction: column;
}
.admin-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.admin-head h2 { margin: 0; font-size: 16px; }
.admin-tabs { display: flex; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.admin-tab {
  background: transparent; color: #aaa; border: 1px solid #333;
  padding: 6px 12px; border-radius: 8px; font-size: 13px;
}
.admin-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.admin-pane {
  flex: 1; overflow: auto;
  background: var(--bg-side); margin: 0; padding: 12px;
  font-family: ui-monospace, monospace; font-size: 12px;
  white-space: pre-wrap; word-break: break-all;
  color: #ccc;
}
#admin-users { font-family: inherit; }
.admin-user-row {
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  font-size: 13px; cursor: pointer;
}
.admin-user-row:hover { background: var(--bg-side); }
.admin-user-row .uname { color: #fff; font-weight: 500; }
.admin-user-row .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.admin-back {
  background: transparent; color: #4f8cff;
  padding: 6px 10px; font-size: 13px;
  border: 1px solid #2a2f38; border-radius: 8px;
  margin: 8px;
}
.admin-back:active { background: var(--bg-side); }
.admin-breadcrumb {
  padding: 8px 12px; font-size: 12px; color: var(--muted);
  border-bottom: 1px solid var(--border);
  background: var(--bg-side);
}
.admin-conv-row {
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  font-size: 13px; cursor: pointer;
}
.admin-conv-row:hover { background: var(--bg-side); }
.admin-conv-row .ctitle { color: #fff; }
.admin-conv-row .cmeta { color: var(--muted); font-size: 11px; margin-top: 2px; }
.admin-msg {
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.admin-msg .role-tag {
  display: inline-block; padding: 1px 6px;
  border-radius: 4px; font-size: 10px;
  margin-right: 6px; vertical-align: middle;
}
.admin-msg.user .role-tag { background: #1a2233; color: #4f8cff; }
.admin-msg.assistant .role-tag { background: #2a2f38; color: #aaa; }
.admin-msg .mtext {
  white-space: pre-wrap; word-break: break-word;
  margin-top: 4px; line-height: 1.5;
}
.admin-msg img, .admin-msg video {
  max-width: 240px; max-height: 200px;
  border-radius: 6px; margin-top: 6px; display: block;
}
.admin-msg .mtime {
  color: var(--muted); font-size: 10px; margin-top: 4px;
}

/* ========== 채팅 레이아웃 ========== */
#chat {
  position: absolute; left: 0; right: 0;
  top: var(--vv-top);
  height: var(--app-height);
  display: flex; flex-direction: column;
}

/* 상단바 (모바일 전용) */
.topbar {
  display: none;
  flex-shrink: 0;
  align-items: center;
  height: 48px;
  padding: 0 8px;
  padding-top: var(--safe-top);
  height: calc(48px + var(--safe-top));
  background: var(--bg-side);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}
.topbar-title {
  flex: 1; text-align: center;
  font-size: 15px; font-weight: 500; color: #ddd;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  margin: 0 8px;
}

/* 본체 = 사이드바 + 메인 */
.body-row {
  flex: 1; display: flex; min-height: 0; position: relative;
}

.backdrop {
  display: none;
  position: absolute; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
}
.backdrop.show { display: block; }

/* 사이드바 */
.sidebar {
  width: 280px; flex-shrink: 0;
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 12px; gap: 10px;
  padding-left: max(12px, var(--safe-left));
  padding-bottom: max(12px, var(--safe-bottom));
}
.conv-list {
  flex: 1; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
  -webkit-overflow-scrolling: touch;
}
.conv-item {
  padding: 12px 14px; border-radius: 10px;
  cursor: pointer; font-size: 14px; color: #ccc;
  display: flex; justify-content: space-between;
  align-items: center; gap: 6px;
}
.conv-item:active { background: var(--bg-elev); }
.conv-item.active { background: var(--user-bubble); color: #fff; }
.conv-item .title {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.conv-item .del {
  color: #666; font-size: 22px; padding: 4px 10px; line-height: 1;
}
.conv-item .del:active { color: #ff6b6b; }
.sidebar-footer {
  display: flex; justify-content: space-between; align-items: center;
  padding-top: 8px; border-top: 1px solid var(--border);
  font-size: 13px; color: var(--muted);
}

/* 메인 */
.main {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
}
.messages {
  flex: 1; overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column; gap: 14px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  padding-right: max(16px, var(--safe-right));
  padding-left: max(16px, var(--safe-left));
}
.msg { max-width: 800px; width: 100%; margin: 0 auto; }
.msg .role { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg .body {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px;
  line-height: 1.6; font-size: 15px;
  word-wrap: break-word; overflow-wrap: break-word;
}
.msg.user .body { background: var(--user-bubble); border-color: #243353; }
.msg .body p { margin: 0 0 0.6em; }
.msg .body p:last-child { margin: 0; }
.msg .body pre {
  background: var(--bg-side); padding: 12px; border-radius: 8px;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  font-size: 13px;
}
.msg .body code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.9em;
}
.msg .body :not(pre) > code {
  background: var(--bg-side); padding: 2px 6px; border-radius: 4px;
}
.msg .status { font-size: 12px; color: var(--accent); margin: 4px 0; }
.thinking {
  display: flex; align-items: center; gap: 8px;
  color: var(--muted); font-size: 14px;
}
.thinking .dots { display: inline-flex; gap: 3px; }
.thinking .dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted);
  animation: thinking-pulse 1.2s infinite;
}
.thinking .dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking .dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes thinking-pulse {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); }
  40% { opacity: 1; transform: scale(1); }
}
.msg .sources { font-size: 12px; color: var(--muted); margin-top: 8px; line-height: 1.8; }
.msg .sources a {
  color: var(--accent); text-decoration: none;
  margin-right: 8px; word-break: break-all;
}

/* 입력창 */
.composer {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
  display: flex; flex-direction: column; gap: 8px;
  background: var(--bg);
}
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.image-preview {
  position: relative; align-self: flex-start;
  max-width: 120px;
}
.image-preview img {
  max-width: 120px; max-height: 120px;
  border-radius: 10px; border: 1px solid var(--border);
  display: block;
}
.preview-x {
  position: absolute; top: -6px; right: -6px;
  background: #ff4757; color: #fff;
  width: 22px; height: 22px;
  border-radius: 50%; padding: 0;
  font-size: 16px; line-height: 22px;
  display: flex; align-items: center; justify-content: center;
}

/* 메시지 내부 이미지/비디오 */
.msg .body img.attached {
  max-width: 100%; max-height: 320px;
  border-radius: 8px; margin-bottom: 8px;
  display: block; cursor: pointer;
}
.msg .body video.attached {
  max-width: 100%; max-height: 360px;
  border-radius: 8px; margin-bottom: 8px;
  display: block; background: #000;
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px;
  line-height: 1.4; padding: 10px 12px;
}
.search-toggle {
  display: flex; align-items: center; gap: 4px;
  font-size: 12px; color: var(--muted);
  white-space: nowrap; cursor: pointer;
  padding: 8px 6px;
}
.search-toggle input[type="checkbox"] {
  width: 18px; height: 18px; margin: 0;
}

/* + 메뉴 */
.plus-wrap { position: relative; }
.plus-menu {
  position: absolute; bottom: 52px; left: 0;
  background: var(--bg-elev); border: 1px solid #2a2f38;
  border-radius: 12px; padding: 6px;
  min-width: 240px; z-index: 60;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  display: flex; flex-direction: column; gap: 2px;
}
.plus-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 8px;
  background: transparent; color: var(--text);
  font-size: 14px; text-align: left; width: 100%;
  border: none; cursor: pointer;
}
.plus-item:hover { background: var(--bg-side); }
.plus-row { justify-content: space-between; cursor: default; }
.plus-section {
  padding: 8px 12px;
  border-top: 1px solid #2a2f38;
  margin-top: 4px;
}
.plus-label {
  font-size: 11px; color: var(--muted);
  margin-bottom: 6px; text-transform: uppercase;
  letter-spacing: 0.5px;
}
.plus-tiers {
  display: flex; gap: 4px;
  background: var(--bg-side);
  border-radius: 8px; padding: 3px;
}
.tier-btn {
  flex: 1; background: transparent; color: #aaa;
  padding: 6px 8px; font-size: 13px; font-weight: 500;
  border-radius: 6px; border: none; cursor: pointer;
}
.tier-btn.active { background: var(--accent); color: #fff; }
.tier-btn[data-tier="pro"].active { background: #8b5cf6; }
.tier-btn[data-tier="max"].active { background: #f59e0b; color: #000; }

/* 토글 스위치 */
.switch { position: relative; display: inline-block; width: 40px; height: 22px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background: #444; border-radius: 22px;
  transition: 0.2s;
}
.switch .slider::before {
  content: ""; position: absolute;
  height: 16px; width: 16px;
  left: 3px; top: 3px;
  background: #fff; border-radius: 50%;
  transition: 0.2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(18px); }

/* Pro/Max 활성 시 입력창에 작은 표시 */
.composer.tier-pro #plus-btn { color: #8b5cf6; }
.composer.tier-max #plus-btn { color: #f59e0b; }
.composer.think-on #plus-btn::after {
  content: "🧠"; position: absolute; top: -2px; right: -2px;
  font-size: 10px;
}
.send-btn {
  width: 44px; height: 44px;
  padding: 0; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.send-btn.stopping { background: #ff4757; }
.send-btn.stopping:active { background: #d93948; }

/* 사용자 메시지 수정 */
.msg.user { position: relative; }
.msg.user .body { padding-right: 36px; }
.msg-edit-btn {
  position: absolute; top: 22px; right: 6px;
  background: rgba(0,0,0,0.3); color: #aaa;
  width: 28px; height: 28px;
  border-radius: 50%; padding: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; opacity: 0.6;
}
.msg-edit-btn:active { opacity: 1; background: rgba(79,140,255,0.3); color: #fff; }
.msg.editing .body { display: none; }
.msg.editing .msg-edit-btn { display: none; }
.msg-edit-area {
  background: var(--user-bubble); border: 1px solid #4f8cff;
  border-radius: 14px; padding: 12px;
  display: flex; flex-direction: column; gap: 8px;
}
.msg-edit-area textarea {
  background: transparent; border: none;
  padding: 0; font-size: 15px; line-height: 1.6;
  color: var(--text);
  resize: vertical; min-height: 40px;
}
.msg-edit-buttons { display: flex; gap: 6px; justify-content: flex-end; }
.msg-edit-buttons button {
  padding: 6px 12px; font-size: 13px; border-radius: 8px;
}
.msg-edit-buttons .save { background: var(--accent); color: #fff; }
.msg-edit-buttons .cancel { background: transparent; color: #aaa; border: 1px solid #333; }

/* ========== 디바이스 클래스 (JS가 부여) ========== */

/* 모바일 — 폰: 폭 ≤ 767px 또는 .device-phone */
.device-phone .topbar,
body:not(.device-tablet):not(.device-desktop) .topbar { /* fallback */ }

@media (max-width: 767px) {
  .topbar { display: flex; }
  .sidebar {
    position: absolute;
    top: 0; left: 0; bottom: 0;
    width: min(85vw, 320px);
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 2px 0 24px rgba(0, 0, 0, 0.5);
    padding-top: calc(12px + var(--safe-top));
  }
  .sidebar.open { transform: translateX(0); }
  .messages { padding: 12px; }
  .msg .body { padding: 11px 13px; }
  .composer { padding: 8px 10px; padding-bottom: calc(8px + var(--safe-bottom)); }
  .search-toggle span { display: none; }
  .search-toggle { padding: 8px 4px; }
}

/* 데스크탑 — 폭 ≥ 768px */
@media (min-width: 768px) {
  #new-chat-mobile, #menu-btn { display: none; }
  .topbar { display: none; }
}

/* 가로 모드 폰 — 키보드가 위로 더 많이 올라옴 */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  .topbar { height: 40px; }
  .messages { padding: 8px 12px; }
  .composer textarea { max-height: 80px; }
}

/* 큰 폰/작은 태블릿 — 폭 768~900: 사이드바 살짝 좁게 */
@media (min-width: 768px) and (max-width: 900px) {
  .sidebar { width: 220px; }
}
