:root {
    --green: #06c755;
    --green-dark: #05a647;
    --bubble-me: #8de055;
    --bubble-other: #ffffff;
    --chat-bg: #8ca8d2;
    --rail: #1f2a37;
    --rail-icon: #8b98a9;
    --text: #1e2329;
    --muted: #8a9099;
    --line: #e9ecef;
    --hover: #f3f5f7;
    --danger: #f04c4c;
    --radius: 18px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
    font-family: "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
    color: var(--text);
    background: #fff;
    font-size: 14px;
}
button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; font-size: 14px; }
svg { width: 22px; height: 22px; fill: currentColor; }
[hidden] { display: none !important; }

/* ---------- Auth ---------- */
.auth-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background: linear-gradient(160deg, #06c755 0%, #04a44a 100%);
    padding: 16px;
}
.auth-card {
    width: 100%;
    max-width: 380px;
    background: #fff;
    border-radius: 16px;
    padding: 32px 28px;
    box-shadow: 0 20px 50px rgba(0,0,0,.18);
}
.auth-logo { text-align: center; margin-bottom: 20px; }
.auth-logo h1 { margin: 12px 0 4px; font-size: 22px; }
.auth-logo p { margin: 0; color: var(--muted); }
.auth-card label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.auth-card input {
    width: 100%; padding: 11px 12px; border: 1px solid var(--line);
    border-radius: 8px; outline: none; background: #f7f8fa;
}
.auth-card input:focus { border-color: var(--green); background: #fff; }
.auth-errors ul { margin: 0; padding: 10px 14px 10px 28px; background: #fff1f1; color: #c33; border-radius: 8px; }
.auth-errors.validation-summary-valid { display: none; }
.auth-switch { text-align: center; margin-top: 16px; color: var(--muted); }
.auth-switch a { color: var(--green-dark); font-weight: 600; text-decoration: none; }

.logo-bubble {
    display: inline-grid; place-items: center;
    width: 64px; height: 56px; border-radius: 18px;
    background: var(--green); color: #fff; font-weight: 700; font-size: 22px;
    position: relative;
}
.logo-bubble::after {
    content: ""; position: absolute; bottom: -8px; left: 14px;
    border: 8px solid transparent; border-top-color: var(--green); border-left-color: var(--green);
}
.logo-bubble.big { width: 96px; height: 84px; font-size: 32px; border-radius: 26px; }

.btn-primary {
    display: inline-block; border: 0; background: var(--green); color: #fff;
    padding: 11px 18px; border-radius: 8px; font-weight: 600; font-size: 15px; text-decoration: none; text-align: center;
}
.btn-primary:hover { background: var(--green-dark); }
.btn-primary:disabled { opacity: .5; cursor: default; }
.btn-primary.block { display: block; width: 100%; margin-top: 20px; }
.btn-light {
    border: 1px solid var(--line); background: #fff; padding: 7px 14px; border-radius: 8px; font-size: 13px;
}
.btn-light:hover { background: var(--hover); }
.btn-danger { border: 0; background: var(--danger); color: #fff; padding: 9px 16px; border-radius: 8px; }

/* ---------- App layout ---------- */
.app-body { overflow: hidden; }
.app { display: flex; height: 100vh; height: 100dvh; }

.rail {
    width: 64px; background: var(--rail); display: flex; flex-direction: column; align-items: center;
    padding: 14px 0; gap: 6px; flex-shrink: 0;
}
.rail form { margin: 0; }
.rail-spacer { flex: 1; }
.rail-btn, .rail-avatar {
    position: relative; width: 44px; height: 44px; border: 0; border-radius: 12px;
    background: transparent; color: var(--rail-icon); display: grid; place-items: center;
}
.rail-btn:hover { background: rgba(255,255,255,.08); color: #fff; }
.rail-btn.active { color: var(--green); }
.rail-avatar { margin-bottom: 10px; padding: 0; }
.badge {
    position: absolute; top: 3px; right: 1px; min-width: 18px; height: 18px; padding: 0 5px;
    border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; font-weight: 600;
}

.list-pane {
    width: 320px; border-right: 1px solid var(--line); display: flex; flex-direction: column; flex-shrink: 0; background: #fff;
}
.list-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; }
.list-header h2 { margin: 0; font-size: 20px; }
.search-box {
    margin: 4px 16px 10px; display: flex; align-items: center; gap: 6px; background: #f1f3f5; border-radius: 8px; padding: 0 10px;
}
.search-box svg { width: 18px; height: 18px; fill: var(--muted); }
.search-box input { flex: 1; border: 0; background: transparent; padding: 9px 0; outline: none; }
.list-body { flex: 1; overflow-y: auto; }
.section-label { padding: 12px 16px 6px; font-size: 12px; color: var(--muted); }

.item-list { list-style: none; margin: 0; padding: 0; }
.item {
    display: flex; align-items: center; gap: 12px; padding: 9px 16px; cursor: pointer;
}
.item:hover { background: var(--hover); }
.item.active { background: #eaf9f0; }
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.item-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 11px; color: var(--muted); }
.unread { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--green); color: #fff; font-size: 11px; line-height: 20px; text-align: center; font-weight: 600; }
.me-card { display: flex; align-items: center; gap: 12px; padding: 10px 16px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.me-card .item-name { font-size: 16px; }

.avatar {
    position: relative; flex-shrink: 0; width: 44px; height: 44px; border-radius: 50%;
    display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 17px; background: #9aa5b1;
}
.avatar img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.avatar.sm { width: 34px; height: 34px; font-size: 14px; }
.avatar.lg { width: 96px; height: 96px; font-size: 36px; }
.avatar.group svg { width: 24px; height: 24px; }
.avatar .dot {
    position: absolute; right: 0; bottom: 0; width: 12px; height: 12px; border-radius: 50%;
    background: var(--green); border: 2px solid #fff;
}

.icon-btn {
    border: 0; background: transparent; color: #5b6470; width: 38px; height: 38px; border-radius: 10px;
    display: inline-grid; place-items: center; flex-shrink: 0; cursor: pointer;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }

/* ---------- Chat pane ---------- */
.chat-pane { flex: 1; min-width: 0; position: relative; background: #f7f8fa; }
.empty-state { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); gap: 14px; }
.chat { height: 100%; display: flex; flex-direction: column; background: var(--chat-bg); position: relative; }
.chat-header {
    display: flex; align-items: center; gap: 4px; padding: 10px 12px 10px 18px; background: #fff; border-bottom: 1px solid var(--line);
}
.chat-title { flex: 1; min-width: 0; }
.chat-title .name { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title .sub { font-size: 12px; color: var(--muted); }
.back-btn { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px 18px 8px; display: flex; flex-direction: column; gap: 6px; }
.date-sep { align-self: center; margin: 10px 0 4px; padding: 3px 12px; border-radius: 12px; background: rgba(0,0,0,.18); color: #fff; font-size: 12px; }
.sys-msg { align-self: center; padding: 4px 14px; border-radius: 12px; background: rgba(0,0,0,.18); color: #fff; font-size: 12px; text-align: center; max-width: 80%; }

.msg { display: flex; align-items: flex-end; gap: 6px; max-width: 78%; }
.msg.other { align-self: flex-start; }
.msg.me { align-self: flex-end; flex-direction: row-reverse; }
.msg .avatar { align-self: flex-start; }
.msg.cont .avatar { visibility: hidden; height: 0; }
.msg-col { display: flex; flex-direction: column; min-width: 0; }
.msg.me .msg-col { align-items: flex-end; }
.sender { font-size: 12px; color: #fff; margin: 0 0 3px 4px; text-shadow: 0 1px 1px rgba(0,0,0,.15); }
.msg.cont .sender { display: none; }
.bubble-row { display: flex; align-items: flex-end; gap: 6px; }
.msg.me .bubble-row { flex-direction: row-reverse; }
.bubble {
    padding: 8px 13px; border-radius: var(--radius); background: var(--bubble-other); word-wrap: break-word;
    overflow-wrap: anywhere; white-space: pre-wrap; line-height: 1.45; position: relative; max-width: 100%;
}
.msg.me .bubble { background: var(--bubble-me); }
.msg.other:not(.cont) .bubble { border-top-left-radius: 4px; }
.msg.me:not(.cont) .bubble { border-top-right-radius: 4px; }
.bubble a { color: #0b63ce; }
.meta { display: flex; flex-direction: column; font-size: 10.5px; color: rgba(255,255,255,.95); white-space: nowrap; line-height: 1.3; }
.msg.me .meta { align-items: flex-end; }
.read-label { color: #fff; }

.bubble.sticker { background: transparent !important; padding: 0; font-size: 84px; line-height: 1.1; }
.bubble.image { padding: 0; background: transparent !important; }
.bubble.image img { display: block; max-width: 260px; max-height: 320px; border-radius: 14px; cursor: zoom-in; background: #ddd; }
.bubble.file { display: flex; align-items: center; gap: 10px; min-width: 200px; text-decoration: none; color: var(--text); }
.bubble.file .file-icon { width: 40px; height: 40px; border-radius: 10px; background: #eef1f5; display: grid; place-items: center; color: #5b6470; flex-shrink: 0; }
.msg.me .bubble.file .file-icon { background: rgba(255,255,255,.5); }
.bubble.file .file-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 190px; }
.bubble.file .file-size { font-size: 12px; color: var(--muted); }
.bubble.call { display: flex; align-items: center; gap: 10px; }
.bubble.call .call-ico { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: #eef1f5; }
.bubble.call.missed .call-ico { color: var(--danger); }
.bubble.call small { display: block; color: var(--muted); font-size: 12px; }

.typing { padding: 0 20px 6px; color: #fff; font-size: 12px; }
.load-more { align-self: center; color: #fff; font-size: 12px; opacity: .8; padding: 4px; }

.composer { display: flex; align-items: flex-end; gap: 4px; padding: 8px 10px; background: #fff; border-top: 1px solid var(--line); }
.composer textarea {
    flex: 1; resize: none; border: 0; outline: none; padding: 9px 10px; max-height: 140px; line-height: 1.4;
    background: #f1f3f5; border-radius: 18px;
}
.send-btn { border: 0; width: 38px; height: 38px; border-radius: 50%; background: var(--green); color: #fff; display: grid; place-items: center; }
.send-btn svg { width: 18px; height: 18px; }
.send-btn:disabled { background: #c9ced4; }

.sticker-panel {
    background: #fff; border-top: 1px solid var(--line); height: 220px; overflow-y: auto;
    display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 4px; padding: 10px;
}
.sticker-panel button { border: 0; background: transparent; font-size: 42px; border-radius: 12px; padding: 4px; }
.sticker-panel button:hover { background: var(--hover); transform: scale(1.08); }

.drop-hint {
    position: absolute; inset: 0; background: rgba(6,199,85,.85); color: #fff; font-size: 22px; font-weight: 700;
    display: grid; place-items: center; z-index: 5; pointer-events: none;
}

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { width: 100%; max-width: 420px; max-height: 86vh; background: #fff; border-radius: 16px; display: flex; flex-direction: column; overflow: hidden; }
.modal header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal header h3 { margin: 0; font-size: 17px; }
.modal header .icon-btn { font-size: 24px; }
.modal-body { padding: 16px; overflow-y: auto; }
.modal-body .field { margin-bottom: 12px; }
.modal-body .field label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }
.modal-body input[type=text], .modal-body textarea {
    width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 10px; outline: none;
}
.modal-body input[type=text]:focus { border-color: var(--green); }
.modal-body .item { padding: 8px 4px; }
.modal-body .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.profile-view { text-align: center; }
.profile-view .avatar { margin: 6px auto 12px; }
.profile-view h2 { margin: 0; font-size: 20px; }
.profile-view p { color: var(--muted); margin: 6px 0 18px; }
.profile-actions { display: flex; justify-content: center; gap: 22px; }
.profile-actions button { border: 0; background: transparent; display: flex; flex-direction: column; align-items: center; gap: 4px; color: var(--text); font-size: 12px; }
.profile-actions button span { width: 48px; height: 48px; border-radius: 50%; background: #eef1f5; display: grid; place-items: center; }
.check-item { display: flex; align-items: center; gap: 12px; padding: 7px 4px; cursor: pointer; }
.check-item input { width: 18px; height: 18px; accent-color: var(--green); }
.muted { color: var(--muted); }

/* ---------- Calls ---------- */
.incoming { position: fixed; inset: 0; z-index: 70; background: rgba(15,23,32,.75); display: grid; place-items: center; }
.incoming-card { text-align: center; color: #fff; }
.incoming-name { font-size: 24px; font-weight: 700; margin-top: 16px; }
.incoming-type { opacity: .8; margin-top: 4px; }
.pulse-avatar { display: inline-block; border-radius: 50%; animation: pulse 1.6s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(6,199,85,.6); } 70% { box-shadow: 0 0 0 28px rgba(6,199,85,0); } 100% { box-shadow: 0 0 0 0 rgba(6,199,85,0); } }
.call-actions { display: flex; gap: 60px; justify-content: center; margin-top: 40px; }
.call-btn { width: 62px; height: 62px; border-radius: 50%; border: 0; color: #fff; display: grid; place-items: center; }
.call-btn svg { width: 28px; height: 28px; }
.call-btn.accept { background: var(--green); }
.call-btn.reject { background: var(--danger); }
.call-btn.ghost { background: rgba(255,255,255,.18); }
.call-btn.ghost.off { background: #fff; color: #222; }

.call-screen { position: fixed; inset: 0; z-index: 60; background: #111820; color: #fff; }
#remoteVideo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
#localVideo {
    position: absolute; right: 18px; top: 18px; width: 200px; max-width: 32vw; aspect-ratio: 4/3; object-fit: cover;
    border-radius: 12px; background: #222; box-shadow: 0 6px 20px rgba(0,0,0,.4); transform: scaleX(-1);
}
#localVideo.screen { transform: none; }
.call-info { position: absolute; top: 18%; left: 0; right: 0; text-align: center; }
.call-peer { font-size: 26px; font-weight: 700; margin-top: 16px; }
.call-status { opacity: .8; margin-top: 6px; }
.call-screen.has-video .call-info { top: 24px; left: 24px; right: auto; text-align: left; }
.call-screen.has-video .call-info .pulse-avatar { display: none; }
.call-screen.has-video .call-peer { font-size: 18px; margin: 0; text-shadow: 0 1px 4px #000; }
.call-controls { position: absolute; bottom: 36px; left: 0; right: 0; display: flex; justify-content: center; gap: 22px; }

.toast {
    position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); background: rgba(20,26,33,.92); color: #fff;
    padding: 10px 18px; border-radius: 20px; z-index: 90; font-size: 13.5px; max-width: 90vw;
}

/* ---------- Call recording ---------- */
.rec-badges { position: absolute; top: 18px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 6px; z-index: 2; }
.rec-badge { background: rgba(240,76,76,.92); color: #fff; padding: 5px 14px; border-radius: 16px; font-size: 13px; font-weight: 600; animation: blink 1.4s infinite; }
.rec-badge.peer { background: rgba(0,0,0,.6); color: #ff8a8a; animation: none; }
@keyframes blink { 50% { opacity: .55; } }
#btnRecord svg { fill: #ff5b5b; }
#btnRecord.off { background: #ff5b5b; }
#btnRecord.off svg { fill: #fff; }
.rec-item { display: flex; align-items: center; gap: 12px; padding: 10px 4px; border-bottom: 1px solid var(--line); }
.rec-item:last-child { border-bottom: 0; }
.rec-ico { width: 42px; height: 42px; border-radius: 10px; background: #eaf9f0; color: var(--green-dark); display: grid; place-items: center; flex-shrink: 0; font-size: 20px; }
.rec-actions { display: flex; gap: 4px; }
.rec-actions .icon-btn.del:hover { color: var(--danger); background: #fff1f1; }
.rec-player video, .rec-player audio { width: 100%; border-radius: 10px; background: #000; max-height: 60vh; }
.modal.wide { max-width: 760px; }

/* ---------- Remote control ---------- */
.rec-badge.remote { background: rgba(255,153,0,.95); animation: none; display: flex; align-items: center; gap: 8px; }
.rec-badge.remote button { border: 0; background: #fff; color: #c25e00; border-radius: 10px; padding: 2px 10px; font-weight: 700; }
#btnControl.off { background: var(--green); }

.remote-bar {
    position: absolute; top: 8px; right: 8px; z-index: 3;
    display: flex; align-items: center; gap: 8px; padding: 6px 8px 6px 14px; border-radius: 22px;
    background: rgba(20,26,33,.88); box-shadow: 0 4px 16px rgba(0,0,0,.4); max-width: calc(100vw - 300px);
}
.remote-title { white-space: nowrap; font-size: 13px; }
.remote-bar input { width: 320px; max-width: 40vw; border: 0; border-radius: 14px; padding: 6px 12px; outline: none; }
.remote-bar button { border: 0; background: var(--danger); color: #fff; border-radius: 14px; padding: 6px 14px; font-weight: 600; white-space: nowrap; }

.call-screen.controlling #remoteVideo { cursor: crosshair; }
.call-screen.controlling #localVideo, .call-screen.controlling .call-info { display: none; }
.call-screen.controlling .call-controls { bottom: auto; top: 8px; left: 8px; right: auto; gap: 6px; opacity: .45; transition: opacity .2s; }
.call-screen.controlling .call-controls:hover { opacity: 1; }
.call-screen.controlling .call-btn { width: 38px; height: 38px; }
.call-screen.controlling .call-btn svg { width: 18px; height: 18px; }

.remote-prompt {
    position: absolute; inset: 0; z-index: 5; background: rgba(0,0,0,.55); display: grid; place-items: center; padding: 16px;
}
.remote-card { background: #fff; color: var(--text); border-radius: 16px; padding: 22px; width: 100%; max-width: 420px; }
.remote-card h3 { margin: 0 0 6px; font-size: 18px; }
.remote-card p { margin: 6px 0; color: #444; line-height: 1.5; }
.remote-card .warn { background: #fff6e5; color: #8a5300; border-radius: 10px; padding: 10px 12px; font-size: 13px; }
.remote-card .ok { background: #eaf9f0; color: #05793a; border-radius: 10px; padding: 10px 12px; font-size: 13px; }
.remote-card .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.remote-card a { color: #0b63ce; }
.remote-card .warn code { background: #fff; border: 1px solid #f0d9a8; border-radius: 6px; padding: 1px 6px; font-size: 12.5px; user-select: all; }
.agent-fields { margin: 8px 0; border-collapse: collapse; }
.agent-fields td { padding: 2px 10px 2px 0; vertical-align: top; }
.agent-cmd { margin: 8px 0 4px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.agent-cmd code { word-break: break-all; }
.agent-cmd .btn-light { padding: 3px 10px; font-size: 12px; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
    .rail { width: 56px; }
    .list-pane { flex: 1; width: auto; border-right: 0; }
    .chat-pane { display: none; }
    .app.chat-open .list-pane { display: none; }
    .app.chat-open .rail { display: none; }
    .app.chat-open .chat-pane { display: block; }
    .back-btn { display: inline-grid; }
    .chat-header { padding-left: 4px; }
    .msg { max-width: 88%; }
    .bubble.image img { max-width: 200px; }
}
