:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-subtle: #f8fafc;
  --border: #e5e9f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #0f766e;
  --accent-hover: #0d625b;
  --accent-soft: #f0fdfa;
  --bubble-me: #0f766e;
  --bubble-me-text: #ffffff;
  --bubble-ai: #ffffff;
  --bubble-staff: #fffbeb;
  --bubble-staff-border: #fde68a;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 6px 20px rgba(15, 23, 42, 0.04);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

button, input, textarea {
  font-family: inherit;
  font-size: 14px;
}

.hidden {
  display: none !important;
}

/* ---------- Chatroom Layout ---------- */
.chat-app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 920px;
  margin: 0 auto;
  background: var(--bg);
}

@media (min-width: 940px) {
  .chat-app {
    height: calc(100dvh - 32px);
    margin: 16px auto;
    border-radius: 18px;
    overflow: hidden;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
  }
}

.chat-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}

.chat-title-wrap {
  min-width: 0;
}

.chat-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-title {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #10b981;
  flex-shrink: 0;
}

.status-dot.offline {
  background: #f59e0b;
}

.chat-subtitle {
  margin-top: 2px;
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-subtle);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.pill-btn:hover {
  background: #eef2f6;
}

.pill-btn.staff-active {
  background: #fef3c7;
  border-color: #f59e0b;
  color: #92400e;
  font-weight: 600;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.load-more-wrap {
  text-align: center;
  margin-bottom: 4px;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 340px;
  padding: 28px 20px;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.7;
}

.empty-state strong {
  display: block;
  color: var(--text);
  font-size: 15px;
  margin-bottom: 6px;
}

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 82%;
}

.msg-row.mine {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-row.other {
  align-self: flex-start;
  align-items: flex-start;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
  padding: 0 4px;
}

.role-badge {
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 999px;
  font-weight: 600;
}

.role-badge.ai {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid #99f6e4;
}

.role-badge.staff {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fcd34d;
}

.msg-bubble {
  position: relative;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.6;
  font-size: 14px;
  word-break: break-word;
  white-space: pre-wrap;
  border: 1px solid var(--border);
  background: var(--bubble-ai);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.03);
}

.msg-row.mine .msg-bubble {
  background: var(--bubble-me);
  color: var(--bubble-me-text);
  border-color: transparent;
  border-bottom-right-radius: 4px;
}

.msg-row.other .msg-bubble {
  border-bottom-left-radius: 4px;
}

.msg-row.staff-row .msg-bubble {
  background: var(--bubble-staff);
  border-color: var(--bubble-staff-border);
}

.msg-bubble a {
  color: inherit;
  text-decoration: underline;
  word-break: break-all;
}

.msg-reply-ref {
  font-size: 12px;
  padding: 4px 8px;
  margin-bottom: 6px;
  border-left: 3px solid rgba(100, 116, 139, 0.45);
  background: rgba(15, 23, 42, 0.04);
  border-radius: 4px;
  color: var(--text-muted);
}

.msg-row.mine .msg-reply-ref {
  border-left-color: rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.14);
  color: rgba(255, 255, 255, 0.9);
}

.msg-img {
  display: block;
  max-width: min(320px, 68vw);
  max-height: 320px;
  border-radius: 10px;
  margin-top: 6px;
  cursor: zoom-in;
  border: 1px solid rgba(15, 23, 42, 0.08);
}

.msg-file-card {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.05);
  color: inherit;
  text-decoration: none;
  font-size: 13px;
}

.msg-row.mine .msg-file-card {
  background: rgba(255, 255, 255, 0.16);
}

.msg-actions {
  margin-top: 3px;
  padding: 0 4px;
  opacity: 0;
  transition: opacity 0.15s;
}

.msg-row:hover .msg-actions {
  opacity: 1;
}

.mini-link {
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 4px;
}

.mini-link:hover {
  color: var(--accent);
}

/* Composer */
.composer-wrap {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  flex-shrink: 0;
}

.reply-banner, .file-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  margin-bottom: 8px;
  border-radius: 8px;
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-subtle);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: #eef2f6;
}

.composer-input {
  flex: 1;
  min-height: 40px;
  max-height: 140px;
  resize: none;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-subtle);
  color: var(--text);
  line-height: 1.5;
  outline: none;
}

.composer-input:focus {
  border-color: var(--accent);
  background: var(--surface);
}

.send-btn {
  height: 40px;
  padding: 0 18px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}

.send-btn:hover {
  background: var(--accent-hover);
}

.send-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Modal & Lightbox */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border-radius: 16px;
  padding: 22px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.2);
}

.modal-card h3 {
  font-size: 17px;
  margin-bottom: 6px;
}

.modal-card p {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 14px;
  line-height: 1.5;
}

.field {
  margin-bottom: 12px;
}

.field label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 5px;
}

.field input, .field textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  background: var(--surface-subtle);
}

.field input:focus, .field textarea:focus {
  border-color: var(--accent);
  background: var(--surface);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.btn-secondary {
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.btn-primary {
  padding: 8px 16px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.lightbox img {
  max-width: 92vw;
  max-height: 88vh;
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

/* ---------- Admin Portal ---------- */
.admin-shell {
  max-width: 1120px;
  margin: 0 auto;
  padding: 20px 16px 48px;
}

.login-box {
  max-width: 390px;
  margin: 12vh auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
}

.login-box h1 {
  font-size: 20px;
  margin-bottom: 6px;
}

.login-box .sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 20px;
}

.admin-header {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 20px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.admin-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

.search-input {
  flex: 1;
  min-width: 220px;
  padding: 9px 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  outline: none;
}

.search-input:focus {
  border-color: var(--accent);
}

.rooms-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 14px;
}

.room-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: var(--shadow);
}

.room-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.room-card-title {
  font-size: 15px;
  font-weight: 600;
}

.room-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}

.tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.tag.ok {
  background: var(--accent-soft);
  border-color: #99f6e4;
  color: var(--accent);
}

.url-box {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  border-radius: 9px;
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: #334155;
  overflow: hidden;
}

.url-box span {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-preview {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-card-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #0f172a;
  color: #ffffff;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 80;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.25);
}

.error-text {
  color: var(--danger);
  font-size: 12px;
  margin-top: 6px;
}
