:root {
    color-scheme: light dark;
    --bg: #f7f2ed;
    --surface: #ffffff;
    --surface-2: #f2e9e2;
    --text: #1f1b18;
    --muted: #6f6259;
    --primary: #ff6a00;
    --primary-ink: #ffffff;
    --line: #e5dbd3;
    --danger: #b3261e;
    --success: #1e8e5a;
    --shadow: 0 10px 30px rgba(52, 32, 20, .08);
    --radius: 20px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #17120f;
        --surface: #211a16;
        --surface-2: #2b211c;
        --text: #ede0da;
        --muted: #d8c2b7;
        --line: #49372e;
        --shadow: 0 12px 32px rgba(0, 0, 0, .28);
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
h1, h2, p { margin: 0; }
.muted { color: var(--muted); }
.hidden { display: none; }

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px max(16px, env(safe-area-inset-left)) 12px max(16px, env(safe-area-inset-right));
    background: color-mix(in srgb, var(--bg) 86%, transparent);
    backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border-radius: 14px;
    background: linear-gradient(135deg, #ff8a00, #ff4d00 55%, #7b3fe4);
    color: #fff;
    font-size: 12px;
    font-weight: 800;
    letter-spacing: .4px;
    box-shadow: 0 8px 20px rgba(255, 90, 0, .24);
}
.brand-mark.big { width: 64px; height: 64px; border-radius: 20px; font-size: 16px; }

.shell { width: min(920px, 100%); margin: 0 auto; padding: 18px 16px 40px; }
.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 6px 0 16px;
}
.page-head h1 { font-size: clamp(28px, 6vw, 38px); letter-spacing: -.04em; }
.page-head p { margin-top: 4px; font-size: 14px; }

.count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 26px;
    height: 26px;
    padding: 0 8px;
    border-radius: 999px;
    background: var(--primary);
    color: var(--primary-ink);
    font-size: 12px;
    font-weight: 700;
}
.count:empty, .count[data-zero="1"] { display: none; }

.filters {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 8px;
    margin-bottom: 14px;
}
.filters input, .filters select {
    min-height: 46px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--surface);
    color: var(--text);
    padding: 0 14px;
    outline: none;
}
.filters input:focus, .filters select:focus { border-color: var(--primary); }

.chat-list { display: grid; gap: 10px; }
.chat-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    transition: transform .15s ease, border-color .15s ease;
}
.chat-row:active { transform: scale(.985); }
.chat-row.unread { border-color: color-mix(in srgb, var(--primary) 55%, var(--line)); }
.avatar {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 15px;
    background: linear-gradient(135deg, #ff8a00, #ff4d00 60%, #7b3fe4);
    color: #fff;
    font-size: 14px;
    font-weight: 800;
}
.chat-main { min-width: 0; flex: 1; display: grid; gap: 4px; }
.row-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.row-top strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-top time { flex: 0 0 auto; color: var(--muted); font-size: 12px; }
.preview {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted);
    font-size: 14px;
}
.chat-meta { display: flex; gap: 8px; align-items: center; color: var(--muted); font-size: 11px; }
.status {
    display: inline-flex;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--surface-2);
    font-size: 11px;
    font-weight: 600;
}
.status.resolved { color: var(--success); }
.status.open { color: var(--primary); }
.empty { padding: 34px 16px; text-align: center; color: var(--muted); }

.primary, .ghost, .send {
    border: 0;
    border-radius: 14px;
    min-height: 42px;
    padding: 0 16px;
    font-weight: 650;
}
.primary, .send { background: var(--primary); color: var(--primary-ink); }
.ghost { background: var(--surface-2); color: var(--text); }
.primary:active, .send:active, .ghost:active { transform: scale(.98); }
button:disabled { opacity: .55; cursor: default; }

.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login-wrap { width: min(420px, 100%); }
.login-card {
    display: grid;
    justify-items: center;
    gap: 8px;
    padding: 30px 22px;
    border: 1px solid var(--line);
    border-radius: 28px;
    background: var(--surface);
    box-shadow: var(--shadow);
    text-align: center;
}
.login-card h1 { margin-top: 8px; font-size: 30px; letter-spacing: -.04em; }
.stack { display: grid; gap: 14px; width: 100%; margin-top: 16px; text-align: left; }
.stack label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.stack input {
    min-height: 48px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--bg);
    color: var(--text);
    padding: 0 14px;
    outline: none;
}
.stack input:focus { border-color: var(--primary); }
.alert {
    width: 100%;
    margin-top: 8px;
    padding: 12px 14px;
    border-radius: 14px;
    background: color-mix(in srgb, var(--danger) 12%, var(--surface));
    color: var(--danger);
    font-size: 13px;
}

.chat-page { height: 100dvh; overflow: hidden; }
.chat-page .topbar { display: none; }
.chat-shell {
    height: 100dvh;
    width: min(920px, 100%);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    padding-top: env(safe-area-inset-top, 0px);
}
.chat-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px max(12px, env(safe-area-inset-left)) 10px max(12px, env(safe-area-inset-right));
    border-bottom: 1px solid var(--line);
    background: color-mix(in srgb, var(--bg) 90%, transparent);
    backdrop-filter: blur(14px);
}
.back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--surface-2);
    font-size: 22px;
}
.who { min-width: 0; flex: 1; display: grid; gap: 2px; }
.who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who .muted { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inline { margin: 0; }
.chat-head .ghost { min-height: 38px; padding: 0 12px; font-size: 13px; }

.messages {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 18px max(14px, env(safe-area-inset-left)) 12px max(14px, env(safe-area-inset-right));
    scroll-behavior: smooth;
}
.msg { display: flex; }
.msg.admin { justify-content: flex-start; }
.msg.user { justify-content: flex-end; }
.bubble {
    max-width: min(76ch, 84%);
    padding: 10px 14px;
    border-radius: 20px;
    background: var(--surface-2);
    border: 1px solid var(--line);
}
.msg.user .bubble {
    background: linear-gradient(135deg, #ff7a00, #ff5a00);
    color: #fff;
    border-color: transparent;
}
.text { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.42; }
.meta {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 4px;
    font-size: 10.5px;
    opacity: .72;
}
.msg.user .meta { color: #fff; }

.composer {
    border-top: 1px solid var(--line);
    background: color-mix(in srgb, var(--bg) 93%, transparent);
    backdrop-filter: blur(16px);
    padding: 10px max(12px, env(safe-area-inset-left)) calc(10px + var(--safe-bottom)) max(12px, env(safe-area-inset-right));
    display: grid;
    gap: 8px;
}
.quick { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.quick::-webkit-scrollbar { display: none; }
.chip {
    flex: 0 0 auto;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    padding: 7px 12px;
    font-size: 12px;
    white-space: nowrap;
}
.input-row { display: flex; align-items: flex-end; gap: 8px; }
#message-input {
    flex: 1;
    min-height: 44px;
    max-height: 128px;
    resize: none;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--surface);
    color: var(--text);
    padding: 11px 14px;
    outline: none;
    line-height: 1.4;
}
#message-input:focus { border-color: var(--primary); }
.send { min-width: 104px; border-radius: 999px; }
.send-status { min-height: 16px; font-size: 12px; color: var(--muted); }
.send-status.error { color: var(--danger); }
.send-status.ok { color: var(--success); }

@media (max-width: 560px) {
    .filters { grid-template-columns: 1fr auto; }
    .filters .primary { grid-column: 1 / -1; }
    .chat-page .topbar { display: none; }
    .send { min-width: 88px; padding: 0 14px; }
}
