:root {
  --bg: #0e0c0a;
  --bg-2: #161310;
  --bg-3: #1e1a16;
  --line: #2c2720;
  --text: #f4efe6;
  --muted: #9a9084;
  --gold: #e4b56a;
  --gold-2: #c7923e;
  --danger: #d46a5c;
  --user: #2a241c;
  --radius: 18px;
  --font: "Instrument Sans", system-ui, sans-serif;
  --display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background:
    radial-gradient(1200px 600px at 10% -10%, #2a2014 0%, transparent 55%),
    var(--bg);
  color: var(--text);
  letter-spacing: 0.01em;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 0.9rem; margin: 0.4rem 0 0; }
code { font-size: 0.85em; }

button, input, textarea, select {
  font: inherit;
  color: inherit;
}
button { cursor: pointer; }

.gate {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.gate-card {
  width: min(420px, 100%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 28px;
  padding: 36px 32px 32px;
  box-shadow: 0 30px 80px rgba(0,0,0,.35);
}
.mark {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, var(--gold), var(--gold-2));
  color: #1a1208;
  font-family: var(--display);
  font-size: 1.7rem;
  font-weight: 650;
}
.gate-card h1 {
  font-family: var(--display);
  font-weight: 650;
  font-size: 2rem;
  margin: 16px 0 6px;
}
.gate-card form {
  display: grid;
  gap: 12px;
  margin-top: 22px;
}
.gate-card label {
  display: grid;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--muted);
}
.gate-card input, .add-user input, .add-user select {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 12px;
  outline: none;
}
.gate-card input:focus, textarea:focus, .add-user input:focus, .add-user select:focus {
  border-color: var(--gold);
}
.gate-card button[type=submit], .add-user button, .btn-new {
  background: var(--gold);
  color: #1a1208;
  border: 0;
  border-radius: 12px;
  padding: 12px 14px;
  font-weight: 600;
}
.gate-card button[type=submit]:hover, .btn-new:hover, .add-user button:hover {
  filter: brightness(1.05);
}

.app {
  height: 100%;
  display: grid;
  grid-template-columns: 280px 1fr;
}

.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-top { padding: 16px; }
.btn-new { width: 100%; }
.chat-list {
  flex: 1;
  overflow: auto;
  padding: 0 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.chat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 12px;
  padding: 2px 4px 2px 10px;
}
.chat-item button.pick {
  flex: 1;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 10px 6px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-item:hover, .chat-item.active {
  background: var(--bg-3);
}
.chat-item .x {
  background: transparent;
  border: 0;
  color: var(--muted);
  padding: 6px 8px;
  border-radius: 8px;
  opacity: 0;
}
.chat-item:hover .x { opacity: 1; }
.sidebar-foot {
  border-top: 1px solid var(--line);
  padding: 12px 14px 16px;
  display: grid;
  gap: 8px;
}
.who {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.92rem;
}
.ghost {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 10px;
  color: var(--muted);
}
.ghost:hover { color: var(--text); border-color: #4a4338; }

.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 22px;
  border-bottom: 1px solid transparent;
  font-family: var(--display);
  font-size: 1.15rem;
}
.icon-btn {
  display: none;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px 10px;
}

.thread {
  overflow: auto;
  padding: 12px 0 32px;
}
.msg {
  max-width: 760px;
  margin: 0 auto;
  padding: 18px 24px;
}
.msg .role {
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
}
.msg.user .bubble {
  background: var(--user);
  border: 1px solid var(--line);
  border-radius: 16px 16px 6px 16px;
  padding: 12px 14px;
  white-space: pre-wrap;
}
.msg.assistant .md {
  line-height: 1.65;
}
.msg.assistant .md p { margin: 0 0 0.8em; }
.msg.assistant .md p:last-child { margin-bottom: 0; }
.msg.assistant .md pre {
  background: #120f0c;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  overflow: auto;
}
.msg.assistant .md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
}
.msg.assistant .md :not(pre) > code {
  background: #211c16;
  padding: 0.1em 0.35em;
  border-radius: 6px;
}
.empty {
  height: 100%;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 40px 20px;
}
.empty h2 {
  font-family: var(--display);
  font-weight: 500;
  font-size: 2.1rem;
  margin: 0 0 8px;
}
.cursor::after {
  content: "▍";
  animation: blink 1s steps(1) infinite;
  color: var(--gold);
}
@keyframes blink { 50% { opacity: 0; } }

.composer {
  max-width: 760px;
  width: calc(100% - 32px);
  margin: 0 auto 22px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 8px 8px 8px 12px;
}
.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 180px;
  background: transparent;
  border: 0;
  outline: none;
  padding: 10px 0;
  line-height: 1.45;
}
.composer button#send {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: var(--gold);
  color: #1a1208;
  font-size: 1.15rem;
  flex: none;
}
.composer button:disabled {
  opacity: 0.4;
  cursor: default;
}
.attach-btn {
  width: 40px;
  height: 40px;
  flex: none;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: transparent;
  color: var(--gold);
  font-size: 1.25rem;
  line-height: 1;
}
.attach-btn:hover { border-color: var(--gold); }
.composer.drop { border-color: var(--gold); }
.attach-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 4px 0;
}
.attach-chip {
  position: relative;
  width: 72px;
  height: 72px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--line);
}
.attach-chip img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.attach-chip .rm {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 999px;
  background: rgba(14,12,10,.78);
  color: var(--text);
  font-size: 0.85rem;
  line-height: 1;
  padding: 0;
}
.msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.msg-images img {
  max-width: min(320px, 100%);
  max-height: 280px;
  border-radius: 14px;
  border: 1px solid var(--line);
  object-fit: cover;
}

.admin {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(8,6,4,.62);
  padding: 20px;
  z-index: 20;
}
.admin-panel {
  width: min(640px, 100%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 22px;
  max-height: 86vh;
  overflow: auto;
}
.admin-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.admin-head h2 {
  font-family: var(--display);
  margin: 0;
}
.add-user {
  display: grid;
  grid-template-columns: 1fr 1fr 110px auto;
  gap: 8px;
  margin: 16px 0;
}
table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  font-size: 0.95rem;
}
td button { font-size: 0.85rem; }

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 5;
}

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(300px, 86vw);
    z-index: 10;
    transform: translateX(-105%);
    transition: transform .2s ease;
  }
  .sidebar.open { transform: none; }
  .icon-btn { display: inline-flex; }
  .add-user { grid-template-columns: 1fr; }
}
