/* ===================== Gemini-style UI ===================== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  /* JS가 동적으로 갱신 — visualViewport 기준 (키보드/주소창 추적). dvh 는 JS 전 폴백 */
  --app-height: 100dvh;
  --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);

  /* Gemini 시그니처 그라데이션 (인사말/스파클) */
  --grad: linear-gradient(74deg, #4285f4 0%, #9b72cb 48%, #d96570 100%);

  /* ===== DARK (기본) ===== */
  --bg: #131314;
  --bg-elev: #1e1f20;
  --bg-side: #1b1c1d;
  --surface: #1e1f20;
  --surface-2: #282a2c;
  --surface-hover: #2d2f31;
  --border: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text: #e3e3e3;
  --text-strong: #f6f6f6;
  --muted: #a8abb0;
  --accent: #8ab4f8;
  --accent-ink: #062e6f;
  --user-bubble: #2a2b2d;
  --danger: #f28b82;
  --shadow: 0 1px 3px rgba(0,0,0,0.4), 0 4px 16px rgba(0,0,0,0.3);
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-elev: #f0f4f9;
  --bg-side: #f0f4f9;
  --surface: #f0f4f9;
  --surface-2: #e9eef6;
  --surface-hover: #dde7f4;
  --border: #dadce0;
  --border-strong: #c4c7cc;
  --text: #1f1f1f;
  --text-strong: #1f1f1f;
  --muted: #5f6368;
  --accent: #0b57d0;
  --accent-ink: #ffffff;
  --user-bubble: #d3e3fd;
  --danger: #d93025;
  --shadow: 0 1px 2px rgba(0,0,0,0.08), 0 4px 14px rgba(0,0,0,0.08);
}

html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  font-family: "Google Sans", "Roboto", -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; position: fixed; inset: 0; overflow: hidden; }
.hidden { display: none !important; }

/* ========== 버튼/폼 ========== */
button {
  cursor: pointer; border: none; border-radius: 999px;
  padding: 10px 18px; font-size: 14px; font-weight: 500;
  font-family: inherit; touch-action: manipulation; color: inherit;
  transition: background 0.15s, box-shadow 0.15s, opacity 0.15s, transform 0.08s;
}
button.primary { background: var(--accent); color: var(--accent-ink); }
button.primary:hover { box-shadow: var(--shadow); }
button.primary:active { transform: scale(0.97); }
button.ghost {
  background: transparent; color: var(--muted);
  border: 1px solid var(--border-strong); padding: 7px 14px; font-size: 13px;
}
button.ghost:hover { background: var(--surface-hover); color: var(--text); }
button.danger {
  background: transparent; color: var(--danger);
  border: 1px solid var(--danger); font-weight: 500;
}
button.danger:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); }
button:disabled { opacity: 0.45; cursor: default; }
.icon-btn {
  background: transparent; color: var(--text);
  width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; padding: 0;
}
.icon-btn:hover { background: var(--surface-hover); }

input, textarea {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 12px; padding: 12px 14px;
  font-size: 16px; font-family: inherit; width: 100%;
  -webkit-appearance: none; appearance: none;
  direction: ltr; unicode-bidi: plaintext; text-align: left;
  transition: border-color 0.15s;
}
input:focus, textarea:focus { outline: none; border-color: var(--accent); }

/* ========== 인증 ========== */
#auth {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: max(20px, var(--safe-top)) 20px max(20px, var(--safe-bottom));
  overflow-y: auto;
}
.auth-card {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 28px; padding: 32px 26px;
  width: 100%; max-width: 380px;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: var(--shadow);
}
.auth-card h1 {
  margin: 0 0 8px; text-align: center; font-size: 26px; font-weight: 600;
  background: var(--grad); -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
.tabs { display: flex; gap: 8px; background: var(--surface); border-radius: 999px; padding: 4px; }
.tab {
  flex: 1; background: transparent; color: var(--muted);
  padding: 9px; border-radius: 999px; font-size: 14px; border: none;
}
.tab.active { background: var(--accent); color: var(--accent-ink); }
.error { color: var(--danger); 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: var(--border); }
.google-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: #fff; color: #3c4043; border: 1px solid #dadce0;
  padding: 12px 14px; border-radius: 999px; text-decoration: none; font-weight: 500; font-size: 14px;
}
.google-btn:active { opacity: 0.85; }
.footer-btns { display: flex; gap: 6px; align-items: center; }

/* ========== 채팅 레이아웃 ========== */
#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;
  padding: 0 6px; padding-top: var(--safe-top);
  height: calc(52px + var(--safe-top));
  background: var(--bg); border-bottom: 1px solid var(--border); z-index: 30;
}
.topbar-title {
  flex: 1; text-align: center; font-size: 16px; font-weight: 500; color: var(--text);
  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.5); z-index: 40; }
.backdrop.show { display: block; }

/* 사이드바 */
.sidebar {
  width: 280px; flex-shrink: 0; background: var(--bg-side);
  display: flex; flex-direction: column; gap: 8px;
  padding: 12px; padding-left: max(12px, var(--safe-left));
  padding-bottom: max(12px, var(--safe-bottom));
}
/* 사이드바 상단: 접기 + 새 대화 + 설정 + 계정 아바타 */
.sidebar-top { display: flex; align-items: center; gap: 6px; }
.new-chat-btn {
  flex: 1; min-width: 0; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--surface-2); color: var(--text);
  padding: 10px 14px; border-radius: 999px; font-size: 14px; font-weight: 500; white-space: nowrap;
}
.new-chat-btn:hover { background: var(--surface-hover); box-shadow: var(--shadow); }
.avatar-btn {
  width: 38px; height: 38px; flex-shrink: 0; border: none; border-radius: 50%;
  background: var(--grad); color: #fff; font-weight: 600; font-size: 15px; text-transform: uppercase;
  display: flex; align-items: center; justify-content: center;
}
.account-wrap { position: relative; flex-shrink: 0; }
.account-menu {
  position: absolute; top: 46px; right: 0; z-index: 70;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 16px;
  padding: 8px; min-width: 210px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 2px;
}
.account-menu.hidden { display: none; }
.account-name {
  padding: 8px 12px 10px; font-size: 13px; color: var(--muted);
  border-bottom: 1px solid var(--border); margin-bottom: 4px; word-break: break-all;
}
.menu-item {
  text-align: left; background: transparent; color: var(--text);
  padding: 10px 12px; border-radius: 10px; font-size: 14px; width: 100%; border: none;
}
.menu-item:hover { background: var(--surface-hover); }
.conv-list {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px;
  margin-top: 6px; -webkit-overflow-scrolling: touch;
}
.conv-item {
  padding: 10px 14px; border-radius: 999px; cursor: pointer;
  font-size: 14px; color: var(--text);
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
}
.conv-item:hover { background: var(--surface-hover); }
.conv-item.active { background: var(--user-bubble); color: var(--text-strong); }
.conv-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv-item .del { color: var(--muted); font-size: 20px; padding: 2px 8px; line-height: 1; border-radius: 50%; }
.conv-item .del:hover { color: var(--danger); }
/* 모던 스크롤바 (윈도우 기본 ▲▼ 화살표 제거) */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 999px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); background-clip: padding-box; }
::-webkit-scrollbar-button { display: none; width: 0; height: 0; }
::-webkit-scrollbar-corner { background: transparent; }

/* 사이드바 접기 (데스크탑/태블릿) */
.sidebar { transition: width 0.22s ease, opacity 0.22s ease, padding 0.22s ease; }
.sidebar-expand {
  display: none; position: absolute; top: 10px; left: 10px; z-index: 25;
  background: var(--surface); border: 1px solid var(--border);
}
.sidebar-expand:hover { background: var(--surface-hover); }
@media (min-width: 768px) {
  body.sidebar-collapsed .sidebar {
    width: 0; min-width: 0; padding-left: 0; padding-right: 0;
    opacity: 0; pointer-events: none; overflow: hidden; border: none;
  }
  body.sidebar-collapsed .sidebar-expand { display: flex; }
  body.sidebar-collapsed .messages,
  body.sidebar-collapsed .welcome { padding-top: 58px; }
}

/* 메인 */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.messages {
  flex: 1; overflow-y: auto; padding: 20px 16px 8px;
  display: flex; flex-direction: column; gap: 22px;
  -webkit-overflow-scrolling: touch; scroll-behavior: smooth;
  padding-right: max(16px, var(--safe-right)); padding-left: max(16px, var(--safe-left));
}

/* 메시지 */
.msg { max-width: 760px; width: 100%; margin: 0 auto; }
.msg .role {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px;
}
.msg.assistant .role::before {
  content: ""; width: 20px; height: 20px; flex-shrink: 0; border-radius: 50%;
  background: var(--grad);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2l2.2 6.3L20.5 10l-6.3 2.2L12 18.5 9.8 12.2 3.5 10l6.3-1.7z'/%3E%3C/svg%3E") center/contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2l2.2 6.3L20.5 10l-6.3 2.2L12 18.5 9.8 12.2 3.5 10l6.3-1.7z'/%3E%3C/svg%3E") center/contain no-repeat;
}
.msg .body {
  line-height: 1.7; font-size: 15.5px; color: var(--text);
  word-wrap: break-word; overflow-wrap: break-word;
  direction: ltr; unicode-bidi: plaintext; text-align: left;
}
/* AI: 버블 없이 본문 (Gemini 스타일) / 사용자: 둥근 버블 우측 */
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .role { align-self: flex-end; }
.msg.user .body {
  background: var(--user-bubble); border-radius: 20px 20px 6px 20px;
  padding: 12px 16px; max-width: 90%; display: inline-block;
}
.msg .body p { margin: 0 0 0.7em; }
.msg .body p:last-child { margin: 0; }
.msg .body pre {
  background: var(--surface); border: 1px solid var(--border);
  padding: 14px; border-radius: 12px; 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(--surface); padding: 2px 6px; border-radius: 6px; }
.msg .body a { color: var(--accent); }
.msg .status { font-size: 13px; color: var(--accent); margin: 4px 0; }
.msg .sources { font-size: 12px; color: var(--muted); margin-top: 10px; line-height: 1.9; }
.msg .sources a { color: var(--accent); text-decoration: none; margin-right: 8px; word-break: break-all; }

.thinking { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.thinking .dots { display: inline-flex; gap: 4px; }
.thinking .dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); 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.8); } 40% { opacity: 1; transform: scale(1); } }

/* AI 생각(사고) 블록 — 기본 숨김, body.show-think 일 때만 표시 */
.think-block { display: none; margin: 2px 0 10px; }
body.show-think .think-block { display: block; }
.think-head {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface); color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 4px 12px; font-size: 12.5px; cursor: pointer; font-family: inherit;
}
.think-head:hover { background: var(--surface-hover); }
.think-caret { transition: transform .15s; }
.think-block.folded .think-caret { transform: rotate(-90deg); }
.think-body {
  margin-top: 8px; padding: 12px 14px;
  background: var(--surface); border: 1px dashed var(--border);
  border-radius: 12px; color: var(--muted); font-size: 13px;
  line-height: 1.7; white-space: pre-wrap; word-break: break-word;
  direction: ltr; unicode-bidi: plaintext; text-align: left;
}
.think-block.folded .think-body { display: none; }

/* 코드블록 툴바 (파일명 + 복사/저장) */
.code-toolbar {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-bottom: none;
  border-radius: 12px 12px 0 0; padding: 6px 10px; margin-top: 4px;
}
.code-toolbar + pre { border-radius: 0 0 12px 12px !important; margin-top: 0 !important; border-top: none !important; }
.code-fname {
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px;
  color: var(--muted); flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.code-btn {
  background: transparent; color: var(--muted);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 3px 10px; font-size: 12px; cursor: pointer; font-family: inherit; white-space: nowrap;
}
.code-btn:hover { background: var(--surface-hover); color: var(--text); }
.code-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

/* 첨부 파일 칩 — 입력창 미리보기 & 메시지 내부 */
.file-preview {
  display: inline-flex; align-items: center; gap: 8px; position: relative;
  align-self: flex-start; max-width: 320px; margin-bottom: 6px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 8px 34px 8px 12px;
}
.file-preview .file-nm, .file-chip .file-nm {
  font-size: 13px; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 240px;
}
.file-preview .preview-x { top: 50%; right: 6px; transform: translateY(-50%); width: 22px; height: 22px; font-size: 15px; }
.file-chip {
  display: inline-flex; align-items: center; gap: 8px; margin-bottom: 6px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 8px 12px; max-width: 100%;
}
.file-chip .file-ic, .file-preview .file-ic { font-size: 16px; }

/* 다운로드 가능한 첨부 칩 (업로드 파일 / AI가 만든 파일) */
.att-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 2px; }
.att-chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 8px 12px; max-width: 100%;
  text-decoration: none; color: var(--text); cursor: pointer;
}
.att-chip:hover { background: var(--surface-hover); border-color: var(--border-strong); }
.att-chip .att-ic { font-size: 16px; flex-shrink: 0; }
.att-chip .att-nm {
  font-size: 13px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; max-width: 260px;
}
.att-chip .att-dl { color: var(--accent); font-weight: 700; flex-shrink: 0; }

/* 메시지 내부 미디어 */
.msg .body img.attached { max-width: 100%; max-height: 360px; border-radius: 14px; display: block; cursor: pointer; }
.msg .body video.attached { max-width: 100%; max-height: 380px; border-radius: 14px; display: block; background: #000; }
.media-wrap { position: relative; display: inline-block; margin-bottom: 8px; }
.download-btn {
  position: absolute; top: 8px; right: 8px;
  background: rgba(0,0,0,0.6); color: #fff;
  display: inline-flex; align-items: center; gap: 4px;
  padding: 7px 11px; border-radius: 999px; font-size: 12px; font-weight: 500;
  text-decoration: none; border: 1px solid rgba(255,255,255,0.18);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.download-btn:hover { background: var(--accent); color: var(--accent-ink); }
.download-btn svg { display: block; }
@media (max-width: 768px) { .download-btn span { display: none; } .download-btn { padding: 8px; } }

/* ========== 환영 화면 ========== */
.welcome {
  flex: 1; display: none; flex-direction: column; justify-content: center;
  max-width: 760px; width: 100%; margin: 0 auto;
  padding: 24px 16px; padding-left: max(16px, var(--safe-left)); padding-right: max(16px, var(--safe-right));
}
.main.empty .welcome { display: flex; }
.main.empty .messages { display: none; }
.welcome-greeting {
  font-size: clamp(30px, 6vw, 48px); font-weight: 600; line-height: 1.15;
  background: var(--grad); -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; margin: 0;
}
.welcome-sub { font-size: clamp(20px, 4vw, 30px); font-weight: 500; color: var(--muted); margin: 2px 0 28px; }
.suggestions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.suggestion {
  text-align: left; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 16px; padding: 16px;
  font-size: 14px; line-height: 1.4; min-height: 96px;
  display: flex; flex-direction: column; justify-content: space-between; gap: 10px;
}
.suggestion:hover { background: var(--surface-hover); box-shadow: var(--shadow); }
.suggestion .emoji { font-size: 22px; align-self: flex-end; }
@media (max-width: 600px) { .suggestions { grid-template-columns: 1fr; } .suggestion { min-height: 0; flex-direction: row; align-items: center; } }

/* ========== 입력창 (Gemini 알약형) ========== */
.composer {
  flex-shrink: 0; margin-top: auto; padding: 8px 16px 14px;
  padding-bottom: calc(14px + var(--safe-bottom));
  padding-left: max(16px, var(--safe-left)); padding-right: max(16px, var(--safe-right));
  display: flex; flex-direction: column; gap: 8px; background: var(--bg);
}
.composer .composer-row, .composer .image-preview { max-width: 760px; width: 100%; margin: 0 auto; }
/* + 버튼 묶음은 입력칸을 밀지 않도록 내용 크기로 고정 (width:100% 상속 차단) */
.composer .composer-row .plus-wrap { flex: 0 0 auto; width: auto; max-width: none; margin: 0; position: relative; }
.composer-row {
  display: flex; gap: 6px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 28px; padding: 6px 6px 6px 8px;
}
.composer-row:focus-within { border-color: var(--border-strong); box-shadow: var(--shadow); }
.composer textarea {
  flex: 1; resize: none; max-height: 160px; min-height: 28px;
  line-height: 1.5; padding: 11px 6px; border: none; background: transparent;
  border-radius: 0;
}
.composer textarea:focus { border: none; }
.image-preview { position: relative; align-self: flex-start; max-width: 120px; margin-bottom: 4px; }
.image-preview img { max-width: 120px; max-height: 120px; border-radius: 14px; border: 1px solid var(--border); display: block; }
.preview-x {
  position: absolute; top: -6px; right: -6px; background: var(--danger); color: #fff;
  width: 24px; height: 24px; border-radius: 50%; padding: 0; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
}
.send-btn {
  width: 42px; height: 42px; padding: 0; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  background: var(--accent); color: var(--accent-ink);
}
.send-btn:disabled { background: var(--surface-2); color: var(--muted); opacity: 1; }
.send-btn.stopping { background: var(--danger); color: #fff; }

/* + 메뉴 */
.plus-wrap { position: relative; }
#plus-btn { width: 42px; height: 42px; flex-shrink: 0; position: relative; }
.plus-menu {
  position: absolute; bottom: 54px; left: 0;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 18px; padding: 8px; min-width: 250px; z-index: 60;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 2px;
}
.plus-item {
  display: flex; align-items: center; gap: 10px; padding: 11px 12px; border-radius: 12px;
  background: transparent; color: var(--text); font-size: 14px; text-align: left; width: 100%; border: none; cursor: pointer;
}
.plus-item:hover { background: var(--surface-hover); }
.plus-row { justify-content: space-between; cursor: default; }
.plus-section { padding: 10px 12px 4px; border-top: 1px solid var(--border); margin-top: 4px; }
.plus-label { font-size: 11px; color: var(--muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px; }
.plus-tiers { display: flex; gap: 4px; background: var(--surface); border-radius: 999px; padding: 4px; }
.tier-btn { flex: 1; background: transparent; color: var(--muted); padding: 7px 8px; font-size: 13px; font-weight: 500; border-radius: 999px; border: none; cursor: pointer; }
.tier-btn.active { background: var(--accent); color: var(--accent-ink); }
.tier-btn[data-tier="pro"].active { background: #9b72cb; color: #fff; }
.tier-btn[data-tier="max"].active { background: #f59e0b; color: #1f1f1f; }

/* 토글 스위치 */
.switch { position: relative; display: inline-block; width: 42px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; cursor: pointer; inset: 0; background: var(--surface-2); border-radius: 24px; transition: 0.2s; }
.switch .slider::before { content: ""; position: absolute; height: 18px; width: 18px; 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); }

.composer.tier-pro #plus-btn { color: #9b72cb; }
.composer.tier-max #plus-btn { color: #f59e0b; }
.composer.think-on #plus-btn::after { content: "🧠"; position: absolute; top: 0; right: 0; font-size: 11px; }

/* 사용자 메시지 수정 */
.msg.user .body { position: relative; padding-right: 16px; }
.msg-edit-btn {
  background: transparent; color: var(--muted);
  width: 30px; height: 30px; border-radius: 50%; padding: 0; margin-top: 4px;
  display: flex; align-items: center; justify-content: center; font-size: 14px; align-self: flex-end;
}
.msg-edit-btn:hover { background: var(--surface-hover); color: var(--text); }
.msg.editing .body { display: none; }
.msg.editing .msg-edit-btn { display: none; }
.msg-edit-area { background: var(--surface); border: 1px solid var(--accent); border-radius: 18px; padding: 12px; display: flex; flex-direction: column; gap: 8px; width: 100%; }
.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: 7px 14px; font-size: 13px; }
.msg-edit-buttons .save { background: var(--accent); color: var(--accent-ink); }
.msg-edit-buttons .cancel { background: transparent; color: var(--muted); border: 1px solid var(--border-strong); }

/* ========== 모달 (관리자/설정) ========== */
.admin-modal { position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 20px; }
.admin-card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 24px; width: 100%; max-width: 900px; max-height: 85vh; display: flex; flex-direction: column; box-shadow: var(--shadow); overflow: hidden; }
.admin-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.admin-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.admin-tabs { display: flex; gap: 6px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.admin-tab { background: transparent; color: var(--muted); border: 1px solid var(--border-strong); padding: 7px 14px; border-radius: 999px; font-size: 13px; }
.admin-tab.active { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.admin-pane { flex: 1; overflow: auto; background: var(--bg); margin: 0; padding: 14px; font-family: ui-monospace, monospace; font-size: 12px; white-space: pre-wrap; word-break: break-all; color: var(--text); }
#admin-users, #admin-ops, #settings-pane { font-family: inherit; padding: 18px; color: var(--text); }
#ai-nickname, #ai-personality, #ai-tone, #announce-text {
  width: 100%; box-sizing: border-box; resize: vertical;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 12px; color: var(--text); padding: 11px; font-size: 14px;
}
.admin-section { margin-bottom: 16px; }
.admin-section h3 { margin: 0 0 6px; font-size: 15px; font-weight: 600; }
.admin-hint { font-size: 12px; color: var(--muted); margin: 0 0 10px; line-height: 1.5; }
#settings-save, #announce-send, #maintenance-toggle { margin-top: 8px; background: var(--accent); color: var(--accent-ink); }
#maintenance-toggle.on { background: var(--danger); color: #fff; }
.admin-status { margin-top: 8px; font-size: 13px; color: var(--accent); min-height: 1em; }
.admin-status.err { color: var(--danger); }
.admin-user-row, .admin-conv-row, .admin-msg { padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 13px; }
.admin-user-row, .admin-conv-row { cursor: pointer; border-radius: 12px; }
.admin-user-row:hover, .admin-conv-row:hover { background: var(--surface-hover); }
.admin-user-row .uname, .admin-conv-row .ctitle { color: var(--text-strong); font-weight: 500; }
.admin-user-row .meta, .admin-conv-row .cmeta, .admin-msg .mtime { color: var(--muted); font-size: 12px; margin-top: 2px; }
.admin-back { background: transparent; color: var(--accent); padding: 7px 12px; font-size: 13px; border: 1px solid var(--border-strong); margin: 10px; }
.admin-breadcrumb { padding: 10px 14px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); background: var(--bg); }
.admin-msg .role-tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 10px; margin-right: 6px; vertical-align: middle; }
.admin-msg.user .role-tag { background: var(--user-bubble); color: var(--accent); }
.admin-msg.assistant .role-tag { background: var(--surface-2); color: var(--muted); }
.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: 10px; margin-top: 6px; display: block; }

/* 점검 오버레이 */
.maintenance-overlay { position: fixed; inset: 0; background: var(--bg); z-index: 9999; display: flex; align-items: center; justify-content: center; padding: 20px; }
.maintenance-overlay.hidden { display: none; }
.maintenance-card { text-align: center; max-width: 360px; color: var(--text); }
.maintenance-icon { font-size: 60px; line-height: 1; margin-bottom: 16px; animation: spin 3s linear infinite; display: inline-block; }
@keyframes spin { from { transform: rotate(0); } to { transform: rotate(360deg); } }
.maintenance-card h2 { margin: 0 0 12px; font-size: 24px; }
.maintenance-card p { margin: 0; font-size: 14px; color: var(--muted); line-height: 1.6; }
.maintenance-card .primary { margin-top: 20px; padding: 10px 22px; }

/* 테마 토글 */
.theme-toggle { background: transparent; border: 1px solid var(--border-strong); color: var(--muted); width: 38px; height: 38px; border-radius: 50%; padding: 0; display: flex; align-items: center; justify-content: center; font-size: 16px; }
.theme-toggle:hover { background: var(--surface-hover); color: var(--text); }

/* ========== 반응형 ========== */
@media (max-width: 767px) {
  .topbar { display: flex; }
  .sidebar {
    position: absolute; top: 0; left: 0; bottom: 0; width: min(86vw, 320px); z-index: 50;
    transform: translateX(-100%); transition: transform 0.25s ease;
    box-shadow: 4px 0 30px rgba(0,0,0,0.4); padding-top: calc(12px + var(--safe-top));
  }
  .sidebar.open { transform: translateX(0); }
  .messages { padding: 14px 12px 6px; gap: 18px; }
  .composer { padding: 6px 10px; padding-bottom: max(8px, var(--safe-bottom)); }
}
/* 키보드가 올라오면 홈 인디케이터 여백 제거(키보드가 그 영역을 덮음) → 입력창이 키보드에 딱 붙음 */
body.kb-open .composer { padding-bottom: 8px; }
@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: calc(40px + var(--safe-top)); }
  .messages { padding: 8px 12px; }
  .composer textarea { max-height: 80px; }
}
/* 태블릿/패드: 사이드바 약간 좁게 */
@media (min-width: 768px) and (max-width: 1024px) { .sidebar { width: 240px; } }
/* 노트북/낮은 높이 화면: 세로 여백 축소 */
@media (min-width: 768px) and (max-height: 760px) {
  .messages { gap: 16px; }
  .welcome-sub { margin-bottom: 18px; }
}
/* 큰 데스크탑: 본문 폭 살짝 넓게 */
@media (min-width: 1400px) {
  .msg, .welcome, .composer .composer-row, .composer .image-preview, .composer .plus-wrap { max-width: 820px; }
}
