/* Экотек — гостевой веб-чат. Корпоративная светлая тема. */

:root {
    --accent: #e7472d;
    --accent-hover: #c93622;
    --bg: #f4f6f8;
    --panel: #ffffff;
    --panel-soft: #f9fafb;
    --in: #eef1f5;
    --out: #e7472d;
    --text: #26323f;
    --muted: #687382;
    --border: #dce2e8;
    --shadow: rgba(31, 41, 55, .12);
    --focus: rgba(231, 71, 45, .22);
    --page-gradient: linear-gradient(135deg, #f8fafc 0%, #eef3f7c4 54%, #0600002b 100%);
    --pattern: url("chemistry-pattern.svg?v=13");
    --pattern-opacity: .03;
    color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--page-gradient), var(--bg);
    color: var(--text);
    position: relative; isolation: isolate;
    display: flex; flex-direction: column;
    height: 100dvh; overflow: hidden;
}
body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background-image: var(--pattern);
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    opacity: var(--pattern-opacity);
}
body > * { position: relative; z-index: 1; }
.hidden { display: none !important; }
header {
    flex: 0 0 auto; display: flex; align-items: center; gap: 12px;
    padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
    background: var(--panel); border-bottom: 1px solid var(--border);
    border-top: 4px solid var(--accent);
    box-shadow: 0 2px 12px var(--shadow);
}
header .logo-mark { width: 42px; height: 42px; object-fit: contain; flex: 0 0 auto; }
header .brand { min-width: 0; }
header .t { font-weight: 700; font-size: 17px; color: var(--text); }
header .s { font-size: 12px; color: var(--muted); }
#log {
    flex: 1 1 auto; overflow-y: auto; padding: 16px 12px;
    display: flex; flex-direction: column; gap: 8px;
    -webkit-overflow-scrolling: touch;
}
.msg { max-width: 82%; padding: 9px 12px; border-radius: 8px; font-size: 15px; line-height: 1.35; word-wrap: break-word; white-space: pre-wrap; box-shadow: 0 1px 2px rgba(31, 41, 55, .06); }
.msg.in { align-self: flex-start; background: var(--in); border: 1px solid var(--border); }
.msg.out { align-self: flex-end; background: var(--out); color: #fff; border: 1px solid var(--out); }
.msg-meta { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.msg.in .msg-meta { justify-content: flex-start; }
.msg.out .msg-meta { justify-content: flex-end; }
.msg .time { font-size: 10px; color: var(--muted); }
.msg.out .time { color: #ffe1d7; }
.msg-status { font-size: 11px; line-height: 1; color: #ffe1d7; }
.msg.out[data-status="sending"] .msg-status { opacity: .7; }
.msg.out[data-status="failed"] .msg-status { color: #fff; cursor: pointer; }
.msg.out[data-status="failed"] { opacity: .9; cursor: pointer; }
.state { margin: auto; color: var(--muted); font-size: 14px; text-align: center; padding: 20px; }
footer {
    flex: 0 0 auto; display: flex; align-items: center; gap: 10px; padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
    background: rgba(255, 255, 255, .94); border-top: 1px solid var(--border);
    box-shadow: 0 -8px 24px rgba(31, 41, 55, .06);
}
#txt {
    flex: 1; resize: none; border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px;
    background: var(--panel-soft); color: var(--text); font-size: 15px; max-height: 120px; outline: none;
}
#txt:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
#txt { scrollbar-width: thin; }
#txt::-webkit-scrollbar { width: 8px; }
#txt::-webkit-scrollbar-button { display: none; height: 0; width: 0; }
#txt::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
.composer-btn {
    flex: 0 0 auto; width: 44px; height: 44px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; transition: transform .16s ease, box-shadow .16s ease, border-color .16s ease, background .16s ease, color .16s ease, opacity .16s ease;
}
.composer-btn svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; transition: transform .16s ease; }
#attach {
    border: 1px solid rgba(104, 115, 130, .24);
    background: linear-gradient(180deg, #ffffff 0%, #f3f6f9 100%);
    color: #5f6c7d;
    box-shadow: 0 4px 12px rgba(31, 41, 55, .08);
}
#attach:hover { border-color: rgba(231, 71, 45, .38); color: var(--accent); transform: translateY(-1px); box-shadow: 0 8px 18px rgba(31, 41, 55, .12); }
#attach:hover svg { transform: rotate(-8deg); }
#send {
    width: 48px; height: 48px; border: none;
    background: linear-gradient(135deg, #f15a40 0%, #e7472d 48%, #c93622 100%);
    color: #fff;
    box-shadow: 0 10px 22px rgba(231, 71, 45, .28);
}
#send:hover { transform: translateY(-1px); box-shadow: 0 13px 28px rgba(231, 71, 45, .34); }
#send:hover svg { transform: translateX(2px); }
.composer-btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus), 0 8px 18px rgba(31, 41, 55, .12); }
.composer-btn:active { transform: translateY(0) scale(.98); }
.composer-btn:disabled { opacity: .45; cursor: default; transform: none; box-shadow: none; }
.composer-btn:disabled svg { transform: none; }
.msg-img { max-width: 220px; max-height: 260px; border-radius: 8px; display: block; cursor: pointer; margin-bottom: 2px; }
.msg-file { display: inline-block; color: inherit; text-decoration: underline; font-size: 15px; word-break: break-all; }
#login {
    position: fixed; inset: 0; z-index: 10;
    background: var(--page-gradient), var(--bg);
    overflow: hidden;
    display: flex; align-items: center; justify-content: center; padding: 24px;
}
#login::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background-image: var(--pattern);
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    opacity: var(--pattern-opacity);
}
.login-box { position: relative; z-index: 1; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 12px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 28px 24px 24px; box-shadow: 0 18px 42px var(--shadow); }
.login-logo-img { width: 300px; max-width: 90%; height: auto; display: block; margin: 0 auto 10px; }
.login-box h2 { text-align: center; font-size: 18px; font-weight: 700; margin: 0 0 8px; color: var(--text); }
.login-box input { padding: 13px 14px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-soft);
    color: var(--text); font-size: 16px; outline: none; }
.login-box input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
.login-box button { padding: 13px; border: none; border-radius: 8px; background: var(--accent);
    color: #fff; font-size: 16px; font-weight: 600; cursor: pointer; }
.login-box button:hover { background: var(--accent-hover); }
.login-box button:disabled { opacity: .5; }
#li-err { color: #b42318; font-size: 13px; text-align: center; min-height: 16px; }

@media (max-width: 420px) {
    .login-box { padding: 24px 18px 20px; }
    .login-logo-img { width: 270px; }
    header { padding-left: 14px; padding-right: 14px; }
}

/* Drag & drop overlay — drop a file anywhere over the chat to send it. */
#drop {
    position: fixed; inset: 0; z-index: 20;
    display: flex; align-items: center; justify-content: center; padding: 24px;
    pointer-events: none;
    background: rgba(38, 50, 63, .32); backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
}
#drop .drop-inner {
    display: flex; flex-direction: column; align-items: center; gap: 10px;
    padding: 28px 36px; text-align: center;
    border: 2px dashed var(--accent); border-radius: 14px;
    background: rgba(255, 255, 255, .96); color: var(--text);
    font-size: 16px; font-weight: 600; box-shadow: 0 18px 42px var(--shadow);
}
#drop .drop-inner svg { width: 40px; height: 40px; fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Selection formatting toolbar (markdown). */
.fmtbar {
    position: fixed; z-index: 30; display: flex; gap: 2px; padding: 4px;
    background: var(--panel); border: 1px solid var(--border);
    border-radius: 10px; box-shadow: 0 8px 24px var(--shadow);
}
.fmt-btn {
    min-width: 32px; height: 32px; padding: 0 9px;
    display: flex; align-items: center; justify-content: center;
    border: none; border-radius: 7px; background: transparent;
    color: var(--text); font-size: 14px; line-height: 1; cursor: pointer;
}
.fmt-btn:hover { background: var(--in); color: var(--accent); }
.fmt-btn:active { transform: scale(.95); }

/* Rendered markdown inside message bubbles. */
.msg-text { white-space: normal; }
.msg-text > :first-child { margin-top: 0; }
.msg-text > :last-child { margin-bottom: 0; }
.msg-text p { margin: 0 0 6px; }
.msg-text ul, .msg-text ol { margin: 4px 0; padding-left: 20px; }
.msg-text h1, .msg-text h2, .msg-text h3, .msg-text h4, .msg-text h5, .msg-text h6 { margin: 6px 0 4px; font-size: 1em; font-weight: 700; }
.msg-text code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; background: rgba(38, 50, 63, .08); padding: 1px 5px; border-radius: 4px; }
.msg-text pre { margin: 6px 0; padding: 10px 12px; background: rgba(38, 50, 63, .08); border-radius: 8px; overflow-x: auto; }
.msg-text pre code { background: none; padding: 0; }
.msg-text blockquote { margin: 6px 0; padding: 2px 0 2px 10px; border-left: 3px solid var(--border); color: var(--muted); }
.msg-text a { color: var(--accent); }
.md-table-wrap { overflow-x: auto; }
.md-table { border-collapse: collapse; margin: 6px 0; font-size: .95em; }
.md-table th, .md-table td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }

/* Keep markdown readable on the red outgoing bubble. */
.msg.out .msg-text a { color: #fff; text-decoration: underline; }
.msg.out .msg-text code, .msg.out .msg-text pre { background: rgba(255, 255, 255, .22); }
.msg.out .msg-text blockquote { border-left-color: rgba(255, 255, 255, .5); color: #ffe1d7; }
.msg.out .md-table th, .msg.out .md-table td { border-color: rgba(255, 255, 255, .4); }

/* Header logout + connection state. */
header .icon-btn {
    flex: 0 0 auto; width: 38px; height: 38px;
    border: none; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
header .icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
header .icon-btn:hover { background: var(--in); color: var(--accent); }
#logout { margin-left: auto; }
header .install-btn { margin-left: auto; }
header .install-btn:not(.hidden) + #logout { margin-left: 0; }
header .s.off { color: #c2410c; }

.login-box .install-login {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    background: #fff; color: var(--accent); border: 1px solid rgba(231, 71, 45, .28);
    box-shadow: none;
}
.login-box .install-login svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.1; stroke-linecap: round; stroke-linejoin: round; }
.login-box .install-login:hover { background: #fff5f2; color: var(--accent-hover); }

/* Date separators. */
.day-divider { align-self: center; margin: 6px 0; }
.day-divider span {
    display: inline-block; background: rgba(38, 50, 63, .08); color: var(--muted);
    font-size: 12px; padding: 3px 10px; border-radius: 10px;
}

/* Scroll-to-bottom button with unread badge. */
.scrollbtn {
    position: fixed; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 80px); z-index: 15;
    width: 44px; height: 44px; border: 1px solid var(--border); border-radius: 50%;
    background: var(--panel); color: var(--muted); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 6px 18px var(--shadow);
}
.scrollbtn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.scrollbtn:hover { color: var(--accent); border-color: rgba(231, 71, 45, .38); }
.scrollbtn[data-count]:not([data-count=""])::after {
    content: attr(data-count); position: absolute; top: -5px; right: -5px;
    min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
    background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
    display: flex; align-items: center; justify-content: center;
}

/* Image lightbox. */
#lightbox {
    position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .85);
    display: flex; align-items: center; justify-content: center; padding: 24px;
}
#lightbox img { max-width: 100%; max-height: 84vh; border-radius: 8px; box-shadow: 0 10px 40px rgba(0, 0, 0, .5); }
#lb-close {
    position: absolute; top: calc(env(safe-area-inset-top) + 12px); right: 16px;
    width: 40px; height: 40px; border: none; border-radius: 50%;
    background: rgba(255, 255, 255, .15); color: #fff; font-size: 26px; line-height: 1; cursor: pointer;
}
#lb-dl {
    position: absolute; bottom: calc(env(safe-area-inset-bottom) + 22px); left: 50%; transform: translateX(-50%);
    border: none; border-radius: 8px; padding: 10px 20px; background: var(--accent);
    color: #fff; font-size: 15px; font-weight: 600; cursor: pointer;
}
