:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --line: #e3e7ee;
  --accent: #1f4e8c;
  --accent-soft: #e8eef8;
  --out: #1f4e8c;
  --out-text: #ffffff;
  --in: #eef1f5;
  --danger: #b42318;
  --line-green: #06c755;
  --radius: 10px;
  font-family: "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 15px;
}
* { box-sizing: border-box; }
body { margin: 0; height: 100dvh; }
[hidden] { display: none !important; }
button {
  font: inherit; border: 0; border-radius: 8px; padding: 9px 16px; cursor: pointer;
  background: var(--accent); color: #fff;
}
button:disabled { opacity: .6; cursor: default; }
button.ghost { background: transparent; color: var(--accent); padding: 6px 10px; }
button.ghost:hover { background: var(--accent-soft); }
input, textarea {
  font: inherit; width: 100%; padding: 9px 11px; border: 1px solid var(--line);
  border-radius: 8px; background: #fff; color: var(--text);
}
input:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.muted { color: var(--muted); }
.center { text-align: center; margin: 40px 16px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }
.spacer { flex: 1; }

/* Auth screens */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.card {
  background: var(--panel); border-radius: 14px; padding: 28px; width: min(380px, 100%);
  display: grid; gap: 14px; box-shadow: 0 2px 16px rgba(16, 24, 40, .08);
}
.card h1 { margin: 0; font-size: 1.35rem; }
.card p { margin: 0; }
.card label { display: grid; gap: 6px; font-weight: 600; }
.steps { margin: 0; padding-left: 20px; color: var(--muted); line-height: 1.6; }
#enroll-qr { justify-self: center; border: 1px solid var(--line); border-radius: 8px; }
code { word-break: break-all; font-size: .85rem; }

/* App layout */
.app { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.panes { flex: 1; display: grid; grid-template-columns: 340px 1fr; min-height: 0; }
.list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; gap: 4px; padding: 8px; border-bottom: 1px solid var(--line); }
.tabs button { background: transparent; color: var(--muted); padding: 6px 10px; }
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.conv-list li button {
  width: 100%; text-align: left; background: transparent; color: var(--text); border-radius: 0;
  padding: 12px 14px; border-bottom: 1px solid var(--line); display: grid; gap: 3px;
}
.conv-list li button:hover { background: var(--bg); }
.conv-list li button.selected { background: var(--accent-soft); }
.conv-row { display: flex; align-items: center; gap: 8px; }
.conv-name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: .8rem; color: var(--muted); }
.conv-preview { color: var(--muted); font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.unread { background: var(--danger); color: #fff; border-radius: 999px; font-size: .75rem; padding: 1px 7px; }
.badge { font-size: .75rem; padding: 2px 8px; border-radius: 999px; color: #fff; background: var(--muted); }
.badge.line { background: var(--line-green); }

.chat-pane { display: flex; flex-direction: column; min-height: 0; }
#chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.back { display: none; }
.messages { list-style: none; margin: 0; padding: 16px; flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: min(560px, 80%); padding: 9px 13px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; line-height: 1.45; }
.msg.in { align-self: flex-start; background: var(--in); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); color: var(--out-text); border-bottom-right-radius: 4px; }
.msg.failed { background: #fdecea; color: var(--danger); border: 1px solid var(--danger); }
.meta { font-size: .75rem; color: var(--muted); }
.msg-wrap { display: flex; flex-direction: column; gap: 2px; }
.msg-wrap.out { align-items: flex-end; }
.composer { display: flex; gap: 8px; padding: 12px; background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea { resize: none; }

/* Filters */
.filters { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); align-items: center; }
.filters select, .chat-meta select, .topic-form select {
  font: inherit; font-size: .85rem; padding: 5px 6px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--text);
}
.check { display: inline-flex; align-items: center; gap: 4px; font-size: .85rem; cursor: pointer; }
.check input { width: auto; }

/* Topic / assignee / aging */
.tags { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.tag { font-size: .72rem; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.tag.owner { background: #f2f4f7; color: var(--text); }
.aging { font-size: .72rem; padding: 1px 7px; border-radius: 999px; background: #ecfdf3; color: #067647; white-space: nowrap; }
.aging.warn { background: #fffaeb; color: #b54708; }
.aging.alert { background: #fef3f2; color: var(--danger); font-weight: 600; }
.chat-meta { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); font-size: .85rem; }
.chat-meta label { display: inline-flex; gap: 6px; align-items: center; }

/* Settings */
.settings-pane { display: flex; flex-direction: column; min-height: 0; grid-column: 1 / -1; }
.settings-body { overflow-y: auto; padding: 16px; display: grid; gap: 16px; justify-items: center; }
.card.wide { width: min(720px, 100%); }
.card h2 { margin: 0; font-size: 1.1rem; }
.card h3 { margin: 0; font-size: 1rem; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row-buttons { display: flex; gap: 8px; }
.topic-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.topic-list li { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; display: grid; gap: 4px; }
.topic-list .topic-head { display: flex; gap: 8px; align-items: center; }
.topic-list .kw { color: var(--muted); font-size: .85rem; word-break: break-word; }
.topic-form { display: grid; gap: 10px; border-top: 1px solid var(--line); padding-top: 14px; }
.topic-form label { display: grid; gap: 6px; font-weight: 600; }
.danger { color: var(--danger) !important; }
.small { font-size: .8rem; font-weight: normal; }
.card label { display: grid; gap: 6px; font-weight: 600; }
.ok-text { color: #067647; }

.app.settings-open .list-pane, .app.settings-open .chat-pane { display: none !important; }
.topbar button, .topbar strong { white-space: nowrap; }
#channel-status { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }

/* Mobile: one pane at a time */
@media (max-width: 760px) {
  .panes { grid-template-columns: 1fr; }
  .app.chat-open .list-pane { display: none; }
  .app:not(.chat-open) .chat-pane { display: none; }
  .back { display: inline-block; }
  #me, #channel-status { display: none; }
  .row { grid-template-columns: 1fr; }
}
