:root {
  --bg: #111214;
  --bg-elevated: #2a2d2f;
  --border: #45494c;
  --text: #cfd4d7;
  --text-dim: #9da5aa;
  --accent: #ff7a1a;
  --accent-dim: #603719;
  --good: #8fd6a8;
  --bad: #ff7668;
  --warn: #ffd0a6;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}

.topnav {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 14px 24px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}
.topnav-brand {
  font-weight: 600;
  margin-right: auto;
}
.topnav a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 14px;
  padding: 6px 0;
  border-bottom: 2px solid transparent;
}
.topnav a.active,
.topnav a:hover {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.page {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

.stat-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  margin: 4px 0;
  color: var(--text-dim);
}
.stat-row b {
  color: var(--text);
  font-weight: 500;
}

.bar {
  height: 6px;
  border-radius: 4px;
  background: #1d2022;
  overflow: hidden;
  margin: 2px 0 8px;
}
.bar-fill {
  height: 100%;
  background: var(--accent);
}
.bar-fill.warn {
  background: var(--warn);
}
.bar-fill.bad {
  background: var(--bad);
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge.online {
  background: rgba(63, 207, 142, 0.15);
  color: var(--good);
}
.badge.offline {
  background: rgba(239, 91, 91, 0.15);
  color: var(--bad);
}

.device-card {
  cursor: pointer;
  transition: border-color 0.15s;
}
.device-card:hover {
  border-color: var(--accent);
}
.device-card h3 {
  margin: 0 0 8px;
  font-size: 15px;
}

.split {
  display: flex;
  gap: 16px;
  height: calc(100vh - 130px);
}
.split > div {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.terminal {
  background: #0b0c0d;
  color: #cfd4d7;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-all;
}
.terminal-input {
  display: flex;
  margin-top: 8px;
  gap: 8px;
}
.terminal-input input {
  flex: 1;
  background: #0b0c0d;
  color: #cfd4d7;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  font-family: inherit;
}

.screen-view {
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: var(--text-dim);
}
.screen-view img {
  max-width: 100%;
  max-height: 100%;
  cursor: crosshair;
  outline: none;
}
.screen-view img:focus {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.chat-layout {
  display: flex;
  height: calc(100vh - 60px);
}

.chat-sidebar {
  width: 280px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.sidebar-section select {
  width: 100%;
  margin-bottom: 8px;
}
.sidebar-section h4 {
  margin: 0 0 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}
.device-stats-card {
  padding: 12px;
}
.conversations-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.conversations-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.conversations-header h4 {
  margin: 0;
}
.conversations-header .icon-btn {
  width: 26px;
  height: 26px;
  font-size: 14px;
  padding: 0;
}
.conversation-list {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 8px;
  overflow-y: auto;
}
.conversation-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-dim);
}
.conversation-item:hover {
  background: var(--bg-elevated);
}
.conversation-item.active {
  background: var(--accent-dim);
  color: var(--text);
}
.conversation-item .conv-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation-item .conv-title-input {
  flex: 1;
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 13px;
  font-family: inherit;
}
.conversation-item .conv-menu-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 6px;
  border-radius: 4px;
  line-height: 1;
}
.conversation-item .conv-menu-btn:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
}
.conv-menu {
  position: fixed;
  z-index: 200;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  min-width: 120px;
  padding: 4px;
  display: flex;
  flex-direction: column;
}
.conv-menu button {
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 7px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
.conv-menu button:hover {
  background: rgba(255, 255, 255, 0.08);
}
.conv-menu button.danger {
  color: var(--bad);
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay[hidden] {
  display: none;
}
.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(520px, 90vw);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.modal-header .icon-btn {
  width: 28px;
  height: 28px;
  font-size: 13px;
}
.modal-body {
  padding: 16px;
  overflow-y: auto;
}
.modal-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px 0;
  color: var(--text-dim);
  text-align: center;
}
.modal-empty pre {
  margin: 0;
  font-size: 11px;
  line-height: 1.2;
  color: var(--text-dim);
}
.files-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}
.files-grid img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.files-grid .file-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.files-grid .file-role {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.conversation-title {
  font-weight: 600;
  margin-right: auto;
}
.chat-header {
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 12px;
  align-items: center;
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
}
.msg {
  border-radius: var(--radius);
  padding: 12px 16px;
  max-width: 80%;
  line-height: 1.5;
  white-space: pre-wrap;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent-dim);
}
.msg.assistant {
  align-self: flex-start;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
}
.msg img {
  max-width: 240px;
  border-radius: 8px;
  display: block;
  margin-top: 8px;
}
.msg.system {
  align-self: center;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--text-dim);
  font-style: italic;
  font-size: 13px;
  max-width: 90%;
}

.msg-status {
  color: var(--text-dim);
  font-style: italic;
}

.msg-content > *:first-child {
  margin-top: 0;
}
.msg-content > *:last-child {
  margin-bottom: 0;
}
.msg-content h1,
.msg-content h2,
.msg-content h3,
.msg-content h4,
.msg-content h5,
.msg-content h6 {
  margin: 12px 0 6px;
  line-height: 1.3;
}
.msg-content p {
  margin: 8px 0;
}
.msg-content ul,
.msg-content ol {
  margin: 8px 0;
  padding-left: 22px;
}
.msg-content blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--border);
  color: var(--text-dim);
}
.msg-content code {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 13px;
}
.msg-content pre {
  background: #17191a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg-content pre code {
  background: none;
  padding: 0;
  white-space: pre;
}
.msg-content table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
}
.msg-content th,
.msg-content td {
  border: 1px solid var(--border);
  padding: 4px 10px;
  text-align: left;
}
.msg-content a {
  color: var(--accent);
}

.msg .thinking {
  margin-bottom: 8px;
  font-size: 12px;
  color: var(--text-dim);
}
.msg .thinking summary {
  cursor: pointer;
  user-select: none;
}
.msg .thinking-body {
  margin-top: 6px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 6px;
  white-space: pre-wrap;
  max-height: 240px;
  overflow-y: auto;
}

.msg-footer {
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-dim);
}

.stop-btn {
  border-color: var(--bad);
  color: var(--bad);
}
.stop-btn:hover {
  background: rgba(239, 91, 91, 0.12);
}
.chat-input-area {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  width: 100%;
}
.chat-input-area textarea {
  flex: 1;
  resize: none;
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 14px;
  max-height: 160px;
}
.icon-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  font-size: 16px;
}
.icon-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}
.send-btn {
  background: var(--accent);
  border: none;
  color: white;
  border-radius: 8px;
  padding: 0 16px;
  height: 40px;
  cursor: pointer;
  font-weight: 600;
}
.send-btn:disabled {
  opacity: 0.5;
  cursor: default;
}
.image-preview-row {
  display: flex;
  gap: 8px;
  padding: 0 24px;
  width: 100%;
}
.image-preview {
  position: relative;
}
.image-preview img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 6px;
}
.image-preview button {
  position: absolute;
  top: -6px;
  right: -6px;
  background: var(--bad);
  border: none;
  color: white;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  font-size: 11px;
  cursor: pointer;
  line-height: 1;
}

select,
button.btn {
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
}

.empty-state {
  color: var(--text-dim);
  text-align: center;
  padding: 60px 0;
}
.error-banner {
  background: rgba(239, 91, 91, 0.15);
  color: var(--bad);
  border: 1px solid var(--bad);
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 16px;
}