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

:root {
  --bg: #0d1117;
  --surface: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #58a6ff;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --blue: #58a6ff;
  --status-idle: #30363d;
  --status-running: #1f6feb;
  --status-done: #238636;
  --status-stuck: #da3633;
  --titlebar-height: 28px;
  --dock-height: 32px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* =============================================
   APP LAYOUT: header, grid-area, dock, status
   ============================================= */

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* --- Header --- */

.header {
  flex: 0 0 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header h1 {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.header h1 span { color: var(--accent); }

.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* --- Tmux tab bar --- */

.tmux-tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: 16px;
  flex: 1;
  overflow-x: auto;
}

.tmux-tab {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
}

.tmux-tab:hover {
  background: var(--border);
  color: var(--text);
}

.tmux-tab.active {
  background: var(--bg);
  color: var(--text);
  border-color: var(--border);
}

.tmux-tab .tab-close {
  display: none;
  width: 14px;
  height: 14px;
  font-size: 12px;
  line-height: 14px;
  text-align: center;
  border-radius: 2px;
  color: var(--text-muted);
  cursor: pointer;
  border: none;
  background: none;
  padding: 0;
  font-family: inherit;
}

.tmux-tab:hover .tab-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tmux-tab .tab-close:hover {
  background: var(--red);
  color: #fff;
}

.tmux-tab-new {
  padding: 4px 8px;
  font-size: 14px;
  color: var(--text-muted);
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
}

.tmux-tab-new:hover {
  background: var(--border);
  color: var(--text);
  border-style: solid;
}

/* =============================================
   GRID AREA - window manager grid
   ============================================= */

.grid-area {
  flex: 1;
  display: grid;
  overflow: hidden;
  gap: 1px;
  background: var(--border);
  position: relative;
}

/* Grid layout classes set by JS */

/* All 3 normal: desktop top-left, info top-right, terminal bottom spanning both */
.grid-area.layout-all {
  grid-template-areas:
    "desktop info"
    "terminal terminal";
  grid-template-rows: 45% 1fr;
  grid-template-columns: 1fr 1fr;
}

/* No desktop */
.grid-area.layout-no-desktop {
  grid-template-areas:
    "info"
    "terminal";
  grid-template-rows: 40% 1fr;
  grid-template-columns: 1fr;
}

/* No info */
.grid-area.layout-no-info {
  grid-template-areas:
    "desktop"
    "terminal";
  grid-template-rows: 40% 1fr;
  grid-template-columns: 1fr;
}

/* No terminal */
.grid-area.layout-no-terminal {
  grid-template-areas:
    "desktop info";
  grid-template-rows: 1fr;
  grid-template-columns: 1fr 1fr;
}

/* Only terminal */
.grid-area.layout-terminal-only {
  grid-template-areas: "terminal";
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
}

/* Only desktop */
.grid-area.layout-desktop-only {
  grid-template-areas: "desktop";
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
}

/* Only info */
.grid-area.layout-info-only {
  grid-template-areas: "info";
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
}

/* Only desktop + info (no terminal) - same as no-terminal */

/* Empty state - no normal panels */
.grid-area.layout-empty {
  grid-template-areas: "empty";
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
}

/* =============================================
   PANELS
   ============================================= */

.panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  min-width: 0;
  min-height: 0;
}

/* Grid area assignments */
.panel[data-panel="desktop"] { grid-area: desktop; }
.panel[data-panel="info"]    { grid-area: info; }
.panel[data-panel="terminal"]{ grid-area: terminal; }

/* Panel states */
.panel[data-state="minimized"],
.panel[data-state="closed"],
.panel[data-state="docked"] {
  display: none !important;
}

.panel[data-state="expanded"] {
  grid-area: 1 / 1 / -1 / -1;
  z-index: 10;
}

/* --- Panel title bar --- */

.panel-titlebar {
  flex: 0 0 var(--titlebar-height);
  display: flex;
  align-items: center;
  padding: 0 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 4px;
  user-select: none;
}

.panel-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-right: 4px;
  white-space: nowrap;
}

.panel-spacer {
  flex: 1;
}

.titlebar-btn {
  width: 22px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: 1px solid transparent;
  border-radius: 3px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
}

.titlebar-btn:hover {
  background: var(--border);
  color: var(--text);
}

.titlebar-btn-close:hover {
  background: var(--red);
  color: #fff;
}

/* --- Info panel tabs (in titlebar) --- */

.info-tabs {
  display: flex;
  gap: 0;
  margin-left: 4px;
}

.info-tab {
  padding: 2px 10px;
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  border: none;
  background: none;
  border-bottom: 2px solid transparent;
  font-family: inherit;
  font-weight: 500;
}

.info-tab:hover { color: var(--text); }

.info-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* --- Info tab content --- */

.info-tab-content {
  display: none;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

.info-tab-content.active {
  display: flex;
  flex-direction: column;
}

/* --- Panel body --- */

.panel-body {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* =============================================
   EMPTY STATE
   ============================================= */

.empty-state {
  grid-area: 1 / 1 / -1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-muted);
  font-size: 14px;
  background: var(--bg);
}

.empty-shortcuts {
  font-size: 12px;
  color: var(--text-muted);
  opacity: 0.7;
}

/* =============================================
   DOCK BAR
   ============================================= */

.dock-bar {
  flex: 0 0 var(--dock-height);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 6px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.dock-btn {
  padding: 4px 12px;
  font-size: 11px;
  font-weight: 500;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  font-family: inherit;
  transition: border-color 0.15s, color 0.15s;
}

.dock-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

/* =============================================
   STATUS BAR
   ============================================= */

.status-bar {
  flex: 0 0 36px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  font-size: 12px;
  border-top: 3px solid var(--status-idle);
  background: var(--surface);
  gap: 16px;
  transition: border-color 0.3s;
}

.status-bar.running { border-top-color: var(--status-running); }
.status-bar.done    { border-top-color: var(--status-done); }
.status-bar.stuck   { border-top-color: var(--status-stuck); }

.status-text { color: var(--text-muted); }
.status-text.active { color: var(--text); }
.status-timer { color: var(--text-muted); font-family: monospace; }
.shortcuts-bar { flex: 1; text-align: right; color: var(--text-muted); font-size: 11px; opacity: 0.6; }

/* =============================================
   VNC CONTAINER - aspect-ratio with black bars
   ============================================= */

.vnc-container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

.vnc-container iframe {
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 16 / 9;
  border: none;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* =============================================
   TERMINAL CONTAINER
   ============================================= */

#terminal-container {
  width: 100%;
  height: 100%;
  background: var(--bg);
}

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

/* =============================================
   EVENT LOG
   ============================================= */

.event-log {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
  font-size: 12px;
  line-height: 1.5;
  background: var(--bg);
}

.event-line {
  padding: 2px 0;
  border-bottom: 1px solid #1c2128;
}

.event-line .time  { color: var(--text-muted); }
.event-line .tool  { color: var(--yellow); }
.event-line .file  { color: var(--green); }
.event-line .error { color: var(--red); }
.event-line .text  { color: var(--text); }

/* =============================================
   TASKS PANEL
   ============================================= */

.tasks-panel {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.task-input {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.task-input textarea {
  width: 100%;
  min-height: 80px;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  resize: vertical;
}

.task-input textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.task-input-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.templates {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.template-btn {
  padding: 4px 10px;
  font-size: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text-muted);
  cursor: pointer;
  font-family: inherit;
}

.template-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

/* --- Buttons --- */

.btn {
  padding: 8px 16px;
  border-radius: 6px;
  border: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover { opacity: 0.9; }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-danger {
  background: var(--red);
  color: #fff;
}

select {
  padding: 6px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 12px;
  font-family: inherit;
}

/* --- Task list --- */

.task-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.task-item {
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}

.task-item:hover { border-color: var(--accent); }

.task-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.task-status {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
}

.task-status.queued  { background: var(--status-idle); color: var(--text-muted); }
.task-status.running { background: var(--status-running); color: #fff; }
.task-status.done    { background: var(--status-done); color: #fff; }
.task-status.stuck   { background: var(--status-stuck); color: #fff; }
.task-status.failed  { background: var(--status-stuck); color: #fff; }

.task-prompt {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.task-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* --- Intervention panel --- */

.intervention {
  padding: 16px;
  background: #1c1210;
  border: 1px solid var(--red);
  border-radius: 8px;
}

.intervention h3 {
  color: var(--red);
  font-size: 14px;
  margin-bottom: 8px;
}

.intervention textarea {
  width: 100%;
  min-height: 60px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  margin-bottom: 8px;
}

.intervention-actions {
  display: flex;
  gap: 8px;
}

/* =============================================
   LOGIN OVERLAY
   ============================================= */

.login-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: 360px;
  text-align: center;
}

.login-box h2 {
  margin-bottom: 8px;
  font-size: 20px;
}

.login-box p {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 24px;
}

.login-box input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 14px;
  margin-bottom: 16px;
  font-family: inherit;
}

.login-box .error-msg {
  color: var(--red);
  font-size: 13px;
  margin-bottom: 12px;
}

/* =============================================
   SETTINGS MODAL
   ============================================= */

.settings-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 2px 6px;
  font-family: inherit;
}

.settings-btn:hover {
  background: var(--border);
  color: var(--text);
}

.settings-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.settings-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 400px;
}

.settings-box h3 {
  font-size: 16px;
  margin-bottom: 16px;
}

.settings-check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  font-size: 14px;
  cursor: pointer;
}

.settings-check input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.settings-token-row {
  padding: 0 0 8px 24px;
}

.settings-token-row input {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  font-family: 'SF Mono', 'Consolas', monospace;
}

.settings-token-row input:focus {
  outline: none;
  border-color: var(--accent);
}

.settings-setup-row {
  padding: 8px 0 8px 24px;
}

.settings-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin: 4px 0 2px;
}

.settings-cmd {
  display: block;
  padding: 4px 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 12px;
  font-family: 'SF Mono', 'Consolas', monospace;
  color: var(--accent);
  margin: 2px 0 6px;
  user-select: all;
}

.settings-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  justify-content: flex-end;
}

/* =============================================
   NOTIFICATION
   ============================================= */

.notification-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
  display: none;
}

.notification-dot.visible { display: inline-block; }
