:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f8f9fb;
  --text: #1b1f24;
  --muted: #6b7280;
  --line: #e4e7ec;
  --accent: #2563eb;
  --accent-2: #1d4ed8;
  --accent-soft: #e8efff;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --side: #111827;
  --side-2: #1f2937;
  --side-text: #e5e7eb;
  --side-muted: #9ca3af;
  --bubble-me: #2563eb;
  --bubble-me-text: #ffffff;
  --bubble: #ffffff;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(15, 23, 42, .12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f17;
    --panel: #121826;
    --panel-2: #0f1522;
    --text: #e6e9ef;
    --muted: #94a3b8;
    --line: #243044;
    --accent: #3b82f6;
    --accent-2: #60a5fa;
    --accent-soft: #172554;
    --side: #070a11;
    --side-2: #141b2a;
    --bubble: #1a2233;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Noto Sans Thai", "Sarabun", "Leelawadee UI", "Segoe UI", Tahoma, system-ui, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; background: var(--accent); color: #fff;
  padding: 9px 16px; border-radius: 10px; font-weight: 600; transition: background .15s, opacity .15s;
}
.btn:hover { background: var(--accent-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.ghost:hover { background: var(--panel-2); }
.btn.danger { background: var(--danger); }
.btn.danger:hover { background: #b91c1c; }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.icon-btn {
  background: transparent; border: 0; width: 36px; height: 36px; border-radius: 9px;
  display: inline-flex; align-items: center; justify-content: center; font-size: 18px; color: inherit;
}
.icon-btn:hover { background: rgba(127, 127, 127, .15); }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 600; }
.input, .field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel); outline: none; transition: border-color .15s, box-shadow .15s;
}
.input:focus, .field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.check input { width: 17px; height: 17px; accent-color: var(--accent); }

/* toast */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 1000; }
.toast {
  background: var(--side); color: #fff; padding: 11px 16px; border-radius: 10px; box-shadow: var(--shadow);
  max-width: 340px; animation: pop .2s ease-out; font-size: 14px;
}
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* modal */
.modal-bg {
  position: fixed; inset: 0; background: rgba(2, 6, 23, .55); display: flex; align-items: center;
  justify-content: center; z-index: 500; padding: 16px;
}
.modal {
  background: var(--panel); border-radius: 16px; box-shadow: var(--shadow); width: 100%; max-width: 440px;
  max-height: calc(100vh - 32px); overflow: auto; padding: 22px;
}
.modal h2 { margin: 0 0 16px; font-size: 19px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
.member-pick { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; }
.member-pick .check { display: flex; padding: 5px 0; }

/* ------------------------------------------------ login */
.login-wrap { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px;
  background: radial-gradient(1200px 600px at 10% -10%, rgba(37, 99, 235, .18), transparent),
              radial-gradient(900px 500px at 110% 110%, rgba(16, 185, 129, .14), transparent), var(--bg); }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 18px; box-shadow: var(--shadow); padding: 30px 28px; }
.logo { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 22px; margin-bottom: 6px; }
.logo-mark { width: 38px; height: 38px; border-radius: 11px; background: linear-gradient(135deg, #2563eb, #10b981);
  display: grid; place-items: center; color: #fff; font-size: 20px; }
.login-card p.sub { margin: 0 0 22px; color: var(--muted); font-size: 14px; }
.login-card .btn { width: 100%; padding: 11px; }
.err { color: var(--danger); font-size: 14px; min-height: 20px; margin-bottom: 8px; }
.switch-link { text-align: center; margin-top: 14px; font-size: 14px; color: var(--muted); }
.switch-link a { cursor: pointer; }

/* ------------------------------------------------ chat layout */
.app { display: flex; height: 100%; overflow: hidden; }
.sidebar {
  width: 290px; flex-shrink: 0; background: var(--side); color: var(--side-text);
  display: flex; flex-direction: column; overflow: hidden;
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 10px; }
.side-head .logo { font-size: 18px; margin: 0; }
.side-head .logo-mark { width: 30px; height: 30px; font-size: 16px; border-radius: 9px; }
.side-scroll { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.side-section { margin-top: 12px; }
.side-section h3 {
  display: flex; align-items: center; justify-content: space-between; margin: 0 8px 6px;
  font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--side-muted); font-weight: 700;
}
.side-section h3 .icon-btn { width: 26px; height: 26px; font-size: 16px; color: var(--side-muted); }
.side-list { list-style: none; margin: 0; padding: 0; }
.side-item {
  display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 9px; cursor: pointer;
  color: var(--side-text); position: relative;
}
.side-item:hover { background: var(--side-2); }
.side-item.active { background: var(--accent); color: #fff; }
.side-item .ico { width: 22px; text-align: center; opacity: .8; flex-shrink: 0; }
.side-item .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-item .sub { display: block; font-size: 12px; color: var(--side-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-item.active .sub { color: rgba(255, 255, 255, .8); }
.badge { background: #ef4444; color: #fff; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 99px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #4b5563; flex-shrink: 0; }
.dot.on { background: #22c55e; box-shadow: 0 0 0 2px rgba(34, 197, 94, .25); }
.side-search { margin: 0 6px 6px; width: calc(100% - 12px); background: var(--side-2); border: 1px solid transparent; color: var(--side-text); padding: 7px 10px; border-radius: 8px; outline: none; }
.side-search:focus { border-color: var(--accent); }
.me-box { border-top: 1px solid rgba(255, 255, 255, .08); padding: 10px 12px; display: flex; align-items: center; gap: 10px; }
.me-box .who { flex: 1; min-width: 0; }
.me-box .who b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me-box .who span { font-size: 12px; color: var(--side-muted); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-weight: 700;
  color: #fff; flex-shrink: 0; font-size: 15px; text-transform: uppercase;
}

.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; background: var(--bg); }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel);
  border-bottom: 1px solid var(--line); min-height: 58px;
}
.chat-head .title { flex: 1; min-width: 0; }
.chat-head .title b { font-size: 16px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .title span { font-size: 13px; color: var(--muted); }
#menuBtn { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px 18px 8px; display: flex; flex-direction: column; gap: 2px; }
.load-more { align-self: center; margin: 4px 0 10px; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); padding: 2px 12px; border-radius: 99px; margin: 12px 0 8px; }
.msg { display: flex; gap: 10px; max-width: 78%; margin-top: 8px; }
.msg.cont { margin-top: 1px; }
.msg.cont .avatar { visibility: hidden; height: 0; }
.msg.cont .meta { display: none; }
.msg.me { align-self: flex-end; flex-direction: row-reverse; }
.msg .body { min-width: 0; display: flex; flex-direction: column; }
.msg.me .body { align-items: flex-end; }
.msg .meta { font-size: 12px; color: var(--muted); margin: 0 4px 3px; }
.msg .meta b { color: var(--text); font-weight: 600; margin-right: 6px; }
.bubble {
  background: var(--bubble); border: 1px solid var(--line); padding: 8px 12px; border-radius: 14px;
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; position: relative;
}
.msg.me .bubble { background: var(--bubble-me); color: var(--bubble-me-text); border-color: transparent; }
.msg.me .bubble a { color: #fff; }
.bubble .time { font-size: 11px; opacity: .6; margin-left: 8px; float: right; margin-top: 4px; }
.msg-actions { opacity: 0; transition: opacity .15s; align-self: center; }
.msg:hover .msg-actions { opacity: 1; }
.msg-actions button { background: none; border: 0; color: var(--muted); font-size: 13px; padding: 4px 6px; border-radius: 6px; }
.msg-actions button:hover { background: var(--panel); color: var(--danger); }

.file-card {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; min-width: 220px; max-width: 340px;
  background: var(--panel); border: 1px solid var(--line); color: var(--text); text-decoration: none; margin-bottom: 4px;
}
.file-card:hover { border-color: var(--accent); }
.file-card .fi { width: 40px; height: 40px; border-radius: 9px; display: grid; place-items: center; background: var(--accent-soft); font-size: 20px; flex-shrink: 0; }
.file-card .fn { flex: 1; min-width: 0; }
.file-card .fn b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; font-size: 14px; }
.file-card .fn span { font-size: 12px; color: var(--muted); }
.file-card .dl { font-size: 18px; color: var(--accent); }
.file-card.locked { opacity: .7; cursor: not-allowed; }
.file-card.deleted { opacity: .55; font-style: italic; }
.img-wrap { display: block; margin-bottom: 4px; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); max-width: 320px; background: var(--panel); }
.img-wrap img { display: block; max-width: 100%; max-height: 300px; object-fit: contain; }
.img-wrap video, .img-wrap audio { display: block; max-width: 100%; }
.img-cap { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; padding: 5px 9px; color: var(--muted); }

.storage-banner { background: #fef3c7; color: #92400e; border-bottom: 1px solid #fde68a; padding: 8px 16px; font-size: 13px; }
@media (prefers-color-scheme: dark) { .storage-banner { background: #422006; color: #fde68a; border-color: #713f12; } }
.typing { height: 22px; padding: 0 20px; font-size: 13px; color: var(--muted); font-style: italic; }
.upload-bar { padding: 0 16px; }
.upload-item { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; margin-bottom: 6px; font-size: 13px; }
.upload-item .bar { height: 5px; border-radius: 9px; background: var(--line); overflow: hidden; margin-top: 4px; }
.upload-item .bar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .1s; }

.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px 14px; background: var(--bg); }
.composer .box { flex: 1; display: flex; align-items: flex-end; gap: 4px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 4px 6px; }
.composer .box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer textarea { flex: 1; border: 0; background: transparent; resize: none; outline: none; padding: 8px 4px; max-height: 160px; min-height: 22px; }
.composer .btn { height: 44px; border-radius: 12px; padding: 0 18px; }
.readonly-note { text-align: center; color: var(--muted); padding: 14px; font-size: 14px; border-top: 1px solid var(--line); }

.drop-overlay {
  position: absolute; inset: 0; background: rgba(37, 99, 235, .12); border: 3px dashed var(--accent);
  display: flex; align-items: center; justify-content: center; font-size: 20px; font-weight: 700; color: var(--accent);
  z-index: 50; pointer-events: none; border-radius: 6px; margin: 6px;
}
.empty { margin: auto; text-align: center; color: var(--muted); }
.empty .big { font-size: 44px; }

.side-backdrop { display: none; }
@media (max-width: 760px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 200; transform: translateX(-100%); transition: transform .2s; width: 84%; max-width: 320px; }
  .sidebar.open { transform: none; }
  .side-backdrop.show { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 150; }
  #menuBtn { display: inline-flex; }
  .msg { max-width: 92%; }
  .msg-actions { opacity: 1; }
  .messages { padding: 12px 10px 6px; }
}

/* ------------------------------------------------ admin */
.admin-top { background: var(--side); color: var(--side-text); display: flex; align-items: center; gap: 16px; padding: 10px 20px; flex-wrap: wrap; }
.admin-top .logo { font-size: 18px; margin: 0; }
.admin-top .logo-mark { width: 30px; height: 30px; font-size: 16px; }
.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tab { background: transparent; border: 0; color: var(--side-muted); padding: 8px 14px; border-radius: 9px; font-weight: 600; }
.tab:hover { color: #fff; background: var(--side-2); }
.tab.active { background: var(--accent); color: #fff; }
.admin-top a.btn { text-decoration: none; }
.admin-main { max-width: 1200px; margin: 0 auto; padding: 22px 20px 60px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.card h2 { margin: 0 0 14px; font-size: 17px; display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat span { font-size: 13px; color: var(--muted); }
.stat b { display: block; font-size: 26px; margin-top: 2px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 700; white-space: nowrap; }
tr:hover td { background: var(--panel-2); }
.perm-grid { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.perm-grid .check { font-size: 13px; }
.pill { display: inline-block; font-size: 12px; padding: 2px 9px; border-radius: 99px; background: var(--panel-2); border: 1px solid var(--line); white-space: nowrap; }
.pill.admin { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.pill.off { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.pill.on { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 0 12px; align-items: end; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
code { background: var(--panel-2); border: 1px solid var(--line); padding: 1px 6px; border-radius: 6px; font-size: 13px; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar .input { max-width: 260px; }
