/* 세로 스크롤바 공간을 항상 예약 — 콘텐츠 로드로 스크롤바가 생길 때 viewport
   폭이 ~15px 줄며 발생하던 가로 리플로우(특히 auto-fit 그리드 열 재계산)를 제거. */
html { scrollbar-gutter: stable; }

/* Main Panel */
.main-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    margin-left: 0;
    margin-right: 0;
    transition: margin-left var(--transition), margin-right var(--transition-slow);
    background: var(--color-bg);
    overflow-x: hidden;
}

/* Main Header — 베이스 (모바일 기본): viewport 상단 고정. nested scroll
   container (chat-layout 등) 에서도 sticky 처럼 사라지지 않고 항상 보이게.
   데스크탑은 아래 미디어쿼리에서 left 와 z-index 만 덮어씀. */
.main-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 3rem;
    padding: 0 1rem;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 5;
    flex-shrink: 0;
}

/* ─── Desktop 룰 ────────────────────────────────────────────────────────────
   규칙 1: 우측 패널과 중앙은 유기적으로 움직임. body.right-open 일 때
           main-panel 이 margin-right 만큼 축소 → 안의 채팅 영역 함께 축소.
   규칙 2: 최상단 헤더는 우측 패널과 무관. position:fixed 로 분리해 viewport
           풀폭 (좌측 패널 옆부터 우측 끝까지). main-panel 의 margin-right 가
           바뀌어도 헤더는 영향 없음.
*/
@media (min-width: 768px) {
    .main-panel { margin-left: var(--left-width); }
    .main-panel.no-nav { margin-left: 0; }

    /* 규칙 1: 우측 패널 열림 → main-panel 좁아짐 */
    body.right-open .main-panel {
        margin-right: var(--right-width, 560px);
    }

    /* 규칙 2: 헤더는 데스크탑에선 좌측 패널 우측부터 시작 (모바일 베이스의
       left:0 / z-index:5 를 덮어씀). position:fixed 자체는 베이스 이미 적용. */
    .main-panel > .main-header {
        left: var(--left-width);
        z-index: 100;
    }
    .main-panel.no-nav > .main-header {
        left: 0;
    }

    /* NOTE: .main-content padding-top 양보 룰은 base .main-content { padding: 1.5rem }
       이 더 *늦은* line 에 정의돼 shorthand 가 padding-top 을 덮어쓰는 문제 때문에
       여기 두지 않고, base rule 뒤쪽 (.main-content.full-page 옆) 에 둠. */
}

.header-left {
    display: flex;
    align-items: center;
    min-width: 2.5rem;
}

.header-center {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    min-width: 0;
}

.header-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--color-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
}

.header-title-icon {
    flex-shrink: 0;
}

.folder-icon {
    color: #e2e8f0;
}

.header-right {
    display: flex;
    align-items: center;
    min-width: 2.5rem;
    justify-content: flex-end;
}

.toggle-left-panel {
    display: none;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
    font-size: 1.25rem;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 0.375rem;
    color: var(--color-text);
    transition: background var(--transition);
}

.toggle-left-panel:hover {
    background: var(--color-bg-secondary);
}

@media (max-width: 767px) {
    .toggle-left-panel {
        display: flex;
    }
}

/* 헤더 프로필 link/avatar/name 룰은 좌측 사이드바 chip 으로 이전 (left.css 의
   .left-profile-* 참조). 헤더 우측엔 toggle 버튼만 남음. */

.toggle-right-panel {
    display: none;  /* 기본 숨김 — body.has-artifact 일 때만 노출 */
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--color-text-secondary);
    border-radius: 0.375rem;
    transition: all var(--transition);
}

/* 아티팩트가 도착하면 chat.js 가 body.has-artifact 부여 → 토글 버튼 노출 */
body.has-artifact .toggle-right-panel {
    display: inline-flex;
}

.toggle-right-panel:hover {
    background: var(--color-bg-secondary);
    color: var(--color-text);
}

/* 패널 열림 시 chevron 180° 회전 (|<- → ->|) */
.toggle-right-panel svg { transition: transform var(--transition); }
body.right-open .toggle-right-panel svg { transform: rotate(180deg); }

/* Main Content */
.main-content {
    flex: 1;
    padding: 1.5rem;
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
}

.main-content.full-page {
    padding: 0;
    max-width: none;
}

/* 모바일/데스크탑 모두 .main-header 가 fixed (3rem 높이) 라 모든 콘텐츠가 그
   만큼 상단 양보. base .main-content { padding: 1.5rem } / .full-page { padding: 0 }
   shorthand 보다 *늦게* 정의해야 padding-top override 가 이김 (specificity 동일
   하면 후순위 win). 이전에 @media block 을 base 앞에 두어 적용 안 되던 버그 ↑ */
@media (max-width: 767px) {
    .main-content {
        padding: 1rem 0.75rem;
        padding-top: 2.5rem;
    }
    .main-content.full-page {
        padding-top: 3rem;
    }
}
/* 초협폭(≤480px): 좌우 거터 추가 축소 (콘텐츠 폭 확보). 상하는 유지. */
@media (max-width: 480px) {
    .main-content {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
}
@media (min-width: 768px) {
    .main-content {
        padding-top: 2.5rem;
    }
    .main-content.full-page {
        padding-top: 3rem;
    }
}

/* settings-page (계정/설정/통계) 는 .settings-page-body 가 height:calc(100vh - 3rem)
   + overflow:auto 인 *중첩 스크롤 컨테이너*. base 의 padding-top 2.5rem 으로는 컨테이너
   상단이 3rem 헤더보다 0.5rem 위에서 시작 → 스크롤 시 상단 콘텐츠가 fixed 헤더 밑으로
   깔려 가린다. 스크롤 컨테이너 상단을 헤더 바닥(3rem)에 정확히 맞춰 클리핑 제거.
   (내용이 짧아 스크롤 안 생기던 account 페이지엔 무해 — 하단 0.5rem gap 도 함께 정렬됨) */
.main-content.settings-page {
    padding-top: 3rem;
}

/* Right Panel — 메인 헤더(3rem) 아래에서 시작해 채팅 영역과 유기적으로 작동.
   폭은 --right-width 변수로 (drag handle 로 조절, localStorage 저장). 중앙이
   양보하는 *유기적* 모드라 box-shadow 없이 left border 만으로 구분 — overlay
   모드 시절의 떠있는 패널 느낌 제거. */
.right-panel {
    position: fixed;
    right: 0;
    top: 3rem;
    bottom: 0;
    width: var(--right-width, 560px);
    background: var(--color-bg);
    border-left: 1px solid var(--color-border);
    transform: translateX(100%);
    transition: transform var(--transition-slow), width var(--transition-slow);
    z-index: 1000;
    display: flex;
    flex-direction: column;
}

/* 리사이즈 중엔 부드러운 transition 끔 — 마우스 추적이 즉각적이게 */
body.right-resizing .right-panel,
body.right-resizing .main-panel,
body.right-resizing .main-header {
    transition: none !important;
}

/* 리사이즈 중 전역 가드:
   - col-resize 커서를 페이지 전체에 고정 (커서가 패널 안 요소 위에서 깜빡이지 않게)
   - 텍스트 선택 잠금 (드래그가 본문 텍스트를 selection 으로 긁지 않게 — JS userSelect 토글 대체)
   - iframe pointer-events 차단 — setPointerCapture 의 보강. 캡처가 표준대로
     동작하면 불필요하지만, 일부 sandboxed iframe 에서 capture 가 무력화되는
     케이스(w3c/pointerevents#16)까지 막는 belt-and-suspenders. */
body.right-resizing {
    cursor: col-resize !important;
    user-select: none;
    -webkit-user-select: none;
}
body.right-resizing * { cursor: col-resize !important; }
body.right-resizing iframe { pointer-events: none; }

/* drag handle — 얇은 1px hairline + 넓은 16px 투명 그랩존. 평소엔 가는 선,
   hover/drag 시 굵어지고 진해짐. (예전 12px 띠+좌우 보더+4px 그립바는 시각적으로
   너무 두꺼웠음 — 보이는 선은 가늘게, 끌기 편한 hit-area 는 투명하게 넓게 유지.) */
.right-panel-resize-handle {
    position: absolute;
    left: -8px;            /* 16px 그랩존의 절반만큼 패널 바깥 overhang → 중심이 분할선 위 */
    top: 0;
    bottom: 0;
    width: 16px;           /* 투명 hit-area (끌기 편하게 넓게) */
    cursor: col-resize;
    z-index: 1001;
    background: transparent;   /* 그랩존 자체는 투명 — 보이는 건 ::after hairline 뿐 */
    touch-action: none;    /* 터치 드래그가 페이지 스크롤로 새지 않게 (pointer 캡처 전제) */
}
.right-panel-resize-handle::after {
    content: '';
    position: absolute;
    left: 7.5px;           /* 그랩존(16px) 중심 = 분할선 위의 1px 선 */
    top: 0;
    bottom: 0;
    width: 1px;            /* 평소엔 1px hairline */
    background: rgba(148, 163, 184, 0.45);
    transition: background 0.15s, width 0.15s, left 0.15s;
}
.right-panel-resize-handle:hover::after {
    width: 2px;
    left: 7px;             /* 2px 선을 분할선 중심에 유지 */
    background: rgba(51, 65, 85, 0.95);
}
body.right-resizing .right-panel-resize-handle::after {
    width: 3px;
    left: 6.5px;           /* 3px 선을 분할선 중심에 유지 */
    background: rgba(15, 23, 42, 1);
}

@media (max-width: 767px) {
    /* 모바일은 전폭 패널이라 리사이즈 의미 없음 */
    .right-panel-resize-handle { display: none; }
}

/* 리사이즈 핸들은 패널이 *열렸을* 때만 노출. 닫힌 패널은 translateX(100%) 로
   화면 밖에 있지만 핸들이 left:-6px 오버행이라 오른쪽 가장자리에 6px 잔상으로
   삐져나오던 것 제거. 닫힌 패널은 끌 게 없으니 핸들도 숨김 (아티팩트 유무 무관). */
.right-panel:not(.open) .right-panel-resize-handle { display: none; }

.right-panel.open {
    transform: translateX(0);
}

.right-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
}

.right-panel-title {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--color-text);
}

.right-panel-close {
    background: none;
    border: none;
    font-size: 1.375rem;
    color: var(--color-text-muted);
    cursor: pointer;
    line-height: 1;
    padding: 0.125rem 0.25rem;
    flex-shrink: 0;
    transition: color var(--transition);
}

.right-panel-close:hover {
    color: var(--color-text);
}

.right-panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.25rem;
}

.right-panel-empty {
    color: var(--color-text-muted);
    text-align: center;
    padding: 2rem 0.5rem;
    font-size: 0.875rem;
}

@media (max-width: 767px) {
    .right-panel {
        width: 100%;
    }
    /* 모바일에선 main 패널 margin shift 비활성 (overlay 모드) */
    body.right-open .main-panel {
        margin-right: 0;
    }
    /* 모바일에선 우측 패널이 이미 전폭이라 확대 버튼 의미 없음 */
    .right-panel-expand-btn { display: none !important; }
}

/* 확대 버튼 — 패널 우상단 floating. body.has-artifact 일 때만 노출 (차트가 있어야 의미) */
.right-panel-expand-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid var(--color-border, #e2e8f0);
    border-radius: 6px;
    color: var(--color-text-muted, #64748b);
    cursor: pointer;
    z-index: 5;
    transition: color var(--transition), background var(--transition), border-color var(--transition);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}
body.has-artifact .right-panel-expand-btn {
    display: flex;
}
.right-panel-expand-btn:hover {
    color: var(--color-text, #0f172a);
    border-color: #94a3b8;
    background: #ffffff;
}

/* 와이드 모드 — 확대 버튼으로 우측 패널이 브라우저 폭을 넓게 차지.
   좌측 nav 만 남기고 패널이 나머지 전체를 채움 → 차트가 최대 폭으로.
   데스크탑(≥768px)에서만 의미 (모바일은 이미 전폭). transition 은
   .right-panel 의 기존 transform transition + 아래 width transition. */
@media (min-width: 768px) {
    body.right-wide .right-panel {
        width: calc(100vw - var(--left-width));
    }
    /* 채팅 main 영역은 패널 뒤로 밀려 거의 가려짐 — 차트에 집중. */
    body.right-wide.right-open .main-panel {
        margin-right: calc(100vw - var(--left-width));
    }
    /* 와이드 중엔 드래그 리사이즈 핸들 숨김 (폭은 토글이 관리). */
    body.right-wide .right-panel-resize-handle { display: none; }
}
/* 와이드 상태일 때 확대 버튼 아이콘 회전으로 '축소' 의미 표현. */
.right-panel-expand-btn.is-wide { transform: rotate(180deg); }

/* Global setup banner — UserConfigGuard 누락 안내. base.tmpl 이 main-panel 의
   header 와 main-content 사이에 렌더. amber tone — 차단이 아닌 권유 톤.
   클래스 이름이 `setup-banner-global` 인 이유: settings.tmpl 의 in-panel
   `.setup-banner` (settings.css 가 own) 와 분리해 fixed positioning 규칙이
   in-panel 배너로 leak 되지 않게. 이전엔 같은 클래스로 main.css 의 fixed/z-index:99
   가 in-panel 배너에도 적용돼 API 키 입력창 위를 덮어 클릭/포커스가 막혔다. */
.setup-banner-global {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    background: #fff7ed;
    border-bottom: 1px solid #fed7aa;
    color: #9a3412;
    font-size: 0.85rem;
    line-height: 1.3;
    flex-shrink: 0;
}
.setup-banner-global .setup-banner-icon {
    display: flex;
    align-items: center;
    color: #c2410c;
    flex-shrink: 0;
}
.setup-banner-global .setup-banner-text {
    flex: 1;
    min-width: 0;
}
.setup-banner-global-cta {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    padding: 0.3rem 0.75rem;
    background: #c2410c;
    color: #fff;
    border-radius: 0.375rem;
    font-size: 0.8rem;
    font-weight: 500;
    text-decoration: none;
    white-space: nowrap;
    transition: background var(--transition);
}
.setup-banner-global-cta:hover { background: #9a3412; }

:root { --setup-banner-h: 2.5rem; }
@media (min-width: 768px) {
    .setup-banner-global {
        position: fixed;
        top: 3rem;
        left: var(--left-width);
        right: 0;
        z-index: 99;
    }
    .main-panel.no-nav .setup-banner-global { left: 0; }
    body.has-setup-banner .main-content {
        padding-top: calc(3rem + var(--setup-banner-h) + 1.5rem);
    }
    body.has-setup-banner .main-content.full-page {
        padding-top: calc(3rem + var(--setup-banner-h));
    }
}
@media (max-width: 767px) {
    .setup-banner-global {
        position: fixed;
        top: 3rem;
        left: 0;
        right: 0;
        z-index: 4;
    }
    body.has-setup-banner .main-content {
        padding-top: calc(3rem + var(--setup-banner-h) + 1rem);
    }
    body.has-setup-banner .main-content.full-page {
        padding-top: calc(3rem + var(--setup-banner-h));
    }
}

/* progress-line — single replaceable status line shown under an in-progress
   assistant turn while a long tool (deep_research) runs. Muted, small, italic,
   with a subtle pulse so it reads as "working" without competing with the
   streamed answer text. Removed on stream done/error (chat.js). */
.progress-line {
    margin: 0.25rem 0 0.5rem;
    font-size: 0.8125rem;
    font-style: italic;
    color: var(--color-text-muted, #64748b);
    animation: progress-line-pulse 1.4s ease-in-out infinite;
}
.progress-line::before {
    content: "\22EF  ";
}
@keyframes progress-line-pulse {
    0%, 100% { opacity: 0.55; }
    50%      { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
    .progress-line { animation: none; opacity: 0.85; }
}
