/* ---- base ---------------------------------------------------------------- */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
    --green: #06c755;
    --green-d: #05a948;
    --bg: #eef1f4;
    --panel: #ffffff;
    --line: #e3e7ec;
    --text: #1f2937;
    --muted: #8a93a0;
    --danger: #e5484d;
    --topbar-h: 54px;
}
html, body { height: 100%; }
body {
    font-family: -apple-system, "Segoe UI", "Helvetica Neue", "Sarabun", Tahoma, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
}
a { color: var(--green-d); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; }

/* ---- topbar -------------------------------------------------------------- */
.topbar {
    height: var(--topbar-h);
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 0 18px;
    position: sticky;
    top: 0;
    z-index: 20;
}
.brand { font-weight: 700; font-size: 16px; }
.mainnav { display: flex; gap: 6px; flex: 1; }
.mainnav a {
    padding: 7px 14px;
    border-radius: 8px;
    color: var(--text);
    font-weight: 500;
}
.mainnav a:hover { background: var(--bg); text-decoration: none; }
.mainnav a.on { background: var(--green); color: #fff; }
.userbox { display: flex; align-items: center; gap: 10px; }
.uname { font-weight: 600; }
.urole {
    font-size: 11px; text-transform: uppercase; color: var(--muted);
    border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px;
}
.logout { color: var(--danger); font-weight: 500; }

/* ---- generic page -------------------------------------------------------- */
.page { display: flex; flex-direction: column; min-height: calc(100vh - var(--topbar-h)); }

/* ---- auth ---------------------------------------------------------------- */
.auth-body {
    display: flex; align-items: center; justify-content: center;
    min-height: 100vh; padding: 20px;
    background: linear-gradient(135deg, #06c755 0%, #038a3c 100%);
}
.auth-card {
    background: var(--panel); width: 100%; max-width: 380px;
    border-radius: 16px; padding: 32px 28px;
    box-shadow: 0 18px 50px rgba(0,0,0,.25);
    text-align: center;
}
.auth-logo { font-size: 40px; }
.auth-card h1 { font-size: 22px; margin-top: 6px; }
.auth-sub { color: var(--muted); margin-bottom: 20px; }
.auth-card label, .form-grid label { display: block; text-align: left; margin-bottom: 12px; font-weight: 500; }
.auth-card .hint { text-align: left; }

/* ---- form controls ------------------------------------------------------- */
input[type=text], input[type=password], input[type=search], select, textarea {
    width: 100%; margin-top: 4px;
    padding: 9px 11px;
    border: 1px solid var(--line); border-radius: 9px;
    font: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--green);
    box-shadow: 0 0 0 3px rgba(6,199,85,.15);
}
textarea { resize: vertical; }
label.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 500; }
label.checkbox input { width: auto; margin: 0; }

.btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 9px 16px; border-radius: 9px;
    border: 1px solid var(--line); background: #fff; color: var(--text);
    font: inherit; font-weight: 600; cursor: pointer;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-d); }
.btn.danger { color: var(--danger); border-color: #f0c4c5; }
.btn.danger:hover { background: #fdeaea; }
.btn.block { width: 100%; margin-top: 8px; }
.btn.small { padding: 5px 10px; font-size: 12px; border-radius: 7px; }

/* ---- alerts / badges ----------------------------------------------------- */
.alert { padding: 9px 12px; border-radius: 9px; margin-bottom: 14px; text-align: left; font-size: 13px; }
.alert.error { background: #fdeaea; color: #b42318; }
.alert.info  { background: #e6f6ec; color: #05702f; }
.badge {
    display: inline-block; font-size: 11px; font-weight: 600;
    padding: 2px 8px; border-radius: 20px; background: var(--bg); color: var(--muted);
}
.badge.ok    { background: #e6f6ec; color: #05702f; }
.badge.off   { background: #fdeaea; color: #b42318; }
.badge.admin { background: #e8eefc; color: #2a4bd0; }
.hint { color: var(--muted); font-size: 13px; }

/* ---- admin pages --------------------------------------------------------- */
.admin { max-width: 920px; margin: 0 auto; padding: 24px 18px; width: 100%; }
.admin h1 { font-size: 20px; margin-bottom: 16px; }
.card {
    background: var(--panel); border: 1px solid var(--line);
    border-radius: 14px; padding: 20px; margin-bottom: 20px;
}
.card h2 { font-size: 15px; margin-bottom: 14px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 18px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 10px; margin-top: 6px; }

.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td {
    text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line);
    vertical-align: middle;
}
.data-table th { font-size: 12px; color: var(--muted); text-transform: uppercase; }
.data-table small { color: var(--muted); }
.data-table code.copyable { word-break: break-all; background: var(--bg); padding: 2px 6px; border-radius: 5px; }
.row-actions { display: flex; gap: 6px; }
.row-actions form { display: inline; }

/* ---- chat layout --------------------------------------------------------- */
.chat { display: flex; flex: 1; height: calc(100vh - var(--topbar-h)); }

.convlist {
    width: 320px; flex-shrink: 0;
    background: var(--panel); border-right: 1px solid var(--line);
    display: flex; flex-direction: column;
}
.convlist-head { padding: 12px; border-bottom: 1px solid var(--line); }
.filter-row { display: flex; gap: 6px; margin-top: 8px; }
.filter-row select { padding: 6px 8px; font-size: 12px; }

.conv-items { list-style: none; overflow-y: auto; flex: 1; }
.conv-item {
    display: flex; gap: 10px; padding: 11px 12px;
    border-bottom: 1px solid var(--line); cursor: pointer;
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: #e6f6ec; }
.conv-avatar {
    width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0;
    background: var(--bg); overflow: hidden;
    display: flex; align-items: center; justify-content: center;
    font-size: 18px; color: var(--muted);
}
.conv-avatar img {
    width: 100%; height: 100%; object-fit: cover; border-radius: 50%;
}
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.conv-preview {
    color: var(--muted); font-size: 13px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-meta { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.conv-oa { font-size: 10px; background: var(--bg); color: var(--muted); padding: 1px 6px; border-radius: 10px; }
.conv-unread {
    margin-left: auto; background: var(--green); color: #fff;
    font-size: 11px; font-weight: 700; min-width: 18px; height: 18px;
    border-radius: 10px; display: flex; align-items: center; justify-content: center; padding: 0 5px;
}
.conv-empty, .conv-loading { padding: 24px; text-align: center; color: var(--muted); }

/* ---- thread -------------------------------------------------------------- */
.thread { flex: 1; display: flex; flex-direction: column; background: #d9e5ee; min-width: 0; }
.thread-empty {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center; color: var(--muted); gap: 6px;
}
.thread-empty-icon { font-size: 48px; }

.thread-head {
    background: var(--panel); border-bottom: 1px solid var(--line);
    padding: 10px 16px; display: flex; align-items: center; gap: 12px;
}
.thread-head .conv-avatar { width: 38px; height: 38px; font-size: 15px; }
.thread-title { font-weight: 700; }
.thread-sub { font-size: 12px; color: var(--muted); }
.thread-actions { margin-left: auto; display: flex; gap: 6px; }

.thread-body { flex: 1; overflow-y: auto; padding: 18px 16px; display: flex; flex-direction: column; gap: 4px; }

.msg { display: flex; flex-direction: column; max-width: 70%; margin-bottom: 6px; }
.msg.in  { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble {
    padding: 8px 12px; border-radius: 14px;
    word-wrap: break-word; white-space: pre-wrap; font-size: 14px;
}
.msg.in  .bubble { background: #fff; border-bottom-left-radius: 4px; }
.msg.out .bubble { background: var(--green); color: #fff; border-bottom-right-radius: 4px; }
.bubble img, .bubble video { max-width: 240px; border-radius: 10px; display: block; }
.bubble.media { padding: 4px; background: transparent !important; }
.bubble.placeholder { font-size: 13px; }
.placeholder-note { font-size: 11px; opacity: .65; margin-top: 3px; }
.bubble.sticker { background: transparent !important; padding: 0; }
.bubble.sticker img { width: 130px; }
.bubble a { color: inherit; text-decoration: underline; }
.msg-meta { font-size: 11px; color: #5a6b78; margin-top: 2px; padding: 0 4px; }
.msg.out .msg-meta { color: #4a6b58; }
.day-sep { align-self: center; background: rgba(255,255,255,.7); color: #5a6b78;
    font-size: 12px; padding: 2px 12px; border-radius: 12px; margin: 8px 0; }

/* ---- composer ------------------------------------------------------------ */
.composer {
    background: var(--panel); border-top: 1px solid var(--line);
    padding: 10px 14px; display: flex; gap: 10px; align-items: flex-end;
}
.composer textarea {
    flex: 1; border-radius: 20px; max-height: 120px; padding: 9px 14px;
}
.composer .btn { border-radius: 20px; height: 38px; }
.composer.disabled { opacity: .55; pointer-events: none; }
.toast {
    position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
    background: #1f2937; color: #fff; padding: 9px 16px; border-radius: 10px;
    font-size: 13px; z-index: 50; opacity: 0; transition: opacity .2s;
}
.toast.show { opacity: 1; }

@media (max-width: 720px) {
    .convlist { width: 100%; }
    .convlist.hide-mobile { display: none; }
    .thread.hide-mobile { display: none; }
    .form-grid { grid-template-columns: 1fr; }
}
