:root {
  --font-base: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --bg: #0d0d10;
  --bg-elev: #17171b;
  --bg-card: #1f1f24;
  --bg-soft: #151519;
  --text: #e7e7eb;
  --muted: #a3a3ab;
  --line: #34343b;
  --accent: #b4b4bc;
  --accent-2: #94949c;
  --danger: #ef4444;
  --ok: #10b981;
  --warn: #f59e0b;
  --focus: #c9c9d0;
}

body {
  margin: 0;
  font-family: var(--font-base);
  background: var(--bg);
  color: var(--text);
}

h1,
h2,
p,
label,
input,
textarea,
button {
  font-family: var(--font-base);
}

h1 {
  font-weight: 700;
  margin: 0;
}

h2 {
  font-weight: 600;
  margin: 0 0 6px;
}

label {
  font-weight: 500;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 29px 20px;
  background: var(--bg-elev);
  color: var(--text);
  position: sticky;
  top: 0;
  z-index: 4;
  gap: 14px;
}

.topbar h1 {
  font-size: 16px;
  line-height: 1.05;
  font-weight: 600;
}

.topbar a {
  color: #dadade;
  margin-left: 0;
  text-decoration: none;
  font-size: 14px;
}

.topbar a:hover {
  color: var(--accent);
}

.topbar nav {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 90px;
  justify-content: flex-end;
  margin-left: auto;
}

.topbar nav a,
.topbar nav .link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 32px;
  box-sizing: border-box;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid #32323a;
  background: #212127;
  color: #e0e0e7;
  text-decoration: none;
  font-size: 14px;
  line-height: 1;
}

.topbar nav a:hover,
.topbar nav .link-btn:hover {
  border-color: #3f3f4a;
  background: #282830;
}

.topbar nav a.active {
  font-weight: 700;
}

.top-menu {
  position: relative;
}

.top-menu-toggle {
  border: 1px solid #373741;
  border-radius: 8px;
  background: #23232b;
  color: #ececf2;
  padding: 6px 10px;
  font-size: 13px;
  line-height: 1.1;
  cursor: pointer;
  user-select: none;
  list-style: none;
}

.top-menu-toggle::-webkit-details-marker {
  display: none;
}

.top-menu-toggle::marker {
  content: "";
}

.top-menu[open] .top-menu-toggle {
  border-color: #50505d;
}

.top-menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 150px;
  padding: 6px;
  border: 1px solid #3a3a45;
  border-radius: 10px;
  background: #1b1b22;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.42);
  z-index: 12;
}

.top-menu-form {
  margin: 0;
}

.top-menu-item {
  display: block;
  width: 100%;
  box-sizing: border-box;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: #e6e6ed;
  padding: 8px 10px;
  text-decoration: none;
  text-align: left;
  font-size: 13px;
  line-height: 1.2;
  cursor: pointer;
  font-weight: 400;
}

.top-menu-item:hover {
  background: #2c2c36;
  transform: none;
}

.top-menu-logout {
  font-weight: 400;
}

.top-context {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
  justify-content: center;
}

.top-chip {
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid #32323a;
  background: #212127;
  color: #e0e0e7;
  font-size: 14px;
  text-decoration: none;
  white-space: nowrap;
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-chip-link:hover {
  border-color: #3f3f4a;
  background: #282830;
}

.top-chat-switcher {
  position: relative;
  min-width: 0;
}

.top-chat-toggle {
  border: 1px solid #3a3a46;
  border-radius: 8px;
  background: #23232b;
  color: #ececf2;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  font-size: 14px;
  max-width: 320px;
}

.top-chat-toggle:disabled,
.top-chat-new:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.top-chat-caption {
  color: #b3b3be;
  font-size: 14px;
}

.top-chat-current {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-chat-caret {
  color: #bdbdc6;
  font-size: 14px;
}

.top-chat-dropdown {
  position: absolute;
  top: calc(100% + 7px);
  left: 0;
  width: 360px;
  max-height: 350px;
  overflow: hidden;
  border-radius: 10px;
  border: 1px solid #3a3a45;
  background: #1a1a20;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
  z-index: 10;
}

.top-chat-new {
  margin: 10px;
}

.top-chat-list {
  max-height: 260px;
  overflow-y: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.top-chat-item {
  width: calc(100% - 10px);
  box-sizing: border-box;
  margin: 5px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: #23232a;
  color: #ececf2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  text-align: left;
}

.top-chat-item:hover {
  border-color: #4c4c56;
}

.top-chat-item.active {
  border-color: #5f5f6d;
  background: #2b2b35;
}

.top-chat-item-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.top-chat-select {
  border: 0;
  background: transparent;
  color: inherit;
  min-width: 0;
  flex: 1;
  padding: 4px 4px;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.top-chat-item-time {
  color: #a7a7b2;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.top-chat-item-meta {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.top-chat-delete {
  width: 24px;
  height: 24px;
  padding: 0;
  border: 1px solid #464654;
  border-radius: 6px;
  background: #2d2d38;
  color: #ececf2;
  font-size: 12px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 0.14s ease, background 0.14s ease;
}

.top-chat-delete:hover {
  border-color: #666679;
  background: #383847;
  transform: none;
}

.user-chip {
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 8px;
  background: #2a2a31;
  color: #ededf0;
}

.logout-form {
  margin: 0;
}

.link-btn {
  width: auto;
  background: transparent;
  color: #d6d6de;
  padding: 0;
  border: 0;
  font-size: 14px;
  font-weight: 400;
  transform: none;
  transition: none;
}

.container {
  max-width: none;
  width: 100%;
  box-sizing: border-box;
  margin: 0 auto;
  padding: 22px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

textarea {
  width: 100%;
  margin-top: 12px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  resize: vertical;
  background: var(--bg-soft);
  color: var(--text);
  line-height: 1.45;
  transition: border-color 0.16s ease, box-shadow 0.16s ease;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.users-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #18181d;
}

.user-row-main {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.user-row-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.user-action-form {
  margin: 0;
}

.pw-chip {
  display: inline-block;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #151519;
  color: #f0f0f3;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 14px;
}

.pw-reveal {
  margin-top: 10px;
  margin-bottom: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #18181d;
}

input[type="password"],
input[type="text"] {
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-soft);
  color: var(--text);
  transition: border-color 0.16s ease, box-shadow 0.16s ease;
}

input::placeholder,
textarea::placeholder {
  color: var(--muted);
}

input:focus,
textarea:focus {
  outline: 0;
  border-color: var(--focus);
  box-shadow: 0 0 0 2px rgba(201, 201, 208, 0.16);
}

button {
  width: fit-content;
  padding: 9px 13px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #111114;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.08s ease, opacity 0.18s ease;
}

button:hover {
  transform: translateY(-1px);
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

button.danger {
  background: var(--danger);
  color: #fff;
}

.notice {
  color: var(--ok);
  font-weight: 600;
}

.status {
  color: #d2d2da;
  font-weight: 500;
}

.chat-status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 26px;
  gap: 10px;
}

.chat-status-timer {
  text-align: right;
  min-width: 42px;
  font-variant-numeric: tabular-nums;
}

.chat-status-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-status-hint {
  color: var(--muted);
  font-size: 12px;
}

.chat-status-row .status {
  margin: 0;
  font-size: 14px;
  line-height: 1.46;
}

.chat-meta {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  margin-bottom: 10px;
  min-height: 24px;
  flex-wrap: wrap;
}

.meta-chip {
  display: inline-flex;
  align-items: center;
  padding: 5px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #212128;
  color: #d8d8de;
  font-size: 12px;
  line-height: 1;
}

.meta-chip.ok {
  border-color: #1f6c52;
  color: #b9f4df;
}

.meta-chip.warn {
  border-color: #8d6a26;
  color: #f8e3b4;
}

.hidden {
  display: none !important;
}

.chat-banner {
  display: none;
}

.chat-banner.show {
  display: block;
  margin-bottom: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid #565661;
  background: #202028;
  color: #e7e7eb;
  font-size: 13px;
}

.chat-banner.warn {
  border-color: #7b6026;
  background: #2a2315;
  color: #ffefcf;
}

.muted {
  color: var(--muted);
  margin-top: 0;
}

hr {
  border: 0;
  border-top: 1px solid var(--line);
}

.login-card {
  max-width: 420px;
  margin: 28px auto;
}

.login-card .form button[type="submit"] {
  margin: 4px 0 0;
}

.chat-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 16px;
}

.chat-config {
  height: fit-content;
}

.chat-main {
  min-height: calc(100dvh - 120px);
  display: flex;
  flex-direction: column;
  background: transparent;
  border: 0;
  box-shadow: none;
  padding: 0;
}

.chat-log {
  flex: 1;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 12px;
}

.msg-row {
  display: flex;
  margin: 20px 0;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-row.assistant {
  justify-content: flex-start;
}

.msg-bubble {
  max-width: 82%;
  padding: 10px 13px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.46;
  white-space: pre-wrap;
}

.msg-row.user .msg-bubble {
  background: #3a3a42;
  color: #f5f5f8;
}

.msg-row.assistant .msg-bubble {
  background: #23232a;
  color: var(--text);
  border: 1px solid #313139;
}

.chat-input-row {
  margin-top: 12px;
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding-top: 10px;
  padding-bottom: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
  position: sticky;
  bottom: 0;
  z-index: 2;
  background: linear-gradient(180deg, rgba(13, 13, 16, 0.7) 0%, rgba(13, 13, 16, 0.95) 35%);
}

.chat-input-row textarea {
  margin-top: 0;
  min-height: 70px;
}

.chat-main.thinking .chat-input-row textarea {
  opacity: 0.82;
}

.settings-grid {
  display: grid;
  gap: 14px;
  font-size: 14px;
}

.settings-grid p,
.settings-grid label,
.settings-grid input,
.settings-grid button,
.settings-grid .muted,
.settings-grid .status {
  font-size: 14px;
}

.settings-grid h2 {
  font-size: 20px;
}

.settings-section p {
  line-height: 1.45;
}

.settings-section h3 {
  margin: 10px 0 8px;
  font-size: 16px;
}

@media (max-width: 900px) {
  .chat-layout {
    grid-template-columns: 1fr;
  }

  .container {
    padding: 14px;
  }

  .topbar {
    padding: 27px 14px;
    align-items: flex-start;
    gap: 10px;
    flex-wrap: wrap;
  }

  .top-context {
    order: 3;
    width: 100%;
    justify-content: flex-start;
    overflow-x: auto;
    padding-bottom: 4px;
  }

  .top-chat-toggle {
    max-width: 220px;
  }

  .top-chat-current {
    max-width: 120px;
  }

  .top-chat-dropdown {
    width: 300px;
  }

  .topbar nav {
    order: 2;
    width: auto;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .chat-status-right {
    gap: 6px;
  }

  .chat-status-hint {
    display: none;
  }

  .msg-bubble {
    max-width: 92%;
  }

  .user-row {
    flex-direction: column;
    align-items: flex-start;
  }
}
