:root {
  color-scheme: light;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  --surface: #fff;
  --background: #f1f4f8;
  --text: #182538;
  --muted: #53637a;
  --border: #dce3ed;
  --brand: #254ec4;
  --brand-hover: #1d3f9e;
  --selection: #edf3ff;
  --danger: #b32638;
  --shadow: 0 12px 40px #172b4d0d;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100dvh; background: var(--background); color: var(--text); }

h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.3rem; letter-spacing: -.02em; }
h2 { font-size: 1.14rem; }
button, input, textarea, select { font: inherit; }
button, .admin-link {
  min-height: 40px;
  padding: 8px 13px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font-size: .875rem;
  font-weight: 550;
  cursor: pointer;
}
button:hover, .admin-link:hover { background: #f0f4fa; }
button:disabled { cursor: wait; opacity: .55; }
:focus-visible { outline: 3px solid #829dff; outline-offset: 2px; }
.primary, .danger { border-color: transparent; color: white; }
.primary { background: var(--brand); }
.primary:hover { background: var(--brand-hover); }
.danger { background: var(--danger); }
.danger:hover { background: #94192b; }
.danger-text { color: var(--danger); }

input, textarea, select {
  width: 100%;
  min-height: 42px;
  min-width: 0;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: white;
  color: var(--text);
}
textarea { resize: vertical; }
label { display: grid; gap: 6px; font-size: .875rem; font-weight: 600; }
label :is(input, textarea, select) { font-weight: normal; }

.topbar { background: #fff; border-bottom: 1px solid var(--border); }
.topbar-inner {
  max-width: 1228px;
  min-height: 68px;
  margin: auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 11px; font-weight: 730; letter-spacing: -.01em; white-space: nowrap; }
.brand-mark {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--brand);
}
.brand-mark::before {
  content: "";
  width: 16px;
  height: 12px;
  margin-bottom: 2px;
  border-radius: 4px;
  background: #fff;
}
.brand-mark::after {
  position: absolute;
  left: 11px;
  top: 19px;
  width: 5px;
  height: 5px;
  transform: skewY(-30deg);
  background: #fff;
  content: "";
}
nav { display: flex; gap: 6px; }
nav button { border-color: transparent; }
nav button[aria-current="page"] { background: var(--selection); color: var(--brand-hover); }
#logout { margin-left: 4px; color: var(--muted); }

.workspace { max-width: 1228px; margin: auto; padding: 24px; }
.card { border: 1px solid var(--border); border-radius: 16px; background: var(--surface); box-shadow: var(--shadow); }
.loading, .empty { padding: 24px 14px; color: var(--muted); text-align: center; font-size: .9rem; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: .875rem; }
.notice {
  margin-bottom: 12px;
  padding: 9px 13px;
  border: 1px solid #ccdafa;
  border-radius: 9px;
  background: var(--selection);
  color: #254578;
  font-size: .875rem;
}
.notice[data-error="true"] { border-color: #f2cbd2; background: #fff2f4; color: var(--danger); }

.login { width: min(100%, 400px); margin: clamp(32px, 12vh, 136px) auto; padding: 28px; }
.login form, dialog form { display: grid; gap: 15px; }
.login h1 { margin-bottom: 22px; }

.messages {
  display: grid;
  grid-template-columns: minmax(270px, 34%) minmax(0, 1fr);
  overflow: hidden;
  height: min(790px, calc(100dvh - 118px));
  min-height: 450px;
}
.conversation-list { min-width: 0; min-height: 0; overflow: auto; border-right: 1px solid var(--border); }
.list-header {
  position: sticky;
  z-index: 1;
  top: 0;
  padding: 18px 16px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.section-title { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.search-form { display: flex; gap: 7px; margin-top: 14px; }
.search-form button { flex-shrink: 0; }
.conversations, .thread-messages, .device-cards { list-style: none; padding: 0; margin: 0; }
.conversations li + li { border-top: 1px solid #edf0f6; }
.conversation-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 3px 8px;
  width: 100%;
  padding: 14px 16px;
  border: 0;
  border-radius: 0;
  text-align: left;
}
.conversation-item[aria-current="true"] { background: var(--selection); box-shadow: inset 3px 0 var(--brand); }
.conversation-peer { overflow: hidden; text-overflow: ellipsis; font-size: .95rem; white-space: nowrap; }
.conversation-time, .conversation-device, .conversation-preview { font-size: .79rem; font-weight: 400; color: var(--muted); }
.conversation-preview { grid-column: 1 / -1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.more { display: block; margin: 12px auto; }

.conversation { min-width: 0; min-height: 0; background: #f6f8fb; }
.conversation-placeholder { display: grid; place-content: center; height: 100%; }
.thread { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; height: 100%; }
.thread-header {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: white;
}
.thread-header button { flex-shrink: 0; white-space: nowrap; }
.thread-title { min-width: 0; margin-right: auto; }
.thread-title h2 { overflow-wrap: anywhere; }
.thread-title p { margin-top: 3px; color: var(--muted); font-size: .8rem; }
.back { display: none; }
.thread-scroll { min-height: 0; overflow: auto; }
.thread-messages { display: flex; flex-direction: column; gap: 14px; padding: 16px; }
.message { display: grid; gap: 4px; align-self: flex-start; max-width: min(82%, 540px); }
.message[data-outgoing="true"] { align-self: flex-end; }
.message-body {
  padding: 10px 13px;
  border: 1px solid var(--border);
  border-radius: 13px 13px 13px 3px;
  background: white;
  line-height: 1.55;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.message[data-outgoing="true"] .message-body { border-color: #dce7ff; border-radius: 13px 13px 3px 13px; background: #e6eeff; }
.message-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 8px; color: var(--muted); font-size: .75rem; }
.message[data-outgoing="true"] .message-meta { justify-content: flex-end; }
.message-meta button { min-height: 32px; padding: 5px 6px; border: 0; background: transparent; color: var(--muted); font-size: inherit; }
.message-meta button:hover { background: #e8edf5; color: var(--danger); }
.reply-form { display: flex; gap: 9px; align-items: end; padding: 12px 16px; background: white; border-top: 1px solid var(--border); }
.reply-form button { flex-shrink: 0; min-height: 43px; }
.reply-form textarea { max-height: 155px; }

.devices-page { max-width: 930px; margin: auto; }
.device-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 290px), 1fr)); gap: 14px; margin-top: 16px; }
.device-card { display: grid; gap: 16px; padding: 19px; align-content: start; }
.device-heading { display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px 13px; }
.device-heading p { font-size: .82rem; color: var(--muted); }
.device-heading p[data-online="true"] { color: #147152; }
.slots { display: flex; flex-wrap: wrap; gap: 7px; }
.slot { border-radius: 7px; background: #f0f4f9; padding: 5px 8px; color: var(--muted); font-size: .84rem; }
.admin-link { display: inline-flex; align-items: center; width: fit-content; text-decoration: none; }
.device-access { margin-top: 7px; font-size: .8rem; }

.settings-page { max-width: 620px; margin: auto; }
.settings-card { display: grid; gap: 14px; margin-top: 16px; padding: 22px; }
.settings-help { font-size: .875rem; }
.feishu-summary {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 16px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: #f6f8fb;
  font-size: .875rem;
}
.feishu-status[data-configured="true"] { color: #147152; font-weight: 600; }
.feishu-deliveries, .feishu-deliveries div { display: flex; flex-wrap: wrap; gap: 5px 12px; }
.feishu-deliveries { margin: 0; color: var(--muted); }
.feishu-deliveries div { gap: 5px; }
.feishu-deliveries dd { margin: 0; color: var(--text); font-variant-numeric: tabular-nums; }
#feishu-fields { display: grid; gap: 13px; min-width: 0; margin: 0; padding: 0; border: 0; }
#feishu-sign-help { margin-top: -7px; }
#feishu-error { margin-top: 10px; }
.settings-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }

dialog {
  width: min(100% - 24px, 470px);
  max-height: calc(100dvh - 24px);
  padding: 21px;
  border: 1px solid var(--border);
  border-radius: 15px;
  box-shadow: 0 22px 65px #14254533;
  color: var(--text);
}
dialog::backdrop { background: #1722366b; }
.dialog-header, .dialog-actions { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.dialog-actions { justify-content: flex-end; }
.compose-options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

@media (max-width: 680px) {
  .topbar-inner { min-height: 56px; padding: 0 12px; }
  nav { gap: 2px; }
  nav button { padding: 7px 10px; }
  .workspace { padding: 12px; }
  .messages { display: block; height: calc(100dvh - 81px); min-height: 320px; }
  .messages[data-view="list"] .conversation,
  .messages[data-view="thread"] .conversation-list { display: none; }
  .conversation-list, .conversation { height: 100%; border: 0; }
  .list-header, .thread-header, .reply-form, .thread-messages { padding-inline: 11px; }
  .back { display: inline-flex; align-items: center; }
  .thread-header button { padding-inline: 8px; }
  .thread-title h2 { font-size: 1rem; }
  .message { max-width: 88%; }
  .reply-form { padding-bottom: max(10px, env(safe-area-inset-bottom)); }
}

@media (max-height: 520px) {
  .messages { min-height: 240px; }
}

.list-refresh {
  display: block;
  min-height: 32px;
  margin: 7px 12px 5px auto;
  border-color: transparent;
  color: var(--muted);
  padding-block: 5px;
}

@media (max-width: 380px) {
  .topbar-inner { gap: 8px; }
  nav button { padding-inline: 7px; }
  .thread-header { gap: 5px; }
  .thread-header button { padding-inline: 7px; }
}
