/* Telegram WebApp native theme */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #999999);
  --link: var(--tg-theme-link-color, #2678b6);
  --btn: var(--tg-theme-button-color, #3390ec);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f0f0f0);
  --header-bg: var(--tg-theme-header-bg-color, #3390ec);
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --destructive: var(--tg-theme-destructive-text-color, #e53935);
  --subtitle: var(--tg-theme-subtitle-text-color, var(--hint));
  --section-bg: var(--tg-theme-section-bg-color, var(--bg));
  --section-header: var(--tg-theme-section-header-text-color, var(--accent));
  --section-sep: var(--tg-theme-section-separator-color, #e0e0e0);
  --radius: 12px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  overflow-x: hidden;
  min-height: 100vh;
  padding-bottom: 64px;
}

/* --- Auth screen --- */
#auth-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
  text-align: center;
}
#auth-screen .logo { font-size: 48px; margin-bottom: 16px; }
#auth-screen h1 { font-size: 22px; margin-bottom: 8px; }
#auth-screen p { color: var(--hint); font-size: 14px; }
#auth-screen .error { color: var(--destructive); margin-top: 12px; }

/* --- Tab bar --- */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--section-bg);
  border-top: 1px solid var(--section-sep);
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.tab-bar button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px;
  background: none;
  border: none;
  color: var(--hint);
  font-size: 10px;
  cursor: pointer;
  transition: color 0.15s;
}
.tab-bar button.active { color: var(--accent); }
.tab-bar button svg { width: 24px; height: 24px; }

/* --- Tab content --- */
.tab-content { display: none; padding: 12px; }
.tab-content.active { display: block; }

/* --- Cards --- */
.card {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 8px;
  border: 1px solid var(--section-sep);
  cursor: pointer;
  transition: background 0.15s;
}
.card:active { opacity: 0.7; }

.card-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.card-subtitle { color: var(--subtitle); font-size: 13px; }
.card-meta { color: var(--hint); font-size: 12px; margin-top: 4px; display: flex; gap: 12px; flex-wrap: wrap; }

/* --- Badges --- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}
.badge-active { background: #e8f5e9; color: #2e7d32; }
.badge-idle { background: #fff3e0; color: #e65100; }
.badge-enabled { background: #e8f5e9; color: #2e7d32; }
.badge-disabled { background: #ffebee; color: #c62828; }
.badge-model { background: var(--secondary-bg); color: var(--subtitle); }

/* --- Chat view --- */
.chat-view {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 200;
  display: none;
  flex-direction: column;
}
.chat-view.open { display: flex; }

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--section-bg);
  border-bottom: 1px solid var(--section-sep);
  min-height: 52px;
}
.chat-header .back-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
}
.chat-header .title { font-weight: 600; font-size: 16px; flex: 1; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg-bubble {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
}
.msg-sender {
  align-self: flex-start;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  padding: 0 4px;
  margin-top: 4px;
}
.msg-user {
  align-self: flex-start;
  background: var(--secondary-bg);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.msg-assistant {
  align-self: flex-end;
  background: var(--btn);
  color: var(--btn-text);
  border-bottom-right-radius: 4px;
}
.msg-tool {
  align-self: flex-start;
  background: #fff8e1;
  color: #5d4037;
  font-family: monospace;
  font-size: 12px;
  border-radius: 8px;
  opacity: 0.8;
}
.msg-system {
  align-self: center;
  color: var(--hint);
  font-size: 12px;
  text-align: center;
  padding: 4px 12px;
}

.chat-send-mode {
  display: flex;
  gap: 0;
  padding: 4px 12px 0;
  background: var(--section-bg);
  border-top: 1px solid var(--section-sep);
}
.send-mode-btn {
  flex: 1;
  padding: 5px 8px;
  border: 1px solid var(--section-sep);
  background: var(--bg);
  color: var(--hint);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.send-mode-btn:first-child { border-radius: 8px 0 0 8px; }
.send-mode-btn:last-child { border-radius: 0 8px 8px 0; }
.send-mode-btn.active { background: var(--btn); color: var(--btn-text); border-color: var(--btn); }
.chat-input-bar {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: var(--section-bg);
  padding-bottom: max(8px, env(safe-area-inset-bottom, 0));
}
.chat-input-bar textarea {
  flex: 1;
  border: 1px solid var(--section-sep);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  resize: none;
  min-height: 38px;
  max-height: 120px;
  font-family: inherit;
  line-height: 1.3;
}
.chat-input-bar button {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--btn);
  color: var(--btn-text);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* --- File tree --- */
.file-tree { list-style: none; }
.file-tree .dir > .dir-label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}
.file-tree .dir > .dir-label::before { content: '\25B6'; font-size: 10px; transition: transform 0.2s; }
.file-tree .dir.open > .dir-label::before { transform: rotate(90deg); }
.file-tree .dir > ul { display: none; padding-left: 16px; }
.file-tree .dir.open > ul { display: block; }

.file-tree .file-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 8px 20px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.15s;
}
.file-tree .file-item:active { background: var(--secondary-bg); }
.file-meta { color: var(--hint); font-size: 11px; }

/* --- File editor --- */
.file-editor {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 200;
  display: none;
  flex-direction: column;
}
.file-editor.open { display: flex; }

.editor-textarea {
  flex: 1;
  width: 100%;
  border: none;
  padding: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  resize: none;
}

.editor-actions {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: var(--section-bg);
  border-top: 1px solid var(--section-sep);
}
.editor-actions button {
  flex: 1;
  padding: 10px;
  border-radius: 10px;
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn-save { background: var(--btn); color: var(--btn-text); }
.btn-cancel { background: var(--secondary-bg); color: var(--text); }

/* --- Live feed --- */
.live-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  align-items: center;
}
.live-controls select {
  flex: 1;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--section-sep);
  background: var(--section-bg);
  color: var(--text);
  font-size: 14px;
}
.live-controls label {
  font-size: 13px;
  color: var(--hint);
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.live-feed {
  background: #1e1e1e;
  color: #d4d4d4;
  border-radius: var(--radius);
  padding: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 12px;
  line-height: 1.5;
  min-height: 300px;
  max-height: calc(100vh - 180px);
  overflow-y: auto;
  word-break: break-all;
}
.live-feed .event-line { margin-bottom: 4px; }
.live-feed .event-type { color: #569cd6; }
.live-feed .event-session { color: #ce9178; }
.live-feed .event-time { color: #6a9955; }
.live-feed .event-data { color: #dcdcaa; }
.live-feed .event-text { color: #d4d4d4; }
.live-feed .event-tool { color: #c586c0; }
.live-feed .event-error { color: #f44747; }

/* --- Section headers --- */
.section-header {
  font-size: 13px;
  font-weight: 600;
  color: var(--section-header);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 16px 4px 8px;
}

/* --- Loading / empty state --- */
.loading, .empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: var(--hint);
  font-size: 14px;
  text-align: center;
}
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--section-sep);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Status bar --- */
.status-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
  padding: 6px 12px;
  font-size: 12px;
  text-align: center;
  transition: all 0.3s;
}
.status-bar.connected { background: #e8f5e9; color: #2e7d32; }
.status-bar.disconnected { background: #ffebee; color: #c62828; }
.status-bar.hidden { transform: translateY(-100%); }

/* --- Cron card extras --- */
.cron-schedule { font-family: monospace; font-size: 13px; color: var(--accent); }
.cron-times { display: flex; gap: 16px; margin-top: 4px; }
.cron-times span { font-size: 12px; color: var(--hint); }

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-thumb { background: var(--hint); border-radius: 2px; }
