:root {
  --kchat-bg: rgba(12, 12, 16, 0.82);
  --kchat-border: rgba(255, 255, 255, 0.1);
  --kchat-text: #e8e8ee;
  --kchat-muted: #9a9aad;
  --kchat-accent: #8b5cf6;
  --kchat-danger: #ef4444;
  --kchat-radius: 14px;
  --kchat-font: "Inter", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

.kchat-page {
  margin: 0;
  min-height: 100vh;
  font-family: var(--kchat-font);
  color: var(--kchat-text);
  background: transparent;
}

.kchat-page__shell {
  position: relative;
  z-index: 1;
  max-width: 920px;
  margin: 0 auto;
  padding: 20px 16px 32px;
}

.kchat-page__head {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
}

.kchat-page__head h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 600;
}

.kchat-page__home {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--kchat-muted);
  text-decoration: none;
  font-size: 0.9rem;
}

.kchat-page__home:hover {
  color: #fff;
}

.kchat {
  border: 1px solid var(--kchat-border);
  border-radius: calc(var(--kchat-radius) + 4px);
  background: var(--kchat-bg);
  backdrop-filter: blur(16px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 520px;
}

#view-chat .kchat {
  min-height: 560px;
}

.kchat__panel[hidden],
.kchat__subpanel[hidden],
.kchat__session-home[hidden],
.kchat__session-active[hidden] {
  display: none !important;
}

.kchat__subtabs {
  display: flex;
  gap: 8px;
  padding: 12px 16px 0;
}

.kchat__subtab {
  appearance: none;
  border: 1px solid var(--kchat-border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--kchat-muted);
  padding: 8px 14px;
  border-radius: 10px;
  font: inherit;
  cursor: pointer;
}

.kchat__subtab--active {
  color: #fff;
  border-color: rgba(139, 92, 246, 0.55);
  background: rgba(139, 92, 246, 0.18);
}

.kchat__subpanel {
  padding: 14px 16px 16px;
}

.kchat__tabs {
  display: flex;
  gap: 8px;
  padding: 12px 12px 0;
  border-bottom: 1px solid var(--kchat-border);
}

.kchat__tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--kchat-muted);
  padding: 10px 14px;
  border-radius: 10px 10px 0 0;
  font: inherit;
  cursor: pointer;
}

.kchat__tab--active {
  color: #fff;
  background: rgba(255, 255, 255, 0.06);
}

.kchat__status {
  margin: 10px 14px 0;
  font-size: 0.85rem;
  color: var(--kchat-muted);
}

.kchat__status--error {
  color: #fca5a5;
}

.kchat__panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.kchat__messages {
  flex: 1;
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 320px;
  max-height: 52vh;
}

.kchat__messages--session {
  max-height: 36vh;
  min-height: 180px;
}

.kchat__msg {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.kchat__msg--mine {
  flex-direction: row-reverse;
}

.kchat__msg--mine .kchat__msg-body {
  align-items: flex-end;
}

.kchat__avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.kchat__avatar--fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(139, 92, 246, 0.35);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
}

.kchat__msg-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: min(78%, 560px);
}

.kchat__msg-meta {
  display: flex;
  gap: 8px;
  align-items: baseline;
  font-size: 0.78rem;
  color: var(--kchat-muted);
}

.kchat__msg-meta strong {
  color: #fff;
  font-size: 0.82rem;
}

.kchat__msg-text {
  margin: 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  line-height: 1.45;
  word-break: break-word;
}

.kchat__msg--mine .kchat__msg-text {
  background: rgba(139, 92, 246, 0.28);
}

.kchat__msg-image {
  display: block;
  max-width: min(280px, 70vw);
  max-height: 240px;
  width: auto;
  height: auto;
  margin: 6px 0 4px;
  border-radius: 12px;
  border: 1px solid var(--kchat-border);
  object-fit: contain;
  background: rgba(0, 0, 0, 0.25);
  cursor: pointer;
}

.kchat__composer {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--kchat-border);
  align-items: center;
}

.kchat__img-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--kchat-border);
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
  font: inherit;
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
}

.kchat__img-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.kchat__attach {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px 12px;
}

.kchat__attach[hidden] {
  display: none !important;
}

.kchat__attach img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--kchat-border);
}

.kchat__attach button {
  border: 0;
  border-radius: 8px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  font: inherit;
  cursor: pointer;
}

.kchat__composer input {
  flex: 1;
  border: 1px solid var(--kchat-border);
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
  border-radius: 10px;
  padding: 11px 12px;
  font: inherit;
}

.kchat__composer button {
  border: 0;
  border-radius: 10px;
  padding: 0 16px;
  background: var(--kchat-accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.kchat__session-home {
  padding: 16px;
  display: grid;
  gap: 14px;
}

.kchat__card {
  border: 1px solid var(--kchat-border);
  border-radius: var(--kchat-radius);
  padding: 16px;
  background: rgba(0, 0, 0, 0.18);
}

.kchat__card h2 {
  margin: 0 0 6px;
  font-size: 1rem;
}

.kchat__card p {
  margin: 0 0 12px;
  color: var(--kchat-muted);
  font-size: 0.9rem;
}

.kchat__create-row,
.kchat__join-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.kchat__create-row button,
.kchat__join-row button,
.kchat__btn-ghost,
.kchat__btn-danger {
  border: 1px solid var(--kchat-border);
  background: rgba(255, 255, 255, 0.05);
  color: #fff;
  border-radius: 10px;
  padding: 10px 14px;
  font: inherit;
  cursor: pointer;
}

.kchat__create-row button:hover,
.kchat__join-row button:hover {
  background: rgba(255, 255, 255, 0.1);
}

.kchat__join-row input {
  flex: 1;
  min-width: 140px;
  border: 1px solid var(--kchat-border);
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.kchat__session-active {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.kchat__session-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--kchat-border);
}

.kchat__session-mode {
  margin-left: 10px;
  font-size: 0.75rem;
  color: var(--kchat-muted);
  letter-spacing: 0.08em;
}

.kchat__session-actions {
  display: flex;
  gap: 8px;
}

.kchat__btn-danger {
  border-color: rgba(239, 68, 68, 0.45);
  color: #fecaca;
}

.kchat__session-members {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--kchat-border);
}

.kchat__member {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  font-size: 0.82rem;
}

.kchat__member img,
.kchat__member-fallback {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
}

.kchat__member-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(139, 92, 246, 0.35);
  font-size: 0.7rem;
  font-weight: 600;
}

@media (max-width: 640px) {
  .kchat__messages {
    max-height: 44vh;
  }

  .kchat__session-bar {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 768px) {
  #view-chat .kchat {
    min-height: min(560px, calc(100dvh - 12rem));
  }

  .kchat-page__shell {
    padding: 12px 12px calc(24px + env(safe-area-inset-bottom, 0px));
  }

  .kchat__create-row,
  .kchat__join-row {
    flex-direction: column;
  }

  .kchat__create-row button,
  .kchat__join-row button {
    width: 100%;
  }
}

@media (max-width: 430px) {
  .kchat {
    min-height: min(480px, calc(100dvh - 10rem));
    border-radius: 14px;
  }

  .kchat__composer {
    flex-wrap: wrap;
  }

  .kchat__composer button {
    width: 100%;
    min-height: 44px;
  }

  .kchat__messages {
    max-height: 38vh;
    min-height: 180px;
  }
}
