:root {
  --chat-bg: #bacee0;
  --yellow: #fee500;
  --text: #191919;
  --muted: #8a8a8a;
  --line: #ececec;
  --claude: #d97757;
  --time: #556677;
  --danger: #e5484d;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "맑은 고딕", "Noto Sans KR", system-ui, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: #fff; overscroll-behavior: none; }
body { -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ───────── 로그인 ───────── */
.login {
  min-height: 100%; display: flex; align-items: center; justify-content: center;
  background: var(--yellow); padding: 24px;
}
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; text-align: center; }
.login-logo {
  width: 84px; height: 84px; margin: 0 auto 8px; border-radius: 30px; background: var(--claude);
  display: flex; align-items: center; justify-content: center; color: #fff; font-size: 46px; line-height: 1;
  box-shadow: 0 6px 18px rgba(0,0,0,.12);
}
.login-logo.small { width: 64px; height: 64px; border-radius: 24px; font-size: 34px; opacity: .7; }
.login h1 { margin: 4px 0 0; font-size: 24px; color: #3c1e1e; }
.login-sub { margin: 0 0 14px; color: #6b5a2e; font-size: 14px; }
.login input {
  height: 48px; border: 1px solid rgba(0,0,0,.08); border-radius: 6px; padding: 0 14px; background: #fff; outline: none;
  font-size: 16px;
}
.login input:focus { border-color: #3c1e1e; }
.login button {
  height: 50px; border: 0; border-radius: 6px; background: #3c1e1e; color: #fff; font-weight: 700; font-size: 16px; margin-top: 6px;
}
.login button:disabled { opacity: .6; }
.login-err { min-height: 20px; margin: 4px 0 0; color: #b42318; font-size: 14px; }

/* ───────── 레이아웃 ───────── */
.app { height: 100dvh; display: flex; overflow: hidden; }
.list-view { width: 100%; display: flex; flex-direction: column; background: #fff; }
.chat-view { display: none; flex: 1; min-width: 0; }
.app.in-chat .list-view { display: none; }
.app.in-chat .chat-view { display: flex; }
@media (min-width: 900px) {
  .list-view { width: 360px; flex: none; border-right: 1px solid var(--line); }
  .chat-view { display: flex; }
  .app.in-chat .list-view { display: flex; }
  .back-btn { display: none !important; }
}

.icon-btn {
  width: 40px; height: 40px; border: 0; background: transparent; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center; color: #222;
}
.icon-btn:active { background: rgba(0,0,0,.06); }

/* ───────── 채팅방 목록 ───────── */
.list-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-t)) 12px 10px 20px;
}
.list-header h2 { margin: 0; font-size: 22px; font-weight: 800; }
.list-actions { display: flex; gap: 2px; }
.room-list { list-style: none; margin: 0; padding: 0 0 var(--safe-b); overflow-y: auto; flex: 1; }
.room-item {
  display: flex; align-items: center; gap: 12px; padding: 10px 20px; cursor: pointer; user-select: none;
}
.room-item:hover { background: #f7f7f7; }
.room-item.active { background: #f0f0f0; }
.avatar {
  width: 48px; height: 48px; flex: none; border-radius: 18px; background: var(--claude); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 24px; line-height: 1;
}
.avatar.sm { width: 38px; height: 38px; border-radius: 15px; font-size: 19px; }
.room-main { flex: 1; min-width: 0; }
.room-top { display: flex; align-items: baseline; gap: 6px; }
.room-name { font-weight: 700; font-size: 15.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room-folder { font-size: 12px; color: var(--muted); white-space: nowrap; }
.room-last {
  margin-top: 3px; font-size: 13.5px; color: #777; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.room-last.running { color: var(--claude); font-weight: 600; }
.room-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; flex: none; }
.room-time { font-size: 11.5px; color: #aaa; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: #fa4f3d; color: #fff;
  font-size: 11.5px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
}
.room-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--muted); }
.primary-btn {
  border: 0; background: var(--yellow); color: var(--text); font-weight: 700; padding: 12px 20px; border-radius: 8px;
}

/* ───────── 대화창 ───────── */
.chat-placeholder {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  background: var(--chat-bg); color: #50637a;
}
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chat-bg); height: 100dvh; }
.chat-header {
  display: flex; align-items: center; gap: 4px; padding: calc(8px + var(--safe-t)) 8px 8px;
  background: var(--chat-bg); position: relative; z-index: 2;
}
.chat-title { flex: 1; min-width: 0; display: flex; flex-direction: column; padding-left: 4px; }
.chat-title strong { font-size: 16.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 12px; color: #50637a; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages {
  flex: 1; overflow-y: auto; padding: 6px 12px 14px; display: flex; flex-direction: column;
  -webkit-overflow-scrolling: touch;
}
.date-sep { align-self: center; margin: 14px 0 10px; padding: 5px 14px; border-radius: 14px; background: rgba(0,0,0,.12); color: #fff; font-size: 12px; }

.row { display: flex; align-items: flex-end; gap: 6px; margin-top: 3px; max-width: 100%; }
.row.first { margin-top: 12px; }
.row.me { justify-content: flex-end; }
.row.bot { padding-left: 46px; }
.row.bot.first { padding-left: 0; align-items: flex-start; }
.bot-col { display: flex; flex-direction: column; gap: 4px; min-width: 0; max-width: calc(100% - 46px); }
.bot-name { font-size: 12.5px; color: #3d4b5c; margin: 0 0 1px 2px; }
.bubble-line { display: flex; align-items: flex-end; gap: 6px; min-width: 0; }
.row.me .bubble-line { flex-direction: row-reverse; }
.bubble {
  padding: 8px 11px; border-radius: var(--radius); font-size: 15px; line-height: 1.45;
  word-break: break-word; overflow-wrap: anywhere; white-space: pre-wrap; max-width: min(78vw, 640px); min-width: 0;
  user-select: text;
}
.row.me .bubble { background: var(--yellow); border-top-right-radius: 4px; }
.row.bot .bubble { background: #fff; border-top-left-radius: 4px; }
.row.me:not(.first) .bubble { border-top-right-radius: var(--radius); }
.row.bot:not(.first) .bubble { border-top-left-radius: var(--radius); }
.msg-time { font-size: 10.5px; color: var(--time); flex: none; white-space: nowrap; padding-bottom: 1px; }

.bubble code { font-family: Consolas, "D2Coding", ui-monospace, monospace; font-size: 13px; background: rgba(0,0,0,.06); padding: 1px 4px; border-radius: 4px; }
.bubble .code-wrap { position: relative; margin: 6px 0; }
.bubble pre {
  margin: 0; padding: 10px 12px; background: #1f2430; color: #e6e6e6; border-radius: 8px; overflow-x: auto;
  white-space: pre; font-size: 12.5px; line-height: 1.5;
}
.bubble pre code { background: none; padding: 0; color: inherit; font-size: inherit; }
.copy-btn {
  position: absolute; top: 6px; right: 6px; border: 0; background: rgba(255,255,255,.14); color: #ddd;
  font-size: 11px; padding: 3px 8px; border-radius: 6px;
}
.bubble a { color: #1a5fb4; }
.bubble .h { font-weight: 800; }

.tool-row { align-self: flex-start; margin: 4px 0 0 46px; max-width: calc(100% - 46px); }
.tool-chip {
  display: inline-flex; gap: 6px; align-items: baseline; max-width: 100%;
  background: rgba(255,255,255,.55); border-radius: 10px; padding: 5px 10px; font-size: 12.5px; color: #3d4b5c;
  cursor: pointer; border: 0; text-align: left;
}
.tool-chip b { flex: none; color: #2b3a4a; }
.tool-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: Consolas, ui-monospace, monospace; }
.tool-chip.open span { white-space: pre-wrap; word-break: break-all; }

.sys-row { align-self: center; margin: 10px 0 4px; max-width: 90%; }
.sys-msg {
  background: rgba(0,0,0,.12); color: #fff; font-size: 12.5px; padding: 6px 12px; border-radius: 12px;
  text-align: center; white-space: pre-wrap; line-height: 1.45;
}
.sys-msg.err { background: rgba(229,72,77,.88); }

.typing { display: inline-flex; gap: 4px; padding: 12px 14px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: #b5b5b5; animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 10px calc(8px + var(--safe-b));
  background: #fff;
}
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none; padding: 9px 4px; font-size: 16px; line-height: 1.4;
  max-height: 140px; background: transparent;
}
.send-btn {
  height: 38px; min-width: 56px; padding: 0 12px; border: 0; border-radius: 8px; background: var(--yellow);
  font-weight: 700; font-size: 14.5px;
}
.send-btn:disabled { background: #f2f2f2; color: #b8b8b8; cursor: default; }
.send-btn.stop { background: #333; color: #fff; }

/* ───────── 시트 / 모달 ───────── */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20; }
.sheet {
  position: fixed; left: 50%; bottom: 0; transform: translateX(-50%); width: 100%; max-width: 480px; z-index: 21;
  background: #fff; border-radius: 18px 18px 0 0; padding: 18px 18px calc(18px + var(--safe-b));
  max-height: 85dvh; overflow-y: auto; box-shadow: 0 -4px 24px rgba(0,0,0,.15);
}
@media (min-width: 900px) {
  .sheet { bottom: auto; top: 50%; transform: translate(-50%, -50%); border-radius: 16px; }
}
.sheet h3 { margin: 0 0 14px; font-size: 18px; }
.sheet label { display: block; font-size: 13px; color: #555; margin: 12px 0 6px; font-weight: 600; }
.sheet input[type=text], .sheet select {
  width: 100%; height: 44px; border: 1px solid #ddd; border-radius: 8px; padding: 0 12px; outline: none; background: #fff; font-size: 16px;
}
.sheet input:focus, .sheet select:focus { border-color: #888; }
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; line-height: 1.5; word-break: break-all; }
.mode-list { display: flex; flex-direction: column; gap: 8px; }
.sheet .mode-opt {
  display: flex; gap: 10px; align-items: flex-start; border: 1px solid #e3e3e3; border-radius: 10px; padding: 10px 12px; cursor: pointer;
  margin: 0 !important; font-weight: 400 !important; color: var(--text) !important; font-size: 14px !important;
}
.mode-opt input { margin-top: 3px; accent-color: #3c1e1e; }
.mode-opt.checked { border-color: #3c1e1e; background: #fffbe0; }
.mode-opt b { display: block; font-size: 14.5px; }
.mode-opt small { color: #666; font-size: 12.5px; line-height: 1.45; }
.sheet-actions { display: flex; gap: 8px; margin-top: 18px; }
.sheet-actions button { flex: 1; height: 46px; border-radius: 8px; border: 0; font-weight: 700; }
.btn-cancel { background: #f1f1f1; }
.btn-ok { background: var(--yellow); }
.menu-list { list-style: none; margin: 0; padding: 0; }
.menu-list button {
  width: 100%; text-align: left; border: 0; background: none; padding: 14px 4px; font-size: 16px; border-bottom: 1px solid #f2f2f2;
}
.menu-list li:last-child button { border-bottom: 0; }
.menu-list button.danger { color: var(--danger); }
.menu-list .menu-sub { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); z-index: 40;
  background: rgba(30,30,30,.9); color: #fff; padding: 10px 16px; border-radius: 20px; font-size: 14px; max-width: 90vw; text-align: center;
}
