:root {
    --bg: #141616;
    --panel: #1d2020;
    --panel-alt: #232827;
    --line: #303736;
    --text: #f2f3ee;
    --muted: #b1b8b1;
    --accent: #74c69d;
    --sent: #d97a43;
    --received: #2a2f2e;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
}

body {
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}

body.viewer-open {
    overflow: hidden;
}

.layout {
    display: grid;
    grid-template-columns: 360px minmax(0, 1fr);
    height: 100vh;
    min-height: 0;
}

.chat-sidebar {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--line);
    background: var(--panel);
    min-height: 0;
    overflow: hidden;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 20px;
    border-bottom: 1px solid var(--line);
}

.brand-mark {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: var(--accent);
    color: #10201a;
    display: grid;
    place-items: center;
    font-size: 22px;
    font-weight: 700;
}

.brand-copy h1 {
    font-size: 18px;
    line-height: 1.3;
}

.brand-copy p {
    margin-top: 2px;
    font-size: 13px;
    color: var(--muted);
}

.search {
    display: block;
}

.search input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #111313;
    color: var(--text);
    outline: none;
}

.search input::placeholder {
    color: #86908a;
}

.sidebar-tools {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 160px;
    align-items: end;
    gap: 12px;
    padding: 16px 20px 12px;
}

.sort-control {
    position: relative;
}

.sort-control select {
    width: 100%;
    padding: 10px 34px 10px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #111313;
    color: var(--text);
    outline: none;
    appearance: none;
}

.sort-control::after {
    content: "";
    position: absolute;
    top: 50%;
    right: 14px;
    width: 8px;
    height: 8px;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: translateY(-70%) rotate(45deg);
    pointer-events: none;
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 10px 12px;
    min-height: 0;
}

.chat-row {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
    padding: 12px;
    margin-bottom: 8px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    color: inherit;
    text-align: left;
    cursor: pointer;
}

.chat-row:hover {
    background: rgba(255, 255, 255, 0.04);
}

.chat-row.active {
    background: rgba(116, 198, 157, 0.12);
    border-color: rgba(116, 198, 157, 0.28);
}

.chat-row-body {
    min-width: 0;
}

.chat-row-top {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: baseline;
}

.chat-row-top h3 {
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row-time,
.chat-row-body p,
.chat-list-empty,
.summary-label,
.message-meta,
#conversation-meta {
    color: var(--muted);
    font-size: 12px;
}

.chat-row-body p {
    margin-top: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row-count {
    min-width: 34px;
    padding: 4px 6px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.06);
    text-align: center;
    font-size: 12px;
}

.chat-list-empty {
    padding: 20px 12px;
}

.conversation-panel {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: #171a1a;
    overflow: hidden;
}

.conversation-header {
    display: flex;
    justify-content: space-between;
    gap: 20px;
    align-items: flex-start;
    padding: 20px 24px;
    border-bottom: 1px solid var(--line);
    background: var(--panel-alt);
}

.conversation-header h2 {
    font-size: 22px;
    line-height: 1.3;
}

.conversation-header p {
    margin-top: 4px;
}

.summary {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.summary-item {
    min-width: 88px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #171b1a;
}

.summary-item strong {
    display: block;
    margin-top: 4px;
    font-size: 15px;
}

.message-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 24px;
    min-height: 0;
}

.message {
    max-width: min(760px, 78%);
    padding: 12px 14px;
    border-radius: 8px;
    border: 1px solid transparent;
}

.message.received {
    align-self: flex-start;
    background: var(--received);
    border-color: #38403d;
}

.message.sent {
    align-self: flex-end;
    background: rgba(217, 122, 67, 0.14);
    border-color: rgba(217, 122, 67, 0.34);
}

.message-text {
    font-size: 14px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
}

.message-caption {
    margin-top: 10px;
}

.message-media-trigger {
    position: relative;
    display: inline-block;
    max-width: 100%;
    padding: 0;
    border: 0;
    background: transparent;
    cursor: pointer;
}

.message-media-trigger:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
    border-radius: 8px;
}

.message-image {
    display: block;
    width: auto;
    max-width: min(420px, 100%);
    max-height: 360px;
    object-fit: contain;
    border-radius: 8px;
    background: #101212;
}

.message-video-fallback {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: min(320px, 100%);
    min-height: 156px;
    padding: 18px;
    border: 1px solid #38403d;
    border-radius: 8px;
    background: #101212;
}

.message-video-icon {
    width: 42px;
    height: 42px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.1);
    display: grid;
    place-items: center;
    font-size: 18px;
    flex: 0 0 auto;
}

.message-video-label {
    min-width: 0;
    font-size: 13px;
    line-height: 1.5;
    word-break: break-all;
    text-align: left;
}

.message-media-badge {
    position: absolute;
    right: 10px;
    bottom: 10px;
    padding: 4px 8px;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.7);
    color: #f5f6f2;
    font-size: 12px;
    line-height: 1;
}

.message-attachment {
    display: inline-flex;
    align-items: center;
    margin-top: 10px;
    color: #9ed9ba;
    text-decoration: none;
    font-size: 13px;
}

.message-open-original:hover,
.message-attachment:hover {
    text-decoration: underline;
}

.message-meta {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-top: 8px;
}

.empty-state {
    margin: auto;
    text-align: center;
    color: var(--muted);
}

.empty-state h3 {
    color: var(--text);
    font-size: 20px;
}

.empty-state p {
    margin-top: 8px;
    font-size: 14px;
}

.hidden {
    display: none;
}

.media-viewer {
    position: fixed;
    inset: 0;
    z-index: 1000;
}

.media-viewer-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.84);
}

.media-viewer-shell {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100%;
    padding: 18px 20px 20px;
}

.media-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding-bottom: 14px;
}

.media-toolbar-title {
    min-width: 0;
    font-size: 14px;
    color: #f2f3ee;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.media-toolbar-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.media-toolbar-actions button {
    min-width: 64px;
    padding: 10px 12px;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 8px;
    background: rgba(20, 22, 22, 0.74);
    color: var(--text);
    cursor: pointer;
}

.media-toolbar-actions button:hover {
    background: rgba(39, 45, 44, 0.94);
}

.media-stage {
    position: relative;
    min-height: 0;
    overflow: hidden;
    display: grid;
    place-items: center;
    touch-action: none;
}

.media-stage.is-draggable {
    cursor: grab;
}

.media-stage.is-dragging {
    cursor: grabbing;
}

.viewer-media {
    display: block;
    max-width: min(92vw, 1280px);
    max-height: calc(100vh - 132px);
    object-fit: contain;
    border-radius: 8px;
    user-select: none;
    transform-origin: center center;
    transition: transform 120ms ease;
    will-change: transform;
    box-shadow: 0 20px 80px rgba(0, 0, 0, 0.45);
}

.media-stage.is-dragging .viewer-media {
    transition: none;
}

.viewer-media-image {
    background: #0e1010;
}

.viewer-media-video {
    background: #000;
}

::-webkit-scrollbar {
    width: 8px;
}

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

@media (max-width: 960px) {
    .layout {
        grid-template-columns: 300px minmax(0, 1fr);
    }

    .sidebar-tools {
        grid-template-columns: 1fr;
    }

    .conversation-header {
        flex-direction: column;
    }

    .summary {
        justify-content: flex-start;
    }

    .media-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .media-toolbar-actions {
        justify-content: flex-start;
    }
}
