:root {
  --bg: #e6dfd4;
  --ink: #1c1917;
  --muted: #74695f;
  --panel: #fffdf9;
  --panel-2: #f7f2ea;
  --line: #e3dbd0;
  --accent: #e4572e;
  --accent-ink: #fff8f4;
  --mine: #1c1917;
  --mine-ink: #f6f1ea;
  --theirs: #f3eee6;
  --sidebar: #1c1917;
  --sidebar-ink: #f6f1ea;
  --sidebar-muted: #b7aea3;
  --sidebar-line: rgba(255, 255, 255, 0.08);
  --sidebar-hover: rgba(255, 255, 255, 0.06);
  --sidebar-active: rgba(255, 255, 255, 0.12);
  --shadow: 0 18px 50px rgba(48, 32, 16, 0.08);
  --topbar: #1c1917;
  --danger: #b42318;
  --radius: 18px;
  --font: "Manrope", "Segoe UI", sans-serif;
  --display: "Unbounded", "Manrope", sans-serif;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #100e0c;
  --ink: #f4efe8;
  --muted: #a89d92;
  --panel: #24211d;
  --panel-2: #2e2a26;
  --line: #3a342e;
  --mine: #e7d7c4;
  --mine-ink: #1c1917;
  --theirs: #3a342e;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
  --sidebar: #181614;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
}
button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, a:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 64px;
  flex: 0 0 64px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  background: var(--topbar);
  color: #f6f1ea;
}
.brand {
  font-family: var(--display);
  font-size: 20px;
  letter-spacing: -0.04em;
  color: #f6f1ea;
  text-decoration: none;
  line-height: 1;
}
.brand span { color: #e4572e; }
.domain {
  color: #b7aea3;
  font-size: 13px;
  margin-right: auto;
}
.topnav { display: flex; gap: 6px; }
.nav-link {
  background: transparent;
  border: 0;
  color: #d9d0c6;
  padding: 8px 12px;
  border-radius: 999px;
}
.nav-link.is-current { background: rgba(255, 255, 255, 0.1); color: white; }
.nav-link:hover { color: white; }
.icon-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: inherit;
  display: grid;
  place-items: center;
  padding: 0;
}
.icon-btn svg { width: 22px; height: 22px; }
.topbar .icon-btn:hover { background: rgba(255, 255, 255, 0.08); }
#nav-toggle { display: none; }
.theme-btn .icon-sun { display: none; }
html[data-theme="dark"] .theme-btn .icon-moon { display: none; }
html[data-theme="dark"] .theme-btn .icon-sun { display: block; }

.shell {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 300px 1fr;
  padding: 16px;
  gap: 16px;
}

.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-ink);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px 12px;
}
.sidebar-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}
.text-btn {
  border: 0;
  background: transparent;
  color: #f0a08a;
  font-weight: 700;
  padding: 6px 8px;
  border-radius: 8px;
}
.text-btn:hover { background: rgba(255, 255, 255, 0.06); }
.dialog-list {
  overflow: auto;
  padding: 4px 8px 12px;
  flex: 1;
}
.dialog-empty {
  margin: 28px 12px;
  color: var(--sidebar-muted);
  font-size: 14px;
  line-height: 1.45;
}
.dialog-item {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 36px;
  align-items: center;
  gap: 2px;
  border-radius: 14px;
}
.dialog-open {
  min-width: 0;
  text-align: left;
  border: 0;
  background: transparent;
  color: inherit;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 10px;
  padding: 10px;
  border-radius: 14px;
}
.dialog-item:hover { background: var(--sidebar-hover); }
.dialog-item.is-active,
.dialog-item.is-active:hover { background: var(--sidebar-active); }
.dialog-delete {
  width: 36px;
  height: 36px;
  margin-right: 4px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--sidebar-muted);
  display: grid;
  place-items: center;
  padding: 0;
  opacity: 0;
}
.dialog-delete svg { width: 18px; height: 18px; }
.dialog-item:hover .dialog-delete,
.dialog-item.is-active .dialog-delete,
.dialog-delete:focus-visible { opacity: 1; }
.dialog-delete:hover { background: rgba(228, 87, 46, 0.18); color: #ffb4a2; }
.dialog-item strong, .dialog-item span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dialog-item strong { font-size: 14px; }
.dialog-item span { color: var(--sidebar-muted); font-size: 13px; margin-top: 2px; }
.avatar, .mini-avatar {
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: white;
  flex: 0 0 auto;
}
.mini-avatar { width: 42px; height: 42px; font-size: 15px; }
.avatar { width: 44px; height: 44px; font-size: 16px; }

.stage {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
}
.view { flex: 1; min-height: 0; min-width: 0; }

.setup {
  overflow: auto;
  padding: 32px 20px 40px;
}
.setup-card { width: min(580px, 100%); margin: 0 auto; }
.eyebrow {
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 12px;
  font-weight: 800;
  color: var(--accent);
}
.setup-card h1, .search-card h1 {
  margin: 0;
  font-family: var(--display);
  font-weight: 500;
  font-size: clamp(28px, 4vw, 40px);
  letter-spacing: -0.04em;
  line-height: 1.15;
}
.lead, .hint, .form-note, .search-filters {
  color: var(--muted);
  line-height: 1.5;
}
.lead { margin: 12px 0 8px; font-size: 16px; }
.field { border: 0; margin: 18px 0 0; padding: 0; }
.field legend {
  font-size: 13px;
  font-weight: 800;
  margin-bottom: 8px;
  padding: 0;
}
.hint { margin: -2px 0 8px; font-size: 13px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 999px;
  padding: 8px 14px;
  font-weight: 600;
  font-size: 14px;
}
.chip:hover { border-color: #c9bfb2; }
.chip.is-on {
  background: var(--ink);
  color: var(--panel);
  border-color: var(--ink);
}
html[data-theme="dark"] .chip.is-on {
  background: #f4efe8;
  color: #1c1917;
  border-color: #f4efe8;
}

.btn {
  border: 0;
  border-radius: 999px;
  font-weight: 800;
}
.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
}
.btn-primary:hover { filter: brightness(1.05); }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }
#start-search {
  width: 100%;
  margin-top: 22px;
  height: 52px;
  font-size: 16px;
}
.form-note { margin: 12px 0 0; font-size: 13px; text-align: center; }
.form-note strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.btn-ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
  height: 48px;
  padding: 0 22px;
}
.btn-ghost:hover { background: var(--panel-2); }
.btn-small {
  height: 36px;
  padding: 0 14px;
  background: var(--ink);
  color: var(--panel);
  font-size: 14px;
}
html[data-theme="dark"] .btn-small { background: #f4efe8; color: #1c1917; }
.btn-quiet {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

.search {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.search-card { text-align: center; width: min(460px, 100%); }
.search-card h1 { margin-top: 8px; }
.search-filters { min-height: 44px; }
.radar {
  width: 112px;
  height: 112px;
  margin: 0 auto 8px;
  position: relative;
}
.radar span {
  position: absolute;
  inset: 0;
  border: 1.5px solid var(--accent);
  border-radius: 50%;
  opacity: 0;
  animation: pulse 2.4s ease-out infinite;
}
.radar span:nth-child(2) { animation-delay: 0.6s; }
.radar span:nth-child(3) { animation-delay: 1.2s; }
.radar::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent);
  transform: translate(-50%, -50%);
}
@keyframes pulse {
  0% { transform: scale(0.35); opacity: 0.7; }
  100% { transform: scale(1); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .radar span { animation: none; opacity: 0.45; }
}

.chat {
  display: flex;
  flex-direction: column;
}
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  min-width: 0;
}
.peer { display: flex; align-items: center; gap: 12px; min-width: 0; }
.peer-meta { min-width: 0; }
.peer-meta strong, .peer-meta span { display: block; }
.peer-meta strong { font-size: 15px; }
.peer-meta span {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.chat-actions .icon-btn { color: var(--muted); }
.chat-actions .icon-btn:hover { background: var(--panel-2); color: var(--danger); }

.messages {
  flex: 1;
  overflow: auto;
  padding: 18px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background:
    linear-gradient(180deg, transparent, transparent),
    var(--panel);
}
.day {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel-2);
  border-radius: 999px;
  padding: 4px 10px;
  margin-bottom: 6px;
}
.bubble-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  max-width: min(82%, 640px);
}
.bubble-row.me { align-self: flex-end; align-items: flex-end; }
.bubble-row.peer { align-self: flex-start; }
.bubble-row.system { align-self: center; align-items: center; max-width: 100%; }
.bubble {
  padding: 10px 12px;
  border-radius: 16px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble-row.peer .bubble { background: var(--theirs); border-bottom-left-radius: 5px; }
.bubble-row.me .bubble { background: var(--mine); color: var(--mine-ink); border-bottom-right-radius: 5px; }
.bubble-row.system .bubble {
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  padding: 4px 8px;
}
.time {
  font-size: 11px;
  color: var(--muted);
  margin: 0 2px;
}
.typing {
  display: flex;
  gap: 4px;
  padding: 12px 14px;
}
.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.1s infinite;
}
.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

.composer-wrap {
  border-top: 1px solid var(--line);
  padding: 12px;
  position: relative;
}
.composer-wrap.is-closed { display: none; }
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--panel-2);
  border-radius: 16px;
  padding: 6px;
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 0;
  background: transparent;
  padding: 10px 4px;
  max-height: 120px;
  line-height: 1.4;
}
.composer textarea:focus { outline: none; }
.send {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: white;
  display: grid;
  place-items: center;
}
.send svg { width: 20px; height: 20px; }
.send:disabled { opacity: 0.4; cursor: not-allowed; }
.emoji-panel {
  position: absolute;
  left: 12px;
  bottom: 72px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  width: min(280px, calc(100% - 24px));
}
.emoji-panel button {
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  border-radius: 8px;
  font-size: 20px;
}
.emoji-panel button:hover { background: var(--panel-2); }
.closed-note {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  padding: 8px 0 4px;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(28, 25, 23, 0.46);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 30;
}
.modal-card {
  width: min(440px, 100%);
  background: var(--panel);
  color: var(--ink);
  border-radius: 20px;
  padding: 22px;
  box-shadow: var(--shadow);
}
.modal-card h3 { margin: 0 0 8px; font-size: 22px; letter-spacing: -0.03em; }
.modal-card p { margin: 0; color: var(--muted); line-height: 1.5; white-space: pre-wrap; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.modal-actions .btn { height: 42px; padding: 0 16px; }
.modal-actions .btn-primary.danger { background: var(--danger); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--panel);
  border-radius: 999px;
  padding: 10px 16px;
  z-index: 40;
  font-size: 14px;
  font-weight: 700;
}

@media (max-width: 720px) {
  .chat-head { flex-wrap: wrap; row-gap: 10px; }
  .chat-actions { flex: 1 1 100%; justify-content: flex-start; }
}

@media (hover: none) {
  .dialog-delete { opacity: 1; }
}

@media (max-width: 860px) {
  #nav-toggle { display: grid; }
  .domain { display: none; }
  .shell { grid-template-columns: 1fr; padding: 0; gap: 0; }
  .stage { border-radius: 0; box-shadow: none; }
  .sidebar {
    position: fixed;
    z-index: 20;
    top: 64px;
    bottom: 0;
    left: 0;
    width: min(320px, 88vw);
    border-radius: 0 18px 18px 0;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
  }
  .sidebar.is-open { transform: none; }
  .backdrop {
    position: fixed;
    inset: 64px 0 0;
    background: rgba(28, 25, 23, 0.4);
    z-index: 19;
  }
  .bubble-row { max-width: 92%; }
  .chat-actions .btn-small { padding: 0 12px; }
  .composer, .composer-wrap, .messages, .chat-head { max-width: 100%; }
}

@media (max-width: 520px) {
  .topnav .nav-link:not(#open-rules) { display: none; }
  .setup { padding: 20px 16px 28px; }
  .setup-card h1 { font-size: 28px; }
}
