/* --- Reset & base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a2e;
  --bg-light: #16213e;
  --bg-sidebar: #0f0f23;
  --bg-input: #252545;
  --bg-hover: #2a2a4a;
  --bg-active: #3a3a5a;
  --text: #e0e0e0;
  --text-dim: #888;
  --text-bright: #fff;
  --accent: #7c5cbf;
  --accent-light: #9b7fd4;
  --green: #4ade80;
  --red: #f87171;
  --yellow: #fbbf24;
  --border: #333;
  --radius: 6px;
}

html, body {
  height: 100%;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
}

/* --- Login view --- */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.login-box {
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.login-box h1 {
  color: var(--accent-light);
  font-size: 1.4rem;
  margin-bottom: 0.25rem;
}

.login-subtitle {
  color: var(--text-dim);
  font-size: 0.8rem;
  margin-bottom: 1.5rem;
}

#login-form input {
  display: block;
  width: 100%;
  padding: 0.65rem 0.75rem;
  margin-bottom: 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}

#login-form input:focus {
  outline: none;
  border-color: var(--accent);
}

#login-form button {
  width: 100%;
  padding: 0.65rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

#login-form button:hover { background: var(--accent-light); }

.error {
  color: var(--red);
  font-size: 0.8rem;
  margin-top: 0.75rem;
}

/* --- App layout --- */
.view { display: flex; flex-direction: column; }
#app-view { height: 100%; }
#app-view[hidden] { display: none; }
#login-view[hidden] { display: none; }

/* Header */
#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  height: 48px;
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-left { display: flex; align-items: center; gap: 0.75rem; }
.brand { color: var(--accent-light); font-weight: bold; font-size: 0.95rem; }

.header-nav { display: flex; gap: 0.25rem; }
.nav-btn {
  background: none;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-family: inherit;
  font-size: 0.8rem;
  padding: 0.3rem 0.75rem;
  border-radius: var(--radius);
  cursor: pointer;
}
.nav-btn:hover { color: var(--text); background: var(--bg-hover); }
.nav-btn.active { color: var(--text-bright); border-color: var(--accent); background: var(--bg-hover); }

.header-right { display: flex; align-items: center; gap: 0.75rem; }
#user-freq { color: var(--accent-light); font-size: 0.85rem; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0.25rem;
}
.icon-btn:hover { color: var(--text); }

/* Body = sidebar + content */
#app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Sidebar */
#sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar-section { padding: 1rem 0.75rem 0.5rem; }
.sidebar-section h3 {
  color: var(--text-dim);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.5rem;
}

#channel-list, #status-list {
  list-style: none;
}

#channel-list li {
  padding: 0.35rem 0.5rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
#channel-list li:hover { background: var(--bg-hover); }
#channel-list li.active { background: var(--bg-active); color: var(--text-bright); }

.channel-name::before { content: '# '; color: var(--text-dim); }
.unread-badge {
  background: var(--accent);
  color: #fff;
  font-size: 0.65rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  display: none;
}
.unread-badge.show { display: inline; }

#status-list li {
  font-size: 0.8rem;
  padding: 0.2rem 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.status-dot.running { background: var(--green); }
.status-dot.stale { background: var(--yellow); }
.status-dot.stopped { background: var(--red); }
.status-dot.external { background: var(--accent-light); }

.sidebar-btn {
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.4rem;
  background: var(--bg-hover);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--text-dim);
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}
.sidebar-btn:hover { color: var(--text); border-color: var(--accent); }

/* Content views */
.content-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.content-view[hidden] { display: none !important; }

/* Chat */
#chat-header {
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-light);
  flex-shrink: 0;
}
#chat-channel-name {
  font-weight: bold;
  color: var(--text-bright);
}
#chat-channel-name::before { content: '# '; color: var(--text-dim); }
#chat-channel-members {
  color: var(--text-dim);
  font-size: 0.75rem;
  margin-left: 0.75rem;
}

#messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 1rem;
}

#messages {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.msg {
  padding: 0.3rem 0;
  line-height: 1.45;
}
.msg-ts {
  color: var(--text-dim);
  font-size: 0.75rem;
  margin-right: 0.4rem;
}
.msg-sender {
  font-weight: bold;
  margin-right: 0.35rem;
}
.msg-body {
  word-break: break-word;
}

/* Sender colors — rotate through a palette */
.sender-0 { color: #7eb8da; }
.sender-1 { color: #c084fc; }
.sender-2 { color: #4ade80; }
.sender-3 { color: #fbbf24; }
.sender-4 { color: #f87171; }
.sender-5 { color: #67e8f9; }
.sender-6 { color: #fb923c; }
.sender-7 { color: #a78bfa; }

/* Message form */
#message-form {
  display: flex;
  padding: 0.5rem 1rem;
  gap: 0.5rem;
  border-top: 1px solid var(--border);
  background: var(--bg-light);
  flex-shrink: 0;
}
#message-input {
  flex: 1;
  padding: 0.55rem 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}
#message-input:focus { outline: none; border-color: var(--accent); }

#message-form button {
  padding: 0.55rem 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1rem;
  cursor: pointer;
}
#message-form button:hover { background: var(--accent-light); }

/* Dashboard */
#dashboard-view { padding: 1.5rem; overflow-y: auto; }
#dashboard-view h2 { color: var(--accent-light); margin-bottom: 1rem; font-size: 1.1rem; }

#dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.dash-card {
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}
.dash-card-name {
  font-weight: bold;
  color: var(--text-bright);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}
.dash-card-info {
  font-size: 0.8rem;
  color: var(--text-dim);
  line-height: 1.6;
}

/* Events */
#events-view { padding: 1.5rem; overflow-y: auto; }
#events-view h2 { color: var(--accent-light); margin-bottom: 1rem; font-size: 1.1rem; }

#events-feed {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.event-item {
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.85rem;
  font-size: 0.85rem;
}
.event-ts { color: var(--text-dim); font-size: 0.75rem; }
.event-body { margin-top: 0.2rem; }

/* Modal */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#modal-overlay[hidden] { display: none; }

#create-channel-modal {
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 100%;
  max-width: 400px;
}
#create-channel-modal h3 { color: var(--accent-light); margin-bottom: 1rem; }

#modal-members {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 1rem;
  max-height: 200px;
  overflow-y: auto;
}
#modal-members label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.4rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
}
#modal-members label:hover { background: var(--bg-hover); }

.modal-field { margin-bottom: 1rem; }
.modal-field label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-bottom: 0.3rem;
}
.modal-field input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
}
.modal-field input:focus { outline: none; border-color: var(--accent); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}
.modal-actions button {
  padding: 0.45rem 1rem;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  border: 1px solid var(--border);
}
#modal-cancel { background: var(--bg-hover); color: var(--text); }
#modal-cancel:hover { background: var(--bg-active); }
#modal-create { background: var(--accent); color: #fff; border-color: var(--accent); }
#modal-create:hover { background: var(--accent-light); }

/* Media messages */
.msg-media-img {
  max-width: 400px;
  max-height: 300px;
  border-radius: var(--radius);
  cursor: pointer;
  display: block;
  margin-top: 0.3rem;
}
.msg-media-img:hover { opacity: 0.9; }

.msg-media-video {
  max-width: 480px;
  max-height: 360px;
  border-radius: var(--radius);
  display: block;
  margin-top: 0.3rem;
}

.msg-media-audio {
  max-width: 400px;
  display: block;
  margin-top: 0.3rem;
}

.msg-filename {
  color: var(--text-dim);
  font-size: 0.75rem;
  display: block;
  margin-top: 0.15rem;
}

/* Upload progress */
#upload-progress {
  padding: 0.4rem 1rem;
  background: var(--bg-light);
  border-top: 1px solid var(--border);
  position: relative;
}
#upload-progress[hidden] { display: none; }

#upload-progress-fill {
  height: 4px;
  background: var(--accent);
  border-radius: 2px;
  width: 0%;
  transition: width 0.2s ease;
}

#upload-progress-text {
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-top: 0.15rem;
  display: block;
}

/* Attach button */
#attach-btn {
  padding: 0.55rem 0.75rem;
  background: var(--bg-hover);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
}
#attach-btn:hover { color: var(--text); border-color: var(--accent); }

/* Terminal view */
#terminals-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#terminal-tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.5rem 0.75rem;
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
}

.terminal-tab {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.7rem;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text-dim);
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.terminal-tab:hover { color: var(--text); background: var(--bg-hover); }
.terminal-tab.active {
  color: var(--text-bright);
  border-color: var(--accent);
  background: var(--bg-hover);
}

#terminal-container {
  flex: 1;
  background: #0f0f23;
  padding: 4px;
  overflow: hidden;
}

#terminal-container .xterm {
  height: 100%;
}

#terminal-status {
  padding: 0.5rem 1rem;
  background: var(--bg-light);
  border-top: 1px solid var(--border);
  color: var(--yellow);
  font-size: 0.8rem;
  flex-shrink: 0;
}
#terminal-status[hidden] { display: none; }

/* --- Responsive --- */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    top: 48px;
    left: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  #sidebar.open { transform: translateX(0); }

  .header-nav { gap: 0; }
  .nav-btn { padding: 0.3rem 0.5rem; font-size: 0.75rem; }
}

@media (min-width: 769px) {
  #sidebar-toggle { display: none; }
}
