* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
    color: #1f2937;
    background: #f5f7fb;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 14px;
}

button,
input,
select {
    font: inherit;
}

button {
    cursor: pointer;
}

[hidden] {
    display: none !important;
}

.history-topbar {
    height: 58px;
    display: grid;
    grid-template-columns: 180px 1fr 180px;
    align-items: center;
    padding: 0 22px;
    background: #fff;
    border-bottom: 1px solid #e5e7eb;
}

.brand,
.back-link {
    color: #1677ff;
    text-decoration: none;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
}

.brand img {
    width: 24px;
    height: 24px;
    border-radius: 5px;
}

.topbar-title {
    text-align: center;
    font-weight: 700;
    font-size: 16px;
}

.back-link {
    justify-self: end;
    min-width: 92px;
    height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 16px;
    border: 1px solid #1677ff;
    border-radius: 999px;
    background: #ffffff;
    color: #1677ff;
    font-size: 14px;
    font-weight: 600;
}

.history-shell {
    display: grid;
    grid-template-columns: minmax(360px, 440px) minmax(0, 1fr);
    gap: 16px;
    height: calc(100vh - 58px);
    padding: 16px;
}

.history-list-panel,
.history-detail-panel {
    min-height: 0;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
}

.history-list-panel {
    display: flex;
    flex-direction: column;
}

.filters {
    display: grid;
    gap: 10px;
    padding: 14px;
    border-bottom: 1px solid #edf0f5;
}

.filters input,
.filters select {
    width: 100%;
    height: 36px;
    padding: 0 10px;
    border: 1px solid #d8dee8;
    border-radius: 18px;
    background: #fff;
    color: #1f2937;
}

.filter-row,
.filter-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.filter-actions button,
.pager button,
#shareDialog,
#continueChat {
    height: 34px;
    border: 1px solid #1677ff;
    border-radius: 999px;
    background: #1677ff;
    color: #fff;
}

.filter-actions button:last-child,
.pager button,
#shareDialog {
    border-color: #d8dee8;
    background: #fff;
    color: #334155;
}

.list-meta {
    padding: 10px 14px;
    color: #64748b;
    border-bottom: 1px solid #edf0f5;
}

.history-list {
    min-height: 0;
    overflow: auto;
}

.history-item {
    width: 100%;
    display: block;
    padding: 13px 14px;
    text-align: left;
    border: 0;
    border-bottom: 1px solid #edf0f5;
    background: #fff;
    color: #1f2937;
}

.history-item:hover,
.history-item.active {
    background: #f0f7ff;
}

.item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 6px;
}

.item-type {
    font-weight: 700;
}

.item-time {
    color: #64748b;
    font-size: 12px;
    white-space: nowrap;
}

.item-text {
    display: -webkit-box;
    overflow: hidden;
    color: #334155;
    line-height: 1.5;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.item-model {
    margin-top: 7px;
    color: #64748b;
    font-size: 12px;
}

.item-context {
    display: grid;
    gap: 3px;
    margin-top: 8px;
    padding: 8px 10px;
    border-left: 3px solid #b7d6ff;
    border-radius: 6px;
    background: #f5f9ff;
    color: #52627a;
    font-size: 12px;
}

.item-context span {
    color: #1677ff;
    font-weight: 700;
}

.item-context strong {
    font-weight: 500;
}

.item-context em {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-style: normal;
}

.pager {
    display: grid;
    grid-template-columns: 86px 1fr 86px;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid #edf0f5;
}

.pager span {
    text-align: center;
    color: #64748b;
}

.pager button:disabled,
#shareDialog:disabled,
#continueChat:disabled {
    cursor: not-allowed;
    opacity: .45;
}

.history-detail-panel {
    min-width: 0;
    overflow: hidden;
    position: relative;
}

.detail-empty {
    height: 100%;
    display: grid;
    place-items: center;
    color: #94a3b8;
}

.detail-wrap {
    height: 100%;
    display: flex;
    flex-direction: column;
}

.detail-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 18px;
    border-bottom: 1px solid #edf0f5;
}

.detail-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.detail-actions button {
    min-width: 86px;
    padding: 0 12px;
}

.share-result {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 86px;
    gap: 8px;
    padding: 10px 18px;
    border-bottom: 1px solid #edf0f5;
    background: #f8fbff;
}

.share-result input {
    min-width: 0;
    height: 34px;
    padding: 0 10px;
    border: 1px solid #cdd8e8;
    border-radius: 17px;
    color: #334155;
    background: #fff;
}

.share-result button {
    height: 34px;
    border: 1px solid #1677ff;
    border-radius: 999px;
    background: #1677ff;
    color: #fff;
}

.detail-title {
    font-weight: 700;
    font-size: 16px;
}

.detail-subtitle {
    margin-top: 4px;
    color: #64748b;
    font-size: 12px;
}

.dialog-list {
    min-height: 0;
    overflow: auto;
    padding: 18px;
}

.context-loading {
    position: absolute;
    top: 76px;
    left: 50%;
    z-index: 5;
    min-width: 260px;
    max-width: calc(100% - 36px);
    padding: 10px 14px;
    border: 1px solid #c7d7ee;
    border-radius: 999px;
    background: rgba(255, 255, 255, .96);
    color: #64748b;
    font-size: 13px;
    line-height: 1.6;
    text-align: center;
    box-shadow: 0 8px 24px rgba(15, 23, 42, .14);
    transform: translateX(-50%);
}

.context-loading::before {
    content: "";
    width: 14px;
    height: 14px;
    display: inline-block;
    margin-right: 8px;
    vertical-align: -2px;
    border: 2px solid #d8e7fb;
    border-top-color: #1677ff;
    border-radius: 50%;
    animation: spin .8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.dialog-pair {
    position: relative;
    padding: 8px 10px 2px;
    border-radius: 8px;
}

.dialog-pair.selected {
    background: #f7fbff;
    box-shadow: inset 3px 0 0 #1677ff;
}

.share-check-block {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0 0 10px 42px;
    padding: 7px 10px;
    border: 1px dashed #c7d7ee;
    border-radius: 7px;
    background: #fbfdff;
    color: #64748b;
    font-size: 12px;
}

.share-check-block .turn-continue {
    margin-left: auto;
    height: 28px;
    padding: 0 10px;
    border: 1px solid #1677ff;
    border-radius: 999px;
    background: #fff;
    color: #1677ff;
    font-size: 12px;
    white-space: nowrap;
}

.share-check-block .turn-continue:hover {
    background: #eef6ff;
}

.share-check-line {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    color: #334155;
    font-weight: 700;
    user-select: none;
}

.share-check-block span {
    color: #64748b;
}

.share-check-line input {
    margin: 0;
}

.dialog-turn {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    margin-bottom: 18px;
}

.dialog-turn.user {
    justify-content: flex-end;
}

.dialog-turn.user .avatar {
    order: 2;
    background: #e8f2ff;
    color: #1677ff;
}

.dialog-turn.user .bubble {
    order: 1;
    background: #eaf4ff;
    color: #1f2937;
    border-top-right-radius: 4px;
    white-space: pre-wrap;
}

.dialog-turn.assistant .bubble {
    border-top-left-radius: 4px;
}

.avatar {
    width: 34px;
    height: 34px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: #e8f2ff;
    color: #1677ff;
    font-weight: 700;
}

.avatar img {
    width: 22px;
    height: 22px;
    border-radius: 5px;
}

.bubble {
    max-width: 980px;
    padding: 11px 13px;
    border-radius: 10px;
    background: #f6f8fb;
    line-height: 1.7;
    word-break: break-word;
}

.bubble pre {
    overflow: auto;
    padding: 10px;
    border-radius: 6px;
    background: #0f172a;
    color: #e5e7eb;
}

.bubble img.history-image {
    max-width: 160px;
    max-height: 160px;
    display: inline-block;
    margin-top: 8px;
    margin-right: 8px;
    border-radius: 8px;
    object-fit: cover;
}

.history-file-link {
    display: block;
    max-width: 360px;
    margin-top: 8px;
    padding: 8px 10px;
    border: 1px solid #dbe5f0;
    border-radius: 7px;
    background: #fff;
    color: #1677ff;
    text-decoration: none;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.generated-files {
    display: grid;
    gap: 8px;
    margin-top: 10px;
}

.generated-file-link {
    display: inline-grid;
    max-width: 360px;
    padding: 9px 11px;
    border: 1px solid #cfe0f6;
    border-radius: 7px;
    background: #f8fbff;
    color: #1677ff;
    cursor: pointer;
    font: inherit;
    text-decoration: none;
    text-align: left;
}

.generated-file-link.loading,
.generated-file-link:disabled {
    cursor: wait;
    opacity: .72;
}

.generated-file-link span {
    color: #64748b;
    font-size: 12px;
}

.generated-file-link strong {
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.bubble.draw-bubble {
    background: #f6f8fb;
    color: #1f2937;
}

.draw-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 10px;
    max-width: 820px;
}

.draw-gallery.single {
    max-width: 420px;
    grid-template-columns: 1fr;
}

.draw-gallery a {
    display: block;
    overflow: hidden;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
    background: #fff;
}

.draw-gallery img {
    width: 100%;
    display: block;
    aspect-ratio: 1 / 1;
    object-fit: contain;
}

.draw-prompt {
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 8px;
    background: #fff;
    color: #475569;
    font-size: 13px;
    line-height: 1.6;
    white-space: pre-wrap;
}

.empty-state,
.error-state {
    padding: 18px;
    color: #64748b;
}

.error-state {
    color: #b42318;
}

@media (max-width: 900px) {
    .history-topbar {
        height: auto;
        grid-template-columns: minmax(0, 1fr) auto;
        gap: 10px;
        padding: 10px 12px;
    }

    .topbar-title {
        display: none;
    }

    .history-shell {
        grid-template-columns: 1fr;
        height: auto;
        min-height: calc(100vh - 58px);
        padding: 10px;
        gap: 10px;
    }

    .detail-head {
        align-items: flex-start;
        flex-direction: column;
    }

    .history-list-panel,
    .history-detail-panel {
        min-height: 360px;
    }

    .filters {
        padding: 12px;
        gap: 8px;
    }

    input,
    select,
    button {
        font-size: 16px;
    }

    .history-list {
        max-height: 52vh;
        overflow: auto;
    }

    .history-file-link,
    .generated-file-link {
        max-width: 100%;
    }

    .dialog-turn {
        gap: 7px;
    }

    .avatar {
        width: 30px;
        height: 30px;
    }

    .bubble {
        max-width: calc(100% - 38px);
        padding: 10px 11px;
    }
}

@media (max-width: 480px) {
    .filter-row,
    .filter-actions {
        grid-template-columns: 1fr;
    }

    .detail-actions,
    .share-result {
        width: 100%;
    }

    .share-result {
        grid-template-columns: 1fr;
    }

    #continueChat,
    #shareDialog,
    .share-result button {
        width: 100%;
    }
}
