/* ===== Reset ===== */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
}

button,
textarea {
    font-family: inherit;
    font-size: 100%;
    color: inherit;
}

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

svg {
    display: block;
}

/* ===== Tokens ===== */
:root {
    --darkbg: #0f172a;
    --cardbg: #1e293b;
    --bordercol: #334155;
    --slate-100: #f1f5f9;
    --slate-200: #e2e8f0;
    --slate-300: #cbd5e1;
    --slate-400: #94a3b8;
    --slate-500: #64748b;
    --slate-800: #1e293b;
    --slate-900: #0f172a;
    --slate-950: #020617;
    --indigo-300: #a5b4fc;
    --indigo-400: #818cf8;
    --indigo-500: #6366f1;
    --indigo-600: #4f46e5;
    --indigo-700: #4338ca;
    --emerald-400: #34d399;
    --emerald-500: #10b981;
    --emerald-600: #059669;
    --amber-300: #fcd34d;
    --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

body {
    background: var(--darkbg);
    color: var(--slate-100);
    height: 100vh;
    height: 100svh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
    line-height: 1.5;
}

code {
    font-family: var(--mono);
    font-size: 0.75rem;
    background: var(--slate-900);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--indigo-300);
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: #0f172a;
}

::-webkit-scrollbar-thumb {
    background: #334155;
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: #475569;
}

/* ===== Header ===== */
.header {
    background: var(--cardbg);
    border-bottom: 1px solid var(--bordercol);
    padding: 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, .3), 0 4px 6px -4px rgba(0, 0, 0, .3);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.status-dot {
    width: 0.75rem;
    height: 0.75rem;
    border-radius: 9999px;
    background: var(--emerald-500);
    animation: pulse 2s cubic-bezier(.4, 0, .6, 1) infinite;
}

.title {
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    color: #fff;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.375rem;
}

.badge-amber {
    font-size: 0.75rem;
    font-weight: 400;
    padding: 2px 8px;
    border-radius: 4px;
    background: rgba(245, 158, 11, .2);
    color: var(--amber-300);
    border: 1px solid rgba(245, 158, 11, .3);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.75rem;
    color: var(--slate-400);
    font-family: var(--mono);
}

.engine-label {
    display: none;
}

.toggle-btn {
    display: none;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.75rem;
    border-radius: 0.5rem;
    background: var(--slate-800);
    color: var(--slate-200);
    border: 1px solid var(--bordercol);
    transition: background-color .15s;
    font-family: inherit;
    font-size: 0.75rem;
}

.toggle-btn:hover {
    background: #334155;
}

.toggle-btn svg {
    width: 1rem;
    height: 1rem;
    color: var(--indigo-400);
}

/* ===== Workspace ===== */
.workspace {
    flex: 1;
    display: flex;
    overflow: hidden;
    position: relative;
    min-height: 0;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    min-width: 0;
    background: var(--darkbg);
}

/* ===== Message list ===== */
.message-list {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.msg-row {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
}

.msg-row.user {
    flex-direction: row-reverse;
}

.avatar {
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.75rem;
    color: #fff;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, .3);
    flex-shrink: 0;
}

.avatar.ai {
    background: var(--indigo-600);
}

.avatar.user {
    background: var(--emerald-600);
}

.bubble-wrap {
    flex: 1;
    max-width: 85%;
    min-width: 0;
}

.msg-row.user .bubble-wrap {
    display: flex;
    justify-content: flex-end;
}

.bubble {
    border-radius: 1rem;
    padding: 0.875rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}

.bubble.ai {
    background: var(--cardbg);
    border: 1px solid var(--bordercol);
    color: var(--slate-200);
    border-top-left-radius: 0;
}

.bubble.user {
    background: var(--indigo-600);
    color: #fff;
    border-top-right-radius: 0;
}

.bubble p {
    line-height: 1.625;
    white-space: pre-wrap;
    font-size: 0.875rem;
    word-break: break-word;
}

/* Welcome */
.welcome-bubble {
    flex: 1;
    max-width: 48rem;
    background: var(--cardbg);
    border: 1px solid var(--bordercol);
    border-radius: 1rem;
    padding: 1rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}

.welcome-bubble p {
    color: var(--slate-200);
    line-height: 1.625;
    font-size: 0.875rem;
}

.preset-row {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid rgba(51, 65, 85, .5);
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

.preset-label {
    font-size: 0.75rem;
    color: var(--slate-400);
}

.preset-btn {
    font-size: 0.75rem;
    background: var(--slate-800);
    color: var(--indigo-300);
    padding: 0.25rem 0.625rem;
    border-radius: 0.5rem;
    border: 1px solid var(--bordercol);
    transition: background-color .15s;
}

.preset-btn:hover {
    background: #334155;
}

/* Intent chips */
.intent-box {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid rgba(51, 65, 85, .55);
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.intent-label {
    width: 100%;
    font-size: 0.6875rem;
    color: var(--slate-400);
    font-weight: 500;
    margin-bottom: 0.25rem;
}

.intent-btn {
    font-size: 0.75rem;
    background: var(--slate-900);
    color: var(--indigo-300);
    border: 1px solid rgba(51, 65, 85, .8);
    padding: 0.375rem 0.75rem;
    border-radius: 0.75rem;
    transition: all .15s;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}

.intent-btn:hover {
    background: rgba(30, 27, 75, .6);
    border-color: rgba(99, 102, 241, .5);
}

.intent-btn svg {
    width: 0.875rem;
    height: 0.875rem;
    color: var(--indigo-400);
    margin-left: 0.25rem;
}

.intent-btn.selected {
    background: var(--indigo-600);
    color: #fff;
    border-color: var(--indigo-500);
    box-shadow: 0 4px 6px -1px rgba(79, 70, 229, .3);
}

.intent-btn.selected svg {
    color: #fff;
}

/* Loading */
.loading-bubble {
    background: var(--cardbg);
    border: 1px solid var(--bordercol);
    border-radius: 1rem;
    padding: 1rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.dot {
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 9999px;
    background: var(--indigo-400);
    animation: bounce 1s infinite;
}

.dot:nth-child(2) {
    animation-delay: .2s;
}

.dot:nth-child(3) {
    animation-delay: .4s;
}

.loading-text {
    font-size: 0.75rem;
    color: var(--slate-400);
    margin-left: 0.5rem;
    font-family: var(--mono);
}

/* ===== Input ===== */
.input-area {
    background: var(--cardbg);
    border-top: 1px solid var(--bordercol);
    padding: 1rem;
    flex-shrink: 0;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, .3);
}

.chat-form {
    max-width: 56rem;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 0.75rem;
}

.input-wrap {
    flex: 1;
    background: var(--slate-900);
    border: 1px solid var(--bordercol);
    border-radius: 0.75rem;
    transition: border-color .15s;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, .3);
    display: flex;
    align-items: center;
    padding: 0.5rem 1rem;
}

.input-wrap:focus-within {
    border-color: var(--indigo-500);
}

#user-input {
    width: 100%;
    background: transparent;
    color: var(--slate-100);
    border: 0;
    outline: none;
    resize: none;
    max-height: 8rem;
    padding: 0.25rem 0;
    font-size: 0.875rem;
    line-height: 1.625;
}

#user-input::placeholder {
    color: var(--slate-500);
}

.send-btn {
    background: var(--indigo-600);
    color: #fff;
    font-weight: 500;
    padding: 0.75rem 1.25rem;
    border-radius: 0.75rem;
    transition: all .15s;
    box-shadow: 0 4px 6px -1px rgba(79, 70, 229, .2);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.send-btn:hover {
    background: var(--indigo-500);
}

.send-btn:active {
    transform: scale(.95);
}

.send-btn svg {
    width: 1.25rem;
    height: 1.25rem;
}

.input-footer {
    max-width: 56rem;
    margin: 0.5rem auto 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    color: var(--slate-500);
    padding: 0 0.25rem;
}

.hint-desktop {
    display: none;
}

#latency-indicator {
    font-family: var(--mono);
    color: var(--emerald-400);
}

/* ===== Sidebar ===== */
.sidebar {
    display: none;
    width: 24rem;
    flex-direction: column;
    height: 100%;
    background: var(--cardbg);
    border-left: 1px solid var(--bordercol);
    flex-shrink: 0;
    z-index: 20;
}

.sidebar-head {
    padding: 1rem;
    border-bottom: 1px solid var(--bordercol);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-head h2 {
    font-weight: 600;
    font-size: 0.875rem;
    letter-spacing: -0.025em;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.sidebar-head h2 svg {
    width: 1rem;
    height: 1rem;
    color: var(--indigo-400);
}

.audit-badge {
    font-size: 10px;
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.05em;
    padding: 2px 8px;
    border-radius: 4px;
    background: rgba(99, 102, 241, .1);
    color: var(--indigo-400);
    border: 1px solid rgba(99, 102, 241, .2);
}

.sidebar-body {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.info-box {
    background: rgba(15, 23, 42, .6);
    border: 1px solid rgba(51, 65, 85, .7);
    border-radius: 0.75rem;
    padding: 0.875rem;
    font-size: 0.75rem;
    color: var(--slate-300);
    line-height: 1.625;
}

.panel {
    background: var(--slate-900);
    border: 1px solid var(--bordercol);
    border-radius: 0.75rem;
    padding: 1rem;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, .3);
    position: relative;
    overflow: hidden;
}

.panel h3 {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--slate-400);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.75rem;
}

.panel.audit {
    box-shadow: none;
}

.panel.audit h3 {
    margin-bottom: 0.5rem;
}

#sankey-nodes-area {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    position: relative;
}

.empty-state {
    text-align: center;
    padding: 2rem 0;
    color: var(--slate-500);
    font-size: 0.75rem;
    font-family: var(--mono);
}

.flow-item {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}

.flow-head {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    font-weight: 500;
}

.flow-name {
    color: var(--slate-200);
    padding-right: 0.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.flow-count {
    color: var(--indigo-400);
    font-family: var(--mono);
    flex-shrink: 0;
}

.flow-track {
    width: 100%;
    background: var(--slate-950);
    border-radius: 9999px;
    height: 0.5rem;
    overflow: hidden;
    border: 1px solid rgba(51, 65, 85, .5);
}

.flow-fill {
    background: var(--indigo-500);
    height: 100%;
    border-radius: 9999px;
    transition: all .5s;
}

#audit-payload-view {
    font-size: 11px;
    font-family: var(--mono);
    color: var(--indigo-300);
    background: var(--slate-950);
    padding: 0.75rem;
    border-radius: 0.5rem;
    border: 1px solid rgba(51, 65, 85, .6);
    overflow-x: auto;
    min-height: 80px;
    max-height: 160px;
    overflow-y: auto;
    white-space: pre-wrap;
}

/* ===== Animations ===== */
@keyframes pulse {
    50% {
        opacity: .5;
    }
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(-25%);
        animation-timing-function: cubic-bezier(.8, 0, 1, 1);
    }

    50% {
        transform: none;
        animation-timing-function: cubic-bezier(0, 0, .2, 1);
    }
}

/* ===== Responsive: sm (>=640px) ===== */
@media (min-width: 640px) {
    .header {
        padding: 1rem 1.5rem;
    }

    .title {
        font-size: 1.125rem;
    }

    .message-list {
        padding: 1.5rem;
    }

    .msg-row {
        gap: 1rem;
    }

    .avatar {
        width: 2rem;
        height: 2rem;
        font-size: 0.875rem;
    }

    .bubble-wrap {
        max-width: 48rem;
    }

    .bubble {
        padding: 1rem;
    }

    .welcome-bubble p {
        font-size: 1rem;
    }

    .input-area {
        padding: 1.5rem;
    }

    .hint-desktop {
        display: inline;
    }

    .hint-mobile {
        display: none;
    }
}

/* ===== Responsive: md (>=768px) ===== */
@media (min-width: 768px) {
    .engine-label {
        display: inline;
    }

    .toggle-btn {
        display: flex;
    }

    .sidebar {
        display: flex;
    }

    .sidebar.collapsed {
        display: none;
    }
}