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

:root {
  --bg: #0d1117;
  --surface: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --text-muted: #7d8590;
  --accent-claude: #a855f7;
  --accent-codex: #22d3ee;
  --accent-btn: #238636;
  --danger: #da3633;
  --radius: 6px;
  --sidebar-w: 220px;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-size: 14px; }

/* ── Login ── */
#login-screen {
  display: flex; align-items: center; justify-content: center; height: 100vh;
}
.login-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 32px; width: 320px; display: flex; flex-direction: column; gap: 12px;
}
.login-box h1 { font-size: 20px; text-align: center; margin-bottom: 8px; }
.login-box input {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: 8px 12px; font-size: 14px; width: 100%;
}
.login-box button {
  background: var(--accent-btn); border: none; border-radius: var(--radius);
  color: #fff; padding: 9px; font-size: 14px; cursor: pointer; font-weight: 600;
}
.login-box button:hover { opacity: 0.9; }
#login-error { color: var(--danger); font-size: 12px; text-align: center; min-height: 16px; }

/* ── Layout ── */
#app { display: flex; height: 100vh; overflow: hidden; }
.hidden { display: none !important; }

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-w); background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0; overflow-y: auto;
}
.sidebar-header {
  padding: 14px 12px; border-bottom: 1px solid var(--border); font-weight: 700; font-size: 15px;
}
.logo { letter-spacing: 0.5px; cursor: pointer; }
.logo:hover { opacity: 0.8; }
.sidebar-section { flex: 1; padding: 8px 0; }
.section-title {
  padding: 4px 12px 6px; font-size: 11px; font-weight: 600;
  text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.8px;
}
#session-list { list-style: none; }
#session-list li {
  padding: 6px 12px; cursor: pointer; display: flex; align-items: center;
  gap: 8px; border-radius: 0; transition: background 0.1s;
}
#session-list li:hover { background: rgba(255,255,255,0.05); }
#session-list li.active { background: rgba(255,255,255,0.08); }
.session-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot-claude { background: var(--accent-claude); }
.dot-codex  { background: var(--accent-codex); }
.dot-idle   { background: #3fb950; }
.dot-running { background: #f0a030; animation: pulse 1.2s infinite; }
.dot-error  { background: var(--danger); }
.dot-stopped { background: var(--text-muted); }
@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:0.4; } }
.session-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.session-type  { font-size: 10px; color: var(--text-muted); text-transform: uppercase; }
.session-actions { display: none; gap: 4px; }
#session-list li:hover .session-actions { display: flex; }
.session-actions button {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 14px; padding: 0 2px; line-height: 1;
}
.session-actions button:hover { color: var(--text); }

.sidebar-actions {
  padding: 10px; border-top: 1px solid var(--border); display: flex; gap: 6px;
}
.btn-new {
  flex: 1; background: var(--accent-btn); border: none; border-radius: var(--radius);
  color: #fff; padding: 7px 6px; font-size: 12px; cursor: pointer; font-weight: 600;
}
.btn-new:hover { opacity: 0.9; }
.btn-codex { background: #0e7490; }

/* ── Terminals area ── */
#terminals-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; position: relative; }
#terminals-grid {
  flex: 1; display: grid; gap: 4px; padding: 4px; overflow: hidden;
  grid-template-columns: 1fr;
}
#terminals-grid.cols-2 { grid-template-columns: 1fr 1fr; }
#terminals-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
#terminals-grid.cols-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }

.terminal-pane {
  background: #000; border: 1px solid var(--border); border-radius: var(--radius);
  display: flex; flex-direction: column; overflow: hidden; min-height: 0;
}
.terminal-pane.focused { border-color: var(--accent-claude); }
.terminal-header {
  background: var(--surface); padding: 5px 10px; display: flex; align-items: center;
  gap: 8px; font-size: 12px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.terminal-header .t-name { font-weight: 600; flex: 1; }
.terminal-header .t-type { color: var(--text-muted); font-size: 11px; }
.terminal-header .t-status { font-size: 11px; }
.status-idle    { color: #3fb950; }
.status-running { color: #f0a030; }
.status-waiting_approval { color: #f0e030; }
.status-error   { color: var(--danger); }
.status-stopped { color: var(--text-muted); }
.terminal-header .t-btn {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 13px; padding: 1px 4px; border-radius: 3px;
}
.terminal-header .t-btn:hover { background: rgba(255,255,255,0.1); color: var(--text); }
.t-btn.btn-stop { color: var(--text-muted); }
.t-btn.btn-resume { color: #3fb950; }
.attach-hint {
  font-size: 11px; color: var(--text-muted); cursor: pointer; padding: 0 4px;
  border-radius: 3px; border: 1px solid var(--border);
}
.attach-hint:hover { background: rgba(255,255,255,0.05); color: var(--text); }
.xterm-container { flex: 1; min-height: 0; overflow: hidden; padding: 4px; position: relative; }
.scroll-btn {
  position: absolute; bottom: 8px; right: 8px;
  background: rgba(255,255,255,0.15); border: none; border-radius: 50%;
  color: #e6edf3; width: 28px; height: 28px; font-size: 14px;
  cursor: pointer; display: none; z-index: 10; line-height: 1;
}
.scroll-btn.visible { display: block; }
.scroll-btn:hover { background: rgba(255,255,255,0.25); }

#empty-state {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); text-align: center; line-height: 1.8; pointer-events: none;
}

/* ── Message input ── */
.terminal-input-row {
  display: flex; gap: 6px; padding: 6px; border-top: 1px solid var(--border);
  background: var(--surface); flex-shrink: 0;
}
.terminal-input-row textarea {
  flex: 1; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); font-family: inherit; font-size: 13px; padding: 6px 10px;
  resize: none; min-height: 34px; max-height: 120px; line-height: 1.4; outline: none;
}
.terminal-input-row textarea:focus { border-color: #4d8ef0; }
.terminal-input-row button {
  background: var(--accent-btn); border: none; border-radius: var(--radius);
  color: #fff; font-size: 13px; font-weight: 600; padding: 0 14px; cursor: pointer;
  white-space: nowrap; flex-shrink: 0;
}
.terminal-input-row button:hover { opacity: 0.9; }
.terminal-input-row button:active { opacity: 0.75; }

/* ── Modal ── */
#modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 24px; width: 340px; display: flex; flex-direction: column; gap: 12px;
}
.modal-box h3 { font-size: 16px; }
.modal-box input {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: 8px 12px; font-size: 14px;
}
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions button {
  border: none; border-radius: var(--radius); padding: 8px 16px; cursor: pointer; font-size: 13px;
}
#modal-cancel  { background: var(--border); color: var(--text); }
#modal-create  { background: var(--accent-btn); color: #fff; font-weight: 600; }

/* ── Attach tooltip ── */
#attach-toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 16px; font-size: 12px; font-family: monospace; color: var(--accent-codex);
  z-index: 200; pointer-events: none; opacity: 0; transition: opacity 0.2s;
}
#attach-toast.show { opacity: 1; }

/* ── Stats button in sidebar header ── */
.sidebar-header { display: flex; align-items: center; justify-content: space-between; }
#stats-btn {
  background: none; border: none; cursor: pointer;
  padding: 4px 6px; border-radius: 4px; color: var(--text-muted);
  display: flex; align-items: center; flex-shrink: 0;
}
#stats-btn:hover { background: rgba(255,255,255,0.1); color: var(--text); }

/* ── Stats panel ── */
#stats-panel {
  position: absolute; top: 0; left: var(--sidebar-w); right: 0; bottom: 0;
  background: var(--bg); z-index: 20; overflow-y: auto;
  display: flex; flex-direction: column;
}
#app { position: relative; }
.stats-header {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.stats-title { font-weight: 700; font-size: 15px; }
.stats-week-label { font-size: 12px; color: var(--text-muted); flex: 1; }
#stats-close {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 16px; padding: 2px 6px; border-radius: 4px;
}
#stats-close:hover { background: rgba(255,255,255,0.1); color: var(--text); }

.stats-body { padding: 16px; display: flex; flex-direction: column; gap: 16px; }

.stats-cards { display: flex; gap: 12px; }
.stats-card {
  flex: 1; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px;
}
.stats-card-claude { border-left: 3px solid var(--accent-claude); }
.stats-card-codex  { border-left: 3px solid var(--accent-codex); }
.stats-card-label  { font-size: 11px; text-transform: uppercase; color: var(--text-muted); margin-bottom: 4px; letter-spacing: 0.8px; }
.stats-card-value  { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.stats-card-detail { font-size: 11px; color: var(--text-muted); }

.stats-chart-wrap {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 12px 8px;
}
.stats-chart-legend { display: flex; gap: 16px; margin-bottom: 6px; }
.legend-claude { font-size: 11px; color: var(--accent-claude); }
.legend-codex  { font-size: 11px; color: var(--accent-codex); }

.stats-sessions { display: flex; flex-direction: column; gap: 8px; }
.stats-group {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.stats-group-header {
  display: flex; align-items: center; gap: 8px; padding: 10px 14px;
  cursor: pointer; user-select: none;
}
.stats-group-header:hover { background: rgba(255,255,255,0.03); }
.stats-arrow { font-size: 10px; color: var(--text-muted); width: 10px; }
.stats-group-title { font-size: 13px; font-weight: 600; flex: 1; }
.stats-group-count { font-size: 12px; color: var(--text-muted); }

.stats-session-list { list-style: none; border-top: 1px solid var(--border); }
.stats-session-item {
  display: flex; align-items: baseline; gap: 8px; padding: 7px 14px;
  border-bottom: 1px solid rgba(48,54,61,0.5); font-size: 12px;
}
.stats-session-item:last-child { border-bottom: none; }
.stats-session-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-muted); }
.stats-session-tokens { font-weight: 600; white-space: nowrap; font-size: 12px; }
.stats-session-dur { color: var(--text-muted); white-space: nowrap; font-size: 11px; min-width: 36px; text-align: right; }

/* ── Mobile / Telegram Mini App ── */
#menu-btn {
  display: none; position: fixed; top: 8px; left: 8px; z-index: 50;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  width: 36px; height: 36px; font-size: 18px; cursor: pointer;
  align-items: center; justify-content: center;
}
@media (max-width: 600px) {
  :root { --sidebar-w: 180px; }
  #terminals-grid.cols-2,
  #terminals-grid.cols-3,
  #terminals-grid.cols-4 { grid-template-columns: 1fr; }
  #menu-btn { display: flex; }
  #sidebar { position: fixed; top: 0; left: 0; height: 100vh; z-index: 40; transform: translateX(-100%); transition: transform 0.2s; display: flex !important; }
  #sidebar.open { transform: translateX(0); }
  .sidebar-header { padding-left: 52px; }
  #stats-panel { left: 0; z-index: 30; }
  .stats-cards { flex-direction: column; }
}
