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

body {
  font-family: system-ui, sans-serif;
  background: #111;
  color: #eee;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: stretch;
}

#app {
  display: none;
  flex-direction: column;
  width: 100%;
  max-width: 760px;
  min-height: 100vh;
  height: 100vh;
  padding: 0.75rem 1rem 0.5rem;
}

#status-bar {
  padding: 0.4rem 0;
  font-size: 0.8rem;
  color: #888;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #555;
}

.dot.connected {
  background: #4a4;
}

.dot.disconnected {
  background: #a44;
}

#conv-bar {
  display: flex;
  gap: 0.4rem;
  padding: 0.3rem 0;
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none;
}

#conv-bar::-webkit-scrollbar { display: none; }

#new-conv-btn {
  padding: 0.25rem 0.6rem;
  border: 1px solid #444;
  border-radius: 6px;
  background: #1a2a1a;
  color: #aaa;
  cursor: pointer;
  font-size: 0.75rem;
  white-space: nowrap;
  flex-shrink: 0;
}

.conv-item {
  padding: 0.25rem 0.6rem;
  border: 1px solid #333;
  border-radius: 6px;
  background: #1a1a2a;
  color: #999;
  cursor: pointer;
  font-size: 0.75rem;
  white-space: nowrap;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
}

.conv-item.active {
  border-color: #5a7aaa;
  color: #ccc;
  background: #2a3a5a;
}

.conv-item:hover { border-color: #555; color: #ccc; }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0 1.5rem;
  min-height: 0;
}

.msg {
  margin-bottom: 0.9rem;
  padding: 0.8rem 0.95rem;
  border-radius: 10px;
  max-width: min(88%, 640px);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.45;
}

.msg.user {
  background: #2a2a3a;
  margin-left: auto;
}

.msg.assistant {
  background: #1a2a1a;
}

.msg.error {
  background: #3a1a1a;
  color: #f88;
}

#chat-form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  position: sticky;
  bottom: 0;
  padding: 0.9rem 0 calc(0.35rem + env(safe-area-inset-bottom));
  background: linear-gradient(to top, #111 82%, rgba(17, 17, 17, 0));
}

#upload-label {
  cursor: pointer;
  font-size: 1.2rem;
  padding: 0.4rem;
  flex-shrink: 0;
}

#file-input {
  display: none;
}

#input {
  flex: 1;
  padding: 0.6rem;
  border: 1px solid #333;
  border-radius: 12px;
  background: #1a1a1a;
  color: #eee;
  font-size: 1rem;
  min-height: 144px;
  max-height: 240px;
  resize: none;
  overflow-y: auto;
  font-family: inherit;
  line-height: 1.4;
}

#chat-form button {
  padding: 0.8rem 1.2rem;
  border: none;
  border-radius: 12px;
  background: #3a5a8a;
  color: #eee;
  cursor: pointer;
  font-size: 1rem;
  flex-shrink: 0;
}

#token-gate {
  text-align: center;
  margin: auto;
}

#token-gate label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 1.1rem;
}

#token-gate input {
  padding: 0.6rem;
  border: 1px solid #333;
  border-radius: 6px;
  background: #1a1a1a;
  color: #eee;
  font-size: 1rem;
  width: 250px;
}

#token-gate button {
  margin-left: 0.5rem;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 6px;
  background: #3a5a8a;
  color: #eee;
  cursor: pointer;
  font-size: 1rem;
}

@media (max-width: 720px) {
  #app {
    max-width: none;
    padding: 0.5rem 0.75rem 0.35rem;
  }

  .msg {
    max-width: 92%;
  }

  #chat-form {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "input input"
      "upload send";
    gap: 0.45rem;
    align-items: stretch;
  }

  #input {
    grid-area: input;
    width: 100%;
    min-height: 160px;
  }

  #upload-label {
    grid-area: upload;
    align-self: center;
    justify-self: start;
    padding: 0.25rem 0.15rem;
  }

  #chat-form button {
    grid-area: send;
    padding: 0.8rem 1rem;
    justify-self: end;
  }
}
