/* ===== 디자인 토큰 ===== */
:root {
    --bg: #f4f5f8;          /* 전체 배경 */
    --surface: #ffffff;     /* 카드·입력창 */
    --sidebar: #1d2433;     /* 사이드바 */
    --ink: #1d2433;         /* 본문 글자 */
    --muted: #6b7385;       /* 보조 글자 */
    --line: #e3e6ec;        /* 테두리 */
    --accent: #4c5fd5;      /* 포인트 색 */
    --accent-hover: #3d4fc0;
    --accent-soft: #eceffd;
    --danger: #c9383a;
    --radius-lg: 16px;
    --radius-md: 10px;
    --radius-sm: 8px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
}

body {
    font-family: 'Pretendard Variable', Pretendard, -apple-system, BlinkMacSystemFont,
        'Apple SD Gothic Neo', 'Malgun Gothic', 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--ink);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

button, input, textarea {
    font: inherit;
    color: inherit;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ===== 전체 레이아웃: 사이드바 + 메인 ===== */
.app {
    display: grid;
    grid-template-columns: 280px 1fr;
    height: 100vh;
}

/* ===== 사이드바 ===== */
.sidebar {
    background: var(--sidebar);
    color: #e8ebf2;
    display: flex;
    flex-direction: column;
    padding: 24px 16px;
    gap: 24px;
    min-height: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 8px;
}

.brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--accent);
    color: #fff;
    display: grid;
    place-items: center;
    font-weight: 800;
    font-size: 18px;
}

.brand strong {
    display: block;
    font-size: 17px;
    letter-spacing: -0.01em;
}

.brand div span {
    font-size: 12px;
    color: #9aa3b8;
}

.tabs {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.tab-button {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px;
    background: none;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    text-align: left;
    color: #c4cada;
    transition: background 0.15s;
}

.tab-button:hover {
    background: rgba(255, 255, 255, 0.06);
}

.tab-button.active {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    box-shadow: inset 3px 0 0 var(--accent);
}

.tab-icon {
    font-size: 20px;
    width: 28px;
    text-align: center;
    flex-shrink: 0;
}

.tab-text strong {
    display: block;
    font-size: 15px;
    font-weight: 600;
}

.tab-text small {
    display: block;
    font-size: 12px;
    color: #8f98ad;
}

.tab-button.active .tab-text small {
    color: #b8bfd2;
}

.sidebar-footer {
    padding: 0 8px;
    font-size: 12px;
    color: #7f889c;
}

/* ===== 최근 대화 기록 ===== */
.history {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    padding-top: 20px;
}

.history-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 8px;
}

.history-head h2 {
    font-size: 13px;
    font-weight: 600;
    color: #9aa3b8;
}

.history-new {
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.16);
    color: #dfe3ee;
    font-size: 12px;
    padding: 4px 10px;
    border-radius: 999px;
    cursor: pointer;
}

.history-new:hover {
    background: rgba(255, 255, 255, 0.08);
}

.history-list {
    list-style: none;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.history-list:empty::before {
    content: '아직 저장된 대화가 없습니다';
    display: block;
    padding: 8px;
    font-size: 12px;
    color: #6f7890;
}

.history-item {
    display: flex;
    align-items: center;
    border-radius: var(--radius-sm);
}

.history-item:hover,
.history-item.active {
    background: rgba(255, 255, 255, 0.07);
}

.history-item.active .history-title {
    color: #fff;
}

.history-open {
    flex: 1;
    min-width: 0;
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 8px;
    background: none;
    border: none;
    text-align: left;
    cursor: pointer;
}

.history-title {
    font-size: 13px;
    color: #c4cada;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-time {
    font-size: 11px;
    color: #727b90;
    flex-shrink: 0;
}

.history-delete {
    width: 26px;
    height: 26px;
    margin-right: 4px;
    border: none;
    border-radius: 6px;
    background: none;
    color: #8f98ad;
    font-size: 16px;
    cursor: pointer;
    opacity: 0;
}

.history-item:hover .history-delete,
.history-delete:focus-visible {
    opacity: 1;
}

.history-delete:hover {
    background: rgba(201, 56, 58, 0.25);
    color: #fff;
}

@media (hover: none) {
    .history-delete { opacity: 1; }
}

/* ===== 메인 영역 ===== */
.main {
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.tab-content {
    display: none;
    flex: 1;
    min-height: 0;
    flex-direction: column;
    padding: 28px 40px 32px;
    gap: 20px;
}

.tab-content.active {
    display: flex;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--line);
}

.panel-header h1 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.panel-header p {
    color: var(--muted);
    font-size: 14px;
}

/* ===== 버튼 ===== */
.btn-primary,
.btn-ghost {
    padding: 11px 22px;
    border-radius: var(--radius-md);
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, border-color 0.15s;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: 1px solid var(--accent);
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-ghost {
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--line);
}

.btn-ghost:hover {
    border-color: #c8cdd8;
    background: #fafbfc;
}

.btn-primary:active,
.btn-ghost:active {
    transform: scale(0.98);
}

/* ===== 공통 입력창 ===== */
input[type="text"],
input[type="url"],
textarea {
    width: 100%;
    padding: 12px 16px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus,
textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea {
    resize: vertical;
}

/* ===== 채팅 ===== */
.chat-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
    max-width: 920px;
    margin: 0 auto;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 8px 4px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* 대화가 없을 때 안내 */
.chat-messages:empty::before {
    content: '무엇이든 물어보세요.\A예: "AWS Lambda와 EC2의 차이를 표로 정리해줘"';
    white-space: pre-line;
    margin: auto;
    text-align: center;
    color: var(--muted);
    font-size: 15px;
}

.message {
    max-width: 78%;
    padding: 14px 18px;
    border-radius: var(--radius-lg);
    word-wrap: break-word;
    white-space: pre-wrap;
}

.message.user {
    align-self: flex-end;
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.message.assistant {
    align-self: flex-start;
    background: var(--surface);
    border: 1px solid var(--line);
    border-bottom-left-radius: 4px;
}

.message-header {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 4px;
    opacity: 0.7;
}

.message-content {
    line-height: 1.7;
}

.usage-info {
    margin-top: 10px;
    padding-top: 8px;
    border-top: 1px dashed var(--line);
    font-size: 12px;
    color: var(--muted);
}

.message.loading {
    color: var(--muted);
    font-style: normal;
}

.message.error {
    background: #fdf0f0;
    border-color: #f2caca;
    color: var(--danger);
}

.chat-input-container {
    display: flex;
    gap: 10px;
    padding: 8px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: 0 4px 16px rgba(29, 36, 51, 0.06);
}

.chat-input-container input[type="text"] {
    border: none;
    padding: 10px 12px;
}

.chat-input-container input[type="text"]:focus {
    box-shadow: none;
}

/* ===== 이미지 분석 / 웹 요약 공통 ===== */
.input-section {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.result-panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    padding: 24px 28px;
    overflow-y: auto;
    line-height: 1.8;
}

.result-panel:empty {
    display: grid;
    place-items: center;
    background: transparent;
    border-style: dashed;
}

.result-panel:empty::before {
    color: var(--muted);
    font-size: 14px;
    text-align: center;
}

.multimodal-response:empty::before {
    content: '분석 결과가 여기에 표시됩니다';
}

.web-response:empty::before {
    content: '요약 결과가 여기에 표시됩니다';
}

.loading {
    padding: 20px;
    text-align: center;
    color: var(--muted);
}

.error {
    background: #fdf0f0;
    color: var(--danger);
    padding: 14px 16px;
    border-radius: var(--radius-sm);
    border-left: 4px solid var(--danger);
}

/* ===== 이미지 분석 ===== */
.multimodal-container {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(320px, 420px) 1fr;
    gap: 24px;
}

.multimodal-container .input-section {
    overflow-y: auto;
}

.file-upload-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.file-label {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 28px 16px;
    background: var(--surface);
    border: 1.5px dashed #c5cbe0;
    border-radius: var(--radius-lg);
    cursor: pointer;
    text-align: center;
    transition: border-color 0.15s, background 0.15s;
}

.file-label:hover {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.file-label-icon {
    font-size: 28px;
}

.file-label small {
    color: var(--muted);
    font-size: 12px;
}

.file-upload-section input[type="file"] {
    display: none;
}

.image-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.image-preview:empty {
    display: none;
}

.image-preview-item {
    position: relative;
    width: 84px;
    height: 84px;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--line);
}

.image-preview-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.image-preview-item .remove-btn {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 22px;
    height: 22px;
    border: none;
    border-radius: 50%;
    background: rgba(29, 36, 51, 0.75);
    color: #fff;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
}

.image-preview-item .remove-btn:hover {
    background: var(--danger);
}

.button-group {
    display: flex;
    gap: 10px;
}

.button-group .btn-primary {
    flex: 1;
}

/* ===== 웹 요약 ===== */
.web-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
    max-width: 920px;
    margin: 0 auto;
}

.url-row {
    display: flex;
    gap: 10px;
}

.web-response {
    flex: 1;
}

/* ===== 반응형: 태블릿·모바일 ===== */
@media (max-width: 900px) {
    .app {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr;
    }

    .sidebar {
        padding: 12px;
        gap: 12px;
    }

    .tabs {
        flex-direction: row;
    }

    .tab-button {
        justify-content: center;
        padding: 10px 8px;
    }

    .tab-button.active {
        box-shadow: inset 0 -3px 0 var(--accent);
    }

    .tab-text small,
    .sidebar-footer {
        display: none;
    }

    /* 모바일: 최근 대화를 가로로 넘겨보기 */
    .history {
        padding-top: 10px;
        flex: none;
    }

    .history-list {
        flex-direction: row;
        overflow-x: auto;
        gap: 6px;
    }

    .history-item {
        flex: 0 0 auto;
        max-width: 180px;
        background: rgba(255, 255, 255, 0.05);
    }

    .history-time {
        display: none;
    }

    .tab-content {
        padding: 20px 16px;
    }

    .multimodal-container {
        grid-template-columns: 1fr;
        overflow-y: auto;
    }

    .message {
        max-width: 90%;
    }
}

@media (max-width: 520px) {
    .brand div span,
    .tab-icon {
        display: none;
    }

    .panel-header p {
        display: none;
    }

    .url-row {
        flex-direction: column;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
    }
}
