@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&display=swap');

:root {
    --background: #faf9f5;
    --foreground: #3d3929;
    --card: #f5f4ef;
    --card-foreground: #141413;
    --popover: #ffffff;
    --popover-foreground: #28261b;
    --primary: #c96442;
    --primary-foreground: #ffffff;
    --secondary: #e9e6dc;
    --secondary-foreground: #535146;
    --muted: #ede9de;
    --muted-foreground: #6e6d68;
    --accent: #e9e6dc;
    --accent-foreground: #28261b;
    --destructive: #141413;
    --destructive-foreground: #ffffff;
    --border: #dad9d4;
    --input: #b4b2a7;
    --ring: #c96442;
    --chart-1: #b05730;
    --chart-5: #b4552d;
    --sidebar: #f5f4ee;
    --sidebar-foreground: #3d3d3a;
    --sidebar-primary: #c96442;
    --sidebar-accent: #e9e6dc;
    --sidebar-accent-foreground: #343434;
    --sidebar-border: #ebebeb;
    --font-sans: 'Outfit', sans-serif;
    --font-mono: ui-monospace, 'Cascadia Code', 'Segoe UI Mono', monospace;
    --radius: 0.75rem;
}

*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    height: 100%;
}

body {
    font-family: var(--font-sans);
    background: var(--background);
    color: var(--foreground);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    position: fixed;
    inset: 0;
    width: 100%;
}

::-webkit-scrollbar {
    width: 5px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 99px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--input);
}

button {
    cursor: pointer;
    font-family: inherit;
}

#app {
    display: flex;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

/* ── Sidebar ────────────────────────────────── */
.sidebar {
    width: 340px;
    min-width: 340px;
    background: var(--sidebar);
    border-right: 1px solid var(--sidebar-border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
}

.sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 18px;
    border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-head-title {
    font-size: 0.88rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--sidebar-foreground);
}

.sidebar-head-actions {
    display: flex;
    gap: 2px;
}

.sidebar-head-actions button {
    background: none;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted-foreground);
    transition: background 0.15s, color 0.15s;
}

.sidebar-head-actions button:hover {
    background: var(--sidebar-accent);
    color: var(--foreground);
}

.sidebar-head-actions button svg,
.ctx-menu button svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex-shrink: 0;
}

/* Profile */
.profile-card {
    padding: 22px 18px 18px;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    border-bottom: 1px solid var(--sidebar-border);
}

.profile-avatar {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--chart-5));
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 1.4rem;
    font-weight: 600;
    margin-bottom: 10px;
    box-shadow: 0 3px 14px rgba(201, 100, 66, 0.25);
}

.profile-name {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--foreground);
    line-height: 1.2;
}

.profile-role {
    font-size: 0.82rem;
    color: var(--muted-foreground);
    margin-top: 3px;
}

.sb-section {
    padding: 16px 18px;
    border-bottom: 1px solid var(--sidebar-border);
}

.sb-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted-foreground);
    margin-bottom: 10px;
}

/* Affection */
.affection-bar-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.affection-track {
    flex: 1;
    height: 8px;
    background: var(--secondary);
    border-radius: 99px;
    overflow: hidden;
}

.affection-fill {
    height: 100%;
    border-radius: 99px;
    transition: width 0.5s cubic-bezier(0.22, 1, 0.36, 1), background 0.4s ease, box-shadow 0.4s ease;
}

.affection-fill.lv-low {
    background: #94a3b8;
}

.affection-fill.lv-mid {
    background: var(--primary);
}

.affection-fill.lv-high {
    background: #d4874a;
}

.affection-fill.lv-max {
    background: var(--chart-5);
    box-shadow: 0 0 10px rgba(180, 85, 45, 0.4);
}

.affection-pct {
    font-size: 0.82rem;
    font-weight: 600;
    font-family: var(--font-mono);
    color: var(--foreground);
    min-width: 32px;
    text-align: right;
}

.mood-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
}

.mood-label {
    font-size: 0.82rem;
    color: var(--muted-foreground);
}

.mood-chip {
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--primary);
    background: rgba(201, 100, 66, 0.08);
    padding: 4px 12px;
    border-radius: 99px;
}

/* Scene */
.scene-card {
    background: var(--muted);
    border-radius: var(--radius);
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.scene-row {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.88rem;
    color: var(--sidebar-foreground);
}

.scene-row .scene-icon {
    font-size: 0.9rem;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
    color: var(--muted-foreground);
}

.scene-row .scene-val {
    font-weight: 500;
}

/* Thought */
.thought-bubble {
    background: var(--card);
    border-left: 3px solid var(--primary);
    border-radius: 0 var(--radius) var(--radius) 0;
    padding: 12px 14px;
    font-size: 0.88rem;
    font-style: italic;
    color: var(--secondary-foreground);
    line-height: 1.55;
    transition: opacity 0.3s;
}

.prompt-textarea {
    width: 100%;
    min-height: 120px;
    max-height: 240px;
    resize: vertical;
    font-family: var(--font-mono);
    font-size: 0.78rem;
    line-height: 1.55;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--popover);
    color: var(--foreground);
    outline: none;
    transition: border-color 0.15s;
}

.prompt-textarea:focus {
    border-color: var(--ring);
}

/* Config */
.config-section {
    margin-top: auto;
    border-top: 1px solid var(--sidebar-border);
    padding: 16px 18px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.config-field label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted-foreground);
    margin-bottom: 6px;
}

.config-field input {
    width: 100%;
    font-family: var(--font-mono);
    font-size: 0.82rem;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--popover);
    color: var(--foreground);
    outline: none;
    transition: border-color 0.15s;
}

.config-field input:focus {
    border-color: var(--ring);
}

.config-field .field-hint {
    font-size: 0.72rem;
    margin-top: 4px;
    color: var(--muted-foreground);
}

.config-field .field-hint.ok {
    color: #5b8a5e;
}

.btn-save {
    width: 100%;
    font-size: 0.82rem;
    font-weight: 500;
    padding: 9px 14px;
    border-radius: 8px;
    border: 1px solid var(--primary);
    background: var(--primary);
    color: var(--primary-foreground);
    transition: background 0.15s;
}

.btn-save:hover {
    background: var(--chart-1);
}

/* ── Main Chat ──────────────────────────────── */
.main-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-header {
    height: 56px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    background: var(--background);
    flex-shrink: 0;
    z-index: 10;
    position: relative;
}

.chat-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-avatar-sm {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--chart-5));
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 0.8rem;
    font-weight: 600;
}

.chat-header-info .chat-name {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--foreground);
    line-height: 1.2;
}

.chat-header-info .chat-sub {
    font-size: 0.75rem;
    color: var(--muted-foreground);
}

.sidebar-toggle {
    display: none;
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    width: 34px;
    height: 34px;
    font-size: 1.1rem;
    color: var(--foreground);
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.sidebar-toggle:hover {
    background: var(--secondary);
}

/* Messages area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 22px 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.chat-messages-inner {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
}

/* Welcome */
.welcome {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    gap: 10px;
    padding: 28px;
}

.welcome-avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--chart-1));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6rem;
    box-shadow: 0 4px 18px rgba(201, 100, 66, 0.2);
    margin-bottom: 6px;
}

.welcome h2 {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--foreground);
}

.welcome p {
    font-size: 0.92rem;
    color: var(--muted-foreground);
    max-width: 380px;
    line-height: 1.5;
}

/* New Day Banner */
.new-day-banner {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    gap: 8px;
    padding: 28px;
    animation: msgIn 0.5s cubic-bezier(0.22, 1, 0.36, 1);
}

.new-day-icon {
    font-size: 2.4rem;
    margin-bottom: 4px;
}

.new-day-banner h2 {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--foreground);
}

.new-day-banner p {
    font-size: 0.92rem;
    color: var(--muted-foreground);
}

.new-day-btn {
    margin-top: 8px;
    padding: 10px 28px;
    border-radius: 10px;
    border: none;
    background: linear-gradient(135deg, var(--primary), var(--chart-5));
    color: #fff;
    font-size: 0.92rem;
    font-weight: 600;
    font-family: var(--font-sans);
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    box-shadow: 0 3px 12px rgba(201, 100, 66, 0.25);
}

.new-day-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 5px 18px rgba(201, 100, 66, 0.35);
}

.new-day-btn:active {
    transform: translateY(0);
}

/* Messages */
.msg-group {
    display: flex;
    flex-direction: column;
    gap: 3px;
    max-width: 85%;
    animation: msgIn 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes msgIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.msg-group.user {
    align-self: flex-end;
}

.msg-group.model {
    align-self: flex-start;
}

.msg-sender {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted-foreground);
    margin-bottom: 2px;
    padding: 0 2px;
}

.msg-group.user .msg-sender {
    text-align: right;
}

.msg-bubble {
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 0.95rem;
    line-height: 1.6;
    word-wrap: break-word;
    position: relative;
}

.msg-group.user .msg-bubble {
    background: var(--primary);
    color: var(--primary-foreground);
    border-bottom-right-radius: 4px;
}

.msg-group.model .msg-bubble {
    background: var(--card);
    color: var(--card-foreground);
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

.action-text {
    color: var(--chart-1);
    font-style: italic;
}

.msg-group.user .msg-bubble .action-text {
    color: rgba(255, 255, 255, 0.75);
}

/* Thinking */
.think-block {
    margin-bottom: 4px;
}

.think-block details {
    background: var(--muted);
    border-radius: 8px;
    overflow: hidden;
}

.think-block summary {
    padding: 8px 14px;
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--muted-foreground);
    cursor: pointer;
    user-select: none;
    transition: background 0.15s;
}

.think-block summary:hover {
    background: var(--secondary);
}

.think-block details[open] summary {
    border-bottom: 1px solid var(--border);
}

.think-block .think-body {
    padding: 12px 14px;
    font-size: 0.8rem;
    line-height: 1.5;
    color: var(--muted-foreground);
    font-family: var(--font-mono);
    white-space: pre-wrap;
    max-height: 200px;
    overflow-y: auto;
}

/* Typing */
.typing {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 2px;
    animation: msgIn 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}

.typing .dots {
    display: flex;
    gap: 4px;
}

.typing .dots span {
    width: 7px;
    height: 7px;
    background: var(--input);
    border-radius: 50%;
    animation: bounce 1.2s infinite ease-in-out;
}

.typing .dots span:nth-child(2) {
    animation-delay: 0.15s;
}

.typing .dots span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.4;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

.typing-label {
    font-size: 0.8rem;
    color: var(--muted-foreground);
    font-style: italic;
}

/* ── Chat Input ─────────────────────────────── */
.chat-input-area {
    padding: 12px 20px 16px;
    border-top: 1px solid var(--border);
    background: var(--background);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.edit-bar {
    width: 100%;
    max-width: 1200px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 14px;
    margin-bottom: 8px;
    background: var(--muted);
    border-radius: 8px;
    font-size: 0.82rem;
    color: var(--secondary-foreground);
}

.edit-bar span {
    font-weight: 500;
}

.edit-bar-actions {
    display: flex;
    gap: 6px;
}

.edit-bar-actions button {
    font-size: 0.78rem;
    font-weight: 500;
    padding: 5px 14px;
    border-radius: 6px;
    border: 1px solid var(--border);
    transition: all 0.15s;
}

.edit-bar-actions .eb-cancel {
    background: var(--card);
    color: var(--foreground);
}

.edit-bar-actions .eb-cancel:hover {
    background: var(--secondary);
}

.edit-bar-actions .eb-save {
    background: var(--primary);
    color: var(--primary-foreground);
    border-color: var(--primary);
}

.edit-bar-actions .eb-save:hover {
    background: var(--chart-1);
}

.chat-input-wrap {
    width: 100%;
    max-width: 1200px;
    display: flex;
    gap: 10px;
    align-items: flex-end;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 5px 5px 5px 16px;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.chat-input-wrap:focus-within {
    border-color: var(--ring);
    box-shadow: 0 0 0 3px rgba(201, 100, 66, 0.08);
}

.chat-input-wrap textarea {
    flex: 1;
    border: none;
    background: transparent;
    font-family: var(--font-sans);
    font-size: 0.95rem;
    color: var(--foreground);
    resize: none;
    outline: none;
    line-height: 1.5;
    max-height: 110px;
    padding: 8px 0;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.chat-input-wrap textarea::-webkit-scrollbar {
    display: none;
}

.chat-input-wrap textarea::placeholder {
    color: var(--input);
}

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: var(--primary);
    color: var(--primary-foreground);
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, transform 0.1s;
}

.send-btn svg {
    transform: translate(-1px, 1px);
}

.send-btn:hover {
    background: var(--chart-1);
}

.send-btn:active {
    transform: scale(0.93);
}

.send-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

/* ── Context Menu ───────────────────────────── */
.ctx-menu {
    position: fixed;
    z-index: 900;
    background: var(--popover);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 4px;
    min-width: 180px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    animation: ctxIn 0.15s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes ctxIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

.ctx-menu button {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 9px 14px;
    border: none;
    background: none;
    font-size: 0.85rem;
    font-weight: 450;
    color: var(--popover-foreground);
    border-radius: 7px;
    transition: background 0.12s;
    text-align: left;
}

.ctx-menu button:hover {
    background: var(--accent);
}

.ctx-menu button svg {
    width: 16px;
    height: 16px;
}

.ctx-menu .ctx-sep {
    height: 1px;
    background: var(--border);
    margin: 3px 8px;
}

.ctx-menu button.ctx-danger {
    color: #b91c1c;
}

.ctx-menu button.ctx-danger:hover {
    background: #fef2f2;
}

.ctx-menu button.ctx-danger svg {
    stroke: #b91c1c;
}

/* ── Modal ──────────────────────────────────── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(3px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-box {
    background: var(--popover);
    border-radius: var(--radius);
    padding: 24px;
    max-width: 380px;
    width: 90%;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    transform: scale(0.96);
    transition: transform 0.2s cubic-bezier(0.22, 1, 0.36, 1);
}

.modal-overlay.active .modal-box {
    transform: scale(1);
}

.modal-box h3 {
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--popover-foreground);
    margin-bottom: 8px;
}

.modal-box p {
    font-size: 0.88rem;
    color: var(--muted-foreground);
    line-height: 1.5;
    margin-bottom: 20px;
}

.modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.modal-actions button {
    padding: 8px 18px;
    border-radius: 8px;
    font-size: 0.85rem;
    font-weight: 500;
    border: 1px solid var(--border);
    transition: all 0.15s;
}

.modal-actions .m-cancel {
    background: var(--card);
    color: var(--foreground);
}

.modal-actions .m-cancel:hover {
    background: var(--secondary);
}

.modal-actions .m-confirm {
    background: var(--destructive);
    color: var(--destructive-foreground);
    border-color: var(--destructive);
}

.modal-actions .m-confirm:hover {
    opacity: 0.9;
}

/* ── Responsive ─────────────────────────────── */
.sidebar-close-btn,
.sidebar-head-actions .sidebar-close-btn {
    display: none;
}

@media (max-width: 860px) {
    #app {
        flex-direction: column;
        position: relative;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 340px;
        z-index: 100;
        transform: translateX(-100%);
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.08);
        transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .main-chat {
        width: 100%;
        flex: 1;
        min-height: 0;
    }

    .sidebar-toggle {
        display: flex;
    }

    .sidebar-close-btn,
    .sidebar-head-actions .sidebar-close-btn {
        display: flex;
    }

    .msg-group {
        max-width: 90%;
    }
}

@media (max-width: 480px) {
    .sidebar {
        width: 100%;
        min-width: unset;
    }

    .chat-messages {
        padding: 14px 12px;
    }

    .chat-input-area {
        padding: 10px 12px calc(14px + env(safe-area-inset-bottom));
    }
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.2);
    z-index: 99;
}

.sidebar-overlay.active {
    display: block;
}

.flash {
    animation: statusFlash 0.6s ease;
}

@keyframes statusFlash {
    0% {
        background-color: rgba(201, 100, 66, 0.12);
    }

    100% {
        background-color: transparent;
    }
}

.hidden {
    display: none !important;
}