:root {
  --bg: #f7f6f3; --panel: #ffffff; --line: #e6e3dc; --text: #1d1c1a; --muted: #7a766e;
  --accent: #c96442; --clay: #c96442; --ok: #2f8f5b; --warn: #b7791f; --bad: #c0392b; --info: #3b6fb6;
  --bubble: #efece6; --mine: #1d1c1a; --mine-text: #fff; --code: #f1efea;
  --add: #e3f4e8; --del: #fbe6e3; --r: 14px;
  color-scheme: light dark;
}
/* 테마: 설정에서 고르면 data-theme, 아니면 시스템 따름 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --bg: #161615; --panel: #1f1f1d; --line: #33322f; --text: #ecebe7; --muted: #9b978e;
    --bubble: #2a2927; --mine: #ecebe7; --mine-text: #161615; --code: #262523; --add: #1d3527; --del: #3d2220; }
}
:root[data-theme="dark"] { --bg: #161615; --panel: #1f1f1d; --line: #33322f; --text: #ecebe7; --muted: #9b978e;
    --bubble: #2a2927; --mine: #ecebe7; --mine-text: #161615; --code: #262523; --add: #1d3527; --del: #3d2220; color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans KR", sans-serif; -webkit-text-size-adjust: 100%; }
#app { height: 100dvh; display: flex; flex-direction: column; max-width: 760px; margin: 0 auto; }
button { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; cursor: pointer; }
button.primary { background: var(--text); color: var(--bg); border-color: var(--text); }
button.danger { color: var(--bad); }
button.ghost { background: none; border: none; padding: 6px 8px; }
button:disabled { opacity: .45; cursor: default; }
input, select, textarea { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; width: 100%; }
textarea { resize: none; }
label { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { margin: auto; text-align: center; padding: 24px; }
.row { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

header { display: flex; align-items: center; gap: 8px; padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--bg); position: sticky; top: 0; z-index: 2; }
header h1 { font-size: 17px; margin: 0; font-weight: 600; }
main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.pad { padding: 12px 14px; }
footer { position: relative; border-top: 1px solid var(--line); padding: 8px 10px; padding-bottom: max(8px, env(safe-area-inset-bottom)); background: var(--bg); }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); flex: none; }
.dot.on { background: var(--ok); } .dot.off { background: var(--bad); }
.conn { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; }

.group { margin: 14px 0 6px; font-size: 12.5px; color: var(--muted); font-weight: 600; padding: 0 2px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 11px 12px; }
.list .card { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; cursor: pointer; }
.badge { font-size: 11.5px; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--line); white-space: nowrap; flex: none; }
.badge.working { color: var(--info); border-color: currentColor; }
.badge.waiting { color: var(--warn); border-color: currentColor; }
.badge.blocked { color: var(--bad); border-color: currentColor; }
.badge.done { color: var(--ok); border-color: currentColor; }

.agent-ico { width: 30px; height: 30px; flex: none; display: block; }
.agent-ico.sm { width: 22px; height: 22px; }

.statusbar { font-size: 12.5px; padding: 6px 12px; border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: center; background: var(--panel); }
.statusbar.working { color: var(--info); } .statusbar.waiting { color: var(--warn); } .statusbar.blocked { color: var(--bad); }

.chat { padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; padding: 9px 12px; border-radius: 16px; background: var(--bubble); white-space: pre-wrap; word-break: break-word; }
.msg.mine { align-self: flex-end; background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 5px; }
.msg.agent { align-self: flex-start; border-bottom-left-radius: 5px; }
.msg pre { background: var(--code); color: var(--text); padding: 8px; border-radius: 8px; overflow-x: auto; margin: 6px 0; font-size: 12.5px; white-space: pre; }
.delivery { align-self: flex-end; font-size: 11.5px; color: var(--muted); margin-top: -6px; }
.delivery.bad { color: var(--bad); }
.tool { align-self: stretch; font-size: 13px; }
.tool summary { cursor: pointer; color: var(--muted); list-style: none; }
.tool summary::-webkit-details-marker { display: none; }
.tool pre, .diff { font: 12px/1.45 ui-monospace, "Cascadia Mono", Consolas, monospace; background: var(--code); border-radius: 8px; padding: 8px; overflow-x: auto; margin: 6px 0 0; white-space: pre; }
.diff .a { background: var(--add); display: block; } .diff .d { background: var(--del); display: block; }
.diff .h { color: var(--muted); display: block; }
.ctitle { font-weight: 600; font-size: 13.5px; margin-bottom: 4px; display: flex; gap: 6px; align-items: center; }
.approval { border-color: var(--warn); }
.approval .actions { display: flex; gap: 8px; margin-top: 10px; }
.approval .actions button { flex: 1; }
.blocked-card { border-color: var(--bad); }
.blocked-card pre { white-space: pre-wrap; word-break: break-word; }
.hint { font-size: 13px; color: var(--muted); margin-top: 6px; }
.imgcard img { max-width: 100%; border-radius: 10px; display: block; margin-top: 6px; background: var(--code); }
.sys { align-self: center; font-size: 12px; color: var(--muted); }

.composer { display: flex; gap: 6px; align-items: flex-end; }
.composer button { white-space: nowrap; flex: none; }
.composer textarea { max-height: 160px; min-height: 42px; }
.attach-list { font-size: 12px; color: var(--muted); margin-bottom: 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.attach-list span { background: var(--panel); border: 1px solid var(--line); border-radius: 99px; padding: 2px 8px; }

.seg { display: flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.seg button { flex: 1; border: none; border-radius: 0; display: flex; gap: 8px; align-items: center; justify-content: center; padding: 10px; }
.seg button.on { background: var(--text); color: var(--bg); }
.code6 { font: 600 32px/1 ui-monospace, Consolas, monospace; letter-spacing: 6px; text-align: center; margin: 18px 0; }
.banner { background: var(--del); color: var(--bad); font-size: 13px; padding: 8px 12px; }
.err { color: var(--bad); font-size: 13px; white-space: pre-wrap; margin-top: 8px; }

/* ---- 채팅: 데스크톱 앱 느낌 ---- */
.shell { display: contents; }
.msg.pending { opacity: .7; }
.agent-text { align-self: stretch; max-width: 100%; word-break: break-word; line-height: 1.6; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .7em; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { margin: 1em 0 .45em; line-height: 1.35; font-weight: 650; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.18em; } .md h3 { font-size: 1.07em; } .md h4, .md h5, .md h6 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .7em; padding-left: 1.4em; } .md li { margin: .2em 0; } .md li > ul, .md li > ol { margin: .2em 0; }
.md blockquote { margin: 0 0 .7em; padding: 2px 0 2px 12px; border-left: 3px solid var(--line); color: var(--muted); }
.md hr { border: none; border-top: 1px solid var(--line); margin: 1em 0; }
.md a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.md code { font: .88em ui-monospace, "Cascadia Mono", Consolas, monospace; background: var(--code); border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; }
.codeblock { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin: 0 0 .7em; background: var(--code); }
.cb-head { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px 2px 12px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.cb-copy { font-size: 12px; color: var(--muted); padding: 4px 8px; }
.codeblock pre { margin: 0; padding: 10px 12px; overflow-x: auto; font: 12.5px/1.5 ui-monospace, "Cascadia Mono", Consolas, monospace; }
.codeblock code { background: none; border: none; padding: 0; font: inherit; }
.tablewrap { overflow-x: auto; margin: 0 0 .7em; }
.md table { border-collapse: collapse; font-size: 13.5px; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.md th { background: var(--code); font-weight: 600; }

/* 도구 사용: 한 줄 요약, 눌러서 펼침 */
.tools { align-self: stretch; font-size: 13px; color: var(--muted); border-left: 2px solid var(--line); padding-left: 10px; }
.tools summary { cursor: pointer; list-style: none; display: flex; gap: 6px; align-items: center; min-width: 0; padding: 2px 0; }
.tools summary::-webkit-details-marker { display: none; }
.tools summary::before { content: '›'; display: inline-block; width: 10px; transition: transform .15s; flex: none; }
.tools details[open] > summary::before { transform: rotate(90deg); }
.step { margin: 2px 0; } .step.flat { display: flex; gap: 6px; align-items: center; padding: 2px 0 2px 16px; min-width: 0; }
.tools > details > .step { margin-left: 14px; }
.verb { color: var(--text); font-weight: 600; flex: none; }
.target { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12px; }
.dcount { flex: none; font-size: 12px; } .plus { color: var(--ok); } .minus { color: var(--bad); }
.fail { color: var(--bad); flex: none; font-size: 12px; }
.tools .diff, .tools pre.out { margin: 4px 0 6px 16px; max-height: 320px; overflow: auto; }
pre.out { font: 12px/1.45 ui-monospace, "Cascadia Mono", Consolas, monospace; background: var(--code); border-radius: 8px; padding: 8px; white-space: pre-wrap; word-break: break-word; margin: 6px 0 0; color: var(--text); }
.working { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.spinner { width: 12px; height: 12px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
.spinner.sm { width: 9px; height: 9px; border-width: 1.5px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- 긴 글은 화면 안에서 줄바꿈 (가로 스크롤 없음) ---- */
html, body { overflow-x: hidden; }
.agent-text, .msg, .card, .hint, .err { overflow-wrap: anywhere; }
.codeblock pre, .tool pre, .diff, pre.out, .blocked-card pre { white-space: pre-wrap; overflow-wrap: anywhere; overflow-x: hidden; }
.tablewrap { overflow-x: visible; }
.md table { width: 100%; }
.md th, .md td { overflow-wrap: anywhere; }
.md code { overflow-wrap: anywhere; }

/* ---- 부드러운 반응 ---- */
button { transition: background-color .15s, color .15s, border-color .15s, transform .1s, opacity .15s; -webkit-tap-highlight-color: transparent; }
button:active:not(:disabled) { transform: scale(.97); }
.list .card { transition: transform .12s, background-color .15s; -webkit-touch-callout: none; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
.list .card:active { transform: scale(.985); }
.statusbar, .badge, .dot { transition: color .25s, background-color .25s, border-color .25s; }
.enter { animation: msg-in .24s cubic-bezier(.2, .8, .2, 1); }
@keyframes msg-in { from { opacity: 0; transform: translateY(8px); } }
details.opening > :not(summary) { animation: fold-in .2s ease-out; }
@keyframes fold-in { from { opacity: 0; transform: translateY(-4px); } }

/* 화면 전환 */
::view-transition-old(root), ::view-transition-new(root) { animation-duration: .26s; animation-timing-function: cubic-bezier(.2, .8, .2, 1); }
html[data-nav="forward"]::view-transition-old(root) { animation-name: vt-out, vt-to-left; }
html[data-nav="forward"]::view-transition-new(root) { animation-name: vt-in, vt-from-right; }
html[data-nav="back"]::view-transition-old(root) { animation-name: vt-out, vt-to-right; }
html[data-nav="back"]::view-transition-new(root) { animation-name: vt-in, vt-from-left; }
@keyframes vt-in { from { opacity: 0; } }
@keyframes vt-out { to { opacity: 0; } }
@keyframes vt-from-right { from { transform: translateX(32px); } }
@keyframes vt-to-left { to { transform: translateX(-32px); } }
@keyframes vt-from-left { from { transform: translateX(-32px); } }
@keyframes vt-to-right { to { transform: translateX(32px); } }

/* 아래에서 올라오는 시트 */
.sheet-ov { position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, 0); display: flex; align-items: flex-end; justify-content: center; transition: background-color .22s; }
.sheet-ov.open { background: rgba(0, 0, 0, .38); }
.sheet { width: 100%; max-width: 760px; max-height: 88dvh; overflow-y: auto; background: var(--bg); border-radius: 18px 18px 0 0; padding: 8px 16px max(16px, env(safe-area-inset-bottom));
  transform: translateY(100%); transition: transform .26s cubic-bezier(.2, .8, .2, 1); box-shadow: 0 -6px 24px rgba(0, 0, 0, .12); }
.sheet-ov.open .sheet { transform: none; }
.sheet-grip { width: 38px; height: 4px; border-radius: 99px; background: var(--line); margin: 2px auto 12px; }
.opts { display: flex; flex-direction: column; gap: 6px; }
.opt { text-align: left; padding: 10px 12px; border-radius: 12px; }
.opt.on { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); }
.opt-t { font-weight: 600; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
/* 테마 바꿀 때 색이 부드럽게 */
html, body, header, footer, .card, .statusbar, input, textarea, select { transition: background-color .25s, color .25s, border-color .25s; }

/* 최신 메시지로 이동 버튼 (위로 올려 보고 있을 때만) */
.jump { position: absolute; right: 12px; top: -52px; width: 40px; height: 40px; padding: 0; border-radius: 50%; font-size: 18px; line-height: 1;
  box-shadow: 0 2px 10px rgba(0,0,0,.18); opacity: 0; transform: translateY(8px) scale(.9); pointer-events: none; transition: opacity .18s, transform .18s; z-index: 3; }
.jump.show { opacity: 1; transform: none; pointer-events: auto; }
.jump.new::after { content: ''; position: absolute; top: 2px; right: 2px; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); border: 2px solid var(--panel); }
@media (prefers-reduced-motion: reduce) { .jump { transition: none; } }

/* 이미지 전체 화면 보기 */
.imgcard img.zoomable { cursor: zoom-in; }
.imgcard video { width: 100%; max-height: 60dvh; border-radius: 10px; margin-top: 6px; background: #000; display: block; }
.imgcard audio { width: 100%; margin-top: 6px; display: block; }
.imgcard .play { margin-top: 6px; }
.sheet-ov.viewer { flex-direction: column; align-items: stretch; background: rgba(0, 0, 0, .94); opacity: 0; transition: opacity .2s; }
.sheet-ov.viewer.open { opacity: 1; background: rgba(0, 0, 0, .94); }
.vw-bar { display: flex; gap: 6px; align-items: center; color: #fff; padding: max(8px, env(safe-area-inset-top)) 10px 8px; font-size: 14px; }
.vw-bar button { color: #fff; background: rgba(255, 255, 255, .12); border-color: transparent; }
.vw-stage { flex: 1; min-height: 0; overflow: hidden; display: flex; align-items: center; justify-content: center; touch-action: none; padding-bottom: env(safe-area-inset-bottom); }
.vw-stage img { max-width: 100%; max-height: 100%; transform-origin: center; user-select: none; -webkit-user-select: none; will-change: transform; }
@media (prefers-reduced-motion: reduce) { .sheet-ov.viewer { transition: none; } }
