:root {
  color-scheme: dark;
  --bg: #111214;
  --panel: #1b1d21;
  --panel-2: #23262b;
  --line: #343841;
  --text: #f4f6f8;
  --muted: #9fa7b3;
  --blue: #72a7ff;
  --green: #56d09a;
  --red: #ff6b7a;
  --ink: #07080a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  min-height: 40px;
  padding: 0 14px;
  cursor: pointer;
}

button:hover {
  border-color: var(--blue);
}

button.primary {
  background: var(--blue);
  border-color: var(--blue);
  color: var(--ink);
  font-weight: 700;
}

button.secondary {
  background: #202a36;
}

button.ghost {
  background: transparent;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #111317;
  color: var(--text);
  padding: 12px;
  outline: none;
}

input:focus,
textarea:focus {
  border-color: var(--blue);
}

.shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  border-right: 1px solid var(--line);
  background: #16181c;
  padding: 22px 18px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
}

.brand span,
.sidebarFoot,
.topbar p,
.metric small,
.panelHead span,
label {
  color: var(--muted);
}

.brandMark {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--blue), var(--green));
  color: var(--ink);
  font-weight: 900;
}

.brandMark.large {
  width: 58px;
  height: 58px;
  margin: 0 auto 10px;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav a {
  color: var(--muted);
  text-decoration: none;
  border-radius: 8px;
  padding: 11px 12px;
}

.nav a:hover,
.nav a.active {
  color: var(--text);
  background: var(--panel-2);
}

.sidebarFoot {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.main {
  min-width: 0;
}

.loginPanel {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(114, 167, 255, 0.16), transparent 46%),
    linear-gradient(180deg, #111214, #151719),
    var(--bg);
}

.loginBox {
  width: min(420px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 30px;
  text-align: center;
  box-shadow: 0 22px 80px rgba(0, 0, 0, 0.34);
}

.loginBox form {
  display: grid;
  gap: 12px;
  margin-top: 22px;
  text-align: left;
}

.authSteps {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 18px;
}

.authSteps span {
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  padding: 9px 10px;
  font-size: 13px;
}

.authSteps span.active {
  border-color: rgba(114, 167, 255, 0.7);
  color: var(--text);
  background: rgba(114, 167, 255, 0.12);
}

.appPanel {
  padding: 24px;
}

.hidden {
  display: none !important;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  margin-bottom: 20px;
}

.topbar h1,
.loginBox h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.15;
}

.topActions,
.vncActions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.metricGrid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.metric,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.metric {
  padding: 16px;
  display: grid;
  gap: 8px;
}

.metric span {
  color: var(--muted);
}

.metric strong {
  font-size: 22px;
}

.metric small {
  overflow-wrap: anywhere;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(340px, 0.8fr) minmax(420px, 1.2fr);
  gap: 18px;
  align-items: start;
}

.fullColumn {
  display: grid;
  gap: 18px;
  grid-column: 1 / -1;
}

.leftColumn,
.rightColumn {
  display: grid;
  gap: 18px;
}

.panel {
  min-width: 0;
  overflow: hidden;
}

.panelHead {
  min-height: 58px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panelHead h2 {
  margin: 0;
  font-size: 16px;
}

.sendForm,
.conversationList {
  padding: 16px;
}

.sendForm {
  display: grid;
  gap: 14px;
}

label {
  display: grid;
  gap: 7px;
  font-size: 13px;
}

.pill {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.pill.ok {
  border-color: rgba(86, 208, 154, 0.5);
  color: var(--green);
}

.pill.bad,
.errorText {
  color: var(--red);
}

.pill.loading::after {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 2px solid var(--line);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.conversationList {
  display: grid;
  gap: 10px;
  max-height: 360px;
  overflow: auto;
}

.conversation {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #15181c;
  padding: 12px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  cursor: pointer;
  transition: border-color 0.15s;
}

.conversation:hover {
  border-color: var(--blue);
}

.convTitle {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  grid-column: 1;
}

.convTitle strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.conversation small {
  color: var(--muted);
}

.conversation .preview {
  grid-column: 1 / -1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: block;
}

.headerBadge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--green);
  color: var(--ink);
  font-size: 0.7rem;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  margin-left: 8px;
  vertical-align: middle;
}

.conversation.unread .convTitle strong {
  font-weight: 700;
}

.conversation.unread {
  border-color: rgba(86, 208, 154, 0.35);
  background: #151d17;
}

.messageList {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 480px;
  overflow-y: auto;
}

.msg {
  max-width: 78%;
  padding: 8px 12px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
}

/* Group spacing: new group or timestamp gets extra top margin */
.msg[data-group="first"],
.msg[data-group="single"],
.msg.ts {
  margin-top: 6px;
}

/* Outgoing (right side) */
.msg.out {
  align-self: flex-end;
  background: var(--blue);
  color: var(--ink);
}

.msg.out[data-group="first"]  { border-bottom-right-radius: 5px; }
.msg.out[data-group="mid"]    { border-top-right-radius: 5px; border-bottom-right-radius: 5px; }
.msg.out[data-group="last"]   { border-top-right-radius: 5px; }

/* Incoming (left side) */
.msg.in {
  align-self: flex-start;
  background: var(--panel-2);
}

.msg.in[data-group="first"]  { border-bottom-left-radius: 5px; }
.msg.in[data-group="mid"]    { border-top-left-radius: 5px; border-bottom-left-radius: 5px; }
.msg.in[data-group="last"]   { border-top-left-radius: 5px; }

.msg.ts {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  padding: 2px 0;
}

.msg.skeleton {
  min-height: 36px;
  background: var(--panel-2);
  color: transparent;
  pointer-events: none;
  animation: skeleton-pulse 1.4s ease-in-out infinite;
}

.msg.skeleton.out { align-self: flex-end; }
.msg.skeleton.in  { align-self: flex-start; }

@keyframes skeleton-pulse {
  0%, 100% { opacity: 0.5; }
  50%       { opacity: 0.2; }
}

@keyframes msgFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.messageList > .msg:not(.skeleton) {
  animation: msgFadeIn 0.18s ease;
}

.pinIcon {
  font-size: 11px;
  opacity: 0.6;
  flex-shrink: 0;
}

.controlGrid {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.vncPanel iframe {
  width: 100%;
  height: 520px;
  border: 0;
  background: #050607;
}

.errorText {
  min-height: 20px;
  margin-top: 12px;
}

.keyForm {
  padding: 16px;
  display: grid;
  gap: 12px;
  border-bottom: 1px solid var(--line);
}

.tokenBanner {
  padding: 14px 16px;
  background: rgba(86, 208, 154, 0.08);
  border-bottom: 1px solid rgba(86, 208, 154, 0.3);
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 13px;
}

.tokenBanner code {
  font-family: monospace;
  background: var(--panel-2);
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  word-break: break-all;
  flex: 1;
  min-width: 0;
}

.apiKeyList {
  display: grid;
  gap: 0;
}

.apiKeyRow {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 12px;
  align-items: start;
}

.apiKeyRow:last-child { border-bottom: 0; }

.apiKeyRow.disabled { opacity: 0.5; }

.apiKeyMeta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
}

.apiKeyActions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
  align-items: flex-start;
}

.ipTag {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 11px;
  font-family: monospace;
}

.apiLogList {
  max-height: 380px;
  overflow-y: auto;
  display: grid;
  gap: 0;
}

.logRow {
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  display: grid;
  grid-template-columns: 130px 120px 60px 1fr auto;
  gap: 8px;
  align-items: center;
  font-family: monospace;
}

.logRow:last-child { border-bottom: 0; }

.logRow .logMethod { color: var(--blue); }
.logRow .logPath   { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logRow .logKey    { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logRow .logIp     { color: var(--muted); font-size: 11px; }

/* Send queue panel */
.queueList {
  max-height: 420px;
  overflow-y: auto;
  display: grid;
  gap: 0;
}

.queueRow {
  display: grid;
  grid-template-columns: 56px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
}
.queueRow:last-child { border-bottom: 0; }

.qPriority {
  font-size: 10px;
  font-weight: 700;
  text-align: center;
  padding: 3px 6px;
  border-radius: 6px;
  text-transform: uppercase;
}
.qHigh   { background: #ffe2e2; color: #c0392b; }
.qNormal { background: var(--line); color: var(--muted); }

.qBody { display: grid; gap: 2px; overflow: hidden; }
.qBody strong { font-size: 13px; }
.qText { font-size: 12px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qMeta { color: var(--muted); font-size: 11px; }

.qActions { display: flex; gap: 6px; }
.qActions button { padding: 4px 8px; font-size: 12px; }
.qActions button:disabled { opacity: .4; cursor: not-allowed; }

@media (max-width: 1100px) {
  .metricGrid {
    grid-template-columns: 1fr 1fr;
  }

  .workspace {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 780px) {
  .shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .nav {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .topbar,
  .panelHead {
    align-items: stretch;
    flex-direction: column;
  }

  .metricGrid,
  .controlGrid {
    grid-template-columns: 1fr;
  }

  .appPanel {
    padding: 16px;
  }
}
