:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6b66;
  --line: #e3e2dd;
  --accent: #2f6fde;
  --accent-text: #ffffff;
  --user-bg: #e9f0fd;
  --code-bg: #f1f0ec;
  --ok: #1f8a4c;
  --warn: #b26a00;
  --err: #c43c3c;
  --claude: #c96442;
  --codex: #3a3a3a;
  --radius: 10px;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, "D2Coding", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --panel: #1f1f1d;
    --text: #ecebe6;
    --muted: #9b9a94;
    --line: #34332f;
    --accent: #5b8ef0;
    --user-bg: #23304a;
    --code-bg: #2a2a27;
    --ok: #4cc27f;
    --warn: #e0a040;
    --err: #ef6b6b;
    --claude: #e08a68;
    --codex: #c8c8c8;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 8px 14px; cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { background: var(--err); color: #fff; border-color: var(--err); }
button.ghost { background: transparent; }
button.small { padding: 4px 10px; font-size: 13px; }
button.icon { padding: 4px 10px; font-size: 18px; line-height: 1; }
input, select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
}
textarea { resize: none; }
.muted { color: var(--muted); }
.small-text { font-size: 12px; }
.error { color: var(--err); min-height: 1.2em; margin: 6px 0 0; }

/* 로그인 */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 28px; display: grid; gap: 10px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0 0 8px; }

/* 레이아웃 */
.app { display: flex; height: 100%; }
.sidebar {
  width: 290px; flex: none; display: flex; flex-direction: column;
  border-right: 1px solid var(--line); background: var(--panel);
}
.sidebar-head, .sidebar-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 14px;
}
.sidebar-head { border-bottom: 1px solid var(--line); }
.usage-panel {
  display: grid; gap: 2px; width: 100%; padding: 8px 14px; border: 0; border-top: 1px solid var(--line); border-radius: 0;
  background: transparent; text-align: left; font-size: 12px; color: var(--muted);
}
.usage-panel:empty { display: none; }
.usage-panel .line { display: flex; gap: 6px; flex-wrap: wrap; }
.usage-panel strong { color: var(--text); font-weight: 600; }
.usage-panel .high { color: var(--warn); font-weight: 600; }
.usage-panel .full { color: var(--err); font-weight: 600; }
.sidebar-foot { border-top: 1px solid var(--line); margin-top: auto; }
.connection { font-size: 12px; color: var(--muted); }
.connection.ok::before { content: "● "; color: var(--ok); }
.connection.down::before { content: "● "; color: var(--err); }

.sidebar-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.session-list { list-style: none; margin: 0; padding: 6px; }
.sidebar-tools { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px 0; }
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; padding: 4px 10px; font-size: 12px; background: transparent; color: var(--muted); }
.segmented button.active { background: var(--user-bg); color: var(--text); font-weight: 600; }
.filter { display: block; width: calc(100% - 20px); margin: 8px 10px 4px; padding: 6px 9px; font-size: 13px; }
.group { padding: 2px 0; }
.group-head {
  display: flex; align-items: center; gap: 6px; width: 100%; padding: 6px 14px; border: 0; border-radius: 0;
  background: transparent; font-size: 12px; color: var(--muted); text-align: left;
}
.group-head:disabled { opacity: 1; cursor: default; }
.group-head .caret { width: 10px; flex: none; }
.group-head .group-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-head .group-count { font-size: 11px; }
.session-list { padding: 0 6px 4px; }
.session-list li.external .name { font-weight: 500; }
.session-list li.is-hidden { opacity: .5; }
.session-list li.busy { opacity: .55; cursor: progress; }
.dot.outside { background: transparent; border: 1px solid var(--muted); }
.more {
  border: 0; background: transparent; padding: 0 4px; font-size: 16px; line-height: 1; color: var(--muted);
  visibility: hidden; flex: none;
}
.session-list li:hover .more, .session-list li.active .more, .more:focus-visible { visibility: visible; }
.list-empty { padding: 10px 14px; margin: 0; }
.hidden-toggle { display: block; margin: 6px auto 12px; font-size: 12px; color: var(--muted); }
.item-menu {
  position: fixed; z-index: 30; display: grid; min-width: 140px; padding: 4px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--panel); box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
}
.item-menu button { border: 0; text-align: left; padding: 7px 10px; font-size: 13px; background: transparent; }
.item-menu button:hover { background: var(--bg); }
@media (pointer: coarse) { .more { visibility: visible; } }
.badge.readonly { color: var(--warn); border-color: var(--warn); }
.badge.fast { color: var(--warn); border-color: var(--warn); }
textarea:disabled { opacity: .7; }
.session-list li {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; display: grid; gap: 2px;
}
.session-list li:hover { background: var(--bg); }
.session-list li.active { background: var(--user-bg); }
.session-list .row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.session-list .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.session-list .sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  font-size: 11px; padding: 1px 7px; border-radius: 999px; border: 1px solid var(--line);
  color: var(--muted); white-space: nowrap;
}
.badge.claude { color: var(--claude); border-color: var(--claude); }
.badge.codex { color: var(--codex); border-color: var(--codex); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--line); }
.dot.running { background: var(--accent); animation: pulse 1.2s infinite; }
.dot.waiting { background: var(--warn); animation: pulse 1.2s infinite; }
.dot.error { background: var(--err); }
@keyframes pulse { 50% { opacity: .35; } }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; }
.main-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.title-block { flex: 1; min-width: 0; }
.title-block h2 { margin: 0; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 3px; font-size: 12px; color: var(--muted); }
.head-actions { display: flex; align-items: center; gap: 8px; }
#menu { display: none; }

/* 대화 */
.messages { flex: 1; overflow-y: auto; padding: 18px 16px 28px; }
.messages > * { max-width: 860px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 18vh; }
.msg { margin: 14px auto; }
.msg.user {
  background: var(--user-bg); border-radius: var(--radius); padding: 10px 14px; white-space: pre-wrap;
  width: fit-content; max-width: min(860px, 92%); margin-right: 0;
}
.msg.user-wrap { display: flex; justify-content: flex-end; }
.msg.assistant { overflow-wrap: anywhere; }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant pre, .tool pre, .approval pre {
  background: var(--code-bg); padding: 10px 12px; border-radius: 8px; overflow-x: auto;
  font: 13px/1.45 var(--mono); white-space: pre;
}
.msg.assistant code { font-family: var(--mono); font-size: 13px; background: var(--code-bg); padding: 1px 4px; border-radius: 4px; }
.msg.assistant pre code { background: none; padding: 0; }
.msg.assistant table { border-collapse: collapse; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--line); padding: 4px 8px; }
.streaming::after { content: "▍"; animation: pulse 1s infinite; color: var(--muted); }

.reasoning { color: var(--muted); font-size: 13px; }
.reasoning summary { cursor: pointer; }

.tool {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); margin: 8px auto;
}
.tool > summary {
  display: flex; align-items: center; gap: 8px; padding: 7px 12px; cursor: pointer; list-style: none;
  font-size: 13px; min-width: 0;
}
.tool > summary::-webkit-details-marker { display: none; }
.tool .kind { flex: none; }
.tool .title { font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.tool .body { padding: 0 12px 10px; }
.tool .body pre { max-height: 360px; overflow: auto; margin: 6px 0 0; }
.state { font-size: 12px; flex: none; }
.state.running { color: var(--accent); }
.state.completed { color: var(--ok); }
.state.failed, .state.declined { color: var(--err); }
.state.interrupted { color: var(--warn); }

.approval {
  border: 2px solid var(--warn); border-radius: var(--radius); padding: 12px 14px; margin: 10px auto;
  background: var(--panel);
}
.approval h4 { margin: 0 0 6px; font-size: 14px; }
.approval .title { font-family: var(--mono); font-size: 13px; overflow-wrap: anywhere; }
.approval .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.approval.resolved { border-color: var(--line); }
.approval.resolved .actions { display: none; }
.approval .result { font-size: 13px; color: var(--muted); margin-top: 6px; }
.diff .add { color: var(--ok); }
.diff .del { color: var(--err); }
.diff .hunk { color: var(--accent); }

.turn-end { font-size: 12px; color: var(--muted); margin: 6px auto 18px; display: flex; gap: 10px; flex-wrap: wrap; }
.turn-end.failed { color: var(--err); }
.turn-end.interrupted { color: var(--warn); }
.notice { font-size: 13px; padding: 6px 10px; border-radius: 8px; background: var(--code-bg); margin: 8px auto; white-space: pre-wrap; }
.notice.error { color: var(--err); }
.notice.warning { color: var(--warn); }

/* 입력창: 글 입력칸 아래에 모델 선택과 보내기 버튼이 있는 상자 */
.composer { padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer-box {
  max-width: 860px; margin: 0 auto; border: 1px solid var(--line); border-radius: 14px; background: var(--panel);
  padding: 8px 8px 6px;
}
.composer-box:focus-within { border-color: var(--accent); }
.composer textarea { border: 0; padding: 6px 8px; max-height: 40vh; min-height: 40px; background: transparent; }
.composer textarea:focus { outline: none; }
.composer-bar { display: flex; align-items: center; gap: 8px; }
.composer-bar .spacer { flex: 1; }
.composer button { flex: none; white-space: nowrap; }
.pill {
  border: 0; border-radius: 999px; padding: 5px 12px; font-size: 13px; background: var(--bg); color: var(--text);
  max-width: 60%; overflow: hidden; text-overflow: ellipsis;
}
.pill .sub { color: var(--muted); }
.pill.perm.full_auto { background: var(--err); color: #fff; }
.pill.perm.auto_edit { color: var(--accent); }
.perm-menu { width: min(300px, calc(100vw - 24px)); }
.menu-note.muted-note { color: var(--muted); }
.pill:disabled { opacity: .6; }

/* 모델 선택 메뉴 */
.model-menu {
  position: fixed; z-index: 30; width: min(320px, calc(100vw - 24px)); max-height: min(70vh, 560px); overflow-y: auto;
  padding: 8px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .2);
}
.menu-title { font-size: 12px; color: var(--muted); padding: 4px 8px; }
.menu-options { display: grid; }
.menu-options button, .menu-more {
  display: flex; align-items: center; gap: 8px; border: 0; border-radius: 8px; padding: 7px 8px; text-align: left;
  background: transparent; font-size: 14px; width: 100%;
}
.menu-options button:hover, .menu-more:hover { background: var(--bg); }
.menu-options .label { flex: 1; min-width: 0; }
.menu-options .label small { display: block; color: var(--muted); font-size: 12px; }
.menu-options .check { color: var(--accent); flex: none; }
.menu-more { color: var(--muted); font-size: 13px; }
.menu-sep { border-top: 1px solid var(--line); margin: 6px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 2px 8px 6px; }
.chips button { border-radius: 999px; padding: 4px 11px; font-size: 13px; }
.chips button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.menu-toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 8px; cursor: pointer; }
.menu-toggle span { display: grid; gap: 2px; }
.menu-toggle small { color: var(--muted); font-size: 12px; }
.menu-toggle input { width: 38px; height: 20px; flex: none; accent-color: var(--accent); }
.menu-note { margin: 4px 8px 2px; font-size: 12px; color: var(--warn); min-height: 0; }
.menu-note:empty { display: none; }
.dialog-form label.check { display: flex; align-items: center; gap: 8px; }
.dialog-form label.check input { width: auto; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea:focus, input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* 대화상자, 알림 */
dialog {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--text);
  width: min(460px, calc(100% - 32px)); padding: 0;
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-form { display: grid; gap: 12px; padding: 20px; }
.dialog-form h3 { margin: 0; }
.dialog-form label { display: grid; gap: 4px; font-size: 14px; }
.agent-pick { border: 0; padding: 0; margin: 0; display: flex; gap: 16px; }
.agent-pick legend { font-size: 14px; margin-bottom: 4px; padding: 0; }
.agent-pick label { display: flex; align-items: center; gap: 6px; }
.agent-pick input { width: auto; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 20;
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 8px; max-width: calc(100% - 32px);
}

/* 모바일: 사이드바는 서랍으로 */
@media (max-width: 760px) {
  #menu { display: inline-block; }
  .sidebar {
    position: fixed; z-index: 10; inset: 0 auto 0 0; width: min(300px, 86vw);
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .sidebar.open { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 9; background: rgba(0, 0, 0, .35); }
  .messages { padding: 12px 12px 24px; }
  .main-head { padding: 8px 10px; }
  .composer { padding-left: 8px; padding-right: 8px; }
  .pill { max-width: 55%; }
  #rate-limit { display: none; }
  .meta { flex-wrap: nowrap; overflow: hidden; white-space: nowrap; }
  .meta .path { display: none; }
}
