/**
 * layout.css - 전체 애플리케이션 3분할 셸 및 상단 바, 반응형 레이아웃
 */

.app-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background-color: var(--bg-app);
}

/* ==========================================================================
   상단 헤더
   ========================================================================== */
.app-header {
  height: var(--header-height);
  min-height: var(--header-height);
  flex-shrink: 0;
  background-color: var(--bg-header);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 12px;
  z-index: 20;
  user-select: none;
  min-width: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
  color: var(--text-primary);
  white-space: nowrap;
}

.brand-badge {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 2px 6px;
  border-radius: var(--radius-xs);
  background-color: var(--accent-subtle);
  color: var(--accent-text);
  border: 1px solid var(--border-subtle);
  text-transform: uppercase;
}

.header-center {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  justify-content: center;
  max-width: 650px;
  min-width: 0;
}

.connection-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 500;
  border: 1px solid var(--border-subtle);
  background-color: var(--bg-surface);
  white-space: nowrap;
  transition: all var(--transition-fast);
}

.connection-pill.connected {
  border-color: rgba(16, 185, 129, 0.3);
  color: var(--success-text);
}

.connection-pill.disconnected {
  border-color: rgba(239, 68, 68, 0.3);
  color: var(--danger-text);
}

.connection-pill.checking {
  border-color: rgba(59, 130, 246, 0.3);
  color: var(--accent-text);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
}

.connection-pill.connected .status-dot {
  background-color: var(--success-primary);
  box-shadow: 0 0 6px var(--success-primary);
}

.connection-pill.disconnected .status-dot {
  background-color: var(--danger-primary);
}

.connection-pill.checking .status-dot {
  background-color: var(--accent-primary);
  animation: pulse-dot 1.2s infinite ease-in-out;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 0.3; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.15); }
}

.model-selector-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  max-width: 320px;
  min-width: 140px;
}

.model-select {
  flex: 1;
  height: 32px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-normal);
  border-radius: var(--radius-sm);
  padding: 0 10px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-primary);
  cursor: pointer;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  transition: border-color var(--transition-fast), background-color var(--transition-fast);
}

.model-select:hover {
  background-color: var(--bg-hover);
  border-color: var(--border-focus);
}

.model-select option {
  background-color: var(--bg-surface-elevated);
  color: var(--text-primary);
}

.token-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  color: var(--text-secondary);
  background-color: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  font-family: var(--font-mono);
  white-space: nowrap;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  border: 1px solid transparent;
  background-color: transparent;
  transition: all var(--transition-fast);
  position: relative;
}

.icon-btn:hover {
  color: var(--text-primary);
  background-color: var(--bg-hover);
  border-color: var(--border-subtle);
}

.icon-btn.active {
  color: var(--accent-text);
  background-color: var(--accent-subtle);
  border-color: rgba(59, 130, 246, 0.3);
}

/* ==========================================================================
   본문 작업영역 (3분할 셸)
   ========================================================================== */
.app-workspace {
  display: flex;
  flex: 1;
  min-height: 0;
  width: 100%;
  height: auto;
  overflow: hidden;
  position: relative;
}

/* 사이드바 */
.app-sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  flex: 0 0 var(--sidebar-width);
  height: 100%;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  transition: transform var(--transition-normal), width var(--transition-normal);
  z-index: 15;
  overflow: hidden;
}

.app-sidebar.collapsed {
  width: 0;
  min-width: 0;
  flex-basis: 0;
  transform: translateX(-100%);
  border-right: none;
}

/* 대화 메인 영역 */
.app-chat {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-app);
  position: relative;
  min-width: 0;
  overflow: hidden;
}

/* 설정 패널 */
.app-settings {
  width: var(--settings-width);
  min-width: var(--settings-width);
  flex: 0 0 var(--settings-width);
  height: 100%;
  background-color: var(--bg-surface);
  border-left: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  transition: transform var(--transition-normal), width var(--transition-normal);
  z-index: 15;
  overflow-y: auto;
}

.app-settings.closed {
  width: 0;
  min-width: 0;
  flex-basis: 0;
  transform: translateX(100%);
  border-left: none;
}

/* 모달 백드롭 (태블릿/모바일) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background-color: var(--backdrop-color);
  backdrop-filter: blur(2px);
  z-index: 14;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-normal);
}

.modal-backdrop.show {
  opacity: 1;
  pointer-events: auto;
}

/* ==========================================================================
   반응형 브레이크포인트
   ========================================================================== */
/* 태블릿 (<= 1024px) */
@media (max-width: 1024px) {
  .app-settings {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    box-shadow: var(--shadow-popover);
  }

  .app-settings.closed {
    transform: translateX(100%);
  }

  .header-center {
    max-width: 480px;
  }
}

/* 모바일 (<= 768px) - 2열 헤더 및 초소형 화면(320px~) 방어 */
@media (max-width: 768px) {
  :root {
    --header-height: 88px;
  }

  /* 헤더 2열 그리드 배치: Row 1 (좌측/연결상태/우측), Row 2 (모델 셀렉터 전체 너비) */
  .app-header {
    display: grid;
    grid-template-columns: auto 1fr auto;
    grid-template-rows: 34px 34px;
    align-items: center;
    padding: 7px 12px;
    row-gap: 6px;
    column-gap: 8px;
    height: var(--header-height);
    min-height: var(--header-height);
    max-height: var(--header-height);
    overflow: hidden;
  }

  /* Row 1 좌측: 사이드바 토글 및 컴팩트 브랜드(로봇 아이콘 + Local 뱃지) */
  .header-left {
    grid-column: 1;
    grid-row: 1;
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
  }

  .header-left .icon-btn {
    width: 32px;
    height: 32px;
    min-width: 32px;
    padding: 0;
  }

  .header-brand {
    display: flex;
    align-items: center;
    gap: 4px;
    min-width: 0;
    user-select: none;
  }

  .header-brand svg {
    flex-shrink: 0;
    width: 17px;
    height: 17px;
  }

  .header-brand span:not(.brand-badge) {
    display: none;
  }

  .header-brand .brand-badge {
    font-size: 9px;
    font-weight: 700;
    padding: 1px 5px;
    letter-spacing: 0.3px;
    white-space: nowrap;
  }

  /* 중앙 컨테이너 해제: 자식 엘리먼트를 .app-header 그리드 셀에 직접 매핑 */
  .header-center {
    display: contents;
  }

  /* Row 1 중앙: 서버 연결 상태 알약 */
  #connection-pill {
    grid-column: 2;
    grid-row: 1;
    justify-self: center;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 24px;
    padding: 2px 8px;
    font-size: 11px;
    max-width: 120px;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #connection-status-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Row 1 우측: 테마 전환 및 설정 패널 토글 버튼 */
  .header-right {
    grid-column: 3;
    grid-row: 1;
    justify-self: end;
    display: flex;
    align-items: center;
    gap: 4px;
  }

  .header-right .icon-btn {
    width: 32px;
    height: 32px;
    min-width: 32px;
    padding: 0;
  }

  /* Row 2: 모델 선택 드롭다운 (잔여/전체 너비 확보) + 새로고침 액션 버튼 */
  .model-selector-wrapper {
    grid-column: 1 / -1;
    grid-row: 2;
    display: flex;
    align-items: center;
    gap: 6px;
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .model-select {
    flex: 1;
    min-width: 0;
    width: 0;
    height: 34px;
    font-size: 12px;
    padding: 0 8px;
    border-radius: var(--radius-sm);
  }

  #refresh-models-btn {
    width: 34px;
    height: 34px;
    min-width: 34px;
    padding: 0;
    flex-shrink: 0;
  }

  /* 모바일 화면에서 누적 토큰 알약 숨김 */
  .token-pill {
    display: none !important;
  }

  /* 사이드바 드로어 (320px 기기에서도 안전 여백 44px 확보) */
  .app-sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(290px, calc(100vw - 44px));
    min-width: 0;
    flex-basis: auto;
    box-shadow: var(--shadow-popover);
  }

  .app-sidebar.collapsed {
    transform: translateX(-100%);
    width: min(290px, calc(100vw - 44px));
    min-width: 0;
    flex-basis: auto;
  }

  /* 설정 패널 드로어 */
  .app-settings {
    width: 100%;
    max-width: min(360px, calc(100vw - 44px));
    min-width: 0;
    flex-basis: auto;
  }

  /* 모바일 스크롤 및 입력창 여백 최적화 */
  .messages-scroll-area {
    padding: 14px 10px;
  }

  .chat-input-area {
    padding: 8px 10px 12px 10px;
  }

  .input-container {
    padding: 8px 12px;
  }

  .keyboard-shortcut-hint {
    display: none;
  }
}
