:root {
  color-scheme: light;
  --bg: #f7f7f4;
  --panel: #ffffff;
  --text: #202124;
  --muted: #6b7280;
  --line: #d9ded8;
  --self: #dceeff;
  --other: #f0f2ee;
  --accent: #24745a;
  --accent-text: #ffffff;
  --shadow: 0 12px 30px rgba(38, 45, 55, 0.08);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111315;
  --panel: #1b1f22;
  --text: #f3f4f6;
  --muted: #a5adb8;
  --line: #30363d;
  --self: #183c56;
  --other: #282d31;
  --accent: #62b896;
  --accent-text: #0f1412;
  --shadow: none;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  overflow-x: hidden;
}

html {
  overflow-x: hidden;
}

body[data-auth="locked"] .topbar,
body[data-auth="locked"] .shell,
body[data-auth="locked"] .float-actions,
body[data-auth="locked"] dialog {
  display: none;
}

body[data-auth="unlocked"] .auth-screen {
  display: none;
}

.auth-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-panel {
  width: min(340px, 100%);
  display: grid;
  gap: 18px;
  justify-items: center;
  padding: 28px 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.auth-panel h1 {
  margin: 0;
  font-size: 22px;
}

.pin-inputs {
  display: grid;
  grid-template-columns: repeat(4, 48px);
  gap: 10px;
  justify-content: center;
}

.pin-inputs input {
  width: 48px;
  height: 54px;
  padding: 0;
  text-align: center;
  font-size: 24px;
  font-weight: 600;
}

.auth-error {
  min-height: 18px;
  margin: 0;
  color: #c2413a;
  font-size: 13px;
}

button,
input,
select {
  font: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
  padding: 12px clamp(16px, 4vw, 40px);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
}

.title {
  display: grid;
  gap: 2px;
}

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

.title strong {
  font-size: 18px;
}

.swap-sides-btn {
  width: 28px;
  min-height: 28px;
  padding: 0;
  border-radius: 50%;
  color: var(--muted);
  background: transparent;
  font-size: 15px;
  line-height: 1;
}

.swap-sides-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.title span,
.result-bar {
  color: var(--muted);
  font-size: 13px;
}

.icon-button,
button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 14px;
  color: var(--text);
  background: var(--panel);
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.shell {
  display: grid;
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr);
  gap: 18px;
  width: min(1280px, 100%);
  margin: 0 auto;
  padding: 18px clamp(12px, 3vw, 32px) 32px;
}

.filters,
.chat-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.filters {
  align-self: start;
  position: sticky;
  top: 82px;
  display: grid;
  gap: 14px;
  padding: 16px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

input,
select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  color: var(--text);
  background: var(--bg);
}

.date-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.actions button:first-child {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
}

.secondary {
  background: transparent;
}

.chat-panel {
  min-width: 0;
  overflow: hidden;
}

.result-bar {
  position: sticky;
  top: 64px;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.messages {
  display: grid;
  gap: 10px;
  padding: 16px;
}

.load-sentinel {
  min-height: 52px;
  padding: 14px 16px 22px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.load-sentinel-top:empty {
  display: none;
}

.date-divider {
  justify-self: center;
  margin: 10px 0 4px;
  padding: 4px 10px;
  border-radius: 999px;
  color: var(--muted);
  background: var(--bg);
  font-size: 12px;
}

.message {
  display: grid;
  grid-template-columns: 44px minmax(0, 680px);
  gap: 10px;
  align-items: start;
}

.message.self {
  grid-template-columns: minmax(0, 680px) 44px;
  justify-content: end;
}

.message.self .avatar {
  grid-column: 2;
}

.message.self .bubble-wrap {
  grid-column: 1;
  grid-row: 1;
  justify-self: end;
}

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  overflow: hidden;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--accent);
  font-size: 14px;
  flex: none;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.float-actions {
  position: fixed;
  right: max(18px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 20;
  display: grid;
  gap: 10px;
}

.float-actions button {
  width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 0;
  border-color: color-mix(in srgb, var(--accent) 22%, var(--line));
  border-radius: 50%;
  color: var(--accent);
  background: color-mix(in srgb, var(--panel) 76%, transparent);
  box-shadow: 0 8px 20px rgba(38, 45, 55, 0.1);
  backdrop-filter: blur(14px);
  font-size: 20px;
  line-height: 1;
  opacity: 0.78;
}

.float-actions button:hover {
  color: var(--accent);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  opacity: 0.95;
}

.bubble-wrap {
  min-width: 0;
}

.meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
}

.message.self .meta {
  justify-content: end;
}

.locate-btn {
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  color: var(--accent);
  background: transparent;
  font-size: 12px;
}

.message.located .bubble {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.bubble {
  display: grid;
  gap: 8px;
  width: fit-content;
  max-width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--other);
  overflow-wrap: anywhere;
}

.message.self .bubble {
  margin-left: auto;
  background: var(--self);
}

.text {
  white-space: pre-wrap;
  line-height: 1.55;
}

.assets {
  display: grid;
  gap: 8px;
}

.asset-image {
  width: min(320px, 68vw);
  height: min(320px, 68vw);
  border-radius: 8px;
  object-fit: contain;
  cursor: zoom-in;
  background: color-mix(in srgb, var(--bg) 86%, var(--line));
}

.asset-file {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  max-width: 100%;
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  background: var(--panel);
  overflow-wrap: anywhere;
}

audio,
video {
  max-width: min(420px, 75vw);
}

#mediaDialog {
  width: min(960px, 94vw);
  border: 0;
  border-radius: 8px;
  padding: 44px 12px 12px;
  background: var(--panel);
}

#mediaDialog::backdrop {
  background: rgba(0, 0, 0, 0.72);
}

.close-button {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  min-height: 32px;
  padding: 0;
}

#dialogBody img,
#dialogBody video {
  display: block;
  max-width: 100%;
  max-height: 80vh;
  margin: 0 auto;
  object-fit: contain;
}

.empty,
.loading {
  padding: 40px 16px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 760px) {
  .topbar {
    min-height: 56px;
  }

  .shell {
    grid-template-columns: 1fr;
    padding: 10px 8px 24px;
  }

  .filters {
    position: static;
    padding: 12px;
  }

  .result-bar {
    top: 56px;
    align-items: flex-start;
    flex-direction: column;
  }

  .messages {
    padding: 12px 8px;
  }

  .message,
  .message.self {
    grid-template-columns: 36px minmax(0, calc(100vw - 76px));
  }

  .message.self {
    grid-template-columns: minmax(0, calc(100vw - 76px)) 36px;
  }

  .avatar {
    width: 36px;
    height: 36px;
    font-size: 12px;
  }

  .asset-image {
    width: min(220px, 58vw);
    height: min(220px, 58vw);
  }

  audio,
  video {
    max-width: calc(100vw - 96px);
  }

  .float-actions {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
  }

  .float-actions button {
    width: 40px;
    height: 40px;
    min-height: 40px;
  }

  .date-row,
  .actions {
    grid-template-columns: 1fr;
  }
}
