/* ==========================================================================
   디자인 토큰
   ========================================================================== */
:root {
    color-scheme: light dark;

    --font-sans: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard",
        "Segoe UI", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
    --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;

    --bg: #ffffff;
    --bg-elevated: #f5f5f7;
    --bg-overlay: rgba(255, 255, 255, 0.82);
    --fill: rgba(120, 120, 128, 0.12);
    --fill-strong: rgba(120, 120, 128, 0.2);
    --separator: rgba(0, 0, 0, 0.1);
    --text: #1d1d1f;
    --text-secondary: #55555a;
    --text-tertiary: #6e6e73;
    --accent: #0066cc;
    --accent-fill: #0071e3;
    --accent-fill-hover: #0077ed;
    --on-accent: #ffffff;
    --accent-soft: rgba(0, 113, 227, 0.12);
    --danger: #d70015;
    --on-danger: #ffffff;
    --danger-soft: rgba(215, 0, 21, 0.08);
    --code-bg: #f5f5f7;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 24px 48px rgba(0, 0, 0, 0.18);

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 18px;
    --radius-xl: 24px;
    --radius-full: 999px;

    --content-width: 760px;
    --topbar-height: 56px;

    --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
    --duration: 180ms;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #161617;
        --bg-elevated: #1f1f21;
        --bg-overlay: rgba(22, 22, 23, 0.82);
        --fill: rgba(120, 120, 128, 0.24);
        --fill-strong: rgba(120, 120, 128, 0.36);
        --separator: rgba(255, 255, 255, 0.12);
        --text: #f5f5f7;
        --text-secondary: #a1a1a6;
        --text-tertiary: #8e8e93;
        --accent: #4da3ff;
        --accent-fill: #0a66d8;
        --accent-fill-hover: #1673e6;
        --accent-soft: rgba(77, 163, 255, 0.16);
        --danger: #ff6961;
        --on-danger: #161617;
        --danger-soft: rgba(255, 105, 97, 0.12);
        --code-bg: #232325;
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 24px 48px rgba(0, 0, 0, 0.5);
    }
}

/* ==========================================================================
   기본
   ========================================================================== */
*,
*::before,
*::after {
    box-sizing: border-box;
}

* {
    margin: 0;
    padding: 0;
}

html,
body {
    height: 100%;
}

body {
    font-family: var(--font-sans);
    font-size: 16px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    word-break: keep-all;
    overflow-wrap: anywhere;
}

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

button {
    cursor: pointer;
    background: none;
    border: none;
}

button:disabled {
    cursor: default;
}

svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex: none;
}

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

.visually-hidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

/* ==========================================================================
   레이아웃
   ========================================================================== */
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

main {
    position: relative;
    flex: 1;
    min-height: 0;
}

.panel {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
}

.panel[hidden] {
    display: none;
}

.scroller {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scroll-padding-bottom: 24px;
}

/* ==========================================================================
   상단 바
   ========================================================================== */
.topbar {
    flex: none;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 12px;
    height: var(--topbar-height);
    padding: 0 16px;
    padding-top: env(safe-area-inset-top);
    box-sizing: content-box;
    background: var(--bg-overlay);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    backdrop-filter: saturate(180%) blur(20px);
    border-bottom: 1px solid var(--separator);
    position: relative;
    z-index: 10;
}

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

.brand-mark {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    border-radius: var(--radius-sm);
    background: var(--text);
    color: var(--bg);
}

.brand-mark svg {
    width: 16px;
    height: 16px;
    stroke-width: 2;
}

.brand-name {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.01em;
    white-space: nowrap;
}

.topbar-actions {
    display: flex;
    justify-content: flex-end;
}

.icon-button {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    color: var(--accent);
    transition: background-color var(--duration) var(--ease), opacity var(--duration) var(--ease);
}

.icon-button:hover:not(:disabled) {
    background: var(--fill);
}

.icon-button:disabled {
    color: var(--text-tertiary);
    opacity: 0.5;
}

/* 세그먼트 컨트롤 */
.segmented {
    display: inline-flex;
    padding: 2px;
    gap: 2px;
    border-radius: 10px;
    background: var(--fill);
}

.segment {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 32px;
    padding: 0 14px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    white-space: nowrap;
    transition: background-color var(--duration) var(--ease), color var(--duration) var(--ease),
        box-shadow var(--duration) var(--ease);
}

.segment svg {
    width: 16px;
    height: 16px;
}

.segment:hover {
    color: var(--text);
}

.segment[aria-selected="true"] {
    background: var(--bg);
    color: var(--text);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
    .segment[aria-selected="true"] {
        background: #3a3a3c;
    }
}

/* ==========================================================================
   빈 상태
   ========================================================================== */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100%;
    padding: 48px 24px;
    text-align: center;
    animation: fade-in 400ms var(--ease);
}

.empty-state[hidden] {
    display: none;
}

.empty-icon {
    display: grid;
    place-items: center;
    width: 56px;
    height: 56px;
    margin-bottom: 20px;
    border-radius: var(--radius-lg);
    background: var(--fill);
    color: var(--text-secondary);
}

.empty-icon svg {
    width: 28px;
    height: 28px;
    stroke-width: 1.6;
}

.empty-state h2 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin-bottom: 8px;
}

.empty-state p {
    max-width: 420px;
    font-size: 15px;
    line-height: 1.6;
    color: var(--text-secondary);
}

.suggestions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    margin-top: 28px;
}

.chip {
    padding: 8px 14px;
    border-radius: var(--radius-full);
    border: 1px solid var(--separator);
    background: var(--bg);
    font-size: 14px;
    color: var(--text);
    transition: background-color var(--duration) var(--ease), border-color var(--duration) var(--ease);
}

.chip:hover {
    background: var(--bg-elevated);
    border-color: var(--fill-strong);
}

/* ==========================================================================
   대화
   ========================================================================== */
.thread {
    display: flex;
    flex-direction: column;
    gap: 24px;
    max-width: var(--content-width);
    margin: 0 auto;
    padding: 28px 20px 12px;
}

.thread:empty {
    display: none;
}

.message {
    display: flex;
    flex-direction: column;
    animation: message-in 260ms var(--ease);
}

/* 사용자 메시지: 오른쪽 말풍선 */
.message.user {
    align-items: flex-end;
}

.message.user .bubble {
    max-width: min(80%, 560px);
    padding: 10px 16px;
    border-radius: var(--radius-lg);
    border-bottom-right-radius: 6px;
    background: var(--accent-fill);
    color: var(--on-accent);
    line-height: 1.5;
    white-space: pre-wrap;
}

.message.user .bubble-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 2px;
    word-break: break-all;
}

.message.user .bubble-link svg {
    width: 16px;
    height: 16px;
}

.message.user .bubble-note {
    display: block;
    margin-top: 4px;
    font-size: 14px;
}

.message-images {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
    max-width: min(80%, 560px);
    margin-bottom: 6px;
}

.message-images img {
    display: block;
    width: 120px;
    height: 120px;
    object-fit: cover;
    border-radius: var(--radius-md);
    background: var(--fill);
}

.message-images img:only-child {
    width: auto;
    max-width: 280px;
    height: auto;
    max-height: 280px;
}

/* AI 메시지: 말풍선 없이 본문으로 */
.message.assistant {
    align-items: stretch;
}

.message-meta {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
    min-height: 28px;
    font-size: 12px;
    color: var(--text-tertiary);
    font-variant-numeric: tabular-nums;
}

.message-meta .usage {
    margin-left: 6px;
}

.meta-button {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 28px;
    padding: 0 8px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    color: var(--text-secondary);
    transition: background-color var(--duration) var(--ease), color var(--duration) var(--ease);
}

.meta-button svg {
    width: 15px;
    height: 15px;
}

.meta-button:hover {
    background: var(--fill);
    color: var(--text);
}

.meta-button.is-done {
    color: var(--accent);
}

/* 오류 */
.message.error .error-card {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 14px 16px;
    border-radius: var(--radius-md);
    background: var(--danger-soft);
    color: var(--text);
}

.error-title {
    font-weight: 600;
    color: var(--danger);
}

.error-detail {
    font-size: 13px;
    color: var(--text-secondary);
}

.error-card .meta-button {
    align-self: flex-start;
    margin: 6px 0 0 -8px;
    color: var(--accent);
    font-size: 14px;
    font-weight: 500;
}

.error-card .meta-button svg {
    width: 16px;
    height: 16px;
}

/* 입력 중 표시 */
.typing {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    height: 28px;
    color: var(--text-secondary);
    font-size: 14px;
}

.typing-dots {
    display: inline-flex;
    gap: 4px;
}

.typing-dots span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-tertiary);
    animation: typing 1.2s infinite var(--ease);
}

.typing-dots span:nth-child(2) {
    animation-delay: 0.15s;
}

.typing-dots span:nth-child(3) {
    animation-delay: 0.3s;
}

/* ==========================================================================
   마크다운 본문
   ========================================================================== */
.prose {
    font-size: 16px;
    line-height: 1.7;
    color: var(--text);
}

.prose > * + * {
    margin-top: 0.8em;
}

.prose h1,
.prose h2,
.prose h3,
.prose h4,
.prose h5,
.prose h6 {
    font-weight: 700;
    line-height: 1.35;
    letter-spacing: -0.01em;
}

.prose > h1 + *,
.prose > h2 + *,
.prose > h3 + *,
.prose > h4 + * {
    margin-top: 0.4em;
}

.prose * + h1,
.prose * + h2,
.prose * + h3 {
    margin-top: 1.4em;
}

.prose h1 { font-size: 1.4em; }
.prose h2 { font-size: 1.25em; }
.prose h3 { font-size: 1.1em; }
.prose h4,
.prose h5,
.prose h6 { font-size: 1em; }

.prose ul,
.prose ol {
    padding-left: 1.4em;
}

.prose li + li {
    margin-top: 0.3em;
}

.prose li::marker {
    color: var(--text-tertiary);
}

.prose strong {
    font-weight: 650;
}

.prose a {
    color: var(--accent);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
}

.prose a:hover {
    text-decoration-thickness: 2px;
}

.prose blockquote {
    padding-left: 14px;
    border-left: 3px solid var(--fill-strong);
    color: var(--text-secondary);
}

.prose hr {
    border: none;
    border-top: 1px solid var(--separator);
    margin: 1.4em 0;
}

.prose code {
    font-family: var(--font-mono);
    font-size: 0.88em;
    padding: 0.15em 0.4em;
    border-radius: 6px;
    background: var(--code-bg);
}

.prose pre {
    overflow-x: auto;
    padding: 14px 16px;
    border-radius: var(--radius-md);
    background: var(--code-bg);
    border: 1px solid var(--separator);
    line-height: 1.55;
}

.prose pre code {
    padding: 0;
    background: none;
    font-size: 13.5px;
    white-space: pre;
}

.prose table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
    display: block;
    overflow-x: auto;
}

.prose th,
.prose td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--separator);
    text-align: left;
    white-space: nowrap;
}

.prose th {
    font-weight: 600;
    color: var(--text-secondary);
}

/* ==========================================================================
   입력창
   ========================================================================== */
.composer-wrap {
    flex: none;
    width: 100%;
    max-width: calc(var(--content-width) + 32px);
    margin: 0 auto;
    padding: 8px 16px calc(12px + env(safe-area-inset-bottom));
    background: linear-gradient(to bottom, transparent, var(--bg) 16px);
}

.composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 6px 6px 6px 18px;
    border: 1px solid var(--separator);
    border-radius: var(--radius-xl);
    background: var(--bg-elevated);
    box-shadow: var(--shadow-sm);
    transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}

.composer:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer.has-attachments-slot,
.composer.composer-stacked {
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    padding: 6px;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 6px;
}

.composer-stacked .composer-row:first-child {
    align-items: center;
}

.composer-row-divided {
    border-top: 1px solid var(--separator);
    margin-top: 4px;
    padding-top: 4px;
}

.composer textarea,
.composer input {
    flex: 1;
    min-width: 0;
    min-height: 36px;
    max-height: 200px;
    padding: 6px 0;
    border: none;
    outline: none;
    background: transparent;
    font-size: 16px; /* iOS 확대 방지 */
    line-height: 24px;
    resize: none;
}

.composer textarea:focus-visible,
.composer input:focus-visible {
    outline: none;
    box-shadow: none;
}

.composer-row textarea,
.composer-row input {
    padding-left: 6px;
}

.composer-stacked .composer-row textarea {
    padding-left: 12px;
}

.composer textarea::placeholder,
.composer input::placeholder {
    color: var(--text-tertiary);
}

.field-icon {
    display: grid;
    place-items: center;
    width: 32px;
    height: 36px;
    color: var(--text-tertiary);
}

.field-icon svg {
    width: 18px;
    height: 18px;
}

.send-button {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    background: var(--accent-fill);
    color: var(--on-accent);
    transition: background-color var(--duration) var(--ease), opacity var(--duration) var(--ease),
        transform var(--duration) var(--ease);
}

.send-button svg {
    width: 18px;
    height: 18px;
    stroke-width: 2.4;
}

.send-button:hover:not(:disabled) {
    background: var(--accent-fill-hover);
}

.send-button:active:not(:disabled) {
    transform: scale(0.92);
}

.send-button:disabled {
    background: var(--fill-strong);
    color: var(--text-tertiary);
}

.attach-button {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    color: var(--text-secondary);
    transition: background-color var(--duration) var(--ease), color var(--duration) var(--ease);
}

.attach-button:hover {
    background: var(--fill);
    color: var(--text);
}

.attachments {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 6px 6px 8px;
}

.attachments:empty {
    display: none;
}

.attachment {
    position: relative;
    flex: none;
    width: 64px;
    height: 64px;
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--fill);
    animation: message-in 200ms var(--ease);
}

.attachment img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.attachment-remove {
    position: absolute;
    top: 4px;
    right: 4px;
    display: grid;
    place-items: center;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
}

/* 부모(.attachment)가 overflow: hidden이므로 포커스 링을 안쪽에 그림 */
.attachment-remove:focus-visible {
    outline-color: #fff;
    outline-offset: -3px;
}

.attachment-remove svg {
    width: 12px;
    height: 12px;
    stroke-width: 2.6;
}

.composer-hint {
    min-height: 18px;
    margin-top: 8px;
    font-size: 12px;
    text-align: center;
    color: var(--text-tertiary);
}

.composer-hint.is-error {
    color: var(--danger);
}

@media (hover: none) {
    .composer-hint .hint-keys {
        visibility: hidden;
    }
}

/* 드래그 앤 드롭 */
.drop-overlay {
    position: absolute;
    inset: 12px;
    display: grid;
    place-items: center;
    border: 2px dashed var(--accent);
    border-radius: var(--radius-xl);
    background: var(--bg-overlay);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--duration) var(--ease);
    z-index: 5;
}

.panel.is-dragging .drop-overlay {
    opacity: 1;
}

.drop-overlay-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    color: var(--accent);
    font-weight: 600;
}

.drop-overlay-inner svg {
    width: 40px;
    height: 40px;
    stroke-width: 1.5;
}

/* ==========================================================================
   확인 시트
   ========================================================================== */
.sheet {
    margin: auto;
    width: min(340px, calc(100% - 40px));
    padding: 24px 20px 20px;
    border: none;
    border-radius: var(--radius-lg);
    background: var(--bg-elevated);
    color: var(--text);
    box-shadow: var(--shadow-lg);
    text-align: center;
}

.sheet[open] {
    animation: sheet-in 220ms var(--ease);
}

.sheet::backdrop {
    background: rgba(0, 0, 0, 0.32);
    animation: fade-in 220ms var(--ease);
}

.sheet h2 {
    font-size: 17px;
    font-weight: 600;
    margin-bottom: 6px;
}

.sheet p {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.sheet-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 20px;
}

.button {
    height: 40px;
    border-radius: var(--radius-md);
    font-size: 15px;
    font-weight: 600;
    transition: background-color var(--duration) var(--ease);
}

.button-secondary {
    background: var(--fill);
    color: var(--text);
}

.button-secondary:hover {
    background: var(--fill-strong);
}

.button-danger {
    background: var(--danger);
    color: var(--on-danger);
}

.button-danger:hover {
    filter: brightness(1.08);
}

/* ==========================================================================
   애니메이션
   ========================================================================== */
@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes message-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}

@keyframes sheet-in {
    from { opacity: 0; transform: scale(0.96); }
    to { opacity: 1; transform: none; }
}

@keyframes typing {
    0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-3px); }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 1ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 1ms !important;
    }
}

/* ==========================================================================
   반응형
   ========================================================================== */
@media (max-width: 640px) {
    .topbar {
        grid-template-columns: auto 1fr auto;
        gap: 8px;
        padding: 0 8px;
        padding-top: env(safe-area-inset-top);
    }

    .brand-name {
        display: none;
    }

    .segmented {
        justify-self: center;
    }

    .segment {
        padding: 0 10px;
    }

    .thread {
        padding: 20px 16px 8px;
    }

    .composer-wrap {
        padding-left: 10px;
        padding-right: 10px;
    }

    .message.user .bubble,
    .message-images {
        max-width: 88%;
    }

    .empty-state h2 {
        font-size: 21px;
    }
}

@media (max-width: 380px) {
    .segment svg {
        display: none;
    }
}
